forked from privacy-scaling-explorations/zkevm-circuits
-
Notifications
You must be signed in to change notification settings - Fork 387
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: unify tx types(1559, 2930, pre155 etc) tests into dedicated…
… folder (#1425) * tests folder and move preeip155, 1559 tests into * move 2930 tests * add eip155 * rename * fix clippy * mod and clean * fix clippy * fmt fix
- Loading branch information
1 parent
2879c23
commit fab9d65
Showing
10 changed files
with
337 additions
and
288 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// tests for eip155 tx | ||
#[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(); | ||
} | ||
|
||
fn build_ctx(sender_balance: Word) -> Result<TestContext<2, 1>, 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), | ||
) | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
zkevm-circuits/src/evm_circuit/execution/tests/eip1559.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
#[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_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<TestContext<2, 1>, 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), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// tests for eip2930 | ||
#[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(); | ||
|
||
// 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<AccessList>, | ||
) -> Result<TestContext<2, 1>, 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), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
Oops, something went wrong.