Fixed app tests for state spaces

This commit is contained in:
Ethan Frey 2017-07-11 15:21:02 +02:00
parent 771c08483e
commit 66d1f86098
2 changed files with 27 additions and 3 deletions

View File

@ -5,6 +5,7 @@ import (
"os"
"testing"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@ -13,6 +14,7 @@ import (
"github.com/tendermint/basecoin/modules/auth"
"github.com/tendermint/basecoin/modules/base"
"github.com/tendermint/basecoin/modules/coin"
"github.com/tendermint/basecoin/stack"
"github.com/tendermint/basecoin/state"
wire "github.com/tendermint/go-wire"
eyes "github.com/tendermint/merkleeyes/client"
@ -82,8 +84,16 @@ func (at *appTest) reset() {
}
func getBalance(key basecoin.Actor, state state.KVStore) (coin.Coins, error) {
acct, err := coin.NewAccountant("").GetAccount(state, key)
return acct.Coins, err
var acct coin.Account
k := stack.PrefixedKey(coin.NameCoin, key.Bytes())
v := state.Get(k)
// empty if no data
if len(v) == 0 {
return nil, nil
}
// otherwise read it
err := wire.ReadBinaryBytes(v, &acct)
return acct.Coins, errors.WithStack(err)
}
func getAddr(addr []byte, state state.KVStore) (coin.Coins, error) {

View File

@ -26,6 +26,20 @@ func stateSpace(store state.KVStore, app string) state.KVStore {
store = pstore.store
}
// wrap it with the prefix
prefix := append([]byte(app), byte(0))
prefix := makePrefix(app)
return prefixStore{prefix, store}
}
func makePrefix(app string) []byte {
return append([]byte(app), byte(0))
}
// PrefixedKey gives us the absolute path to a key that is embedded in an
// application-specific state-space.
//
// This is useful for tests or utilities that have access to the global
// state to check individual app spaces. Individual apps should not be able
// to use this to read each other's space
func PrefixedKey(app string, key []byte) []byte {
return append(makePrefix(app), key...)
}