Skip to content

Commit

Permalink
Implement query account without proofs
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey authored and rigelrozanski committed Mar 1, 2018
1 parent 58f2cbf commit 8c93a64
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 10 deletions.
8 changes: 8 additions & 0 deletions client/node.go
Original file line number Diff line number Diff line change
@@ -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")
}
9 changes: 8 additions & 1 deletion examples/basecoin/cmd/basecli/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
flagCommit = "commit"
flagValHash = "validator-set"

flagHeight = "height"
flagSelect = "select"
flagTags = "tag"
flagAny = "any"
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
77 changes: 77 additions & 0 deletions examples/basecoin/cmd/basecli/commands.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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
}
12 changes: 3 additions & 9 deletions examples/basecoin/cmd/basecli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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())...)

Expand Down

0 comments on commit 8c93a64

Please sign in to comment.