add range queries, add candidates query

This commit is contained in:
rigelrozanski 2018-05-08 12:47:31 -04:00
parent 8a6ec9a257
commit d44c7afa30
8 changed files with 78 additions and 56 deletions

View File

@ -43,8 +43,23 @@ func (ctx CoreContext) BroadcastTx(tx []byte) (*ctypes.ResultBroadcastTxCommit,
// Query from Tendermint with the provided key and storename // Query from Tendermint with the provided key and storename
func (ctx CoreContext) Query(key cmn.HexBytes, storeName string) (res []byte, err error) { func (ctx CoreContext) Query(key cmn.HexBytes, storeName string) (res []byte, err error) {
return ctx.query(key, storeName, "key")
}
path := fmt.Sprintf("/%s/key", storeName) // Query from Tendermint with the provided storename and subspace
func (ctx CoreContext) QuerySubspace(cdc *wire.Codec, subspace []byte, storeName string) (res []sdk.KV, err error) {
resRaw, err := ctx.query(subspace, storeName, "iter")
if err != nil {
return res, err
}
cdc.MustUnmarshalBinary(resRaw, &res)
return
}
// Query from Tendermint with the provided storename and path
func (ctx CoreContext) query(key cmn.HexBytes, storeName, endPath string) (res []byte, err error) {
path := fmt.Sprintf("/%s/%s", storeName, endPath)
node, err := ctx.GetNode() node, err := ctx.GetNode()
if err != nil { if err != nil {
return res, err return res, err

View File

@ -46,7 +46,7 @@ func main() {
client.GetCommands( client.GetCommands(
authcmd.GetAccountCmd("acc", cdc, authcmd.GetAccountDecoder(cdc)), authcmd.GetAccountCmd("acc", cdc, authcmd.GetAccountDecoder(cdc)),
stakecmd.GetCmdQueryCandidate("stake", cdc), stakecmd.GetCmdQueryCandidate("stake", cdc),
//stakecmd.GetCmdQueryCandidates("stake", cdc), stakecmd.GetCmdQueryCandidates("stake", cdc),
stakecmd.GetCmdQueryDelegatorBond("stake", cdc), stakecmd.GetCmdQueryDelegatorBond("stake", cdc),
//stakecmd.GetCmdQueryDelegatorBonds("stake", cdc), //stakecmd.GetCmdQueryDelegatorBonds("stake", cdc),
)...) )...)

View File

@ -176,7 +176,16 @@ func (st *iavlStore) Query(req abci.RequestQuery) (res abci.ResponseQuery) {
} else { } else {
_, res.Value = tree.GetVersioned(key, height) _, res.Value = tree.GetVersioned(key, height)
} }
case "/iter": // Get by key
key := req.Data // Data holds the key bytes
res.Key = key
var KVs []KV
iterator := st.SubspaceIterator(key)
for ; iterator.Valid(); iterator.Next() {
KVs = append(KVs, KV{iterator.Key(), iterator.Value()})
}
iterator.Close()
res.Value = cdc.MustMarshalBinary(KVs)
default: default:
msg := fmt.Sprintf("Unexpected Query path: %v", req.Path) msg := fmt.Sprintf("Unexpected Query path: %v", req.Path)
return sdk.ErrUnknownRequest(msg).QueryResult() return sdk.ErrUnknownRequest(msg).QueryResult()

View File

@ -13,6 +13,7 @@ type MultiStore = types.MultiStore
type CacheMultiStore = types.CacheMultiStore type CacheMultiStore = types.CacheMultiStore
type CommitMultiStore = types.CommitMultiStore type CommitMultiStore = types.CommitMultiStore
type KVStore = types.KVStore type KVStore = types.KVStore
type KV = types.KV
type Iterator = types.Iterator type Iterator = types.Iterator
type CacheKVStore = types.CacheKVStore type CacheKVStore = types.CacheKVStore
type CommitKVStore = types.CommitKVStore type CommitKVStore = types.CommitKVStore

View File

@ -256,3 +256,10 @@ func PrefixEndBytes(prefix []byte) []byte {
} }
return end return end
} }
//----------------------------------------
// key-value result for iterator queries
type KV struct {
Key, Value []byte
}

View File

@ -15,42 +15,6 @@ import (
"github.com/cosmos/cosmos-sdk/x/stake" "github.com/cosmos/cosmos-sdk/x/stake"
) )
//// create command to query for all candidates
//func GetCmdQueryCandidates(storeName string, cdc *wire.Codec) *cobra.Command {
//cmd := &cobra.Command{
//Use: "candidates",
//Short: "Query for the set of validator-candidates pubkeys",
//RunE: func(cmd *cobra.Command, args []string) error {
//key := stake.CandidatesKey
//ctx := context.NewCoreContextFromViper()
//res, err := ctx.Query(key, storeName)
//if err != nil {
//return err
//}
//// parse out the candidates
//candidates := new(stake.Candidates)
//err = cdc.UnmarshalBinary(res, candidates)
//if err != nil {
//return err
//}
//output, err := wire.MarshalJSONIndent(cdc, candidates)
//if err != nil {
//return err
//}
//fmt.Println(string(output))
//return nil
//// TODO output with proofs / machine parseable etc.
//},
//}
//cmd.Flags().AddFlagSet(fsDelegator)
//return cmd
//}
// get the command to query a candidate // get the command to query a candidate
func GetCmdQueryCandidate(storeName string, cdc *wire.Codec) *cobra.Command { func GetCmdQueryCandidate(storeName string, cdc *wire.Codec) *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
@ -64,9 +28,7 @@ func GetCmdQueryCandidate(storeName string, cdc *wire.Codec) *cobra.Command {
} }
key := stake.GetCandidateKey(addr) key := stake.GetCandidateKey(addr)
ctx := context.NewCoreContextFromViper() ctx := context.NewCoreContextFromViper()
res, err := ctx.Query(key, storeName) res, err := ctx.Query(key, storeName)
if err != nil { if err != nil {
return err return err
@ -74,10 +36,7 @@ func GetCmdQueryCandidate(storeName string, cdc *wire.Codec) *cobra.Command {
// parse out the candidate // parse out the candidate
candidate := new(stake.Candidate) candidate := new(stake.Candidate)
err = cdc.UnmarshalBinary(res, candidate) cdc.MustUnmarshalBinary(res, candidate)
if err != nil {
return err
}
output, err := wire.MarshalJSONIndent(cdc, candidate) output, err := wire.MarshalJSONIndent(cdc, candidate)
if err != nil { if err != nil {
return err return err
@ -93,6 +52,41 @@ func GetCmdQueryCandidate(storeName string, cdc *wire.Codec) *cobra.Command {
return cmd return cmd
} }
// get the command to query a candidate
func GetCmdQueryCandidates(storeName string, cdc *wire.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "candidates",
Short: "Query for all validator-candidate accounts",
RunE: func(cmd *cobra.Command, args []string) error {
key := stake.CandidatesKey
ctx := context.NewCoreContextFromViper()
resKVs, err := ctx.QuerySubspace(cdc, key, storeName)
if err != nil {
return err
}
// parse out the candidates
var candidates []stake.Candidate
for _, KV := range resKVs {
var candidate stake.Candidate
cdc.MustUnmarshalBinary(KV.Value, &candidate)
candidates = append(candidates, candidate)
}
output, err := wire.MarshalJSONIndent(cdc, candidates)
if err != nil {
return err
}
fmt.Println(string(output))
return nil
// TODO output with proofs / machine parseable etc.
},
}
return cmd
}
// get the command to query a single delegator bond // get the command to query a single delegator bond
func GetCmdQueryDelegatorBond(storeName string, cdc *wire.Codec) *cobra.Command { func GetCmdQueryDelegatorBond(storeName string, cdc *wire.Codec) *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
@ -122,10 +116,7 @@ func GetCmdQueryDelegatorBond(storeName string, cdc *wire.Codec) *cobra.Command
// parse out the bond // parse out the bond
bond := new(stake.DelegatorBond) bond := new(stake.DelegatorBond)
err = cdc.UnmarshalBinary(res, bond) cdc.MustUnmarshalBinary(res, bond)
if err != nil {
return err
}
output, err := wire.MarshalJSONIndent(cdc, bond) output, err := wire.MarshalJSONIndent(cdc, bond)
if err != nil { if err != nil {
return err return err

View File

@ -22,6 +22,8 @@ func GetCmdDeclareCandidacy(cdc *wire.Codec) *cobra.Command {
Use: "declare-candidacy", Use: "declare-candidacy",
Short: "create new validator-candidate account and delegate some coins to it", Short: "create new validator-candidate account and delegate some coins to it",
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))
amount, err := sdk.ParseCoin(viper.GetString(FlagAmount)) amount, err := sdk.ParseCoin(viper.GetString(FlagAmount))
if err != nil { if err != nil {
return err return err
@ -56,8 +58,6 @@ func GetCmdDeclareCandidacy(cdc *wire.Codec) *cobra.Command {
msg := stake.NewMsgDeclareCandidacy(candidateAddr, pk, amount, description) msg := stake.NewMsgDeclareCandidacy(candidateAddr, pk, amount, description)
// build and sign the transaction, then broadcast to Tendermint // build and sign the transaction, then broadcast to Tendermint
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))
res, err := ctx.EnsureSignBuildBroadcast(ctx.FromAddressName, msg, cdc) res, err := ctx.EnsureSignBuildBroadcast(ctx.FromAddressName, msg, cdc)
if err != nil { if err != nil {
return err return err

View File

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
crypto "github.com/tendermint/go-crypto" crypto "github.com/tendermint/go-crypto"
) )
@ -45,11 +46,9 @@ func (msg MsgDeclareCandidacy) GetSigners() []sdk.Address { return []sdk.Address
// get the bytes for the message signer to sign on // get the bytes for the message signer to sign on
func (msg MsgDeclareCandidacy) GetSignBytes() []byte { func (msg MsgDeclareCandidacy) GetSignBytes() []byte {
b, err := json.Marshal(msg) cdc := wire.NewCodec()
if err != nil { wire.RegisterCrypto(cdc)
panic(err) return cdc.MustMarshalBinary(msg)
}
return b
} }
// quick validity check // quick validity check