diff --git a/cmd/commands/utils_test.go b/cmd/commands/utils_test.go index c530b04a14..012431e35f 100644 --- a/cmd/commands/utils_test.go +++ b/cmd/commands/utils_test.go @@ -1,12 +1,29 @@ package commands import ( + "encoding/hex" "testing" "github.com/stretchr/testify/assert" "github.com/tendermint/basecoin/types" ) +func TestHex(t *testing.T) { + + //test isHex + hexNoPrefix := hex.EncodeToString([]byte("foobar")) + hexWPrefix := "0x" + hexNoPrefix + str := "foobar" + strWPrefix := "0xfoobar" + assert.True(t, isHex(hexWPrefix), "isHex not identifying hex with 0x prefix") + assert.True(t, !isHex(hexNoPrefix), "isHex shouldn't identify hex without 0x prefix") + assert.True(t, !isHex(str), "isHex shouldn't identify non-hex string") + assert.True(t, !isHex(strWPrefix), "isHex shouldn't identify non-hex string with 0x prefix") + + //test strip hex + assert.True(t, StripHex(hexWPrefix) == hexNoPrefix, "StripHex doesn't remove first two characters") +} + //Test the parse coin and parse coins functionality func TestParse(t *testing.T) { diff --git a/plugins/counter/counter.go b/plugins/counter/counter.go index 9d849d1d05..79694c4052 100644 --- a/plugins/counter/counter.go +++ b/plugins/counter/counter.go @@ -35,7 +35,7 @@ func New() *CounterPlugin { return &CounterPlugin{} } -func (cp *CounterPlugin) SetOption(store types.KVStore, key string, value string) (log string) { +func (cp *CounterPlugin) SetOption(store types.KVStore, key, value string) (log string) { return "" } diff --git a/state/state.go b/state/state.go index 026c6a9ad9..385c17dc9b 100644 --- a/state/state.go +++ b/state/state.go @@ -38,7 +38,7 @@ func (s *State) GetChainID() string { } func (s *State) Get(key []byte) (value []byte) { - if s.readCache != nil { + if s.readCache != nil { //if not a cachewrap value, ok := s.readCache[string(key)] if ok { return value @@ -48,7 +48,7 @@ func (s *State) Get(key []byte) (value []byte) { } func (s *State) Set(key []byte, value []byte) { - if s.readCache != nil { + if s.readCache != nil { //if not a cachewrap s.readCache[string(key)] = value } s.store.Set(key, value) @@ -78,8 +78,14 @@ func (s *State) CacheSync() { } func (s *State) Commit() abci.Result { - s.readCache = make(map[string][]byte) - return s.store.(*eyes.Client).CommitSync() + switch s.store.(type) { + case *eyes.Client: + s.readCache = make(map[string][]byte) + return s.store.(*eyes.Client).CommitSync() + default: + return abci.NewError(abci.CodeType_InternalError, "can only use commit is store is merkleeyes") + } + } //---------------------------------------- diff --git a/state/state_test.go b/state/state_test.go new file mode 100644 index 0000000000..f1feac492e --- /dev/null +++ b/state/state_test.go @@ -0,0 +1,68 @@ +package state + +import ( + "bytes" + "testing" + + "github.com/tendermint/basecoin/types" + eyes "github.com/tendermint/merkleeyes/client" + + "github.com/stretchr/testify/assert" +) + +func TestState(t *testing.T) { + + s := NewState(types.NewMemKVStore()) + + s.SetChainID("testchain") + assert.True(t, s.GetChainID() == "testchain", "ChainID is improperly stored") + + setRecords := func(kv types.KVStore) { + kv.Set([]byte("foo"), []byte("snake")) + kv.Set([]byte("bar"), []byte("mouse")) + } + + setRecords(s) + assert.True(t, bytes.Equal(s.Get([]byte("foo")), []byte("snake")), "state doesn't retrieve after Set") + assert.True(t, bytes.Equal(s.Get([]byte("bar")), []byte("mouse")), "state doesn't retrieve after Set") + + // Test account retrieve + dumAddr := []byte("dummyAddress") + + acc := &types.Account{ + PubKey: nil, + Sequence: 1, + Balance: nil, + } + + s.SetAccount(dumAddr, acc) + assert.True(t, s.GetAccount(dumAddr).Sequence == 1, "GetAccount not retrieving") + + //Test CacheWrap with local mem store + store := types.NewMemKVStore() + s = NewState(store) + cache := s.CacheWrap() + setRecords(cache) + assert.True(t, !bytes.Equal(store.Get([]byte("foo")), []byte("snake")), "store retrieving before Commit") + assert.True(t, !bytes.Equal(store.Get([]byte("bar")), []byte("mouse")), "store retrieving before Commit") + cache.CacheSync() + assert.True(t, bytes.Equal(store.Get([]byte("foo")), []byte("snake")), "store doesn't retrieve after Commit") + assert.True(t, bytes.Equal(store.Get([]byte("bar")), []byte("mouse")), "store doesn't retrieve after Commit") + + //Test Commit on state with non-merkle store + assert.True(t, !s.Commit().IsOK(), "Commit shouldn't work with non-merkle store") + + //Test CacheWrap with merkleeyes client store + eyesCli := eyes.NewLocalClient("", 0) + s = NewState(eyesCli) + + cache = s.CacheWrap() + setRecords(cache) + assert.True(t, !bytes.Equal(eyesCli.Get([]byte("foo")), []byte("snake")), "store retrieving before Commit") + assert.True(t, !bytes.Equal(eyesCli.Get([]byte("bar")), []byte("mouse")), "store retrieving before Commit") + cache.CacheSync() + assert.True(t, s.Commit().IsOK(), "Bad Commit") + assert.True(t, bytes.Equal(eyesCli.Get([]byte("foo")), []byte("snake")), "store doesn't retrieve after Commit") + assert.True(t, bytes.Equal(eyesCli.Get([]byte("bar")), []byte("mouse")), "store doesn't retrieve after Commit") + +} diff --git a/types/account.go b/types/account.go index d78f560a6e..d1e62d8321 100644 --- a/types/account.go +++ b/types/account.go @@ -13,6 +13,9 @@ type Account struct { } func (acc *Account) Copy() *Account { + if acc == nil { + return nil + } accCopy := *acc return &accCopy } diff --git a/types/account_test.go b/types/account_test.go new file mode 100644 index 0000000000..771e7f07c6 --- /dev/null +++ b/types/account_test.go @@ -0,0 +1,28 @@ +package types + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestAccount(t *testing.T) { + + acc := Account{ + PubKey: nil, + Sequence: 0, + Balance: nil, + } + + //test Copy + accCopy := acc.Copy() + accCopy.Sequence = 1 + t.Log(acc.Sequence) + t.Log(accCopy.Sequence) + assert.True(t, acc.Sequence != accCopy.Sequence, "Account Copy Error") + + //test sending nils for panic + var nilAcc *Account + nilAcc.String() + nilAcc.Copy() +} diff --git a/types/coin_test.go b/types/coin_test.go index 158f159c50..7a3fc2ecdd 100644 --- a/types/coin_test.go +++ b/types/coin_test.go @@ -19,6 +19,11 @@ func TestCoins(t *testing.T) { t.Fatalf("Expected coins to be positive: %v", coins) } + emptyCoins := Coins{Coin{"GOLD", 0}} + if !coins.IsGTE(emptyCoins) { + t.Fatalf("Expected %v to be >= %v", coins, emptyCoins) + } + negCoins := coins.Negative() if negCoins.IsPositive() { t.Fatalf("Expected neg coins to not be positive: %v", negCoins) diff --git a/types/kvstore.go b/types/kvstore.go index 96f8c7d0ff..15088bdfb3 100644 --- a/types/kvstore.go +++ b/types/kvstore.go @@ -113,6 +113,7 @@ func (kvc *KVCache) Get(key []byte) (value []byte) { } } +//Update the store with the values from the cache func (kvc *KVCache) Sync() { for e := kvc.keys.Front(); e != nil; e = e.Next() { key := e.Value.([]byte) diff --git a/types/kvstore_test.go b/types/kvstore_test.go new file mode 100644 index 0000000000..5c40a2597b --- /dev/null +++ b/types/kvstore_test.go @@ -0,0 +1,59 @@ +package types + +import ( + "bytes" + "fmt" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestMemKVStore(t *testing.T) { + + ms := NewMemKVStore() + ms.Set([]byte("foo"), []byte("snake")) + ms.Set([]byte("bar"), []byte("mouse")) + assert.True(t, bytes.Equal(ms.Get([]byte("foo")), []byte("snake")), "MemKVStore doesn't retrieve after Set") + assert.True(t, bytes.Equal(ms.Get([]byte("bar")), []byte("mouse")), "MemKVStore doesn't retrieve after Set") +} + +func TestKVCache(t *testing.T) { + + store := NewMemKVStore() + kvc := NewKVCache(store) + + setRecords := func() { + kvc.Set([]byte("foo"), []byte("snake")) + kvc.Set([]byte("bar"), []byte("mouse")) + } + + //test read/write + setRecords() + assert.True(t, bytes.Equal(kvc.Get([]byte("foo")), []byte("snake")), "KVCache doesn't retrieve after Set") + assert.True(t, bytes.Equal(kvc.Get([]byte("bar")), []byte("mouse")), "KVCache doesn't retrieve after Set") + + //test reset + kvc.Reset() + assert.True(t, !bytes.Equal(kvc.Get([]byte("foo")), []byte("snake")), "KVCache retrieving after reset") + assert.True(t, !bytes.Equal(kvc.Get([]byte("bar")), []byte("mouse")), "KVCache retrieving after reset") + + //test sync + setRecords() + assert.True(t, !bytes.Equal(store.Get([]byte("foo")), []byte("snake")), "store retrieving before synced") + assert.True(t, !bytes.Equal(store.Get([]byte("bar")), []byte("mouse")), "store retrieving before synced") + kvc.Sync() + assert.True(t, bytes.Equal(store.Get([]byte("foo")), []byte("snake")), "store isn't retrieving after synced") + assert.True(t, bytes.Equal(store.Get([]byte("bar")), []byte("mouse")), "store isn't retrieving after synced") + + //test logging + assert.True(t, len(kvc.GetLogLines()) == 0, "logging events existed before using SetLogging") + fmt.Println(len(kvc.GetLogLines())) + + kvc.SetLogging() + setRecords() + assert.True(t, len(kvc.GetLogLines()) == 2, "incorrect number of logging events recorded") + + kvc.ClearLogLines() + assert.True(t, len(kvc.GetLogLines()) == 0, "logging events still exists after ClearLogLines") + +} diff --git a/types/plugin.go b/types/plugin.go index ac95cac679..9783f2c7c4 100644 --- a/types/plugin.go +++ b/types/plugin.go @@ -15,7 +15,7 @@ type Plugin interface { RunTx(store KVStore, ctx CallContext, txBytes []byte) (res abci.Result) // Other ABCI message handlers - SetOption(store KVStore, key string, value string) (log string) + SetOption(store KVStore, key, value string) (log string) InitChain(store KVStore, vals []*abci.Validator) BeginBlock(store KVStore, hash []byte, header *abci.Header) EndBlock(store KVStore, height uint64) abci.ResponseEndBlock diff --git a/types/plugin_test.go b/types/plugin_test.go new file mode 100644 index 0000000000..21cd92114d --- /dev/null +++ b/types/plugin_test.go @@ -0,0 +1,42 @@ +package types + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + abci "github.com/tendermint/abci/types" +) + +//---------------------------------- + +type Dummy struct{} + +func (d *Dummy) Name() string { + return "dummy" +} +func (d *Dummy) RunTx(store KVStore, ctx CallContext, txBytes []byte) (res abci.Result) { + return +} +func (d *Dummy) SetOption(storei KVStore, key, value string) (log string) { + return "" +} +func (d *Dummy) InitChain(store KVStore, vals []*abci.Validator) { +} +func (d *Dummy) BeginBlock(store KVStore, hash []byte, header *abci.Header) { +} +func (d *Dummy) EndBlock(store KVStore, height uint64) (res abci.ResponseEndBlock) { + return +} + +//---------------------------------- + +func TestPlugin(t *testing.T) { + + plugins := NewPlugins() + assert.True(t, len(plugins.GetList()) == 0, "plugins object init with a objects") + plugins.RegisterPlugin(&Dummy{}) + assert.True(t, len(plugins.GetList()) == 1, "plugin wasn't added to plist after registered") + dum := plugins.GetByName("dummy") + assert.True(t, dum.Name() == "dummy", "plugin wasn't retrieved properly with GetByName") +}