Skip to content

Commit 2b5920c

Browse files
julienrbrtmergify[bot]
authored andcommitted
feat: check latest block if no arg in q block and q block-results (#21084)
(cherry picked from commit c4de9a9) # Conflicts: # server/cmt_cmds.go
1 parent 7d801cc commit 2b5920c

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

server/cmt_cmds.go

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -226,32 +226,51 @@ $ %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:
245+
<<<<<<< HEAD
240246

241247
if args[0] == "" {
242248
return fmt.Errorf("argument should be a block height")
249+
=======
250+
var (
251+
err error
252+
height int64
253+
)
254+
heightStr := ""
255+
if len(args) > 0 {
256+
heightStr = args[0]
257+
>>>>>>> c4de9a970 (feat: check latest block if no arg in `q block` and `q block-results` (#21084))
243258
}
244259

245-
// optional height
246-
var height *int64
247-
if len(args) > 0 {
248-
height, err = parseOptionalHeight(args[0])
260+
if heightStr == "" {
261+
cmd.Println("Falling back to latest block height:")
262+
height, err = rpc.GetChainHeight(clientCtx)
249263
if err != nil {
250-
return err
264+
return fmt.Errorf("failed to get chain height: %w", err)
265+
}
266+
} else {
267+
height, err = strconv.ParseInt(heightStr, 10, 64)
268+
if err != nil {
269+
return fmt.Errorf("failed to parse block height: %w", err)
251270
}
252271
}
253272

254-
output, err := rpc.GetBlockByHeight(clientCtx, height)
273+
output, err := rpc.GetBlockByHeight(clientCtx, &height)
255274
if err != nil {
256275
return err
257276
}
@@ -311,15 +330,21 @@ func QueryBlockResultsCmd() *cobra.Command {
311330
}
312331

313332
// optional height
314-
var height *int64
333+
var height int64
315334
if len(args) > 0 {
316-
height, err = parseOptionalHeight(args[0])
335+
height, err = strconv.ParseInt(args[0], 10, 64)
317336
if err != nil {
318337
return err
319338
}
339+
} else {
340+
cmd.Println("Falling back to latest block height:")
341+
height, err = rpc.GetChainHeight(clientCtx)
342+
if err != nil {
343+
return fmt.Errorf("failed to get chain height: %w", err)
344+
}
320345
}
321346

322-
blockRes, err := node.BlockResults(context.Background(), height)
347+
blockRes, err := node.BlockResults(context.Background(), &height)
323348
if err != nil {
324349
return err
325350
}
@@ -341,21 +366,6 @@ func QueryBlockResultsCmd() *cobra.Command {
341366
return cmd
342367
}
343368

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

0 commit comments

Comments
 (0)