forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a BeforeSend hook to the bank module (#278)
* 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]>
- Loading branch information
1 parent
3a4d0fc
commit 058afdd
Showing
9 changed files
with
354 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package bank_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"cosmossdk.io/math" | ||
tmproto "github.com/cometbft/cometbft/proto/tendermint/types" | ||
"github.com/stretchr/testify/require" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
"github.com/cosmos/cosmos-sdk/x/bank/keeper" | ||
banktestutil "github.com/cosmos/cosmos-sdk/x/bank/testutil" | ||
"github.com/cosmos/cosmos-sdk/x/bank/types" | ||
|
||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" | ||
) | ||
|
||
var _ types.BankHooks = &MockBankHooksReceiver{} | ||
|
||
// BankHooks event hooks for bank (noalias) | ||
type MockBankHooksReceiver struct{} | ||
|
||
// Mock BeforeSend bank hook that doesn't allow the sending of exactly 100 coins of any denom. | ||
func (h *MockBankHooksReceiver) BeforeSend(ctx context.Context, from, to sdk.AccAddress, amount sdk.Coins) error { | ||
for _, coin := range amount { | ||
if coin.Amount.Equal(math.NewInt(100)) { | ||
return fmt.Errorf("not allowed; expected %v, got: %v", 100, coin.Amount) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func TestHooks(t *testing.T) { | ||
acc := &authtypes.BaseAccount{ | ||
Address: addr1.String(), | ||
} | ||
|
||
genAccs := []authtypes.GenesisAccount{acc} | ||
app := createTestSuite(t, genAccs) | ||
baseApp := app.App.BaseApp | ||
ctx := baseApp.NewContextLegacy(false, tmproto.Header{}) | ||
|
||
require.NoError(t, banktestutil.FundAccount(ctx, app.BankKeeper, addr1, sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 10000)))) | ||
require.NoError(t, banktestutil.FundAccount(ctx, app.BankKeeper, addr2, sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 10000)))) | ||
banktestutil.FundModuleAccount(ctx, app.BankKeeper, stakingtypes.BondedPoolName, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(1000)))) | ||
|
||
// create a valid send amount which is 1 coin, and an invalidSendAmount which is 100 coins | ||
validSendAmount := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(1))) | ||
invalidSendAmount := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(100))) | ||
|
||
// setup our mock bank hooks receiver that prevents the send of 100 coins | ||
bankHooksReceiver := MockBankHooksReceiver{} | ||
baseBankKeeper, ok := app.BankKeeper.(keeper.BaseKeeper) | ||
require.True(t, ok) | ||
baseBankKeeper.SetHooks( | ||
types.NewMultiBankHooks(&bankHooksReceiver), | ||
) | ||
app.BankKeeper = baseBankKeeper | ||
|
||
// try sending a validSendAmount and it should work | ||
err := app.BankKeeper.SendCoins(ctx, addr1, addr2, validSendAmount) | ||
require.NoError(t, err) | ||
|
||
// try sending an invalidSendAmount and it should not work | ||
err = app.BankKeeper.SendCoins(ctx, addr1, addr2, invalidSendAmount) | ||
require.Error(t, err) | ||
|
||
// try doing SendManyCoins and make sure if even a single subsend is invalid, the entire function fails | ||
err = app.BankKeeper.SendManyCoins(ctx, addr1, []sdk.AccAddress{addr1, addr2}, []sdk.Coins{invalidSendAmount, validSendAmount}) | ||
require.Error(t, err) | ||
|
||
// make sure that account to module doesn't bypass hook | ||
err = app.BankKeeper.SendCoinsFromAccountToModule(ctx, addr1, stakingtypes.BondedPoolName, validSendAmount) | ||
require.NoError(t, err) | ||
err = app.BankKeeper.SendCoinsFromAccountToModule(ctx, addr1, stakingtypes.BondedPoolName, invalidSendAmount) | ||
require.Error(t, err) | ||
|
||
// make sure that module to account doesn't bypass hook | ||
err = app.BankKeeper.SendCoinsFromModuleToAccount(ctx, stakingtypes.BondedPoolName, addr1, validSendAmount) | ||
require.NoError(t, err) | ||
err = app.BankKeeper.SendCoinsFromModuleToAccount(ctx, stakingtypes.BondedPoolName, addr1, invalidSendAmount) | ||
require.Error(t, err) | ||
|
||
// make sure that module to module doesn't bypass hook | ||
err = app.BankKeeper.SendCoinsFromModuleToModule(ctx, stakingtypes.BondedPoolName, stakingtypes.NotBondedPoolName, validSendAmount) | ||
require.NoError(t, err) | ||
err = app.BankKeeper.SendCoinsFromModuleToModule(ctx, stakingtypes.BondedPoolName, stakingtypes.NotBondedPoolName, invalidSendAmount) | ||
require.Error(t, err) | ||
|
||
// make sure that module to many accounts doesn't bypass hook | ||
err = app.BankKeeper.SendCoinsFromModuleToManyAccounts(ctx, stakingtypes.BondedPoolName, []sdk.AccAddress{addr1, addr2}, []sdk.Coins{validSendAmount, validSendAmount}) | ||
require.NoError(t, err) | ||
err = app.BankKeeper.SendCoinsFromModuleToManyAccounts(ctx, stakingtypes.BondedPoolName, []sdk.AccAddress{addr1, addr2}, []sdk.Coins{validSendAmount, invalidSendAmount}) | ||
require.Error(t, err) | ||
|
||
// make sure that DelegateCoins doesn't bypass the hook | ||
err = app.BankKeeper.DelegateCoins(ctx, addr1, app.AccountKeeper.GetModuleAddress(stakingtypes.BondedPoolName), validSendAmount) | ||
require.NoError(t, err) | ||
err = app.BankKeeper.DelegateCoins(ctx, addr1, app.AccountKeeper.GetModuleAddress(stakingtypes.BondedPoolName), invalidSendAmount) | ||
require.Error(t, err) | ||
|
||
// make sure that UndelegateCoins doesn't bypass the hook | ||
err = app.BankKeeper.UndelegateCoins(ctx, app.AccountKeeper.GetModuleAddress(stakingtypes.BondedPoolName), addr1, validSendAmount) | ||
require.NoError(t, err) | ||
err = app.BankKeeper.UndelegateCoins(ctx, app.AccountKeeper.GetModuleAddress(stakingtypes.BondedPoolName), addr1, invalidSendAmount) | ||
require.Error(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package keeper | ||
|
||
import ( | ||
"context" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/bank/types" | ||
) | ||
|
||
// Implements StakingHooks interface | ||
var _ types.BankHooks = BaseSendKeeper{} | ||
|
||
// BeforeSend executes the BeforeSend hook if registered. | ||
func (k BaseSendKeeper) BeforeSend(ctx context.Context, from, to sdk.AccAddress, amount sdk.Coins) error { | ||
if k.hooks != nil { | ||
return k.hooks.BeforeSend(ctx, from, to, amount) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package keeper | ||
|
||
import "github.com/cosmos/cosmos-sdk/x/bank/types" | ||
|
||
// UnsafeSetHooks updates the x/bank keeper's hooks, overriding any potential | ||
// pre-existing hooks. | ||
// | ||
// WARNING: this function should only be used in tests. | ||
func UnsafeSetHooks(k *BaseKeeper, h types.BankHooks) { | ||
k.hooks = h | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.