Skip to content

Commit 91d412c

Browse files
mergify[bot]julienrbrtsontrinh16
authored
feat: check latest block if no arg in q block and q block-results (backport #21084) (#21111)
Co-authored-by: Julien Robert <[email protected]> Co-authored-by: sontrinh16 <[email protected]>
1 parent e135030 commit 91d412c

File tree

3 files changed

+34
-30
lines changed

3 files changed

+34
-30
lines changed

server/cmt_cmds.go

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -226,32 +226,45 @@ $ %s query block --%s=%s <hash>
226226
`,
227227
version.AppName, auth.FlagType, auth.TypeHeight,
228228
version.AppName, auth.FlagType, auth.TypeHash)),
229-
Args: cobra.ExactArgs(1),
229+
Args: cobra.MaximumNArgs(1),
230230
RunE: func(cmd *cobra.Command, args []string) error {
231231
clientCtx, err := client.GetClientQueryContext(cmd)
232232
if err != nil {
233233
return err
234234
}
235235

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

238243
switch typ {
239244
case auth.TypeHeight:
240-
241-
if args[0] == "" {
242-
return fmt.Errorf("argument should be a block height")
245+
var (
246+
err error
247+
height int64
248+
)
249+
heightStr := ""
250+
if len(args) > 0 {
251+
heightStr = args[0]
243252
}
244253

245-
// optional height
246-
var height *int64
247-
if len(args) > 0 {
248-
height, err = parseOptionalHeight(args[0])
254+
if heightStr == "" {
255+
cmd.Println("Falling back to latest block height:")
256+
height, err = rpc.GetChainHeight(clientCtx)
257+
if err != nil {
258+
return fmt.Errorf("failed to get chain height: %w", err)
259+
}
260+
} else {
261+
height, err = strconv.ParseInt(heightStr, 10, 64)
249262
if err != nil {
250-
return err
263+
return fmt.Errorf("failed to parse block height: %w", err)
251264
}
252265
}
253266

254-
output, err := rpc.GetBlockByHeight(clientCtx, height)
267+
output, err := rpc.GetBlockByHeight(clientCtx, &height)
255268
if err != nil {
256269
return err
257270
}
@@ -311,15 +324,21 @@ func QueryBlockResultsCmd() *cobra.Command {
311324
}
312325

313326
// optional height
314-
var height *int64
327+
var height int64
315328
if len(args) > 0 {
316-
height, err = parseOptionalHeight(args[0])
329+
height, err = strconv.ParseInt(args[0], 10, 64)
317330
if err != nil {
318331
return err
319332
}
333+
} else {
334+
cmd.Println("Falling back to latest block height:")
335+
height, err = rpc.GetChainHeight(clientCtx)
336+
if err != nil {
337+
return fmt.Errorf("failed to get chain height: %w", err)
338+
}
320339
}
321340

322-
blockRes, err := node.BlockResults(context.Background(), height)
341+
blockRes, err := node.BlockResults(context.Background(), &height)
323342
if err != nil {
324343
return err
325344
}
@@ -341,21 +360,6 @@ func QueryBlockResultsCmd() *cobra.Command {
341360
return cmd
342361
}
343362

344-
func parseOptionalHeight(heightStr string) (*int64, error) {
345-
h, err := strconv.Atoi(heightStr)
346-
if err != nil {
347-
return nil, err
348-
}
349-
350-
if h == 0 {
351-
return nil, nil
352-
}
353-
354-
tmp := int64(h)
355-
356-
return &tmp, nil
357-
}
358-
359363
func BootstrapStateCmd(appCreator types.AppCreator) *cobra.Command {
360364
cmd := &cobra.Command{
361365
Use: "bootstrap-state",

server/grpc/server_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (s *IntegrationTestSuite) SetupSuite() {
5151
s.Require().NoError(err)
5252

5353
val0 := s.network.Validators[0]
54-
s.conn, err = grpc.Dial(
54+
s.conn, err = grpc.Dial( //nolint:staticcheck // ignore this line for this linter
5555
val0.AppConfig.GRPC.Address,
5656
grpc.WithInsecure(), //nolint:staticcheck // ignore SA1019, we don't need to use a secure connection for tests
5757
grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(s.cfg.InterfaceRegistry).GRPCCodec())),

server/start.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ func startGrpcServer(
476476
}
477477

478478
// if gRPC is enabled, configure gRPC client for gRPC gateway
479-
grpcClient, err := grpc.Dial(
479+
grpcClient, err := grpc.Dial( //nolint: staticcheck // ignore this line for this linter
480480
config.Address,
481481
grpc.WithTransportCredentials(insecure.NewCredentials()),
482482
grpc.WithDefaultCallOptions(

0 commit comments

Comments
 (0)