Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

Commit f2ce8f5

Browse files
authored
misc fix for mainnet (#717)
* fix * x * fixes * fix: create2 deploys contracts at address with non 0 balance * done * revert precompile changes * revert precompile changes
1 parent 87993f5 commit f2ce8f5

File tree

18 files changed

+114
-70
lines changed

18 files changed

+114
-70
lines changed

bus-mapping/src/circuit_input_builder.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,10 +1028,13 @@ impl<P: JsonRpcClient> BuilderClient<P> {
10281028
history_hashes: Vec<Word>,
10291029
_prev_state_root: Word,
10301030
) -> Result<CircuitInputBuilder, Error> {
1031-
let block = BlockHead::new(self.chain_id, history_hashes, eth_block)?;
1032-
let mut builder =
1033-
CircuitInputBuilder::new_from_headers(self.circuits_params, sdb, code_db, &[block]);
1034-
1031+
let block = Block::new(
1032+
self.chain_id,
1033+
history_hashes,
1034+
eth_block,
1035+
self.circuits_params,
1036+
)?;
1037+
let mut builder = CircuitInputBuilder::new(sdb, code_db, &block);
10351038
builder.handle_block(eth_block, geth_traces)?;
10361039
Ok(builder)
10371040
}

bus-mapping/src/circuit_input_builder/block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ impl Block {
190190
}
191191
}
192192
/// Create a new block.
193-
pub fn new<TX>(
193+
pub fn new(
194194
chain_id: u64,
195195
history_hashes: Vec<Word>,
196196
eth_block: &eth_types::Block<eth_types::Transaction>,

bus-mapping/src/circuit_input_builder/input_state_ref.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,11 +1275,17 @@ impl<'a> CircuitInputStateRef<'a> {
12751275

12761276
let [last_callee_return_data_offset, last_callee_return_data_length] = match geth_step.op {
12771277
OpcodeId::STOP => [Word::zero(); 2],
1278+
OpcodeId::CALL | OpcodeId::CALLCODE | OpcodeId::STATICCALL | OpcodeId::DELEGATECALL => {
1279+
// must be precompile
1280+
// FIXME: self.caller_ctx()?.return_data.len().into()
1281+
[Word::zero(), Word::zero()]
1282+
}
12781283
OpcodeId::REVERT | OpcodeId::RETURN => {
12791284
let offset = geth_step.stack.nth_last(0)?;
12801285
let length = geth_step.stack.nth_last(1)?;
12811286
// This is the convention we are using for memory addresses so that there is no
12821287
// memory expansion cost when the length is 0.
1288+
// https://github.com/privacy-scaling-explorations/zkevm-circuits/pull/279/files#r787806678
12831289
if length.is_zero() {
12841290
[Word::zero(); 2]
12851291
} else {

bus-mapping/src/circuit_input_builder/transaction.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use eth_types::{
66
evm_types::{gas_utils::tx_data_gas_cost, Memory},
77
geth_types,
88
geth_types::{get_rlp_unsigned, TxType},
9-
Address, GethExecTrace, Signature, Word, H256,
9+
AccessList, Address, GethExecTrace, Signature, Word, H256,
1010
};
1111
use ethers_core::utils::get_contract_address;
1212

@@ -229,6 +229,8 @@ pub struct Transaction {
229229
pub l1_fee: TxL1Fee,
230230
/// Committed values of L1 fee
231231
pub l1_fee_committed: TxL1Fee,
232+
/// EIP2930
233+
pub access_list: Option<AccessList>,
232234
/// Calls made in the transaction
233235
pub(crate) calls: Vec<Call>,
234236
/// Execution steps
@@ -287,6 +289,7 @@ impl Transaction {
287289
tx_type: Default::default(),
288290
l1_fee: Default::default(),
289291
l1_fee_committed: Default::default(),
292+
access_list: None,
290293
}
291294
}
292295

@@ -392,6 +395,7 @@ impl Transaction {
392395
},
393396
l1_fee,
394397
l1_fee_committed,
398+
access_list: eth_tx.access_list.clone(),
395399
})
396400
}
397401

bus-mapping/src/evm/opcodes.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,10 @@ pub fn gen_begin_tx_ops(
770770
exec_step.gas_cost = real_gas_cost;
771771
}
772772
} else {
773-
debug_assert_eq!(exec_step.gas_cost, real_gas_cost);
773+
// EIP2930 not implemented
774+
if state.tx.access_list.is_none() {
775+
debug_assert_eq!(exec_step.gas_cost, real_gas_cost);
776+
}
774777
}
775778

776779
log::trace!("begin_tx_step: {:?}", exec_step);

bus-mapping/src/evm/opcodes/callop.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,14 +271,14 @@ impl<const N_ARGS: usize> Opcode for CallOpcode<N_ARGS> {
271271
// written from memory addr 0 to memory addr result.len()
272272
state.call_ctx_mut()?.memory.extend_at_least(result.len());
273273

274+
state.caller_ctx_mut()?.return_data = result.clone();
274275
// mutate the caller memory.
275276
let length = min(result.len(), ret_length);
276277
if length > 0 {
277-
{
278-
let caller_ctx_mut = state.caller_ctx_mut()?;
279-
caller_ctx_mut.return_data = result.clone();
280-
caller_ctx_mut.memory.extend_at_least(ret_offset + length);
281-
}
278+
state
279+
.caller_ctx_mut()?
280+
.memory
281+
.extend_at_least(ret_offset + length);
282282
}
283283

284284
for (field, value) in [

bus-mapping/src/evm/opcodes/create.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ impl<const IS_CREATE2: bool> Opcode for Create<IS_CREATE2> {
6666

6767
let callee_account = &state.sdb.get_account(&address).1.clone();
6868
let callee_exists = !callee_account.is_empty();
69+
let is_address_collision = callee_account.code_hash != CodeDB::empty_code_hash();
6970
if !callee_exists && callee.value.is_zero() {
7071
state.sdb.get_account_mut(&address).1.storage.clear();
7172
}
@@ -170,7 +171,7 @@ impl<const IS_CREATE2: bool> Opcode for Create<IS_CREATE2> {
170171
// operation happens in evm create() method before checking
171172
// ErrContractAddressCollision
172173
let code_hash_previous = if callee_exists {
173-
if is_precheck_ok {
174+
if is_precheck_ok && is_address_collision {
174175
// CREATE2 may cause address collision error. And for a tricky
175176
// case of CREATE, it could also cause this error. e.g. the `to`
176177
// field of transaction is set to the calculated contract
@@ -198,12 +199,12 @@ impl<const IS_CREATE2: bool> Opcode for Create<IS_CREATE2> {
198199
code_hash_previous.to_word(),
199200
);
200201

201-
if is_precheck_ok && !callee_exists {
202+
if is_precheck_ok && !is_address_collision {
202203
state.transfer(
203204
&mut exec_step,
204205
callee.caller_address,
205206
callee.address,
206-
true,
207+
true, // since `must_create` is true, the `receiver_exists` is unused
207208
true,
208209
callee.value,
209210
)?;
@@ -311,7 +312,7 @@ impl<const IS_CREATE2: bool> Opcode for Create<IS_CREATE2> {
311312
state.block.sha3_inputs.push(keccak_input);
312313
state.block.sha3_inputs.push(initialization_code);
313314

314-
if length == 0 || callee_exists {
315+
if length == 0 || is_address_collision {
315316
for (field, value) in [
316317
(CallContextField::LastCalleeId, 0.into()),
317318
(CallContextField::LastCalleeReturnDataOffset, 0.into()),

bus-mapping/src/evm/opcodes/extcodehash.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ impl Opcode for Extcodehash {
5656
let account = state.sdb.get_account(&external_address).1;
5757
let exists = !account.is_empty();
5858
let code_hash = if exists {
59-
account.keccak_code_hash
59+
if cfg!(feature = "scroll") {
60+
account.keccak_code_hash
61+
} else {
62+
account.code_hash
63+
}
6064
} else {
6165
H256::zero()
6266
};

bus-mapping/src/evm/opcodes/extcodesize.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ impl Opcode for Extcodesize {
5454
let account = state.sdb.get_account(&address).1;
5555
let exists = !account.is_empty();
5656
let (code_hash, code_size) = if exists {
57-
(account.code_hash, account.code_size)
57+
(
58+
account.code_hash,
59+
state.code(account.code_hash)?.len().into(),
60+
)
5861
} else {
5962
(H256::zero(), Word::zero())
6063
};

eth-types/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,13 @@ pub struct EIP1186ProofResponse {
284284
/// The balance of the account
285285
pub balance: U256,
286286
/// The keccak hash of the code of the account
287+
#[serde(default)]
287288
pub keccak_code_hash: H256,
288289
/// The poseidon hash of the code of the account
289290
#[serde(alias = "poseidonCodeHash")]
290291
pub code_hash: H256,
291292
/// Size of the code, i.e. code length
293+
#[serde(default)]
292294
pub code_size: U256,
293295
/// The nonce of the account
294296
pub nonce: U256,

0 commit comments

Comments
 (0)