feat: Integrate tendermint Block endpoints into the cli (#14659)

Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: Julien Robert <julien@rbrt.fr>
This commit is contained in:
cipherZ
2023-02-14 19:23:45 +01:00
committed by GitHub
co-authored by Aleksandr Bezobchuk Julien Robert
parent cb23af6d97
commit 86eca4c72b
20 changed files with 1876 additions and 321 deletions
+134
View File
@@ -2,6 +2,8 @@ package server
import (
"fmt"
"strconv"
"strings"
"github.com/cometbft/cometbft/p2p"
pvm "github.com/cometbft/cometbft/privval"
@@ -10,8 +12,13 @@ import (
"sigs.k8s.io/yaml"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
rpc "github.com/cosmos/cosmos-sdk/client/rpc"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/version"
auth "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
)
// ShowNodeIDCmd - ported from CometBFT, dump node ID to stdout
@@ -115,3 +122,130 @@ func VersionCmd() *cobra.Command {
},
}
}
// QueryBlocksCmd returns a command to search through blocks by events.
func QueryBlocksCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "blocks",
Short: "Query for paginated blocks that match a set of events",
Long: `Search for blocks that match the exact given events where results are paginated.
The events query is directly passed to CometBFT's RPC BlockSearch method and must
conform to CometBFT's query syntax.
Please refer to each module's documentation for the full set of events to query
for. Each module documents its respective events under 'xx_events.md'.
`,
Example: fmt.Sprintf(
"$ %s query blocks --query \"message.sender='cosmos1...' AND block.height > 7\" --page 1 --limit 30 --order-by ASC",
version.AppName,
),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
query, _ := cmd.Flags().GetString(auth.FlagQuery)
page, _ := cmd.Flags().GetInt(flags.FlagPage)
limit, _ := cmd.Flags().GetInt(flags.FlagLimit)
orderBy, _ := cmd.Flags().GetString(auth.FlagOrderBy)
blocks, err := rpc.QueryBlocks(clientCtx, page, limit, query, orderBy)
if err != nil {
return err
}
return clientCtx.PrintProto(blocks)
},
}
flags.AddQueryFlagsToCmd(cmd)
cmd.Flags().Int(flags.FlagPage, query.DefaultPage, "Query a specific page of paginated results")
cmd.Flags().Int(flags.FlagLimit, query.DefaultLimit, "Query number of transactions results per page returned")
cmd.Flags().String(auth.FlagQuery, "", "The blocks events query per CometBFT's query semantics")
cmd.Flags().String(auth.FlagOrderBy, "", "The ordering semantics (asc|dsc)")
_ = cmd.MarkFlagRequired(auth.FlagQuery)
return cmd
}
// QueryBlockCmd implements the default command for a Block query.
func QueryBlockCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "block --type=[height|hash] [height|hash]",
Short: "Query for a committed block by height, hash, or event(s)",
Long: "Query for a specific committed block using the CometBFT RPC `block` and `block_by_hash` method",
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s query block --%s=%s <height>
$ %s query block --%s=%s <hash>
`,
version.AppName, auth.FlagType, auth.TypeHeight,
version.AppName, auth.FlagType, auth.TypeHash)),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
typ, _ := cmd.Flags().GetString(auth.FlagType)
switch typ {
case auth.TypeHeight:
if args[0] == "" {
return fmt.Errorf("argument should be a block height")
}
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 := rpc.GetBlockByHeight(clientCtx, height)
if err != nil {
return err
}
if output.Header.Height == 0 {
return fmt.Errorf("no block found with height %s", args[0])
}
return clientCtx.PrintProto(output)
case auth.TypeHash:
if args[0] == "" {
return fmt.Errorf("argument should be a tx hash")
}
// If hash is given, then query the tx by hash.
output, err := rpc.GetBlockByHash(clientCtx, args[0])
if err != nil {
return err
}
if output.Header.AppHash == nil {
return fmt.Errorf("no block found with hash %s", args[0])
}
return clientCtx.PrintProto(output)
default:
return fmt.Errorf("unknown --%s value %s", auth.FlagType, typ)
}
},
}
flags.AddQueryFlagsToCmd(cmd)
cmd.Flags().String(auth.FlagType, auth.TypeHash, fmt.Sprintf("The type to be used when querying tx, can be one of \"%s\", \"%s\"", auth.TypeHeight, auth.TypeHash))
return cmd
}