github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/auth/ante/ante_test.go (about) 1 package ante_test 2 3 import ( 4 "encoding/json" 5 "errors" 6 "fmt" 7 "math/rand" 8 "strings" 9 "testing" 10 11 "github.com/fibonacci-chain/fbc/libs/tendermint/crypto" 12 "github.com/fibonacci-chain/fbc/libs/tendermint/crypto/ed25519" 13 "github.com/fibonacci-chain/fbc/libs/tendermint/crypto/multisig" 14 "github.com/fibonacci-chain/fbc/libs/tendermint/crypto/secp256k1" 15 "github.com/stretchr/testify/require" 16 17 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 18 sdkerrors "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/errors" 19 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/ante" 20 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/types" 21 ) 22 23 // run the tx through the anteHandler and ensure its valid 24 func checkValidTx(t *testing.T, anteHandler sdk.AnteHandler, ctx sdk.Context, tx sdk.Tx, simulate bool) { 25 _, err := anteHandler(ctx, tx, simulate) 26 require.Nil(t, err) 27 } 28 29 // run the tx through the anteHandler and ensure it fails with the given code 30 func checkInvalidTx(t *testing.T, anteHandler sdk.AnteHandler, ctx sdk.Context, tx sdk.Tx, simulate bool, expErr error) { 31 _, err := anteHandler(ctx, tx, simulate) 32 require.NotNil(t, err) 33 require.True(t, errors.Is(expErr, err)) 34 } 35 36 // Test that simulate transaction accurately estimates gas cost 37 func TestSimulateGasCost(t *testing.T) { 38 // setup 39 app, ctx := createTestApp(true) 40 ctx.SetBlockHeight(1) 41 anteHandler := ante.NewAnteHandler(app.AccountKeeper, app.SupplyKeeper, ante.DefaultSigVerificationGasConsumer) 42 43 // keys and addresses 44 priv1, _, addr1 := types.KeyTestPubAddr() 45 priv2, _, addr2 := types.KeyTestPubAddr() 46 priv3, _, addr3 := types.KeyTestPubAddr() 47 48 // set the accounts 49 acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1) 50 acc1.SetCoins(types.NewTestCoins()) 51 require.NoError(t, acc1.SetAccountNumber(0)) 52 app.AccountKeeper.SetAccount(ctx, acc1) 53 acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2) 54 acc2.SetCoins(types.NewTestCoins()) 55 require.NoError(t, acc2.SetAccountNumber(1)) 56 app.AccountKeeper.SetAccount(ctx, acc2) 57 acc3 := app.AccountKeeper.NewAccountWithAddress(ctx, addr3) 58 acc3.SetCoins(types.NewTestCoins()) 59 require.NoError(t, acc3.SetAccountNumber(2)) 60 app.AccountKeeper.SetAccount(ctx, acc3) 61 62 // set up msgs and fee 63 var tx sdk.Tx 64 msg1 := types.NewTestMsg(addr1, addr2) 65 msg2 := types.NewTestMsg(addr3, addr1) 66 msg3 := types.NewTestMsg(addr2, addr3) 67 msgs := []sdk.Msg{msg1, msg2, msg3} 68 fee := types.NewTestStdFee() 69 70 // signers in order. accnums are all 0 because it is in genesis block 71 privs, accnums, seqs := []crypto.PrivKey{priv1, priv2, priv3}, []uint64{0, 1, 2}, []uint64{0, 0, 0} 72 tx = types.NewTestTx(ctx, msgs, privs, accnums, seqs, fee) 73 74 cc, _ := ctx.CacheContext() 75 newCtx, err := anteHandler(cc, tx, true) 76 require.Nil(t, err, "transaction failed on simulate mode") 77 78 simulatedGas := newCtx.GasMeter().GasConsumed() 79 fee.Gas = simulatedGas 80 81 // update tx with simulated gas estimate 82 tx = types.NewTestTx(ctx, msgs, privs, accnums, seqs, fee) 83 _, err = anteHandler(ctx, tx, false) 84 85 require.Nil(t, err, "transaction failed with gas estimate") 86 } 87 88 // Test various error cases in the AnteHandler control flow. 89 func TestAnteHandlerSigErrors(t *testing.T) { 90 // setup 91 app, ctx := createTestApp(true) 92 anteHandler := ante.NewAnteHandler(app.AccountKeeper, app.SupplyKeeper, ante.DefaultSigVerificationGasConsumer) 93 94 // keys and addresses 95 priv1, _, addr1 := types.KeyTestPubAddr() 96 priv2, _, addr2 := types.KeyTestPubAddr() 97 priv3, _, addr3 := types.KeyTestPubAddr() 98 99 // msg and signatures 100 var tx sdk.Tx 101 msg1 := types.NewTestMsg(addr1, addr2) 102 msg2 := types.NewTestMsg(addr1, addr3) 103 fee := types.NewTestStdFee() 104 105 msgs := []sdk.Msg{msg1, msg2} 106 107 // test no signatures 108 privs, accNums, seqs := []crypto.PrivKey{}, []uint64{}, []uint64{} 109 tx = types.NewTestTx(ctx, msgs, privs, accNums, seqs, fee) 110 111 // tx.GetSigners returns addresses in correct order: addr1, addr2, addr3 112 expectedSigners := []sdk.AccAddress{addr1, addr2, addr3} 113 stdTx := tx.(*types.StdTx) 114 require.Equal(t, expectedSigners, stdTx.GetSigners()) 115 116 // Check no signatures fails 117 checkInvalidTx(t, anteHandler, ctx, tx, false, sdkerrors.ErrNoSignatures) 118 119 // test num sigs dont match GetSigners 120 privs, accNums, seqs = []crypto.PrivKey{priv1}, []uint64{0}, []uint64{0} 121 tx = types.NewTestTx(ctx, msgs, privs, accNums, seqs, fee) 122 checkInvalidTx(t, anteHandler, ctx, tx, false, sdkerrors.ErrUnauthorized) 123 124 // test an unrecognized account 125 privs, accNums, seqs = []crypto.PrivKey{priv1, priv2, priv3}, []uint64{0, 1, 2}, []uint64{0, 0, 0} 126 tx = types.NewTestTx(ctx, msgs, privs, accNums, seqs, fee) 127 checkInvalidTx(t, anteHandler, ctx, tx, false, sdkerrors.ErrUnknownAddress) 128 129 // save the first account, but second is still unrecognized 130 acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1) 131 acc1.SetCoins(fee.Amount) 132 app.AccountKeeper.SetAccount(ctx, acc1) 133 checkInvalidTx(t, anteHandler, ctx, tx, false, sdkerrors.ErrUnknownAddress) 134 } 135 136 // Test logic around account number checking with one signer and many signers. 137 func TestAnteHandlerAccountNumbers(t *testing.T) { 138 // setup 139 app, ctx := createTestApp(false) 140 ctx.SetBlockHeight(1) 141 anteHandler := ante.NewAnteHandler(app.AccountKeeper, app.SupplyKeeper, ante.DefaultSigVerificationGasConsumer) 142 143 // keys and addresses 144 priv1, _, addr1 := types.KeyTestPubAddr() 145 priv2, _, addr2 := types.KeyTestPubAddr() 146 147 // set the accounts 148 acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1) 149 acc1.SetCoins(types.NewTestCoins()) 150 require.NoError(t, acc1.SetAccountNumber(0)) 151 app.AccountKeeper.SetAccount(ctx, acc1) 152 acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2) 153 acc2.SetCoins(types.NewTestCoins()) 154 require.NoError(t, acc2.SetAccountNumber(1)) 155 app.AccountKeeper.SetAccount(ctx, acc2) 156 157 // msg and signatures 158 var tx sdk.Tx 159 msg := types.NewTestMsg(addr1) 160 fee := types.NewTestStdFee() 161 162 msgs := []sdk.Msg{msg} 163 164 // test good tx from one signer 165 privs, accnums, seqs := []crypto.PrivKey{priv1}, []uint64{0}, []uint64{0} 166 tx = types.NewTestTx(ctx, msgs, privs, accnums, seqs, fee) 167 checkValidTx(t, anteHandler, ctx, tx, false) 168 169 // new tx from wrong account number 170 seqs = []uint64{1} 171 tx = types.NewTestTx(ctx, msgs, privs, []uint64{1}, seqs, fee) 172 checkInvalidTx(t, anteHandler, ctx, tx, false, sdkerrors.ErrUnauthorized) 173 174 // from correct account number 175 seqs = []uint64{1} 176 tx = types.NewTestTx(ctx, msgs, privs, []uint64{0}, seqs, fee) 177 checkValidTx(t, anteHandler, ctx, tx, false) 178 179 // new tx with another signer and incorrect account numbers 180 msg1 := types.NewTestMsg(addr1, addr2) 181 msg2 := types.NewTestMsg(addr2, addr1) 182 msgs = []sdk.Msg{msg1, msg2} 183 privs, accnums, seqs = []crypto.PrivKey{priv1, priv2}, []uint64{1, 0}, []uint64{2, 0} 184 tx = types.NewTestTx(ctx, msgs, privs, accnums, seqs, fee) 185 checkInvalidTx(t, anteHandler, ctx, tx, false, sdkerrors.ErrUnauthorized) 186 187 // correct account numbers 188 privs, accnums, seqs = []crypto.PrivKey{priv1, priv2}, []uint64{0, 1}, []uint64{2, 0} 189 tx = types.NewTestTx(ctx, msgs, privs, accnums, seqs, fee) 190 checkValidTx(t, anteHandler, ctx, tx, false) 191 } 192 193 // Test logic around account number checking with many signers when BlockHeight is 0. 194 func TestAnteHandlerAccountNumbersAtBlockHeightZero(t *testing.T) { 195 // setup 196 app, ctx := createTestApp(false) 197 ctx.SetBlockHeight(0) 198 anteHandler := ante.NewAnteHandler(app.AccountKeeper, app.SupplyKeeper, ante.DefaultSigVerificationGasConsumer) 199 200 // keys and addresses 201 priv1, _, addr1 := types.KeyTestPubAddr() 202 priv2, _, addr2 := types.KeyTestPubAddr() 203 204 // set the accounts, we don't need the acc numbers as it is in the genesis block 205 acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1) 206 acc1.SetCoins(types.NewTestCoins()) 207 app.AccountKeeper.SetAccount(ctx, acc1) 208 acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2) 209 acc2.SetCoins(types.NewTestCoins()) 210 require.NoError(t, acc2.SetAccountNumber(1)) 211 app.AccountKeeper.SetAccount(ctx, acc2) 212 213 // msg and signatures 214 var tx sdk.Tx 215 msg := types.NewTestMsg(addr1) 216 fee := types.NewTestStdFee() 217 218 msgs := []sdk.Msg{msg} 219 220 // test good tx from one signer 221 privs, accnums, seqs := []crypto.PrivKey{priv1}, []uint64{0}, []uint64{0} 222 tx = types.NewTestTx(ctx, msgs, privs, accnums, seqs, fee) 223 checkValidTx(t, anteHandler, ctx, tx, false) 224 225 // new tx from wrong account number 226 seqs = []uint64{1} 227 tx = types.NewTestTx(ctx, msgs, privs, []uint64{1}, seqs, fee) 228 checkInvalidTx(t, anteHandler, ctx, tx, false, sdkerrors.ErrUnauthorized) 229 230 // from correct account number 231 seqs = []uint64{1} 232 tx = types.NewTestTx(ctx, msgs, privs, []uint64{0}, seqs, fee) 233 checkValidTx(t, anteHandler, ctx, tx, false) 234 235 // new tx with another signer and incorrect account numbers 236 msg1 := types.NewTestMsg(addr1, addr2) 237 msg2 := types.NewTestMsg(addr2, addr1) 238 msgs = []sdk.Msg{msg1, msg2} 239 privs, accnums, seqs = []crypto.PrivKey{priv1, priv2}, []uint64{1, 0}, []uint64{2, 0} 240 tx = types.NewTestTx(ctx, msgs, privs, accnums, seqs, fee) 241 checkInvalidTx(t, anteHandler, ctx, tx, false, sdkerrors.ErrUnauthorized) 242 243 // correct account numbers 244 privs, accnums, seqs = []crypto.PrivKey{priv1, priv2}, []uint64{0, 0}, []uint64{2, 0} 245 tx = types.NewTestTx(ctx, msgs, privs, accnums, seqs, fee) 246 checkValidTx(t, anteHandler, ctx, tx, false) 247 } 248 249 // Test logic around sequence checking with one signer and many signers. 250 func TestAnteHandlerSequences(t *testing.T) { 251 // setup 252 app, ctx := createTestApp(false) 253 ctx.SetBlockHeight(1) 254 anteHandler := ante.NewAnteHandler(app.AccountKeeper, app.SupplyKeeper, ante.DefaultSigVerificationGasConsumer) 255 256 // keys and addresses 257 priv1, _, addr1 := types.KeyTestPubAddr() 258 priv2, _, addr2 := types.KeyTestPubAddr() 259 priv3, _, addr3 := types.KeyTestPubAddr() 260 261 // set the accounts 262 acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1) 263 acc1.SetCoins(types.NewTestCoins()) 264 require.NoError(t, acc1.SetAccountNumber(0)) 265 app.AccountKeeper.SetAccount(ctx, acc1) 266 acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2) 267 acc2.SetCoins(types.NewTestCoins()) 268 require.NoError(t, acc2.SetAccountNumber(1)) 269 app.AccountKeeper.SetAccount(ctx, acc2) 270 acc3 := app.AccountKeeper.NewAccountWithAddress(ctx, addr3) 271 acc3.SetCoins(types.NewTestCoins()) 272 require.NoError(t, acc3.SetAccountNumber(2)) 273 app.AccountKeeper.SetAccount(ctx, acc3) 274 275 // msg and signatures 276 var tx sdk.Tx 277 msg := types.NewTestMsg(addr1) 278 fee := types.NewTestStdFee() 279 280 msgs := []sdk.Msg{msg} 281 282 // test good tx from one signer 283 privs, accnums, seqs := []crypto.PrivKey{priv1}, []uint64{0}, []uint64{0} 284 tx = types.NewTestTx(ctx, msgs, privs, accnums, seqs, fee) 285 checkValidTx(t, anteHandler, ctx, tx, false) 286 287 // test sending it again fails (replay protection) 288 checkInvalidTx(t, anteHandler, ctx, tx, false, sdkerrors.ErrUnauthorized) 289 290 // fix sequence, should pass 291 seqs = []uint64{1} 292 tx = types.NewTestTx(ctx, msgs, privs, accnums, seqs, fee) 293 checkValidTx(t, anteHandler, ctx, tx, false) 294 295 // new tx with another signer and correct sequences 296 msg1 := types.NewTestMsg(addr1, addr2) 297 msg2 := types.NewTestMsg(addr3, addr1) 298 msgs = []sdk.Msg{msg1, msg2} 299 300 privs, accnums, seqs = []crypto.PrivKey{priv1, priv2, priv3}, []uint64{0, 1, 2}, []uint64{2, 0, 0} 301 tx = types.NewTestTx(ctx, msgs, privs, accnums, seqs, fee) 302 checkValidTx(t, anteHandler, ctx, tx, false) 303 304 // replay fails 305 checkInvalidTx(t, anteHandler, ctx, tx, false, sdkerrors.ErrUnauthorized) 306 307 // tx from just second signer with incorrect sequence fails 308 msg = types.NewTestMsg(addr2) 309 msgs = []sdk.Msg{msg} 310 privs, accnums, seqs = []crypto.PrivKey{priv2}, []uint64{1}, []uint64{0} 311 tx = types.NewTestTx(ctx, msgs, privs, accnums, seqs, fee) 312 checkInvalidTx(t, anteHandler, ctx, tx, false, sdkerrors.ErrUnauthorized) 313 314 // fix the sequence and it passes 315 tx = types.NewTestTx(ctx, msgs, []crypto.PrivKey{priv2}, []uint64{1}, []uint64{1}, fee) 316 checkValidTx(t, anteHandler, ctx, tx, false) 317 318 // another tx from both of them that passes 319 msg = types.NewTestMsg(addr1, addr2) 320 msgs = []sdk.Msg{msg} 321 privs, accnums, seqs = []crypto.PrivKey{priv1, priv2}, []uint64{0, 1}, []uint64{3, 2} 322 tx = types.NewTestTx(ctx, msgs, privs, accnums, seqs, fee) 323 checkValidTx(t, anteHandler, ctx, tx, false) 324 } 325 326 // Test logic around fee deduction. 327 func TestAnteHandlerFees(t *testing.T) { 328 // setup 329 app, ctx := createTestApp(true) 330 anteHandler := ante.NewAnteHandler(app.AccountKeeper, app.SupplyKeeper, ante.DefaultSigVerificationGasConsumer) 331 332 // keys and addresses 333 priv1, _, addr1 := types.KeyTestPubAddr() 334 335 // set the accounts 336 acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1) 337 app.AccountKeeper.SetAccount(ctx, acc1) 338 339 // msg and signatures 340 var tx sdk.Tx 341 msg := types.NewTestMsg(addr1) 342 privs, accnums, seqs := []crypto.PrivKey{priv1}, []uint64{0}, []uint64{0} 343 fee := types.NewTestStdFee() 344 msgs := []sdk.Msg{msg} 345 346 // signer does not have enough funds to pay the fee 347 tx = types.NewTestTx(ctx, msgs, privs, accnums, seqs, fee) 348 checkInvalidTx(t, anteHandler, ctx, tx, false, sdkerrors.ErrInsufficientFunds) 349 350 acc1.SetCoins(sdk.NewCoins(sdk.NewInt64Coin("atom", 149))) 351 app.AccountKeeper.SetAccount(ctx, acc1) 352 checkInvalidTx(t, anteHandler, ctx, tx, false, sdkerrors.ErrInsufficientFunds) 353 354 require.True(t, app.SupplyKeeper.GetModuleAccount(ctx, types.FeeCollectorName).GetCoins().Empty()) 355 require.True(sdk.DecEq(t, app.AccountKeeper.GetAccount(ctx, addr1).GetCoins().AmountOf("atom"), sdk.NewDec(149))) 356 357 acc1.SetCoins(sdk.NewCoins(sdk.NewInt64Coin("atom", 150))) 358 app.AccountKeeper.SetAccount(ctx, acc1) 359 checkValidTx(t, anteHandler, ctx, tx, false) 360 361 require.True(sdk.DecEq(t, app.SupplyKeeper.GetModuleAccount(ctx, types.FeeCollectorName).GetCoins().AmountOf("atom"), sdk.NewDec(150))) 362 require.True(sdk.DecEq(t, app.AccountKeeper.GetAccount(ctx, addr1).GetCoins().AmountOf("atom"), sdk.NewDec(0))) 363 } 364 365 // Test logic around memo gas consumption. 366 func TestAnteHandlerMemoGas(t *testing.T) { 367 // setup 368 app, ctx := createTestApp(true) 369 ctx.SetBlockHeight(1) 370 anteHandler := ante.NewAnteHandler(app.AccountKeeper, app.SupplyKeeper, ante.DefaultSigVerificationGasConsumer) 371 372 // keys and addresses 373 priv1, _, addr1 := types.KeyTestPubAddr() 374 375 // set the accounts 376 acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1) 377 require.NoError(t, acc1.SetAccountNumber(0)) 378 app.AccountKeeper.SetAccount(ctx, acc1) 379 380 // msg and signatures 381 var tx sdk.Tx 382 msg := types.NewTestMsg(addr1) 383 privs, accnums, seqs := []crypto.PrivKey{priv1}, []uint64{0}, []uint64{0} 384 fee := types.NewStdFee(0, sdk.NewCoins(sdk.NewInt64Coin("atom", 0))) 385 386 // tx does not have enough gas 387 tx = types.NewTestTx(ctx, []sdk.Msg{msg}, privs, accnums, seqs, fee) 388 checkInvalidTx(t, anteHandler, ctx, tx, false, sdkerrors.ErrOutOfGas) 389 390 // tx with memo doesn't have enough gas 391 fee = types.NewStdFee(801, sdk.NewCoins(sdk.NewInt64Coin("atom", 0))) 392 tx = types.NewTestTxWithMemo(ctx, []sdk.Msg{msg}, privs, accnums, seqs, fee, "abcininasidniandsinasindiansdiansdinaisndiasndiadninsd") 393 checkInvalidTx(t, anteHandler, ctx, tx, false, sdkerrors.ErrOutOfGas) 394 395 // memo too large 396 fee = types.NewStdFee(50000, sdk.NewCoins(sdk.NewInt64Coin("atom", 0))) 397 tx = types.NewTestTxWithMemo(ctx, []sdk.Msg{msg}, privs, accnums, seqs, fee, strings.Repeat("01234567890", 500)) 398 checkInvalidTx(t, anteHandler, ctx, tx, false, sdkerrors.ErrMemoTooLarge) 399 400 // tx with memo has enough gas 401 fee = types.NewStdFee(50000, sdk.NewCoins(sdk.NewInt64Coin("atom", 0))) 402 tx = types.NewTestTxWithMemo(ctx, []sdk.Msg{msg}, privs, accnums, seqs, fee, strings.Repeat("0123456789", 10)) 403 checkValidTx(t, anteHandler, ctx, tx, false) 404 } 405 406 func TestAnteHandlerMultiSigner(t *testing.T) { 407 // setup 408 app, ctx := createTestApp(false) 409 ctx.SetBlockHeight(1) 410 anteHandler := ante.NewAnteHandler(app.AccountKeeper, app.SupplyKeeper, ante.DefaultSigVerificationGasConsumer) 411 412 // keys and addresses 413 priv1, _, addr1 := types.KeyTestPubAddr() 414 priv2, _, addr2 := types.KeyTestPubAddr() 415 priv3, _, addr3 := types.KeyTestPubAddr() 416 417 // set the accounts 418 acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1) 419 acc1.SetCoins(types.NewTestCoins()) 420 require.NoError(t, acc1.SetAccountNumber(0)) 421 app.AccountKeeper.SetAccount(ctx, acc1) 422 acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2) 423 acc2.SetCoins(types.NewTestCoins()) 424 require.NoError(t, acc2.SetAccountNumber(1)) 425 app.AccountKeeper.SetAccount(ctx, acc2) 426 acc3 := app.AccountKeeper.NewAccountWithAddress(ctx, addr3) 427 acc3.SetCoins(types.NewTestCoins()) 428 require.NoError(t, acc3.SetAccountNumber(2)) 429 app.AccountKeeper.SetAccount(ctx, acc3) 430 431 // set up msgs and fee 432 var tx sdk.Tx 433 msg1 := types.NewTestMsg(addr1, addr2) 434 msg2 := types.NewTestMsg(addr3, addr1) 435 msg3 := types.NewTestMsg(addr2, addr3) 436 msgs := []sdk.Msg{msg1, msg2, msg3} 437 fee := types.NewTestStdFee() 438 439 // signers in order 440 privs, accnums, seqs := []crypto.PrivKey{priv1, priv2, priv3}, []uint64{0, 1, 2}, []uint64{0, 0, 0} 441 tx = types.NewTestTxWithMemo(ctx, msgs, privs, accnums, seqs, fee, "Check signers are in expected order and different account numbers works") 442 443 checkValidTx(t, anteHandler, ctx, tx, false) 444 445 // change sequence numbers 446 tx = types.NewTestTx(ctx, []sdk.Msg{msg1}, []crypto.PrivKey{priv1, priv2}, []uint64{0, 1}, []uint64{1, 1}, fee) 447 checkValidTx(t, anteHandler, ctx, tx, false) 448 tx = types.NewTestTx(ctx, []sdk.Msg{msg2}, []crypto.PrivKey{priv3, priv1}, []uint64{2, 0}, []uint64{1, 2}, fee) 449 checkValidTx(t, anteHandler, ctx, tx, false) 450 451 // expected seqs = [3, 2, 2] 452 tx = types.NewTestTxWithMemo(ctx, msgs, privs, accnums, []uint64{3, 2, 2}, fee, "Check signers are in expected order and different account numbers and sequence numbers works") 453 checkValidTx(t, anteHandler, ctx, tx, false) 454 } 455 456 func TestAnteHandlerBadSignBytes(t *testing.T) { 457 // setup 458 app, ctx := createTestApp(true) 459 ctx.SetBlockHeight(1) 460 anteHandler := ante.NewAnteHandler(app.AccountKeeper, app.SupplyKeeper, ante.DefaultSigVerificationGasConsumer) 461 462 // keys and addresses 463 priv1, _, addr1 := types.KeyTestPubAddr() 464 priv2, _, addr2 := types.KeyTestPubAddr() 465 466 // set the accounts 467 acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1) 468 acc1.SetCoins(types.NewTestCoins()) 469 require.NoError(t, acc1.SetAccountNumber(0)) 470 app.AccountKeeper.SetAccount(ctx, acc1) 471 acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2) 472 acc2.SetCoins(types.NewTestCoins()) 473 require.NoError(t, acc2.SetAccountNumber(1)) 474 app.AccountKeeper.SetAccount(ctx, acc2) 475 476 var tx sdk.Tx 477 msg := types.NewTestMsg(addr1) 478 msgs := []sdk.Msg{msg} 479 fee := types.NewTestStdFee() 480 fee2 := types.NewTestStdFee() 481 fee2.Gas += 100 482 fee3 := types.NewTestStdFee() 483 fee3.Amount[0].Amount = fee3.Amount[0].Amount.Add(sdk.NewDec(100)) 484 485 // test good tx and signBytes 486 privs, accnums, seqs := []crypto.PrivKey{priv1}, []uint64{0}, []uint64{0} 487 tx = types.NewTestTx(ctx, msgs, privs, accnums, seqs, fee) 488 checkValidTx(t, anteHandler, ctx, tx, false) 489 490 chainID := ctx.ChainID() 491 chainID2 := chainID + "somemorestuff" 492 errUnauth := sdkerrors.ErrUnauthorized 493 494 cases := []struct { 495 chainID string 496 accnum uint64 497 seq uint64 498 fee types.StdFee 499 msgs []sdk.Msg 500 err error 501 }{ 502 {chainID2, 0, 1, fee, msgs, errUnauth}, // test wrong chain_id 503 {chainID, 0, 2, fee, msgs, errUnauth}, // test wrong seqs 504 {chainID, 1, 1, fee, msgs, errUnauth}, // test wrong accnum 505 {chainID, 0, 1, fee, []sdk.Msg{types.NewTestMsg(addr2)}, errUnauth}, // test wrong msg 506 {chainID, 0, 1, fee2, msgs, errUnauth}, // test wrong fee 507 {chainID, 0, 1, fee3, msgs, errUnauth}, // test wrong fee 508 } 509 510 privs, seqs = []crypto.PrivKey{priv1}, []uint64{1} 511 for _, cs := range cases { 512 tx := types.NewTestTxWithSignBytes( 513 msgs, privs, accnums, seqs, fee, 514 types.StdSignBytes(cs.chainID, cs.accnum, cs.seq, cs.fee, cs.msgs, ""), 515 "", 516 ) 517 checkInvalidTx(t, anteHandler, ctx, tx, false, cs.err) 518 } 519 520 // test wrong signer if public key exist 521 privs, accnums, seqs = []crypto.PrivKey{priv2}, []uint64{0}, []uint64{1} 522 tx = types.NewTestTx(ctx, msgs, privs, accnums, seqs, fee) 523 checkInvalidTx(t, anteHandler, ctx, tx, false, sdkerrors.ErrInvalidPubKey) 524 525 // test wrong signer if public doesn't exist 526 msg = types.NewTestMsg(addr2) 527 msgs = []sdk.Msg{msg} 528 privs, accnums, seqs = []crypto.PrivKey{priv1}, []uint64{1}, []uint64{0} 529 tx = types.NewTestTx(ctx, msgs, privs, accnums, seqs, fee) 530 checkInvalidTx(t, anteHandler, ctx, tx, false, sdkerrors.ErrInvalidPubKey) 531 } 532 533 func TestAnteHandlerSetPubKey(t *testing.T) { 534 // setup 535 app, ctx := createTestApp(true) 536 ctx.SetBlockHeight(1) 537 anteHandler := ante.NewAnteHandler(app.AccountKeeper, app.SupplyKeeper, ante.DefaultSigVerificationGasConsumer) 538 539 // keys and addresses 540 priv1, _, addr1 := types.KeyTestPubAddr() 541 _, _, addr2 := types.KeyTestPubAddr() 542 543 // set the accounts 544 acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1) 545 acc1.SetCoins(types.NewTestCoins()) 546 require.NoError(t, acc1.SetAccountNumber(0)) 547 app.AccountKeeper.SetAccount(ctx, acc1) 548 acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2) 549 acc2.SetCoins(types.NewTestCoins()) 550 require.NoError(t, acc2.SetAccountNumber(1)) 551 app.AccountKeeper.SetAccount(ctx, acc2) 552 553 var tx sdk.Tx 554 555 // test good tx and set public key 556 msg := types.NewTestMsg(addr1) 557 msgs := []sdk.Msg{msg} 558 privs, accnums, seqs := []crypto.PrivKey{priv1}, []uint64{0}, []uint64{0} 559 fee := types.NewTestStdFee() 560 tx = types.NewTestTx(ctx, msgs, privs, accnums, seqs, fee) 561 checkValidTx(t, anteHandler, ctx, tx, false) 562 563 acc1 = app.AccountKeeper.GetAccount(ctx, addr1) 564 require.Equal(t, acc1.GetPubKey(), priv1.PubKey()) 565 566 // test public key not found 567 msg = types.NewTestMsg(addr2) 568 msgs = []sdk.Msg{msg} 569 tx = types.NewTestTx(ctx, msgs, privs, []uint64{1}, seqs, fee) 570 sigs := tx.(*types.StdTx).Signatures 571 sigs[0].PubKey = nil 572 checkInvalidTx(t, anteHandler, ctx, tx, false, sdkerrors.ErrInvalidPubKey) 573 574 acc2 = app.AccountKeeper.GetAccount(ctx, addr2) 575 require.Nil(t, acc2.GetPubKey()) 576 577 // test invalid signature and public key 578 tx = types.NewTestTx(ctx, msgs, privs, []uint64{1}, seqs, fee) 579 checkInvalidTx(t, anteHandler, ctx, tx, false, sdkerrors.ErrInvalidPubKey) 580 581 acc2 = app.AccountKeeper.GetAccount(ctx, addr2) 582 require.Nil(t, acc2.GetPubKey()) 583 } 584 585 func generatePubKeysAndSignatures(n int, msg []byte, keyTypeed25519 bool) (pubkeys []crypto.PubKey, signatures [][]byte) { 586 pubkeys = make([]crypto.PubKey, n) 587 signatures = make([][]byte, n) 588 for i := 0; i < n; i++ { 589 var privkey crypto.PrivKey 590 if rand.Int63()%2 == 0 { 591 privkey = ed25519.GenPrivKey() 592 } else { 593 privkey = secp256k1.GenPrivKey() 594 } 595 pubkeys[i] = privkey.PubKey() 596 signatures[i], _ = privkey.Sign(msg) 597 } 598 return 599 } 600 601 func expectedGasCostByKeys(pubkeys []crypto.PubKey) uint64 { 602 cost := uint64(0) 603 for _, pubkey := range pubkeys { 604 pubkeyType := strings.ToLower(fmt.Sprintf("%T", pubkey)) 605 switch { 606 case strings.Contains(pubkeyType, "ed25519"): 607 cost += types.DefaultParams().SigVerifyCostED25519 608 case strings.Contains(pubkeyType, "secp256k1"): 609 cost += types.DefaultParams().SigVerifyCostSecp256k1 610 default: 611 panic("unexpected key type") 612 } 613 } 614 return cost 615 } 616 617 func TestCountSubkeys(t *testing.T) { 618 genPubKeys := func(n int) []crypto.PubKey { 619 var ret []crypto.PubKey 620 for i := 0; i < n; i++ { 621 ret = append(ret, secp256k1.GenPrivKey().PubKey()) 622 } 623 return ret 624 } 625 singleKey := secp256k1.GenPrivKey().PubKey() 626 singleLevelMultiKey := multisig.NewPubKeyMultisigThreshold(4, genPubKeys(5)) 627 multiLevelSubKey1 := multisig.NewPubKeyMultisigThreshold(4, genPubKeys(5)) 628 multiLevelSubKey2 := multisig.NewPubKeyMultisigThreshold(4, genPubKeys(5)) 629 multiLevelMultiKey := multisig.NewPubKeyMultisigThreshold(2, []crypto.PubKey{ 630 multiLevelSubKey1, multiLevelSubKey2, secp256k1.GenPrivKey().PubKey()}) 631 type args struct { 632 pub crypto.PubKey 633 } 634 tests := []struct { 635 name string 636 args args 637 want int 638 }{ 639 {"single key", args{singleKey}, 1}, 640 {"single level multikey", args{singleLevelMultiKey}, 5}, 641 {"multi level multikey", args{multiLevelMultiKey}, 11}, 642 } 643 for _, tt := range tests { 644 tt := tt 645 t.Run(tt.name, func(T *testing.T) { 646 require.Equal(t, tt.want, types.CountSubKeys(tt.args.pub)) 647 }) 648 } 649 } 650 651 func TestAnteHandlerSigLimitExceeded(t *testing.T) { 652 // setup 653 app, ctx := createTestApp(true) 654 ctx.SetBlockHeight(1) 655 anteHandler := ante.NewAnteHandler(app.AccountKeeper, app.SupplyKeeper, ante.DefaultSigVerificationGasConsumer) 656 657 // keys and addresses 658 priv1, _, addr1 := types.KeyTestPubAddr() 659 priv2, _, addr2 := types.KeyTestPubAddr() 660 priv3, _, addr3 := types.KeyTestPubAddr() 661 priv4, _, addr4 := types.KeyTestPubAddr() 662 priv5, _, addr5 := types.KeyTestPubAddr() 663 priv6, _, addr6 := types.KeyTestPubAddr() 664 priv7, _, addr7 := types.KeyTestPubAddr() 665 priv8, _, addr8 := types.KeyTestPubAddr() 666 667 addrs := []sdk.AccAddress{addr1, addr2, addr3, addr4, addr5, addr6, addr7, addr8} 668 669 // set the accounts 670 for i, addr := range addrs { 671 acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr) 672 acc.SetCoins(types.NewTestCoins()) 673 acc.SetAccountNumber(uint64(i)) 674 app.AccountKeeper.SetAccount(ctx, acc) 675 } 676 677 var tx sdk.Tx 678 msg := types.NewTestMsg(addr1, addr2, addr3, addr4, addr5, addr6, addr7, addr8) 679 msgs := []sdk.Msg{msg} 680 fee := types.NewTestStdFee() 681 682 // test rejection logic 683 privs, accnums, seqs := []crypto.PrivKey{priv1, priv2, priv3, priv4, priv5, priv6, priv7, priv8}, 684 []uint64{0, 1, 2, 3, 4, 5, 6, 7}, []uint64{0, 0, 0, 0, 0, 0, 0, 0} 685 tx = types.NewTestTx(ctx, msgs, privs, accnums, seqs, fee) 686 checkInvalidTx(t, anteHandler, ctx, tx, false, sdkerrors.ErrTooManySignatures) 687 } 688 689 // Test custom SignatureVerificationGasConsumer 690 func TestCustomSignatureVerificationGasConsumer(t *testing.T) { 691 // setup 692 app, ctx := createTestApp(true) 693 ctx.SetBlockHeight(1) 694 // setup an ante handler that only accepts PubKeyEd25519 695 anteHandler := ante.NewAnteHandler(app.AccountKeeper, app.SupplyKeeper, func(meter sdk.GasMeter, sig []byte, pubkey crypto.PubKey, params types.Params) error { 696 switch pubkey := pubkey.(type) { 697 case ed25519.PubKeyEd25519: 698 meter.ConsumeGas(params.SigVerifyCostED25519, "ante verify: ed25519") 699 return nil 700 default: 701 return sdkerrors.Wrapf(sdkerrors.ErrInvalidPubKey, "unrecognized public key type: %T", pubkey) 702 } 703 }) 704 705 // verify that an secp256k1 account gets rejected 706 priv1, _, addr1 := types.KeyTestPubAddr() 707 acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1) 708 _ = acc1.SetCoins(sdk.NewCoins(sdk.NewInt64Coin("atom", 150))) 709 app.AccountKeeper.SetAccount(ctx, acc1) 710 711 var tx sdk.Tx 712 msg := types.NewTestMsg(addr1) 713 privs, accnums, seqs := []crypto.PrivKey{priv1}, []uint64{0}, []uint64{0} 714 fee := types.NewTestStdFee() 715 msgs := []sdk.Msg{msg} 716 tx = types.NewTestTx(ctx, msgs, privs, accnums, seqs, fee) 717 checkInvalidTx(t, anteHandler, ctx, tx, false, sdkerrors.ErrInvalidPubKey) 718 719 // verify that an ed25519 account gets accepted 720 priv2 := ed25519.GenPrivKey() 721 pub2 := priv2.PubKey() 722 addr2 := sdk.AccAddress(pub2.Address()) 723 acc2 := app.AccountKeeper.NewAccountWithAddress(ctx, addr2) 724 require.NoError(t, acc2.SetCoins(sdk.NewCoins(sdk.NewInt64Coin("atom", 150)))) 725 require.NoError(t, acc2.SetAccountNumber(1)) 726 app.AccountKeeper.SetAccount(ctx, acc2) 727 msg = types.NewTestMsg(addr2) 728 privs, accnums, seqs = []crypto.PrivKey{priv2}, []uint64{1}, []uint64{0} 729 fee = types.NewTestStdFee() 730 msgs = []sdk.Msg{msg} 731 tx = types.NewTestTx(ctx, msgs, privs, accnums, seqs, fee) 732 checkValidTx(t, anteHandler, ctx, tx, false) 733 } 734 735 func TestAnteHandlerReCheck(t *testing.T) { 736 // setup 737 app, ctx := createTestApp(true) 738 // set blockheight and recheck=true 739 ctx.SetBlockHeight(1) 740 ctx.SetIsReCheckTx(true) 741 742 // keys and addresses 743 priv1, _, addr1 := types.KeyTestPubAddr() 744 // priv2, _, addr2 := types.KeyTestPubAddr() 745 746 // set the accounts 747 acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1) 748 acc1.SetCoins(types.NewTestCoins()) 749 require.NoError(t, acc1.SetAccountNumber(0)) 750 app.AccountKeeper.SetAccount(ctx, acc1) 751 752 antehandler := ante.NewAnteHandler(app.AccountKeeper, app.SupplyKeeper, ante.DefaultSigVerificationGasConsumer) 753 754 // test that operations skipped on recheck do not run 755 756 msg := types.NewTestMsg(addr1) 757 msgs := []sdk.Msg{msg} 758 fee := types.NewTestStdFee() 759 760 privs, accnums, seqs := []crypto.PrivKey{priv1}, []uint64{0}, []uint64{0} 761 tx := types.NewTestTxWithMemo(ctx, msgs, privs, accnums, seqs, fee, "thisisatestmemo") 762 763 // make signature array empty which would normally cause ValidateBasicDecorator and SigVerificationDecorator fail 764 // since these decorators don't run on recheck, the tx should pass the antehandler 765 stdTx := tx.(*types.StdTx) 766 stdTx.Signatures = []types.StdSignature{} 767 768 _, err := antehandler(ctx, stdTx, false) 769 require.Nil(t, err, "AnteHandler errored on recheck unexpectedly: %v", err) 770 771 tx = types.NewTestTxWithMemo(ctx, msgs, privs, accnums, seqs, fee, "thisisatestmemo") 772 txBytes, err := json.Marshal(tx) 773 require.Nil(t, err, "Error marshalling tx: %v", err) 774 ctx.SetTxBytes(txBytes) 775 776 // require that state machine param-dependent checking is still run on recheck since parameters can change between check and recheck 777 testCases := []struct { 778 name string 779 params types.Params 780 }{ 781 {"memo size check", types.NewParams(1, types.DefaultTxSigLimit, types.DefaultTxSizeCostPerByte, types.DefaultSigVerifyCostED25519, types.DefaultSigVerifyCostSecp256k1)}, 782 {"txsize check", types.NewParams(types.DefaultMaxMemoCharacters, types.DefaultTxSigLimit, 10000000, types.DefaultSigVerifyCostED25519, types.DefaultSigVerifyCostSecp256k1)}, 783 {"sig verify cost check", types.NewParams(types.DefaultMaxMemoCharacters, types.DefaultTxSigLimit, types.DefaultTxSizeCostPerByte, types.DefaultSigVerifyCostED25519, 100000000)}, 784 } 785 for _, tc := range testCases { 786 // set testcase parameters 787 app.AccountKeeper.SetParams(ctx, tc.params) 788 789 _, err := antehandler(ctx, tx, false) 790 791 require.NotNil(t, err, "tx does not fail on recheck with updated params in test case: %s", tc.name) 792 793 // reset parameters to default values 794 app.AccountKeeper.SetParams(ctx, types.DefaultParams()) 795 } 796 797 // require that local mempool fee check is still run on recheck since validator may change minFee between check and recheck 798 // create new minimum gas price so antehandler fails on recheck 799 ctx.SetMinGasPrices([]sdk.DecCoin{{ 800 Denom: "dnecoin", // fee does not have this denom 801 Amount: sdk.NewDec(5), 802 }}) 803 _, err = antehandler(ctx, tx, false) 804 require.NotNil(t, err, "antehandler on recheck did not fail when mingasPrice was changed") 805 // reset min gasprice 806 ctx.SetMinGasPrices(sdk.DecCoins{}) 807 808 // remove funds for account so antehandler fails on recheck 809 acc1.SetCoins(sdk.Coins{}) 810 app.AccountKeeper.SetAccount(ctx, acc1) 811 812 _, err = antehandler(ctx, tx, false) 813 require.NotNil(t, err, "antehandler on recheck did not fail once feePayer no longer has sufficient funds") 814 }