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

refactor(x/distribution): audit QA #21316

Merged
merged 12 commits into from
Aug 17, 2024
Merged

refactor(x/distribution): audit QA #21316

merged 12 commits into from
Aug 17, 2024

Conversation

lucaslopezf
Copy link
Contributor

@lucaslopezf lucaslopezf commented Aug 15, 2024

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...

  • included the correct type prefix in the PR title, you can find examples of the prefixes below:
  • confirmed ! in the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • updated the relevant documentation or specification, including comments for documenting Go code
  • confirmed all CI checks have passed

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...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

Summary by CodeRabbit

  • Documentation

    • Updated links in the distribution module documentation to reference the latest Protocol Buffer files, ensuring accuracy in schema definitions.
  • Tests

    • Added new test cases to validate token allocation logic under various commission scenarios, enhancing overall test coverage.
  • Bug Fixes

    • Improved error handling in several functions to provide clearer and more informative error messages for better debugging and context understanding.
  • Chores

    • Refined internal code comments for clarity and consistency, including corrections to typographical errors in documentation.

Copy link
Contributor

coderabbitai bot commented Aug 15, 2024

Walkthrough

Walkthrough

The 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

Files Change Summary
x/distribution/README.md Updated links to Protocol Buffer files, reflecting a transition from version 0.47.0-rc1 to release/v0.52.x.
x/distribution/keeper/allocation_test.go Added two tests: one for allocating tokens to a validator with 0% commission, another for zero tokens allocation.
x/distribution/keeper/grpc_query.go Enhanced error messages in ValidatorOutstandingRewards and ValidatorCommission methods for clarity.
x/distribution/keeper/keeper.go Fixed typographical error in comment for ValidatorOutstandingRewards field.
x/distribution/keeper/msg_server.go Improved error handling in DepositValidatorRewardsPool function with clearer messages for missing validators.
x/distribution/keeper/validator.go Introduced maxReferenceCount constant, improved error handling in incrementReferenceCount and decrementReferenceCount methods.
x/distribution/migrations/v4/migrate.go Renamed variable store to kvStore for clarity, updated function signatures to indicate unused parameters.

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
Loading

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?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions bot added the C:x/distribution distribution module related label Aug 15, 2024
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)
Copy link
Contributor Author

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

Copy link
Member

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

Copy link
Contributor Author

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

Copy link
Member

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?

Copy link
Contributor Author

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

Copy link
Member

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.

Copy link
Contributor Author

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?

@sontrinh16 sontrinh16 mentioned this pull request Aug 15, 2024
28 tasks
@lucaslopezf lucaslopezf marked this pull request as ready for review August 16, 2024 09:48
@lucaslopezf lucaslopezf requested a review from a team as a code owner August 16, 2024 09:48
Copy link
Contributor

@coderabbitai coderabbitai bot left a 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

Commits

Files that changed from the base of the PR and between e2ec889 and 9a1da06.

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 in incrementReferenceCount.

Replacing the panic with an error return enhances robustness and aligns with Go best practices.


147-147: Improved error handling in decrementReferenceCount.

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 in DepositValidatorRewardsPool.

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 in ValidatorOutstandingRewards.

Including the validator address in the error message provides better context for debugging.


154-154: Enhanced error message clarity in ValidatorCommission.

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.

@julienrbrt julienrbrt added the backport/v0.52.x PR scheduled for inclusion in the v0.52's next stable release label Aug 16, 2024
Copy link
Member

@julienrbrt julienrbrt left a 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

x/distribution/keeper/grpc_query.go Outdated Show resolved Hide resolved
x/distribution/keeper/grpc_query.go Outdated Show resolved Hide resolved
x/distribution/keeper/msg_server.go Outdated Show resolved Hide resolved
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)
Copy link
Member

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?

@lucaslopezf lucaslopezf added this pull request to the merge queue Aug 17, 2024
Merged via the queue into main with commit ee04cee Aug 17, 2024
75 checks passed
@lucaslopezf lucaslopezf deleted the lucas/distribution-audit branch August 17, 2024 19:53
mergify bot pushed a commit that referenced this pull request Aug 17, 2024
Co-authored-by: Randy Grok <98407738+randygrok@users.noreply.github.com>
Co-authored-by: marbar3778 <marbar3778@yahoo.com>
Co-authored-by: Cosmos SDK <113218068+github-prbot@users.noreply.github.com>
Co-authored-by: github-merge-queue <118344674+github-merge-queue@users.noreply.github.com>
Co-authored-by: Julián Toledano <JulianToledano@users.noreply.github.com>
Co-authored-by: Julien Robert <julien@rbrt.fr>
Co-authored-by: Aaron Craelius <aaronc@users.noreply.github.com>
(cherry picked from commit ee04cee)
julienrbrt pushed a commit that referenced this pull request Aug 17, 2024
Co-authored-by: Lucas Francisco López <lucaslopezf@gmail.com>
alpe added a commit that referenced this pull request Aug 19, 2024
* 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)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport/v0.52.x PR scheduled for inclusion in the v0.52's next stable release C:x/distribution distribution module related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants