Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add CLI commands: 1) simulate a transaction, 2) query block results #16887

Merged
merged 8 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add block-results query command
  • Loading branch information
larry0x committed Jul 7, 2023
commit 0e066be3b6cc16ed104f0f4e3f8532cf2b37b6f7
1 change: 1 addition & 0 deletions client/cometbft.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type CometRPC interface {
Status(context.Context) (*coretypes.ResultStatus, error)
Block(ctx context.Context, height *int64) (*coretypes.ResultBlock, error)
BlockByHash(ctx context.Context, hash []byte) (*coretypes.ResultBlock, error)
BlockResults(ctx context.Context, height *int64) (*coretypes.ResultBlockResults, error)
BlockchainInfo(ctx context.Context, minHeight, maxHeight int64) (*coretypes.ResultBlockchainInfo, error)
Commit(ctx context.Context, height *int64) (*coretypes.ResultCommit, error)
Tx(ctx context.Context, hash []byte, prove bool) (*coretypes.ResultTx, error)
Expand Down
75 changes: 68 additions & 7 deletions server/cmt_cmds.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package server

import (
"context"
"encoding/json"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -206,18 +208,13 @@ $ %s query block --%s=%s <hash>
return fmt.Errorf("argument should be a block height")
}

var height *int64

// optional height
var height *int64
if len(args) > 0 {
h, err := strconv.Atoi(args[0])
height, err = parseOptionalHeight(args[0])
if err != nil {
return err
}
if h > 0 {
tmp := int64(h)
height = &tmp
}
}

output, err := rpc.GetBlockByHeight(clientCtx, height)
Expand Down Expand Up @@ -261,6 +258,70 @@ $ %s query block --%s=%s <hash>
return cmd
}

// QueryBlockResultCmd implements the default command for a BlockResults query.
func QueryBlockResultCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "block-results [height]",
Short: "Query for a committed block's results by height",
Long: "Query for a specific committed block's results using the CometBFT RPC `block_results` method",
Args: cobra.RangeArgs(0, 1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

node, err := clientCtx.GetNode()
if err != nil {
return err
}

// optional height
var height *int64
if len(args) > 0 {
height, err = parseOptionalHeight(args[0])
if err != nil {
return err
}
}

blockRes, err := node.BlockResults(context.Background(), height)
if err != nil {
return err
}

// coretypes.ResultBlockResults doesn't implement proto.Message interface
// so we can't print it using clientCtx.PrintProto
// we choose to serialize it to json and print the json instead
blockResStr, err := json.Marshal(blockRes)
if err != nil {
return err
}

return clientCtx.PrintString(string(blockResStr) + "\n")
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}

func parseOptionalHeight(heightStr string) (*int64, error) {
h, err := strconv.Atoi(heightStr)
if err != nil {
return nil, err
}

if h == 0 {
return nil, nil
}

tmp := int64(h)

return &tmp, nil
}

func BootstrapStateCmd(appCreator types.AppCreator) *cobra.Command {
cmd := &cobra.Command{
Use: "bootstrap-state",
Expand Down
5 changes: 3 additions & 2 deletions simapp/simd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,14 @@ func queryCommand() *cobra.Command {
cmd.AddCommand(
rpc.ValidatorCommand(),
server.QueryBlockCmd(),
authcmd.QueryTxsByEventsCmd(),
server.QueryBlocksCmd(),
server.QueryBlockResultCmd(),
authcmd.QueryTxsByEventsCmd(),
authcmd.QueryTxCmd(),
)

return cmd
}
}``

func txCommand() *cobra.Command {
cmd := &cobra.Command{
Expand Down
3 changes: 2 additions & 1 deletion simapp/simd/cmd/root_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,9 @@ func queryCommand() *cobra.Command {
cmd.AddCommand(
rpc.ValidatorCommand(),
server.QueryBlockCmd(),
authcmd.QueryTxsByEventsCmd(),
server.QueryBlocksCmd(),
server.QueryBlockResultCmd(),
authcmd.QueryTxsByEventsCmd(),
authcmd.QueryTxCmd(),
)

Expand Down
Loading