Skip to content

Commit

Permalink
Add code hash regression tests (scroll-tech#977)
Browse files Browse the repository at this point in the history
* Add regression test for EXTCODEHASH during account creation tx

* Add regression test for creation tx for accounts with non-zero balance

---------

Co-authored-by: Mason Liang <[email protected]>
  • Loading branch information
z2trillion and Mason Liang authored Sep 23, 2023
1 parent 2b39b4e commit f0a10fe
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
39 changes: 36 additions & 3 deletions zkevm-circuits/src/evm_circuit/execution/begin_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1054,9 +1054,10 @@ mod test {

use crate::{evm_circuit::test::rand_bytes, test_util::CircuitTestBuilder};
use bus_mapping::evm::OpcodeId;
use eth_types::{self, address, bytecode, evm_types::GasCost, word, Bytecode, Hash, Word};
use ethers_core::types::Bytes;

use eth_types::{
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};

fn gas(call_data: &[u8]) -> Word {
Expand Down Expand Up @@ -1437,4 +1438,36 @@ mod test {

CircuitTestBuilder::new_from_test_ctx(ctx).run();
}

// Test that we handle the case where account creation tx happens for an account that already
// has a non-zero balance and codehash.
#[test]
fn create_tx_for_existing_account() {
let address = Address::repeat_byte(23);
let nonce = U256::from(4);
let new_address = get_contract_address(address, nonce + U256::one());
dbg!(nonce);
dbg!(new_address);

let ctx = TestContext::<1, 2>::new(
None,
|accs| {
accs[0].address(address).nonce(nonce).balance(eth(10));
},
|mut txs, _| {
txs[0]
.from(address)
.to(new_address)
.value(eth(2))
.nonce(nonce);
txs[1].from(address).nonce(nonce + U256::one());
},
|block, _| block,
)
.unwrap();

CircuitTestBuilder::new_from_test_ctx(ctx)
.block_modifier(Box::new(|block| block.circuits_params.max_txs = 3))
.run();
}
}
26 changes: 25 additions & 1 deletion zkevm-circuits/src/evm_circuit/execution/extcodehash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ mod test {
address, bytecode, geth_types::Account, Address, Bytecode, Bytes, ToWord, Word, U256,
};
use lazy_static::lazy_static;
use mock::TestContext;
use mock::{eth, TestContext};

lazy_static! {
static ref EXTERNAL_ADDRESS: Address =
Expand Down Expand Up @@ -263,4 +263,28 @@ mod test {
test_ok(Some(account), false);
}
}

#[test]
// Regression test to ensure that the code hash for an account that is is being initialized is
// the empty code hash.
fn create_tx_extcodehash() {
let code = bytecode! {
ADDRESS
EXTCODEHASH
};

let ctx = TestContext::<1, 1>::new(
None,
|accs| {
accs[0].address(Address::repeat_byte(23)).balance(eth(10));
},
|mut txs, accs| {
txs[0].from(accs[0].address).input(code.into());
},
|block, _tx| block.number(0xcafeu64),
)
.unwrap();

CircuitTestBuilder::new_from_test_ctx(ctx).run()
}
}
3 changes: 2 additions & 1 deletion zkevm-circuits/src/test_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,12 @@ impl<const NACC: usize, const NTX: usize> CircuitTestBuilder<NACC, NTX> {
/// into a [`Block`] and apply the default or provided block_modifiers or
/// circuit checks to the provers generated for the State and EVM circuits.
pub fn run(self) {
let params = if let Some(block) = self.block.as_ref() {
let mut params = if let Some(block) = self.block.as_ref() {
block.circuits_params
} else {
self.circuits_params.unwrap_or_default()
};
params.max_txs = NTX;
log::debug!("params in CircuitTestBuilder: {:?}", params);

let block: Block<Fr> = if self.block.is_some() {
Expand Down

0 comments on commit f0a10fe

Please sign in to comment.