2018-02-23 10:23:24 -08:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
2020-09-23 06:44:40 -07:00
|
|
|
"context"
|
2018-02-23 10:23:24 -08:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
2019-02-14 08:53:36 -08:00
|
|
|
"github.com/spf13/cobra"
|
2019-02-04 07:48:26 -08:00
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2019-05-28 01:44:04 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
2020-12-22 20:22:53 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec/legacy"
|
2018-02-23 10:23:24 -08:00
|
|
|
)
|
|
|
|
|
2021-02-23 00:46:01 -08:00
|
|
|
// BlockCommand returns the verified block data for a given heights
|
2018-05-28 15:00:37 -07:00
|
|
|
func BlockCommand() *cobra.Command {
|
2018-02-23 10:23:24 -08:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "block [height]",
|
|
|
|
Short: "Get verified data for a the block at given height",
|
2018-04-26 14:32:20 -07:00
|
|
|
Args: cobra.MaximumNArgs(1),
|
2020-07-20 09:42:46 -07:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2020-12-14 14:09:51 -08:00
|
|
|
clientCtx, err := client.GetClientQueryContext(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-20 09:42:46 -07:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
output, err := getBlock(clientCtx, height)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(string(output))
|
|
|
|
return nil
|
|
|
|
},
|
2018-02-23 10:23:24 -08:00
|
|
|
}
|
2020-07-20 09:42:46 -07:00
|
|
|
|
2019-05-28 01:44:04 -07:00
|
|
|
cmd.Flags().StringP(flags.FlagNode, "n", "tcp://localhost:26657", "Node to connect to")
|
2020-07-20 09:42:46 -07:00
|
|
|
|
2018-02-23 10:23:24 -08:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
func getBlock(clientCtx client.Context, height *int64) ([]byte, error) {
|
2018-02-28 23:16:54 -08:00
|
|
|
// get the node
|
2020-06-01 05:46:03 -07:00
|
|
|
node, err := clientCtx.GetNode()
|
2018-02-28 23:16:54 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// header -> BlockchainInfo
|
|
|
|
// header, tx -> Block
|
|
|
|
// results -> BlockResults
|
2020-09-23 06:44:40 -07:00
|
|
|
res, err := node.Block(context.Background(), height)
|
2018-02-28 23:16:54 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-22 20:22:53 -08:00
|
|
|
return legacy.Cdc.MarshalJSON(res)
|
2018-02-28 23:16:54 -08:00
|
|
|
}
|
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
// get the current blockchain height
|
2020-06-01 05:46:03 -07:00
|
|
|
func GetChainHeight(clientCtx client.Context) (int64, error) {
|
|
|
|
node, err := clientCtx.GetNode()
|
2018-02-28 23:16:54 -08:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2019-08-03 06:56:15 -07:00
|
|
|
|
2020-09-23 06:44:40 -07:00
|
|
|
status, err := node.Status(context.Background())
|
2018-02-28 23:16:54 -08:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2019-08-03 06:56:15 -07:00
|
|
|
|
2018-04-13 02:04:31 -07:00
|
|
|
height := status.SyncInfo.LatestBlockHeight
|
2018-02-28 23:16:54 -08:00
|
|
|
return height, nil
|
|
|
|
}
|