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

Commit d949847

Browse files
committed
use l2 trace in super circuit test
1 parent cdb62f6 commit d949847

File tree

3 files changed

+124
-84
lines changed

3 files changed

+124
-84
lines changed

bus-mapping/src/circuit_input_builder.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod input_state_ref;
99
#[cfg(test)]
1010
mod tracer_tests;
1111
mod transaction;
12+
#[cfg(feature="scroll")]
1213
mod l2;
1314

1415
use self::access::gen_state_access_trace;

bus-mapping/src/circuit_input_builder/l2.rs

Lines changed: 65 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
use mpt_zktrie::state::{ZktrieState, AccountData, StorageData};
2-
use crate::state_db::{self, CodeDB, StateDB};
3-
use crate::circuit_input_builder::{self, CircuitInputBuilder, CircuitsParams};
1+
use mpt_zktrie::state::{ZktrieState, AccountData};
2+
use crate::{
3+
error::Error,
4+
state_db::{self, CodeDB, StateDB},
5+
circuit_input_builder::{self, CircuitInputBuilder, BlockHead, CircuitsParams},
6+
};
7+
pub use super::block::{Block, BlockContext};
48
use ethers_core::types::{Bytes, U256};
59
use eth_types::{
610
self,
711
l2_types::{BlockTrace, ExecStep, EthBlock},
812
evm_types::OpcodeId,
9-
geth_types,
10-
sign_types::{pk_bytes_le, pk_bytes_swap_endianness, SignData},
11-
ToAddress, Address, GethExecStep, GethExecTrace, ToBigEndian, ToWord, Word, H256,
12-
};
13-
use std::{
14-
collections::{hash_map::Entry, HashMap},
13+
ToAddress, H256,
1514
};
15+
use std::collections::hash_map::Entry;
1616

1717
impl From<&AccountData> for state_db::Account {
1818

@@ -41,7 +41,7 @@ impl From<&ZktrieState> for StateDB {
4141
for (storage_key, data) in mpt_state.storage() {
4242
//TODO: add an warning on non-existed account?
4343
let (_, acc) = sdb.get_account_mut(&storage_key.0);
44-
acc.storage.insert(*&storage_key.1, *data.as_ref());
44+
acc.storage.insert(storage_key.1, *data.as_ref());
4545
}
4646

4747
sdb
@@ -165,7 +165,7 @@ fn update_codedb(cdb: &mut CodeDB, sdb: &StateDB, block: &BlockTrace) {
165165
1
166166
};
167167
let callee_code = data.get_code_at(code_idx);
168-
assert!(callee_code.is_none(), "invalid trace: cannot get code of call: {:?}", step);
168+
assert!(callee_code.is_none(), "invalid trace: cannot get code of call: {step:?}");
169169
let code_hash = match step.op {
170170
OpcodeId::CALL | OpcodeId::CALLCODE => data.get_code_hash_at(1),
171171
OpcodeId::STATICCALL => data.get_code_hash_at(0),
@@ -179,7 +179,7 @@ fn update_codedb(cdb: &mut CodeDB, sdb: &StateDB, block: &BlockTrace) {
179179
}
180180
OpcodeId::EXTCODESIZE | OpcodeId::EXTCODECOPY => {
181181
let code = data.get_code_at(0);
182-
assert!(code.is_none(), "invalid trace: cannot get code of ext: {:?}", step);
182+
assert!(code.is_none(), "invalid trace: cannot get code of ext: {step:?}");
183183
trace_code(cdb, None, code.unwrap(), step, sdb, 0);
184184
}
185185

@@ -200,15 +200,49 @@ fn dump_code_db(cdb: &CodeDB){
200200
}
201201

202202
impl CircuitInputBuilder {
203+
204+
fn apply_l2_trace(
205+
&mut self,
206+
block_trace: &BlockTrace,
207+
is_last: bool,
208+
) -> Result<(), Error>{
209+
let geth_trace : Vec<eth_types::GethExecTrace>
210+
= block_trace.execution_results.iter().map(From::from).collect();
211+
let eth_block: EthBlock = block_trace.clone().into();
212+
assert_eq!(
213+
self.block.chain_id, block_trace.chain_id,
214+
"unexpected chain id in new block_trace"
215+
);
216+
// TODO: Get the history_hashes.
217+
let mut header = BlockHead::new_with_l1_queue_index(
218+
self.block.chain_id,
219+
block_trace.start_l1_queue_index,
220+
Vec::new(),
221+
&eth_block,
222+
)?;
223+
// override zeroed minder field with additional "coinbase" field in blocktrace
224+
if let Some(address) = block_trace.coinbase.address {
225+
header.coinbase = address;
226+
}
227+
let block_num = header.number.as_u64();
228+
self.block.start_l1_queue_index = block_trace.start_l1_queue_index;
229+
// TODO: should be check the block number is in sequence?
230+
self.block.headers.insert(block_num, header);
231+
// note the actions when `handle_rwc_reversion` argument (the 4th one)
232+
// is true is executing outside this closure
233+
self.handle_block_inner(&eth_block, &geth_trace, false, is_last)?;
234+
log::debug!("handle_block_inner done for block {:?}", block_num);
235+
Ok(())
236+
}
237+
203238
/// Create a new CircuitInputBuilder from the given `l2_trace` and `circuits_params`
204239
pub fn new_from_l2_trace(
205240
circuits_params: CircuitsParams,
206241
l2_trace: &BlockTrace,
207242
more: bool,
208-
) -> Self {
243+
) -> Result<Self, Error> {
209244

210245
let chain_id = l2_trace.chain_id;
211-
let start_l1_queue_index = l2_trace.start_l1_queue_index;
212246

213247
let mut code_db = CodeDB::new();
214248
code_db.insert(Vec::new());
@@ -263,12 +297,24 @@ impl CircuitInputBuilder {
263297
let mut builder_block = circuit_input_builder::Block::from_headers(&[], circuits_params);
264298
builder_block.chain_id = chain_id;
265299
builder_block.prev_state_root = U256::from(mpt_state.root());
266-
let mut builder = CircuitInputBuilder::new(sdb, code_db, &builder_block);
267-
268-
let eth_block: EthBlock = l2_trace.clone().into();
269-
270-
builder
300+
let mut builder = Self {
301+
sdb,
302+
code_db,
303+
block: builder_block,
304+
block_ctx: BlockContext::new(),
305+
mpt_state,
306+
};
307+
308+
builder.apply_l2_trace(l2_trace, !more)?;
309+
Ok(builder)
271310
}
272311

312+
/// make finalize actions on building, must called after
313+
/// all block trace have been input
314+
pub fn finalize_building(&mut self) -> Result<(), Error>{
315+
self.set_value_ops_call_context_rwc_eor();
316+
self.set_end_block()
317+
}
318+
273319
}
274320

0 commit comments

Comments
 (0)