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"
"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) {

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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...)
}