From 48d7061aac1f9c90f317e32b46517c802b66c7d5 Mon Sep 17 00:00:00 2001 From: "KIm, JinSan" Date: Wed, 17 Mar 2021 13:35:58 +0900 Subject: [PATCH] chore: cherry-pick cosmos-sdk#8857 (#92) * Fix SendToModuleAccountTest * https://github.com/cosmos/cosmos-sdk/pull/8857 * https://github.com/cosmos/cosmos-sdk/commit/280ee4f15e46117a5061ab3a374d9b695ed082bf Co-authored-by: Jonathan Gimeno --- x/bank/app_test.go | 81 ------------------------------------------ x/bank/handler_test.go | 68 +++++++++++++++++++++++++++++++++-- 2 files changed, 66 insertions(+), 83 deletions(-) diff --git a/x/bank/app_test.go b/x/bank/app_test.go index 1db74ef2de..a4bc945c69 100644 --- a/x/bank/app_test.go +++ b/x/bank/app_test.go @@ -12,7 +12,6 @@ import ( sdk "github.com/line/lbm-sdk/v2/types" authtypes "github.com/line/lbm-sdk/v2/x/auth/types" "github.com/line/lbm-sdk/v2/x/bank/types" - distrtypes "github.com/line/lbm-sdk/v2/x/distribution/types" ) type ( @@ -122,86 +121,6 @@ func TestSendNotEnoughBalance(t *testing.T) { require.Equal(t, res2.GetSequence(), origSeq+1) } -// A module account cannot be the recipient of bank sends unless it has been marked as such -func TestSendToModuleAcc(t *testing.T) { - tests := []struct { - name string - fromBalance sdk.Coins - msg *types.MsgSend - expSimPass bool - expPass bool - expFromBalance sdk.Coins - expToBalance sdk.Coins - }{ - { - name: "Normal module account cannot be the recipient of bank sends", - fromBalance: coins, - msg: types.NewMsgSend(addr1, moduleAccAddr, coins), - expSimPass: false, - expPass: false, - expFromBalance: coins, - expToBalance: sdk.NewCoins(), - }, - { - name: "Allowed module account can be the recipient of bank sends", - fromBalance: coins, - msg: types.NewMsgSend(addr1, authtypes.NewModuleAddress(distrtypes.ModuleName), coins), - expPass: true, - expSimPass: true, - expFromBalance: sdk.NewCoins(), - expToBalance: coins, - }, - } - - for _, test := range tests { - test := test - t.Run(test.name, func(t *testing.T) { - acc := &authtypes.BaseAccount{ - Address: test.msg.FromAddress, - } - - genAccs := []authtypes.GenesisAccount{acc} - app := simapp.SetupWithGenesisAccounts(genAccs) - ctx := app.BaseApp.NewContext(false, tmproto.Header{}) - - fromAddr, err := sdk.AccAddressFromBech32(test.msg.FromAddress) - require.NoError(t, err) - toAddr, err := sdk.AccAddressFromBech32(test.msg.ToAddress) - require.NoError(t, err) - - err = app.BankKeeper.SetBalances(ctx, fromAddr, test.fromBalance) - require.NoError(t, err) - - app.Commit() - - res1 := app.AccountKeeper.GetAccount(ctx, fromAddr) - require.NotNil(t, res1) - require.Equal(t, acc, res1.(*authtypes.BaseAccount)) - - origAccNum := res1.GetAccountNumber() - origSeq := res1.GetSequence() - - header := tmproto.Header{Height: app.LastBlockHeight() + 1} - txGen := simapp.MakeTestEncodingConfig().TxConfig - _, _, err = simapp.SignCheckDeliver(t, txGen, app.BaseApp, header, []sdk.Msg{test.msg}, "", []uint64{origAccNum}, []uint64{origSeq}, test.expSimPass, test.expPass, priv1) - if test.expPass { - require.NoError(t, err) - } else { - require.Error(t, err) - } - - simapp.CheckBalance(t, app, fromAddr, test.expFromBalance) - simapp.CheckBalance(t, app, toAddr, test.expToBalance) - - res2 := app.AccountKeeper.GetAccount(app.NewContext(true, tmproto.Header{}), addr1) - require.NotNil(t, res2) - - require.Equal(t, res2.GetAccountNumber(), origAccNum) - require.Equal(t, res2.GetSequence(), origSeq+1) - }) - } -} - func TestMsgMultiSendWithAccounts(t *testing.T) { acc := &authtypes.BaseAccount{ Address: addr1.String(), diff --git a/x/bank/handler_test.go b/x/bank/handler_test.go index e38054ea69..af7192b01d 100644 --- a/x/bank/handler_test.go +++ b/x/bank/handler_test.go @@ -1,4 +1,4 @@ -package bank +package bank_test import ( "strings" @@ -7,13 +7,20 @@ import ( "github.com/stretchr/testify/require" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" + "github.com/line/lbm-sdk/v2/crypto/keys/secp256k1" + "github.com/line/lbm-sdk/v2/simapp" "github.com/line/lbm-sdk/v2/testutil/testdata" sdk "github.com/line/lbm-sdk/v2/types" sdkerrors "github.com/line/lbm-sdk/v2/types/errors" + authtypes "github.com/line/lbm-sdk/v2/x/auth/types" + "github.com/line/lbm-sdk/v2/x/bank" + bankkeeper "github.com/line/lbm-sdk/v2/x/bank/keeper" + "github.com/line/lbm-sdk/v2/x/bank/types" + stakingtypes "github.com/line/lbm-sdk/v2/x/staking/types" ) func TestInvalidMsg(t *testing.T) { - h := NewHandler(nil) + h := bank.NewHandler(nil) res, err := h(sdk.NewContext(nil, tmproto.Header{}, false, nil), testdata.NewTestMsg()) require.Error(t, err) @@ -22,3 +29,60 @@ func TestInvalidMsg(t *testing.T) { _, _, log := sdkerrors.ABCIInfo(err, false) require.True(t, strings.Contains(log, "unrecognized bank message type")) } + +// A module account cannot be the recipient of bank sends unless it has been marked as such +func TestSendToModuleAccount(t *testing.T) { + priv1 := secp256k1.GenPrivKey() + addr1 := sdk.AccAddress(priv1.PubKey().Address()) + moduleAccAddr := authtypes.NewModuleAddress(stakingtypes.BondedPoolName) + coins := sdk.Coins{sdk.NewInt64Coin("foocoin", 10)} + + tests := []struct { + name string + expectedError error + msg *types.MsgSend + }{ + { + name: "not allowed module account", + msg: types.NewMsgSend(addr1, moduleAccAddr, coins), + expectedError: sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "%s is not allowed to receive funds", moduleAccAddr), + }, + { + name: "allowed module account", + msg: types.NewMsgSend(addr1, authtypes.NewModuleAddress(stakingtypes.ModuleName), coins), + expectedError: nil, + }, + } + + acc1 := &authtypes.BaseAccount{ + Address: addr1.String(), + } + accs := authtypes.GenesisAccounts{acc1} + balances := []types.Balance{ + { + Address: addr1.String(), + Coins: coins, + }, + } + + app := simapp.SetupWithGenesisAccounts(accs, balances...) + ctx := app.BaseApp.NewContext(false, tmproto.Header{}) + + app.BankKeeper = bankkeeper.NewBaseKeeper( + app.AppCodec(), app.GetKey(types.StoreKey), app.AccountKeeper, app.GetSubspace(types.ModuleName), map[string]bool{ + moduleAccAddr.String(): true, + }, + ) + handler := bank.NewHandler(app.BankKeeper) + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + _, err := handler(ctx, tc.msg) + if tc.expectedError != nil { + require.EqualError(t, err, tc.expectedError.Error()) + } else { + require.NoError(t, err) + } + }) + } +}