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: add a BeforeSend hook to the bank module #278

Merged
merged 13 commits into from
Aug 6, 2022

Conversation

sunnya97
Copy link

@sunnya97 sunnya97 commented Jul 5, 2022

Description

This PR adds a hook point to before all sends in the bank module.

It makes sure to trigger the hook in the following Bank keeper functions:

  • SendCoins
  • SendManyCoins
  • SendCoinsFromAccountToModule
  • SendCoinsFromModuleToAccount
  • SendCoinsFromModuleToModule
  • SendCoinsFromModuleToManyAccounts
  • DelegateCoins
  • UndelegateCoins

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
  • added ! to 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
  • followed the guidelines for building modules
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • included comments for documenting Go code
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary
  • 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 ! in the type prefix if API or client breaking change
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic
  • reviewed API design and naming
  • reviewed documentation is accurate
  • reviewed tests and test coverage
  • manually tested (if applicable)

@sunnya97 sunnya97 changed the title Add a BeforeSend hook to the bank module feat: add a BeforeSend hook to the bank module Jul 5, 2022
@sunnya97 sunnya97 marked this pull request as ready for review July 5, 2022 23:53
@sunnya97 sunnya97 requested a review from a team July 5, 2022 23:53
x/bank/keeper/hooks_test.go Outdated Show resolved Hide resolved
Copy link
Member

@mattverse mattverse left a comment

Choose a reason for hiding this comment

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

I'm wondering if we don;t need to call SetHooks in app.go?

x/bank/keeper/keeper.go Show resolved Hide resolved
@sunnya97
Copy link
Author

sunnya97 commented Jul 6, 2022

Don't we only need to call SetHooks if we're setting some hooks? There's nothing in the cosmos-sdk repo that makes use of these hooks. In the upstream osmosis, we will add some

@@ -231,6 +239,14 @@ func (k BaseKeeper) UndelegateCoins(ctx sdk.Context, moduleAccAddr, delegatorAdd
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, amt.String())
}

// if there are hooks, call the BeforeSend hook
if k.hooks != nil {
Copy link
Member

Choose a reason for hiding this comment

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

Lets please add these hooks != nil check in the hooks struct itself, and not in this logic.

Copy link
Member

Choose a reason for hiding this comment

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

Unless this is the standard for the rest of bank. If so, lets make a github issue for it to be fixed

@ValarDragon
Copy link
Member

Yeah no need to set hooks, as long as theres nil guards in the hooks structs

@sunnya97
Copy link
Author

sunnya97 commented Jul 7, 2022

How can we add nil guards to the hook structs if what we're checking if the hook struct itself is nil?

Other modules solve it by adding keeper functions for each hook, wrapping them and checking if hooks exist

https://github.com/osmosis-labs/cosmos-sdk/blob/osmosis-main/x/staking/keeper/hooks.go

Imo, this is uglier and "more boilerplate-y", but I can update to that to be standard with the rest of the sdk.

@sunnya97 sunnya97 requested a review from ValarDragon July 7, 2022 00:44
@sunnya97 sunnya97 requested a review from mattverse July 24, 2022 12:54
Copy link
Member

@p0mvn p0mvn left a comment

Choose a reason for hiding this comment

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

Looks great overall, I like the approach for testing with the mock.

I think having a separate case with a valid amount for all methods would be good

x/bank/keeper/send.go Outdated Show resolved Hide resolved
x/bank/keeper/hooks_test.go Show resolved Hide resolved
x/bank/keeper/hooks_test.go Show resolved Hide resolved
x/bank/keeper/hooks_test.go Show resolved Hide resolved
x/bank/keeper/hooks_test.go Show resolved Hide resolved
x/bank/keeper/keeper.go Show resolved Hide resolved
@sunnya97
Copy link
Author

Why would we package the calling of the before send hook with subUnlockedCoins? These seem to be two very different functions. The BeforeSend hook needs to have a recipient address, and feels weird to be passing that into a function focused on subtracting coins

x/bank/keeper/hooks_test.go Show resolved Hide resolved
x/bank/keeper/keeper.go Show resolved Hide resolved
@p0mvn
Copy link
Member

p0mvn commented Aug 1, 2022

Why would we package the calling of the before send hook with subUnlockedCoins? These seem to be two very different functions. The BeforeSend hook needs to have a recipient address, and feels weird to be passing that into a function focused on subtracting coins

#278 (comment)

x/bank/keeper/hooks.go Outdated Show resolved Hide resolved
x/bank/keeper/hooks_test.go Outdated Show resolved Hide resolved
x/bank/keeper/internal_test.go Outdated Show resolved Hide resolved
x/bank/keeper/internal_test.go Show resolved Hide resolved
x/bank/keeper/hooks_test.go Outdated Show resolved Hide resolved
Co-authored-by: Aleksandr Bezobchuk <[email protected]>
Copy link
Member

@p0mvn p0mvn left a comment

Choose a reason for hiding this comment

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

LGTM once linter and tests are happy

@sunnya97 sunnya97 merged commit 6fcd25f into osmosis-main Aug 6, 2022
@sunnya97 sunnya97 deleted the sunny/bank-hooks branch August 6, 2022 12:40
p0mvn added a commit that referenced this pull request Sep 12, 2022
p0mvn added a commit that referenced this pull request Sep 13, 2022
czarcas7ic pushed a commit that referenced this pull request Oct 4, 2023
* in progress

* add bank hooks:

* Remove stale comments

* add nil guards

* add hooks function

* Apply suggestions from code review

Co-authored-by: Roman <[email protected]>

* add tests

* Apply suggestions from code review

Co-authored-by: Roman <[email protected]>

* Apply suggestions from code review

Co-authored-by: Aleksandr Bezobchuk <[email protected]>

* lint

Co-authored-by: Roman <[email protected]>
Co-authored-by: Aleksandr Bezobchuk <[email protected]>
czarcas7ic pushed a commit that referenced this pull request Oct 30, 2023
* in progress

* add bank hooks:

* Remove stale comments

* add nil guards

* add hooks function

* Apply suggestions from code review

Co-authored-by: Roman <[email protected]>

* add tests

* Apply suggestions from code review

Co-authored-by: Roman <[email protected]>

* Apply suggestions from code review

Co-authored-by: Aleksandr Bezobchuk <[email protected]>

* lint

Co-authored-by: Roman <[email protected]>
Co-authored-by: Aleksandr Bezobchuk <[email protected]>
czarcas7ic pushed a commit that referenced this pull request May 9, 2024
* in progress

* add bank hooks:

* Remove stale comments

* add nil guards

* add hooks function

* Apply suggestions from code review

Co-authored-by: Roman <[email protected]>

* add tests

* Apply suggestions from code review

Co-authored-by: Roman <[email protected]>

* Apply suggestions from code review

Co-authored-by: Aleksandr Bezobchuk <[email protected]>

* lint

Co-authored-by: Roman <[email protected]>
Co-authored-by: Aleksandr Bezobchuk <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

5 participants