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"
|
|
|
|
|
2018-02-28 06:36:21 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2018-04-02 05:05:23 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
2018-08-06 11:11:30 -07:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/spf13/cobra"
|
2018-09-25 14:36:42 -07:00
|
|
|
"github.com/spf13/viper"
|
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
|
|
|
}
|
2018-06-13 15:13:51 -07:00
|
|
|
cmd.Flags().StringP(client.FlagNode, "n", "tcp://localhost:26657", "Node to connect to")
|
2018-09-25 14:36:42 -07:00
|
|
|
viper.BindPFlag(client.FlagNode, cmd.Flags().Lookup(client.FlagNode))
|
2018-09-14 11:41:21 -07:00
|
|
|
cmd.Flags().Bool(client.FlagTrustNode, false, "Trust connected full node (don't verify proofs for responses)")
|
2018-09-25 14:36:42 -07:00
|
|
|
viper.BindPFlag(client.FlagTrustNode, cmd.Flags().Lookup(client.FlagTrustNode))
|
2018-09-14 11:41:21 -07:00
|
|
|
cmd.Flags().String(client.FlagChainID, "", "Chain ID of Tendermint node")
|
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-09-17 07:13:11 -07:00
|
|
|
check, err := cliCtx.Certify(res.Block.Height)
|
2018-09-14 11:41:21 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tmliteProxy.ValidateBlockMeta(res.BlockMeta, check)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tmliteProxy.ValidateBlock(res.Block, check)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-28 23:16:54 -08:00
|
|
|
// TODO move maarshalling into cmd/rest functions
|
2018-09-13 11:17:32 -07:00
|
|
|
// output, err := tmcodec.MarshalJSON(res)
|
2018-04-07 10:56:49 -07:00
|
|
|
output, err := cdc.MarshalJSON(res)
|
2018-02-28 23:16:54 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return output, nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
status, err := node.Status()
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
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
|
|
|
|
}
|
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)
|
|
|
|
height, err := strconv.ParseInt(vars["height"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(400)
|
|
|
|
w.Write([]byte("ERROR: Couldn't parse block height. Assumed format is '/block/{height}'."))
|
|
|
|
return
|
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
chainHeight, err := GetChainHeight(cliCtx)
|
2018-04-25 07:49:31 -07:00
|
|
|
if height > chainHeight {
|
|
|
|
w.WriteHeader(404)
|
|
|
|
w.Write([]byte("ERROR: Requested block height is bigger then the chain length."))
|
|
|
|
return
|
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
output, err := getBlock(cliCtx, &height)
|
2018-04-25 07:49:31 -07:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Write(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) {
|
2018-08-06 11:11:30 -07:00
|
|
|
height, err := GetChainHeight(cliCtx)
|
2018-04-25 07:49:31 -07:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
2018-08-06 11:11:30 -07:00
|
|
|
output, err := getBlock(cliCtx, &height)
|
2018-04-25 07:49:31 -07:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Write(output)
|
2018-02-28 23:16:54 -08:00
|
|
|
}
|
2018-02-23 10:23:24 -08:00
|
|
|
}
|