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

refactor(x/bank): check send enabled on coins #20343

Merged
merged 12 commits into from
May 13, 2024
2 changes: 1 addition & 1 deletion baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ func (app *BaseApp) preBlock(req *abci.FinalizeBlockRequest) error {
return nil
}

func (app *BaseApp) beginBlock(req *abci.FinalizeBlockRequest) (sdk.BeginBlock, error) {
func (app *BaseApp) beginBlock(_ *abci.FinalizeBlockRequest) (sdk.BeginBlock, error) {
var (
resp sdk.BeginBlock
err error
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/bank/keeper/deterministic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ func initDeterministicFixture(t *testing.T) *deterministicFixture {
authority.String(),
)

assert.NilError(t, bankKeeper.SetParams(newCtx, banktypes.DefaultParams()))

authModule := auth.NewAppModule(cdc, accountKeeper, acctsModKeeper, authsims.RandomGenesisAccounts)
bankModule := bank.NewAppModule(cdc, bankKeeper, accountKeeper)

Expand Down
2 changes: 2 additions & 0 deletions tests/integration/distribution/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ func initFixture(t *testing.T) *fixture {
authority.String(),
)

assert.NilError(t, bankKeeper.SetParams(newCtx, banktypes.DefaultParams()))

msgRouter := baseapp.NewMsgServiceRouter()
grpcRouter := baseapp.NewGRPCQueryRouter()
cometService := runtime.NewContextAwareCometInfoService()
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/evidence/keeper/infraction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ func initFixture(tb testing.TB) *fixture {
authority.String(),
)

assert.NilError(tb, bankKeeper.SetParams(newCtx, banktypes.DefaultParams()))

stakingKeeper := stakingkeeper.NewKeeper(cdc, runtime.NewEnvironment(runtime.NewKVStoreService(keys[stakingtypes.StoreKey]), log.NewNopLogger(), runtime.EnvWithRouterService(grpcQueryRouter, msgRouter)), accountKeeper, bankKeeper, authority.String(), addresscodec.NewBech32Codec(sdk.Bech32PrefixValAddr), addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr), runtime.NewContextAwareCometInfoService())

slashingKeeper := slashingkeeper.NewKeeper(runtime.NewEnvironment(runtime.NewKVStoreService(keys[slashingtypes.StoreKey]), log.NewNopLogger()), cdc, codec.NewLegacyAmino(), stakingKeeper, authority.String())
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/gov/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ func initFixture(tb testing.TB) *fixture {
authority.String(),
)

assert.NilError(tb, bankKeeper.SetParams(newCtx, banktypes.DefaultParams()))

stakingKeeper := stakingkeeper.NewKeeper(cdc, runtime.NewEnvironment(runtime.NewKVStoreService(keys[stakingtypes.StoreKey]), log.NewNopLogger()), accountKeeper, bankKeeper, authority.String(), addresscodec.NewBech32Codec(sdk.Bech32PrefixValAddr), addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr), runtime.NewContextAwareCometInfoService())

poolKeeper := poolkeeper.NewKeeper(cdc, runtime.NewEnvironment(runtime.NewKVStoreService(keys[pooltypes.StoreKey]), log.NewNopLogger()), accountKeeper, bankKeeper, stakingKeeper, authority.String())
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/slashing/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ func initFixture(tb testing.TB) *fixture {
authority.String(),
)

assert.NilError(tb, bankKeeper.SetParams(newCtx, banktypes.DefaultParams()))

cometInfoService := runtime.NewContextAwareCometInfoService()

stakingKeeper := stakingkeeper.NewKeeper(cdc, runtime.NewEnvironment(runtime.NewKVStoreService(keys[stakingtypes.StoreKey]), log.NewNopLogger(), runtime.EnvWithRouterService(queryRouter, msgRouter)), accountKeeper, bankKeeper, authority.String(), addresscodec.NewBech32Codec(sdk.Bech32PrefixValAddr), addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr), cometInfoService)
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/staking/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ func initFixture(tb testing.TB) *fixture {
authority.String(),
)

assert.NilError(tb, bankKeeper.SetParams(newCtx, banktypes.DefaultParams()))

stakingKeeper := stakingkeeper.NewKeeper(cdc, runtime.NewEnvironment(runtime.NewKVStoreService(keys[types.StoreKey]), log.NewNopLogger(), runtime.EnvWithRouterService(queryRouter, msgRouter)), accountKeeper, bankKeeper, authority.String(), addresscodec.NewBech32Codec(sdk.Bech32PrefixValAddr), addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr), runtime.NewContextAwareCometInfoService())

authModule := auth.NewAppModule(cdc, accountKeeper, acctsModKeeper, authsims.RandomGenesisAccounts)
Expand Down
4 changes: 3 additions & 1 deletion tests/integration/staking/keeper/deterministic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ func initDeterministicFixture(t *testing.T) *deterministicFixture {
authority.String(),
)

assert.NilError(t, bankKeeper.SetParams(newCtx, banktypes.DefaultParams()))

stakingKeeper := stakingkeeper.NewKeeper(cdc, runtime.NewEnvironment(runtime.NewKVStoreService(keys[stakingtypes.StoreKey]), log.NewNopLogger()), accountKeeper, bankKeeper, authority.String(), addresscodec.NewBech32Codec(sdk.Bech32PrefixValAddr), addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr), runtime.NewContextAwareCometInfoService())

authModule := auth.NewAppModule(cdc, accountKeeper, acctsModKeeper, authsims.RandomGenesisAccounts)
Expand Down Expand Up @@ -199,7 +201,7 @@ func bondTypeGenerator() *rapid.Generator[stakingtypes.BondStatus] {
}

// createValidator creates a validator with random values.
func createValidator(t *testing.T, rt *rapid.T, f *deterministicFixture) stakingtypes.Validator {
func createValidator(t *testing.T, rt *rapid.T, _ *deterministicFixture) stakingtypes.Validator {
t.Helper()
pubkey := pubKeyGenerator().Draw(rt, "pubkey")
pubkeyAny, err := codectypes.NewAnyWithValue(&pubkey)
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/types/filtered_pagination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func (s *paginationTestSuite) TestFilteredPaginate() {
// balances:<denom:"test0denom" amount:"250" > pagination:<next_key:"test1denom" total:5 >
}

func execFilterPaginate(store storetypes.KVStore, pageReq *query.PageRequest, appCodec codec.Codec) (balances sdk.Coins, res *query.PageResponse, err error) {
func execFilterPaginate(store storetypes.KVStore, pageReq *query.PageRequest, _ codec.Codec) (balances sdk.Coins, res *query.PageResponse, err error) {
balancesStore := prefix.NewStore(store, types.BalancesPrefix)
accountStore := prefix.NewStore(balancesStore, address.MustLengthPrefix(addr1))

Expand Down Expand Up @@ -263,7 +263,7 @@ func (s *paginationTestSuite) TestFilteredPaginationsNextKey() {
s.Require().NoError(testutil.FundAccount(s.ctx, s.bankKeeper, addr1, balances))
store := s.ctx.KVStore(s.app.UnsafeFindStoreKey(types.StoreKey))

execFilterPaginate := func(store storetypes.KVStore, pageReq *query.PageRequest, appCodec codec.Codec) (balances sdk.Coins, res *query.PageResponse, err error) {
execFilterPaginate := func(store storetypes.KVStore, pageReq *query.PageRequest, _ codec.Codec) (balances sdk.Coins, res *query.PageResponse, err error) {
balancesStore := prefix.NewStore(store, types.BalancesPrefix)
accountStore := prefix.NewStore(balancesStore, address.MustLengthPrefix(addr1))

Expand Down
2 changes: 2 additions & 0 deletions tests/integration/types/pagination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ func (s *paginationTestSuite) SetupTest() {
ctx := app.BaseApp.NewContextLegacy(false, cmtproto.Header{Height: 1})

s.ctx, s.bankKeeper, s.accountKeeper, s.cdc, s.app, s.interfaceReg = ctx, bankKeeper, accountKeeper, cdc, app, reg

s.Require().NoError(s.bankKeeper.SetParams(s.ctx, types.DefaultParams()))
}

func (s *paginationTestSuite) TestParsePagination() {
Expand Down
1 change: 1 addition & 0 deletions x/bank/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Consensus Breaking Changes

* [#19188](https://github.com/cosmos/cosmos-sdk/pull/19188) Remove creation of `BaseAccount` when sending a message to an account that does not exist
* [#20343](https://github.com/cosmos/cosmos-sdk/pull/20343) Add a check in send moduleaccount to account to prevent module accounts from sending disabled tokens to accounts
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 grammatical error in the changelog entry.

- * [#20343](https://github.com/cosmos/cosmos-sdk/pull/20343) Add a check in send moduleaccount to account to prevent module accounts from sending disabled tokens to accounts
+ * [#20343](https://github.com/cosmos/cosmos-sdk/pull/20343) Add a check in send module account to account to prevent module accounts from sending disabled tokens to accounts

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
* [#20343](https://github.com/cosmos/cosmos-sdk/pull/20343) Add a check in send moduleaccount to account to prevent module accounts from sending disabled tokens to accounts
* [#20343](https://github.com/cosmos/cosmos-sdk/pull/20343) Add a check in send module account to account to prevent module accounts from sending disabled tokens to accounts

6 changes: 6 additions & 0 deletions x/bank/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,12 @@ func (k BaseKeeper) SendCoinsFromModuleToAccount(
return errorsmod.Wrapf(sdkerrors.ErrUnknownAddress, "module account %s does not exist", senderModule)
}

for _, coin := range amt {
if ok := k.IsSendEnabledDenom(ctx, coin.Denom); !ok {
return fmt.Errorf("denom: %s, is prohibited from being sent at this time", coin.Denom)
}
}

if k.BlockedAddr(recipientAddr) {
return errorsmod.Wrapf(sdkerrors.ErrUnauthorized, "%s is not allowed to receive funds", recipientAddr)
}
Expand Down
29 changes: 26 additions & 3 deletions x/bank/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ func (suite *KeeperTestSuite) SetupTest() {
authority,
)

suite.Require().NoError(suite.bankKeeper.SetParams(ctx, banktypes.Params{
DefaultSendEnabled: banktypes.DefaultDefaultSendEnabled,
}))

banktypes.RegisterInterfaces(encCfg.InterfaceRegistry)

queryHelper := baseapp.NewQueryServerTestHelper(ctx, encCfg.InterfaceRegistry)
Expand All @@ -175,7 +179,7 @@ func (suite *KeeperTestSuite) mockMintCoins(moduleAcc *authtypes.ModuleAccount)
suite.authKeeper.EXPECT().GetModuleAccount(suite.ctx, moduleAcc.Name).Return(moduleAcc)
}

func (suite *KeeperTestSuite) mockSendCoinsFromModuleToAccount(moduleAcc *authtypes.ModuleAccount, accAddr sdk.AccAddress) {
func (suite *KeeperTestSuite) mockSendCoinsFromModuleToAccount(moduleAcc *authtypes.ModuleAccount, _ sdk.AccAddress) {
suite.authKeeper.EXPECT().GetModuleAddress(moduleAcc.Name).Return(moduleAcc.GetAddress())
suite.authKeeper.EXPECT().GetAccount(suite.ctx, moduleAcc.GetAddress()).Return(moduleAcc)
}
Expand All @@ -195,7 +199,7 @@ func (suite *KeeperTestSuite) mockSendCoinsFromAccountToModule(acc *authtypes.Ba
suite.authKeeper.EXPECT().GetAccount(suite.ctx, acc.GetAddress()).Return(acc)
}

func (suite *KeeperTestSuite) mockSendCoins(ctx context.Context, sender sdk.AccountI, receiver sdk.AccAddress) {
func (suite *KeeperTestSuite) mockSendCoins(ctx context.Context, sender sdk.AccountI, _ sdk.AccAddress) {
suite.authKeeper.EXPECT().GetAccount(ctx, sender.GetAddress()).Return(sender)
}

Expand All @@ -204,7 +208,7 @@ func (suite *KeeperTestSuite) mockFundAccount(receiver sdk.AccAddress) {
suite.mockSendCoinsFromModuleToAccount(mintAcc, receiver)
}

func (suite *KeeperTestSuite) mockInputOutputCoins(inputs []sdk.AccountI, outputs []sdk.AccAddress) {
func (suite *KeeperTestSuite) mockInputOutputCoins(inputs []sdk.AccountI, _ []sdk.AccAddress) {
for _, input := range inputs {
suite.authKeeper.EXPECT().GetAccount(suite.ctx, input.GetAddress()).Return(input)
}
Expand Down Expand Up @@ -385,6 +389,24 @@ func (suite *KeeperTestSuite) TestSendCoinsFromModuleToAccount_Blocklist() {
))
}

func (suite *KeeperTestSuite) TestSendCoinsFromModuleToAccount_CoinSendDisabled() {
ctx := suite.ctx
require := suite.Require()
keeper := suite.bankKeeper

suite.mockMintCoins(mintAcc)
require.NoError(keeper.MintCoins(ctx, banktypes.MintModuleName, initCoins))

keeper.SetSendEnabled(ctx, sdk.DefaultBondDenom, false)

suite.authKeeper.EXPECT().GetModuleAddress(mintAcc.Name).Return(mintAcc.GetAddress())
err := keeper.SendCoinsFromModuleToAccount(
ctx, banktypes.MintModuleName, accAddrs[2], initCoins,
)
require.Contains(err.Error(), "stake, is prohibited from being sent at this time")
keeper.SetSendEnabled(ctx, sdk.DefaultBondDenom, true)
}

func (suite *KeeperTestSuite) TestSupply_DelegateUndelegateCoins() {
ctx := suite.ctx
require := suite.Require()
Expand Down Expand Up @@ -1863,6 +1885,7 @@ func (suite *KeeperTestSuite) TestBalanceTrackingEvents() {
multiPermAcc.Name,
sdk.NewCoins(sdk.NewCoin("utxo", math.NewInt(100000)))),
)

// send coins to address
suite.mockSendCoinsFromModuleToAccount(multiPermAcc, accAddrs[0])
require.NoError(
Expand Down
Loading