From 8392cf93acefad1b74b3fb49b1cf77528fcd4669 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Fri, 23 Feb 2018 19:23:24 +0100 Subject: [PATCH] Implement RPC subcommands Turned out the tendermint rpc was broken in the refactor and had to fix that first... --- client/rpc/block.go | 63 ++++++++++++++++++++++++++++++++++++++++ client/rpc/root.go | 39 ++++--------------------- client/rpc/status.go | 38 ++++++++++++++++++++++++ client/rpc/validators.go | 55 +++++++++++++++++++++++++++++++++++ glide.lock | 6 ++-- tests/check_basecli.sh | 8 +++++ 6 files changed, 172 insertions(+), 37 deletions(-) create mode 100644 client/rpc/block.go create mode 100644 client/rpc/status.go create mode 100644 client/rpc/validators.go diff --git a/client/rpc/block.go b/client/rpc/block.go new file mode 100644 index 0000000000..bc127ddb21 --- /dev/null +++ b/client/rpc/block.go @@ -0,0 +1,63 @@ +package rpc + +import ( + "encoding/json" + "fmt" + "strconv" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +const ( + flagSelect = "select" +) + +func blockCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "block [height]", + Short: "Get verified data for a the block at given height", + RunE: getBlock, + } + cmd.Flags().StringP(client.FlagNode, "n", "tcp://localhost:46657", "Node to connect to") + // TODO: change this to false when we can + cmd.Flags().Bool(client.FlagTrustNode, true, "Don't verify proofs for responses") + cmd.Flags().StringSlice(flagSelect, []string{"header", "tx"}, "Fields to return (header|txs|results)") + return cmd +} + +func getBlock(cmd *cobra.Command, args []string) error { + var height *int64 + // optional height + if len(args) > 0 { + h, err := strconv.Atoi(args[0]) + if err != nil { + return err + } + if h > 0 { + tmp := int64(h) + height = &tmp + } + } + + // get the node + uri := viper.GetString(client.FlagNode) + node := client.GetNode(uri) + + // TODO: actually honor the --select flag! + // header -> BlockchainInfo + // header, tx -> Block + // results -> BlockResults + res, err := node.Block(height) + if err != nil { + return err + } + + output, err := json.MarshalIndent(res, " ", "") + if err != nil { + return err + } + fmt.Println(string(output)) + return nil +} diff --git a/client/rpc/root.go b/client/rpc/root.go index a65a9959ec..8acf8ddad1 100644 --- a/client/rpc/root.go +++ b/client/rpc/root.go @@ -7,33 +7,23 @@ import ( "github.com/cosmos/cosmos-sdk/client" ) -// XXX: remove this when not needed -func todoNotImplemented(_ *cobra.Command, _ []string) error { - return errors.New("TODO: Command not yet implemented") -} - const ( // one of the following should be provided to verify the connection flagGenesis = "genesis" flagCommit = "commit" flagValHash = "validator-set" - - flagSelect = "select" ) -var ( - statusCmd = &cobra.Command{ - Use: "status", - Short: "Query remote node for status", - RunE: todoNotImplemented, - } -) +// XXX: remove this when not needed +func todoNotImplemented(_ *cobra.Command, _ []string) error { + return errors.New("TODO: Command not yet implemented") +} // AddCommands adds a number of rpc-related subcommands func AddCommands(cmd *cobra.Command) { cmd.AddCommand( initClientCommand(), - statusCmd, + statusCommand(), blockCommand(), validatorCommand(), ) @@ -52,22 +42,3 @@ func initClientCommand() *cobra.Command { cmd.Flags().String(flagValHash, "", "Hash of trusted validator set (hex-encoded)") return cmd } - -func blockCommand() *cobra.Command { - cmd := &cobra.Command{ - Use: "block ", - Short: "Get verified data for a the block at given height", - RunE: todoNotImplemented, - } - cmd.Flags().StringSlice(flagSelect, []string{"header", "tx"}, "Fields to return (header|txs|results)") - return cmd -} - -func validatorCommand() *cobra.Command { - cmd := &cobra.Command{ - Use: "validatorset ", - Short: "Get the full validator set at given height", - RunE: todoNotImplemented, - } - return cmd -} diff --git a/client/rpc/status.go b/client/rpc/status.go new file mode 100644 index 0000000000..61e922f4c6 --- /dev/null +++ b/client/rpc/status.go @@ -0,0 +1,38 @@ +package rpc + +import ( + "encoding/json" + "fmt" + + "github.com/spf13/cobra" + "github.com/spf13/viper" + + "github.com/cosmos/cosmos-sdk/client" +) + +func statusCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "status", + Short: "Query remote node for status", + RunE: checkStatus, + } + cmd.Flags().StringP(client.FlagNode, "n", "tcp://localhost:46657", "Node to connect to") + return cmd +} + +func checkStatus(cmd *cobra.Command, args []string) error { + // get the node + uri := viper.GetString(client.FlagNode) + node := client.GetNode(uri) + res, err := node.Status() + if err != nil { + return err + } + + output, err := json.MarshalIndent(res, " ", "") + if err != nil { + return err + } + fmt.Println(string(output)) + return nil +} diff --git a/client/rpc/validators.go b/client/rpc/validators.go new file mode 100644 index 0000000000..143bbe17e6 --- /dev/null +++ b/client/rpc/validators.go @@ -0,0 +1,55 @@ +package rpc + +import ( + "encoding/json" + "fmt" + "strconv" + + "github.com/spf13/cobra" + "github.com/spf13/viper" + + "github.com/cosmos/cosmos-sdk/client" +) + +func validatorCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "validatorset ", + Short: "Get the full validator set at given height", + RunE: getValidators, + } + cmd.Flags().StringP(client.FlagNode, "n", "tcp://localhost:46657", "Node to connect to") + // TODO: change this to false when we can + cmd.Flags().Bool(client.FlagTrustNode, true, "Don't verify proofs for responses") + return cmd +} + +func getValidators(cmd *cobra.Command, args []string) error { + var height *int64 + // optional height + if len(args) > 0 { + h, err := strconv.Atoi(args[0]) + if err != nil { + return err + } + if h > 0 { + tmp := int64(h) + height = &tmp + } + } + + // get the node + uri := viper.GetString(client.FlagNode) + node := client.GetNode(uri) + + res, err := node.Validators(height) + if err != nil { + return err + } + + output, err := json.MarshalIndent(res, " ", "") + if err != nil { + return err + } + fmt.Println(string(output)) + return nil +} diff --git a/glide.lock b/glide.lock index 43b61b13f4..57384c8673 100644 --- a/glide.lock +++ b/glide.lock @@ -1,5 +1,5 @@ hash: fa45c8a4f5512ed730f793b93d4876bdc604a1333a5a1f938c98a0f7dd55f22e -updated: 2018-02-22T16:18:43.639619321+01:00 +updated: 2018-02-23T14:49:05.743112+01:00 imports: - name: github.com/bgentry/speakeasy version: 4aabc24848ce5fd31929f7d1e4ea74d3709c14cd @@ -123,7 +123,7 @@ imports: - extra25519 - name: github.com/tendermint/go-crypto version: 4fc3055dbd17aa1203d0abc64b9293f378da22ec - subpackages: + subpackages - keys - keys/bcrypt - keys/words @@ -133,7 +133,7 @@ imports: - name: github.com/tendermint/iavl version: 1a59ec0c82dc940c25339dd7c834df5cb76a95cb - name: github.com/tendermint/tendermint - version: 4a63409d5c72a36eee49c962b060fd0063958bda + version: 6947e118f54e4df24f5e2c79bcdd66199e54d885 subpackages: - blockchain - cmd/tendermint/commands diff --git a/tests/check_basecli.sh b/tests/check_basecli.sh index b470ea218a..ec2c458cb6 100755 --- a/tests/check_basecli.sh +++ b/tests/check_basecli.sh @@ -42,7 +42,9 @@ HASH=`echo $TX | cut -d' ' -f6` echo "tx hash:" $HASH # let some blocks come up.... +./build/basecli status | jq .latest_block_height sleep 2 +./build/basecli status | jq .latest_block_height # balances change echo; echo "My account went down" @@ -54,6 +56,12 @@ echo; echo "Empty account got some cash" echo; echo "View tx" ./build/basecli tx $HASH +# wait a bit then dump out some blockchain state +sleep 10 +./build/basecli status --trace +./build/basecli block --trace +./build/basecli validatorset --trace + # shutdown, but add a sleep if you want to manually run some cli scripts # against this server before it goes away # sleep 120