refactor(x/bank): check send enabled on coins (#20343)
This commit is contained in:
parent
1a8425a343
commit
3a750719ba
@ -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
|
||||
|
||||
@ -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)
|
||||
|
||||
|
||||
@ -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()
|
||||
|
||||
@ -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())
|
||||
|
||||
@ -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())
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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)
|
||||
@ -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)
|
||||
|
||||
@ -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))
|
||||
|
||||
@ -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))
|
||||
|
||||
|
||||
@ -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() {
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
@ -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)
|
||||
@ -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)
|
||||
}
|
||||
@ -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)
|
||||
}
|
||||
|
||||
@ -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)
|
||||
}
|
||||
@ -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()
|
||||
@ -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(
|
||||
|
||||
Loading…
Reference in New Issue
Block a user