Update cli to properly query into app state-space

This commit is contained in:
Ethan Frey 2017-07-11 15:35:43 +02:00
parent 66d1f86098
commit bb61b9fca3
5 changed files with 27 additions and 24 deletions

View File

@ -5,7 +5,6 @@ import (
"os" "os"
"testing" "testing"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -83,17 +82,10 @@ func (at *appTest) reset() {
require.True(at.t, resabci.IsOK(), resabci) require.True(at.t, resabci.IsOK(), resabci)
} }
func getBalance(key basecoin.Actor, state state.KVStore) (coin.Coins, error) { func getBalance(key basecoin.Actor, store state.KVStore) (coin.Coins, error) {
var acct coin.Account cspace := stack.PrefixedStore(coin.NameCoin, store)
k := stack.PrefixedKey(coin.NameCoin, key.Bytes()) acct, err := coin.GetAccount(cspace, key)
v := state.Get(k) return acct.Coins, err
// 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) { func getAddr(addr []byte, state state.KVStore) (coin.Coins, error) {

View File

@ -13,6 +13,7 @@ import (
"github.com/tendermint/basecoin/modules/auth" "github.com/tendermint/basecoin/modules/auth"
"github.com/tendermint/basecoin/modules/coin" "github.com/tendermint/basecoin/modules/coin"
"github.com/tendermint/basecoin/stack"
) )
// AccountQueryCmd - command to query an account // AccountQueryCmd - command to query an account
@ -27,7 +28,7 @@ func doAccountQuery(cmd *cobra.Command, args []string) error {
if err != nil { if err != nil {
return err return err
} }
key := coin.NewAccountant("").MakeKey(auth.SigPerm(addr)) key := stack.PrefixedKey(coin.NameCoin, auth.SigPerm(addr).Bytes())
acc := coin.Account{} acc := coin.Account{}
proof, err := proofcmd.GetAndParseAppProof(key, &acc) proof, err := proofcmd.GetAndParseAppProof(key, &acc)

View File

@ -6,6 +6,7 @@ import (
proofcmd "github.com/tendermint/light-client/commands/proofs" proofcmd "github.com/tendermint/light-client/commands/proofs"
"github.com/tendermint/basecoin/docs/guide/counter/plugins/counter" "github.com/tendermint/basecoin/docs/guide/counter/plugins/counter"
"github.com/tendermint/basecoin/stack"
) )
//CounterQueryCmd - CLI command to query the counter state //CounterQueryCmd - CLI command to query the counter state
@ -16,7 +17,7 @@ var CounterQueryCmd = &cobra.Command{
} }
func counterQueryCmd(cmd *cobra.Command, args []string) error { func counterQueryCmd(cmd *cobra.Command, args []string) error {
key := counter.StateKey() key := stack.PrefixedKey(counter.NameCounter, counter.StateKey())
var cp counter.State var cp counter.State
proof, err := proofcmd.GetAndParseAppProof(key, &cp) proof, err := proofcmd.GetAndParseAppProof(key, &cp)

View File

@ -195,7 +195,7 @@ type State struct {
// StateKey - store key for the counter state // StateKey - store key for the counter state
func StateKey() []byte { func StateKey() []byte {
return []byte(NameCounter + "/state") return []byte("state")
} }
// LoadState - retrieve the counter state from the store // LoadState - retrieve the counter state from the store

View File

@ -20,26 +20,35 @@ func (p prefixStore) Get(key []byte) (value []byte) {
} }
// stateSpace will unwrap any prefixStore and then add the prefix // 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 { func stateSpace(store state.KVStore, app string) state.KVStore {
// unwrap one-level if wrapped // unwrap one-level if wrapped
if pstore, ok := store.(prefixStore); ok { if pstore, ok := store.(prefixStore); ok {
store = pstore.store store = pstore.store
} }
// wrap it with the prefix return PrefixedStore(app, store)
prefix := makePrefix(app) }
// 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} return prefixStore{prefix, store}
} }
func makePrefix(app string) []byte { // PrefixedKey returns the absolute path to a given key in a particular
return append([]byte(app), byte(0)) // app's state-space
}
// 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 // 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 // state to check individual app spaces. Individual apps should not be able
// to use this to read each other's space // to use this to read each other's space
func PrefixedKey(app string, key []byte) []byte { func PrefixedKey(app string, key []byte) []byte {
return append(makePrefix(app), key...) prefix := append([]byte(app), byte(0))
return append(prefix, key...)
} }