test(auth): make test not depend on hard-coded account numbers (#15968)

Co-authored-by: unknown unknown <unknown@unknown>
This commit is contained in:
testinginprod 2023-04-28 17:51:33 +02:00 committed by GitHub
parent 16e940df55
commit 27c6be521a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 89 additions and 133 deletions

View File

@ -49,13 +49,10 @@ func TestSimulateGasCost(t *testing.T) {
}
return TestCaseArgs{
accNums: []uint64{0, 1, 2},
accSeqs: []uint64{0, 0, 0},
feeAmount: feeAmount,
gasLimit: testdata.NewTestGasLimit(),
msgs: msgs,
privs: []cryptotypes.PrivKey{accs[0].priv, accs[1].priv, accs[2].priv},
}
}.WithAccountsInfo(accs)
},
true,
true,
@ -74,13 +71,10 @@ func TestSimulateGasCost(t *testing.T) {
}
return TestCaseArgs{
accNums: []uint64{0, 1, 2},
accSeqs: []uint64{0, 0, 0},
feeAmount: feeAmount,
gasLimit: simulatedGas,
msgs: msgs,
privs: []cryptotypes.PrivKey{accs[0].priv, accs[1].priv, accs[2].priv},
}
}.WithAccountsInfo(accs)
},
false,
true,
@ -238,11 +232,8 @@ func TestAnteHandlerAccountNumbers(t *testing.T) {
suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), accs[0].acc.GetAddress(), gomock.Any(), gomock.Any()).Return(nil)
return TestCaseArgs{
accNums: []uint64{0},
accSeqs: []uint64{0},
msgs: []sdk.Msg{msg},
privs: []cryptotypes.PrivKey{accs[0].priv},
}
msgs: []sdk.Msg{msg},
}.WithAccountsInfo(accs)
},
false,
true,
@ -294,11 +285,8 @@ func TestAnteHandlerAccountNumbers(t *testing.T) {
suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
return TestCaseArgs{
accNums: []uint64{0, 1},
accSeqs: []uint64{0, 0},
msgs: []sdk.Msg{msg1, msg2},
privs: []cryptotypes.PrivKey{accs[0].priv, accs[1].priv},
}
msgs: []sdk.Msg{msg1, msg2},
}.WithAccountsInfo(accs)
},
false,
true,
@ -430,11 +418,8 @@ func TestAnteHandlerSequences(t *testing.T) {
suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
return TestCaseArgs{
accNums: []uint64{0},
accSeqs: []uint64{0},
msgs: []sdk.Msg{msg},
privs: []cryptotypes.PrivKey{accs[0].priv},
}
msgs: []sdk.Msg{msg},
}.WithAccountsInfo(accs)
},
false,
true,
@ -446,7 +431,7 @@ func TestAnteHandlerSequences(t *testing.T) {
accs := suite.CreateTestAccounts(1)
msg := testdata.NewTestMsg(accs[0].acc.GetAddress())
msgs := []sdk.Msg{msg}
privs, accNums, accSeqs := []cryptotypes.PrivKey{accs[0].priv}, []uint64{0}, []uint64{0}
privs, accNums, accSeqs := []cryptotypes.PrivKey{accs[0].priv}, []uint64{accs[0].acc.GetAccountNumber()}, []uint64{accs[0].acc.GetSequence()}
// This will be called only once given that the second tx will fail
suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(2)
@ -457,11 +442,8 @@ func TestAnteHandlerSequences(t *testing.T) {
require.NoError(t, err)
return TestCaseArgs{
accNums: accNums,
accSeqs: accSeqs,
msgs: msgs,
privs: privs,
}
msgs: msgs,
}.WithAccountsInfo(accs)
},
false,
false,
@ -474,7 +456,7 @@ func TestAnteHandlerSequences(t *testing.T) {
msg := testdata.NewTestMsg(accs[0].acc.GetAddress())
msgs := []sdk.Msg{msg}
privs, accNums, accSeqs := []cryptotypes.PrivKey{accs[0].priv}, []uint64{0}, []uint64{0}
privs, accNums, accSeqs := []cryptotypes.PrivKey{accs[0].priv}, []uint64{accs[0].acc.GetAccountNumber()}, []uint64{accs[0].acc.GetSequence()}
suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(2)
// Send the same tx before running the test case, then change the sequence to a valid one.
@ -483,14 +465,11 @@ func TestAnteHandlerSequences(t *testing.T) {
require.NoError(t, err)
// +1 the account sequence
accSeqs = []uint64{1}
require.NoError(t, accs[0].acc.SetSequence(accs[0].acc.GetSequence()+1))
return TestCaseArgs{
accNums: accNums,
accSeqs: accSeqs,
msgs: msgs,
privs: privs,
}
msgs: msgs,
}.WithAccountsInfo(accs)
},
false,
true,
@ -506,11 +485,8 @@ func TestAnteHandlerSequences(t *testing.T) {
suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
return TestCaseArgs{
accNums: []uint64{0, 1, 2},
accSeqs: []uint64{0, 0, 0},
msgs: []sdk.Msg{msg1, msg2},
privs: []cryptotypes.PrivKey{accs[0].priv, accs[1].priv, accs[2].priv},
}
msgs: []sdk.Msg{msg1, msg2},
}.WithAccountsInfo(accs)
},
false,
true,
@ -524,7 +500,9 @@ func TestAnteHandlerSequences(t *testing.T) {
msg2 := testdata.NewTestMsg(accs[2].acc.GetAddress(), accs[0].acc.GetAddress())
msgs := []sdk.Msg{msg1, msg2}
privs, accNums, accSeqs := []cryptotypes.PrivKey{accs[0].priv, accs[1].priv, accs[2].priv}, []uint64{0, 1, 2}, []uint64{0, 0, 0}
privs := []cryptotypes.PrivKey{accs[0].priv, accs[1].priv, accs[2].priv}
accNums := []uint64{accs[0].acc.GetAccountNumber(), accs[1].acc.GetAccountNumber(), accs[2].acc.GetAccountNumber()}
accSeqs := []uint64{accs[0].acc.GetSequence(), accs[1].acc.GetSequence(), accs[2].acc.GetSequence()}
suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(2)
// Send the same tx before running the test case, to trigger replay protection.
@ -533,11 +511,8 @@ func TestAnteHandlerSequences(t *testing.T) {
require.NoError(t, err)
return TestCaseArgs{
accNums: accNums,
accSeqs: accSeqs,
msgs: []sdk.Msg{msg1, msg2},
privs: privs,
}
msgs: []sdk.Msg{msg1, msg2},
}.WithAccountsInfo(accs)
},
false,
false,
@ -551,7 +526,9 @@ func TestAnteHandlerSequences(t *testing.T) {
msg2 := testdata.NewTestMsg(accs[2].acc.GetAddress(), accs[0].acc.GetAddress())
msgs := []sdk.Msg{msg1, msg2}
privs, accNums, accSeqs := []cryptotypes.PrivKey{accs[0].priv, accs[1].priv, accs[2].priv}, []uint64{0, 1, 2}, []uint64{0, 0, 0}
privs := []cryptotypes.PrivKey{accs[0].priv, accs[1].priv, accs[2].priv}
accNums := []uint64{accs[0].acc.GetAccountNumber(), accs[1].acc.GetAccountNumber(), accs[2].acc.GetAccountNumber()}
accSeqs := []uint64{accs[0].acc.GetSequence(), accs[1].acc.GetSequence(), accs[2].acc.GetSequence()}
suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(2)
// Send the same tx before running the test case, to trigger replay protection.
@ -563,7 +540,7 @@ func TestAnteHandlerSequences(t *testing.T) {
// thus the sequence (0) is incorrect.
msg1 = testdata.NewTestMsg(accs[1].acc.GetAddress())
msgs = []sdk.Msg{msg1}
privs, accNums, accSeqs = []cryptotypes.PrivKey{accs[1].priv}, []uint64{1}, []uint64{0}
privs, accNums, accSeqs = []cryptotypes.PrivKey{accs[1].priv}, []uint64{accs[1].acc.GetAccountNumber()}, []uint64{0}
return TestCaseArgs{
accNums: accNums,
@ -584,7 +561,9 @@ func TestAnteHandlerSequences(t *testing.T) {
msg2 := testdata.NewTestMsg(accs[2].acc.GetAddress(), accs[0].acc.GetAddress())
msgs := []sdk.Msg{msg1, msg2}
privs, accNums, accSeqs := []cryptotypes.PrivKey{accs[0].priv, accs[1].priv, accs[2].priv}, []uint64{0, 1, 2}, []uint64{0, 0, 0}
privs := []cryptotypes.PrivKey{accs[0].priv, accs[1].priv, accs[2].priv}
accNums := []uint64{accs[0].acc.GetAccountNumber(), accs[1].acc.GetAccountNumber(), accs[2].acc.GetAccountNumber()}
accSeqs := []uint64{accs[0].acc.GetSequence(), accs[1].acc.GetSequence(), accs[2].acc.GetSequence()}
suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(2)
// Send the same tx before running the test case, to trigger replay protection.
@ -596,7 +575,7 @@ func TestAnteHandlerSequences(t *testing.T) {
// and the sequence was fixed (1).
msg1 = testdata.NewTestMsg(accs[1].acc.GetAddress())
msgs = []sdk.Msg{msg1}
privs, accNums, accSeqs = []cryptotypes.PrivKey{accs[1].priv}, []uint64{1}, []uint64{1}
privs, accNums, accSeqs = []cryptotypes.PrivKey{accs[1].priv}, []uint64{accs[1].acc.GetAccountNumber()}, []uint64{accs[1].acc.GetSequence() + 1}
return TestCaseArgs{
accNums: accNums,
@ -638,11 +617,8 @@ func TestAnteHandlerFees(t *testing.T) {
suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), accs[0].acc.GetAddress(), gomock.Any(), feeAmount).Return(sdkerrors.ErrInsufficientFunds)
return TestCaseArgs{
accNums: []uint64{0},
accSeqs: []uint64{0},
msgs: []sdk.Msg{testdata.NewTestMsg(accs[0].acc.GetAddress())},
privs: []cryptotypes.PrivKey{accs[0].priv},
}
msgs: []sdk.Msg{testdata.NewTestMsg(accs[0].acc.GetAddress())},
}.WithAccountsInfo(accs)
},
false,
false,
@ -655,11 +631,8 @@ func TestAnteHandlerFees(t *testing.T) {
suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), accs[0].acc.GetAddress(), gomock.Any(), feeAmount).Return(nil)
return TestCaseArgs{
accNums: []uint64{0},
accSeqs: []uint64{0},
msgs: []sdk.Msg{testdata.NewTestMsg(accs[0].acc.GetAddress())},
privs: []cryptotypes.PrivKey{accs[0].priv},
}
msgs: []sdk.Msg{testdata.NewTestMsg(accs[0].acc.GetAddress())},
}.WithAccountsInfo(accs)
},
false,
true,
@ -688,13 +661,10 @@ func TestAnteHandlerMemoGas(t *testing.T) {
func(suite *AnteTestSuite) TestCaseArgs {
accs := suite.CreateTestAccounts(1)
return TestCaseArgs{
accNums: []uint64{0},
accSeqs: []uint64{0},
feeAmount: sdk.NewCoins(sdk.NewInt64Coin("atom", 0)),
gasLimit: 0,
msgs: []sdk.Msg{testdata.NewTestMsg(accs[0].acc.GetAddress())},
privs: []cryptotypes.PrivKey{accs[0].priv},
}
}.WithAccountsInfo(accs)
},
false,
false,
@ -707,13 +677,10 @@ func TestAnteHandlerMemoGas(t *testing.T) {
suite.txBuilder.SetMemo("abcininasidniandsinasindiansdiansdinaisndiasndiadninsd")
return TestCaseArgs{
accNums: []uint64{0},
accSeqs: []uint64{0},
feeAmount: sdk.NewCoins(sdk.NewInt64Coin("atom", 0)),
gasLimit: 801,
msgs: []sdk.Msg{testdata.NewTestMsg(accs[0].acc.GetAddress())},
privs: []cryptotypes.PrivKey{accs[0].priv},
}
}.WithAccountsInfo(accs)
},
false,
false,
@ -726,13 +693,10 @@ func TestAnteHandlerMemoGas(t *testing.T) {
suite.txBuilder.SetMemo(strings.Repeat("01234567890", 500))
return TestCaseArgs{
accNums: []uint64{0},
accSeqs: []uint64{0},
feeAmount: sdk.NewCoins(sdk.NewInt64Coin("atom", 0)),
gasLimit: 50000,
msgs: []sdk.Msg{testdata.NewTestMsg(accs[0].acc.GetAddress())},
privs: []cryptotypes.PrivKey{accs[0].priv},
}
}.WithAccountsInfo(accs)
},
false,
false,
@ -744,13 +708,10 @@ func TestAnteHandlerMemoGas(t *testing.T) {
accs := suite.CreateTestAccounts(1)
suite.txBuilder.SetMemo(strings.Repeat("0123456789", 10))
return TestCaseArgs{
accNums: []uint64{0},
accSeqs: []uint64{0},
feeAmount: sdk.NewCoins(sdk.NewInt64Coin("atom", 0)),
gasLimit: 60000,
msgs: []sdk.Msg{testdata.NewTestMsg(accs[0].acc.GetAddress())},
privs: []cryptotypes.PrivKey{accs[0].priv},
}
}.WithAccountsInfo(accs)
},
false,
true,
@ -785,11 +746,8 @@ func TestAnteHandlerMultiSigner(t *testing.T) {
suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
return TestCaseArgs{
accNums: []uint64{0, 1, 2},
accSeqs: []uint64{0, 0, 0},
msgs: []sdk.Msg{msg1, msg2, msg3},
privs: []cryptotypes.PrivKey{accs[0].priv, accs[1].priv, accs[2].priv},
}
msgs: []sdk.Msg{msg1, msg2, msg3},
}.WithAccountsInfo(accs)
},
false,
true,
@ -803,7 +761,10 @@ func TestAnteHandlerMultiSigner(t *testing.T) {
msg2 := testdata.NewTestMsg(accs[2].acc.GetAddress(), accs[0].acc.GetAddress())
msg3 := testdata.NewTestMsg(accs[1].acc.GetAddress(), accs[2].acc.GetAddress())
msgs := []sdk.Msg{msg1, msg2, msg3}
privs, accNums, accSeqs := []cryptotypes.PrivKey{accs[0].priv, accs[1].priv, accs[2].priv}, []uint64{0, 1, 2}, []uint64{0, 0, 0}
privs := []cryptotypes.PrivKey{accs[0].priv, accs[1].priv, accs[2].priv}
accNums := []uint64{accs[0].acc.GetAccountNumber(), accs[1].acc.GetAccountNumber(), accs[2].acc.GetAccountNumber()}
accSeqs := []uint64{accs[0].acc.GetSequence(), accs[1].acc.GetSequence(), accs[2].acc.GetSequence()}
suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(2)
@ -812,7 +773,7 @@ func TestAnteHandlerMultiSigner(t *testing.T) {
require.NoError(t, err)
msgs = []sdk.Msg{msg1}
privs, accNums, accSeqs = []cryptotypes.PrivKey{accs[0].priv, accs[1].priv}, []uint64{0, 1}, []uint64{1, 1}
privs, accNums, accSeqs = []cryptotypes.PrivKey{accs[0].priv, accs[1].priv}, []uint64{accs[0].acc.GetAccountNumber(), accs[1].acc.GetAccountNumber()}, []uint64{accs[0].acc.GetSequence() + 1, accs[1].acc.GetSequence() + 1}
return TestCaseArgs{
accNums: accNums,
@ -833,7 +794,10 @@ func TestAnteHandlerMultiSigner(t *testing.T) {
msg2 := testdata.NewTestMsg(accs[2].acc.GetAddress(), accs[0].acc.GetAddress())
msg3 := testdata.NewTestMsg(accs[1].acc.GetAddress(), accs[2].acc.GetAddress())
msgs := []sdk.Msg{msg1, msg2, msg3}
privs, accNums, accSeqs := []cryptotypes.PrivKey{accs[0].priv, accs[1].priv, accs[2].priv}, []uint64{0, 1, 2}, []uint64{0, 0, 0}
privs := []cryptotypes.PrivKey{accs[0].priv, accs[1].priv, accs[2].priv}
accNums := []uint64{accs[0].acc.GetAccountNumber(), accs[1].acc.GetAccountNumber(), accs[2].acc.GetAccountNumber()}
accSeqs := []uint64{accs[0].acc.GetSequence(), accs[1].acc.GetSequence(), accs[2].acc.GetSequence()}
suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(2)
var err error
@ -841,7 +805,7 @@ func TestAnteHandlerMultiSigner(t *testing.T) {
require.NoError(t, err)
msgs = []sdk.Msg{msg3}
privs, accNums, accSeqs = []cryptotypes.PrivKey{accs[1].priv, accs[2].priv}, []uint64{1, 2}, []uint64{1, 1}
privs, accNums, accSeqs = []cryptotypes.PrivKey{accs[1].priv, accs[2].priv}, []uint64{accs[1].acc.GetAccountNumber(), accs[2].acc.GetAccountNumber()}, []uint64{accs[1].acc.GetSequence() + 1, accs[2].acc.GetSequence() + 1}
return TestCaseArgs{
accNums: accNums,
@ -863,21 +827,22 @@ func TestAnteHandlerMultiSigner(t *testing.T) {
msg3 := testdata.NewTestMsg(accs[1].acc.GetAddress(), accs[2].acc.GetAddress())
msgs := []sdk.Msg{msg1, msg2, msg3}
privs, accNums, accSeqs := []cryptotypes.PrivKey{accs[0].priv, accs[1].priv, accs[2].priv}, []uint64{0, 1, 2}, []uint64{0, 0, 0}
privs := []cryptotypes.PrivKey{accs[0].priv, accs[1].priv, accs[2].priv}
accNums := []uint64{accs[0].acc.GetAccountNumber(), accs[1].acc.GetAccountNumber(), accs[2].acc.GetAccountNumber()}
accSeqs := []uint64{accs[0].acc.GetSequence(), accs[1].acc.GetSequence(), accs[2].acc.GetSequence()}
suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(2)
var err error
suite.ctx, err = suite.DeliverMsgs(t, privs, msgs, feeAmount, gasLimit, accNums, accSeqs, suite.ctx.ChainID(), false)
require.NoError(t, err)
privs, accNums, accSeqs = []cryptotypes.PrivKey{accs[0].priv, accs[1].priv, accs[2].priv}, []uint64{0, 1, 2}, []uint64{1, 1, 1}
for _, acc := range accs {
require.NoError(t, acc.acc.SetSequence(acc.acc.GetSequence()+1))
}
return TestCaseArgs{
accNums: accNums,
accSeqs: accSeqs,
msgs: msgs,
privs: privs,
}
msgs: msgs,
}.WithAccountsInfo(accs)
},
false,
true,
@ -913,13 +878,10 @@ func TestAnteHandlerBadSignBytes(t *testing.T) {
return TestCaseArgs{
chainID: suite.ctx.ChainID(),
accNums: []uint64{0},
accSeqs: []uint64{0},
feeAmount: feeAmount,
gasLimit: gasLimit,
msgs: []sdk.Msg{msg0},
privs: []cryptotypes.PrivKey{accs[0].priv},
}
}.WithAccountsInfo(accs)
},
false,
true,
@ -934,13 +896,10 @@ func TestAnteHandlerBadSignBytes(t *testing.T) {
return TestCaseArgs{
chainID: "wrong-chain-id",
accNums: []uint64{0},
accSeqs: []uint64{0},
feeAmount: feeAmount,
gasLimit: gasLimit,
msgs: []sdk.Msg{msg0},
privs: []cryptotypes.PrivKey{accs[0].priv},
}
}.WithAccountsInfo(accs)
},
false,
false,
@ -952,16 +911,14 @@ func TestAnteHandlerBadSignBytes(t *testing.T) {
accs := suite.CreateTestAccounts(1)
msg0 := testdata.NewTestMsg(accs[0].acc.GetAddress())
suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
require.NoError(t, accs[0].acc.SetSequence(2)) // wrong accSeq
return TestCaseArgs{
chainID: suite.ctx.ChainID(),
accNums: []uint64{0},
accSeqs: []uint64{2}, // wrong accSeq
feeAmount: feeAmount,
gasLimit: gasLimit,
msgs: []sdk.Msg{msg0},
privs: []cryptotypes.PrivKey{accs[0].priv},
}
}.WithAccountsInfo(accs)
},
false,
false,
@ -973,16 +930,14 @@ func TestAnteHandlerBadSignBytes(t *testing.T) {
accs := suite.CreateTestAccounts(1)
msg0 := testdata.NewTestMsg(accs[0].acc.GetAddress())
suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
require.NoError(t, accs[0].acc.SetAccountNumber(2)) // wrong accNum
return TestCaseArgs{
chainID: suite.ctx.ChainID(),
accNums: []uint64{1}, // wrong accNum
accSeqs: []uint64{0},
feeAmount: feeAmount,
gasLimit: gasLimit,
msgs: []sdk.Msg{msg0},
privs: []cryptotypes.PrivKey{accs[0].priv},
}
}.WithAccountsInfo(accs)
},
false,
false,
@ -996,13 +951,10 @@ func TestAnteHandlerBadSignBytes(t *testing.T) {
return TestCaseArgs{
chainID: suite.ctx.ChainID(),
accNums: []uint64{0},
accSeqs: []uint64{0},
feeAmount: feeAmount,
gasLimit: gasLimit,
msgs: []sdk.Msg{testdata.NewTestMsg(accs[1].acc.GetAddress())}, // wrong account in the msg
privs: []cryptotypes.PrivKey{accs[0].priv},
}
}.WithAccountsInfo(accs[0:1])
},
false,
false,
@ -1017,8 +969,8 @@ func TestAnteHandlerBadSignBytes(t *testing.T) {
return TestCaseArgs{
chainID: suite.ctx.ChainID(),
accNums: []uint64{0},
accSeqs: []uint64{0},
accNums: []uint64{accs[0].acc.GetAccountNumber()},
accSeqs: []uint64{accs[0].acc.GetSequence()},
feeAmount: feeAmount,
gasLimit: gasLimit,
msgs: []sdk.Msg{msg0},
@ -1037,8 +989,8 @@ func TestAnteHandlerBadSignBytes(t *testing.T) {
return TestCaseArgs{
chainID: suite.ctx.ChainID(),
accNums: []uint64{1},
accSeqs: []uint64{0},
accNums: []uint64{accs[1].acc.GetAccountNumber()},
accSeqs: []uint64{accs[1].acc.GetSequence()},
feeAmount: feeAmount,
gasLimit: gasLimit,
msgs: []sdk.Msg{testdata.NewTestMsg(accs[1].acc.GetAddress())},
@ -1074,11 +1026,8 @@ func TestAnteHandlerSetPubKey(t *testing.T) {
suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
return TestCaseArgs{
accNums: []uint64{0},
accSeqs: []uint64{0},
msgs: []sdk.Msg{testdata.NewTestMsg(accs[0].acc.GetAddress())},
privs: []cryptotypes.PrivKey{accs[0].priv},
}
msgs: []sdk.Msg{testdata.NewTestMsg(accs[0].acc.GetAddress())},
}.WithAccountsInfo(accs)
},
false,
true,
@ -1090,7 +1039,7 @@ func TestAnteHandlerSetPubKey(t *testing.T) {
accs := suite.CreateTestAccounts(1)
suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(2)
privs, accNums, accSeqs := []cryptotypes.PrivKey{accs[0].priv}, []uint64{0}, []uint64{0}
privs, accNums, accSeqs := []cryptotypes.PrivKey{accs[0].priv}, []uint64{accs[0].acc.GetAccountNumber()}, []uint64{accs[0].acc.GetSequence()}
msgs := []sdk.Msg{testdata.NewTestMsg(accs[0].acc.GetAddress())}
var err error
suite.ctx, err = suite.DeliverMsgs(t, privs, msgs, feeAmount, gasLimit, accNums, accSeqs, suite.ctx.ChainID(), false)
@ -1117,11 +1066,8 @@ func TestAnteHandlerSetPubKey(t *testing.T) {
accs := suite.CreateTestAccounts(2)
suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
return TestCaseArgs{
accNums: []uint64{0},
accSeqs: []uint64{0},
msgs: []sdk.Msg{testdata.NewTestMsg(accs[1].acc.GetAddress())},
privs: []cryptotypes.PrivKey{accs[0].priv}, // wrong signer
}
msgs: []sdk.Msg{testdata.NewTestMsg(accs[1].acc.GetAddress())},
}.WithAccountsInfo(accs[0:1])
},
false,
false,
@ -1137,7 +1083,7 @@ func TestAnteHandlerSetPubKey(t *testing.T) {
acc1 := suite.accountKeeper.GetAccount(suite.ctx, accs[1].acc.GetAddress())
require.Nil(t, acc1.GetPubKey())
privs, accNums, accSeqs := []cryptotypes.PrivKey{accs[1].priv}, []uint64{1}, []uint64{0}
privs, accNums, accSeqs := []cryptotypes.PrivKey{accs[1].priv}, []uint64{accs[1].acc.GetAccountNumber()}, []uint64{accs[1].acc.GetSequence()}
msgs := []sdk.Msg{testdata.NewTestMsg(accs[1].acc.GetAddress())}
suite.txBuilder.SetMsgs(msgs...)
suite.txBuilder.SetFeeAmount(feeAmount)
@ -1160,7 +1106,7 @@ func TestAnteHandlerSetPubKey(t *testing.T) {
require.Nil(t, acc1.GetPubKey())
// Set incorrect accSeq, to generate incorrect signature.
privs, accNums, accSeqs = []cryptotypes.PrivKey{accs[1].priv}, []uint64{1}, []uint64{1}
privs, accNums, accSeqs = []cryptotypes.PrivKey{accs[1].priv}, []uint64{accs[1].acc.GetAccountNumber()}, []uint64{1}
return TestCaseArgs{
accNums: accNums,
@ -1183,7 +1129,7 @@ func TestAnteHandlerSetPubKey(t *testing.T) {
acc1 := suite.accountKeeper.GetAccount(suite.ctx, accs[1].acc.GetAddress())
require.Nil(t, acc1.GetPubKey())
privs, accNums, accSeqs := []cryptotypes.PrivKey{accs[1].priv}, []uint64{1}, []uint64{0}
privs, accNums, accSeqs := []cryptotypes.PrivKey{accs[1].priv}, []uint64{accs[1].acc.GetAccountNumber()}, []uint64{accs[1].acc.GetSequence()}
msgs := []sdk.Msg{testdata.NewTestMsg(accs[1].acc.GetAddress())}
suite.txBuilder.SetMsgs(msgs...)
suite.txBuilder.SetFeeAmount(feeAmount)
@ -1206,7 +1152,7 @@ func TestAnteHandlerSetPubKey(t *testing.T) {
require.Nil(t, acc1.GetPubKey())
// Set incorrect accSeq, to generate incorrect signature.
privs, accNums, accSeqs = []cryptotypes.PrivKey{accs[1].priv}, []uint64{1}, []uint64{1}
privs, accNums, accSeqs = []cryptotypes.PrivKey{accs[1].priv}, []uint64{accs[1].acc.GetAccountNumber()}, []uint64{1}
suite.ctx, err = suite.DeliverMsgs(t, privs, msgs, feeAmount, gasLimit, accNums, accSeqs, suite.ctx.ChainID(), false)
require.Error(t, err)

View File

@ -117,7 +117,7 @@ func (suite *AnteTestSuite) CreateTestAccounts(numAccs int) []TestAccount {
for i := 0; i < numAccs; i++ {
priv, _, addr := testdata.KeyTestPubAddr()
acc := suite.accountKeeper.NewAccountWithAddress(suite.ctx, addr)
acc.SetAccountNumber(uint64(i))
acc.SetAccountNumber(uint64(i + 1000))
suite.accountKeeper.SetAccount(suite.ctx, acc)
accounts = append(accounts, TestAccount{acc, priv})
}
@ -144,6 +144,16 @@ type TestCaseArgs struct {
privs []cryptotypes.PrivKey
}
func (t TestCaseArgs) WithAccountsInfo(accs []TestAccount) TestCaseArgs {
newT := t
for _, acc := range accs {
newT.accNums = append(newT.accNums, acc.acc.GetAccountNumber())
newT.accSeqs = append(newT.accSeqs, acc.acc.GetSequence())
newT.privs = append(newT.privs, acc.priv)
}
return newT
}
// DeliverMsgs constructs a tx and runs it through the ante handler. This is used to set the context for a test case, for
// example to test for replay protection.
func (suite *AnteTestSuite) DeliverMsgs(t *testing.T, privs []cryptotypes.PrivKey, msgs []sdk.Msg, feeAmount sdk.Coins, gasLimit uint64, accNums, accSeqs []uint64, chainID string, simulate bool) (sdk.Context, error) {