-
Notifications
You must be signed in to change notification settings - Fork 4.1k
refactor(staking): check for nil ptrs after GetCachedValue() #21300
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
Conversation
Minor improvements
WalkthroughWalkthroughThe recent changes focus on enhancing error handling and type safety across various functions in the staking module. Key updates include checks for nil values before performing type assertions on cached public keys, which prevent potential runtime errors. Improved error messaging ensures that issues are conveyed more clearly, promoting better debugging and maintainability. Overall, these modifications significantly strengthen the reliability of the code related to validator public keys. Changes
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
|
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: 0
Review details
Configuration used: .coderabbit.yml
Review profile: CHILL
Files selected for processing (4)
- x/staking/keeper/cons_pubkey.go (1 hunks)
- x/staking/keeper/grpc_query.go (2 hunks)
- x/staking/keeper/msg_server.go (4 hunks)
- x/staking/keeper/val_state_change.go (1 hunks)
Additional context used
Path-based instructions (4)
x/staking/keeper/cons_pubkey.go (1)
Pattern
**/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.x/staking/keeper/val_state_change.go (1)
Pattern
**/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.x/staking/keeper/grpc_query.go (1)
Pattern
**/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.x/staking/keeper/msg_server.go (1)
Pattern
**/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.
Learnings (1)
x/staking/keeper/msg_server.go (1)
Learnt from: alexanderbez PR: cosmos/cosmos-sdk#18506 File: x/staking/keeper/msg_server.go:64-65 Timestamp: 2023-11-20T18:25:05.369Z Learning: The error message in the `CreateValidator` function within `x/staking/keeper/msg_server.go` should clearly indicate the expected type `cryptotypes.PubKey` when a type assertion fails.
Additional comments not posted (13)
x/staking/keeper/cons_pubkey.go (2)
106-112: Excellent use of type assertion checks.The type assertion checks for the cached values are well-implemented, preventing potential runtime panics.
97-103: Great addition of nil checks and error handling.The added checks for nil cached values and the detailed error messages improve the robustness and clarity of the function.
Ensure that all usages of
updateToNewPubkeyare updated to handle these potential errors.Verification successful
Proper error handling for
updateToNewPubkeyfunction calls.The function
updateToNewPubkeyis used inx/staking/keeper/val_state_change.gowith appropriate error handling, ensuring robustness in the code.
- File:
x/staking/keeper/val_state_change.go
- Line: The function call is wrapped in an error check that returns the error if one occurs.
Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify all function calls to `updateToNewPubkey` handle potential errors. # Test: Search for the function usage. Expect: Proper error handling in all occurrences. rg --type go -A 5 $'updateToNewPubkey'Length of output: 1343
x/staking/keeper/val_state_change.go (2)
278-284: Excellent use of type assertion checks.The type assertion checks for the cached values are well-implemented, preventing potential runtime panics.
269-275: Good addition of nil checks and error handling.The nil checks for cached values and the detailed error messages enhance the robustness and clarity of the function.
Ensure that all usages of
ApplyAndReturnValidatorSetUpdatesare updated to handle these potential errors.Verification successful
Error handling for
ApplyAndReturnValidatorSetUpdatesis correctly implemented.All instances of
ApplyAndReturnValidatorSetUpdatesin the codebase include proper error handling, ensuring robust and reliable execution.
- Verified in files such as
x/staking/keeper/val_state_change.go,x/staking/keeper/test_common.go,simapp/export.go, and others.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify all function calls to `ApplyAndReturnValidatorSetUpdates` handle potential errors. # Test: Search for the function usage. Expect: Proper error handling in all occurrences. rg --type go -A 5 $'ApplyAndReturnValidatorSetUpdates'Length of output: 13727
x/staking/keeper/grpc_query.go (1)
68-73: Great addition of nil checks and error handling.The added checks for nil cached values and the detailed error messages improve the robustness and clarity of the function.
Ensure that all usages of
Validatorsare updated to handle these potential errors.x/staking/keeper/msg_server.go (8)
67-70: Nil check for cached value is a good addition.The addition of a nil check for
cvimproves robustness by preventing potential nil pointer dereference errors.
72-74: Improved error handling for type assertion.The error message now clearly indicates the expected type, which aligns with the learning about error messages.
82-84: Check for validator params enhances error handling.Ensuring that
res.Params.Validatoris not nil before proceeding prevents potential runtime errors.
646-649: Nil check for new public key is a good addition.The addition of a nil check for
cvprevents potential nil pointer dereference errors, enhancing robustness.
661-663: Check for validator params enhances error handling.Ensuring that
paramsRes.Params.Validatoris not nil before proceeding prevents potential runtime errors.
665-666: Usage ofvalidatePubKeyimproves modularity.The refactoring to use
validatePubKeyenhances code readability and maintainability.
765-784: NewvalidatePubKeyfunction enhances modularity.The function encapsulates public key validation logic, improving code readability and maintainability. It checks for supported key types and validates Ed25519 key length.
86-87: Usage ofvalidatePubKeyimproves modularity.The refactoring to use
validatePubKeyenhances code readability and maintainability.Ensure that
validatePubKeyis used consistently across the codebase.Verification successful
validatePubKeyis used consistently within its intended scope.The
validatePubKeyfunction is only used withinx/staking/keeper/msg_server.go, indicating that its usage is consistent with its intended design. No other occurrences were found elsewhere in the codebase.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify the usage of `validatePubKey` function across the codebase. # Test: Search for the function usage. Expect: Occurrences of `validatePubKey`. rg --type go -A 5 $'validatePubKey'Length of output: 1196
Script:
#!/bin/bash # Description: Search for the usage of `validatePubKey` function across the entire codebase. # Test: Search for the function usage. Expect: Occurrences of `validatePubKey` outside of `x/staking/keeper/msg_server.go`. rg --type go 'validatePubKey' --glob '!x/staking/keeper/msg_server.go'Length of output: 70
|
Maybe let's document the change somewhere, even tho I don't think there's anyone with Params.Validator empty, might be good to write it down |
(cherry picked from commit 502661b)
…#21300) (#21546) Co-authored-by: Ezequiel Raynaudo <[email protected]>
Description
pubkey.GetCachedValue()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.mdReviewers 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