From b8ad088747daabb6af396d4253ef77be0e7bdfd7 Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Thu, 19 Sep 2024 10:28:37 +0800 Subject: [PATCH 1/8] tests folder and move preeip155, 1559 tests into --- .../src/evm_circuit/execution/begin_tx.rs | 75 ------------ .../src/evm_circuit/tests/eip1559.rs | 108 ++++++++++++++++++ .../src/evm_circuit/tests/pre-eip155.rs | 99 ++++++++++++++++ .../util/common_gadget/tx_eip1559.rs | 108 ------------------ 4 files changed, 207 insertions(+), 183 deletions(-) create mode 100644 zkevm-circuits/src/evm_circuit/tests/eip1559.rs create mode 100644 zkevm-circuits/src/evm_circuit/tests/pre-eip155.rs diff --git a/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs b/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs index 07ddda9764..c4228fd996 100644 --- a/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs +++ b/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs @@ -1738,79 +1738,4 @@ mod test { .block_modifier(Box::new(|block| block.circuits_params.max_txs = 3)) .run(); } - - // Note: all pre-eip155 txs here for testing have signature data. don't need to generate signature dynamically - // because ethers-rs lib's helper `sign_transaction_sync` doesn't support pre-eip155 type. - #[test] - fn test_legacy_tx_pre_eip155() { - let mut tx1 = MockTransaction::default(); - // pre-eip155 tx1 downloaded from [etherscan](https://etherscan.io/getRawTx?tx=0x9cd2288e69623b109e25edc46bc518156498b521e5c162d96e1ab392ff1d9dff) - // tx with signature::v =0x1c (28). - let sig_data1 = ( - 0x1c_u64, - word!("0x90b751c5870e9bc071c8d6b2bf1ee80f36ee7efd8e6fbabaa25bd3b8b68cfe9b"), - word!("0x79c25a01f12493a6d35f1330306d4e3c4e782fcbffc64c6809959577f41ff248"), - ); - - tx1 - .from(address!("0xcf40d0d2b44f2b66e07cace1372ca42b73cf21a3")) - .nonce(word!("0x2ea8")) - .gas_price(word!("0x098bca5a00")) - .gas(word!("0x0249f0")) - .value(word!("0x00")) - // Set tx type to pre-eip155. - .transaction_type(0) - .input(hex::decode("606060405260008054600160a060020a0319163317905560f2806100236000396000f3606060405260e060020a6000350463f5537ede8114601c575b6002565b3460025760f06004356024356044356000805433600160a060020a039081169116141560ea5783905080600160a060020a031663a9059cbb84846000604051602001526040518360e060020a0281526004018083600160a060020a0316815260200182815260200192505050602060405180830381600087803b1560025760325a03f1156002575050604080518481529051600160a060020a0386811693508716917fd0ed88a3f042c6bbb1e3ea406079b5f2b4b198afccaa535d837f4c63abbc4de6919081900360200190a35b50505050565b00") - .expect("hex data can be decoded").into()) - .sig_data(sig_data1); - - // pre-eip155 tx2 refers to https://github.com/scroll-tech/go-ethereum/blob/develop/cmd/evm/testdata/3/txs.json. - let mut tx2 = MockTransaction::default(); - // tx with signature::v =0x1b (27). - let sig_data2 = ( - 0x1b_u64, - word!("0x88544c93a564b4c28d2ffac2074a0c55fdd4658fe0d215596ed2e32e3ef7f56b"), - word!("0x7fb4075d54190f825d7c47bb820284757b34fd6293904a93cddb1d3aa961ac28"), - ); - - tx2.from(address!("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b")) - .to(address!("0x095e7baea6a6c7c4c2dfeb977efac326af552d87")) - .nonce(word!("0x0")) - .gas_price(word!("0x1")) - .gas(word!("0x5f5e100")) - .value(word!("0x186a0")) - // Set tx type to pre-eip155. - .transaction_type(0) - .sig_data(sig_data2); - - for tx in [tx1, tx2] { - let ctx = build_legacy_ctx(gwei(8_000_000), &tx).unwrap(); - CircuitTestBuilder::new_from_test_ctx(ctx) - .params(CircuitsParams { - max_calldata: 300, - ..Default::default() - }) - .run() - } - } - - // build pre-eip155 tx - fn build_legacy_ctx( - sender_balance: Word, - tx: &MockTransaction, - ) -> Result, Error> { - TestContext::new( - None, - |accs| { - accs[0] - .address(tx.from.address()) - .balance(sender_balance) - .nonce(tx.nonce); - }, - |mut txs, _accs| { - txs[0].clone_from(tx); - }, - |block, _tx| block.number(0xcafeu64), - ) - } } diff --git a/zkevm-circuits/src/evm_circuit/tests/eip1559.rs b/zkevm-circuits/src/evm_circuit/tests/eip1559.rs new file mode 100644 index 0000000000..8a47ad2828 --- /dev/null +++ b/zkevm-circuits/src/evm_circuit/tests/eip1559.rs @@ -0,0 +1,108 @@ + +#[cfg(test)] +mod test { + use crate::test_util::CircuitTestBuilder; + use eth_types::{Error, Word}; + use ethers_signers::Signer; + use mock::{eth, gwei, TestContext, MOCK_ACCOUNTS, MOCK_WALLETS}; + + #[test] + fn test_eip1559_tx_for_equal_balance() { + let balance = if cfg!(feature = "scroll") { + // l1 fee + gwei(80_000) + Word::from(279u64) + } else { + gwei(80_000) + }; + let ctx = build_ctx(balance, gwei(2), gwei(2)).unwrap(); + CircuitTestBuilder::new_from_test_ctx(ctx).run(); + } + + #[test] + fn test_eip1559_tx_for_less_balance() { + let res = build_ctx(gwei(79_999), gwei(2), gwei(2)); + + #[cfg(not(feature = "scroll"))] + let expected_err = "Failed to run Trace, err: Failed to apply config.Transactions[0]: insufficient funds for gas * price + value: address 0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241 have 79999000000000 want 80000000000000"; + + // "80000000000279": 279 is l1 fee + #[cfg(feature = "scroll")] + let expected_err = "Failed to run Trace, err: insufficient funds for gas * price + value: address 0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241 have 79999000000000 want 80000000000279"; + + // Address `0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241` in error message comes from + // MOCK_WALLETS[0] in build_ctx. + + // Return a tracing error if insufficient sender balance. + if let Error::TracingError(err) = res.unwrap_err() { + assert_eq!(err, expected_err); + } else { + panic!("Must be a tracing error"); + } + } + + #[test] + fn test_eip1559_tx_for_more_balance() { + let ctx = build_ctx(gwei(80_001), gwei(2), gwei(2)).unwrap(); + CircuitTestBuilder::new_from_test_ctx(ctx).run(); + } + + #[test] + fn test_eip1559_tx_for_gas_fee_cap_gt_gas_tip_cap() { + // Should be successful if `max_fee_per_gas > max_priority_fee_per_gas`. + let balance = if cfg!(feature = "scroll") { + // l1 fee + gwei(80_000) + Word::from(279u64) + } else { + gwei(80_000) + }; + let ctx = build_ctx(balance, gwei(2), gwei(1)).unwrap(); + + CircuitTestBuilder::new_from_test_ctx(ctx).run(); + } + + #[test] + fn test_eip1559_tx_for_gas_fee_cap_lt_gas_tip_cap() { + let res = build_ctx(gwei(80_000), gwei(1), gwei(2)); + + #[cfg(not(feature = "scroll"))] + let expected_err = "Failed to run Trace, err: Failed to apply config.Transactions[0]: max priority fee per gas higher than max fee per gas: address 0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241, maxPriorityFeePerGas: 2000000000, maxFeePerGas: 1000000000"; + #[cfg(feature = "scroll")] + let expected_err = "Failed to run Trace, err: max priority fee per gas higher than max fee per gas: address 0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241, maxPriorityFeePerGas: 2000000000, maxFeePerGas: 1000000000"; + // Address `0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241` in error message comes from + // MOCK_WALLETS[0] in build_ctx. + + // Return a tracing error if `max_fee_per_gas < max_priority_fee_per_gas`. + if let Error::TracingError(err) = res.unwrap_err() { + assert_eq!(err, expected_err); + } else { + panic!("Must be a tracing error"); + } + } + + fn build_ctx( + sender_balance: Word, + max_fee_per_gas: Word, + max_priority_fee_per_gas: Word, + ) -> Result, Error> { + TestContext::new( + None, + |accs| { + accs[0] + .address(MOCK_WALLETS[0].address()) + .balance(sender_balance); + accs[1].address(MOCK_ACCOUNTS[0]).balance(eth(1)); + }, + |mut txs, _accs| { + txs[0] + .from(MOCK_WALLETS[0].clone()) + .to(MOCK_ACCOUNTS[0]) + .gas(30_000.into()) + .value(gwei(20_000)) + .max_fee_per_gas(max_fee_per_gas) + .max_priority_fee_per_gas(max_priority_fee_per_gas) + .transaction_type(2); // Set tx type to EIP-1559. + }, + |block, _tx| block.number(0xcafeu64), + ) + } +} diff --git a/zkevm-circuits/src/evm_circuit/tests/pre-eip155.rs b/zkevm-circuits/src/evm_circuit/tests/pre-eip155.rs new file mode 100644 index 0000000000..968bd5b08a --- /dev/null +++ b/zkevm-circuits/src/evm_circuit/tests/pre-eip155.rs @@ -0,0 +1,99 @@ +#[cfg(test)] +mod test { + use std::{str::FromStr, vec}; + + use crate::{evm_circuit::test::rand_bytes, test_util::CircuitTestBuilder}; + use bus_mapping::{circuit_input_builder::CircuitsParams, evm::OpcodeId}; + use eth_types::{ + self, address, bytecode, evm_types::GasCost, word, Address, Bytecode, Error, Hash, Word, + U256, + }; + use ethers_core::{types::Bytes, utils::get_contract_address}; + use mock::{eth, gwei, MockTransaction, TestContext, MOCK_ACCOUNTS}; + + fn gas(call_data: &[u8]) -> Word { + Word::from( + GasCost::TX.as_u64() + + 2 * OpcodeId::PUSH32.constant_gas_cost().as_u64() + + call_data + .iter() + .map(|&x| if x == 0 { 4 } else { 16 }) + .sum::(), + ) + } + + // Note: all pre-eip155 txs here for testing have signature data. don't need to generate signature dynamically + // because ethers-rs lib's helper `sign_transaction_sync` doesn't support pre-eip155 type. + #[test] + fn test_legacy_tx_pre_eip155() { + let mut tx1 = MockTransaction::default(); + // pre-eip155 tx1 downloaded from [etherscan](https://etherscan.io/getRawTx?tx=0x9cd2288e69623b109e25edc46bc518156498b521e5c162d96e1ab392ff1d9dff) + // tx with signature::v =0x1c (28). + let sig_data1 = ( + 0x1c_u64, + word!("0x90b751c5870e9bc071c8d6b2bf1ee80f36ee7efd8e6fbabaa25bd3b8b68cfe9b"), + word!("0x79c25a01f12493a6d35f1330306d4e3c4e782fcbffc64c6809959577f41ff248"), + ); + + tx1 + .from(address!("0xcf40d0d2b44f2b66e07cace1372ca42b73cf21a3")) + .nonce(word!("0x2ea8")) + .gas_price(word!("0x098bca5a00")) + .gas(word!("0x0249f0")) + .value(word!("0x00")) + // Set tx type to pre-eip155. + .transaction_type(0) + .input(hex::decode("606060405260008054600160a060020a0319163317905560f2806100236000396000f3606060405260e060020a6000350463f5537ede8114601c575b6002565b3460025760f06004356024356044356000805433600160a060020a039081169116141560ea5783905080600160a060020a031663a9059cbb84846000604051602001526040518360e060020a0281526004018083600160a060020a0316815260200182815260200192505050602060405180830381600087803b1560025760325a03f1156002575050604080518481529051600160a060020a0386811693508716917fd0ed88a3f042c6bbb1e3ea406079b5f2b4b198afccaa535d837f4c63abbc4de6919081900360200190a35b50505050565b00") + .expect("hex data can be decoded").into()) + .sig_data(sig_data1); + + // pre-eip155 tx2 refers to https://github.com/scroll-tech/go-ethereum/blob/develop/cmd/evm/testdata/3/txs.json. + let mut tx2 = MockTransaction::default(); + // tx with signature::v =0x1b (27). + let sig_data2 = ( + 0x1b_u64, + word!("0x88544c93a564b4c28d2ffac2074a0c55fdd4658fe0d215596ed2e32e3ef7f56b"), + word!("0x7fb4075d54190f825d7c47bb820284757b34fd6293904a93cddb1d3aa961ac28"), + ); + + tx2.from(address!("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b")) + .to(address!("0x095e7baea6a6c7c4c2dfeb977efac326af552d87")) + .nonce(word!("0x0")) + .gas_price(word!("0x1")) + .gas(word!("0x5f5e100")) + .value(word!("0x186a0")) + // Set tx type to pre-eip155. + .transaction_type(0) + .sig_data(sig_data2); + + for tx in [tx1, tx2] { + let ctx = build_legacy_ctx(gwei(8_000_000), &tx).unwrap(); + CircuitTestBuilder::new_from_test_ctx(ctx) + .params(CircuitsParams { + max_calldata: 300, + ..Default::default() + }) + .run() + } + } + + // build pre-eip155 tx + fn build_legacy_ctx( + sender_balance: Word, + tx: &MockTransaction, + ) -> Result, Error> { + TestContext::new( + None, + |accs| { + accs[0] + .address(tx.from.address()) + .balance(sender_balance) + .nonce(tx.nonce); + }, + |mut txs, _accs| { + txs[0].clone_from(tx); + }, + |block, _tx| block.number(0xcafeu64), + ) + } +} diff --git a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_eip1559.rs b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_eip1559.rs index d78e6a4de3..f3b0745e80 100644 --- a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_eip1559.rs +++ b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_eip1559.rs @@ -237,111 +237,3 @@ impl TxEip1559Gadget { .assign(region, offset, tx.max_fee_per_gas, base_fee) } } - -#[cfg(test)] -mod test { - use crate::test_util::CircuitTestBuilder; - use eth_types::{Error, Word}; - use ethers_signers::Signer; - use mock::{eth, gwei, TestContext, MOCK_ACCOUNTS, MOCK_WALLETS}; - - #[test] - fn test_eip1559_tx_for_equal_balance() { - let balance = if cfg!(feature = "scroll") { - // l1 fee - gwei(80_000) + Word::from(279u64) - } else { - gwei(80_000) - }; - let ctx = build_ctx(balance, gwei(2), gwei(2)).unwrap(); - CircuitTestBuilder::new_from_test_ctx(ctx).run(); - } - - #[test] - fn test_eip1559_tx_for_less_balance() { - let res = build_ctx(gwei(79_999), gwei(2), gwei(2)); - - #[cfg(not(feature = "scroll"))] - let expected_err = "Failed to run Trace, err: Failed to apply config.Transactions[0]: insufficient funds for gas * price + value: address 0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241 have 79999000000000 want 80000000000000"; - - // "80000000000279": 279 is l1 fee - #[cfg(feature = "scroll")] - let expected_err = "Failed to run Trace, err: insufficient funds for gas * price + value: address 0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241 have 79999000000000 want 80000000000279"; - - // Address `0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241` in error message comes from - // MOCK_WALLETS[0] in build_ctx. - - // Return a tracing error if insufficient sender balance. - if let Error::TracingError(err) = res.unwrap_err() { - assert_eq!(err, expected_err); - } else { - panic!("Must be a tracing error"); - } - } - - #[test] - fn test_eip1559_tx_for_more_balance() { - let ctx = build_ctx(gwei(80_001), gwei(2), gwei(2)).unwrap(); - CircuitTestBuilder::new_from_test_ctx(ctx).run(); - } - - #[test] - fn test_eip1559_tx_for_gas_fee_cap_gt_gas_tip_cap() { - // Should be successful if `max_fee_per_gas > max_priority_fee_per_gas`. - let balance = if cfg!(feature = "scroll") { - // l1 fee - gwei(80_000) + Word::from(279u64) - } else { - gwei(80_000) - }; - let ctx = build_ctx(balance, gwei(2), gwei(1)).unwrap(); - - CircuitTestBuilder::new_from_test_ctx(ctx).run(); - } - - #[test] - fn test_eip1559_tx_for_gas_fee_cap_lt_gas_tip_cap() { - let res = build_ctx(gwei(80_000), gwei(1), gwei(2)); - - #[cfg(not(feature = "scroll"))] - let expected_err = "Failed to run Trace, err: Failed to apply config.Transactions[0]: max priority fee per gas higher than max fee per gas: address 0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241, maxPriorityFeePerGas: 2000000000, maxFeePerGas: 1000000000"; - #[cfg(feature = "scroll")] - let expected_err = "Failed to run Trace, err: max priority fee per gas higher than max fee per gas: address 0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241, maxPriorityFeePerGas: 2000000000, maxFeePerGas: 1000000000"; - // Address `0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241` in error message comes from - // MOCK_WALLETS[0] in build_ctx. - - // Return a tracing error if `max_fee_per_gas < max_priority_fee_per_gas`. - if let Error::TracingError(err) = res.unwrap_err() { - assert_eq!(err, expected_err); - } else { - panic!("Must be a tracing error"); - } - } - - fn build_ctx( - sender_balance: Word, - max_fee_per_gas: Word, - max_priority_fee_per_gas: Word, - ) -> Result, Error> { - TestContext::new( - None, - |accs| { - accs[0] - .address(MOCK_WALLETS[0].address()) - .balance(sender_balance); - accs[1].address(MOCK_ACCOUNTS[0]).balance(eth(1)); - }, - |mut txs, _accs| { - txs[0] - .from(MOCK_WALLETS[0].clone()) - .to(MOCK_ACCOUNTS[0]) - .gas(30_000.into()) - .value(gwei(20_000)) - .max_fee_per_gas(max_fee_per_gas) - .max_priority_fee_per_gas(max_priority_fee_per_gas) - .transaction_type(2); // Set tx type to EIP-1559. - }, - |block, _tx| block.number(0xcafeu64), - ) - } -} From 143d7ae58b8dab4cc0a7c42dab0e53b9e8d24a62 Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Thu, 19 Sep 2024 10:46:53 +0800 Subject: [PATCH 2/8] move 2930 tests --- .../src/evm_circuit/tests/eip2930.rs | 97 +++++++++++++++++ .../{tx_access_list.rs => tx_2930.rs} | 100 ------------------ 2 files changed, 97 insertions(+), 100 deletions(-) create mode 100644 zkevm-circuits/src/evm_circuit/tests/eip2930.rs rename zkevm-circuits/src/evm_circuit/util/common_gadget/{tx_access_list.rs => tx_2930.rs} (59%) diff --git a/zkevm-circuits/src/evm_circuit/tests/eip2930.rs b/zkevm-circuits/src/evm_circuit/tests/eip2930.rs new file mode 100644 index 0000000000..0ea667763f --- /dev/null +++ b/zkevm-circuits/src/evm_circuit/tests/eip2930.rs @@ -0,0 +1,97 @@ +// tests for eip2930 +#[cfg(test)] +mod test { + use crate::test_util::CircuitTestBuilder; + use eth_types::{address, AccessList, AccessListItem, Error, Word, H256}; + use ethers_signers::Signer; + use mock::{eth, gwei, TestContext, MOCK_ACCOUNTS, MOCK_WALLETS}; + + // test with empty access list. + #[test] + fn test_eip2930_tx_for_empty_access_list() { + // CASE1: tx not set access list, `access_list` field is none. + let ctx = build_ctx(gwei(80_000), None).unwrap(); + CircuitTestBuilder::new_from_test_ctx(ctx).run(); + + // CASE2: tx set empty (neither address nor storage keys at all) access list into + // `access_list` field. this field is not none. + let test_access_list: AccessList = AccessList(vec![]); + + let ctx = build_ctx(gwei(80_000), Some(test_access_list)).unwrap(); + CircuitTestBuilder::new_from_test_ctx(ctx).run(); + } + + // test with non empty access list(address + storage keys list) + #[test] + fn test_eip2930_non_empty_access_list() { + let test_access_list: AccessList = AccessList(vec![ + AccessListItem { + address: address!("0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241"), + // one storage key + storage_keys: [10].map(H256::from_low_u64_be).to_vec(), + }, + AccessListItem { + address: address!("0x0000000000000000000000000000000000001111"), + // two storage keys + storage_keys: [10, 11].map(H256::from_low_u64_be).to_vec(), + }, + AccessListItem { + address: address!("0x0000000000000000000000000000000000002222"), + // three storage keys + storage_keys: [20, 22, 50].map(H256::from_low_u64_be).to_vec(), + }, + ]); + + let ctx = build_ctx(gwei(80_000), Some(test_access_list)).unwrap(); + CircuitTestBuilder::new_from_test_ctx(ctx).run(); + } + + // test with non empty access list(only address list) + #[test] + fn test_eip2930_only_address_access_list() { + let test_access_list: AccessList = AccessList(vec![ + AccessListItem { + address: address!("0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241"), + // no storage keys + storage_keys: Vec::new(), + }, + AccessListItem { + address: address!("0x0000000000000000000000000000000000001111"), + // no storage keys + storage_keys: Vec::new(), + }, + ]); + + let ctx = build_ctx(gwei(80_000), Some(test_access_list)).unwrap(); + CircuitTestBuilder::new_from_test_ctx(ctx).run(); + } + + fn build_ctx( + sender_balance: Word, + access_list: Option, + ) -> Result, Error> { + TestContext::new( + None, + |accs| { + accs[0] + .address(MOCK_WALLETS[0].address()) + .balance(sender_balance); + accs[1].address(MOCK_ACCOUNTS[0]).balance(eth(1)); + }, + |mut txs, _accs| { + txs[0] + .from(MOCK_WALLETS[0].clone()) + .to(MOCK_ACCOUNTS[0]) + .gas(40_000.into()) + .gas_price(30_000.into()) + .value(gwei(20_000)) + .transaction_type(1); // Set tx type to EIP-2930. + + if let Some(acc_list) = access_list { + txs[0].access_list(acc_list); + } + }, + |block, _tx| block.number(0xcafeu64), + ) + } +} diff --git a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_access_list.rs b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_2930.rs similarity index 59% rename from zkevm-circuits/src/evm_circuit/util/common_gadget/tx_access_list.rs rename to zkevm-circuits/src/evm_circuit/util/common_gadget/tx_2930.rs index 5dcc3363d0..41d2626a1b 100644 --- a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_access_list.rs +++ b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_2930.rs @@ -156,103 +156,3 @@ impl TxAccessListGadget { address_len + storage_key_len } } - -// tests for eip2930 -#[cfg(test)] -mod test { - use crate::test_util::CircuitTestBuilder; - use eth_types::{address, AccessList, AccessListItem, Error, Word, H256}; - use ethers_signers::Signer; - use mock::{eth, gwei, TestContext, MOCK_ACCOUNTS, MOCK_WALLETS}; - - // test with empty access list. - #[test] - fn test_eip2930_tx_for_empty_access_list() { - // CASE1: tx not set access list, `access_list` field is none. - let ctx = build_ctx(gwei(80_000), None).unwrap(); - CircuitTestBuilder::new_from_test_ctx(ctx).run(); - - // CASE2: tx set empty (neither address nor storage keys at all) access list into - // `access_list` field. this field is not none. - let test_access_list: AccessList = AccessList(vec![]); - - let ctx = build_ctx(gwei(80_000), Some(test_access_list)).unwrap(); - CircuitTestBuilder::new_from_test_ctx(ctx).run(); - } - - // test with non empty access list(address + storage keys list) - #[test] - fn test_eip2930_non_empty_access_list() { - let test_access_list: AccessList = AccessList(vec![ - AccessListItem { - address: address!("0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241"), - // one storage key - storage_keys: [10].map(H256::from_low_u64_be).to_vec(), - }, - AccessListItem { - address: address!("0x0000000000000000000000000000000000001111"), - // two storage keys - storage_keys: [10, 11].map(H256::from_low_u64_be).to_vec(), - }, - AccessListItem { - address: address!("0x0000000000000000000000000000000000002222"), - // three storage keys - storage_keys: [20, 22, 50].map(H256::from_low_u64_be).to_vec(), - }, - ]); - - let ctx = build_ctx(gwei(80_000), Some(test_access_list)).unwrap(); - CircuitTestBuilder::new_from_test_ctx(ctx).run(); - } - - // test with non empty access list(only address list) - #[test] - fn test_eip2930_only_address_access_list() { - let test_access_list: AccessList = AccessList(vec![ - AccessListItem { - address: address!("0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241"), - // no storage keys - storage_keys: Vec::new(), - }, - AccessListItem { - address: address!("0x0000000000000000000000000000000000001111"), - // no storage keys - storage_keys: Vec::new(), - }, - ]); - - let ctx = build_ctx(gwei(80_000), Some(test_access_list)).unwrap(); - CircuitTestBuilder::new_from_test_ctx(ctx).run(); - } - - // TODO: check if other types' test required. - - fn build_ctx( - sender_balance: Word, - access_list: Option, - ) -> Result, Error> { - TestContext::new( - None, - |accs| { - accs[0] - .address(MOCK_WALLETS[0].address()) - .balance(sender_balance); - accs[1].address(MOCK_ACCOUNTS[0]).balance(eth(1)); - }, - |mut txs, _accs| { - txs[0] - .from(MOCK_WALLETS[0].clone()) - .to(MOCK_ACCOUNTS[0]) - .gas(40_000.into()) - .gas_price(30_000.into()) - .value(gwei(20_000)) - .transaction_type(1); // Set tx type to EIP-2930. - - if let Some(acc_list) = access_list { - txs[0].access_list(acc_list); - } - }, - |block, _tx| block.number(0xcafeu64), - ) - } -} From 60280f3236e85cf97cecb72e3da1f0ed718e73b1 Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Thu, 19 Sep 2024 12:08:23 +0800 Subject: [PATCH 3/8] add eip155 --- .../src/evm_circuit/tests/eip155.rs | 39 +++++++++++++++++++ .../src/evm_circuit/tests/pre-eip155.rs | 11 ------ .../src/evm_circuit/util/common_gadget.rs | 4 +- .../{tx_2930.rs => tx_eip2930.rs} | 0 4 files changed, 41 insertions(+), 13 deletions(-) create mode 100644 zkevm-circuits/src/evm_circuit/tests/eip155.rs rename zkevm-circuits/src/evm_circuit/util/common_gadget/{tx_2930.rs => tx_eip2930.rs} (100%) diff --git a/zkevm-circuits/src/evm_circuit/tests/eip155.rs b/zkevm-circuits/src/evm_circuit/tests/eip155.rs new file mode 100644 index 0000000000..023e418643 --- /dev/null +++ b/zkevm-circuits/src/evm_circuit/tests/eip155.rs @@ -0,0 +1,39 @@ +// tests for eip155 tx +#[cfg(test)] +mod test { + use crate::test_util::CircuitTestBuilder; + use eth_types::{address, AccessList, AccessListItem, Error, Word, H256}; + use ethers_signers::Signer; + use mock::{eth, gwei, TestContext, MOCK_ACCOUNTS, MOCK_WALLETS}; + + // test with empty access list. + #[test] + fn test_eip2930_tx_for_empty_access_list() { + let ctx = build_ctx(gwei(80_000)).unwrap(); + } + + fn build_ctx( + sender_balance: Word, + ) -> Result, Error> { + TestContext::new( + None, + |accs| { + accs[0] + .address(MOCK_WALLETS[0].address()) + .balance(sender_balance); + accs[1].address(MOCK_ACCOUNTS[0]).balance(eth(1)); + }, + |mut txs, _accs| { + txs[0] + .from(MOCK_WALLETS[0].clone()) + .to(MOCK_ACCOUNTS[0]) + .gas(40_000.into()) + .gas_price(30_000.into()) + .value(gwei(20_000)) + // Set tx type to EIP-155. + .transaction_type(0); + }, + |block, _tx| block.number(0xcafeu64), + ) + } +} diff --git a/zkevm-circuits/src/evm_circuit/tests/pre-eip155.rs b/zkevm-circuits/src/evm_circuit/tests/pre-eip155.rs index 968bd5b08a..9357843d1f 100644 --- a/zkevm-circuits/src/evm_circuit/tests/pre-eip155.rs +++ b/zkevm-circuits/src/evm_circuit/tests/pre-eip155.rs @@ -11,17 +11,6 @@ mod test { use ethers_core::{types::Bytes, utils::get_contract_address}; use mock::{eth, gwei, MockTransaction, TestContext, MOCK_ACCOUNTS}; - fn gas(call_data: &[u8]) -> Word { - Word::from( - GasCost::TX.as_u64() - + 2 * OpcodeId::PUSH32.constant_gas_cost().as_u64() - + call_data - .iter() - .map(|&x| if x == 0 { 4 } else { 16 }) - .sum::(), - ) - } - // Note: all pre-eip155 txs here for testing have signature data. don't need to generate signature dynamically // because ethers-rs lib's helper `sign_transaction_sync` doesn't support pre-eip155 type. #[test] diff --git a/zkevm-circuits/src/evm_circuit/util/common_gadget.rs b/zkevm-circuits/src/evm_circuit/util/common_gadget.rs index de2ee03757..2e0eaa5e8b 100644 --- a/zkevm-circuits/src/evm_circuit/util/common_gadget.rs +++ b/zkevm-circuits/src/evm_circuit/util/common_gadget.rs @@ -32,13 +32,13 @@ use halo2_proofs::{ plonk::{Error, Expression}, }; -mod tx_access_list; mod tx_eip1559; +mod tx_eip2930; mod tx_l1_fee; mod tx_l1_msg; -pub(crate) use tx_access_list::TxAccessListGadget; pub(crate) use tx_eip1559::TxEip1559Gadget; +pub(crate) use tx_eip2930::TxAccessListGadget; pub(crate) use tx_l1_fee::TxL1FeeGadget; pub(crate) use tx_l1_msg::TxL1MsgGadget; diff --git a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_2930.rs b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_eip2930.rs similarity index 100% rename from zkevm-circuits/src/evm_circuit/util/common_gadget/tx_2930.rs rename to zkevm-circuits/src/evm_circuit/util/common_gadget/tx_eip2930.rs From 9aae3cff8eeed21c573cee9b0fad1a11be154f14 Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Thu, 19 Sep 2024 14:33:26 +0800 Subject: [PATCH 4/8] rename --- zkevm-circuits/src/evm_circuit/tests/eip155.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/zkevm-circuits/src/evm_circuit/tests/eip155.rs b/zkevm-circuits/src/evm_circuit/tests/eip155.rs index 023e418643..c672f93dfa 100644 --- a/zkevm-circuits/src/evm_circuit/tests/eip155.rs +++ b/zkevm-circuits/src/evm_circuit/tests/eip155.rs @@ -6,9 +6,8 @@ mod test { use ethers_signers::Signer; use mock::{eth, gwei, TestContext, MOCK_ACCOUNTS, MOCK_WALLETS}; - // test with empty access list. #[test] - fn test_eip2930_tx_for_empty_access_list() { + fn test_eip155() { let ctx = build_ctx(gwei(80_000)).unwrap(); } From 1cccbc040eb5b82ae9d40a3acb33e8be339e93c9 Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Thu, 19 Sep 2024 14:50:28 +0800 Subject: [PATCH 5/8] fix clippy --- zkevm-circuits/src/evm_circuit/execution/begin_tx.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs b/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs index c4228fd996..be786f8da2 100644 --- a/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs +++ b/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs @@ -1254,10 +1254,9 @@ mod test { use std::{str::FromStr, vec}; use crate::{evm_circuit::test::rand_bytes, test_util::CircuitTestBuilder}; - use bus_mapping::{circuit_input_builder::CircuitsParams, evm::OpcodeId}; + use bus_mapping::evm::OpcodeId; use eth_types::{ - self, address, bytecode, evm_types::GasCost, word, Address, Bytecode, Error, Hash, Word, - U256, + self, address, bytecode, evm_types::GasCost, word, Address, Bytecode, Hash, Word, U256, }; use ethers_core::{types::Bytes, utils::get_contract_address}; use mock::{eth, gwei, MockTransaction, TestContext, MOCK_ACCOUNTS}; From a4ffbb9dd26d31bed07981e0998ee1b8bb652b92 Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Thu, 19 Sep 2024 16:33:21 +0800 Subject: [PATCH 6/8] mod and clean --- zkevm-circuits/src/evm_circuit/execution.rs | 3 + .../src/evm_circuit/execution/tests/eip155.rs | 34 ++++++ .../evm_circuit/execution/tests/eip1559.rs | 104 +++++++++++++++++ .../evm_circuit/execution/tests/eip2930.rs | 94 +++++++++++++++ .../src/evm_circuit/execution/tests/mod.rs | 7 ++ .../evm_circuit/execution/tests/pre_eip155.rs | 85 ++++++++++++++ .../src/evm_circuit/tests/eip155.rs | 38 ------ .../src/evm_circuit/tests/eip1559.rs | 108 ------------------ .../src/evm_circuit/tests/eip2930.rs | 97 ---------------- .../src/evm_circuit/tests/pre-eip155.rs | 88 -------------- 10 files changed, 327 insertions(+), 331 deletions(-) create mode 100644 zkevm-circuits/src/evm_circuit/execution/tests/eip155.rs create mode 100644 zkevm-circuits/src/evm_circuit/execution/tests/eip1559.rs create mode 100644 zkevm-circuits/src/evm_circuit/execution/tests/eip2930.rs create mode 100644 zkevm-circuits/src/evm_circuit/execution/tests/mod.rs create mode 100644 zkevm-circuits/src/evm_circuit/execution/tests/pre_eip155.rs delete mode 100644 zkevm-circuits/src/evm_circuit/tests/eip155.rs delete mode 100644 zkevm-circuits/src/evm_circuit/tests/eip1559.rs delete mode 100644 zkevm-circuits/src/evm_circuit/tests/eip2930.rs delete mode 100644 zkevm-circuits/src/evm_circuit/tests/pre-eip155.rs diff --git a/zkevm-circuits/src/evm_circuit/execution.rs b/zkevm-circuits/src/evm_circuit/execution.rs index ffc699279a..934c299530 100644 --- a/zkevm-circuits/src/evm_circuit/execution.rs +++ b/zkevm-circuits/src/evm_circuit/execution.rs @@ -55,6 +55,9 @@ use strum::{EnumCount, IntoEnumIterator}; pub(crate) static CHECK_RW_LOOKUP: LazyLock = LazyLock::new(|| read_env_var("CHECK_RW_LOOKUP", false)); +#[cfg(any(feature = "test", test))] +mod tests; + mod add_sub; mod addmod; mod address; diff --git a/zkevm-circuits/src/evm_circuit/execution/tests/eip155.rs b/zkevm-circuits/src/evm_circuit/execution/tests/eip155.rs new file mode 100644 index 0000000000..564e15b6ab --- /dev/null +++ b/zkevm-circuits/src/evm_circuit/execution/tests/eip155.rs @@ -0,0 +1,34 @@ +// tests for eip155 tx +use crate::test_util::CircuitTestBuilder; +use eth_types::{address, AccessList, AccessListItem, Error, Word, H256}; +use ethers_signers::Signer; +use mock::{eth, gwei, TestContext, MOCK_ACCOUNTS, MOCK_WALLETS}; + +#[test] +fn test_eip155() { + let ctx = build_ctx(gwei(80_000)).unwrap(); + CircuitTestBuilder::new_from_test_ctx(ctx).run(); +} + +fn build_ctx(sender_balance: Word) -> Result, Error> { + TestContext::new( + None, + |accs| { + accs[0] + .address(MOCK_WALLETS[0].address()) + .balance(sender_balance); + accs[1].address(MOCK_ACCOUNTS[0]).balance(eth(1)); + }, + |mut txs, _accs| { + txs[0] + .from(MOCK_WALLETS[0].clone()) + .to(MOCK_ACCOUNTS[0]) + .gas(40_000.into()) + .gas_price(30_000.into()) + .value(gwei(20_000)) + // Set tx type to EIP-155. + .transaction_type(0); + }, + |block, _tx| block.number(0xcafeu64), + ) +} diff --git a/zkevm-circuits/src/evm_circuit/execution/tests/eip1559.rs b/zkevm-circuits/src/evm_circuit/execution/tests/eip1559.rs new file mode 100644 index 0000000000..4382f50eb0 --- /dev/null +++ b/zkevm-circuits/src/evm_circuit/execution/tests/eip1559.rs @@ -0,0 +1,104 @@ +use crate::test_util::CircuitTestBuilder; +use eth_types::{Error, Word}; +use ethers_signers::Signer; +use mock::{eth, gwei, TestContext, MOCK_ACCOUNTS, MOCK_WALLETS}; + +#[test] +fn test_eip1559_tx_for_equal_balance() { + let balance = if cfg!(feature = "scroll") { + // l1 fee + gwei(80_000) + Word::from(279u64) + } else { + gwei(80_000) + }; + let ctx = build_ctx(balance, gwei(2), gwei(2)).unwrap(); + CircuitTestBuilder::new_from_test_ctx(ctx).run(); +} + +#[test] +fn test_eip1559_tx_for_less_balance() { + let res = build_ctx(gwei(79_999), gwei(2), gwei(2)); + + #[cfg(not(feature = "scroll"))] + let expected_err = "Failed to run Trace, err: Failed to apply config.Transactions[0]: insufficient funds for gas * price + value: address 0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241 have 79999000000000 want 80000000000000"; + + // "80000000000279": 279 is l1 fee + #[cfg(feature = "scroll")] + let expected_err = "Failed to run Trace, err: insufficient funds for gas * price + value: address 0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241 have 79999000000000 want 80000000000279"; + + // Address `0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241` in error message comes from + // MOCK_WALLETS[0] in build_ctx. + + // Return a tracing error if insufficient sender balance. + if let Error::TracingError(err) = res.unwrap_err() { + assert_eq!(err, expected_err); + } else { + panic!("Must be a tracing error"); + } +} + +#[test] +fn test_eip1559_tx_for_more_balance() { + let ctx = build_ctx(gwei(80_001), gwei(2), gwei(2)).unwrap(); + CircuitTestBuilder::new_from_test_ctx(ctx).run(); +} + +#[test] +fn test_eip1559_tx_for_gas_fee_cap_gt_gas_tip_cap() { + // Should be successful if `max_fee_per_gas > max_priority_fee_per_gas`. + let balance = if cfg!(feature = "scroll") { + // l1 fee + gwei(80_000) + Word::from(279u64) + } else { + gwei(80_000) + }; + let ctx = build_ctx(balance, gwei(2), gwei(1)).unwrap(); + + CircuitTestBuilder::new_from_test_ctx(ctx).run(); +} + +#[test] +fn test_eip1559_tx_for_gas_fee_cap_lt_gas_tip_cap() { + let res = build_ctx(gwei(80_000), gwei(1), gwei(2)); + + #[cfg(not(feature = "scroll"))] + let expected_err = "Failed to run Trace, err: Failed to apply config.Transactions[0]: max priority fee per gas higher than max fee per gas: address 0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241, maxPriorityFeePerGas: 2000000000, maxFeePerGas: 1000000000"; + #[cfg(feature = "scroll")] + let expected_err = "Failed to run Trace, err: max priority fee per gas higher than max fee per gas: address 0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241, maxPriorityFeePerGas: 2000000000, maxFeePerGas: 1000000000"; + // Address `0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241` in error message comes from + // MOCK_WALLETS[0] in build_ctx. + + // Return a tracing error if `max_fee_per_gas < max_priority_fee_per_gas`. + if let Error::TracingError(err) = res.unwrap_err() { + assert_eq!(err, expected_err); + } else { + panic!("Must be a tracing error"); + } +} + +fn build_ctx( + sender_balance: Word, + max_fee_per_gas: Word, + max_priority_fee_per_gas: Word, +) -> Result, Error> { + TestContext::new( + None, + |accs| { + accs[0] + .address(MOCK_WALLETS[0].address()) + .balance(sender_balance); + accs[1].address(MOCK_ACCOUNTS[0]).balance(eth(1)); + }, + |mut txs, _accs| { + txs[0] + .from(MOCK_WALLETS[0].clone()) + .to(MOCK_ACCOUNTS[0]) + .gas(30_000.into()) + .value(gwei(20_000)) + .max_fee_per_gas(max_fee_per_gas) + .max_priority_fee_per_gas(max_priority_fee_per_gas) + .transaction_type(2); // Set tx type to EIP-1559. + }, + |block, _tx| block.number(0xcafeu64), + ) +} diff --git a/zkevm-circuits/src/evm_circuit/execution/tests/eip2930.rs b/zkevm-circuits/src/evm_circuit/execution/tests/eip2930.rs new file mode 100644 index 0000000000..30eed82d73 --- /dev/null +++ b/zkevm-circuits/src/evm_circuit/execution/tests/eip2930.rs @@ -0,0 +1,94 @@ +// tests for eip2930 +use crate::test_util::CircuitTestBuilder; +use eth_types::{address, AccessList, AccessListItem, Error, Word, H256}; +use ethers_signers::Signer; +use mock::{eth, gwei, TestContext, MOCK_ACCOUNTS, MOCK_WALLETS}; + +// test with empty access list. +#[test] +fn test_eip2930_tx_for_empty_access_list() { + // CASE1: tx not set access list, `access_list` field is none. + let ctx = build_ctx(gwei(80_000), None).unwrap(); + CircuitTestBuilder::new_from_test_ctx(ctx).run(); + + // CASE2: tx set empty (neither address nor storage keys at all) access list into + // `access_list` field. this field is not none. + let test_access_list: AccessList = AccessList(vec![]); + + let ctx = build_ctx(gwei(80_000), Some(test_access_list)).unwrap(); + CircuitTestBuilder::new_from_test_ctx(ctx).run(); +} + +// test with non empty access list(address + storage keys list) +#[test] +fn test_eip2930_non_empty_access_list() { + let test_access_list: AccessList = AccessList(vec![ + AccessListItem { + address: address!("0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241"), + // one storage key + storage_keys: [10].map(H256::from_low_u64_be).to_vec(), + }, + AccessListItem { + address: address!("0x0000000000000000000000000000000000001111"), + // two storage keys + storage_keys: [10, 11].map(H256::from_low_u64_be).to_vec(), + }, + AccessListItem { + address: address!("0x0000000000000000000000000000000000002222"), + // three storage keys + storage_keys: [20, 22, 50].map(H256::from_low_u64_be).to_vec(), + }, + ]); + + let ctx = build_ctx(gwei(80_000), Some(test_access_list)).unwrap(); + CircuitTestBuilder::new_from_test_ctx(ctx).run(); +} + +// test with non empty access list(only address list) +#[test] +fn test_eip2930_only_address_access_list() { + let test_access_list: AccessList = AccessList(vec![ + AccessListItem { + address: address!("0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241"), + // no storage keys + storage_keys: Vec::new(), + }, + AccessListItem { + address: address!("0x0000000000000000000000000000000000001111"), + // no storage keys + storage_keys: Vec::new(), + }, + ]); + + let ctx = build_ctx(gwei(80_000), Some(test_access_list)).unwrap(); + CircuitTestBuilder::new_from_test_ctx(ctx).run(); +} + +fn build_ctx( + sender_balance: Word, + access_list: Option, +) -> Result, Error> { + TestContext::new( + None, + |accs| { + accs[0] + .address(MOCK_WALLETS[0].address()) + .balance(sender_balance); + accs[1].address(MOCK_ACCOUNTS[0]).balance(eth(1)); + }, + |mut txs, _accs| { + txs[0] + .from(MOCK_WALLETS[0].clone()) + .to(MOCK_ACCOUNTS[0]) + .gas(40_000.into()) + .gas_price(30_000.into()) + .value(gwei(20_000)) + .transaction_type(1); // Set tx type to EIP-2930. + + if let Some(acc_list) = access_list { + txs[0].access_list(acc_list); + } + }, + |block, _tx| block.number(0xcafeu64), + ) +} diff --git a/zkevm-circuits/src/evm_circuit/execution/tests/mod.rs b/zkevm-circuits/src/evm_circuit/execution/tests/mod.rs new file mode 100644 index 0000000000..3867636cb0 --- /dev/null +++ b/zkevm-circuits/src/evm_circuit/execution/tests/mod.rs @@ -0,0 +1,7 @@ +// this fold only contains tx type tests, for all opcode related tests are in +// each opcode gadget file. +mod eip155; +mod pre_eip155; + +mod eip1559; +mod eip2930; diff --git a/zkevm-circuits/src/evm_circuit/execution/tests/pre_eip155.rs b/zkevm-circuits/src/evm_circuit/execution/tests/pre_eip155.rs new file mode 100644 index 0000000000..3e05fdcf66 --- /dev/null +++ b/zkevm-circuits/src/evm_circuit/execution/tests/pre_eip155.rs @@ -0,0 +1,85 @@ + +use std::{str::FromStr, vec}; + +use crate::{evm_circuit::test::rand_bytes, test_util::CircuitTestBuilder}; +use bus_mapping::{circuit_input_builder::CircuitsParams, evm::OpcodeId}; +use eth_types::{ + self, address, bytecode, evm_types::GasCost, word, Address, Bytecode, Error, Hash, Word, U256, +}; +use ethers_core::{types::Bytes, utils::get_contract_address}; +use mock::{eth, gwei, MockTransaction, TestContext, MOCK_ACCOUNTS}; + +// Note: all pre-eip155 txs here for testing have signature data. don't need to generate signature dynamically +// because ethers-rs lib's helper `sign_transaction_sync` doesn't support pre-eip155 type. +#[test] +fn test_legacy_tx_pre_eip155() { + let mut tx1 = MockTransaction::default(); + // pre-eip155 tx1 downloaded from [etherscan](https://etherscan.io/getRawTx?tx=0x9cd2288e69623b109e25edc46bc518156498b521e5c162d96e1ab392ff1d9dff) + // tx with signature::v =0x1c (28). + let sig_data1 = ( + 0x1c_u64, + word!("0x90b751c5870e9bc071c8d6b2bf1ee80f36ee7efd8e6fbabaa25bd3b8b68cfe9b"), + word!("0x79c25a01f12493a6d35f1330306d4e3c4e782fcbffc64c6809959577f41ff248"), + ); + + tx1 + .from(address!("0xcf40d0d2b44f2b66e07cace1372ca42b73cf21a3")) + .nonce(word!("0x2ea8")) + .gas_price(word!("0x098bca5a00")) + .gas(word!("0x0249f0")) + .value(word!("0x00")) + // Set tx type to pre-eip155. + .transaction_type(0) + .input(hex::decode("606060405260008054600160a060020a0319163317905560f2806100236000396000f3606060405260e060020a6000350463f5537ede8114601c575b6002565b3460025760f06004356024356044356000805433600160a060020a039081169116141560ea5783905080600160a060020a031663a9059cbb84846000604051602001526040518360e060020a0281526004018083600160a060020a0316815260200182815260200192505050602060405180830381600087803b1560025760325a03f1156002575050604080518481529051600160a060020a0386811693508716917fd0ed88a3f042c6bbb1e3ea406079b5f2b4b198afccaa535d837f4c63abbc4de6919081900360200190a35b50505050565b00") + .expect("hex data can be decoded").into()) + .sig_data(sig_data1); + + // pre-eip155 tx2 refers to https://github.com/scroll-tech/go-ethereum/blob/develop/cmd/evm/testdata/3/txs.json. + let mut tx2 = MockTransaction::default(); + // tx with signature::v =0x1b (27). + let sig_data2 = ( + 0x1b_u64, + word!("0x88544c93a564b4c28d2ffac2074a0c55fdd4658fe0d215596ed2e32e3ef7f56b"), + word!("0x7fb4075d54190f825d7c47bb820284757b34fd6293904a93cddb1d3aa961ac28"), + ); + + tx2.from(address!("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b")) + .to(address!("0x095e7baea6a6c7c4c2dfeb977efac326af552d87")) + .nonce(word!("0x0")) + .gas_price(word!("0x1")) + .gas(word!("0x5f5e100")) + .value(word!("0x186a0")) + // Set tx type to pre-eip155. + .transaction_type(0) + .sig_data(sig_data2); + + for tx in [tx1, tx2] { + let ctx = build_legacy_ctx(gwei(8_000_000), &tx).unwrap(); + CircuitTestBuilder::new_from_test_ctx(ctx) + .params(CircuitsParams { + max_calldata: 300, + ..Default::default() + }) + .run() + } +} + +// build pre-eip155 tx +fn build_legacy_ctx( + sender_balance: Word, + tx: &MockTransaction, +) -> Result, Error> { + TestContext::new( + None, + |accs| { + accs[0] + .address(tx.from.address()) + .balance(sender_balance) + .nonce(tx.nonce); + }, + |mut txs, _accs| { + txs[0].clone_from(tx); + }, + |block, _tx| block.number(0xcafeu64), + ) +} diff --git a/zkevm-circuits/src/evm_circuit/tests/eip155.rs b/zkevm-circuits/src/evm_circuit/tests/eip155.rs deleted file mode 100644 index c672f93dfa..0000000000 --- a/zkevm-circuits/src/evm_circuit/tests/eip155.rs +++ /dev/null @@ -1,38 +0,0 @@ -// tests for eip155 tx -#[cfg(test)] -mod test { - use crate::test_util::CircuitTestBuilder; - use eth_types::{address, AccessList, AccessListItem, Error, Word, H256}; - use ethers_signers::Signer; - use mock::{eth, gwei, TestContext, MOCK_ACCOUNTS, MOCK_WALLETS}; - - #[test] - fn test_eip155() { - let ctx = build_ctx(gwei(80_000)).unwrap(); - } - - fn build_ctx( - sender_balance: Word, - ) -> Result, Error> { - TestContext::new( - None, - |accs| { - accs[0] - .address(MOCK_WALLETS[0].address()) - .balance(sender_balance); - accs[1].address(MOCK_ACCOUNTS[0]).balance(eth(1)); - }, - |mut txs, _accs| { - txs[0] - .from(MOCK_WALLETS[0].clone()) - .to(MOCK_ACCOUNTS[0]) - .gas(40_000.into()) - .gas_price(30_000.into()) - .value(gwei(20_000)) - // Set tx type to EIP-155. - .transaction_type(0); - }, - |block, _tx| block.number(0xcafeu64), - ) - } -} diff --git a/zkevm-circuits/src/evm_circuit/tests/eip1559.rs b/zkevm-circuits/src/evm_circuit/tests/eip1559.rs deleted file mode 100644 index 8a47ad2828..0000000000 --- a/zkevm-circuits/src/evm_circuit/tests/eip1559.rs +++ /dev/null @@ -1,108 +0,0 @@ - -#[cfg(test)] -mod test { - use crate::test_util::CircuitTestBuilder; - use eth_types::{Error, Word}; - use ethers_signers::Signer; - use mock::{eth, gwei, TestContext, MOCK_ACCOUNTS, MOCK_WALLETS}; - - #[test] - fn test_eip1559_tx_for_equal_balance() { - let balance = if cfg!(feature = "scroll") { - // l1 fee - gwei(80_000) + Word::from(279u64) - } else { - gwei(80_000) - }; - let ctx = build_ctx(balance, gwei(2), gwei(2)).unwrap(); - CircuitTestBuilder::new_from_test_ctx(ctx).run(); - } - - #[test] - fn test_eip1559_tx_for_less_balance() { - let res = build_ctx(gwei(79_999), gwei(2), gwei(2)); - - #[cfg(not(feature = "scroll"))] - let expected_err = "Failed to run Trace, err: Failed to apply config.Transactions[0]: insufficient funds for gas * price + value: address 0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241 have 79999000000000 want 80000000000000"; - - // "80000000000279": 279 is l1 fee - #[cfg(feature = "scroll")] - let expected_err = "Failed to run Trace, err: insufficient funds for gas * price + value: address 0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241 have 79999000000000 want 80000000000279"; - - // Address `0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241` in error message comes from - // MOCK_WALLETS[0] in build_ctx. - - // Return a tracing error if insufficient sender balance. - if let Error::TracingError(err) = res.unwrap_err() { - assert_eq!(err, expected_err); - } else { - panic!("Must be a tracing error"); - } - } - - #[test] - fn test_eip1559_tx_for_more_balance() { - let ctx = build_ctx(gwei(80_001), gwei(2), gwei(2)).unwrap(); - CircuitTestBuilder::new_from_test_ctx(ctx).run(); - } - - #[test] - fn test_eip1559_tx_for_gas_fee_cap_gt_gas_tip_cap() { - // Should be successful if `max_fee_per_gas > max_priority_fee_per_gas`. - let balance = if cfg!(feature = "scroll") { - // l1 fee - gwei(80_000) + Word::from(279u64) - } else { - gwei(80_000) - }; - let ctx = build_ctx(balance, gwei(2), gwei(1)).unwrap(); - - CircuitTestBuilder::new_from_test_ctx(ctx).run(); - } - - #[test] - fn test_eip1559_tx_for_gas_fee_cap_lt_gas_tip_cap() { - let res = build_ctx(gwei(80_000), gwei(1), gwei(2)); - - #[cfg(not(feature = "scroll"))] - let expected_err = "Failed to run Trace, err: Failed to apply config.Transactions[0]: max priority fee per gas higher than max fee per gas: address 0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241, maxPriorityFeePerGas: 2000000000, maxFeePerGas: 1000000000"; - #[cfg(feature = "scroll")] - let expected_err = "Failed to run Trace, err: max priority fee per gas higher than max fee per gas: address 0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241, maxPriorityFeePerGas: 2000000000, maxFeePerGas: 1000000000"; - // Address `0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241` in error message comes from - // MOCK_WALLETS[0] in build_ctx. - - // Return a tracing error if `max_fee_per_gas < max_priority_fee_per_gas`. - if let Error::TracingError(err) = res.unwrap_err() { - assert_eq!(err, expected_err); - } else { - panic!("Must be a tracing error"); - } - } - - fn build_ctx( - sender_balance: Word, - max_fee_per_gas: Word, - max_priority_fee_per_gas: Word, - ) -> Result, Error> { - TestContext::new( - None, - |accs| { - accs[0] - .address(MOCK_WALLETS[0].address()) - .balance(sender_balance); - accs[1].address(MOCK_ACCOUNTS[0]).balance(eth(1)); - }, - |mut txs, _accs| { - txs[0] - .from(MOCK_WALLETS[0].clone()) - .to(MOCK_ACCOUNTS[0]) - .gas(30_000.into()) - .value(gwei(20_000)) - .max_fee_per_gas(max_fee_per_gas) - .max_priority_fee_per_gas(max_priority_fee_per_gas) - .transaction_type(2); // Set tx type to EIP-1559. - }, - |block, _tx| block.number(0xcafeu64), - ) - } -} diff --git a/zkevm-circuits/src/evm_circuit/tests/eip2930.rs b/zkevm-circuits/src/evm_circuit/tests/eip2930.rs deleted file mode 100644 index 0ea667763f..0000000000 --- a/zkevm-circuits/src/evm_circuit/tests/eip2930.rs +++ /dev/null @@ -1,97 +0,0 @@ -// tests for eip2930 -#[cfg(test)] -mod test { - use crate::test_util::CircuitTestBuilder; - use eth_types::{address, AccessList, AccessListItem, Error, Word, H256}; - use ethers_signers::Signer; - use mock::{eth, gwei, TestContext, MOCK_ACCOUNTS, MOCK_WALLETS}; - - // test with empty access list. - #[test] - fn test_eip2930_tx_for_empty_access_list() { - // CASE1: tx not set access list, `access_list` field is none. - let ctx = build_ctx(gwei(80_000), None).unwrap(); - CircuitTestBuilder::new_from_test_ctx(ctx).run(); - - // CASE2: tx set empty (neither address nor storage keys at all) access list into - // `access_list` field. this field is not none. - let test_access_list: AccessList = AccessList(vec![]); - - let ctx = build_ctx(gwei(80_000), Some(test_access_list)).unwrap(); - CircuitTestBuilder::new_from_test_ctx(ctx).run(); - } - - // test with non empty access list(address + storage keys list) - #[test] - fn test_eip2930_non_empty_access_list() { - let test_access_list: AccessList = AccessList(vec![ - AccessListItem { - address: address!("0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241"), - // one storage key - storage_keys: [10].map(H256::from_low_u64_be).to_vec(), - }, - AccessListItem { - address: address!("0x0000000000000000000000000000000000001111"), - // two storage keys - storage_keys: [10, 11].map(H256::from_low_u64_be).to_vec(), - }, - AccessListItem { - address: address!("0x0000000000000000000000000000000000002222"), - // three storage keys - storage_keys: [20, 22, 50].map(H256::from_low_u64_be).to_vec(), - }, - ]); - - let ctx = build_ctx(gwei(80_000), Some(test_access_list)).unwrap(); - CircuitTestBuilder::new_from_test_ctx(ctx).run(); - } - - // test with non empty access list(only address list) - #[test] - fn test_eip2930_only_address_access_list() { - let test_access_list: AccessList = AccessList(vec![ - AccessListItem { - address: address!("0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241"), - // no storage keys - storage_keys: Vec::new(), - }, - AccessListItem { - address: address!("0x0000000000000000000000000000000000001111"), - // no storage keys - storage_keys: Vec::new(), - }, - ]); - - let ctx = build_ctx(gwei(80_000), Some(test_access_list)).unwrap(); - CircuitTestBuilder::new_from_test_ctx(ctx).run(); - } - - fn build_ctx( - sender_balance: Word, - access_list: Option, - ) -> Result, Error> { - TestContext::new( - None, - |accs| { - accs[0] - .address(MOCK_WALLETS[0].address()) - .balance(sender_balance); - accs[1].address(MOCK_ACCOUNTS[0]).balance(eth(1)); - }, - |mut txs, _accs| { - txs[0] - .from(MOCK_WALLETS[0].clone()) - .to(MOCK_ACCOUNTS[0]) - .gas(40_000.into()) - .gas_price(30_000.into()) - .value(gwei(20_000)) - .transaction_type(1); // Set tx type to EIP-2930. - - if let Some(acc_list) = access_list { - txs[0].access_list(acc_list); - } - }, - |block, _tx| block.number(0xcafeu64), - ) - } -} diff --git a/zkevm-circuits/src/evm_circuit/tests/pre-eip155.rs b/zkevm-circuits/src/evm_circuit/tests/pre-eip155.rs deleted file mode 100644 index 9357843d1f..0000000000 --- a/zkevm-circuits/src/evm_circuit/tests/pre-eip155.rs +++ /dev/null @@ -1,88 +0,0 @@ -#[cfg(test)] -mod test { - use std::{str::FromStr, vec}; - - use crate::{evm_circuit::test::rand_bytes, test_util::CircuitTestBuilder}; - use bus_mapping::{circuit_input_builder::CircuitsParams, evm::OpcodeId}; - use eth_types::{ - self, address, bytecode, evm_types::GasCost, word, Address, Bytecode, Error, Hash, Word, - U256, - }; - use ethers_core::{types::Bytes, utils::get_contract_address}; - use mock::{eth, gwei, MockTransaction, TestContext, MOCK_ACCOUNTS}; - - // Note: all pre-eip155 txs here for testing have signature data. don't need to generate signature dynamically - // because ethers-rs lib's helper `sign_transaction_sync` doesn't support pre-eip155 type. - #[test] - fn test_legacy_tx_pre_eip155() { - let mut tx1 = MockTransaction::default(); - // pre-eip155 tx1 downloaded from [etherscan](https://etherscan.io/getRawTx?tx=0x9cd2288e69623b109e25edc46bc518156498b521e5c162d96e1ab392ff1d9dff) - // tx with signature::v =0x1c (28). - let sig_data1 = ( - 0x1c_u64, - word!("0x90b751c5870e9bc071c8d6b2bf1ee80f36ee7efd8e6fbabaa25bd3b8b68cfe9b"), - word!("0x79c25a01f12493a6d35f1330306d4e3c4e782fcbffc64c6809959577f41ff248"), - ); - - tx1 - .from(address!("0xcf40d0d2b44f2b66e07cace1372ca42b73cf21a3")) - .nonce(word!("0x2ea8")) - .gas_price(word!("0x098bca5a00")) - .gas(word!("0x0249f0")) - .value(word!("0x00")) - // Set tx type to pre-eip155. - .transaction_type(0) - .input(hex::decode("606060405260008054600160a060020a0319163317905560f2806100236000396000f3606060405260e060020a6000350463f5537ede8114601c575b6002565b3460025760f06004356024356044356000805433600160a060020a039081169116141560ea5783905080600160a060020a031663a9059cbb84846000604051602001526040518360e060020a0281526004018083600160a060020a0316815260200182815260200192505050602060405180830381600087803b1560025760325a03f1156002575050604080518481529051600160a060020a0386811693508716917fd0ed88a3f042c6bbb1e3ea406079b5f2b4b198afccaa535d837f4c63abbc4de6919081900360200190a35b50505050565b00") - .expect("hex data can be decoded").into()) - .sig_data(sig_data1); - - // pre-eip155 tx2 refers to https://github.com/scroll-tech/go-ethereum/blob/develop/cmd/evm/testdata/3/txs.json. - let mut tx2 = MockTransaction::default(); - // tx with signature::v =0x1b (27). - let sig_data2 = ( - 0x1b_u64, - word!("0x88544c93a564b4c28d2ffac2074a0c55fdd4658fe0d215596ed2e32e3ef7f56b"), - word!("0x7fb4075d54190f825d7c47bb820284757b34fd6293904a93cddb1d3aa961ac28"), - ); - - tx2.from(address!("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b")) - .to(address!("0x095e7baea6a6c7c4c2dfeb977efac326af552d87")) - .nonce(word!("0x0")) - .gas_price(word!("0x1")) - .gas(word!("0x5f5e100")) - .value(word!("0x186a0")) - // Set tx type to pre-eip155. - .transaction_type(0) - .sig_data(sig_data2); - - for tx in [tx1, tx2] { - let ctx = build_legacy_ctx(gwei(8_000_000), &tx).unwrap(); - CircuitTestBuilder::new_from_test_ctx(ctx) - .params(CircuitsParams { - max_calldata: 300, - ..Default::default() - }) - .run() - } - } - - // build pre-eip155 tx - fn build_legacy_ctx( - sender_balance: Word, - tx: &MockTransaction, - ) -> Result, Error> { - TestContext::new( - None, - |accs| { - accs[0] - .address(tx.from.address()) - .balance(sender_balance) - .nonce(tx.nonce); - }, - |mut txs, _accs| { - txs[0].clone_from(tx); - }, - |block, _tx| block.number(0xcafeu64), - ) - } -} From b1a3a7a15160d5b718b5dd1f5f0eaf5d439d8eec Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Thu, 19 Sep 2024 16:47:43 +0800 Subject: [PATCH 7/8] fix clippy --- .../src/evm_circuit/execution/tests/eip155.rs | 63 ++++--- .../evm_circuit/execution/tests/eip1559.rs | 171 +++++++++--------- .../evm_circuit/execution/tests/eip2930.rs | 169 ++++++++--------- .../evm_circuit/execution/tests/pre_eip155.rs | 132 +++++++------- 4 files changed, 271 insertions(+), 264 deletions(-) diff --git a/zkevm-circuits/src/evm_circuit/execution/tests/eip155.rs b/zkevm-circuits/src/evm_circuit/execution/tests/eip155.rs index 564e15b6ab..c7fbf8ad6e 100644 --- a/zkevm-circuits/src/evm_circuit/execution/tests/eip155.rs +++ b/zkevm-circuits/src/evm_circuit/execution/tests/eip155.rs @@ -1,34 +1,37 @@ // tests for eip155 tx -use crate::test_util::CircuitTestBuilder; -use eth_types::{address, AccessList, AccessListItem, Error, Word, H256}; -use ethers_signers::Signer; -use mock::{eth, gwei, TestContext, MOCK_ACCOUNTS, MOCK_WALLETS}; +#[cfg(test)] +mod tx_type_test { + use crate::test_util::CircuitTestBuilder; + use eth_types::{Error, Word}; + use ethers_signers::Signer; + use mock::{eth, gwei, TestContext, MOCK_ACCOUNTS, MOCK_WALLETS}; -#[test] -fn test_eip155() { - let ctx = build_ctx(gwei(80_000)).unwrap(); - CircuitTestBuilder::new_from_test_ctx(ctx).run(); -} + #[test] + fn test_eip155() { + let ctx = build_ctx(gwei(80_000)).unwrap(); + CircuitTestBuilder::new_from_test_ctx(ctx).run(); + } -fn build_ctx(sender_balance: Word) -> Result, Error> { - TestContext::new( - None, - |accs| { - accs[0] - .address(MOCK_WALLETS[0].address()) - .balance(sender_balance); - accs[1].address(MOCK_ACCOUNTS[0]).balance(eth(1)); - }, - |mut txs, _accs| { - txs[0] - .from(MOCK_WALLETS[0].clone()) - .to(MOCK_ACCOUNTS[0]) - .gas(40_000.into()) - .gas_price(30_000.into()) - .value(gwei(20_000)) - // Set tx type to EIP-155. - .transaction_type(0); - }, - |block, _tx| block.number(0xcafeu64), - ) + fn build_ctx(sender_balance: Word) -> Result, Error> { + TestContext::new( + None, + |accs| { + accs[0] + .address(MOCK_WALLETS[0].address()) + .balance(sender_balance); + accs[1].address(MOCK_ACCOUNTS[0]).balance(eth(1)); + }, + |mut txs, _accs| { + txs[0] + .from(MOCK_WALLETS[0].clone()) + .to(MOCK_ACCOUNTS[0]) + .gas(40_000.into()) + .gas_price(30_000.into()) + .value(gwei(20_000)) + // Set tx type to EIP-155. + .transaction_type(0); + }, + |block, _tx| block.number(0xcafeu64), + ) + } } diff --git a/zkevm-circuits/src/evm_circuit/execution/tests/eip1559.rs b/zkevm-circuits/src/evm_circuit/execution/tests/eip1559.rs index 4382f50eb0..c3fdc8d8e0 100644 --- a/zkevm-circuits/src/evm_circuit/execution/tests/eip1559.rs +++ b/zkevm-circuits/src/evm_circuit/execution/tests/eip1559.rs @@ -1,104 +1,107 @@ -use crate::test_util::CircuitTestBuilder; -use eth_types::{Error, Word}; -use ethers_signers::Signer; -use mock::{eth, gwei, TestContext, MOCK_ACCOUNTS, MOCK_WALLETS}; +#[cfg(test)] +mod tx_type_test { + use crate::test_util::CircuitTestBuilder; + use eth_types::{Error, Word}; + use ethers_signers::Signer; + use mock::{eth, gwei, TestContext, MOCK_ACCOUNTS, MOCK_WALLETS}; -#[test] -fn test_eip1559_tx_for_equal_balance() { - let balance = if cfg!(feature = "scroll") { - // l1 fee - gwei(80_000) + Word::from(279u64) - } else { - gwei(80_000) - }; - let ctx = build_ctx(balance, gwei(2), gwei(2)).unwrap(); - CircuitTestBuilder::new_from_test_ctx(ctx).run(); -} + #[test] + fn test_eip1559_tx_for_equal_balance() { + let balance = if cfg!(feature = "scroll") { + // l1 fee + gwei(80_000) + Word::from(279u64) + } else { + gwei(80_000) + }; + let ctx = build_ctx(balance, gwei(2), gwei(2)).unwrap(); + CircuitTestBuilder::new_from_test_ctx(ctx).run(); + } -#[test] -fn test_eip1559_tx_for_less_balance() { - let res = build_ctx(gwei(79_999), gwei(2), gwei(2)); + #[test] + fn test_eip1559_tx_for_less_balance() { + let res = build_ctx(gwei(79_999), gwei(2), gwei(2)); - #[cfg(not(feature = "scroll"))] + #[cfg(not(feature = "scroll"))] let expected_err = "Failed to run Trace, err: Failed to apply config.Transactions[0]: insufficient funds for gas * price + value: address 0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241 have 79999000000000 want 80000000000000"; - // "80000000000279": 279 is l1 fee - #[cfg(feature = "scroll")] + // "80000000000279": 279 is l1 fee + #[cfg(feature = "scroll")] let expected_err = "Failed to run Trace, err: insufficient funds for gas * price + value: address 0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241 have 79999000000000 want 80000000000279"; - // Address `0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241` in error message comes from - // MOCK_WALLETS[0] in build_ctx. + // Address `0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241` in error message comes from + // MOCK_WALLETS[0] in build_ctx. - // Return a tracing error if insufficient sender balance. - if let Error::TracingError(err) = res.unwrap_err() { - assert_eq!(err, expected_err); - } else { - panic!("Must be a tracing error"); + // Return a tracing error if insufficient sender balance. + if let Error::TracingError(err) = res.unwrap_err() { + assert_eq!(err, expected_err); + } else { + panic!("Must be a tracing error"); + } } -} -#[test] -fn test_eip1559_tx_for_more_balance() { - let ctx = build_ctx(gwei(80_001), gwei(2), gwei(2)).unwrap(); - CircuitTestBuilder::new_from_test_ctx(ctx).run(); -} + #[test] + fn test_eip1559_tx_for_more_balance() { + let ctx = build_ctx(gwei(80_001), gwei(2), gwei(2)).unwrap(); + CircuitTestBuilder::new_from_test_ctx(ctx).run(); + } -#[test] -fn test_eip1559_tx_for_gas_fee_cap_gt_gas_tip_cap() { - // Should be successful if `max_fee_per_gas > max_priority_fee_per_gas`. - let balance = if cfg!(feature = "scroll") { - // l1 fee - gwei(80_000) + Word::from(279u64) - } else { - gwei(80_000) - }; - let ctx = build_ctx(balance, gwei(2), gwei(1)).unwrap(); + #[test] + fn test_eip1559_tx_for_gas_fee_cap_gt_gas_tip_cap() { + // Should be successful if `max_fee_per_gas > max_priority_fee_per_gas`. + let balance = if cfg!(feature = "scroll") { + // l1 fee + gwei(80_000) + Word::from(279u64) + } else { + gwei(80_000) + }; + let ctx = build_ctx(balance, gwei(2), gwei(1)).unwrap(); - CircuitTestBuilder::new_from_test_ctx(ctx).run(); -} + CircuitTestBuilder::new_from_test_ctx(ctx).run(); + } -#[test] -fn test_eip1559_tx_for_gas_fee_cap_lt_gas_tip_cap() { - let res = build_ctx(gwei(80_000), gwei(1), gwei(2)); + #[test] + fn test_eip1559_tx_for_gas_fee_cap_lt_gas_tip_cap() { + let res = build_ctx(gwei(80_000), gwei(1), gwei(2)); - #[cfg(not(feature = "scroll"))] + #[cfg(not(feature = "scroll"))] let expected_err = "Failed to run Trace, err: Failed to apply config.Transactions[0]: max priority fee per gas higher than max fee per gas: address 0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241, maxPriorityFeePerGas: 2000000000, maxFeePerGas: 1000000000"; - #[cfg(feature = "scroll")] + #[cfg(feature = "scroll")] let expected_err = "Failed to run Trace, err: max priority fee per gas higher than max fee per gas: address 0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241, maxPriorityFeePerGas: 2000000000, maxFeePerGas: 1000000000"; - // Address `0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241` in error message comes from - // MOCK_WALLETS[0] in build_ctx. + // Address `0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241` in error message comes from + // MOCK_WALLETS[0] in build_ctx. - // Return a tracing error if `max_fee_per_gas < max_priority_fee_per_gas`. - if let Error::TracingError(err) = res.unwrap_err() { - assert_eq!(err, expected_err); - } else { - panic!("Must be a tracing error"); + // Return a tracing error if `max_fee_per_gas < max_priority_fee_per_gas`. + if let Error::TracingError(err) = res.unwrap_err() { + assert_eq!(err, expected_err); + } else { + panic!("Must be a tracing error"); + } } -} -fn build_ctx( - sender_balance: Word, - max_fee_per_gas: Word, - max_priority_fee_per_gas: Word, -) -> Result, Error> { - TestContext::new( - None, - |accs| { - accs[0] - .address(MOCK_WALLETS[0].address()) - .balance(sender_balance); - accs[1].address(MOCK_ACCOUNTS[0]).balance(eth(1)); - }, - |mut txs, _accs| { - txs[0] - .from(MOCK_WALLETS[0].clone()) - .to(MOCK_ACCOUNTS[0]) - .gas(30_000.into()) - .value(gwei(20_000)) - .max_fee_per_gas(max_fee_per_gas) - .max_priority_fee_per_gas(max_priority_fee_per_gas) - .transaction_type(2); // Set tx type to EIP-1559. - }, - |block, _tx| block.number(0xcafeu64), - ) + fn build_ctx( + sender_balance: Word, + max_fee_per_gas: Word, + max_priority_fee_per_gas: Word, + ) -> Result, Error> { + TestContext::new( + None, + |accs| { + accs[0] + .address(MOCK_WALLETS[0].address()) + .balance(sender_balance); + accs[1].address(MOCK_ACCOUNTS[0]).balance(eth(1)); + }, + |mut txs, _accs| { + txs[0] + .from(MOCK_WALLETS[0].clone()) + .to(MOCK_ACCOUNTS[0]) + .gas(30_000.into()) + .value(gwei(20_000)) + .max_fee_per_gas(max_fee_per_gas) + .max_priority_fee_per_gas(max_priority_fee_per_gas) + .transaction_type(2); // Set tx type to EIP-1559. + }, + |block, _tx| block.number(0xcafeu64), + ) + } } diff --git a/zkevm-circuits/src/evm_circuit/execution/tests/eip2930.rs b/zkevm-circuits/src/evm_circuit/execution/tests/eip2930.rs index 30eed82d73..1840ea82d9 100644 --- a/zkevm-circuits/src/evm_circuit/execution/tests/eip2930.rs +++ b/zkevm-circuits/src/evm_circuit/execution/tests/eip2930.rs @@ -1,94 +1,97 @@ // tests for eip2930 -use crate::test_util::CircuitTestBuilder; -use eth_types::{address, AccessList, AccessListItem, Error, Word, H256}; -use ethers_signers::Signer; -use mock::{eth, gwei, TestContext, MOCK_ACCOUNTS, MOCK_WALLETS}; +#[cfg(test)] +mod tx_type_test { + use crate::test_util::CircuitTestBuilder; + use eth_types::{address, AccessList, AccessListItem, Error, Word, H256}; + use ethers_signers::Signer; + use mock::{eth, gwei, TestContext, MOCK_ACCOUNTS, MOCK_WALLETS}; -// test with empty access list. -#[test] -fn test_eip2930_tx_for_empty_access_list() { - // CASE1: tx not set access list, `access_list` field is none. - let ctx = build_ctx(gwei(80_000), None).unwrap(); - CircuitTestBuilder::new_from_test_ctx(ctx).run(); + // test with empty access list. + #[test] + fn test_eip2930_tx_for_empty_access_list() { + // CASE1: tx not set access list, `access_list` field is none. + let ctx = build_ctx(gwei(80_000), None).unwrap(); + CircuitTestBuilder::new_from_test_ctx(ctx).run(); - // CASE2: tx set empty (neither address nor storage keys at all) access list into - // `access_list` field. this field is not none. - let test_access_list: AccessList = AccessList(vec![]); + // CASE2: tx set empty (neither address nor storage keys at all) access list into + // `access_list` field. this field is not none. + let test_access_list: AccessList = AccessList(vec![]); - let ctx = build_ctx(gwei(80_000), Some(test_access_list)).unwrap(); - CircuitTestBuilder::new_from_test_ctx(ctx).run(); -} + let ctx = build_ctx(gwei(80_000), Some(test_access_list)).unwrap(); + CircuitTestBuilder::new_from_test_ctx(ctx).run(); + } -// test with non empty access list(address + storage keys list) -#[test] -fn test_eip2930_non_empty_access_list() { - let test_access_list: AccessList = AccessList(vec![ - AccessListItem { - address: address!("0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241"), - // one storage key - storage_keys: [10].map(H256::from_low_u64_be).to_vec(), - }, - AccessListItem { - address: address!("0x0000000000000000000000000000000000001111"), - // two storage keys - storage_keys: [10, 11].map(H256::from_low_u64_be).to_vec(), - }, - AccessListItem { - address: address!("0x0000000000000000000000000000000000002222"), - // three storage keys - storage_keys: [20, 22, 50].map(H256::from_low_u64_be).to_vec(), - }, - ]); + // test with non empty access list(address + storage keys list) + #[test] + fn test_eip2930_non_empty_access_list() { + let test_access_list: AccessList = AccessList(vec![ + AccessListItem { + address: address!("0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241"), + // one storage key + storage_keys: [10].map(H256::from_low_u64_be).to_vec(), + }, + AccessListItem { + address: address!("0x0000000000000000000000000000000000001111"), + // two storage keys + storage_keys: [10, 11].map(H256::from_low_u64_be).to_vec(), + }, + AccessListItem { + address: address!("0x0000000000000000000000000000000000002222"), + // three storage keys + storage_keys: [20, 22, 50].map(H256::from_low_u64_be).to_vec(), + }, + ]); - let ctx = build_ctx(gwei(80_000), Some(test_access_list)).unwrap(); - CircuitTestBuilder::new_from_test_ctx(ctx).run(); -} + let ctx = build_ctx(gwei(80_000), Some(test_access_list)).unwrap(); + CircuitTestBuilder::new_from_test_ctx(ctx).run(); + } -// test with non empty access list(only address list) -#[test] -fn test_eip2930_only_address_access_list() { - let test_access_list: AccessList = AccessList(vec![ - AccessListItem { - address: address!("0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241"), - // no storage keys - storage_keys: Vec::new(), - }, - AccessListItem { - address: address!("0x0000000000000000000000000000000000001111"), - // no storage keys - storage_keys: Vec::new(), - }, - ]); + // test with non empty access list(only address list) + #[test] + fn test_eip2930_only_address_access_list() { + let test_access_list: AccessList = AccessList(vec![ + AccessListItem { + address: address!("0xEeFca179F40D3B8b3D941E6A13e48835a3aF8241"), + // no storage keys + storage_keys: Vec::new(), + }, + AccessListItem { + address: address!("0x0000000000000000000000000000000000001111"), + // no storage keys + storage_keys: Vec::new(), + }, + ]); - let ctx = build_ctx(gwei(80_000), Some(test_access_list)).unwrap(); - CircuitTestBuilder::new_from_test_ctx(ctx).run(); -} + let ctx = build_ctx(gwei(80_000), Some(test_access_list)).unwrap(); + CircuitTestBuilder::new_from_test_ctx(ctx).run(); + } -fn build_ctx( - sender_balance: Word, - access_list: Option, -) -> Result, Error> { - TestContext::new( - None, - |accs| { - accs[0] - .address(MOCK_WALLETS[0].address()) - .balance(sender_balance); - accs[1].address(MOCK_ACCOUNTS[0]).balance(eth(1)); - }, - |mut txs, _accs| { - txs[0] - .from(MOCK_WALLETS[0].clone()) - .to(MOCK_ACCOUNTS[0]) - .gas(40_000.into()) - .gas_price(30_000.into()) - .value(gwei(20_000)) - .transaction_type(1); // Set tx type to EIP-2930. + fn build_ctx( + sender_balance: Word, + access_list: Option, + ) -> Result, Error> { + TestContext::new( + None, + |accs| { + accs[0] + .address(MOCK_WALLETS[0].address()) + .balance(sender_balance); + accs[1].address(MOCK_ACCOUNTS[0]).balance(eth(1)); + }, + |mut txs, _accs| { + txs[0] + .from(MOCK_WALLETS[0].clone()) + .to(MOCK_ACCOUNTS[0]) + .gas(40_000.into()) + .gas_price(30_000.into()) + .value(gwei(20_000)) + .transaction_type(1); // Set tx type to EIP-2930. - if let Some(acc_list) = access_list { - txs[0].access_list(acc_list); - } - }, - |block, _tx| block.number(0xcafeu64), - ) + if let Some(acc_list) = access_list { + txs[0].access_list(acc_list); + } + }, + |block, _tx| block.number(0xcafeu64), + ) + } } diff --git a/zkevm-circuits/src/evm_circuit/execution/tests/pre_eip155.rs b/zkevm-circuits/src/evm_circuit/execution/tests/pre_eip155.rs index 3e05fdcf66..5f85e08443 100644 --- a/zkevm-circuits/src/evm_circuit/execution/tests/pre_eip155.rs +++ b/zkevm-circuits/src/evm_circuit/execution/tests/pre_eip155.rs @@ -1,28 +1,25 @@ +#[cfg(test)] +mod tx_type_test { + use crate::test_util::CircuitTestBuilder; + use bus_mapping::circuit_input_builder::CircuitsParams; + use eth_types::{ + self, address, word, Error, Word, }; + use mock::{gwei, MockTransaction, TestContext}; -use std::{str::FromStr, vec}; + // Note: all pre-eip155 txs here for testing have signature data. don't need to generate signature dynamically + // because ethers-rs lib's helper `sign_transaction_sync` doesn't support pre-eip155 type. + #[test] + fn test_legacy_tx_pre_eip155() { + let mut tx1 = MockTransaction::default(); + // pre-eip155 tx1 downloaded from [etherscan](https://etherscan.io/getRawTx?tx=0x9cd2288e69623b109e25edc46bc518156498b521e5c162d96e1ab392ff1d9dff) + // tx with signature::v =0x1c (28). + let sig_data1 = ( + 0x1c_u64, + word!("0x90b751c5870e9bc071c8d6b2bf1ee80f36ee7efd8e6fbabaa25bd3b8b68cfe9b"), + word!("0x79c25a01f12493a6d35f1330306d4e3c4e782fcbffc64c6809959577f41ff248"), + ); -use crate::{evm_circuit::test::rand_bytes, test_util::CircuitTestBuilder}; -use bus_mapping::{circuit_input_builder::CircuitsParams, evm::OpcodeId}; -use eth_types::{ - self, address, bytecode, evm_types::GasCost, word, Address, Bytecode, Error, Hash, Word, U256, -}; -use ethers_core::{types::Bytes, utils::get_contract_address}; -use mock::{eth, gwei, MockTransaction, TestContext, MOCK_ACCOUNTS}; - -// Note: all pre-eip155 txs here for testing have signature data. don't need to generate signature dynamically -// because ethers-rs lib's helper `sign_transaction_sync` doesn't support pre-eip155 type. -#[test] -fn test_legacy_tx_pre_eip155() { - let mut tx1 = MockTransaction::default(); - // pre-eip155 tx1 downloaded from [etherscan](https://etherscan.io/getRawTx?tx=0x9cd2288e69623b109e25edc46bc518156498b521e5c162d96e1ab392ff1d9dff) - // tx with signature::v =0x1c (28). - let sig_data1 = ( - 0x1c_u64, - word!("0x90b751c5870e9bc071c8d6b2bf1ee80f36ee7efd8e6fbabaa25bd3b8b68cfe9b"), - word!("0x79c25a01f12493a6d35f1330306d4e3c4e782fcbffc64c6809959577f41ff248"), - ); - - tx1 + tx1 .from(address!("0xcf40d0d2b44f2b66e07cace1372ca42b73cf21a3")) .nonce(word!("0x2ea8")) .gas_price(word!("0x098bca5a00")) @@ -34,52 +31,53 @@ fn test_legacy_tx_pre_eip155() { .expect("hex data can be decoded").into()) .sig_data(sig_data1); - // pre-eip155 tx2 refers to https://github.com/scroll-tech/go-ethereum/blob/develop/cmd/evm/testdata/3/txs.json. - let mut tx2 = MockTransaction::default(); - // tx with signature::v =0x1b (27). - let sig_data2 = ( - 0x1b_u64, - word!("0x88544c93a564b4c28d2ffac2074a0c55fdd4658fe0d215596ed2e32e3ef7f56b"), - word!("0x7fb4075d54190f825d7c47bb820284757b34fd6293904a93cddb1d3aa961ac28"), - ); + // pre-eip155 tx2 refers to https://github.com/scroll-tech/go-ethereum/blob/develop/cmd/evm/testdata/3/txs.json. + let mut tx2 = MockTransaction::default(); + // tx with signature::v =0x1b (27). + let sig_data2 = ( + 0x1b_u64, + word!("0x88544c93a564b4c28d2ffac2074a0c55fdd4658fe0d215596ed2e32e3ef7f56b"), + word!("0x7fb4075d54190f825d7c47bb820284757b34fd6293904a93cddb1d3aa961ac28"), + ); - tx2.from(address!("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b")) - .to(address!("0x095e7baea6a6c7c4c2dfeb977efac326af552d87")) - .nonce(word!("0x0")) - .gas_price(word!("0x1")) - .gas(word!("0x5f5e100")) - .value(word!("0x186a0")) - // Set tx type to pre-eip155. - .transaction_type(0) - .sig_data(sig_data2); + tx2.from(address!("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b")) + .to(address!("0x095e7baea6a6c7c4c2dfeb977efac326af552d87")) + .nonce(word!("0x0")) + .gas_price(word!("0x1")) + .gas(word!("0x5f5e100")) + .value(word!("0x186a0")) + // Set tx type to pre-eip155. + .transaction_type(0) + .sig_data(sig_data2); - for tx in [tx1, tx2] { - let ctx = build_legacy_ctx(gwei(8_000_000), &tx).unwrap(); - CircuitTestBuilder::new_from_test_ctx(ctx) - .params(CircuitsParams { - max_calldata: 300, - ..Default::default() - }) - .run() + for tx in [tx1, tx2] { + let ctx = build_legacy_ctx(gwei(8_000_000), &tx).unwrap(); + CircuitTestBuilder::new_from_test_ctx(ctx) + .params(CircuitsParams { + max_calldata: 300, + ..Default::default() + }) + .run() + } } -} -// build pre-eip155 tx -fn build_legacy_ctx( - sender_balance: Word, - tx: &MockTransaction, -) -> Result, Error> { - TestContext::new( - None, - |accs| { - accs[0] - .address(tx.from.address()) - .balance(sender_balance) - .nonce(tx.nonce); - }, - |mut txs, _accs| { - txs[0].clone_from(tx); - }, - |block, _tx| block.number(0xcafeu64), - ) + // build pre-eip155 tx + fn build_legacy_ctx( + sender_balance: Word, + tx: &MockTransaction, + ) -> Result, Error> { + TestContext::new( + None, + |accs| { + accs[0] + .address(tx.from.address()) + .balance(sender_balance) + .nonce(tx.nonce); + }, + |mut txs, _accs| { + txs[0].clone_from(tx); + }, + |block, _tx| block.number(0xcafeu64), + ) + } } From 77bb4787ee09bebfc9274b792cedcaa87978be4c Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Thu, 19 Sep 2024 16:51:46 +0800 Subject: [PATCH 8/8] fmt fix --- zkevm-circuits/src/evm_circuit/execution/tests/pre_eip155.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/zkevm-circuits/src/evm_circuit/execution/tests/pre_eip155.rs b/zkevm-circuits/src/evm_circuit/execution/tests/pre_eip155.rs index 5f85e08443..4eaab93bae 100644 --- a/zkevm-circuits/src/evm_circuit/execution/tests/pre_eip155.rs +++ b/zkevm-circuits/src/evm_circuit/execution/tests/pre_eip155.rs @@ -2,8 +2,7 @@ mod tx_type_test { use crate::test_util::CircuitTestBuilder; use bus_mapping::circuit_input_builder::CircuitsParams; - use eth_types::{ - self, address, word, Error, Word, }; + use eth_types::{self, address, word, Error, Word}; use mock::{gwei, MockTransaction, TestContext}; // Note: all pre-eip155 txs here for testing have signature data. don't need to generate signature dynamically