-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
refactor(x/distribution): audit QA #21316
Conversation
WalkthroughWalkthroughThe recent changes primarily involve enhancements to error handling, updates to documentation links for Protocol Buffer files, and the addition of new test functions in the Cosmos SDK distribution module. Notable improvements include clearer error messages related to validator management, the introduction of constants for better code readability, and the addition of tests to ensure correct token allocation logic. These updates help maintain code reliability and improve developer experience without altering existing functionality. Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant Validator
participant Keeper
User->>Keeper: Request token allocation
Keeper->>Validator: Check commission rate
Validator-->>Keeper: Return commission rate
alt Commission is 0%
Keeper->>Validator: Allocate tokens
else Allocation is zero
Keeper->>Validator: No allocation done
end
Keeper-->>User: Return allocation result
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 as PR comments)
Additionally, you can add CodeRabbit Configuration File (
|
if historical.ReferenceCount > 2 { | ||
panic("reference count should never exceed 2") | ||
if historical.ReferenceCount > maxReferenceCount { | ||
return fmt.Errorf("reference count should never exceed %d", maxReferenceCount) |
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.
I'm not sure about this change, but I think we should be consistent
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.
This is a constant that has been there forever, not sure either, I guess it's fine. Maybe it gives us an opportunity to add a small comment on why it's 2
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.
Sorry if I didn't explain well hehe, I talked about the panic replace
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.
Was the function returning an error before?
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.
Yes, the function has three returns: two of them return an error, and this one returned a panic. That's why I think we should return an error to maintain consistency. However, I'm a bit confused about when we should use panic and when we shouldn't, or if this is a technical debt we've had from before
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.
If you can look at the file history to see when it happened then we can easily know if it was on purpose on not. I haven't checked the code path, mostly returning an error or panicking results to the same thing (since v0.47) while before we had to panic, so it is likely that it is just something that should have been refactored but haven't but it would be good to verify.
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.
Reviewing the history, the panic has been there since 01/23/19, from the very first time the function was created. Therefore, I think the refactor is fine. Now, just to understand, so today in the SDK it's the same to return panic as to return an error, but we are standardizing everything to return errors, right?
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 (7)
- x/distribution/README.md (4 hunks)
- x/distribution/keeper/allocation_test.go (1 hunks)
- x/distribution/keeper/grpc_query.go (2 hunks)
- x/distribution/keeper/keeper.go (1 hunks)
- x/distribution/keeper/msg_server.go (1 hunks)
- x/distribution/keeper/validator.go (4 hunks)
- x/distribution/migrations/v4/migrate.go (2 hunks)
Files skipped from review due to trivial changes (3)
- x/distribution/README.md
- x/distribution/keeper/keeper.go
- x/distribution/migrations/v4/migrate.go
Additional context used
Path-based instructions (4)
x/distribution/keeper/validator.go (1)
Pattern
**/*.go
: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.x/distribution/keeper/msg_server.go (1)
Pattern
**/*.go
: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.x/distribution/keeper/grpc_query.go (1)
Pattern
**/*.go
: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.x/distribution/keeper/allocation_test.go (2)
Pattern
**/*.go
: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.
Pattern
**/*_test.go
: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"
Additional comments not posted (8)
x/distribution/keeper/validator.go (3)
16-18
: Good use of constants for maintainability.The introduction of
maxReferenceCount
enhances code readability and maintainability by avoiding magic numbers.
133-134
: Improved error handling inincrementReferenceCount
.Replacing the panic with an error return enhances robustness and aligns with Go best practices.
147-147
: Improved error handling indecrementReferenceCount
.Replacing the panic with an error return when the reference count is zero enhances robustness and consistency in error management.
x/distribution/keeper/msg_server.go (1)
192-192
: Enhanced error message clarity inDepositValidatorRewardsPool
.Including the validator address in the error message provides better context for debugging.
x/distribution/keeper/grpc_query.go (2)
122-122
: Enhanced error message clarity inValidatorOutstandingRewards
.Including the validator address in the error message provides better context for debugging.
154-154
: Enhanced error message clarity inValidatorCommission
.Including the validator address in the error message provides better context for debugging.
x/distribution/keeper/allocation_test.go (2)
379-442
: Good test coverage and adherence to best practices.The test
TestAllocateTokensToValidatorWithoutCommission
is well-structured, ensuring that the allocation logic for a validator with 0% commission is correctly verified. The use of mocks and assertions is appropriate.
444-505
: Effective testing for zero token allocation.The test
TestAllocateTokensWithZeroTokens
effectively verifies that both commission and rewards remain unchanged when zero tokens are allocated. The test setup and assertions are well-implemented.
Co-authored-by: marbar3778 <[email protected]>
Co-authored-by: github-merge-queue <[email protected]>
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.
Mostly lgtm, but I think we can revert some things
if historical.ReferenceCount > 2 { | ||
panic("reference count should never exceed 2") | ||
if historical.ReferenceCount > maxReferenceCount { | ||
return fmt.Errorf("reference count should never exceed %d", maxReferenceCount) |
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.
Was the function returning an error before?
Co-authored-by: Randy Grok <[email protected]> Co-authored-by: marbar3778 <[email protected]> Co-authored-by: Cosmos SDK <[email protected]> Co-authored-by: github-merge-queue <[email protected]> Co-authored-by: Julián Toledano <[email protected]> Co-authored-by: Julien Robert <[email protected]> Co-authored-by: Aaron Craelius <[email protected]> (cherry picked from commit ee04cee)
Co-authored-by: Lucas Francisco López <[email protected]>
* main: (76 commits) docs: more app v2 renaming (#21336) chore: update link in disclaimer (#21339) refactor(x/distribution): audit QA (#21316) docs: rename app v2 to app di when talking about runtime v0 (#21329) feat(schema): specify JSON mapping (#21243) fix(x/authz): bring back msg response in `DispatchActions` (#21044) chore: fix all lint issue since golangci-lint bump (#21326) refactor(x/mint): v0.52 audit x/mint (#21301) chore: fix spelling errors (#21327) feat: export genesis in simapp v2 (#21199) fix(baseapp)!: Halt at height now does not produce the halt height block (#21256) refactor(scripts): remove unused variable (#21320) chore(schema/testing): upgrade to go 1.23 iterators (#21282) chore: readmes + upgrading docs (#21271) feat(client): add auto cli for node service (#21074) ci: fix github workflow vulnerable to script injection (#21304) build(deps): Bump github.com/prometheus/client_golang from 1.19.1 to 1.20.0 (#21307) build(deps): use Go 1.23 instead of Go 1.22 (#21280) refactor(x/auth): audit x/auth changes (#21146) chore: remove todo: "abstract out staking message back to staking" (#21266) ...
Description
PR related to #20955
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
Documentation
Tests
Bug Fixes
Chores