From abc4340f878dd03a91aa14c93d1122cdd76f965a Mon Sep 17 00:00:00 2001 From: likhita-809 Date: Mon, 18 Jul 2022 16:46:18 +0530 Subject: [PATCH] wip --- x/bank/app_test.go | 64 ---------------------------- x/bank/simulation/operations.go | 2 +- x/bank/simulation/operations_test.go | 8 ++-- x/bank/types/errors.go | 1 + x/bank/types/msgs.go | 4 ++ x/bank/types/msgs_test.go | 23 +++++++--- 6 files changed, 26 insertions(+), 76 deletions(-) diff --git a/x/bank/app_test.go b/x/bank/app_test.go index d1e810277edd..2df9c3a685a0 100644 --- a/x/bank/app_test.go +++ b/x/bank/app_test.go @@ -39,8 +39,6 @@ var ( priv2 = secp256k1.GenPrivKey() addr2 = sdk.AccAddress(priv2.PubKey().Address()) addr3 = sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()) - priv4 = secp256k1.GenPrivKey() - addr4 = sdk.AccAddress(priv4.PubKey().Address()) coins = sdk.Coins{sdk.NewInt64Coin("foocoin", 10)} halfCoins = sdk.Coins{sdk.NewInt64Coin("foocoin", 5)} @@ -58,16 +56,6 @@ var ( types.NewOutput(addr3, halfCoins), }, } - multiSendMsg3 = &types.MsgMultiSend{ - Inputs: []types.Input{ - types.NewInput(addr1, coins), - types.NewInput(addr4, coins), - }, - Outputs: []types.Output{ - types.NewOutput(addr2, coins), - types.NewOutput(addr3, coins), - }, - } multiSendMsg4 = &types.MsgMultiSend{ Inputs: []types.Input{ types.NewInput(addr2, coins), @@ -234,58 +222,6 @@ func TestMsgMultiSendMultipleOut(t *testing.T) { } } -func TestMsgMultiSendMultipleInOut(t *testing.T) { - acc1 := &authtypes.BaseAccount{ - Address: addr1.String(), - } - acc2 := &authtypes.BaseAccount{ - Address: addr2.String(), - } - acc4 := &authtypes.BaseAccount{ - Address: addr4.String(), - } - - genAccs := []authtypes.GenesisAccount{acc1, acc2, acc4} - app := simapp.SetupWithGenesisAccounts(t, genAccs) - ctx := app.BaseApp.NewContext(false, tmproto.Header{}) - - require.NoError(t, testutil.FundAccount(app.BankKeeper, ctx, addr1, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42)))) - - require.NoError(t, testutil.FundAccount(app.BankKeeper, ctx, addr2, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42)))) - - require.NoError(t, testutil.FundAccount(app.BankKeeper, ctx, addr4, sdk.NewCoins(sdk.NewInt64Coin("foocoin", 42)))) - - app.Commit() - - testCases := []appTestCase{ - { - msgs: []sdk.Msg{multiSendMsg3}, - accNums: []uint64{0, 2}, - accSeqs: []uint64{0, 0}, - expSimPass: true, - expPass: true, - privKeys: []cryptotypes.PrivKey{priv1, priv4}, - expectedBalances: []expectedBalance{ - {addr1, sdk.Coins{sdk.NewInt64Coin("foocoin", 32)}}, - {addr4, sdk.Coins{sdk.NewInt64Coin("foocoin", 32)}}, - {addr2, sdk.Coins{sdk.NewInt64Coin("foocoin", 52)}}, - {addr3, sdk.Coins{sdk.NewInt64Coin("foocoin", 10)}}, - }, - }, - } - - for _, tc := range testCases { - header := tmproto.Header{Height: app.LastBlockHeight() + 1} - txGen := simapp.MakeTestEncodingConfig().TxConfig - _, _, err := simapp.SignCheckDeliver(t, txGen, app.BaseApp, header, tc.msgs, "", tc.accNums, tc.accSeqs, tc.expSimPass, tc.expPass, tc.privKeys...) - require.NoError(t, err) - - for _, eb := range tc.expectedBalances { - simapp.CheckBalance(t, app, eb.addr, eb.coins) - } - } -} - func TestMsgMultiSendDependent(t *testing.T) { acc1 := authtypes.NewBaseAccountWithAddress(addr1) acc2 := authtypes.NewBaseAccountWithAddress(addr2) diff --git a/x/bank/simulation/operations.go b/x/bank/simulation/operations.go index b1456b7c6d90..ab8528b7d0ea 100644 --- a/x/bank/simulation/operations.go +++ b/x/bank/simulation/operations.go @@ -177,7 +177,7 @@ func SimulateMsgMultiSend(ak types.AccountKeeper, bk keeper.Keeper) simtypes.Ope accs []simtypes.Account, chainID string, ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { // random number of inputs/outputs between [1, 3] - inputs := make([]types.Input, r.Intn(3)+1) + inputs := make([]types.Input, r.Intn(1)+1) outputs := make([]types.Output, r.Intn(3)+1) // collect signer privKeys diff --git a/x/bank/simulation/operations_test.go b/x/bank/simulation/operations_test.go index f20a5c1e0a62..6a0b8c34ac67 100644 --- a/x/bank/simulation/operations_test.go +++ b/x/bank/simulation/operations_test.go @@ -114,12 +114,12 @@ func (suite *SimTestSuite) TestSimulateMsgMultiSend() { types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) require.True(operationMsg.OK) - require.Len(msg.Inputs, 3) - require.Equal("cosmos1p8wcgrjr4pjju90xg6u9cgq55dxwq8j7u4x9a0", msg.Inputs[1].Address) - require.Equal("185121068stake", msg.Inputs[1].Coins.String()) + require.Len(msg.Inputs, 1) + require.Equal("cosmos1tnh2q55v8wyygtt9srz5safamzdengsnqeycj3", msg.Inputs[0].Address) + require.Equal("114949958stake", msg.Inputs[0].Coins.String()) require.Len(msg.Outputs, 2) require.Equal("cosmos1ghekyjucln7y67ntx7cf27m9dpuxxemn4c8g4r", msg.Outputs[1].Address) - require.Equal("260469617stake", msg.Outputs[1].Coins.String()) + require.Equal("107287087stake", msg.Outputs[1].Coins.String()) require.Equal(types.TypeMsgMultiSend, msg.Type()) require.Equal(types.ModuleName, msg.Route()) require.Len(futureOperations, 0) diff --git a/x/bank/types/errors.go b/x/bank/types/errors.go index 8446d957b678..3fca352de3f4 100644 --- a/x/bank/types/errors.go +++ b/x/bank/types/errors.go @@ -12,4 +12,5 @@ var ( ErrSendDisabled = sdkerrors.Register(ModuleName, 5, "send transactions are disabled") ErrDenomMetadataNotFound = sdkerrors.Register(ModuleName, 6, "client denom metadata not found") ErrInvalidKey = sdkerrors.Register(ModuleName, 7, "invalid key") + ErrMultipleSenders = sdkerrors.Register(ModuleName, 8, "multiple senders not allowed") ) diff --git a/x/bank/types/msgs.go b/x/bank/types/msgs.go index b2b0d21e48ad..0d58908da1c0 100644 --- a/x/bank/types/msgs.go +++ b/x/bank/types/msgs.go @@ -79,6 +79,10 @@ func (msg MsgMultiSend) ValidateBasic() error { return ErrNoInputs } + if len(msg.Inputs) != 1 { + return ErrMultipleSenders + } + if len(msg.Outputs) == 0 { return ErrNoOutputs } diff --git a/x/bank/types/msgs_test.go b/x/bank/types/msgs_test.go index 6223db054f8e..5d31ca943f13 100644 --- a/x/bank/types/msgs_test.go +++ b/x/bank/types/msgs_test.go @@ -178,18 +178,20 @@ func TestMsgMultiSendValidation(t *testing.T) { var emptyAddr sdk.AccAddress cases := []struct { - valid bool - tx MsgMultiSend + valid bool + tx MsgMultiSend + expErrMsg string }{ - {false, MsgMultiSend{}}, // no input or output - {false, MsgMultiSend{Inputs: []Input{input1}}}, // just input - {false, MsgMultiSend{Outputs: []Output{output1}}}, // just output + {false, MsgMultiSend{}, "no inputs to send transaction"}, // no input or output + {false, MsgMultiSend{Inputs: []Input{input1}}, "no outputs to send transaction"}, // just input + {false, MsgMultiSend{Outputs: []Output{output1}}, "no inputs to send transaction"}, // just output { false, MsgMultiSend{ Inputs: []Input{NewInput(emptyAddr, atom123)}, // invalid input Outputs: []Output{output1}, }, + "invalid input address", }, { false, @@ -197,13 +199,15 @@ func TestMsgMultiSendValidation(t *testing.T) { Inputs: []Input{input1}, Outputs: []Output{{emptyAddr.String(), atom123}}, // invalid output }, + "invalid output address", }, { false, MsgMultiSend{ Inputs: []Input{input1}, - Outputs: []Output{output2}, // amounts dont match + Outputs: []Output{output2}, // amounts don't match }, + "sum inputs != sum outputs", }, { true, @@ -211,13 +215,15 @@ func TestMsgMultiSendValidation(t *testing.T) { Inputs: []Input{input1}, Outputs: []Output{output1}, }, + "", }, { - true, + false, MsgMultiSend{ Inputs: []Input{input1, input2}, Outputs: []Output{outputMulti}, }, + "multiple senders not allowed", }, { true, @@ -225,6 +231,7 @@ func TestMsgMultiSendValidation(t *testing.T) { Inputs: []Input{NewInput(addr2, atom123.MulInt(sdk.NewInt(2)))}, Outputs: []Output{output1, output1}, }, + "", }, } @@ -232,8 +239,10 @@ func TestMsgMultiSendValidation(t *testing.T) { err := tc.tx.ValidateBasic() if tc.valid { require.Nil(t, err, "%d: %+v", i, err) + require.Nil(t, err) } else { require.NotNil(t, err, "%d", i) + require.Contains(t, err.Error(), tc.expErrMsg) } } }