Skip to content

Commit

Permalink
backport #21084
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Sep 12, 2024
1 parent aac9a4e commit ab0b2c7
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions server/v2/cometbft/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ for. Each module documents its respective events under 'xx_events.md'.
// QueryBlockCmd implements the default command for a Block query.
func QueryBlockCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "block --type={height|hash} <height|hash>",
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(`
Expand All @@ -232,32 +232,45 @@ $ %s query block --%s=%s <hash>
`,
version.AppName, FlagType, TypeHeight,
version.AppName, FlagType, TypeHash)),
Args: cobra.ExactArgs(1),
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
typ, _ := cmd.Flags().GetString(FlagType)

rpcclient, err := rpcClient(cmd)
fmt.Println("rpcclient", rpcclient, err)
if err != nil {
return err
}

typ, _ := cmd.Flags().GetString(FlagType)
if len(args) == 0 {
// do not break default v0.50 behavior of block hash
// if no args are provided, set the type to height
typ = TypeHeight
}

switch typ {
case TypeHeight:
if args[0] == "" {
return errors.New("argument should be a block height")
var (
err error
height int64
)
heightStr := ""
if len(args) > 0 {
heightStr = args[0]
}

// optional height
var height *int64
if len(args) > 0 {
height, err = parseOptionalHeight(args[0])
if heightStr == "" {
cmd.Println("Falling back to latest block height:")
height, err = rpc.GetChainHeight(cmd.Context(), rpcclient)
if err != nil {
return err
return fmt.Errorf("failed to get chain height: %w", err)
}
} else {
height, err = strconv.ParseInt(heightStr, 10, 64)
if err != nil {
return fmt.Errorf("failed to parse block height: %w", err)
}
}

output, err := rpc.GetBlockByHeight(cmd.Context(), rpcclient, height)
output, err := rpc.GetBlockByHeight(cmd.Context(), rpcclient, &height)
if err != nil {
return err
}
Expand All @@ -272,7 +285,6 @@ $ %s query block --%s=%s <hash>
}

return printOutput(cmd, bz)

case TypeHash:

if args[0] == "" {
Expand Down

0 comments on commit ab0b2c7

Please sign in to comment.