github.com/cosmos/cosmos-sdk@v0.50.10/x/auth/ante/ante_test.go (about) 1 package ante_test 2 3 import ( 4 "encoding/json" 5 "errors" 6 "fmt" 7 "strings" 8 "testing" 9 10 "github.com/golang/mock/gomock" 11 "github.com/stretchr/testify/require" 12 13 errorsmod "cosmossdk.io/errors" 14 "cosmossdk.io/math" 15 storetypes "cosmossdk.io/store/types" 16 17 "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" 18 kmultisig "github.com/cosmos/cosmos-sdk/crypto/keys/multisig" 19 "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" 20 cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" 21 "github.com/cosmos/cosmos-sdk/testutil/testdata" 22 sdk "github.com/cosmos/cosmos-sdk/types" 23 sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" 24 "github.com/cosmos/cosmos-sdk/types/tx/signing" 25 "github.com/cosmos/cosmos-sdk/x/auth/ante" 26 authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" 27 ) 28 29 // Test that simulate transaction accurately estimates gas cost 30 func TestSimulateGasCost(t *testing.T) { 31 // This test has a test case that uses another's output. 32 var simulatedGas uint64 33 34 // Same data for every test case 35 feeAmount := testdata.NewTestFeeAmount() 36 37 testCases := []TestCase{ 38 { 39 "tx with 150atom fee", 40 func(suite *AnteTestSuite) TestCaseArgs { 41 accs := suite.CreateTestAccounts(3) 42 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), accs[0].acc.GetAddress(), authtypes.FeeCollectorName, feeAmount).Return(nil) 43 44 msgs := []sdk.Msg{ 45 testdata.NewTestMsg(accs[0].acc.GetAddress(), accs[1].acc.GetAddress()), 46 testdata.NewTestMsg(accs[2].acc.GetAddress(), accs[0].acc.GetAddress()), 47 testdata.NewTestMsg(accs[1].acc.GetAddress(), accs[2].acc.GetAddress()), 48 } 49 50 return TestCaseArgs{ 51 feeAmount: feeAmount, 52 gasLimit: testdata.NewTestGasLimit(), 53 msgs: msgs, 54 }.WithAccountsInfo(accs) 55 }, 56 true, 57 true, 58 nil, 59 }, 60 { 61 "with previously estimated gas", 62 func(suite *AnteTestSuite) TestCaseArgs { 63 accs := suite.CreateTestAccounts(3) 64 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), accs[0].acc.GetAddress(), authtypes.FeeCollectorName, feeAmount).Return(nil) 65 66 msgs := []sdk.Msg{ 67 testdata.NewTestMsg(accs[0].acc.GetAddress(), accs[1].acc.GetAddress()), 68 testdata.NewTestMsg(accs[2].acc.GetAddress(), accs[0].acc.GetAddress()), 69 testdata.NewTestMsg(accs[1].acc.GetAddress(), accs[2].acc.GetAddress()), 70 } 71 72 return TestCaseArgs{ 73 feeAmount: feeAmount, 74 gasLimit: simulatedGas, 75 msgs: msgs, 76 }.WithAccountsInfo(accs) 77 }, 78 false, 79 true, 80 nil, 81 }, 82 } 83 84 for _, tc := range testCases { 85 t.Run(fmt.Sprintf("Case %s", tc.desc), func(t *testing.T) { 86 suite := SetupTestSuite(t, false) 87 suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() 88 args := tc.malleate(suite) 89 args.chainID = suite.ctx.ChainID() 90 suite.RunTestCase(t, tc, args) 91 92 // Gather info for the next test case 93 simulatedGas = suite.ctx.GasMeter().GasConsumed() 94 }) 95 } 96 } 97 98 // Test various error cases in the AnteHandler control flow. 99 func TestAnteHandlerSigErrors(t *testing.T) { 100 // This test requires the accounts to not be set, so we create them here 101 priv0, _, addr0 := testdata.KeyTestPubAddr() 102 priv1, _, addr1 := testdata.KeyTestPubAddr() 103 priv2, _, addr2 := testdata.KeyTestPubAddr() 104 msgs := []sdk.Msg{ 105 testdata.NewTestMsg(addr0, addr1), 106 testdata.NewTestMsg(addr0, addr2), 107 } 108 109 testCases := []TestCase{ 110 { 111 "check no signatures fails", 112 func(suite *AnteTestSuite) TestCaseArgs { 113 privs, accNums, accSeqs := []cryptotypes.PrivKey{}, []uint64{}, []uint64{} 114 115 // Create tx manually to test the tx's signers 116 require.NoError(t, suite.txBuilder.SetMsgs(msgs...)) 117 tx, err := suite.CreateTestTx(suite.ctx, privs, accNums, accSeqs, suite.ctx.ChainID(), signing.SignMode_SIGN_MODE_DIRECT) 118 require.NoError(t, err) 119 120 // tx.GetSigners returns addresses in correct order: addr1, addr2, addr3 121 expectedSigners := [][]byte{addr0, addr1, addr2} 122 signers, err := tx.GetSigners() 123 require.NoError(t, err) 124 require.Equal(t, expectedSigners, signers) 125 126 return TestCaseArgs{ 127 accNums: accNums, 128 accSeqs: accSeqs, 129 msgs: msgs, 130 privs: privs, 131 } 132 }, 133 false, 134 false, 135 sdkerrors.ErrNoSignatures, 136 }, 137 { 138 "num sigs dont match GetSigners", 139 func(suite *AnteTestSuite) TestCaseArgs { 140 privs, accNums, accSeqs := []cryptotypes.PrivKey{priv0}, []uint64{0}, []uint64{0} 141 142 return TestCaseArgs{ 143 accNums: accNums, 144 accSeqs: accSeqs, 145 msgs: msgs, 146 privs: privs, 147 } 148 }, 149 false, 150 false, 151 sdkerrors.ErrUnauthorized, 152 }, 153 { 154 "unrecognized account", 155 func(suite *AnteTestSuite) TestCaseArgs { 156 privs, accNums, accSeqs := []cryptotypes.PrivKey{priv0, priv1, priv2}, []uint64{0, 1, 2}, []uint64{0, 0, 0} 157 158 return TestCaseArgs{ 159 accNums: accNums, 160 accSeqs: accSeqs, 161 msgs: msgs, 162 privs: privs, 163 } 164 }, 165 false, 166 false, 167 sdkerrors.ErrUnknownAddress, 168 }, 169 { 170 "save the first account, but second is still unrecognized", 171 func(suite *AnteTestSuite) TestCaseArgs { 172 suite.accountKeeper.SetAccount(suite.ctx, suite.accountKeeper.NewAccountWithAddress(suite.ctx, addr0)) 173 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) 174 175 privs, accNums, accSeqs := []cryptotypes.PrivKey{priv0, priv1, priv2}, []uint64{0, 1, 2}, []uint64{0, 0, 0} 176 177 return TestCaseArgs{ 178 accNums: accNums, 179 accSeqs: accSeqs, 180 msgs: msgs, 181 privs: privs, 182 } 183 }, 184 false, 185 false, 186 sdkerrors.ErrUnknownAddress, 187 }, 188 { 189 "save all the accounts, should pass", 190 func(suite *AnteTestSuite) TestCaseArgs { 191 suite.accountKeeper.SetAccount(suite.ctx, suite.accountKeeper.NewAccountWithAddress(suite.ctx, addr0)) 192 suite.accountKeeper.SetAccount(suite.ctx, suite.accountKeeper.NewAccountWithAddress(suite.ctx, addr1)) 193 suite.accountKeeper.SetAccount(suite.ctx, suite.accountKeeper.NewAccountWithAddress(suite.ctx, addr2)) 194 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) 195 196 privs, accNums, accSeqs := []cryptotypes.PrivKey{priv0, priv1, priv2}, []uint64{1, 2, 3}, []uint64{0, 0, 0} 197 198 return TestCaseArgs{ 199 accNums: accNums, 200 accSeqs: accSeqs, 201 msgs: msgs, 202 privs: privs, 203 } 204 }, 205 false, 206 true, 207 nil, 208 }, 209 } 210 211 for _, tc := range testCases { 212 t.Run(fmt.Sprintf("Case %s", tc.desc), func(t *testing.T) { 213 suite := SetupTestSuite(t, false) 214 suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() 215 args := tc.malleate(suite) 216 args.chainID = suite.ctx.ChainID() 217 args.feeAmount = testdata.NewTestFeeAmount() 218 args.gasLimit = testdata.NewTestGasLimit() 219 220 suite.RunTestCase(t, tc, args) 221 }) 222 } 223 } 224 225 // Test logic around account number checking with one signer and many signers. 226 func TestAnteHandlerAccountNumbers(t *testing.T) { 227 testCases := []TestCase{ 228 { 229 "good tx from one signer", 230 func(suite *AnteTestSuite) TestCaseArgs { 231 accs := suite.CreateTestAccounts(1) 232 msg := testdata.NewTestMsg(accs[0].acc.GetAddress()) 233 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), accs[0].acc.GetAddress(), gomock.Any(), gomock.Any()).Return(nil) 234 235 return TestCaseArgs{ 236 msgs: []sdk.Msg{msg}, 237 }.WithAccountsInfo(accs) 238 }, 239 false, 240 true, 241 nil, 242 }, 243 { 244 "new tx from wrong account number", 245 func(suite *AnteTestSuite) TestCaseArgs { 246 accs := suite.CreateTestAccounts(1) 247 msg := testdata.NewTestMsg(accs[0].acc.GetAddress()) 248 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), accs[0].acc.GetAddress(), gomock.Any(), gomock.Any()).Return(nil) 249 250 return TestCaseArgs{ 251 accNums: []uint64{1}, 252 accSeqs: []uint64{0}, 253 msgs: []sdk.Msg{msg}, 254 privs: []cryptotypes.PrivKey{accs[0].priv}, 255 } 256 }, 257 false, 258 false, 259 sdkerrors.ErrUnauthorized, 260 }, 261 { 262 "new tx with another signer and incorrect account numbers", 263 func(suite *AnteTestSuite) TestCaseArgs { 264 accs := suite.CreateTestAccounts(2) 265 msg1 := testdata.NewTestMsg(accs[0].acc.GetAddress(), accs[1].acc.GetAddress()) 266 msg2 := testdata.NewTestMsg(accs[1].acc.GetAddress(), accs[0].acc.GetAddress()) 267 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), accs[0].acc.GetAddress(), gomock.Any(), gomock.Any()).Return(nil) 268 269 return TestCaseArgs{ 270 accNums: []uint64{2, 0}, 271 accSeqs: []uint64{0, 0}, 272 msgs: []sdk.Msg{msg1, msg2}, 273 privs: []cryptotypes.PrivKey{accs[0].priv, accs[1].priv}, 274 } 275 }, 276 false, 277 false, 278 sdkerrors.ErrUnauthorized, 279 }, 280 { 281 "new tx with correct account numbers", 282 func(suite *AnteTestSuite) TestCaseArgs { 283 accs := suite.CreateTestAccounts(2) 284 msg1 := testdata.NewTestMsg(accs[0].acc.GetAddress(), accs[1].acc.GetAddress()) 285 msg2 := testdata.NewTestMsg(accs[1].acc.GetAddress(), accs[0].acc.GetAddress()) 286 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) 287 288 return TestCaseArgs{ 289 msgs: []sdk.Msg{msg1, msg2}, 290 }.WithAccountsInfo(accs) 291 }, 292 false, 293 true, 294 nil, 295 }, 296 } 297 298 for _, tc := range testCases { 299 t.Run(fmt.Sprintf("Case %s", tc.desc), func(t *testing.T) { 300 suite := SetupTestSuite(t, false) 301 args := tc.malleate(suite) 302 args.feeAmount = testdata.NewTestFeeAmount() 303 args.gasLimit = testdata.NewTestGasLimit() 304 args.chainID = suite.ctx.ChainID() 305 306 suite.RunTestCase(t, tc, args) 307 }) 308 } 309 } 310 311 // Test logic around account number checking with many signers when BlockHeight is 0. 312 func TestAnteHandlerAccountNumbersAtBlockHeightZero(t *testing.T) { 313 testCases := []TestCase{ 314 { 315 "good tx from one signer", 316 func(suite *AnteTestSuite) TestCaseArgs { 317 accs := suite.CreateTestAccounts(1) 318 msg := testdata.NewTestMsg(accs[0].acc.GetAddress()) 319 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) 320 321 return TestCaseArgs{ 322 accNums: []uint64{0}, 323 accSeqs: []uint64{0}, 324 msgs: []sdk.Msg{msg}, 325 privs: []cryptotypes.PrivKey{accs[0].priv}, 326 } 327 }, 328 false, 329 true, 330 nil, 331 }, 332 { 333 "new tx from wrong account number", 334 func(suite *AnteTestSuite) TestCaseArgs { 335 accs := suite.CreateTestAccounts(1) 336 msg := testdata.NewTestMsg(accs[0].acc.GetAddress()) 337 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) 338 339 return TestCaseArgs{ 340 accNums: []uint64{1}, // wrong account number 341 accSeqs: []uint64{0}, 342 msgs: []sdk.Msg{msg}, 343 privs: []cryptotypes.PrivKey{accs[0].priv}, 344 } 345 }, 346 false, 347 false, 348 sdkerrors.ErrUnauthorized, 349 }, 350 { 351 "new tx with another signer and incorrect account numbers", 352 func(suite *AnteTestSuite) TestCaseArgs { 353 accs := suite.CreateTestAccounts(2) 354 msg1 := testdata.NewTestMsg(accs[0].acc.GetAddress(), accs[1].acc.GetAddress()) 355 msg2 := testdata.NewTestMsg(accs[1].acc.GetAddress(), accs[0].acc.GetAddress()) 356 357 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), accs[0].acc.GetAddress(), gomock.Any(), gomock.Any()).Return(nil) 358 359 return TestCaseArgs{ 360 accNums: []uint64{1, 0}, // wrong account numbers 361 accSeqs: []uint64{0, 0}, 362 msgs: []sdk.Msg{msg1, msg2}, 363 privs: []cryptotypes.PrivKey{accs[0].priv, accs[1].priv}, 364 } 365 }, 366 false, 367 false, 368 sdkerrors.ErrUnauthorized, 369 }, 370 { 371 "new tx with another signer and correct account numbers", 372 func(suite *AnteTestSuite) TestCaseArgs { 373 accs := suite.CreateTestAccounts(2) 374 msg1 := testdata.NewTestMsg(accs[0].acc.GetAddress(), accs[1].acc.GetAddress()) 375 msg2 := testdata.NewTestMsg(accs[1].acc.GetAddress(), accs[0].acc.GetAddress()) 376 377 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), accs[0].acc.GetAddress(), gomock.Any(), gomock.Any()).Return(nil) 378 379 return TestCaseArgs{ 380 accNums: []uint64{0, 0}, // correct account numbers 381 accSeqs: []uint64{0, 0}, 382 msgs: []sdk.Msg{msg1, msg2}, 383 privs: []cryptotypes.PrivKey{accs[0].priv, accs[1].priv}, 384 } 385 }, 386 false, 387 true, 388 nil, 389 }, 390 } 391 392 for _, tc := range testCases { 393 t.Run(fmt.Sprintf("Case %s", tc.desc), func(t *testing.T) { 394 suite := SetupTestSuite(t, false) 395 suite.ctx = suite.ctx.WithBlockHeight(0) 396 suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() 397 398 args := tc.malleate(suite) 399 args.feeAmount = testdata.NewTestFeeAmount() 400 args.gasLimit = testdata.NewTestGasLimit() 401 402 suite.RunTestCase(t, tc, args) 403 }) 404 } 405 } 406 407 // Test logic around sequence checking with one signer and many signers. 408 func TestAnteHandlerSequences(t *testing.T) { 409 // Same data for every test cases 410 feeAmount := testdata.NewTestFeeAmount() 411 gasLimit := testdata.NewTestGasLimit() 412 413 testCases := []TestCase{ 414 { 415 "good tx from one signer", 416 func(suite *AnteTestSuite) TestCaseArgs { 417 accs := suite.CreateTestAccounts(1) 418 msg := testdata.NewTestMsg(accs[0].acc.GetAddress()) 419 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) 420 421 return TestCaseArgs{ 422 msgs: []sdk.Msg{msg}, 423 }.WithAccountsInfo(accs) 424 }, 425 false, 426 true, 427 nil, 428 }, 429 { 430 "test sending it again fails (replay protection)", 431 func(suite *AnteTestSuite) TestCaseArgs { 432 accs := suite.CreateTestAccounts(1) 433 msg := testdata.NewTestMsg(accs[0].acc.GetAddress()) 434 msgs := []sdk.Msg{msg} 435 privs, accNums, accSeqs := []cryptotypes.PrivKey{accs[0].priv}, []uint64{accs[0].acc.GetAccountNumber()}, []uint64{accs[0].acc.GetSequence()} 436 437 // This will be called only once given that the second tx will fail 438 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(2) 439 440 // Send the same tx before running the test case, to trigger replay protection. 441 var err error 442 suite.ctx, err = suite.DeliverMsgs(t, privs, msgs, feeAmount, gasLimit, accNums, accSeqs, suite.ctx.ChainID(), false) 443 require.NoError(t, err) 444 445 return TestCaseArgs{ 446 msgs: msgs, 447 }.WithAccountsInfo(accs) 448 }, 449 false, 450 false, 451 sdkerrors.ErrWrongSequence, 452 }, 453 { 454 "fix sequence, should pass", 455 func(suite *AnteTestSuite) TestCaseArgs { 456 accs := suite.CreateTestAccounts(1) 457 msg := testdata.NewTestMsg(accs[0].acc.GetAddress()) 458 msgs := []sdk.Msg{msg} 459 460 privs, accNums, accSeqs := []cryptotypes.PrivKey{accs[0].priv}, []uint64{accs[0].acc.GetAccountNumber()}, []uint64{accs[0].acc.GetSequence()} 461 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(2) 462 463 // Send the same tx before running the test case, then change the sequence to a valid one. 464 var err error 465 suite.ctx, err = suite.DeliverMsgs(t, privs, msgs, feeAmount, gasLimit, accNums, accSeqs, suite.ctx.ChainID(), false) 466 require.NoError(t, err) 467 468 // +1 the account sequence 469 require.NoError(t, accs[0].acc.SetSequence(accs[0].acc.GetSequence()+1)) 470 471 return TestCaseArgs{ 472 msgs: msgs, 473 }.WithAccountsInfo(accs) 474 }, 475 false, 476 true, 477 nil, 478 }, 479 { 480 "new tx with another signer and correct sequences", 481 func(suite *AnteTestSuite) TestCaseArgs { 482 accs := suite.CreateTestAccounts(3) 483 msg1 := testdata.NewTestMsg(accs[0].acc.GetAddress(), accs[1].acc.GetAddress()) 484 msg2 := testdata.NewTestMsg(accs[2].acc.GetAddress(), accs[0].acc.GetAddress()) 485 486 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) 487 488 return TestCaseArgs{ 489 msgs: []sdk.Msg{msg1, msg2}, 490 }.WithAccountsInfo(accs) 491 }, 492 false, 493 true, 494 nil, 495 }, 496 { 497 "replay fails", 498 func(suite *AnteTestSuite) TestCaseArgs { 499 accs := suite.CreateTestAccounts(3) 500 msg1 := testdata.NewTestMsg(accs[0].acc.GetAddress(), accs[1].acc.GetAddress()) 501 msg2 := testdata.NewTestMsg(accs[2].acc.GetAddress(), accs[0].acc.GetAddress()) 502 msgs := []sdk.Msg{msg1, msg2} 503 504 privs := []cryptotypes.PrivKey{accs[0].priv, accs[1].priv, accs[2].priv} 505 accNums := []uint64{accs[0].acc.GetAccountNumber(), accs[1].acc.GetAccountNumber(), accs[2].acc.GetAccountNumber()} 506 accSeqs := []uint64{accs[0].acc.GetSequence(), accs[1].acc.GetSequence(), accs[2].acc.GetSequence()} 507 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(2) 508 509 // Send the same tx before running the test case, to trigger replay protection. 510 var err error 511 suite.ctx, err = suite.DeliverMsgs(t, privs, msgs, feeAmount, gasLimit, accNums, accSeqs, suite.ctx.ChainID(), false) 512 require.NoError(t, err) 513 514 return TestCaseArgs{ 515 msgs: []sdk.Msg{msg1, msg2}, 516 }.WithAccountsInfo(accs) 517 }, 518 false, 519 false, 520 sdkerrors.ErrWrongSequence, 521 }, 522 { 523 "tx from just second signer with incorrect sequence fails", 524 func(suite *AnteTestSuite) TestCaseArgs { 525 accs := suite.CreateTestAccounts(3) 526 msg1 := testdata.NewTestMsg(accs[0].acc.GetAddress(), accs[1].acc.GetAddress()) 527 msg2 := testdata.NewTestMsg(accs[2].acc.GetAddress(), accs[0].acc.GetAddress()) 528 msgs := []sdk.Msg{msg1, msg2} 529 530 privs := []cryptotypes.PrivKey{accs[0].priv, accs[1].priv, accs[2].priv} 531 accNums := []uint64{accs[0].acc.GetAccountNumber(), accs[1].acc.GetAccountNumber(), accs[2].acc.GetAccountNumber()} 532 accSeqs := []uint64{accs[0].acc.GetSequence(), accs[1].acc.GetSequence(), accs[2].acc.GetSequence()} 533 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(2) 534 535 // Send the same tx before running the test case, to trigger replay protection. 536 var err error 537 suite.ctx, err = suite.DeliverMsgs(t, privs, msgs, feeAmount, gasLimit, accNums, accSeqs, suite.ctx.ChainID(), false) 538 require.NoError(t, err) 539 540 // Send a message using the second signer, this will fail given that the second signer already sent a TX, 541 // thus the sequence (0) is incorrect. 542 msg1 = testdata.NewTestMsg(accs[1].acc.GetAddress()) 543 msgs = []sdk.Msg{msg1} 544 privs, accNums, accSeqs = []cryptotypes.PrivKey{accs[1].priv}, []uint64{accs[1].acc.GetAccountNumber()}, []uint64{0} 545 546 return TestCaseArgs{ 547 accNums: accNums, 548 accSeqs: accSeqs, 549 msgs: msgs, 550 privs: privs, 551 } 552 }, 553 false, 554 false, 555 sdkerrors.ErrWrongSequence, 556 }, 557 { 558 "fix the sequence and it passes", 559 func(suite *AnteTestSuite) TestCaseArgs { 560 accs := suite.CreateTestAccounts(3) 561 msg1 := testdata.NewTestMsg(accs[0].acc.GetAddress(), accs[1].acc.GetAddress()) 562 msg2 := testdata.NewTestMsg(accs[2].acc.GetAddress(), accs[0].acc.GetAddress()) 563 msgs := []sdk.Msg{msg1, msg2} 564 565 privs := []cryptotypes.PrivKey{accs[0].priv, accs[1].priv, accs[2].priv} 566 accNums := []uint64{accs[0].acc.GetAccountNumber(), accs[1].acc.GetAccountNumber(), accs[2].acc.GetAccountNumber()} 567 accSeqs := []uint64{accs[0].acc.GetSequence(), accs[1].acc.GetSequence(), accs[2].acc.GetSequence()} 568 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(2) 569 570 // Send the same tx before running the test case, to trigger replay protection. 571 var err error 572 suite.ctx, err = suite.DeliverMsgs(t, privs, msgs, feeAmount, gasLimit, accNums, accSeqs, suite.ctx.ChainID(), false) 573 require.NoError(t, err) 574 575 // Send a message using the second signer, this will now pass given that the second signer already sent a TX 576 // and the sequence was fixed (1). 577 msg1 = testdata.NewTestMsg(accs[1].acc.GetAddress()) 578 msgs = []sdk.Msg{msg1} 579 privs, accNums, accSeqs = []cryptotypes.PrivKey{accs[1].priv}, []uint64{accs[1].acc.GetAccountNumber()}, []uint64{accs[1].acc.GetSequence() + 1} 580 581 return TestCaseArgs{ 582 accNums: accNums, 583 accSeqs: accSeqs, 584 msgs: msgs, 585 privs: privs, 586 } 587 }, 588 false, 589 true, 590 nil, 591 }, 592 } 593 594 for _, tc := range testCases { 595 t.Run(fmt.Sprintf("Case %s", tc.desc), func(t *testing.T) { 596 suite := SetupTestSuite(t, false) 597 suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() 598 args := tc.malleate(suite) 599 args.feeAmount = feeAmount 600 args.gasLimit = gasLimit 601 602 suite.RunTestCase(t, tc, args) 603 }) 604 } 605 } 606 607 // Test logic around fee deduction. 608 func TestAnteHandlerFees(t *testing.T) { 609 // Same data for every test cases 610 feeAmount := testdata.NewTestFeeAmount() 611 gasLimit := testdata.NewTestGasLimit() 612 613 testCases := []TestCase{ 614 { 615 "signer has no funds", 616 func(suite *AnteTestSuite) TestCaseArgs { 617 accs := suite.CreateTestAccounts(1) 618 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), accs[0].acc.GetAddress(), gomock.Any(), feeAmount).Return(sdkerrors.ErrInsufficientFunds) 619 620 return TestCaseArgs{ 621 msgs: []sdk.Msg{testdata.NewTestMsg(accs[0].acc.GetAddress())}, 622 }.WithAccountsInfo(accs) 623 }, 624 false, 625 false, 626 sdkerrors.ErrInsufficientFunds, 627 }, 628 { 629 "signer has enough funds, should pass", 630 func(suite *AnteTestSuite) TestCaseArgs { 631 accs := suite.CreateTestAccounts(1) 632 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), accs[0].acc.GetAddress(), gomock.Any(), feeAmount).Return(nil) 633 634 return TestCaseArgs{ 635 msgs: []sdk.Msg{testdata.NewTestMsg(accs[0].acc.GetAddress())}, 636 }.WithAccountsInfo(accs) 637 }, 638 false, 639 true, 640 nil, 641 }, 642 } 643 644 for _, tc := range testCases { 645 t.Run(fmt.Sprintf("Case %s", tc.desc), func(t *testing.T) { 646 suite := SetupTestSuite(t, false) 647 suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() 648 args := tc.malleate(suite) 649 args.feeAmount = feeAmount 650 args.gasLimit = gasLimit 651 652 suite.RunTestCase(t, tc, args) 653 }) 654 } 655 } 656 657 // Test logic around memo gas consumption. 658 func TestAnteHandlerMemoGas(t *testing.T) { 659 testCases := []TestCase{ 660 { 661 "tx does not have enough gas", 662 func(suite *AnteTestSuite) TestCaseArgs { 663 accs := suite.CreateTestAccounts(1) 664 return TestCaseArgs{ 665 feeAmount: sdk.NewCoins(sdk.NewInt64Coin("atom", 0)), 666 gasLimit: 0, 667 msgs: []sdk.Msg{testdata.NewTestMsg(accs[0].acc.GetAddress())}, 668 }.WithAccountsInfo(accs) 669 }, 670 false, 671 false, 672 sdkerrors.ErrOutOfGas, 673 }, 674 { 675 "tx with memo doesn't have enough gas", 676 func(suite *AnteTestSuite) TestCaseArgs { 677 accs := suite.CreateTestAccounts(1) 678 suite.txBuilder.SetMemo("abcininasidniandsinasindiansdiansdinaisndiasndiadninsd") 679 680 return TestCaseArgs{ 681 feeAmount: sdk.NewCoins(sdk.NewInt64Coin("atom", 0)), 682 gasLimit: 801, 683 msgs: []sdk.Msg{testdata.NewTestMsg(accs[0].acc.GetAddress())}, 684 }.WithAccountsInfo(accs) 685 }, 686 false, 687 false, 688 sdkerrors.ErrOutOfGas, 689 }, 690 { 691 "memo too large", 692 func(suite *AnteTestSuite) TestCaseArgs { 693 accs := suite.CreateTestAccounts(1) 694 suite.txBuilder.SetMemo(strings.Repeat("01234567890", 500)) 695 696 return TestCaseArgs{ 697 feeAmount: sdk.NewCoins(sdk.NewInt64Coin("atom", 0)), 698 gasLimit: 50000, 699 msgs: []sdk.Msg{testdata.NewTestMsg(accs[0].acc.GetAddress())}, 700 }.WithAccountsInfo(accs) 701 }, 702 false, 703 false, 704 sdkerrors.ErrMemoTooLarge, 705 }, 706 { 707 "tx with memo has enough gas", 708 func(suite *AnteTestSuite) TestCaseArgs { 709 accs := suite.CreateTestAccounts(1) 710 suite.txBuilder.SetMemo(strings.Repeat("0123456789", 10)) 711 return TestCaseArgs{ 712 feeAmount: sdk.NewCoins(sdk.NewInt64Coin("atom", 0)), 713 gasLimit: 60000, 714 msgs: []sdk.Msg{testdata.NewTestMsg(accs[0].acc.GetAddress())}, 715 }.WithAccountsInfo(accs) 716 }, 717 false, 718 true, 719 nil, 720 }, 721 } 722 723 for _, tc := range testCases { 724 t.Run(fmt.Sprintf("Case %s", tc.desc), func(t *testing.T) { 725 suite := SetupTestSuite(t, false) 726 suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() 727 args := tc.malleate(suite) 728 729 suite.RunTestCase(t, tc, args) 730 }) 731 } 732 } 733 734 func TestAnteHandlerMultiSigner(t *testing.T) { 735 feeAmount := testdata.NewTestFeeAmount() 736 gasLimit := testdata.NewTestGasLimit() 737 738 testCases := []TestCase{ 739 { 740 "signers in order", 741 func(suite *AnteTestSuite) TestCaseArgs { 742 accs := suite.CreateTestAccounts(3) 743 msg1 := testdata.NewTestMsg(accs[0].acc.GetAddress(), accs[1].acc.GetAddress()) 744 msg2 := testdata.NewTestMsg(accs[2].acc.GetAddress(), accs[0].acc.GetAddress()) 745 msg3 := testdata.NewTestMsg(accs[1].acc.GetAddress(), accs[2].acc.GetAddress()) 746 suite.txBuilder.SetMemo("Check signers are in expected order and different account numbers works") 747 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) 748 749 return TestCaseArgs{ 750 msgs: []sdk.Msg{msg1, msg2, msg3}, 751 }.WithAccountsInfo(accs) 752 }, 753 false, 754 true, 755 nil, 756 }, 757 { 758 "change sequence numbers (only accounts 0 and 1 sign)", 759 func(suite *AnteTestSuite) TestCaseArgs { 760 accs := suite.CreateTestAccounts(3) 761 msg1 := testdata.NewTestMsg(accs[0].acc.GetAddress(), accs[1].acc.GetAddress()) 762 msg2 := testdata.NewTestMsg(accs[2].acc.GetAddress(), accs[0].acc.GetAddress()) 763 msg3 := testdata.NewTestMsg(accs[1].acc.GetAddress(), accs[2].acc.GetAddress()) 764 msgs := []sdk.Msg{msg1, msg2, msg3} 765 766 privs := []cryptotypes.PrivKey{accs[0].priv, accs[1].priv, accs[2].priv} 767 accNums := []uint64{accs[0].acc.GetAccountNumber(), accs[1].acc.GetAccountNumber(), accs[2].acc.GetAccountNumber()} 768 accSeqs := []uint64{accs[0].acc.GetSequence(), accs[1].acc.GetSequence(), accs[2].acc.GetSequence()} 769 770 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(2) 771 772 var err error 773 suite.ctx, err = suite.DeliverMsgs(t, privs, msgs, feeAmount, gasLimit, accNums, accSeqs, suite.ctx.ChainID(), false) 774 require.NoError(t, err) 775 776 msgs = []sdk.Msg{msg1} 777 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} 778 779 return TestCaseArgs{ 780 accNums: accNums, 781 accSeqs: accSeqs, 782 msgs: msgs, 783 privs: privs, 784 } 785 }, 786 false, 787 true, 788 nil, 789 }, 790 { 791 "change sequence numbers (only accounts 1 and 2 sign)", 792 func(suite *AnteTestSuite) TestCaseArgs { 793 accs := suite.CreateTestAccounts(3) 794 msg1 := testdata.NewTestMsg(accs[0].acc.GetAddress(), accs[1].acc.GetAddress()) 795 msg2 := testdata.NewTestMsg(accs[2].acc.GetAddress(), accs[0].acc.GetAddress()) 796 msg3 := testdata.NewTestMsg(accs[1].acc.GetAddress(), accs[2].acc.GetAddress()) 797 msgs := []sdk.Msg{msg1, msg2, msg3} 798 799 privs := []cryptotypes.PrivKey{accs[0].priv, accs[1].priv, accs[2].priv} 800 accNums := []uint64{accs[0].acc.GetAccountNumber(), accs[1].acc.GetAccountNumber(), accs[2].acc.GetAccountNumber()} 801 accSeqs := []uint64{accs[0].acc.GetSequence(), accs[1].acc.GetSequence(), accs[2].acc.GetSequence()} 802 803 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(2) 804 var err error 805 suite.ctx, err = suite.DeliverMsgs(t, privs, msgs, feeAmount, gasLimit, accNums, accSeqs, suite.ctx.ChainID(), false) 806 require.NoError(t, err) 807 808 msgs = []sdk.Msg{msg3} 809 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} 810 811 return TestCaseArgs{ 812 accNums: accNums, 813 accSeqs: accSeqs, 814 msgs: msgs, 815 privs: privs, 816 } 817 }, 818 false, 819 true, 820 nil, 821 }, 822 { 823 "everyone signs again", 824 func(suite *AnteTestSuite) TestCaseArgs { 825 accs := suite.CreateTestAccounts(3) 826 msg1 := testdata.NewTestMsg(accs[0].acc.GetAddress(), accs[1].acc.GetAddress()) 827 msg2 := testdata.NewTestMsg(accs[2].acc.GetAddress(), accs[0].acc.GetAddress()) 828 msg3 := testdata.NewTestMsg(accs[1].acc.GetAddress(), accs[2].acc.GetAddress()) 829 msgs := []sdk.Msg{msg1, msg2, msg3} 830 831 privs := []cryptotypes.PrivKey{accs[0].priv, accs[1].priv, accs[2].priv} 832 accNums := []uint64{accs[0].acc.GetAccountNumber(), accs[1].acc.GetAccountNumber(), accs[2].acc.GetAccountNumber()} 833 accSeqs := []uint64{accs[0].acc.GetSequence(), accs[1].acc.GetSequence(), accs[2].acc.GetSequence()} 834 835 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(2) 836 var err error 837 suite.ctx, err = suite.DeliverMsgs(t, privs, msgs, feeAmount, gasLimit, accNums, accSeqs, suite.ctx.ChainID(), false) 838 require.NoError(t, err) 839 840 for _, acc := range accs { 841 require.NoError(t, acc.acc.SetSequence(acc.acc.GetSequence()+1)) 842 } 843 844 return TestCaseArgs{ 845 msgs: msgs, 846 }.WithAccountsInfo(accs) 847 }, 848 false, 849 true, 850 nil, 851 }, 852 } 853 854 for _, tc := range testCases { 855 t.Run(fmt.Sprintf("Case %s", tc.desc), func(t *testing.T) { 856 suite := SetupTestSuite(t, false) 857 suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() 858 args := tc.malleate(suite) 859 args.feeAmount = feeAmount 860 args.gasLimit = gasLimit 861 args.chainID = suite.ctx.ChainID() 862 863 suite.RunTestCase(t, tc, args) 864 }) 865 } 866 } 867 868 func TestAnteHandlerBadSignBytes(t *testing.T) { 869 feeAmount := testdata.NewTestFeeAmount() 870 gasLimit := testdata.NewTestGasLimit() 871 872 testCases := []TestCase{ 873 { 874 "test good tx and signBytes", 875 func(suite *AnteTestSuite) TestCaseArgs { 876 accs := suite.CreateTestAccounts(1) 877 msg0 := testdata.NewTestMsg(accs[0].acc.GetAddress()) 878 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) 879 880 return TestCaseArgs{ 881 chainID: suite.ctx.ChainID(), 882 feeAmount: feeAmount, 883 gasLimit: gasLimit, 884 msgs: []sdk.Msg{msg0}, 885 }.WithAccountsInfo(accs) 886 }, 887 false, 888 true, 889 nil, 890 }, 891 { 892 "test wrong chainID", 893 func(suite *AnteTestSuite) TestCaseArgs { 894 accs := suite.CreateTestAccounts(1) 895 msg0 := testdata.NewTestMsg(accs[0].acc.GetAddress()) 896 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) 897 898 return TestCaseArgs{ 899 chainID: "wrong-chain-id", 900 feeAmount: feeAmount, 901 gasLimit: gasLimit, 902 msgs: []sdk.Msg{msg0}, 903 }.WithAccountsInfo(accs) 904 }, 905 false, 906 false, 907 sdkerrors.ErrUnauthorized, 908 }, 909 { 910 "test wrong accSeqs", 911 func(suite *AnteTestSuite) TestCaseArgs { 912 accs := suite.CreateTestAccounts(1) 913 msg0 := testdata.NewTestMsg(accs[0].acc.GetAddress()) 914 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) 915 require.NoError(t, accs[0].acc.SetSequence(2)) // wrong accSeq 916 917 return TestCaseArgs{ 918 chainID: suite.ctx.ChainID(), 919 feeAmount: feeAmount, 920 gasLimit: gasLimit, 921 msgs: []sdk.Msg{msg0}, 922 }.WithAccountsInfo(accs) 923 }, 924 false, 925 false, 926 sdkerrors.ErrWrongSequence, 927 }, 928 { 929 "test wrong accNums", 930 func(suite *AnteTestSuite) TestCaseArgs { 931 accs := suite.CreateTestAccounts(1) 932 msg0 := testdata.NewTestMsg(accs[0].acc.GetAddress()) 933 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) 934 require.NoError(t, accs[0].acc.SetAccountNumber(2)) // wrong accNum 935 936 return TestCaseArgs{ 937 chainID: suite.ctx.ChainID(), 938 feeAmount: feeAmount, 939 gasLimit: gasLimit, 940 msgs: []sdk.Msg{msg0}, 941 }.WithAccountsInfo(accs) 942 }, 943 false, 944 false, 945 sdkerrors.ErrUnauthorized, 946 }, 947 { 948 "test wrong msg", 949 func(suite *AnteTestSuite) TestCaseArgs { 950 accs := suite.CreateTestAccounts(2) 951 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) 952 953 return TestCaseArgs{ 954 chainID: suite.ctx.ChainID(), 955 feeAmount: feeAmount, 956 gasLimit: gasLimit, 957 msgs: []sdk.Msg{testdata.NewTestMsg(accs[1].acc.GetAddress())}, // wrong account in the msg 958 }.WithAccountsInfo(accs[0:1]) 959 }, 960 false, 961 false, 962 sdkerrors.ErrInvalidPubKey, 963 }, 964 { 965 "test wrong signer if public key exist", 966 func(suite *AnteTestSuite) TestCaseArgs { 967 accs := suite.CreateTestAccounts(2) 968 msg0 := testdata.NewTestMsg(accs[0].acc.GetAddress()) 969 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) 970 971 return TestCaseArgs{ 972 chainID: suite.ctx.ChainID(), 973 accNums: []uint64{accs[0].acc.GetAccountNumber()}, 974 accSeqs: []uint64{accs[0].acc.GetSequence()}, 975 feeAmount: feeAmount, 976 gasLimit: gasLimit, 977 msgs: []sdk.Msg{msg0}, 978 privs: []cryptotypes.PrivKey{accs[1].priv}, 979 } 980 }, 981 false, 982 false, 983 sdkerrors.ErrInvalidPubKey, 984 }, 985 { 986 "test wrong signer if public doesn't exist", 987 func(suite *AnteTestSuite) TestCaseArgs { 988 accs := suite.CreateTestAccounts(2) 989 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) 990 991 return TestCaseArgs{ 992 chainID: suite.ctx.ChainID(), 993 accNums: []uint64{accs[1].acc.GetAccountNumber()}, 994 accSeqs: []uint64{accs[1].acc.GetSequence()}, 995 feeAmount: feeAmount, 996 gasLimit: gasLimit, 997 msgs: []sdk.Msg{testdata.NewTestMsg(accs[1].acc.GetAddress())}, 998 privs: []cryptotypes.PrivKey{accs[0].priv}, 999 } 1000 }, 1001 false, 1002 false, 1003 sdkerrors.ErrInvalidPubKey, 1004 }, 1005 } 1006 1007 for _, tc := range testCases { 1008 t.Run(fmt.Sprintf("Case %s", tc.desc), func(t *testing.T) { 1009 suite := SetupTestSuite(t, false) 1010 suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() 1011 args := tc.malleate(suite) 1012 1013 suite.RunTestCase(t, tc, args) 1014 }) 1015 } 1016 } 1017 1018 func TestAnteHandlerSetPubKey(t *testing.T) { 1019 feeAmount := testdata.NewTestFeeAmount() 1020 gasLimit := testdata.NewTestGasLimit() 1021 1022 testCases := []TestCase{ 1023 { 1024 "test good tx", 1025 func(suite *AnteTestSuite) TestCaseArgs { 1026 accs := suite.CreateTestAccounts(1) 1027 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) 1028 1029 return TestCaseArgs{ 1030 msgs: []sdk.Msg{testdata.NewTestMsg(accs[0].acc.GetAddress())}, 1031 }.WithAccountsInfo(accs) 1032 }, 1033 false, 1034 true, 1035 nil, 1036 }, 1037 { 1038 "make sure public key has been set (tx itself should fail because of replay protection)", 1039 func(suite *AnteTestSuite) TestCaseArgs { 1040 accs := suite.CreateTestAccounts(1) 1041 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(2) 1042 1043 privs, accNums, accSeqs := []cryptotypes.PrivKey{accs[0].priv}, []uint64{accs[0].acc.GetAccountNumber()}, []uint64{accs[0].acc.GetSequence()} 1044 msgs := []sdk.Msg{testdata.NewTestMsg(accs[0].acc.GetAddress())} 1045 var err error 1046 suite.ctx, err = suite.DeliverMsgs(t, privs, msgs, feeAmount, gasLimit, accNums, accSeqs, suite.ctx.ChainID(), false) 1047 require.NoError(t, err) 1048 1049 // Make sure public key has been set from previous test. 1050 acc0 := suite.accountKeeper.GetAccount(suite.ctx, accs[0].acc.GetAddress()) 1051 require.Equal(t, acc0.GetPubKey(), accs[0].priv.PubKey()) 1052 1053 return TestCaseArgs{ 1054 accNums: accNums, 1055 accSeqs: accSeqs, 1056 msgs: msgs, 1057 privs: privs, 1058 } 1059 }, 1060 false, 1061 false, 1062 sdkerrors.ErrWrongSequence, 1063 }, 1064 { 1065 "test public key not found", 1066 func(suite *AnteTestSuite) TestCaseArgs { 1067 accs := suite.CreateTestAccounts(2) 1068 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) 1069 return TestCaseArgs{ 1070 msgs: []sdk.Msg{testdata.NewTestMsg(accs[1].acc.GetAddress())}, 1071 }.WithAccountsInfo(accs[0:1]) 1072 }, 1073 false, 1074 false, 1075 sdkerrors.ErrInvalidPubKey, 1076 }, 1077 { 1078 "make sure public key is not set, when tx has no pubkey or signature", 1079 func(suite *AnteTestSuite) TestCaseArgs { 1080 accs := suite.CreateTestAccounts(2) 1081 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) 1082 1083 // Make sure public key has not been set from previous test. 1084 acc1 := suite.accountKeeper.GetAccount(suite.ctx, accs[1].acc.GetAddress()) 1085 require.Nil(t, acc1.GetPubKey()) 1086 1087 privs, accNums, accSeqs := []cryptotypes.PrivKey{accs[1].priv}, []uint64{accs[1].acc.GetAccountNumber()}, []uint64{accs[1].acc.GetSequence()} 1088 msgs := []sdk.Msg{testdata.NewTestMsg(accs[1].acc.GetAddress())} 1089 suite.txBuilder.SetMsgs(msgs...) 1090 suite.txBuilder.SetFeeAmount(feeAmount) 1091 suite.txBuilder.SetGasLimit(gasLimit) 1092 1093 // Manually create tx, and remove signature. 1094 tx, err := suite.CreateTestTx(suite.ctx, privs, accNums, accSeqs, suite.ctx.ChainID(), signing.SignMode_SIGN_MODE_DIRECT) 1095 require.NoError(t, err) 1096 txBuilder, err := suite.clientCtx.TxConfig.WrapTxBuilder(tx) 1097 require.NoError(t, err) 1098 require.NoError(t, txBuilder.SetSignatures()) 1099 1100 // Run anteHandler manually, expect ErrNoSignatures. 1101 _, err = suite.anteHandler(suite.ctx, txBuilder.GetTx(), false) 1102 require.Error(t, err) 1103 require.True(t, errors.Is(err, sdkerrors.ErrNoSignatures)) 1104 1105 // Make sure public key has not been set. 1106 acc1 = suite.accountKeeper.GetAccount(suite.ctx, accs[1].acc.GetAddress()) 1107 require.Nil(t, acc1.GetPubKey()) 1108 1109 // Set incorrect accSeq, to generate incorrect signature. 1110 privs, accNums, accSeqs = []cryptotypes.PrivKey{accs[1].priv}, []uint64{accs[1].acc.GetAccountNumber()}, []uint64{1} 1111 1112 return TestCaseArgs{ 1113 accNums: accNums, 1114 accSeqs: accSeqs, 1115 msgs: msgs, 1116 privs: privs, 1117 } 1118 }, 1119 false, 1120 false, 1121 sdkerrors.ErrWrongSequence, 1122 }, 1123 { 1124 "make sure previous public key has been set after wrong signature", 1125 func(suite *AnteTestSuite) TestCaseArgs { 1126 accs := suite.CreateTestAccounts(2) 1127 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) 1128 1129 // Make sure public key has not been set from previous test. 1130 acc1 := suite.accountKeeper.GetAccount(suite.ctx, accs[1].acc.GetAddress()) 1131 require.Nil(t, acc1.GetPubKey()) 1132 1133 privs, accNums, accSeqs := []cryptotypes.PrivKey{accs[1].priv}, []uint64{accs[1].acc.GetAccountNumber()}, []uint64{accs[1].acc.GetSequence()} 1134 msgs := []sdk.Msg{testdata.NewTestMsg(accs[1].acc.GetAddress())} 1135 suite.txBuilder.SetMsgs(msgs...) 1136 suite.txBuilder.SetFeeAmount(feeAmount) 1137 suite.txBuilder.SetGasLimit(gasLimit) 1138 1139 // Manually create tx, and remove signature. 1140 tx, err := suite.CreateTestTx(suite.ctx, privs, accNums, accSeqs, suite.ctx.ChainID(), signing.SignMode_SIGN_MODE_DIRECT) 1141 require.NoError(t, err) 1142 txBuilder, err := suite.clientCtx.TxConfig.WrapTxBuilder(tx) 1143 require.NoError(t, err) 1144 require.NoError(t, txBuilder.SetSignatures()) 1145 1146 // Run anteHandler manually, expect ErrNoSignatures. 1147 _, err = suite.anteHandler(suite.ctx, txBuilder.GetTx(), false) 1148 require.Error(t, err) 1149 require.True(t, errors.Is(err, sdkerrors.ErrNoSignatures)) 1150 1151 // Make sure public key has not been set. 1152 acc1 = suite.accountKeeper.GetAccount(suite.ctx, accs[1].acc.GetAddress()) 1153 require.Nil(t, acc1.GetPubKey()) 1154 1155 // Set incorrect accSeq, to generate incorrect signature. 1156 privs, accNums, accSeqs = []cryptotypes.PrivKey{accs[1].priv}, []uint64{accs[1].acc.GetAccountNumber()}, []uint64{1} 1157 1158 suite.ctx, err = suite.DeliverMsgs(t, privs, msgs, feeAmount, gasLimit, accNums, accSeqs, suite.ctx.ChainID(), false) 1159 require.Error(t, err) 1160 1161 // Make sure public key has been set, as SetPubKeyDecorator 1162 // is called before all signature verification decorators. 1163 acc1 = suite.accountKeeper.GetAccount(suite.ctx, accs[1].acc.GetAddress()) 1164 require.Equal(t, acc1.GetPubKey(), accs[1].priv.PubKey()) 1165 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) 1166 1167 return TestCaseArgs{ 1168 accNums: accNums, 1169 accSeqs: accSeqs, 1170 msgs: msgs, 1171 privs: privs, 1172 } 1173 }, 1174 false, 1175 false, 1176 sdkerrors.ErrWrongSequence, 1177 }, 1178 } 1179 1180 for _, tc := range testCases { 1181 t.Run(fmt.Sprintf("Case %s", tc.desc), func(t *testing.T) { 1182 suite := SetupTestSuite(t, false) 1183 suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() 1184 args := tc.malleate(suite) 1185 args.chainID = suite.ctx.ChainID() 1186 args.feeAmount = feeAmount 1187 args.gasLimit = gasLimit 1188 1189 suite.RunTestCase(t, tc, args) 1190 }) 1191 } 1192 } 1193 1194 func generatePubKeysAndSignatures(n int, msg []byte, _ bool) (pubkeys []cryptotypes.PubKey, signatures [][]byte) { 1195 pubkeys = make([]cryptotypes.PubKey, n) 1196 signatures = make([][]byte, n) 1197 for i := 0; i < n; i++ { 1198 var privkey cryptotypes.PrivKey = secp256k1.GenPrivKey() 1199 1200 // TODO: also generate ed25519 keys as below when ed25519 keys are 1201 // actually supported, https://github.com/cosmos/cosmos-sdk/issues/4789 1202 // for now this fails: 1203 // if rand.Int63()%2 == 0 { 1204 // privkey = ed25519.GenPrivKey() 1205 // } else { 1206 // privkey = secp256k1.GenPrivKey() 1207 //} 1208 1209 pubkeys[i] = privkey.PubKey() 1210 signatures[i], _ = privkey.Sign(msg) 1211 } 1212 return 1213 } 1214 1215 func expectedGasCostByKeys(pubkeys []cryptotypes.PubKey) uint64 { 1216 cost := uint64(0) 1217 for _, pubkey := range pubkeys { 1218 pubkeyType := strings.ToLower(fmt.Sprintf("%T", pubkey)) 1219 switch { 1220 case strings.Contains(pubkeyType, "ed25519"): 1221 cost += authtypes.DefaultParams().SigVerifyCostED25519 1222 case strings.Contains(pubkeyType, "secp256k1"): 1223 cost += authtypes.DefaultParams().SigVerifyCostSecp256k1 1224 default: 1225 panic("unexpected key type") 1226 } 1227 } 1228 return cost 1229 } 1230 1231 func TestCountSubkeys(t *testing.T) { 1232 genPubKeys := func(n int) []cryptotypes.PubKey { 1233 var ret []cryptotypes.PubKey 1234 for i := 0; i < n; i++ { 1235 ret = append(ret, secp256k1.GenPrivKey().PubKey()) 1236 } 1237 return ret 1238 } 1239 singleKey := secp256k1.GenPrivKey().PubKey() 1240 singleLevelMultiKey := kmultisig.NewLegacyAminoPubKey(4, genPubKeys(5)) 1241 multiLevelSubKey1 := kmultisig.NewLegacyAminoPubKey(4, genPubKeys(5)) 1242 multiLevelSubKey2 := kmultisig.NewLegacyAminoPubKey(4, genPubKeys(5)) 1243 multiLevelMultiKey := kmultisig.NewLegacyAminoPubKey(2, []cryptotypes.PubKey{ 1244 multiLevelSubKey1, multiLevelSubKey2, secp256k1.GenPrivKey().PubKey(), 1245 }) 1246 type args struct { 1247 pub cryptotypes.PubKey 1248 } 1249 testCases := []struct { 1250 name string 1251 args args 1252 want int 1253 }{ 1254 {"single key", args{singleKey}, 1}, 1255 {"single level multikey", args{singleLevelMultiKey}, 5}, 1256 {"multi level multikey", args{multiLevelMultiKey}, 11}, 1257 {"nil key", args{nil}, 0}, 1258 } 1259 for _, tc := range testCases { 1260 t.Run(tc.name, func(T *testing.T) { 1261 require.Equal(t, tc.want, ante.CountSubKeys(tc.args.pub)) 1262 }) 1263 } 1264 } 1265 1266 func TestAnteHandlerSigLimitExceeded(t *testing.T) { 1267 testCases := []TestCase{ 1268 { 1269 "test rejection logic", 1270 func(suite *AnteTestSuite) TestCaseArgs { 1271 accs := suite.CreateTestAccounts(8) 1272 var ( 1273 addrs []sdk.AccAddress 1274 privs []cryptotypes.PrivKey 1275 ) 1276 for i := 0; i < 8; i++ { 1277 addrs = append(addrs, accs[i].acc.GetAddress()) 1278 privs = append(privs, accs[i].priv) 1279 } 1280 1281 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) 1282 1283 return TestCaseArgs{ 1284 accNums: []uint64{0, 1, 2, 3, 4, 5, 6, 7}, 1285 accSeqs: []uint64{0, 0, 0, 0, 0, 0, 0, 0}, 1286 msgs: []sdk.Msg{testdata.NewTestMsg(addrs...)}, 1287 privs: privs, 1288 } 1289 }, 1290 false, 1291 false, 1292 sdkerrors.ErrTooManySignatures, 1293 }, 1294 } 1295 1296 for _, tc := range testCases { 1297 t.Run(fmt.Sprintf("Case %s", tc.desc), func(t *testing.T) { 1298 suite := SetupTestSuite(t, false) 1299 suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() 1300 args := tc.malleate(suite) 1301 args.chainID = suite.ctx.ChainID() 1302 args.feeAmount = testdata.NewTestFeeAmount() 1303 args.gasLimit = testdata.NewTestGasLimit() 1304 1305 suite.RunTestCase(t, tc, args) 1306 }) 1307 } 1308 } 1309 1310 // Test custom SignatureVerificationGasConsumer 1311 func TestCustomSignatureVerificationGasConsumer(t *testing.T) { 1312 testCases := []TestCase{ 1313 { 1314 "verify that an secp256k1 account gets rejected", 1315 func(suite *AnteTestSuite) TestCaseArgs { 1316 // setup an ante handler that only accepts PubKeyEd25519 1317 anteHandler, err := ante.NewAnteHandler( 1318 ante.HandlerOptions{ 1319 AccountKeeper: suite.accountKeeper, 1320 BankKeeper: suite.bankKeeper, 1321 FeegrantKeeper: suite.feeGrantKeeper, 1322 SignModeHandler: suite.clientCtx.TxConfig.SignModeHandler(), 1323 SigGasConsumer: func(meter storetypes.GasMeter, sig signing.SignatureV2, params authtypes.Params) error { 1324 switch pubkey := sig.PubKey.(type) { 1325 case *ed25519.PubKey: 1326 meter.ConsumeGas(params.SigVerifyCostED25519, "ante verify: ed25519") 1327 return nil 1328 default: 1329 return errorsmod.Wrapf(sdkerrors.ErrInvalidPubKey, "unrecognized public key type: %T", pubkey) 1330 } 1331 }, 1332 }, 1333 ) 1334 require.NoError(t, err) 1335 suite.anteHandler = anteHandler 1336 1337 accs := suite.CreateTestAccounts(1) 1338 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) 1339 1340 return TestCaseArgs{ 1341 accNums: []uint64{0}, 1342 accSeqs: []uint64{0}, 1343 msgs: []sdk.Msg{testdata.NewTestMsg(accs[0].acc.GetAddress())}, 1344 privs: []cryptotypes.PrivKey{accs[0].priv}, 1345 } 1346 }, 1347 false, 1348 false, 1349 sdkerrors.ErrInvalidPubKey, 1350 }, 1351 } 1352 1353 for _, tc := range testCases { 1354 t.Run(fmt.Sprintf("Case %s", tc.desc), func(t *testing.T) { 1355 suite := SetupTestSuite(t, false) 1356 suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() 1357 args := tc.malleate(suite) 1358 args.chainID = suite.ctx.ChainID() 1359 args.feeAmount = testdata.NewTestFeeAmount() 1360 args.gasLimit = testdata.NewTestGasLimit() 1361 1362 suite.RunTestCase(t, tc, args) 1363 }) 1364 } 1365 } 1366 1367 func TestAnteHandlerReCheck(t *testing.T) { 1368 suite := SetupTestSuite(t, false) 1369 // Set recheck=true 1370 suite.ctx = suite.ctx.WithIsReCheckTx(true) 1371 suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() 1372 1373 // Same data for every test case 1374 accs := suite.CreateTestAccounts(1) 1375 1376 feeAmount := testdata.NewTestFeeAmount() 1377 gasLimit := testdata.NewTestGasLimit() 1378 suite.txBuilder.SetFeeAmount(feeAmount) 1379 suite.txBuilder.SetGasLimit(gasLimit) 1380 1381 msg := testdata.NewTestMsg(accs[0].acc.GetAddress()) 1382 msgs := []sdk.Msg{msg} 1383 require.NoError(t, suite.txBuilder.SetMsgs(msgs...)) 1384 1385 suite.txBuilder.SetMemo("thisisatestmemo") 1386 1387 // test that operations skipped on recheck do not run 1388 privs, accNums, accSeqs := []cryptotypes.PrivKey{accs[0].priv}, []uint64{0}, []uint64{0} 1389 tx, err := suite.CreateTestTx(suite.ctx, privs, accNums, accSeqs, suite.ctx.ChainID(), signing.SignMode_SIGN_MODE_DIRECT) 1390 require.NoError(t, err) 1391 1392 // make signature array empty which would normally cause ValidateBasicDecorator and SigVerificationDecorator fail 1393 // since these decorators don't run on recheck, the tx should pass the antehandler 1394 txBuilder, err := suite.clientCtx.TxConfig.WrapTxBuilder(tx) 1395 require.NoError(t, err) 1396 1397 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(2) 1398 _, err = suite.anteHandler(suite.ctx, txBuilder.GetTx(), false) 1399 require.Nil(t, err, "AnteHandler errored on recheck unexpectedly: %v", err) 1400 1401 tx, err = suite.CreateTestTx(suite.ctx, privs, accNums, accSeqs, suite.ctx.ChainID(), signing.SignMode_SIGN_MODE_DIRECT) 1402 require.NoError(t, err) 1403 txBytes, err := json.Marshal(tx) 1404 require.Nil(t, err, "Error marshaling tx: %v", err) 1405 suite.ctx = suite.ctx.WithTxBytes(txBytes) 1406 1407 // require that state machine param-dependent checking is still run on recheck since parameters can change between check and recheck 1408 testCases := []struct { 1409 name string 1410 params authtypes.Params 1411 }{ 1412 {"memo size check", authtypes.NewParams(1, authtypes.DefaultTxSigLimit, authtypes.DefaultTxSizeCostPerByte, authtypes.DefaultSigVerifyCostED25519, authtypes.DefaultSigVerifyCostSecp256k1)}, 1413 {"txsize check", authtypes.NewParams(authtypes.DefaultMaxMemoCharacters, authtypes.DefaultTxSigLimit, 10000000, authtypes.DefaultSigVerifyCostED25519, authtypes.DefaultSigVerifyCostSecp256k1)}, 1414 {"sig verify cost check", authtypes.NewParams(authtypes.DefaultMaxMemoCharacters, authtypes.DefaultTxSigLimit, authtypes.DefaultTxSizeCostPerByte, authtypes.DefaultSigVerifyCostED25519, 100000000)}, 1415 } 1416 1417 for _, tc := range testCases { 1418 1419 // set testcase parameters 1420 err := suite.accountKeeper.Params.Set(suite.ctx, tc.params) 1421 require.NoError(t, err) 1422 1423 _, err = suite.anteHandler(suite.ctx, tx, false) 1424 1425 require.NotNil(t, err, "tx does not fail on recheck with updated params in test case: %s", tc.name) 1426 1427 // reset parameters to default values 1428 err = suite.accountKeeper.Params.Set(suite.ctx, authtypes.DefaultParams()) 1429 require.NoError(t, err) 1430 } 1431 1432 suite.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(sdkerrors.ErrInsufficientFee) 1433 // require that local mempool fee check is still run on recheck since validator may change minFee between check and recheck 1434 // create new minimum gas price so antehandler fails on recheck 1435 suite.ctx = suite.ctx.WithMinGasPrices([]sdk.DecCoin{{ 1436 Denom: "dnecoin", // fee does not have this denom 1437 Amount: math.LegacyNewDec(5), 1438 }}) 1439 _, err = suite.anteHandler(suite.ctx, tx, false) 1440 require.NotNil(t, err, "antehandler on recheck did not fail when mingasPrice was changed") 1441 // reset min gasprice 1442 suite.ctx = suite.ctx.WithMinGasPrices(sdk.DecCoins{}) 1443 1444 // remove funds for account so antehandler fails on recheck 1445 suite.accountKeeper.SetAccount(suite.ctx, accs[0].acc) 1446 1447 _, err = suite.anteHandler(suite.ctx, tx, false) 1448 require.NotNil(t, err, "antehandler on recheck did not fail once feePayer no longer has sufficient funds") 1449 }