Skip to content

Commit 55ea9c8

Browse files
committed
feat: check latest block if no arg in q block and q block-results
1 parent cc5c4d0 commit 55ea9c8

1 file changed

Lines changed: 31 additions & 30 deletions

File tree

server/cmt_cmds.go

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -227,32 +227,42 @@ $ %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")
244-
}
245-
246-
// optional height
247-
var height *int64
248-
if len(args) > 0 {
249-
height, err = parseOptionalHeight(args[0])
246+
var (
247+
err error
248+
height int64
249+
)
250+
heightStr := args[0]
251+
252+
if heightStr == "" {
253+
cmd.Println("Falling back to latest block height:")
254+
height, err = rpc.GetChainHeight(clientCtx)
255+
if err != nil {
256+
return fmt.Errorf("failed to get chain height: %w", err)
257+
}
258+
} else {
259+
height, err = strconv.ParseInt(heightStr, 10, 64)
250260
if err != nil {
251-
return err
261+
return fmt.Errorf("failed to parse block height: %w", err)
252262
}
253263
}
254264

255-
output, err := rpc.GetBlockByHeight(clientCtx, height)
265+
output, err := rpc.GetBlockByHeight(clientCtx, &height)
256266
if err != nil {
257267
return err
258268
}
@@ -312,15 +322,21 @@ func QueryBlockResultsCmd() *cobra.Command {
312322
}
313323

314324
// optional height
315-
var height *int64
325+
var height int64
316326
if len(args) > 0 {
317-
height, err = parseOptionalHeight(args[0])
327+
height, err = strconv.ParseInt(args[0], 10, 64)
318328
if err != nil {
319329
return err
320330
}
331+
} else {
332+
cmd.Println("Falling back to latest block height:")
333+
height, err = rpc.GetChainHeight(clientCtx)
334+
if err != nil {
335+
return fmt.Errorf("failed to get chain height: %w", err)
336+
}
321337
}
322338

323-
blockRes, err := node.BlockResults(context.Background(), height)
339+
blockRes, err := node.BlockResults(context.Background(), &height)
324340
if err != nil {
325341
return err
326342
}
@@ -342,21 +358,6 @@ func QueryBlockResultsCmd() *cobra.Command {
342358
return cmd
343359
}
344360

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-
360361
func BootstrapStateCmd[T types.Application](appCreator types.AppCreator[T]) *cobra.Command {
361362
cmd := &cobra.Command{
362363
Use: "bootstrap-state",

0 commit comments

Comments
 (0)