Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: check latest block if no arg in q block and q block-results #21084

Merged
merged 2 commits into from
Jul 29, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 31 additions & 30 deletions server/cmt_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,32 +227,42 @@ $ %s query block --%s=%s <hash>
`,
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 {
return err
}

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 errors.New("argument should be a block height")
}

// optional height
var height *int64
if len(args) > 0 {
height, err = parseOptionalHeight(args[0])
var (
err error
height int64
)
heightStr := 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error Handling: Improve parsing logic.

The parsing logic for heightStr is improved, but there is a potential issue if args[0] is accessed when args is empty.

-        heightStr := args[0]
+        heightStr := ""
+        if len(args) > 0 {
+            heightStr = args[0]
+        }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
var (
err error
height int64
)
heightStr := 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)
var (
err error
height int64
)
heightStr := ""
if len(args) > 0 {
heightStr = 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 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
}
Expand Down Expand Up @@ -312,15 +322,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
}
Expand All @@ -342,21 +358,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[T types.Application](appCreator types.AppCreator[T]) *cobra.Command {
cmd := &cobra.Command{
Use: "bootstrap-state",
Expand Down
Loading