2018-02-23 10:23:24 -08:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-02-28 23:16:54 -08:00
|
|
|
"net/http"
|
2018-02-23 10:23:24 -08:00
|
|
|
"strconv"
|
|
|
|
|
2019-02-14 08:53:36 -08:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
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-06-04 03:38:24 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec/legacy"
|
2019-02-14 08:53:36 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2018-09-14 11:41:21 -07:00
|
|
|
tmliteProxy "github.com/tendermint/tendermint/lite/proxy"
|
2018-02-23 10:23:24 -08:00
|
|
|
)
|
|
|
|
|
2018-05-28 15:50:07 -07: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),
|
2018-02-28 23:16:54 -08:00
|
|
|
RunE: printBlock,
|
2018-02-23 10:23:24 -08:00
|
|
|
}
|
2019-05-28 01:44:04 -07:00
|
|
|
cmd.Flags().StringP(flags.FlagNode, "n", "tcp://localhost:26657", "Node to connect to")
|
|
|
|
viper.BindPFlag(flags.FlagNode, cmd.Flags().Lookup(flags.FlagNode))
|
|
|
|
cmd.Flags().Bool(flags.FlagTrustNode, false, "Trust connected full node (don't verify proofs for responses)")
|
|
|
|
viper.BindPFlag(flags.FlagTrustNode, cmd.Flags().Lookup(flags.FlagTrustNode))
|
2020-04-11 09:52:36 -07:00
|
|
|
cmd.Flags().String(flags.FlagKeyringBackend, flags.DefaultKeyringBackend, "Select keyring's backend (os|file|kwallet|pass|test)")
|
|
|
|
viper.BindPFlag(flags.FlagKeyringBackend, cmd.Flags().Lookup(flags.FlagKeyringBackend))
|
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
|
|
|
|
res, err := node.Block(height)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
if !clientCtx.TrustNode {
|
|
|
|
check, err := clientCtx.Verify(res.Block.Height)
|
2018-09-14 11:41:21 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-16 13:46:51 -08:00
|
|
|
if err := tmliteProxy.ValidateHeader(&res.Block.Header, check); err != nil {
|
2018-09-14 11:41:21 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-16 13:46:51 -08:00
|
|
|
if err = tmliteProxy.ValidateBlock(res.Block, check); err != nil {
|
2018-09-14 11:41:21 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 03:38:24 -07: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
|
|
|
|
2018-02-28 23:16:54 -08:00
|
|
|
status, err := node.Status()
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// CMD
|
|
|
|
|
|
|
|
func printBlock(cmd *cobra.Command, args []string) error {
|
2018-02-23 10:23:24 -08: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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
output, err := getBlock(client.NewContext(), height)
|
2018-02-28 15:26:39 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-03 06:56:15 -07:00
|
|
|
|
2018-02-28 23:16:54 -08:00
|
|
|
fmt.Println(string(output))
|
|
|
|
return nil
|
|
|
|
}
|
2018-02-23 10:23:24 -08:00
|
|
|
|
2018-02-28 23:16:54 -08:00
|
|
|
// REST
|
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
// REST handler to get a block
|
2020-06-01 05:46:03 -07:00
|
|
|
func BlockRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
|
2018-04-25 07:49:31 -07:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
2019-08-03 06:56:15 -07:00
|
|
|
|
2018-04-25 07:49:31 -07:00
|
|
|
height, err := strconv.ParseInt(vars["height"], 10, 64)
|
|
|
|
if err != nil {
|
2019-03-19 09:52:43 -07:00
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest,
|
2019-06-26 13:30:36 -07:00
|
|
|
"couldn't parse block height. Assumed format is '/block/{height}'.")
|
2018-04-25 07:49:31 -07:00
|
|
|
return
|
|
|
|
}
|
2019-08-03 06:56:15 -07:00
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
chainHeight, err := GetChainHeight(clientCtx)
|
2019-06-26 13:30:36 -07:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, "failed to parse chain height")
|
|
|
|
return
|
|
|
|
}
|
2019-08-03 06:56:15 -07:00
|
|
|
|
2018-04-25 07:49:31 -07:00
|
|
|
if height > chainHeight {
|
2019-06-26 13:30:36 -07:00
|
|
|
rest.WriteErrorResponse(w, http.StatusNotFound, "requested block height is bigger then the chain length")
|
2018-04-25 07:49:31 -07:00
|
|
|
return
|
|
|
|
}
|
2019-08-03 06:56:15 -07:00
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
output, err := getBlock(clientCtx, &height)
|
2020-04-01 00:50:22 -07:00
|
|
|
if rest.CheckInternalServerError(w, err) {
|
2018-04-25 07:49:31 -07:00
|
|
|
return
|
|
|
|
}
|
2019-08-03 06:56:15 -07:00
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
rest.PostProcessResponseBare(w, clientCtx, output)
|
2018-02-28 23:16:54 -08:00
|
|
|
}
|
|
|
|
}
|
2018-02-23 10:23:24 -08:00
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
// REST handler to get the latest block
|
2020-06-01 05:46:03 -07:00
|
|
|
func LatestBlockRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
|
2018-04-25 07:49:31 -07:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2020-06-01 05:46:03 -07:00
|
|
|
output, err := getBlock(clientCtx, nil)
|
2020-04-01 00:50:22 -07:00
|
|
|
if rest.CheckInternalServerError(w, err) {
|
2018-04-25 07:49:31 -07:00
|
|
|
return
|
|
|
|
}
|
2019-05-09 13:42:25 -07:00
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
rest.PostProcessResponseBare(w, clientCtx, output)
|
2018-02-28 23:16:54 -08:00
|
|
|
}
|
2018-02-23 10:23:24 -08:00
|
|
|
}
|