github.com/Finschia/finschia-sdk@v0.49.1/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/stretchr/testify/require" 11 12 "github.com/Finschia/finschia-sdk/crypto/keys/ed25519" 13 kmultisig "github.com/Finschia/finschia-sdk/crypto/keys/multisig" 14 "github.com/Finschia/finschia-sdk/crypto/keys/secp256k1" 15 cryptotypes "github.com/Finschia/finschia-sdk/crypto/types" 16 "github.com/Finschia/finschia-sdk/simapp" 17 "github.com/Finschia/finschia-sdk/testutil/testdata" 18 sdk "github.com/Finschia/finschia-sdk/types" 19 sdkerrors "github.com/Finschia/finschia-sdk/types/errors" 20 "github.com/Finschia/finschia-sdk/types/tx/signing" 21 "github.com/Finschia/finschia-sdk/x/auth/ante" 22 "github.com/Finschia/finschia-sdk/x/auth/types" 23 minttypes "github.com/Finschia/finschia-sdk/x/mint/types" 24 ) 25 26 // Test that simulate transaction accurately estimates gas cost 27 func (suite *AnteTestSuite) TestSimulateGasCost() { 28 suite.SetupTest(false) // reset 29 30 // Same data for every test cases 31 accounts := suite.CreateTestAccounts(3) 32 msgs := []sdk.Msg{ 33 testdata.NewTestMsg(accounts[0].acc.GetAddress(), accounts[1].acc.GetAddress()), 34 testdata.NewTestMsg(accounts[2].acc.GetAddress(), accounts[0].acc.GetAddress()), 35 testdata.NewTestMsg(accounts[1].acc.GetAddress(), accounts[2].acc.GetAddress()), 36 } 37 feeAmount := testdata.NewTestFeeAmount() 38 gasLimit := testdata.NewTestGasLimit() 39 accSeqs := []uint64{0, 0, 0} 40 privs := []cryptotypes.PrivKey{accounts[0].priv, accounts[1].priv, accounts[2].priv} 41 accNums := []uint64{0, 1, 2} 42 43 testCases := []TestCase{ 44 { 45 "tx with 150atom fee", 46 func() { 47 suite.txBuilder.SetFeeAmount(feeAmount) 48 suite.txBuilder.SetGasLimit(gasLimit) 49 }, 50 true, 51 true, 52 nil, 53 }, 54 { 55 "with previously estimated gas", 56 func() { 57 simulatedGas := suite.ctx.GasMeter().GasConsumed() 58 59 accSeqs = []uint64{1, 1, 1} 60 suite.txBuilder.SetFeeAmount(feeAmount) 61 suite.txBuilder.SetGasLimit(simulatedGas) 62 }, 63 false, 64 true, 65 nil, 66 }, 67 } 68 69 for _, tc := range testCases { 70 suite.Run(fmt.Sprintf("Case %s", tc.desc), func() { 71 suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() 72 tc.malleate() 73 74 suite.RunTestCase(privs, msgs, feeAmount, gasLimit, accNums, accSeqs, suite.ctx.ChainID(), tc) 75 }) 76 } 77 } 78 79 // Test various error cases in the AnteHandler control flow. 80 func (suite *AnteTestSuite) TestAnteHandlerSigErrors() { 81 suite.SetupTest(false) // reset 82 83 // Same data for every test cases 84 priv0, _, addr0 := testdata.KeyTestPubAddr() 85 priv1, _, addr1 := testdata.KeyTestPubAddr() 86 priv2, _, addr2 := testdata.KeyTestPubAddr() 87 msgs := []sdk.Msg{ 88 testdata.NewTestMsg(addr0, addr1), 89 testdata.NewTestMsg(addr0, addr2), 90 } 91 feeAmount := testdata.NewTestFeeAmount() 92 gasLimit := testdata.NewTestGasLimit() 93 94 // Variable data per test case 95 var ( 96 privs []cryptotypes.PrivKey 97 accNums []uint64 98 accSeqs []uint64 99 ) 100 101 testCases := []TestCase{ 102 { 103 "check no signatures fails", 104 func() { 105 privs, accNums, accSeqs = []cryptotypes.PrivKey{}, []uint64{}, []uint64{} 106 107 // Create tx manually to test the tx's signers 108 suite.Require().NoError(suite.txBuilder.SetMsgs(msgs...)) 109 tx, err := suite.CreateTestTx(privs, accNums, accSeqs, suite.ctx.ChainID()) 110 suite.Require().NoError(err) 111 // tx.GetSigners returns addresses in correct order: addr1, addr2, addr3 112 expectedSigners := []sdk.AccAddress{addr0, addr1, addr2} 113 suite.Require().Equal(expectedSigners, tx.GetSigners()) 114 }, 115 false, 116 false, 117 sdkerrors.ErrNoSignatures, 118 }, 119 { 120 "num sigs dont match GetSigners", 121 func() { 122 privs, accNums, accSeqs = []cryptotypes.PrivKey{priv0}, []uint64{0}, []uint64{0} 123 }, 124 false, 125 false, 126 sdkerrors.ErrUnauthorized, 127 }, 128 { 129 "unrecognized account", 130 func() { 131 privs, accNums, accSeqs = []cryptotypes.PrivKey{priv0, priv1, priv2}, []uint64{0, 1, 2}, []uint64{0, 0, 0} 132 }, 133 false, 134 false, 135 sdkerrors.ErrUnknownAddress, // unknown account may send tx, but he doesn't have enough balance to pay fee 136 }, 137 { 138 "save the first account, but second is still unrecognized", 139 func() { 140 acc1 := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr0) 141 suite.app.AccountKeeper.SetAccount(suite.ctx, acc1) 142 err := suite.app.BankKeeper.MintCoins(suite.ctx, minttypes.ModuleName, feeAmount) 143 suite.Require().NoError(err) 144 err = suite.app.BankKeeper.SendCoinsFromModuleToAccount(suite.ctx, minttypes.ModuleName, addr0, feeAmount) 145 suite.Require().NoError(err) 146 }, 147 false, 148 false, 149 sdkerrors.ErrUnknownAddress, 150 }, 151 } 152 153 for _, tc := range testCases { 154 suite.Run(fmt.Sprintf("Case %s", tc.desc), func() { 155 suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() 156 tc.malleate() 157 158 suite.RunTestCase(privs, msgs, feeAmount, gasLimit, accNums, accSeqs, suite.ctx.ChainID(), tc) 159 }) 160 } 161 } 162 163 // Test logic around account number checking with one signer and many signers. 164 func (suite *AnteTestSuite) TestAnteHandlerAccountNumbers() { 165 suite.SetupTest(false) // reset 166 167 // Same data for every test cases 168 accounts := suite.CreateTestAccounts(2) 169 feeAmount := testdata.NewTestFeeAmount() 170 gasLimit := testdata.NewTestGasLimit() 171 172 // Variable data per test case 173 var ( 174 accNums []uint64 175 msgs []sdk.Msg 176 privs []cryptotypes.PrivKey 177 accSeqs []uint64 178 ) 179 180 testCases := []TestCase{ 181 { 182 "good tx from one signer", 183 func() { 184 msg := testdata.NewTestMsg(accounts[0].acc.GetAddress()) 185 msgs = []sdk.Msg{msg} 186 187 privs, accNums, accSeqs = []cryptotypes.PrivKey{accounts[0].priv}, []uint64{0}, []uint64{0} 188 }, 189 false, 190 true, 191 nil, 192 }, 193 { 194 "new tx from wrong account number", 195 func() { 196 privs, accNums, accSeqs = []cryptotypes.PrivKey{accounts[0].priv}, []uint64{1}, []uint64{1} 197 }, 198 false, 199 false, 200 sdkerrors.ErrUnauthorized, 201 }, 202 { 203 "new tx from correct account number", 204 func() { 205 privs, accNums, accSeqs = []cryptotypes.PrivKey{accounts[0].priv}, []uint64{0}, []uint64{1} 206 }, 207 false, 208 true, 209 nil, 210 }, 211 { 212 "new tx with another signer and incorrect account numbers", 213 func() { 214 msg1 := testdata.NewTestMsg(accounts[0].acc.GetAddress(), accounts[1].acc.GetAddress()) 215 msg2 := testdata.NewTestMsg(accounts[1].acc.GetAddress(), accounts[0].acc.GetAddress()) 216 msgs = []sdk.Msg{msg1, msg2} 217 privs, accNums, accSeqs = []cryptotypes.PrivKey{accounts[0].priv, accounts[1].priv}, []uint64{1, 0}, []uint64{2, 0} 218 }, 219 false, 220 false, 221 sdkerrors.ErrUnauthorized, 222 }, 223 { 224 "new tx with correct account numbers", 225 func() { 226 privs, accNums, accSeqs = []cryptotypes.PrivKey{accounts[0].priv, accounts[1].priv}, []uint64{0, 1}, []uint64{2, 0} 227 }, 228 false, 229 true, 230 nil, 231 }, 232 } 233 234 for _, tc := range testCases { 235 suite.Run(fmt.Sprintf("Case %s", tc.desc), func() { 236 suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() 237 tc.malleate() 238 239 suite.RunTestCase(privs, msgs, feeAmount, gasLimit, accNums, accSeqs, suite.ctx.ChainID(), tc) 240 }) 241 } 242 } 243 244 // Test logic around account number checking with many signers when BlockHeight is 0. 245 func (suite *AnteTestSuite) TestAnteHandlerAccountNumbersAtBlockHeightZero() { 246 suite.SetupTest(false) // setup 247 suite.ctx = suite.ctx.WithBlockHeight(0) 248 249 // Same data for every test cases 250 accounts := suite.CreateTestAccounts(2) 251 feeAmount := testdata.NewTestFeeAmount() 252 gasLimit := testdata.NewTestGasLimit() 253 254 // Variable data per test case 255 var ( 256 accNums []uint64 257 msgs []sdk.Msg 258 privs []cryptotypes.PrivKey 259 accSeqs []uint64 260 ) 261 262 testCases := []TestCase{ 263 { 264 "good tx from one signer", 265 func() { 266 msg := testdata.NewTestMsg(accounts[0].acc.GetAddress()) 267 msgs = []sdk.Msg{msg} 268 269 privs, accNums, accSeqs = []cryptotypes.PrivKey{accounts[0].priv}, []uint64{0}, []uint64{0} 270 }, 271 false, 272 true, 273 nil, 274 }, 275 { 276 "new tx from wrong account number", 277 func() { 278 privs, accNums, accSeqs = []cryptotypes.PrivKey{accounts[0].priv}, []uint64{1}, []uint64{1} 279 }, 280 false, 281 false, 282 sdkerrors.ErrUnauthorized, 283 }, 284 { 285 "new tx from correct account number", 286 func() { 287 privs, accNums, accSeqs = []cryptotypes.PrivKey{accounts[0].priv}, []uint64{0}, []uint64{1} 288 }, 289 false, 290 true, 291 nil, 292 }, 293 { 294 "new tx with another signer and incorrect account numbers", 295 func() { 296 msg1 := testdata.NewTestMsg(accounts[0].acc.GetAddress(), accounts[1].acc.GetAddress()) 297 msg2 := testdata.NewTestMsg(accounts[1].acc.GetAddress(), accounts[0].acc.GetAddress()) 298 msgs = []sdk.Msg{msg1, msg2} 299 300 privs, accNums, accSeqs = []cryptotypes.PrivKey{accounts[0].priv, accounts[1].priv}, []uint64{1, 0}, []uint64{2, 0} 301 }, 302 false, 303 false, 304 sdkerrors.ErrUnauthorized, 305 }, 306 { 307 "new tx with another signer and correct account numbers", 308 func() { 309 // Note that accNums is [0,0] at block 0. 310 privs, accNums, accSeqs = []cryptotypes.PrivKey{accounts[0].priv, accounts[1].priv}, []uint64{0, 0}, []uint64{2, 0} 311 }, 312 false, 313 true, 314 nil, 315 }, 316 } 317 318 for _, tc := range testCases { 319 suite.Run(fmt.Sprintf("Case %s", tc.desc), func() { 320 suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() 321 tc.malleate() 322 323 suite.RunTestCase(privs, msgs, feeAmount, gasLimit, accNums, accSeqs, suite.ctx.ChainID(), tc) 324 }) 325 } 326 } 327 328 // Test logic around sequence checking with one signer and many signers. 329 func (suite *AnteTestSuite) TestAnteHandlerSequences() { 330 suite.SetupTest(false) // setup 331 332 // Same data for every test cases 333 accounts := suite.CreateTestAccounts(3) 334 feeAmount := testdata.NewTestFeeAmount() 335 gasLimit := testdata.NewTestGasLimit() 336 337 // Variable data per test case 338 var ( 339 accNums []uint64 340 msgs []sdk.Msg 341 privs []cryptotypes.PrivKey 342 accSeqs []uint64 343 ) 344 345 testCases := []TestCase{ 346 { 347 "good tx from one signer", 348 func() { 349 msg := testdata.NewTestMsg(accounts[0].acc.GetAddress()) 350 msgs = []sdk.Msg{msg} 351 352 privs, accNums, accSeqs = []cryptotypes.PrivKey{accounts[0].priv}, []uint64{0}, []uint64{0} 353 }, 354 false, 355 true, 356 nil, 357 }, 358 { 359 "test sending it again fails (replay protection)", 360 func() { 361 privs, accNums, accSeqs = []cryptotypes.PrivKey{accounts[0].priv}, []uint64{0}, []uint64{0} 362 }, 363 false, 364 false, 365 sdkerrors.ErrWrongSequence, 366 }, 367 { 368 "fix sequence, should pass", 369 func() { 370 privs, accNums, accSeqs = []cryptotypes.PrivKey{accounts[0].priv}, []uint64{0}, []uint64{1} 371 }, 372 false, 373 true, 374 nil, 375 }, 376 { 377 "new tx with another signer and correct sequences", 378 func() { 379 msg1 := testdata.NewTestMsg(accounts[0].acc.GetAddress(), accounts[1].acc.GetAddress()) 380 msg2 := testdata.NewTestMsg(accounts[2].acc.GetAddress(), accounts[0].acc.GetAddress()) 381 msgs = []sdk.Msg{msg1, msg2} 382 383 privs, accNums, accSeqs = []cryptotypes.PrivKey{accounts[0].priv, accounts[1].priv, accounts[2].priv}, []uint64{0, 1, 2}, []uint64{2, 0, 0} 384 }, 385 false, 386 true, 387 nil, 388 }, 389 { 390 "replay fails", 391 func() {}, 392 false, 393 false, 394 sdkerrors.ErrWrongSequence, 395 }, 396 { 397 "tx from just second signer with incorrect sequence fails", 398 func() { 399 msg := testdata.NewTestMsg(accounts[1].acc.GetAddress()) 400 msgs = []sdk.Msg{msg} 401 privs, accNums, accSeqs = []cryptotypes.PrivKey{accounts[1].priv}, []uint64{1}, []uint64{0} 402 }, 403 false, 404 false, 405 sdkerrors.ErrWrongSequence, 406 }, 407 { 408 "fix the sequence and it passes", 409 func() { 410 accSeqs = []uint64{1} 411 }, 412 false, 413 true, 414 nil, 415 }, 416 { 417 "fix the sequence and it passes", 418 func() { 419 msg := testdata.NewTestMsg(accounts[0].acc.GetAddress(), accounts[1].acc.GetAddress()) 420 msgs = []sdk.Msg{msg} 421 422 privs, accNums, accSeqs = []cryptotypes.PrivKey{accounts[0].priv, accounts[1].priv}, []uint64{0, 1}, []uint64{3, 2} 423 }, 424 false, 425 true, 426 nil, 427 }, 428 } 429 430 for _, tc := range testCases { 431 suite.Run(fmt.Sprintf("Case %s", tc.desc), func() { 432 suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() 433 tc.malleate() 434 435 suite.RunTestCase(privs, msgs, feeAmount, gasLimit, accNums, accSeqs, suite.ctx.ChainID(), tc) 436 }) 437 } 438 } 439 440 // Test logic around fee deduction. 441 func (suite *AnteTestSuite) TestAnteHandlerFees() { 442 suite.SetupTest(false) // setup 443 444 // Same data for every test cases 445 priv0, _, addr0 := testdata.KeyTestPubAddr() 446 447 acc1 := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr0) 448 suite.app.AccountKeeper.SetAccount(suite.ctx, acc1) 449 msgs := []sdk.Msg{testdata.NewTestMsg(addr0)} 450 feeAmount := testdata.NewTestFeeAmount() 451 gasLimit := testdata.NewTestGasLimit() 452 privs, accNums, accSeqs := []cryptotypes.PrivKey{priv0}, []uint64{0}, []uint64{0} 453 454 testCases := []struct { 455 desc string 456 malleate func() 457 simulate bool 458 expPass bool 459 expErr error 460 }{ 461 { 462 "signer has no funds", 463 func() { 464 accSeqs = []uint64{0} 465 }, 466 false, 467 false, 468 sdkerrors.ErrInsufficientFunds, 469 }, 470 { 471 "signer does not have enough funds to pay the fee", 472 func() { 473 err := simapp.FundAccount(suite.app, suite.ctx, addr0, sdk.NewCoins(sdk.NewInt64Coin("atom", 149))) 474 suite.Require().NoError(err) 475 }, 476 false, 477 false, 478 sdkerrors.ErrInsufficientFunds, 479 }, 480 { 481 "signer as enough funds, should pass", 482 func() { 483 accNums = []uint64{acc1.GetAccountNumber()} 484 modAcc := suite.app.AccountKeeper.GetModuleAccount(suite.ctx, types.FeeCollectorName) 485 486 suite.Require().True(suite.app.BankKeeper.GetAllBalances(suite.ctx, modAcc.GetAddress()).Empty()) 487 require.True(sdk.IntEq(suite.T(), suite.app.BankKeeper.GetAllBalances(suite.ctx, addr0).AmountOf("atom"), sdk.NewInt(149))) 488 489 err := simapp.FundAccount(suite.app, suite.ctx, addr0, sdk.NewCoins(sdk.NewInt64Coin("atom", 1))) 490 suite.Require().NoError(err) 491 }, 492 false, 493 true, 494 nil, 495 }, 496 { 497 "signer doesn't have any more funds", 498 func() { 499 modAcc := suite.app.AccountKeeper.GetModuleAccount(suite.ctx, types.FeeCollectorName) 500 501 require.True(sdk.IntEq(suite.T(), suite.app.BankKeeper.GetAllBalances(suite.ctx, modAcc.GetAddress()).AmountOf("atom"), sdk.NewInt(150))) 502 require.True(sdk.IntEq(suite.T(), suite.app.BankKeeper.GetAllBalances(suite.ctx, addr0).AmountOf("atom"), sdk.NewInt(0))) 503 }, 504 false, 505 false, 506 sdkerrors.ErrInsufficientFunds, 507 }, 508 } 509 510 for _, tc := range testCases { 511 suite.Run(fmt.Sprintf("Case %s", tc.desc), func() { 512 suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() 513 tc.malleate() 514 515 suite.RunTestCase(privs, msgs, feeAmount, gasLimit, accNums, accSeqs, suite.ctx.ChainID(), tc) 516 }) 517 } 518 } 519 520 // Test logic around memo gas consumption. 521 func (suite *AnteTestSuite) TestAnteHandlerMemoGas() { 522 suite.SetupTest(false) // setup 523 524 // Same data for every test cases 525 accounts := suite.CreateTestAccounts(1) 526 msgs := []sdk.Msg{testdata.NewTestMsg(accounts[0].acc.GetAddress())} 527 privs, accNums, accSeqs := []cryptotypes.PrivKey{accounts[0].priv}, []uint64{0}, []uint64{0} 528 529 // Variable data per test case 530 var ( 531 feeAmount sdk.Coins 532 gasLimit uint64 533 ) 534 535 testCases := []TestCase{ 536 { 537 "tx does not have enough gas", 538 func() { 539 feeAmount = sdk.NewCoins(sdk.NewInt64Coin("atom", 0)) 540 gasLimit = 0 541 }, 542 false, 543 false, 544 sdkerrors.ErrOutOfGas, 545 }, 546 { 547 "tx with memo doesn't have enough gas", 548 func() { 549 feeAmount = sdk.NewCoins(sdk.NewInt64Coin("atom", 0)) 550 gasLimit = 801 551 suite.txBuilder.SetMemo("abcininasidniandsinasindiansdiansdinaisndiasndiadninsd") 552 }, 553 false, 554 false, 555 sdkerrors.ErrOutOfGas, 556 }, 557 { 558 "memo too large", 559 func() { 560 feeAmount = sdk.NewCoins(sdk.NewInt64Coin("atom", 0)) 561 gasLimit = 50000 562 suite.txBuilder.SetMemo(strings.Repeat("01234567890", 500)) 563 }, 564 false, 565 false, 566 sdkerrors.ErrMemoTooLarge, 567 }, 568 { 569 "tx with memo has enough gas", 570 func() { 571 feeAmount = sdk.NewCoins(sdk.NewInt64Coin("atom", 0)) 572 gasLimit = 50000 573 suite.txBuilder.SetMemo(strings.Repeat("0123456789", 10)) 574 }, 575 false, 576 true, 577 nil, 578 }, 579 } 580 581 for _, tc := range testCases { 582 suite.Run(fmt.Sprintf("Case %s", tc.desc), func() { 583 suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() 584 tc.malleate() 585 586 suite.RunTestCase(privs, msgs, feeAmount, gasLimit, accNums, accSeqs, suite.ctx.ChainID(), tc) 587 }) 588 } 589 } 590 591 func (suite *AnteTestSuite) TestAnteHandlerMultiSigner() { 592 suite.SetupTest(false) // setup 593 594 // Same data for every test cases 595 accounts := suite.CreateTestAccounts(3) 596 msg1 := testdata.NewTestMsg(accounts[0].acc.GetAddress(), accounts[1].acc.GetAddress()) 597 msg2 := testdata.NewTestMsg(accounts[2].acc.GetAddress(), accounts[0].acc.GetAddress()) 598 msg3 := testdata.NewTestMsg(accounts[1].acc.GetAddress(), accounts[2].acc.GetAddress()) 599 feeAmount := testdata.NewTestFeeAmount() 600 gasLimit := testdata.NewTestGasLimit() 601 602 // Variable data per test case 603 var ( 604 accNums []uint64 605 msgs []sdk.Msg 606 privs []cryptotypes.PrivKey 607 accSeqs []uint64 608 ) 609 610 testCases := []TestCase{ 611 { 612 "signers in order", 613 func() { 614 msgs = []sdk.Msg{msg1, msg2, msg3} 615 privs, accNums, accSeqs = []cryptotypes.PrivKey{accounts[0].priv, accounts[1].priv, accounts[2].priv}, []uint64{0, 1, 2}, []uint64{0, 0, 0} 616 suite.txBuilder.SetMemo("Check signers are in expected order and different account numbers works") 617 }, 618 false, 619 true, 620 nil, 621 }, 622 { 623 "change sequence numbers (only accounts 0 and 1 sign)", 624 func() { 625 msgs = []sdk.Msg{msg1} 626 privs, accNums, accSeqs = []cryptotypes.PrivKey{accounts[0].priv, accounts[1].priv}, []uint64{0, 1}, []uint64{1, 1} 627 }, 628 false, 629 true, 630 nil, 631 }, 632 { 633 "change sequence numbers (only accounts 1 and 2 sign)", 634 func() { 635 msgs = []sdk.Msg{msg2} 636 privs, accNums, accSeqs = []cryptotypes.PrivKey{accounts[2].priv, accounts[0].priv}, []uint64{2, 0}, []uint64{1, 2} 637 }, 638 false, 639 true, 640 nil, 641 }, 642 { 643 "everyone signs again", 644 func() { 645 msgs = []sdk.Msg{msg1, msg2, msg3} 646 privs, accNums, accSeqs = []cryptotypes.PrivKey{accounts[0].priv, accounts[1].priv, accounts[2].priv}, []uint64{0, 1, 2}, []uint64{3, 2, 2} 647 }, 648 false, 649 true, 650 nil, 651 }, 652 } 653 654 for _, tc := range testCases { 655 suite.Run(fmt.Sprintf("Case %s", tc.desc), func() { 656 suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() 657 tc.malleate() 658 659 suite.RunTestCase(privs, msgs, feeAmount, gasLimit, accNums, accSeqs, suite.ctx.ChainID(), tc) 660 }) 661 } 662 } 663 664 func (suite *AnteTestSuite) TestAnteHandlerBadSignBytes() { 665 suite.SetupTest(false) // setup 666 667 // Same data for every test cases 668 accounts := suite.CreateTestAccounts(2) 669 msg0 := testdata.NewTestMsg(accounts[0].acc.GetAddress()) 670 671 // Variable data per test case 672 var ( 673 accNums []uint64 674 chainID string 675 feeAmount sdk.Coins 676 gasLimit uint64 677 msgs []sdk.Msg 678 privs []cryptotypes.PrivKey 679 accSeqs []uint64 680 ) 681 682 testCases := []TestCase{ 683 { 684 "test good tx and signBytes", 685 func() { 686 chainID = suite.ctx.ChainID() 687 feeAmount = testdata.NewTestFeeAmount() 688 gasLimit = testdata.NewTestGasLimit() 689 msgs = []sdk.Msg{msg0} 690 privs, accNums, accSeqs = []cryptotypes.PrivKey{accounts[0].priv}, []uint64{0}, []uint64{0} 691 }, 692 false, 693 true, 694 nil, 695 }, 696 { 697 "test wrong chainID", 698 func() { 699 accSeqs = []uint64{1} // Back to correct accSeqs 700 chainID = "chain-foo" 701 }, 702 false, 703 false, 704 sdkerrors.ErrUnauthorized, 705 }, 706 { 707 "test wrong accSeqs", 708 func() { 709 chainID = suite.ctx.ChainID() // Back to correct chainID 710 accSeqs = []uint64{2} 711 }, 712 false, 713 false, 714 sdkerrors.ErrWrongSequence, 715 }, 716 { 717 "test wrong accNums", 718 func() { 719 accSeqs = []uint64{1} // Back to correct accSeqs 720 accNums = []uint64{1} 721 }, 722 false, 723 false, 724 sdkerrors.ErrUnauthorized, 725 }, 726 { 727 "test wrong msg", 728 func() { 729 msgs = []sdk.Msg{testdata.NewTestMsg(accounts[1].acc.GetAddress())} 730 }, 731 false, 732 false, 733 sdkerrors.ErrInvalidPubKey, 734 }, 735 { 736 "test wrong fee gas", 737 func() { 738 msgs = []sdk.Msg{msg0} // Back to correct msgs 739 feeAmount = testdata.NewTestFeeAmount() 740 gasLimit = testdata.NewTestGasLimit() + 100 741 }, 742 false, 743 false, 744 sdkerrors.ErrUnauthorized, 745 }, 746 { 747 "test wrong fee amount", 748 func() { 749 feeAmount = testdata.NewTestFeeAmount() 750 feeAmount[0].Amount = feeAmount[0].Amount.AddRaw(100) 751 gasLimit = testdata.NewTestGasLimit() 752 }, 753 false, 754 false, 755 sdkerrors.ErrUnauthorized, 756 }, 757 { 758 "test wrong signer if public key exist", 759 func() { 760 feeAmount = testdata.NewTestFeeAmount() 761 gasLimit = testdata.NewTestGasLimit() 762 privs, accNums, accSeqs = []cryptotypes.PrivKey{accounts[1].priv}, []uint64{0}, []uint64{1} 763 }, 764 false, 765 false, 766 sdkerrors.ErrInvalidPubKey, 767 }, 768 { 769 "test wrong signer if public doesn't exist", 770 func() { 771 msgs = []sdk.Msg{testdata.NewTestMsg(accounts[1].acc.GetAddress())} 772 privs, accNums, accSeqs = []cryptotypes.PrivKey{accounts[0].priv}, []uint64{1}, []uint64{0} 773 }, 774 false, 775 false, 776 sdkerrors.ErrInvalidPubKey, 777 }, 778 } 779 780 for _, tc := range testCases { 781 suite.Run(fmt.Sprintf("Case %s", tc.desc), func() { 782 suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() 783 tc.malleate() 784 785 suite.RunTestCase(privs, msgs, feeAmount, gasLimit, accNums, accSeqs, chainID, tc) 786 }) 787 } 788 } 789 790 func (suite *AnteTestSuite) TestAnteHandlerSetPubKey() { 791 suite.SetupTest(false) // setup 792 793 // Same data for every test cases 794 accounts := suite.CreateTestAccounts(2) 795 feeAmount := testdata.NewTestFeeAmount() 796 gasLimit := testdata.NewTestGasLimit() 797 798 // Variable data per test case 799 var ( 800 accNums []uint64 801 msgs []sdk.Msg 802 privs []cryptotypes.PrivKey 803 accSeqs []uint64 804 ) 805 806 testCases := []TestCase{ 807 { 808 "test good tx", 809 func() { 810 privs, accNums, accSeqs = []cryptotypes.PrivKey{accounts[0].priv}, []uint64{0}, []uint64{0} 811 msgs = []sdk.Msg{testdata.NewTestMsg(accounts[0].acc.GetAddress())} 812 }, 813 false, 814 true, 815 nil, 816 }, 817 { 818 "make sure public key has been set (tx itself should fail because of replay protection)", 819 func() { 820 // Make sure public key has been set from previous test. 821 acc0 := suite.app.AccountKeeper.GetAccount(suite.ctx, accounts[0].acc.GetAddress()) 822 suite.Require().Equal(acc0.GetPubKey(), accounts[0].priv.PubKey()) 823 }, 824 false, 825 false, 826 sdkerrors.ErrWrongSequence, 827 }, 828 { 829 "test public key not found", 830 func() { 831 // See above, `privs` still holds the private key of accounts[0]. 832 msgs = []sdk.Msg{testdata.NewTestMsg(accounts[1].acc.GetAddress())} 833 }, 834 false, 835 false, 836 sdkerrors.ErrInvalidPubKey, 837 }, 838 { 839 "make sure public key is not set, when tx has no pubkey or signature", 840 func() { 841 // Make sure public key has not been set from previous test. 842 acc1 := suite.app.AccountKeeper.GetAccount(suite.ctx, accounts[1].acc.GetAddress()) 843 suite.Require().Nil(acc1.GetPubKey()) 844 845 privs, accNums, accSeqs = []cryptotypes.PrivKey{accounts[1].priv}, []uint64{1}, []uint64{0} 846 msgs = []sdk.Msg{testdata.NewTestMsg(accounts[1].acc.GetAddress())} 847 err := suite.txBuilder.SetMsgs(msgs...) 848 suite.Require().NoError(err) 849 suite.txBuilder.SetFeeAmount(feeAmount) 850 suite.txBuilder.SetGasLimit(gasLimit) 851 852 // Manually create tx, and remove signature. 853 tx, err := suite.CreateTestTx(privs, accNums, accSeqs, suite.ctx.ChainID()) 854 suite.Require().NoError(err) 855 txBuilder, err := suite.clientCtx.TxConfig.WrapTxBuilder(tx) 856 suite.Require().NoError(err) 857 suite.Require().NoError(txBuilder.SetSignatures()) 858 859 // Run anteHandler manually, expect ErrNoSignatures. 860 _, err = suite.anteHandler(suite.ctx, txBuilder.GetTx(), false) 861 suite.Require().Error(err) 862 suite.Require().True(errors.Is(err, sdkerrors.ErrNoSignatures)) 863 864 // Make sure public key has not been set. 865 acc1 = suite.app.AccountKeeper.GetAccount(suite.ctx, accounts[1].acc.GetAddress()) 866 suite.Require().Nil(acc1.GetPubKey()) 867 868 // Set incorrect accSeq, to generate incorrect signature. 869 privs, accNums, accSeqs = []cryptotypes.PrivKey{accounts[1].priv}, []uint64{1}, []uint64{1} 870 }, 871 false, 872 false, 873 sdkerrors.ErrWrongSequence, 874 }, 875 { 876 "make sure previous public key has been set after wrong signature", 877 func() { 878 // Make sure public key has been set, as SetPubKeyDecorator 879 // is called before all signature verification decorators. 880 acc1 := suite.app.AccountKeeper.GetAccount(suite.ctx, accounts[1].acc.GetAddress()) 881 suite.Require().Equal(acc1.GetPubKey(), accounts[1].priv.PubKey()) 882 }, 883 false, 884 false, 885 sdkerrors.ErrWrongSequence, 886 }, 887 } 888 889 for _, tc := range testCases { 890 suite.Run(fmt.Sprintf("Case %s", tc.desc), func() { 891 suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() 892 tc.malleate() 893 894 suite.RunTestCase(privs, msgs, feeAmount, gasLimit, accNums, accSeqs, suite.ctx.ChainID(), tc) 895 }) 896 } 897 } 898 899 func generatePubKeysAndSignatures(n int, msg []byte, _ bool) (pubkeys []cryptotypes.PubKey, signatures [][]byte) { 900 pubkeys = make([]cryptotypes.PubKey, n) 901 signatures = make([][]byte, n) 902 for i := 0; i < n; i++ { 903 var privkey cryptotypes.PrivKey = secp256k1.GenPrivKey() 904 905 // TODO: also generate ed25519 keys as below when ed25519 keys are 906 // actually supported, https://github.com/cosmos/cosmos-sdk/issues/4789 907 // for now this fails: 908 // if rand.Int63()%2 == 0 { 909 // privkey = ed25519.GenPrivKey() 910 // } else { 911 // privkey = secp256k1.GenPrivKey() 912 //} 913 914 pubkeys[i] = privkey.PubKey() 915 signatures[i], _ = privkey.Sign(msg) 916 } 917 return 918 } 919 920 func expectedGasCostByKeys(pubkeys []cryptotypes.PubKey) uint64 { 921 cost := uint64(0) 922 for _, pubkey := range pubkeys { 923 pubkeyType := strings.ToLower(fmt.Sprintf("%T", pubkey)) 924 switch { 925 case strings.Contains(pubkeyType, "ed25519"): 926 cost += types.DefaultParams().SigVerifyCostED25519 927 case strings.Contains(pubkeyType, "secp256k1"): 928 cost += types.DefaultParams().SigVerifyCostSecp256k1 929 default: 930 panic("unexpected key type") 931 } 932 } 933 return cost 934 } 935 936 func TestCountSubkeys(t *testing.T) { 937 genPubKeys := func(n int) []cryptotypes.PubKey { 938 var ret []cryptotypes.PubKey 939 for i := 0; i < n; i++ { 940 ret = append(ret, secp256k1.GenPrivKey().PubKey()) 941 } 942 return ret 943 } 944 singleKey := secp256k1.GenPrivKey().PubKey() 945 singleLevelMultiKey := kmultisig.NewLegacyAminoPubKey(4, genPubKeys(5)) 946 multiLevelSubKey1 := kmultisig.NewLegacyAminoPubKey(4, genPubKeys(5)) 947 multiLevelSubKey2 := kmultisig.NewLegacyAminoPubKey(4, genPubKeys(5)) 948 multiLevelMultiKey := kmultisig.NewLegacyAminoPubKey(2, []cryptotypes.PubKey{ 949 multiLevelSubKey1, multiLevelSubKey2, secp256k1.GenPrivKey().PubKey(), 950 }) 951 type args struct { 952 pub cryptotypes.PubKey 953 } 954 testCases := []struct { 955 name string 956 args args 957 want int 958 }{ 959 {"single key", args{singleKey}, 1}, 960 {"single level multikey", args{singleLevelMultiKey}, 5}, 961 {"multi level multikey", args{multiLevelMultiKey}, 11}, 962 } 963 for _, tc := range testCases { 964 t.Run(tc.name, func(T *testing.T) { 965 require.Equal(t, tc.want, ante.CountSubKeys(tc.args.pub)) 966 }) 967 } 968 } 969 970 func (suite *AnteTestSuite) TestAnteHandlerSigLimitExceeded() { 971 suite.SetupTest(false) // setup 972 973 // Same data for every test cases 974 accounts := suite.CreateTestAccounts(8) 975 var addrs []sdk.AccAddress 976 var privs []cryptotypes.PrivKey 977 for i := 0; i < 8; i++ { 978 addrs = append(addrs, accounts[i].acc.GetAddress()) 979 privs = append(privs, accounts[i].priv) 980 } 981 msgs := []sdk.Msg{testdata.NewTestMsg(addrs...)} 982 accNums, accSeqs := []uint64{0, 1, 2, 3, 4, 5, 6, 7}, []uint64{0, 0, 0, 0, 0, 0, 0, 0} 983 feeAmount := testdata.NewTestFeeAmount() 984 gasLimit := testdata.NewTestGasLimit() 985 986 testCases := []TestCase{ 987 { 988 "test rejection logic", 989 func() {}, 990 false, 991 false, 992 sdkerrors.ErrTooManySignatures, 993 }, 994 } 995 996 for _, tc := range testCases { 997 suite.Run(fmt.Sprintf("Case %s", tc.desc), func() { 998 suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() 999 tc.malleate() 1000 1001 suite.RunTestCase(privs, msgs, feeAmount, gasLimit, accNums, accSeqs, suite.ctx.ChainID(), tc) 1002 }) 1003 } 1004 } 1005 1006 // Test custom SignatureVerificationGasConsumer 1007 func (suite *AnteTestSuite) TestCustomSignatureVerificationGasConsumer() { 1008 suite.SetupTest(false) // setup 1009 1010 // setup an ante handler that only accepts PubKeyEd25519 1011 anteHandler, err := ante.NewAnteHandler( 1012 ante.HandlerOptions{ 1013 AccountKeeper: suite.app.AccountKeeper, 1014 BankKeeper: suite.app.BankKeeper, 1015 FeegrantKeeper: suite.app.FeeGrantKeeper, 1016 SignModeHandler: suite.clientCtx.TxConfig.SignModeHandler(), 1017 SigGasConsumer: func(meter sdk.GasMeter, sig signing.SignatureV2, params types.Params) error { 1018 switch pubkey := sig.PubKey.(type) { 1019 case *ed25519.PubKey: 1020 meter.ConsumeGas(params.SigVerifyCostED25519, "ante verify: ed25519") 1021 return nil 1022 default: 1023 return sdkerrors.Wrapf(sdkerrors.ErrInvalidPubKey, "unrecognized public key type: %T", pubkey) 1024 } 1025 }, 1026 }, 1027 ) 1028 1029 suite.Require().NoError(err) 1030 suite.anteHandler = anteHandler 1031 1032 // Same data for every test cases 1033 accounts := suite.CreateTestAccounts(1) 1034 feeAmount := testdata.NewTestFeeAmount() 1035 gasLimit := testdata.NewTestGasLimit() 1036 1037 // Variable data per test case 1038 var ( 1039 accNums []uint64 1040 msgs []sdk.Msg 1041 privs []cryptotypes.PrivKey 1042 accSeqs []uint64 1043 ) 1044 1045 testCases := []TestCase{ 1046 { 1047 "verify that an secp256k1 account gets rejected", 1048 func() { 1049 msgs = []sdk.Msg{testdata.NewTestMsg(accounts[0].acc.GetAddress())} 1050 privs, accNums, accSeqs = []cryptotypes.PrivKey{accounts[0].priv}, []uint64{0}, []uint64{0} 1051 }, 1052 false, 1053 false, 1054 sdkerrors.ErrInvalidPubKey, 1055 }, 1056 } 1057 1058 for _, tc := range testCases { 1059 suite.Run(fmt.Sprintf("Case %s", tc.desc), func() { 1060 suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() 1061 tc.malleate() 1062 1063 suite.RunTestCase(privs, msgs, feeAmount, gasLimit, accNums, accSeqs, suite.ctx.ChainID(), tc) 1064 }) 1065 } 1066 } 1067 1068 func (suite *AnteTestSuite) TestAnteHandlerReCheck() { 1069 suite.SetupTest(false) // setup 1070 // Set recheck=true 1071 suite.ctx = suite.ctx.WithIsReCheckTx(true) 1072 suite.txBuilder = suite.clientCtx.TxConfig.NewTxBuilder() 1073 1074 // Same data for every test cases 1075 accounts := suite.CreateTestAccounts(1) 1076 1077 feeAmount := testdata.NewTestFeeAmount() 1078 gasLimit := testdata.NewTestGasLimit() 1079 suite.txBuilder.SetFeeAmount(feeAmount) 1080 suite.txBuilder.SetGasLimit(gasLimit) 1081 1082 msg := testdata.NewTestMsg(accounts[0].acc.GetAddress()) 1083 msgs := []sdk.Msg{msg} 1084 suite.Require().NoError(suite.txBuilder.SetMsgs(msgs...)) 1085 1086 suite.txBuilder.SetMemo("thisisatestmemo") 1087 1088 // test that operations skipped on recheck do not run 1089 privs, accNums, accSeqs := []cryptotypes.PrivKey{accounts[0].priv}, []uint64{0}, []uint64{0} 1090 tx, err := suite.CreateTestTx(privs, accNums, accSeqs, suite.ctx.ChainID()) 1091 suite.Require().NoError(err) 1092 1093 // make signature array empty which would normally cause ValidateBasicDecorator and SigVerificationDecorator fail 1094 // since these decorators don't run on recheck, the tx should pass the antehandler 1095 txBuilder, err := suite.clientCtx.TxConfig.WrapTxBuilder(tx) 1096 suite.Require().NoError(err) 1097 1098 _, err = suite.anteHandler(suite.ctx, txBuilder.GetTx(), false) 1099 suite.Require().Nil(err, "AnteHandler errored on recheck unexpectedly: %v", err) 1100 1101 tx, err = suite.CreateTestTx(privs, accNums, accSeqs, suite.ctx.ChainID()) 1102 suite.Require().NoError(err) 1103 txBytes, err := json.Marshal(tx) 1104 suite.Require().Nil(err, "Error marshaling tx: %v", err) 1105 suite.ctx = suite.ctx.WithTxBytes(txBytes) 1106 1107 // require that state machine param-dependent checking is still run on recheck since parameters can change between check and recheck 1108 testCases := []struct { 1109 name string 1110 params types.Params 1111 }{ 1112 {"memo size check", types.NewParams(1, types.DefaultTxSigLimit, types.DefaultTxSizeCostPerByte, types.DefaultSigVerifyCostED25519, types.DefaultSigVerifyCostSecp256k1)}, 1113 {"txsize check", types.NewParams(types.DefaultMaxMemoCharacters, types.DefaultTxSigLimit, 10000000, types.DefaultSigVerifyCostED25519, types.DefaultSigVerifyCostSecp256k1)}, 1114 {"sig verify cost check", types.NewParams(types.DefaultMaxMemoCharacters, types.DefaultTxSigLimit, types.DefaultTxSizeCostPerByte, types.DefaultSigVerifyCostED25519, 100000000)}, 1115 } 1116 for _, tc := range testCases { 1117 // set testcase parameters 1118 suite.app.AccountKeeper.SetParams(suite.ctx, tc.params) 1119 1120 _, err := suite.anteHandler(suite.ctx, tx, false) 1121 1122 suite.Require().NotNil(err, "tx does not fail on recheck with updated params in test case: %s", tc.name) 1123 1124 // reset parameters to default values 1125 suite.app.AccountKeeper.SetParams(suite.ctx, types.DefaultParams()) 1126 } 1127 1128 // require that local mempool fee check is still run on recheck since validator may change minFee between check and recheck 1129 // create new minimum gas price so antehandler fails on recheck 1130 suite.ctx = suite.ctx.WithMinGasPrices([]sdk.DecCoin{{ 1131 Denom: "dnecoin", // fee does not have this denom 1132 Amount: sdk.NewDec(5), 1133 }}) 1134 _, err = suite.anteHandler(suite.ctx, tx, false) 1135 suite.Require().NotNil(err, "antehandler on recheck did not fail when mingasPrice was changed") 1136 // reset min gasprice 1137 suite.ctx = suite.ctx.WithMinGasPrices(sdk.DecCoins{}) 1138 1139 // remove funds for account so antehandler fails on recheck 1140 suite.app.AccountKeeper.SetAccount(suite.ctx, accounts[0].acc) 1141 balances := suite.app.BankKeeper.GetAllBalances(suite.ctx, accounts[0].acc.GetAddress()) 1142 err = suite.app.BankKeeper.SendCoinsFromAccountToModule(suite.ctx, accounts[0].acc.GetAddress(), minttypes.ModuleName, balances) 1143 suite.Require().NoError(err) 1144 1145 _, err = suite.anteHandler(suite.ctx, tx, false) 1146 suite.Require().NotNil(err, "antehandler on recheck did not fail once feePayer no longer has sufficient funds") 1147 }