From 55ea9c8ff0d9e0956641122fd9e463aa311f88c4 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Fri, 26 Jul 2024 11:25:59 +0200 Subject: [PATCH 1/2] feat: check latest block if no arg in `q block` and `q block-results` --- server/cmt_cmds.go | 61 +++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/server/cmt_cmds.go b/server/cmt_cmds.go index 1d2eb70b04c7..3f18ff25c975 100644 --- a/server/cmt_cmds.go +++ b/server/cmt_cmds.go @@ -227,7 +227,7 @@ $ %s query block --%s=%s `, version.AppName, auth.FlagType, auth.TypeHeight, version.AppName, auth.FlagType, auth.TypeHash)), - Args: cobra.ExactArgs(1), + Args: cobra.MaximumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { clientCtx, err := client.GetClientQueryContext(cmd) if err != nil { @@ -235,24 +235,34 @@ $ %s query block --%s=%s } typ, _ := cmd.Flags().GetString(auth.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 = auth.TypeHeight + } switch typ { case auth.TypeHeight: - - if args[0] == "" { - return errors.New("argument should be a block height") - } - - // optional height - var height *int64 - if len(args) > 0 { - height, err = parseOptionalHeight(args[0]) + var ( + err error + height int64 + ) + heightStr := args[0] + + if heightStr == "" { + cmd.Println("Falling back to latest block height:") + height, err = rpc.GetChainHeight(clientCtx) + if err != nil { + return fmt.Errorf("failed to get chain height: %w", err) + } + } else { + height, err = strconv.ParseInt(heightStr, 10, 64) if err != nil { - return err + return fmt.Errorf("failed to parse block height: %w", err) } } - output, err := rpc.GetBlockByHeight(clientCtx, height) + output, err := rpc.GetBlockByHeight(clientCtx, &height) if err != nil { return err } @@ -312,15 +322,21 @@ func QueryBlockResultsCmd() *cobra.Command { } // optional height - var height *int64 + var height int64 if len(args) > 0 { - height, err = parseOptionalHeight(args[0]) + height, err = strconv.ParseInt(args[0], 10, 64) if err != nil { return err } + } else { + cmd.Println("Falling back to latest block height:") + height, err = rpc.GetChainHeight(clientCtx) + if err != nil { + return fmt.Errorf("failed to get chain height: %w", err) + } } - blockRes, err := node.BlockResults(context.Background(), height) + blockRes, err := node.BlockResults(context.Background(), &height) if err != nil { return err } @@ -342,21 +358,6 @@ func QueryBlockResultsCmd() *cobra.Command { 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[T types.Application](appCreator types.AppCreator[T]) *cobra.Command { cmd := &cobra.Command{ Use: "bootstrap-state", From 312e73d228936779e23c06b2ba1a12a8ef362438 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Fri, 26 Jul 2024 12:25:45 +0200 Subject: [PATCH 2/2] suggestion --- server/cmt_cmds.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/server/cmt_cmds.go b/server/cmt_cmds.go index 3f18ff25c975..abbc4a9f5da3 100644 --- a/server/cmt_cmds.go +++ b/server/cmt_cmds.go @@ -247,7 +247,10 @@ $ %s query block --%s=%s err error height int64 ) - heightStr := args[0] + heightStr := "" + if len(args) > 0 { + heightStr = args[0] + } if heightStr == "" { cmd.Println("Falling back to latest block height:")