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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions x/bank/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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...

* [#20517](https://github.com/cosmos/cosmos-sdk/pull/20517) `SendCoins` now checks for `SendRestrictions` before instead of after deducting coins using `subUnlockedCoins`.

### API Breaking Changes

Expand Down
2 changes: 1 addition & 1 deletion x/bank/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ Both functions compose the provided restriction with any previously provided res
`PrependSendRestriction` adds the restriction to be run before any previously provided send restrictions.
The composition will short-circuit when an error is encountered. I.e. if the first one returns an error, the second is not run.

During `SendCoins`, the send restriction is applied after coins are removed from the from address, but before adding them to the to address.
During `SendCoins`, the send restriction is applied before coins are removed from the from address and adding them to the to address.
During `InputOutputCoins`, the send restriction is applied after the input coins are removed and once for each output before the funds are added.

A send restriction function should make use of a custom value in the context to allow bypassing that specific restriction.
Expand Down
7 changes: 2 additions & 5 deletions x/bank/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1180,7 +1180,7 @@ func (suite *KeeperTestSuite) TestSendCoinsWithRestrictions() {
},
expErr: "test restriction error",
expBals: expBals{
from: sdk.NewCoins(newFooCoin(885), newBarCoin(273)),
from: sdk.NewCoins(newFooCoin(985), newBarCoin(473)),
to1: sdk.NewCoins(newFooCoin(15)),
to2: sdk.NewCoins(newBarCoin(27)),
},
Expand All @@ -1194,12 +1194,9 @@ func (suite *KeeperTestSuite) TestSendCoinsWithRestrictions() {
actualRestrictionArgs = nil
suite.bankKeeper.SetSendRestriction(tc.fn)
ctx := suite.ctx
if len(tc.expErr) > 0 {
suite.authKeeper.EXPECT().GetAccount(ctx, fromAddr).Return(fromAcc)
} else {
if len(tc.expErr) == 0 {
suite.mockSendCoins(ctx, fromAcc, tc.finalAddr)
}

var err error
testFunc := func() {
err = suite.bankKeeper.SendCoins(ctx, fromAddr, tc.toAddr, tc.amt)
Expand Down
4 changes: 2 additions & 2 deletions x/bank/keeper/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,12 @@ func (k BaseSendKeeper) SendCoins(ctx context.Context, fromAddr, toAddr sdk.AccA
}

var err error
err = k.subUnlockedCoins(ctx, fromAddr, amt)
toAddr, err = k.sendRestriction.apply(ctx, fromAddr, toAddr, amt)
if err != nil {
return err
}

toAddr, err = k.sendRestriction.apply(ctx, fromAddr, toAddr, amt)
err = k.subUnlockedCoins(ctx, fromAddr, amt)
if err != nil {
return err
}
Expand Down
Loading