2018-02-23 10:23:24 -08:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2018-02-28 06:36:21 -08:00
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
|
|
tmwire "github.com/tendermint/tendermint/wire"
|
2018-02-23 10:23:24 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2018-02-28 15:26:39 -08:00
|
|
|
node, err := client.GetNode()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-02-23 10:23:24 -08:00
|
|
|
|
|
|
|
// TODO: actually honor the --select flag!
|
|
|
|
// header -> BlockchainInfo
|
|
|
|
// header, tx -> Block
|
|
|
|
// results -> BlockResults
|
|
|
|
res, err := node.Block(height)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-02-28 06:36:21 -08:00
|
|
|
output, err := tmwire.MarshalJSON(res)
|
|
|
|
// output, err := json.MarshalIndent(res, " ", "")
|
2018-02-23 10:23:24 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println(string(output))
|
|
|
|
return nil
|
|
|
|
}
|