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

Commit 238ef02

Browse files
authored
upgrade l2geth and remove old bytecode collecting codes (#1376)
* upgrade l2geth and remove old bytecode collecting codes * update copy circuit test
1 parent dfa0235 commit 238ef02

File tree

8 files changed

+47
-224
lines changed

8 files changed

+47
-224
lines changed

bus-mapping/src/circuit_input_builder/l2.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ impl CircuitInputBuilder {
138138
.map_err(Error::IoError)?;
139139

140140
log::debug!(
141-
"building partial statedb done, root {}",
141+
"building partial ZktrieState done, root {}",
142142
hex::encode(mpt_init_state.root())
143143
);
144144

@@ -152,6 +152,7 @@ impl CircuitInputBuilder {
152152
&l2_trace.storage_trace,
153153
)) {
154154
let (addr, acc) = parsed.map_err(Error::IoError)?;
155+
log::trace!("sdb trace {:?} {:?}", addr, acc);
155156
sdb.set_account(&addr, state_db::Account::from(&acc));
156157
}
157158

@@ -160,13 +161,14 @@ impl CircuitInputBuilder {
160161
)) {
161162
let ((addr, key), val) = parsed.map_err(Error::IoError)?;
162163
let key = key.to_word();
164+
log::trace!("sdb trace storage {:?} {:?} {:?}", addr, key, val);
163165
*sdb.get_storage_mut(&addr, &key).1 = val.into();
164166
}
165167

166168
let mut code_db = CodeDB::new();
167169
code_db.insert(Vec::new());
168170

169-
let codes = collect_codes(&l2_trace, Some(&sdb))?;
171+
let codes = collect_codes(&l2_trace)?;
170172
for (hash, code) in codes {
171173
code_db.insert_with_hash(hash, code);
172174
}
@@ -240,7 +242,7 @@ impl CircuitInputBuilder {
240242
*self.sdb.get_storage_mut(&addr, &key).1 = val;
241243
}
242244

243-
let codes = collect_codes(&l2_trace, Some(&self.sdb))?;
245+
let codes = collect_codes(&l2_trace)?;
244246
for (hash, code) in codes {
245247
self.code_db.insert_with_hash(hash, code);
246248
}

bus-mapping/src/circuit_input_builder/transaction.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,13 +282,15 @@ impl Transaction {
282282
let block_num = eth_tx.block_number.unwrap().as_u64();
283283
let (found, _) = sdb.get_account(&eth_tx.from);
284284
if !found {
285+
log::error!("tx.from not found {}", eth_tx.from);
285286
return Err(Error::AccountNotFound(eth_tx.from));
286287
}
287288

288289
let call = if let Some(address) = eth_tx.to {
289290
// Contract Call / Transfer
290291
let (found, account) = sdb.get_account(&address);
291292
if !found {
293+
log::error!("tx.to not found {}", address);
292294
return Err(Error::AccountNotFound(address));
293295
}
294296
let code_hash = account.code_hash;

eth-types/src/l2_types.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub struct BlockHeader {
8282

8383
impl From<BlockTrace> for BlockTraceV2 {
8484
fn from(b: BlockTrace) -> Self {
85-
let codes = collect_codes(&b, None)
85+
let codes = collect_codes(&b)
8686
.expect("collect codes should not fail")
8787
.into_iter()
8888
.map(|(hash, code)| BytecodeTrace {
@@ -531,8 +531,6 @@ pub struct ExecStep {
531531
pub memory: Option<Vec<crate::Word>>,
532532
#[cfg(feature = "enable-storage")]
533533
pub storage: Option<HashMap<crate::Word, crate::Word>>,
534-
#[serde(rename = "extraData")]
535-
pub extra_data: Option<ExtraData>,
536534
}
537535

538536
impl From<ExecStep> for GethExecStep {
@@ -556,20 +554,6 @@ impl From<ExecStep> for GethExecStep {
556554
}
557555
}
558556

559-
/// extra data for some steps
560-
#[derive(Serialize, Deserialize, Debug, Clone)]
561-
#[doc(hidden)]
562-
pub struct ExtraData {
563-
#[serde(rename = "codeList")]
564-
pub code_list: Option<Vec<Bytes>>,
565-
}
566-
567-
impl ExtraData {
568-
pub fn get_code_at(&self, i: usize) -> Option<Bytes> {
569-
self.code_list.as_ref().and_then(|c| c.get(i)).cloned()
570-
}
571-
}
572-
573557
/// account wrapper for account status
574558
#[derive(Serialize, Deserialize, Clone, Default, Debug, PartialEq, Eq)]
575559
#[doc(hidden)]

eth-types/src/l2_types/trace.rs

Lines changed: 13 additions & 183 deletions
Original file line numberDiff line numberDiff line change
@@ -1,187 +1,17 @@
1-
use crate::{
2-
evm_types::OpcodeId,
3-
l2_types::BlockTrace,
4-
state_db::{CodeDB, StateDB},
5-
utils::is_precompiled,
6-
Address, Error, H256,
7-
};
8-
use ethers_core::types::Bytes;
1+
use crate::{l2_types::BlockTrace, Error, H256};
92
use itertools::Itertools;
103

11-
use super::ExecStep;
12-
13-
/// Update codedb from statedb and trace
14-
pub fn collect_codes(
15-
block: &BlockTrace,
16-
sdb: Option<&StateDB>,
17-
) -> Result<Vec<(H256, Vec<u8>)>, Error> {
18-
if !block.codes.is_empty() {
19-
log::debug!("codes available in trace, skip collecting");
20-
return Ok(block
21-
.codes
22-
.iter()
23-
.map(|b| (b.hash, b.code.to_vec()))
24-
.collect_vec());
25-
}
26-
27-
log::debug!("collect_codes for block {:?}", block.header.number);
28-
if sdb.is_none() {
29-
log::warn!("collect_codes without sdb can be slow");
30-
}
31-
let mut codes = Vec::new();
32-
for (er_idx, execution_result) in block.execution_results.iter().enumerate() {
33-
if let Some(bytecode) = &execution_result.byte_code {
34-
if let Some(to) = &execution_result.to {
35-
// Not contract deployment
36-
let bytecode = decode_bytecode(bytecode)?.to_vec();
37-
let code_hash = to.poseidon_code_hash;
38-
// code hash 0 means non-existed account
39-
if !code_hash.is_zero() {
40-
codes.push((code_hash, bytecode));
41-
}
42-
//log::debug!("inserted tx bytecode {:?} {:?}", code_hash, hash);
43-
}
44-
}
45-
46-
// filter all precompile calls, empty calls and create
47-
let mut call_trace = execution_result
48-
.call_trace
49-
.flatten_trace(&execution_result.prestate)
50-
.into_iter()
51-
.filter(|call| {
52-
let is_call_to_precompile = call.to.as_ref().map(is_precompiled).unwrap_or(false);
53-
let is_call_to_empty = call.gas_used.is_zero()
54-
&& !call.call_type.is_create()
55-
&& call.is_callee_code_empty;
56-
!(is_call_to_precompile || is_call_to_empty || call.call_type.is_create())
57-
})
58-
.collect::<Vec<_>>();
59-
//log::trace!("call_trace: {call_trace:?}");
60-
61-
for (idx, step) in execution_result.exec_steps.iter().enumerate().rev() {
62-
if step.op.is_create() {
63-
continue;
64-
}
65-
let call = if step.op.is_call() {
66-
// filter call to empty/precompile/!precheck_ok
67-
if let Some(next_step) = execution_result.exec_steps.get(idx + 1) {
68-
// the call doesn't have inner steps, it could be:
69-
// - a call to a precompiled contract
70-
// - a call to an empty account
71-
// - a call that !is_precheck_ok
72-
if next_step.depth != step.depth + 1 {
73-
log::trace!("skip call step due to no inner step, curr: {step:?}, next: {next_step:?}");
74-
continue;
75-
}
76-
} else {
77-
// this is the final step, no inner steps
78-
log::trace!("skip call step due this is the final step: {step:?}");
79-
continue;
80-
}
81-
let call = call_trace.pop();
82-
//log::trace!("call_trace pop: {call:?}, current step: {step:?}");
83-
call
84-
} else {
85-
None
86-
};
87-
88-
if let Some(data) = &step.extra_data {
89-
match step.op {
90-
OpcodeId::CALL
91-
| OpcodeId::CALLCODE
92-
| OpcodeId::DELEGATECALL
93-
| OpcodeId::STATICCALL => {
94-
let call = call.unwrap();
95-
assert_eq!(call.call_type, step.op, "{call:?}");
96-
let code_idx = if block.transactions[er_idx].to.is_none() {
97-
0
98-
} else {
99-
1
100-
};
101-
let callee_code = data.get_code_at(code_idx);
102-
let addr = call.to.unwrap();
103-
trace_code(
104-
&mut codes,
105-
callee_code.unwrap_or_default(),
106-
step,
107-
Some(addr),
108-
sdb,
109-
block,
110-
);
111-
}
112-
OpcodeId::EXTCODECOPY => {
113-
let code = data.get_code_at(0);
114-
if code.is_none() {
115-
log::warn!("unable to fetch code from step. {step:?}");
116-
continue;
117-
}
118-
log::info!("trace extcodecopy! block {:?}", block.header.number);
119-
trace_code(&mut codes, code.unwrap(), step, None, sdb, block);
120-
}
121-
122-
_ => {}
123-
}
124-
}
125-
}
4+
/// Collect bytecodes from trace
5+
pub fn collect_codes(block: &BlockTrace) -> Result<Vec<(H256, Vec<u8>)>, Error> {
6+
if block.codes.is_empty() {
7+
return Err(Error::TracingError(format!(
8+
"codes not available for block {:?}",
9+
block.header.number
10+
)));
12611
}
127-
128-
log::debug!("collect codes done");
129-
Ok(codes)
130-
}
131-
132-
fn trace_code(
133-
codes: &mut Vec<(H256, Vec<u8>)>,
134-
code: Bytes,
135-
step: &ExecStep,
136-
addr: Option<Address>,
137-
// sdb is used to read codehash if available without recomputing
138-
sdb: Option<&StateDB>,
139-
block: &BlockTrace,
140-
) {
141-
let code_hash = addr.and_then(|addr| {
142-
sdb.and_then(|sdb| {
143-
let (_existed, acc_data) = sdb.get_account(&addr);
144-
if acc_data.code_hash != CodeDB::empty_code_hash() && !code.is_empty() {
145-
Some(acc_data.code_hash)
146-
} else {
147-
None
148-
}
149-
})
150-
});
151-
let code_hash = match code_hash {
152-
Some(code_hash) if !code_hash.is_zero() => code_hash,
153-
_ => {
154-
let hash = CodeDB::hash(&code);
155-
log::debug!(
156-
"hash_code done: addr {addr:?}, size {}, hash {hash:?}, step {:?}, gas.left {:?}, block {:?}",
157-
&code.len(),
158-
step.op,
159-
step.gas,
160-
block.header.number,
161-
);
162-
hash
163-
}
164-
};
165-
codes.push((code_hash, code.to_vec()));
166-
log::trace!(
167-
"trace code addr {:?}, size {} hash {:?}",
168-
addr,
169-
&code.len(),
170-
code_hash
171-
);
172-
}
173-
174-
fn decode_bytecode(bytecode: &str) -> Result<Vec<u8>, Error> {
175-
let mut stripped = if let Some(stripped) = bytecode.strip_prefix("0x") {
176-
stripped.to_string()
177-
} else {
178-
bytecode.to_string()
179-
};
180-
181-
let bytecode_len = stripped.len() as u64;
182-
if (bytecode_len & 1) != 0 {
183-
stripped = format!("0{stripped}");
184-
}
185-
186-
hex::decode(stripped).map_err(Error::HexError)
12+
Ok(block
13+
.codes
14+
.iter()
15+
.map(|b| (b.hash, b.code.to_vec()))
16+
.collect_vec())
18717
}

geth-utils/l2geth/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.21
44

55
require (
66
github.com/imdario/mergo v0.3.16
7-
github.com/scroll-tech/go-ethereum v1.10.14-0.20240621133406-517e5b4b0764
7+
github.com/scroll-tech/go-ethereum v1.10.14-0.20240717120140-0360eba83660
88
)
99

1010
require (

go.work.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,8 @@ github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD
331331
github.com/schollz/closestmatch v2.1.0+incompatible/go.mod h1:RtP1ddjLong6gTkbtmuhtR2uUrrJOpYzYRvbcPAid+g=
332332
github.com/scroll-tech/go-ethereum v1.10.14-0.20231108100028-cb76ecd42bf7 h1:xDtuJk3CjD46kHw87Xe9o/1PcvTVgNZYoT2vGgRmO5s=
333333
github.com/scroll-tech/go-ethereum v1.10.14-0.20231108100028-cb76ecd42bf7/go.mod h1:4HrFcoStbViFVy/9l/rvKl1XmizVAaPdgqI8v0U8hOc=
334+
github.com/scroll-tech/go-ethereum v1.10.14-0.20240717120140-0360eba83660 h1:pSFXa3YwuzNiMJAzYDQ4XDml2b5ioDVZ5FJsDPTu/4c=
335+
github.com/scroll-tech/go-ethereum v1.10.14-0.20240717120140-0360eba83660/go.mod h1:e3uA1ySy33Pl+B0PBW5/TbHoLhUtojzjPCYT2a7YdJc=
334336
github.com/sirupsen/logrus v1.2.0 h1:juTguoYk5qI21pwyTXY3B3Y5cOTH3ZUyZCg1v/mihuo=
335337
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
336338
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72 h1:qLC7fQah7D6K1B0ujays3HV9gkFtllcxhzImRR7ArPQ=

zkevm-circuits/src/copy_circuit/test.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use crate::{
44
copy_circuit::*,
55
evm_circuit::{test::rand_bytes, witness::block_convert},
6+
test_util::CircuitTestBuilder,
67
util::unusable_rows,
78
witness::Block,
89
};
@@ -285,7 +286,7 @@ fn gen_tx_log_data() -> CircuitInputBuilder {
285286
builder
286287
}
287288

288-
fn gen_access_list_data() -> CircuitInputBuilder {
289+
fn gen_access_list_data() -> Block {
289290
let test_access_list = AccessList(vec![
290291
AccessListItem {
291292
address: address!("0x0000000000000000000000000000000000001111"),
@@ -318,13 +319,9 @@ fn gen_access_list_data() -> CircuitInputBuilder {
318319
|block, _tx| block.number(0xcafeu64),
319320
)
320321
.unwrap();
321-
let block: GethData = test_ctx.into();
322-
let mut builder = BlockData::new_from_geth_data(block.clone()).new_circuit_input_builder();
323-
builder
324-
.handle_block(&block.eth_block, &block.geth_traces)
325-
.unwrap();
326-
327-
builder
322+
CircuitTestBuilder::new_from_test_ctx(test_ctx)
323+
.build_witness_block()
324+
.0
328325
}
329326

330327
fn gen_create_data() -> CircuitInputBuilder {
@@ -421,8 +418,7 @@ fn copy_circuit_valid_tx_log() {
421418

422419
#[test]
423420
fn copy_circuit_valid_access_list() {
424-
let builder = gen_access_list_data();
425-
let block = block_convert(&builder.block, &builder.code_db).unwrap();
421+
let block = gen_access_list_data();
426422
assert_eq!(test_copy_circuit_from_block(block), Ok(()));
427423
}
428424

0 commit comments

Comments
 (0)