diff --git a/app/app_test.go b/app/app_test.go index 38aa990cd..9656190c8 100644 --- a/app/app_test.go +++ b/app/app_test.go @@ -5,7 +5,6 @@ import ( "os" "testing" - "github.com/pkg/errors" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -83,17 +82,10 @@ func (at *appTest) reset() { require.True(at.t, resabci.IsOK(), resabci) } -func getBalance(key basecoin.Actor, state state.KVStore) (coin.Coins, error) { - 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 getBalance(key basecoin.Actor, store state.KVStore) (coin.Coins, error) { + cspace := stack.PrefixedStore(coin.NameCoin, store) + acct, err := coin.GetAccount(cspace, key) + return acct.Coins, err } func getAddr(addr []byte, state state.KVStore) (coin.Coins, error) { diff --git a/cmd/basecli/commands/query.go b/cmd/basecli/commands/query.go index aba5fbf55..3d5b4fb06 100644 --- a/cmd/basecli/commands/query.go +++ b/cmd/basecli/commands/query.go @@ -13,6 +13,7 @@ import ( "github.com/tendermint/basecoin/modules/auth" "github.com/tendermint/basecoin/modules/coin" + "github.com/tendermint/basecoin/stack" ) // AccountQueryCmd - command to query an account @@ -27,7 +28,7 @@ func doAccountQuery(cmd *cobra.Command, args []string) error { if err != nil { return err } - key := coin.NewAccountant("").MakeKey(auth.SigPerm(addr)) + key := stack.PrefixedKey(coin.NameCoin, auth.SigPerm(addr).Bytes()) acc := coin.Account{} proof, err := proofcmd.GetAndParseAppProof(key, &acc) diff --git a/docs/guide/counter/cmd/countercli/commands/query.go b/docs/guide/counter/cmd/countercli/commands/query.go index be0c50265..489a8a01d 100644 --- a/docs/guide/counter/cmd/countercli/commands/query.go +++ b/docs/guide/counter/cmd/countercli/commands/query.go @@ -6,6 +6,7 @@ import ( proofcmd "github.com/tendermint/light-client/commands/proofs" "github.com/tendermint/basecoin/docs/guide/counter/plugins/counter" + "github.com/tendermint/basecoin/stack" ) //CounterQueryCmd - CLI command to query the counter state @@ -16,7 +17,7 @@ var CounterQueryCmd = &cobra.Command{ } func counterQueryCmd(cmd *cobra.Command, args []string) error { - key := counter.StateKey() + key := stack.PrefixedKey(counter.NameCounter, counter.StateKey()) var cp counter.State proof, err := proofcmd.GetAndParseAppProof(key, &cp) diff --git a/docs/guide/counter/plugins/counter/counter.go b/docs/guide/counter/plugins/counter/counter.go index 0bf38f362..168886160 100644 --- a/docs/guide/counter/plugins/counter/counter.go +++ b/docs/guide/counter/plugins/counter/counter.go @@ -195,7 +195,7 @@ type State struct { // StateKey - store key for the counter state func StateKey() []byte { - return []byte(NameCounter + "/state") + return []byte("state") } // LoadState - retrieve the counter state from the store diff --git a/stack/prefixstore.go b/stack/prefixstore.go index cc0c5807f..f6934d6e7 100644 --- a/stack/prefixstore.go +++ b/stack/prefixstore.go @@ -20,26 +20,35 @@ func (p prefixStore) Get(key []byte) (value []byte) { } // stateSpace will unwrap any prefixStore and then add the prefix +// +// this can be used by the middleware and dispatcher to isolate one space, +// then unwrap and isolate another space func stateSpace(store state.KVStore, app string) state.KVStore { // unwrap one-level if wrapped if pstore, ok := store.(prefixStore); ok { store = pstore.store } - // wrap it with the prefix - prefix := makePrefix(app) + return PrefixedStore(app, store) +} + +// PrefixedStore allows one to create an isolated state-space for a given +// app prefix, but it cannot easily be unwrapped +// +// 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 PrefixedStore(app string, store state.KVStore) state.KVStore { + prefix := append([]byte(app), byte(0)) 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. +// PrefixedKey returns the absolute path to a given key in a particular +// app's 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...) + prefix := append([]byte(app), byte(0)) + return append(prefix, key...) }