Skip to content

Commit c4de9a9

Browse files
authored
feat: check latest block if no arg in q block and q block-results (#21084)
1 parent b298945 commit c4de9a9

1 file changed

Lines changed: 32 additions & 28 deletions

File tree

server/cmt_cmds.go

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

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

239244
switch typ {
240245
case auth.TypeHeight:
241-
242-
if args[0] == "" {
243-
return errors.New("argument should be a block height")
246+
var (
247+
err error
248+
height int64
249+
)
250+
heightStr := ""
251+
if len(args) > 0 {
252+
heightStr = args[0]
244253
}
245254

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

255-
output, err := rpc.GetBlockByHeight(clientCtx, height)
268+
output, err := rpc.GetBlockByHeight(clientCtx, &height)
256269
if err != nil {
257270
return err
258271
}
@@ -312,15 +325,21 @@ func QueryBlockResultsCmd() *cobra.Command {
312325
}
313326

314327
// optional height
315-
var height *int64
328+
var height int64
316329
if len(args) > 0 {
317-
height, err = parseOptionalHeight(args[0])
330+
height, err = strconv.ParseInt(args[0], 10, 64)
318331
if err != nil {
319332
return err
320333
}
334+
} else {
335+
cmd.Println("Falling back to latest block height:")
336+
height, err = rpc.GetChainHeight(clientCtx)
337+
if err != nil {
338+
return fmt.Errorf("failed to get chain height: %w", err)
339+
}
321340
}
322341

323-
blockRes, err := node.BlockResults(context.Background(), height)
342+
blockRes, err := node.BlockResults(context.Background(), &height)
324343
if err != nil {
325344
return err
326345
}
@@ -342,21 +361,6 @@ func QueryBlockResultsCmd() *cobra.Command {
342361
return cmd
343362
}
344363

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

0 commit comments

Comments
 (0)