Implement query account without proofs

This commit is contained in:
Ethan Frey
2018-03-01 02:36:57 +00:00
committed by rigelrozanski
parent 58f2cbfa38
commit 8c93a6455b
4 changed files with 96 additions and 10 deletions
+8
View File
@@ -0,0 +1,8 @@
package client
import rpcclient "github.com/tendermint/tendermint/rpc/client"
// GetNode prepares a simple rpc.Client from the flags
func GetNode(uri string) rpcclient.Client {
return rpcclient.NewHTTP(uri, "/websocket")
}
+8 -1
View File
@@ -12,6 +12,7 @@ const (
flagCommit = "commit"
flagValHash = "validator-set"
flagHeight = "height"
flagSelect = "select"
flagTags = "tag"
flagAny = "any"
@@ -51,7 +52,11 @@ func AddClientCommands(cmd *cobra.Command) {
// GetCommands adds common flags to query commands
func GetCommands(cmds ...*cobra.Command) []*cobra.Command {
for _, c := range cmds {
c.Flags().Bool(flagTrustNode, false, "Don't verify proofs for responses")
// TODO: make this default false when we support proofs
c.Flags().Bool(flagTrustNode, true, "Don't verify proofs for responses")
c.Flags().String(flagChainID, "", "Chain ID of tendermint node")
c.Flags().String(flagNode, "", "<host>:<port> to tendermint rpc interface for this chain")
c.Flags().Int64(flagHeight, 0, "block height to query, omit to get most recent provable block")
}
return cmds
}
@@ -60,6 +65,8 @@ func GetCommands(cmds ...*cobra.Command) []*cobra.Command {
func PostCommands(cmds ...*cobra.Command) []*cobra.Command {
for _, c := range cmds {
c.Flags().String(flagName, "", "Name of private key with which to sign")
c.Flags().String(flagChainID, "", "Chain ID of tendermint node")
c.Flags().String(flagNode, "", "<host>:<port> to tendermint rpc interface for this chain")
}
return cmds
}
+77
View File
@@ -1,7 +1,20 @@
package main
import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
crypto "github.com/tendermint/go-crypto"
rpcclient "github.com/tendermint/tendermint/rpc/client"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/examples/basecoin/app"
"github.com/cosmos/cosmos-sdk/examples/basecoin/types"
)
const (
@@ -21,3 +34,67 @@ func postSendCommand() *cobra.Command {
cmd.Flags().String(flagFee, "", "Fee to pay along with transaction")
return cmd
}
func getAccountCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "account <address>",
Short: "Query account balance",
RunE: getAccount,
}
return cmd
}
func getAccount(cmd *cobra.Command, args []string) error {
if len(args) != 1 || len(args[0]) == 0 {
return errors.New("You must provide an account name")
}
// find the key to look up the account
addr := args[0]
bz, err := hex.DecodeString(addr)
if err != nil {
return err
}
key := crypto.Address(bz)
// TODO: make the store name a variable in getAccountCmd?
path := "/main/key"
uri := viper.GetString(flagNode)
if uri == "" {
return errors.New("Must define which node to query with --node")
}
node := client.GetNode(uri)
opts := rpcclient.ABCIQueryOptions{
Height: viper.GetInt64(flagHeight),
// Trusted: viper.GetBool(flagTrustNode),
Trusted: true,
}
result, err := node.ABCIQueryWithOptions(path, key, opts)
if err != nil {
return err
}
resp := result.Response
if resp.Code != uint32(0) {
return errors.Errorf("Query failed: (%d) %s", resp.Code, resp.Log)
}
// parse out the value
acct := new(types.AppAccount)
cdc := app.MakeTxCodec()
err = cdc.UnmarshalBinary(resp.Value, acct)
if err != nil {
return err
}
// TODO: better
// fmt.Printf("Account: %#v\n", acct)
output, err := json.MarshalIndent(acct, "", " ")
// output, err := json.MarshalIndent(acct.BaseAccount.Coins, "", " ")
if err != nil {
return err
}
fmt.Println(string(output))
return nil
}
+3 -9
View File
@@ -20,12 +20,6 @@ var (
}
lineBreak = &cobra.Command{Run: func(*cobra.Command, []string) {}}
getAccountCmd = &cobra.Command{
Use: "account <address>",
Short: "Query account balance",
RunE: todoNotImplemented,
}
)
func todoNotImplemented(_ *cobra.Command, _ []string) error {
@@ -38,10 +32,10 @@ func main() {
// generic client commands
AddClientCommands(basecliCmd)
// query commands (custom to binary)
// query/post commands (custom to binary)
basecliCmd.AddCommand(
GetCommands(getAccountCmd)...)
// post tx commands (custom to binary)
GetCommands(getAccountCmd())...)
basecliCmd.AddCommand(
PostCommands(postSendCommand())...)