From 91d412cc7ddced3b4cf9c47b26cdb3889c0a840a Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Tue, 30 Jul 2024 10:15:39 +0200 Subject: [PATCH] feat: check latest block if no arg in `q block` and `q block-results` (backport #21084) (#21111) Co-authored-by: Julien Robert Co-authored-by: sontrinh16 --- server/cmt_cmds.go | 60 ++++++++++++++++++++------------------ server/grpc/server_test.go | 2 +- server/start.go | 2 +- 3 files changed, 34 insertions(+), 30 deletions(-) diff --git a/server/cmt_cmds.go b/server/cmt_cmds.go index 359a1554bccc..a5e2026b248a 100644 --- a/server/cmt_cmds.go +++ b/server/cmt_cmds.go @@ -226,7 +226,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 { @@ -234,24 +234,37 @@ $ %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 fmt.Errorf("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(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 } @@ -311,15 +324,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 } @@ -341,21 +360,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(appCreator types.AppCreator) *cobra.Command { cmd := &cobra.Command{ Use: "bootstrap-state", diff --git a/server/grpc/server_test.go b/server/grpc/server_test.go index f79cd8d2c767..6c45094b24f0 100644 --- a/server/grpc/server_test.go +++ b/server/grpc/server_test.go @@ -51,7 +51,7 @@ func (s *IntegrationTestSuite) SetupSuite() { s.Require().NoError(err) val0 := s.network.Validators[0] - s.conn, err = grpc.Dial( + s.conn, err = grpc.Dial( //nolint:staticcheck // ignore this line for this linter val0.AppConfig.GRPC.Address, grpc.WithInsecure(), //nolint:staticcheck // ignore SA1019, we don't need to use a secure connection for tests grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(s.cfg.InterfaceRegistry).GRPCCodec())), diff --git a/server/start.go b/server/start.go index c2a812a7b5f8..80e29b9c1299 100644 --- a/server/start.go +++ b/server/start.go @@ -476,7 +476,7 @@ func startGrpcServer( } // if gRPC is enabled, configure gRPC client for gRPC gateway - grpcClient, err := grpc.Dial( + grpcClient, err := grpc.Dial( //nolint: staticcheck // ignore this line for this linter config.Address, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithDefaultCallOptions(