Implement RPC subcommands

Turned out the tendermint rpc was broken in the refactor and
had to fix that first...
This commit is contained in:
Ethan Frey 2018-02-23 19:23:24 +01:00 committed by rigelrozanski
parent c083678cae
commit 8392cf93ac
6 changed files with 172 additions and 37 deletions

63
client/rpc/block.go Normal file
View File

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

View File

@ -7,33 +7,23 @@ import (
"github.com/cosmos/cosmos-sdk/client" "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 ( const (
// one of the following should be provided to verify the connection // one of the following should be provided to verify the connection
flagGenesis = "genesis" flagGenesis = "genesis"
flagCommit = "commit" flagCommit = "commit"
flagValHash = "validator-set" flagValHash = "validator-set"
flagSelect = "select"
) )
var ( // XXX: remove this when not needed
statusCmd = &cobra.Command{ func todoNotImplemented(_ *cobra.Command, _ []string) error {
Use: "status", return errors.New("TODO: Command not yet implemented")
Short: "Query remote node for status", }
RunE: todoNotImplemented,
}
)
// AddCommands adds a number of rpc-related subcommands // AddCommands adds a number of rpc-related subcommands
func AddCommands(cmd *cobra.Command) { func AddCommands(cmd *cobra.Command) {
cmd.AddCommand( cmd.AddCommand(
initClientCommand(), initClientCommand(),
statusCmd, statusCommand(),
blockCommand(), blockCommand(),
validatorCommand(), validatorCommand(),
) )
@ -52,22 +42,3 @@ func initClientCommand() *cobra.Command {
cmd.Flags().String(flagValHash, "", "Hash of trusted validator set (hex-encoded)") cmd.Flags().String(flagValHash, "", "Hash of trusted validator set (hex-encoded)")
return cmd return cmd
} }
func blockCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "block <height>",
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 <height>",
Short: "Get the full validator set at given height",
RunE: todoNotImplemented,
}
return cmd
}

38
client/rpc/status.go Normal file
View File

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

55
client/rpc/validators.go Normal file
View File

@ -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 <height>",
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
}

6
glide.lock generated
View File

@ -1,5 +1,5 @@
hash: fa45c8a4f5512ed730f793b93d4876bdc604a1333a5a1f938c98a0f7dd55f22e hash: fa45c8a4f5512ed730f793b93d4876bdc604a1333a5a1f938c98a0f7dd55f22e
updated: 2018-02-22T16:18:43.639619321+01:00 updated: 2018-02-23T14:49:05.743112+01:00
imports: imports:
- name: github.com/bgentry/speakeasy - name: github.com/bgentry/speakeasy
version: 4aabc24848ce5fd31929f7d1e4ea74d3709c14cd version: 4aabc24848ce5fd31929f7d1e4ea74d3709c14cd
@ -123,7 +123,7 @@ imports:
- extra25519 - extra25519
- name: github.com/tendermint/go-crypto - name: github.com/tendermint/go-crypto
version: 4fc3055dbd17aa1203d0abc64b9293f378da22ec version: 4fc3055dbd17aa1203d0abc64b9293f378da22ec
subpackages: subpackages
- keys - keys
- keys/bcrypt - keys/bcrypt
- keys/words - keys/words
@ -133,7 +133,7 @@ imports:
- name: github.com/tendermint/iavl - name: github.com/tendermint/iavl
version: 1a59ec0c82dc940c25339dd7c834df5cb76a95cb version: 1a59ec0c82dc940c25339dd7c834df5cb76a95cb
- name: github.com/tendermint/tendermint - name: github.com/tendermint/tendermint
version: 4a63409d5c72a36eee49c962b060fd0063958bda version: 6947e118f54e4df24f5e2c79bcdd66199e54d885
subpackages: subpackages:
- blockchain - blockchain
- cmd/tendermint/commands - cmd/tendermint/commands

View File

@ -42,7 +42,9 @@ HASH=`echo $TX | cut -d' ' -f6`
echo "tx hash:" $HASH echo "tx hash:" $HASH
# let some blocks come up.... # let some blocks come up....
./build/basecli status | jq .latest_block_height
sleep 2 sleep 2
./build/basecli status | jq .latest_block_height
# balances change # balances change
echo; echo "My account went down" echo; echo "My account went down"
@ -54,6 +56,12 @@ echo; echo "Empty account got some cash"
echo; echo "View tx" echo; echo "View tx"
./build/basecli tx $HASH ./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 # shutdown, but add a sleep if you want to manually run some cli scripts
# against this server before it goes away # against this server before it goes away
# sleep 120 # sleep 120