Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
likhita-809 committed Jul 18, 2022
1 parent 6a08830 commit abc4340
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 76 deletions.
64 changes: 0 additions & 64 deletions x/bank/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
Expand All @@ -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),
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion x/bank/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions x/bank/simulation/operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions x/bank/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
4 changes: 4 additions & 0 deletions x/bank/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
23 changes: 16 additions & 7 deletions x/bank/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,62 +178,71 @@ 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,
MsgMultiSend{
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,
MsgMultiSend{
Inputs: []Input{input1},
Outputs: []Output{output1},
},
"",
},
{
true,
false,
MsgMultiSend{
Inputs: []Input{input1, input2},
Outputs: []Output{outputMulti},
},
"multiple senders not allowed",
},
{
true,
MsgMultiSend{
Inputs: []Input{NewInput(addr2, atom123.MulInt(sdk.NewInt(2)))},
Outputs: []Output{output1, output1},
},
"",
},
}

for i, tc := range cases {
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)
}
}
}
Expand Down

0 comments on commit abc4340

Please sign in to comment.