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

feat(x/bank): Placing SendRestriction before Deduction of Coins in SendCoins #20517

Merged
merged 4 commits into from
Jun 3, 2024

Conversation

wr1159
Copy link
Contributor

@wr1159 wr1159 commented Jun 2, 2024

Description

Closes: #20516

Reorders SendRestriction to before subUnlockedCoins. Relevant discussion

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

  • Bug Fixes

    • Adjusted the timing of applying send restrictions in the SendCoins process to ensure restrictions are checked before coin transfer.
  • Tests

    • Updated test scenarios for SendCoins to reflect new coin values and revised error handling logic.
  • Documentation

    • Updated documentation to reflect changes in the SendCoins method and send restriction application timing.

@wr1159 wr1159 requested a review from a team as a code owner June 2, 2024 15:32
Copy link
Contributor

coderabbitai bot commented Jun 2, 2024

Walkthrough

Walkthrough

The primary change involves updating the SendCoins method in the cosmos-sdk to apply send restrictions before deducting coins. This change ensures that restrictions are checked prior to any balance alterations, enhancing safety and consistency within the banking module. Additionally, related tests have been updated to reflect these modifications.

Changes

File Change Summary
x/bank/README.md Updated to reflect the new order of applying send restrictions before deducting coins.
x/bank/keeper/keeper_test.go Modified TestSendCoinsWithRestrictions to adjust coin values and error handling logic.
x/bank/keeper/send.go Reordered operations in SendCoins method to apply send restrictions before subtracting coins.
x/bank/CHANGELOG.md Documented changes in the SendCoins method regarding the order of applying send restrictions.

Sequence Diagram(s) (Beta)

sequenceDiagram
    participant User
    participant BankModule
    participant SendRestriction
    participant CoinKeeper

    User->>+BankModule: Initiate SendCoins
    BankModule->>+SendRestriction: Apply send restriction
    SendRestriction-->>-BankModule: Restriction result
    alt Restriction Passed
        BankModule->>+CoinKeeper: Deduct coins
        CoinKeeper-->>-BankModule: Deduction result
        BankModule->>User: SendCoins Success
    else Restriction Failed
        BankModule->>User: SendCoins Failed
    end
Loading

Assessment against linked issues

Objective Addressed Explanation
Apply SendRestriction before deducting coins (Issue #20516)
Ensure safer usage of SendRestriction in SendCoins (Issue #20516)

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

@wr1159 wr1159 changed the title Feat/Placing SendRestriction before Deduction of Coins in SendCoins (x/bank) feat/Placing SendRestriction before Deduction of Coins in SendCoins (x/bank) Jun 2, 2024
@wr1159 wr1159 changed the title feat/Placing SendRestriction before Deduction of Coins in SendCoins (x/bank) feat: Placing SendRestriction before Deduction of Coins in SendCoins (x/bank) Jun 2, 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

Outside diff range and nitpick comments (1)
x/bank/keeper/keeper_test.go (1)

Line range hint 1183-1197: Ensure proper error handling in TestSendCoinsWithRestrictions
The test case restriction returns error in TestSendCoinsWithRestrictions is designed to simulate a scenario where the send restriction function returns an error. However, the test does not assert that the error message is as expected. It's crucial to verify that the error message matches the expected output to ensure that the system behaves as intended under error conditions.

- suite.Assert().EqualError(err, tc.expErr, "SendCoins error")
+ suite.Assert().EqualError(err, tc.expErr, "SendCoins error: expected error message to match")
Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between fda440d and aeba93e.

Files selected for processing (3)
  • x/bank/README.md (1 hunks)
  • x/bank/keeper/keeper_test.go (2 hunks)
  • x/bank/keeper/send.go (1 hunks)
Additional context used
Path-based instructions (3)
x/bank/keeper/send.go (1)

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

x/bank/README.md (1)

Pattern **/*.md: "Assess the documentation for misspellings, grammatical errors, missing documentation and correctness"

x/bank/keeper/keeper_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"

LanguageTool
x/bank/README.md

[uncategorized] ~12-~12: Possible missing comma found.
Context: ...counts and tracking special-case pseudo-transfers which must work differently with partic...


[uncategorized] ~15-~15: Possible missing comma found.
Context: ...ities for secure interaction with other modules which must alter user balances. In add...


[grammar] ~24-~24: Adverb repetition.
Context: ...sed in the Cosmos Hub. ## Contents * Supply * Total Supply * [Module Accounts](#mo...


[typographical] ~62-~62: It appears that a comma is missing.
Context: ...al cases mint or burn tokens. At a base level these module accounts are capable of se...


[style] ~66-~66: Consider a shorter alternative to avoid wordiness.
Context: ...n track those tokens internally. Later, in order to send tokens, the module would need to e...


[style] ~87-~87: Consider a shorter alternative to avoid wordiness.
Context: ...rthat are related toModuleAccounts in order to be able to: * Get and set ModuleAccou...


[grammar] ~100-~100: The word “lookup” is a noun. The verb is spelled with a space.
Context: ...the allowed functions, the Keeper can lookup the permissions to that specific accoun...


[uncategorized] ~105-~105: Loose punctuation mark.
Context: ...e available permissions are: * Minter: allows for a module to mint a specific ...


[uncategorized] ~105-~105: ‘Amount of’ should usually only be used with uncountable or mass nouns. Consider using “number” if this is not the case.
Context: ... allows for a module to mint a specific amount of coins. * Burner: allows for a modu...


[uncategorized] ~106-~106: Loose punctuation mark.
Context: ...t a specific amount of coins. * Burner: allows for a module to burn a specific ...


[uncategorized] ~106-~106: ‘Amount of’ should usually only be used with uncountable or mass nouns. Consider using “number” if this is not the case.
Context: ... allows for a module to burn a specific amount of coins. * Staking: allows for a mod...


[uncategorized] ~107-~107: Loose punctuation mark.
Context: ... a specific amount of coins. * Staking: allows for a module to delegate and und...


[uncategorized] ~107-~107: ‘Amount of’ should usually only be used with uncountable or mass nouns. Consider using “number” if this is not the case.
Context: ...e to delegate and undelegate a specific amount of coins. ## State The x/bank modul...


[grammar] ~278-~278: The preposition ‘from’ is repeated. Make sure that it is correct.
Context: ...ion is applied before coins are removed from the from address and adding them to the to addre...


[grammar] ~590-~590: The past participle is required after “to be”, alternatively you could omit the “be”.
Context: ... parameter is now deprecated and not to be use. It is replaced with state store record...


[grammar] ~596-~596: There seems to be a noun/verb agreement error. Did you mean “sends” or “sent”?
Context: .... ### DefaultSendEnabled The default send enabled value controls send transfer ca...

Markdownlint
x/bank/README.md

25-25: Expected: 2; Actual: 4
Unordered list indentation


27-27: Expected: 2; Actual: 4
Unordered list indentation


33-33: Expected: 2; Actual: 4
Unordered list indentation


34-34: Expected: 2; Actual: 4
Unordered list indentation


36-36: Expected: 2; Actual: 4
Unordered list indentation


37-37: Expected: 2; Actual: 4
Unordered list indentation


39-39: Expected: 2; Actual: 4
Unordered list indentation


40-40: Expected: 2; Actual: 4
Unordered list indentation


41-41: Expected: 2; Actual: 4
Unordered list indentation


398-398: Expected: 0 or 2; Actual: 1
Trailing spaces


423-423: Expected: 0 or 2; Actual: 1
Trailing spaces


289-289: Column: 1
Hard tabs


290-290: Column: 1
Hard tabs


291-291: Column: 1
Hard tabs


292-292: Column: 1
Hard tabs


294-294: Column: 1
Hard tabs


295-295: Column: 1
Hard tabs


303-303: Column: 1
Hard tabs


304-304: Column: 1
Hard tabs


305-305: Column: 1
Hard tabs


316-316: Column: 1
Hard tabs


321-321: Column: 1
Hard tabs


326-326: Column: 1
Hard tabs


327-327: Column: 1
Hard tabs


328-328: Column: 1
Hard tabs


329-329: Column: 1
Hard tabs


330-330: Column: 1
Hard tabs


331-331: Column: 1
Hard tabs


339-339: Column: 1
Hard tabs


593-593: Expected: 1; Actual: 2
Multiple consecutive blank lines

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

192-197: The change correctly implements the PR objective of applying send restrictions before coin deduction.

This adjustment ensures that the send restriction logic is evaluated before any modification to the account balances, which is crucial for maintaining consistency and preventing errors in non-transactional flows.

x/bank/README.md (1)

278-278: The documentation accurately reflects the updated behavior in the SendCoins method.

It's crucial that the documentation stays in sync with the code changes to ensure that developers and users have a correct understanding of the module's functionality.

Tools
LanguageTool

[grammar] ~278-~278: The preposition ‘from’ is repeated. Make sure that it is correct.
Context: ...ion is applied before coins are removed from the from address and adding them to the to addre...

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: 2

Outside diff range and nitpick comments (1)
x/bank/CHANGELOG.md (1)

Line range hint 41-44: Adjust the indentation of the unordered list items to match the expected format.

-    * Changed `NewInput`, `NewOutput`, `NewQueryBalanceRequest`, `NewQueryAllBalancesRequest`, `NewQuerySpendableBalancesRequest` to accept a string instead of an `AccAddress`.
-    * Added an address codec as an argument to `NewSendAuthorization`.
-    * Added an address codec as an argument to `SanitizeGenesisBalances` which also returns an error.
-    * (simulation) `RandomGenesisBalances` also returns an error.
+  * Changed `NewInput`, `NewOutput`, `NewQueryBalanceRequest`, `NewQueryAllBalancesRequest`, `NewQuerySpendableBalancesRequest` to accept a string instead of an `AccAddress`.
+  * Added an address codec as an argument to `NewSendAuthorization`.
+  * Added an address codec as an argument to `SanitizeGenesisBalances` which also returns an error.
+  * (simulation) `RandomGenesisBalances` also returns an error.
Tools
LanguageTool

[uncategorized] ~35-~35: This verb does not appear to agree with the subject. Consider using a different form.
Context: ...MintCoins and BurnCoins methods now returns an error instead of panicking if any mo...


[grammar] ~35-~35: The verb form ‘does’ does not seem to match the subject ‘accounts’.
Context: ...ead of panicking if any module accounts does not exist or unauthorized. * [#20517](h...

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between aeba93e and dbd2022.

Files selected for processing (1)
  • x/bank/CHANGELOG.md (1 hunks)
Additional context used
Path-based instructions (1)
x/bank/CHANGELOG.md (1)

Pattern **/*.md: "Assess the documentation for misspellings, grammatical errors, missing documentation and correctness"

LanguageTool
x/bank/CHANGELOG.md

[uncategorized] ~35-~35: This verb does not appear to agree with the subject. Consider using a different form.
Context: ...MintCoins and BurnCoins methods now returns an error instead of panicking if any mo...


[grammar] ~35-~35: The verb form ‘does’ does not seem to match the subject ‘accounts’.
Context: ...ead of panicking if any module accounts does not exist or unauthorized. * [#20517](h...

Markdownlint
x/bank/CHANGELOG.md

41-41: Expected: 2; Actual: 4
Unordered list indentation


42-42: Expected: 2; Actual: 4
Unordered list indentation


43-43: Expected: 2; Actual: 4
Unordered list indentation


44-44: Expected: 2; Actual: 4
Unordered list indentation

Additional comments not posted (1)
x/bank/CHANGELOG.md (1)

36-36: The changelog entry clearly and accurately describes the changes made in PR #20517 regarding the SendCoins method.

@@ -33,6 +33,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Improvements

* [#18636](https://github.com/cosmos/cosmos-sdk/pull/18636) `SendCoinsFromModuleToAccount`, `SendCoinsFromModuleToModule`, `SendCoinsFromAccountToModule`, `DelegateCoinsFromAccountToModule`, `UndelegateCoinsFromModuleToAccount`, `MintCoins` and `BurnCoins` methods now returns an error instead of panicking if any module accounts does not exist or unauthorized.
Copy link
Contributor

Choose a reason for hiding this comment

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

Correct the verb form to match the plural subject.

- methods now returns an error instead of panicking if any module accounts does not exist or unauthorized.
+ methods now return an error instead of panicking if any module accounts do not exist or are unauthorized.
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.

Suggested change
* [#18636](https://github.com/cosmos/cosmos-sdk/pull/18636) `SendCoinsFromModuleToAccount`, `SendCoinsFromModuleToModule`, `SendCoinsFromAccountToModule`, `DelegateCoinsFromAccountToModule`, `UndelegateCoinsFromModuleToAccount`, `MintCoins` and `BurnCoins` methods now returns an error instead of panicking if any module accounts does not exist or unauthorized.
* [#18636](https://github.com/cosmos/cosmos-sdk/pull/18636) `SendCoinsFromModuleToAccount`, `SendCoinsFromModuleToModule`, `SendCoinsFromAccountToModule`, `DelegateCoinsFromAccountToModule`, `UndelegateCoinsFromModuleToAccount`, `MintCoins` and `BurnCoins` methods now return an error instead of panicking if any module accounts do not exist or are unauthorized.
Tools
LanguageTool

[uncategorized] ~35-~35: This verb does not appear to agree with the subject. Consider using a different form.
Context: ...MintCoins and BurnCoins methods now returns an error instead of panicking if any mo...


[grammar] ~35-~35: The verb form ‘does’ does not seem to match the subject ‘accounts’.
Context: ...ead of panicking if any module accounts does not exist or unauthorized. * [#20517](h...


Correct the verb form and add the missing verb for clarity.

- methods now returns an error instead of panicking if any module accounts does not exist or unauthorized.
+ methods now return an error instead of panicking if any module accounts do not exist or are unauthorized.
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.

Suggested change
* [#18636](https://github.com/cosmos/cosmos-sdk/pull/18636) `SendCoinsFromModuleToAccount`, `SendCoinsFromModuleToModule`, `SendCoinsFromAccountToModule`, `DelegateCoinsFromAccountToModule`, `UndelegateCoinsFromModuleToAccount`, `MintCoins` and `BurnCoins` methods now returns an error instead of panicking if any module accounts does not exist or unauthorized.
* [#18636](https://github.com/cosmos/cosmos-sdk/pull/18636) `SendCoinsFromModuleToAccount`, `SendCoinsFromModuleToModule`, `SendCoinsFromAccountToModule`, `DelegateCoinsFromAccountToModule`, `UndelegateCoinsFromModuleToAccount`, `MintCoins` and `BurnCoins` methods now return an error instead of panicking if any module accounts do not exist or are unauthorized.
Tools
LanguageTool

[uncategorized] ~35-~35: This verb does not appear to agree with the subject. Consider using a different form.
Context: ...MintCoins and BurnCoins methods now returns an error instead of panicking if any mo...


[grammar] ~35-~35: The verb form ‘does’ does not seem to match the subject ‘accounts’.
Context: ...ead of panicking if any module accounts does not exist or unauthorized. * [#20517](h...

@julienrbrt julienrbrt changed the title feat: Placing SendRestriction before Deduction of Coins in SendCoins (x/bank) feat(x/bank): Placing SendRestriction before Deduction of Coins in SendCoins Jun 2, 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.

I think this change really makes sense.
I am not aware of why it was like this in the first place.
However, I have heard multiple confusion about the current behavior, and this feels more natural as well.

@julienrbrt julienrbrt self-assigned this Jun 2, 2024
@sontrinh16 sontrinh16 self-assigned this Jun 3, 2024
@sontrinh16 sontrinh16 added this pull request to the merge queue Jun 3, 2024
Merged via the queue into cosmos:main with commit f258c5d Jun 3, 2024
73 of 74 checks passed
alpe added a commit that referenced this pull request Jun 3, 2024
* main:
  docs: add docs on permissions (#20526)
  refactor(x/gov): set environment in context for legacy proposals (#20521)
  docs: migrate diagrams to mermaidjs (#20503)
  refactor(tools/hubl): don't use nil panic (#20515)
  refactor(x/authz): set environment in context (#20502)
  build(deps): Bump github.com/spf13/viper from 1.18.2 to 1.19.0 (#20519)
  feat(x/bank): Placing SendRestriction before Deduction of Coins in SendCoins (#20517)
  chore: sonar ignore directories with their own go.mods  (#20509)
  ci: run action in merge queue (#20508)
alpe added a commit that referenced this pull request Jun 3, 2024
* main:
  refactor(x/feegrant): set environment in context (#20529)
  refactor(x/accounts)!: accounts and auth module use the same account number tracking (#20405)
  chore: remove sonar from simapp (#20528)
  docs: add docs on permissions (#20526)
  refactor(x/gov): set environment in context for legacy proposals (#20521)
  docs: migrate diagrams to mermaidjs (#20503)
  refactor(tools/hubl): don't use nil panic (#20515)
  refactor(x/authz): set environment in context (#20502)
  build(deps): Bump github.com/spf13/viper from 1.18.2 to 1.19.0 (#20519)
  feat(x/bank): Placing SendRestriction before Deduction of Coins in SendCoins (#20517)
  chore: sonar ignore directories with their own go.mods  (#20509)
  ci: run action in merge queue (#20508)
  refactor(server/v2/cometbft): update function comments (#20506)
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.

[Feature]: In SendCoins, SendRestriction should be applied before deducting of coins
3 participants