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 } ;
4
8
use ethers_core:: types:: { Bytes , U256 } ;
5
9
use eth_types:: {
6
10
self ,
7
11
l2_types:: { BlockTrace , ExecStep , EthBlock } ,
8
12
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 ,
15
14
} ;
15
+ use std:: collections:: hash_map:: Entry ;
16
16
17
17
impl From < & AccountData > for state_db:: Account {
18
18
@@ -41,7 +41,7 @@ impl From<&ZktrieState> for StateDB {
41
41
for ( storage_key, data) in mpt_state. storage ( ) {
42
42
//TODO: add an warning on non-existed account?
43
43
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 ( ) ) ;
45
45
}
46
46
47
47
sdb
@@ -165,7 +165,7 @@ fn update_codedb(cdb: &mut CodeDB, sdb: &StateDB, block: &BlockTrace) {
165
165
1
166
166
} ;
167
167
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 :?}" ) ;
169
169
let code_hash = match step. op {
170
170
OpcodeId :: CALL | OpcodeId :: CALLCODE => data. get_code_hash_at ( 1 ) ,
171
171
OpcodeId :: STATICCALL => data. get_code_hash_at ( 0 ) ,
@@ -179,7 +179,7 @@ fn update_codedb(cdb: &mut CodeDB, sdb: &StateDB, block: &BlockTrace) {
179
179
}
180
180
OpcodeId :: EXTCODESIZE | OpcodeId :: EXTCODECOPY => {
181
181
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 :?}" ) ;
183
183
trace_code ( cdb, None , code. unwrap ( ) , step, sdb, 0 ) ;
184
184
}
185
185
@@ -200,15 +200,49 @@ fn dump_code_db(cdb: &CodeDB){
200
200
}
201
201
202
202
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
+
203
238
/// Create a new CircuitInputBuilder from the given `l2_trace` and `circuits_params`
204
239
pub fn new_from_l2_trace (
205
240
circuits_params : CircuitsParams ,
206
241
l2_trace : & BlockTrace ,
207
242
more : bool ,
208
- ) -> Self {
243
+ ) -> Result < Self , Error > {
209
244
210
245
let chain_id = l2_trace. chain_id ;
211
- let start_l1_queue_index = l2_trace. start_l1_queue_index ;
212
246
213
247
let mut code_db = CodeDB :: new ( ) ;
214
248
code_db. insert ( Vec :: new ( ) ) ;
@@ -263,12 +297,24 @@ impl CircuitInputBuilder {
263
297
let mut builder_block = circuit_input_builder:: Block :: from_headers ( & [ ] , circuits_params) ;
264
298
builder_block. chain_id = chain_id;
265
299
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)
271
310
}
272
311
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
+
273
319
}
274
320
0 commit comments