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
|
|
|
|
2018-04-02 05:05:23 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
2019-05-28 01:44:04 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
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))
|
2018-02-23 10:23:24 -08:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2018-08-06 11:11:30 -07:00
|
|
|
func getBlock(cliCtx context.CLIContext, height *int64) ([]byte, error) {
|
2018-02-28 23:16:54 -08:00
|
|
|
// get the node
|
2018-08-06 11:11:30 -07:00
|
|
|
node, err := cliCtx.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
|
|
|
|
}
|
|
|
|
|
2018-09-14 11:41:21 -07:00
|
|
|
if !cliCtx.TrustNode {
|
2018-10-03 08:48:23 -07:00
|
|
|
check, err := cliCtx.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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-04 04:00:24 -07:00
|
|
|
if cliCtx.Indent {
|
2019-05-28 01:44:04 -07:00
|
|
|
return codec.Cdc.MarshalJSONIndent(res, "", " ")
|
2018-02-28 23:16:54 -08:00
|
|
|
}
|
2019-08-03 06:56:15 -07:00
|
|
|
|
2019-05-28 01:44:04 -07:00
|
|
|
return codec.Cdc.MarshalJSON(res)
|
2018-02-28 23:16:54 -08:00
|
|
|
}
|
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
// get the current blockchain height
|
2018-08-06 11:11:30 -07:00
|
|
|
func GetChainHeight(cliCtx context.CLIContext) (int64, error) {
|
|
|
|
node, err := cliCtx.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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-06 11:11:30 -07:00
|
|
|
output, err := getBlock(context.NewCLIContext(), 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
|
2018-08-06 11:11:30 -07:00
|
|
|
func BlockRequestHandlerFn(cliCtx context.CLIContext) 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
|
|
|
|
2018-08-06 11:11:30 -07:00
|
|
|
chainHeight, err := GetChainHeight(cliCtx)
|
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
|
|
|
|
2018-08-06 11:11:30 -07:00
|
|
|
output, err := getBlock(cliCtx, &height)
|
2018-04-25 07:49:31 -07:00
|
|
|
if err != nil {
|
2019-03-19 09:52:43 -07:00
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2018-04-25 07:49:31 -07:00
|
|
|
return
|
|
|
|
}
|
2019-08-03 06:56:15 -07:00
|
|
|
|
|
|
|
rest.PostProcessResponseBare(w, cliCtx, 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
|
2018-08-06 11:11:30 -07:00
|
|
|
func LatestBlockRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
2018-04-25 07:49:31 -07:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2019-05-09 13:42:25 -07:00
|
|
|
output, err := getBlock(cliCtx, nil)
|
2018-04-25 07:49:31 -07:00
|
|
|
if err != nil {
|
2019-03-19 09:52:43 -07:00
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2018-04-25 07:49:31 -07:00
|
|
|
return
|
|
|
|
}
|
2019-05-09 13:42:25 -07:00
|
|
|
|
2019-08-03 06:56:15 -07:00
|
|
|
rest.PostProcessResponseBare(w, cliCtx, output)
|
2018-02-28 23:16:54 -08:00
|
|
|
}
|
2018-02-23 10:23:24 -08:00
|
|
|
}
|