-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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(server/v2/store): Add ModuleHashByHeightQuery
command
#23479
Conversation
ModuleHashByHeightQuery
ModuleHashByHeightQuery
command
📝 WalkthroughWalkthroughThis pull request adds a new command Changes
Possibly related PRs
Suggested Labels
Suggested Reviewers
✨ Finishing Touches
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
|
@julienrbrt your pull request is missing a changelog! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (1)
server/v2/store/commands.go (1)
124-165
: Optimize memory usage and improve documentation.Several improvements can be made to this function:
- The hex-encoded hash is unnecessarily converted back to bytes
- Memory allocation can be optimized
- Missing documentation for exported function
Here's the suggested implementation:
+// getModuleHashesAtHeight retrieves the commit info at the specified height and +// returns it with hex-encoded hashes sorted by store name. func getModuleHashesAtHeight(vp *viper.Viper, logger log.Logger, height uint64) (*proof.CommitInfo, error) { rootStore, _, err := createRootStore(vp, logger) if err != nil { return nil, fmt.Errorf("can not create root store %w", err) } commitInfoForHeight, err := rootStore.GetStateCommitment().GetCommitInfo(height) if err != nil { return nil, err } - // Create a new slice of StoreInfos for storing the modified hashes. - storeInfos := make([]*proof.StoreInfo, len(commitInfoForHeight.StoreInfos)) + // Modify the existing StoreInfos slice in-place + storeInfos := commitInfoForHeight.StoreInfos for i, storeInfo := range commitInfoForHeight.StoreInfos { - // Convert the hash to a hexadecimal string. hash := strings.ToUpper(hex.EncodeToString(storeInfo.CommitId.Hash)) - - // Create a new StoreInfo with the modified hash. - storeInfos[i] = &proof.StoreInfo{ - Name: storeInfo.Name, - CommitId: &proof.CommitID{ - Version: storeInfo.CommitId.Version, - Hash: []byte(hash), - }, - } + storeInfo.CommitId.Hash = []byte(hash) + storeInfos[i] = storeInfo } // Sort the storeInfos slice based on the module name. sort.Slice(storeInfos, func(i, j int) bool { return storeInfos[i].Name < storeInfos[j].Name }) - // Create a new CommitInfo with the modified StoreInfos. - commitInfoForHeight = &proof.CommitInfo{ - Version: commitInfoForHeight.Version, - StoreInfos: storeInfos, - Timestamp: commitInfoForHeight.Timestamp, - } return commitInfoForHeight, nil }
📜 Review details
Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
server/v2/store/commands.go
(2 hunks)server/v2/store/server.go
(1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
server/v2/store/server.go (1)
Pattern **/*.go
: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.
server/v2/store/commands.go (1)
Pattern **/*.go
: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.
⏰ Context from checks skipped due to timeout of 90000ms (12)
- GitHub Check: tests (03)
- GitHub Check: tests (02)
- GitHub Check: tests (01)
- GitHub Check: tests (00)
- GitHub Check: test-simapp-v2
- GitHub Check: test-sim-nondeterminism
- GitHub Check: test-system-v2
- GitHub Check: build (arm64)
- GitHub Check: test-integration
- GitHub Check: build (amd64)
- GitHub Check: Analyze
- GitHub Check: Summary
🔇 Additional comments (2)
server/v2/store/server.go (1)
63-63
: LGTM! Command registration follows established patterns.The addition of
ModuleHashByHeightQuery
to the CLI commands is consistent with the existing command registration pattern.server/v2/store/commands.go (1)
4-9
: LGTM! Import organization follows style guide.The new imports are properly grouped and all are necessary for the new functionality.
// ModuleHashByHeightQuery retrieves the module hashes at a given height. | ||
func (s *Server[T]) ModuleHashByHeightQuery() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "module-hash-by-height <height>", | ||
Short: "Get module hashes at a given height", | ||
Long: "Get module hashes at a given height. This command is useful for debugging and verifying the state of the application at a given height. Daemon should not be running when calling this command.", | ||
Example: "<appd module-hash-by-height 16841115", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
vp := serverv2.GetViperFromCmd(cmd) | ||
if err := vp.BindPFlags(cmd.Flags()); err != nil { | ||
return err | ||
} | ||
if err := vp.BindPFlags(cmd.PersistentFlags()); err != nil { | ||
return err | ||
} | ||
|
||
heightToRetrieveString := args[0] | ||
height, err := strconv.ParseInt(heightToRetrieveString, 10, 64) | ||
if err != nil { | ||
return fmt.Errorf("invalid height: %w", err) | ||
} | ||
|
||
commitInfoForHeight, err := getModuleHashesAtHeight(vp, serverv2.GetLoggerFromCmd(cmd), uint64(height)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
bytes, err := json.Marshal(commitInfoForHeight) | ||
if err != nil { | ||
return fmt.Errorf("failed to marshal commit info: %w", err) | ||
} | ||
|
||
cmd.Println(string(bytes)) | ||
return nil | ||
}, | ||
} | ||
|
||
return cmd | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add height validation checks.
The height parameter needs additional validation:
- Check for negative values after parsing
- Verify the height doesn't exceed the latest height
- Improve error messages to be more descriptive
Here's the suggested implementation:
func (s *Server[T]) ModuleHashByHeightQuery() *cobra.Command {
cmd := &cobra.Command{
Use: "module-hash-by-height <height>",
Short: "Get module hashes at a given height",
Long: "Get module hashes at a given height. This command is useful for debugging and verifying the state of the application at a given height. Daemon should not be running when calling this command.",
Example: "<appd module-hash-by-height 16841115",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
vp := serverv2.GetViperFromCmd(cmd)
if err := vp.BindPFlags(cmd.Flags()); err != nil {
return err
}
if err := vp.BindPFlags(cmd.PersistentFlags()); err != nil {
return err
}
heightToRetrieveString := args[0]
height, err := strconv.ParseInt(heightToRetrieveString, 10, 64)
if err != nil {
- return fmt.Errorf("invalid height: %w", err)
+ return fmt.Errorf("failed to parse height %q: %w", heightToRetrieveString, err)
}
+ if height < 0 {
+ return fmt.Errorf("height cannot be negative: %d", height)
+ }
+
+ rootStore, _, err := createRootStore(vp, serverv2.GetLoggerFromCmd(cmd))
+ if err != nil {
+ return fmt.Errorf("failed to create root store: %w", err)
+ }
+
+ latestHeight, err := rootStore.GetLatestVersion()
+ if err != nil {
+ return fmt.Errorf("failed to get latest height: %w", err)
+ }
+
+ if uint64(height) > latestHeight {
+ return fmt.Errorf("height %d is greater than latest height %d", height, latestHeight)
+ }
commitInfoForHeight, err := getModuleHashesAtHeight(vp, serverv2.GetLoggerFromCmd(cmd), uint64(height))
if err != nil {
return err
}
bytes, err := json.Marshal(commitInfoForHeight)
if err != nil {
return fmt.Errorf("failed to marshal commit info: %w", err)
}
cmd.Println(string(bytes))
return nil
},
}
return cmd
}
Committable suggestion skipped: line range outside the PR's diff.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🔭 Outside diff range comments (1)
server/v2/store/snapshot.go (1)
Line range hint
36-51
: Add overflow checks for height conversions.The code should validate height values when converting between signed and unsigned integers to prevent silent overflow issues. This includes both the command-line input and the
lastCommitId.Version
.Apply this diff to add overflow checks:
height, err := cmd.Flags().GetInt64("height") if err != nil { return err } + if height < 0 { + return fmt.Errorf("height cannot be negative: %d", height) + } logger := serverv2.GetLoggerFromCmd(cmd) rootStore, _, err := createRootStore(v, logger) if err != nil { return err } if height == 0 { lastCommitId, err := rootStore.LastCommitID() if err != nil { return err } + if lastCommitId.Version > uint64(math.MaxInt64) { + return fmt.Errorf("height %d exceeds maximum allowed value", lastCommitId.Version) + } height = int64(lastCommitId.Version) }
📜 Review details
Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
server/v2/store/snapshot.go
(1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
server/v2/store/snapshot.go (1)
Pattern **/*.go
: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.
⏰ Context from checks skipped due to timeout of 90000ms (13)
- GitHub Check: tests (03)
- GitHub Check: tests (02)
- GitHub Check: tests (01)
- GitHub Check: tests (00)
- GitHub Check: test-simapp-v2
- GitHub Check: test-system-v2
- GitHub Check: test-sim-nondeterminism
- GitHub Check: test-integration
- GitHub Check: build (arm64)
- GitHub Check: build (amd64)
- GitHub Check: Analyze
- GitHub Check: golangci-lint
- GitHub Check: Summary
@@ -48,7 +48,7 @@ func (s *Server[T]) ExportSnapshotCmd() *cobra.Command { | |||
if err != nil { | |||
return err | |||
} | |||
height = int64(lastCommitId.Version) | |||
height = lastCommitId.Version |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Restore the explicit type conversion for type safety.
The removal of the explicit type conversion from uint64
to int64
could lead to compilation errors and potential data loss for heights greater than MaxInt64. The height
variable is declared as int64
from flag parsing, so we should maintain consistent type handling.
Apply this diff to restore type safety:
- height = lastCommitId.Version
+ height = int64(lastCommitId.Version)
📝 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.
height = lastCommitId.Version | |
height = int64(lastCommitId.Version) |
Description
Closes: #23019
Author Checklist
All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.
I have...
!
in the type prefix if API or client breaking changeCHANGELOG.md
Reviewers Checklist
All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.
Please see Pull Request Reviewer section in the contributing guide for more information on how to review a pull request.
I have...
Summary by CodeRabbit
New Features
Improvements
The new feature allows users to inspect module hashes at a specific blockchain height, providing improved debugging and state verification capabilities.