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

perf: reduce the number of ValidateDenom calls in bank.SendCoins #20354

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

tropicaldog
Copy link
Contributor

@tropicaldog tropicaldog commented May 11, 2024

Description

Partially addresses: #19529


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

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

  • Bug Fixes
    • Improved error handling during coin transfers to ensure validation of coin amounts occurs before processing.
    • Added validation for the validity of coin amounts before minting or burning coins.

@tropicaldog tropicaldog requested a review from a team as a code owner May 11, 2024 15:09
Copy link
Contributor

coderabbitai bot commented May 11, 2024

Walkthrough

Walkthrough

The recent changes across the x/bank/keeper package aim to enhance error handling and validation processes for coin transfers. In send.go, validations for coin amounts have been refined within the SendCoins function, streamlining the transfer process. The subUnlockedCoins function now focuses solely on balance adjustments. In keeper.go, validations for coin amounts have been added before minting or burning coins, ensuring the integrity of these operations.

Changes

File Path Change Summary
x/bank/keeper/send.go Adjusted error handling by refining coin amount validation in SendCoins and updated comments in addCoins.
x/bank/keeper/keeper.go Enhanced validation for coin amounts in MintCoins and BurnCoins functions within BaseKeeper.

Tip

New Features and Improvements

Review Settings

Introduced new personality profiles for code reviews. Users can now select between "Chill" and "Assertive" review tones to tailor feedback styles according to their preferences. The "Assertive" profile posts more comments and nitpicks the code more aggressively, while the "Chill" profile is more relaxed and posts fewer comments.

AST-based Instructions

CodeRabbit offers customizing reviews based on the Abstract Syntax Tree (AST) pattern matching. Read more about AST-based instructions in the documentation.

Community-driven AST-based Rules

We are kicking off a community-driven initiative to create and share AST-based rules. Users can now contribute their AST-based rules to detect security vulnerabilities, code smells, and anti-patterns. Please see the ast-grep-essentials repository for more information.

New Static Analysis Tools

We are continually expanding our support for static analysis tools. We have added support for biome, hadolint, and ast-grep. Update the settings in your .coderabbit.yaml file or head over to the settings page to enable or disable the tools you want to use.

Tone Settings

Users can now customize CodeRabbit to review code in the style of their favorite characters or personalities. Here are some of our favorite examples:

  • Mr. T: "You must talk like Mr. T in all your code reviews. I pity the fool who doesn't!"
  • Pirate: "Arr, matey! Ye must talk like a pirate in all yer code reviews. Yarrr!"
  • Snarky: "You must be snarky in all your code reviews. Snark, snark, snark!"

Revamped Settings Page

We have redesigned the settings page for a more intuitive layout, enabling users to find and adjust settings quickly. This change was long overdue; it not only improves the user experience but also allows our development team to add more settings in the future with ease. Going forward, the changes to .coderabbit.yaml will be reflected in the settings page, and vice versa.

Miscellaneous

  • Turn off free summarization: You can switch off free summarization of PRs opened by users not on a paid plan using the enable_free_tier setting.
  • Knowledge-base scope: You can now set the scope of the knowledge base to either the repository (local) or the organization (global) level using the knowledge_base setting. In addition, you can specify Jira project keys and Linear team keys to limit the knowledge base scope for those integrations.
  • High-level summary placement: You can now customize the location of the high-level summary in the PR description using the high_level_summary_placeholder setting (default @coderabbitai summary).
  • Revamped request changes workflow: You can now configure CodeRabbit to auto-approve or request changes on PRs based on the review feedback using the request_changes_workflow setting.

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.

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 a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

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

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

@tropicaldog tropicaldog changed the title move ValidateDenom to top level function to reduce duplicated validation call perf: reduce the number of ValidateDenom calls in bank.SendCoins May 11, 2024
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 0c91044 and 49e146c.
Files selected for processing (2)
  • types/coin.go (1 hunks)
  • x/bank/keeper/send.go (3 hunks)
Files skipped from review due to trivial changes (1)
  • types/coin.go
Additional Context Used
Path-based Instructions (1)
x/bank/keeper/send.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

Additional comments not posted (3)
x/bank/keeper/send.go (3)

187-189: Validation of amt coins added at the start of SendCoins function enhances early error detection and reduces redundant validation deeper in the call stack.


225-225: Removal of coin validation from subUnlockedCoins function aligns with the goal of reducing redundant validations and clarifies the function's responsibility.


270-270: Updated comment in addCoins function clarifies that coin amounts should be validated prior to being passed, ensuring clear communication of responsibilities.

Copy link
Member

@tac0turtle tac0turtle left a comment

Choose a reason for hiding this comment

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

I dont think this is safe, as the caller of subunlocked and addcoins does not call validate. I would leave as is and optimise in v2

@tropicaldog
Copy link
Contributor Author

I dont think this is safe, as the caller of subunlocked and addcoins does not call validate. I would leave as is and optimise in v2

Yeah but addCoins and subUnlockedCoins are only use internally in a few methods, so I guess adding the amount validation to these methods seems good?

@tac0turtle
Copy link
Member

I dont think this is safe, as the caller of subunlocked and addcoins does not call validate. I would leave as is and optimise in v2

Yeah but addCoins and subUnlockedCoins are only use internally in a few methods, so I guess adding the amount validation to these methods seems good?

yea we can look at doing this. Want to give it a stab

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 49e146c and e2923de.
Files selected for processing (1)
  • x/bank/keeper/keeper.go (2 hunks)
Additional Context Used
Path-based Instructions (1)
x/bank/keeper/keeper.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

Additional comments not posted (2)
x/bank/keeper/keeper.go (2)

369-372: Validation check for amounts ensures only valid coins are minted. This is a good safety measure.


412-414: Validation check for amounts ensures only valid coins are burned. This is a good safety measure.

Copy link
Contributor

@alpe alpe left a comment

Choose a reason for hiding this comment

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

The changes look good. 🏌️
I check all the callers if the amount is validated to fail with the correct error. This is given (at the moment). There is also a safety net in setBalance that ensures only valid denoms are stored.

☝️ Nevertheless this PR only solves the problem to a certain degree.
The subUnlockedCoins method uses locked := sdk.NewCoin(coin.Denom, lockedCoins.AmountOf(coin.Denom)) which comes with 2 denom checks which could be avoided by locked, ok := lockedCoins.Find(coin.Denom).

Please also see #19529 (comment) before closing the Issue

@@ -218,13 +222,9 @@ func (k BaseSendKeeper) SendCoins(ctx context.Context, fromAddr, toAddr sdk.AccA
}

// subUnlockedCoins removes the unlocked amt coins of the given account. An error is
// returned if the resulting balance is negative or the initial amount is invalid.
// returned if the resulting balance is negative.
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: here and SendCoins: some godoc comment about the amount assumptions would be helpful

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 e2923de and 9f839f7.
Files selected for processing (1)
  • x/bank/keeper/send.go (4 hunks)
Files skipped from review as they are similar to previous changes (1)
  • x/bank/keeper/send.go

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 9f839f7 and 7f995ab.
Files selected for processing (1)
  • x/bank/keeper/send.go (4 hunks)
Files skipped from review as they are similar to previous changes (1)
  • x/bank/keeper/send.go

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 7f995ab and 105a2a7.
Files selected for processing (1)
  • x/bank/keeper/keeper.go (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • x/bank/keeper/keeper.go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants