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(bank): Allow injectable restrictions on bank transfers #14224

Merged
merged 58 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from 45 commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
83de950
[14124]: Define a SendRestriction function type.
SpicyLemon Dec 7, 2022
1648159
[14124]: Move the MintingRestrictionFn in with the SendRestrictionFn …
SpicyLemon Dec 7, 2022
66b2b93
[14124]: Change InputOutputCoins to accept only a single input since …
SpicyLemon Dec 8, 2022
845480d
[14124]: Add a SendRestrictionFn to the send keeper. Rename the exist…
SpicyLemon Dec 8, 2022
df54462
[14124]: Remove the SendCoinsWithoutRestriction and always run restri…
SpicyLemon Dec 8, 2022
ba605c3
[14124]: Create a struct to hold the send restriction function that c…
SpicyLemon Dec 8, 2022
66e8474
[14124]: Switch the keeper to use this SendRestriction holder and giv…
SpicyLemon Dec 8, 2022
bc988d9
[14124]: Add some more unit test cases on the SendCoins w/restrictions.
SpicyLemon Dec 8, 2022
3f6ea3a
[14124]: Add unit tests on InputOutputCoins with restrictions.
SpicyLemon Dec 8, 2022
a5b1afe
[14124]: Add unit tests on append and prepend send restrictions.
SpicyLemon Dec 8, 2022
64bb3c1
[14124]: Update the gov mocks to include the new Append and Prepend r…
SpicyLemon Dec 8, 2022
9bd62bf
Merge branch 'main' into dwedul/14124-bank-restrictions
SpicyLemon Dec 8, 2022
987e9c5
Merge branch 'main' into dwedul/14124-bank-restrictions
SpicyLemon Dec 8, 2022
e388801
[14124]: Update changelog.
SpicyLemon Dec 8, 2022
1ef3412
[14124]: Fix a couple test failure messages.
SpicyLemon Dec 8, 2022
60fbf14
[14124]: Fix some import ordering.
SpicyLemon Dec 8, 2022
915216e
Merge branch 'main' into dwedul/14124-bank-restrictions
SpicyLemon Dec 8, 2022
66ec36f
[14124]: Update spec doc with SendKeeper Append and Prepend SendRestr…
SpicyLemon Dec 9, 2022
1f0efb8
Merge branch 'main' into dwedul/14124-bank-restrictions
SpicyLemon Dec 9, 2022
c9cc904
Merge branch 'main' into dwedul/14124-bank-restrictions
SpicyLemon Jan 12, 2023
28f37ed
[14124]: Fix test compilation (due to AccountI change).
SpicyLemon Jan 12, 2023
a0befb3
[14124]: Lint fixes.
SpicyLemon Jan 12, 2023
327f218
[14124]: Add some extra info to the restriction composition function …
SpicyLemon Jan 12, 2023
f4d0a12
Merge branch 'main' into dwedul/14124-bank-restrictions
SpicyLemon Jan 12, 2023
6202a5d
Merge branch 'main' into dwedul/14124-bank-restrictions
SpicyLemon Feb 3, 2023
8aba61a
Merge branch 'main' into dwedul/14124-bank-restrictions
SpicyLemon Feb 16, 2023
a994e79
Merge branch 'main' into dwedul/14124-bank-restrictions
SpicyLemon Mar 8, 2023
916608b
Merge branch 'main' into dwedul/14124-bank-restrictions
SpicyLemon Mar 21, 2023
ed42c18
Merge branch 'main' into dwedul/14124-bank-restrictions
SpicyLemon Mar 22, 2023
4260317
Merge branch 'main' into dwedul/14124-bank-restrictions
SpicyLemon Mar 31, 2023
2a3f9fa
Merge branch 'main' into dwedul/14124-bank-restrictions
SpicyLemon Apr 14, 2023
c9b5bab
[14124]: Remove the API Breaking change listing since another PR did …
SpicyLemon Apr 17, 2023
c7583b4
Merge branch 'main' into dwedul/14124-bank-restrictions
SpicyLemon Apr 17, 2023
f54f8fa
[14124]: Move the definition of the restriction functions into the ty…
SpicyLemon Apr 18, 2023
c9b7b71
Merge branch 'main' into dwedul/14124-bank-restrictions
SpicyLemon Apr 18, 2023
c6e1dca
[14124]: Move the wrapper for the SendRestrictionFn back into the kee…
SpicyLemon Apr 19, 2023
053a518
Merge branch 'main' into dwedul/14124-bank-restrictions
SpicyLemon Apr 19, 2023
0000a6e
[14124]: Regen mocks to get the ClearSendRestriction.
SpicyLemon Apr 19, 2023
2f61c50
Fix broken reference to moved MintingRestrictionFn definition.
SpicyLemon Apr 19, 2023
231c925
Merge branch 'main' into dwedul/14124-bank-restrictions
SpicyLemon May 5, 2023
dfb0f30
Merge branch 'main' into dwedul/14124-bank-restrictions
SpicyLemon May 5, 2023
a452e6a
Merge branch 'main' into dwedul/14124-bank-restrictions
SpicyLemon Jun 5, 2023
1c009f5
[14124]: Fix compilation issue after merge.
SpicyLemon Jun 5, 2023
a0f7191
Merge branch 'main' into dwedul/14124-bank-restrictions
SpicyLemon Jun 5, 2023
c43f119
Merge branch 'main' into dwedul/14124-bank-restrictions
SpicyLemon Jun 9, 2023
114838d
Merge branch 'main' into dwedul/14124-bank-restrictions
SpicyLemon Jun 27, 2023
89680e8
Merge branch 'main' into dwedul/14124-bank-restrictions
SpicyLemon Jun 28, 2023
a116b9a
Merge branch 'main' into dwedul/14124-bank-restrictions
SpicyLemon Jun 29, 2023
0f36433
Move the send restriction application in SendCoins to after subUnlock…
SpicyLemon Jun 29, 2023
acbc96c
Add some spec documentation about the send restrictions.
SpicyLemon Jun 29, 2023
fdd3354
Merge branch 'main' into dwedul/14124-bank-restrictions
SpicyLemon Jun 30, 2023
7140caa
Merge branch 'main' into dwedul/14124-bank-restrictions
SpicyLemon Jun 30, 2023
d4eca5d
[14124]: Fix unit tests that broke when I changed the location of the…
SpicyLemon Jun 30, 2023
8fb5fd4
Merge branch 'main' into dwedul/14124-bank-restrictions
SpicyLemon Jul 31, 2023
7ab7b5a
Merge branch 'main' into dwedul/14124-bank-restrictions
SpicyLemon Aug 8, 2023
555523c
Merge branch 'main' into dwedul/14124-bank-restrictions
SpicyLemon Aug 10, 2023
adb1e73
Merge branch 'main' into dwedul/14124-bank-restrictions
SpicyLemon Aug 18, 2023
58b6f99
Merge branch 'main' into dwedul/14124-bank-restrictions
SpicyLemon Aug 18, 2023
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (cli) [#16209](https://github.com/cosmos/cosmos-sdk/pull/16209) Make `StartCmd` more customizable.
* (types) [#16257](https://github.com/cosmos/cosmos-sdk/pull/16257) Allow setting the base denom in the denom registry.
* (genutil) [#16046](https://github.com/cosmos/cosmos-sdk/pull/16046) Add "module-name" flag to genutil add-genesis-account to enable intializing module accounts at genesis.
* (x/bank) [#14224](https://github.com/cosmos/cosmos-sdk/pull/14224) Allow injection of restrictions on transfers using `AppendSendRestriction` or `PrependSendRestriction`.

### Improvements

Expand Down
8 changes: 6 additions & 2 deletions x/bank/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,12 @@ accounts. The send keeper does not alter the total supply (mint or burn coins).
type SendKeeper interface {
ViewKeeper

InputOutputCoins(ctx context.Context, inputs types.Input, outputs []types.Output) error
SendCoins(ctx context.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) error
AppendSendRestriction(restriction SendRestrictionFn)
PrependSendRestriction(restriction SendRestrictionFn)
Comment on lines +239 to +240
Copy link
Contributor

Choose a reason for hiding this comment

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

This is late, and may have been discussed already, but I wonder what the need is for having 2 new methods, compared to just, say, AppendSendRestriction? More importantly, how can a module decide whether its SendRestrictionFn should be prepended vs appended? It seems like a global decision that can't be made locally.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The ordering of send restrictions might matter. I expect most things will use Append, but there could easily be a situation where a module needs theirs to go before the ones already injected.

I hate when I get into a situation where I just can't do what I know I need to do because the only layer I have access too didn't provide enough control. So, while Append will usually be good enough, Prepend and Clear can easily save the day.

Copy link
Contributor

Choose a reason for hiding this comment

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

could easily be a situation where a module needs theirs to go before the ones already injected.

But how does the module know whether it's safe to insert its restriction function before all others? That is, I see a problem where modules can be fighting each other for the correct position of their restrictions.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The module can't know. The blockchain author can though, and needs to have the tools available to fix ordering problems if the arise.

ClearSendRestriction()
Copy link
Contributor

Choose a reason for hiding this comment

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

When is this needed? If you've added restrictions, do you ever want to remove them again?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Clear() is needed for situations where a blockchain author needs to change the ordering of restrictions, and can't do so by other means.


InputOutputCoins(ctx context.Context, input types.Input, outputs []types.Output) error
SendCoins(ctx context.Context, fromAddr, toAddr sdk.AccAddress, amt sdk.Coins) error

GetParams(ctx context.Context) types.Params
SetParams(ctx context.Context, params types.Params) error
Expand Down
14 changes: 14 additions & 0 deletions x/bank/keeper/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package keeper

import "github.com/cosmos/cosmos-sdk/x/bank/types"

// This file exists in the keeper package to expose some private things
// for the purpose of testing in the keeper_test package.

func (k BaseSendKeeper) SetSendRestriction(restriction types.SendRestrictionFn) {
k.sendRestriction.fn = restriction
}

func (k BaseSendKeeper) GetSendRestrictionFn() types.SendRestrictionFn {
return k.sendRestriction.fn
}
23 changes: 5 additions & 18 deletions x/bank/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var _ Keeper = (*BaseKeeper)(nil)
// between accounts.
type Keeper interface {
SendKeeper
WithMintCoinsRestriction(MintingRestrictionFn) BaseKeeper
WithMintCoinsRestriction(types.MintingRestrictionFn) BaseKeeper

InitGenesis(context.Context, *types.GenesisState)
ExportGenesis(context.Context) *types.GenesisState
Expand Down Expand Up @@ -63,12 +63,10 @@ type BaseKeeper struct {
ak types.AccountKeeper
cdc codec.BinaryCodec
storeService store.KVStoreService
mintCoinsRestrictionFn MintingRestrictionFn
mintCoinsRestrictionFn types.MintingRestrictionFn
logger log.Logger
}

type MintingRestrictionFn func(ctx context.Context, coins sdk.Coins) error

// GetPaginatedTotalSupply queries for the supply, ignoring 0 coins, with a given pagination
func (k BaseKeeper) GetPaginatedTotalSupply(ctx context.Context, pagination *query.PageRequest) (sdk.Coins, *query.PageResponse, error) {
results, pageResp, err := query.CollectionPaginate[string, math.Int](ctx, k.Supply, pagination)
Expand Down Expand Up @@ -109,7 +107,7 @@ func NewBaseKeeper(
ak: ak,
cdc: cdc,
storeService: storeService,
mintCoinsRestrictionFn: func(ctx context.Context, coins sdk.Coins) error { return nil },
mintCoinsRestrictionFn: types.NoOpMintingRestrictionFn,
logger: logger,
}
}
Expand All @@ -119,19 +117,8 @@ func NewBaseKeeper(
// Previous restriction functions can be nested as such:
//
// bankKeeper.WithMintCoinsRestriction(restriction1).WithMintCoinsRestriction(restriction2)
func (k BaseKeeper) WithMintCoinsRestriction(check MintingRestrictionFn) BaseKeeper {
oldRestrictionFn := k.mintCoinsRestrictionFn
k.mintCoinsRestrictionFn = func(ctx context.Context, coins sdk.Coins) error {
err := check(ctx, coins)
if err != nil {
return err
}
err = oldRestrictionFn(ctx, coins)
if err != nil {
return err
}
return nil
}
func (k BaseKeeper) WithMintCoinsRestriction(check types.MintingRestrictionFn) BaseKeeper {
k.mintCoinsRestrictionFn = check.Then(k.mintCoinsRestrictionFn)
return k
}

Expand Down
Loading