1
+ #![ allow( dead_code) ]
1
2
use anyhow:: Result ;
2
3
use ethers_providers:: { Http , Provider } ;
3
- use itertools:: Itertools ;
4
4
use prover:: {
5
5
inner:: Prover ,
6
6
utils:: init_env_and_log,
7
7
zkevm:: circuit:: {
8
8
block_traces_to_witness_block, calculate_row_usage_of_witness_block, SuperCircuit ,
9
- SUB_CIRCUIT_NAMES ,
10
9
} ,
11
10
} ;
12
11
use reqwest:: Url ;
@@ -32,118 +31,117 @@ async fn main() {
32
31
for i in setting. begin_batch ..=setting. end_batch {
33
32
log:: info!( "mock-testnet: requesting block traces of batch {i}" ) ;
34
33
35
- let block_traces = match setting. prove_type {
36
- ProveType :: Batch => get_traces_by_batch_api ( & provider, & setting, i) . await ,
37
- ProveType :: Block => get_traces_by_block_api ( & provider, & setting, i) . await ,
38
- } ;
39
-
40
- let block_traces = block_traces
41
- . unwrap_or_else ( |_| panic ! ( "mock-testnet: failed to request API with batch-{i}" ) ) ;
42
-
43
- if let Some ( block_traces) = block_traces {
44
- let rows_only = true ;
45
- let result = ( || {
46
- if rows_only {
47
- let gas_total: u64 = block_traces
48
- . iter ( )
49
- . map ( |b| b. header . gas_used . as_u64 ( ) )
50
- . sum ( ) ;
51
- let witness_block = block_traces_to_witness_block ( & block_traces) ?;
52
- let rows = calculate_row_usage_of_witness_block ( & witness_block) ?;
53
- log:: info!(
54
- "rows of batch {}(block range {:?} to {:?}):" ,
55
- i,
56
- block_traces. first( ) . and_then( |b| b. header. number) ,
57
- block_traces. last( ) . and_then( |b| b. header. number) ,
58
- ) ;
59
- for ( c, r) in SUB_CIRCUIT_NAMES . iter ( ) . zip_eq ( rows. iter ( ) ) {
60
- log:: info!( "rows of {}: {}" , c, r) ;
34
+ let chunks = get_traces_by_block_api ( & setting, i) . await ;
35
+
36
+ let chunks =
37
+ chunks. unwrap_or_else ( |_| panic ! ( "mock-testnet: failed to request API with batch-{i}" ) ) ;
38
+
39
+ match chunks {
40
+ None => {
41
+ log:: info!( "mock-testnet: finished to prove at batch-{i}" ) ;
42
+ break ;
43
+ }
44
+ Some ( chunks) => {
45
+ for chunk in chunks {
46
+ let ii = chunk. index ;
47
+ log:: info!( "chunk {:?}" , chunk) ;
48
+
49
+ // fetch traces
50
+ let mut block_traces: Vec < BlockTrace > = vec ! [ ] ;
51
+ for i in chunk. start_block_number ..=chunk. end_block_number {
52
+ log:: info!( "mock-testnet: requesting trace of block {i}" ) ;
53
+
54
+ let trace = provider
55
+ . request ( "scroll_getBlockTraceByNumberOrHash" , [ format ! ( "{i:#x}" ) ] )
56
+ . await
57
+ . unwrap ( ) ;
58
+ block_traces. push ( trace) ;
59
+ }
60
+
61
+ // mock prove or estimate rows
62
+ let rows_only = true ;
63
+ let result = ( || {
64
+ if rows_only {
65
+ let gas_total: u64 = block_traces
66
+ . iter ( )
67
+ . map ( |b| b. header . gas_used . as_u64 ( ) )
68
+ . sum ( ) ;
69
+ let witness_block = block_traces_to_witness_block ( & block_traces) ?;
70
+ let rows = calculate_row_usage_of_witness_block ( & witness_block) ?;
71
+ log:: info!(
72
+ "rows of batch {}(block range {:?} to {:?}):" ,
73
+ i,
74
+ block_traces. first( ) . and_then( |b| b. header. number) ,
75
+ block_traces. last( ) . and_then( |b| b. header. number) ,
76
+ ) ;
77
+ for r in & rows {
78
+ log:: info!( "rows of {}: {}" , r. name, r. row_num_real) ;
79
+ }
80
+ let row_num = rows. iter ( ) . map ( |x| x. row_num_real ) . max ( ) . unwrap ( ) ;
81
+ log:: info!(
82
+ "final rows of chunk {}: row {}, gas {}, gas/row {:.2}" ,
83
+ ii,
84
+ row_num,
85
+ gas_total,
86
+ gas_total as f64 / row_num as f64
87
+ ) ;
88
+ Ok ( ( ) )
89
+ } else {
90
+ Prover :: < SuperCircuit > :: mock_prove_target_circuit_batch ( & block_traces)
91
+ }
92
+ } ) ( ) ;
93
+ match result {
94
+ Ok ( _) => {
95
+ log:: info!(
96
+ "mock-testnet: succeeded to prove {ii}th chunk inside batch {i}"
97
+ )
98
+ }
99
+ Err ( err) => log:: error!(
100
+ "mock-testnet: failed to prove {ii}th chunk inside batch {i}:\n {err:?}"
101
+ ) ,
61
102
}
62
- let row_num = rows. iter ( ) . max ( ) . unwrap ( ) ;
63
- log:: info!(
64
- "final rows of batch {}: row {}, gas {}, gas/row {:.2}" ,
65
- i,
66
- row_num,
67
- gas_total,
68
- gas_total as f64 / * row_num as f64
69
- ) ;
70
- Ok ( ( ) )
71
- } else {
72
- Prover :: < SuperCircuit > :: mock_prove_target_circuit_batch ( & block_traces)
73
103
}
74
- } ) ( ) ;
75
- match result {
76
- Ok ( _) => log:: info!( "mock-testnet: succeeded to prove batch-{i}" ) ,
77
- Err ( err) => log:: error!( "mock-testnet: failed to prove batch-{i}:\n {err:?}" ) ,
78
104
}
79
- } else {
80
- log:: info!( "mock-testnet: finished to prove at batch-{i}" ) ;
81
- break ;
82
105
}
83
106
}
84
107
85
108
log:: info!( "mock-testnet: end" ) ;
86
109
}
87
110
88
- /// Request block traces by API `l2_getTracesByBatchIndex`. Return None for no more batches.
89
- async fn get_traces_by_batch_api (
90
- provider : & Provider < Http > ,
91
- _setting : & Setting ,
92
- batch_index : i64 ,
93
- ) -> Result < Option < Vec < BlockTrace > > > {
94
- // TODO: need to test this API.
95
- Ok ( Some (
96
- provider
97
- . request ( "l2_getTracesByBatchIndex" , [ format ! ( "{batch_index:#x}" ) ] )
98
- . await ?,
99
- ) )
100
- }
101
-
102
- /// Request block traces by API `scroll_getBlockTraceByNumberOrHash`. Return None for no more
103
- /// batches.
111
+ /// Request block traces by first using rollup API to get chunk info, then fetching blocks from
112
+ /// l2geth. Return None if no more batches.
104
113
async fn get_traces_by_block_api (
105
- provider : & Provider < Http > ,
106
114
setting : & Setting ,
107
115
batch_index : i64 ,
108
- ) -> Result < Option < Vec < BlockTrace > > > {
116
+ ) -> Result < Option < Vec < ChunkInfo > > > {
109
117
let url = Url :: parse_with_params (
110
118
& setting. rollupscan_api_url ,
111
- & [ ( "index " , batch_index. to_string ( ) ) ] ,
119
+ & [ ( "batch_index " , batch_index. to_string ( ) ) ] ,
112
120
) ?;
113
121
114
122
let resp: RollupscanResponse = reqwest:: get ( url) . await ?. json ( ) . await ?;
115
-
116
- Ok ( if let Some ( batch) = resp. batch {
117
- let mut traces = vec ! [ ] ;
118
- for i in batch. start_block_number ..=batch. end_block_number {
119
- log:: info!( "mock-testnet: requesting trace of block {i}" ) ;
120
-
121
- let trace = provider
122
- . request ( "scroll_getBlockTraceByNumberOrHash" , [ format ! ( "{i:#x}" ) ] )
123
- . await ?;
124
- traces. push ( trace) ;
125
- }
126
-
127
- Some ( traces)
128
- } else {
129
- None
130
- } )
123
+ log:: info!( "handling batch {}" , resp. batch_index) ;
124
+ Ok ( resp. chunks )
131
125
}
132
126
133
- #[ derive( Deserialize ) ]
127
+ #[ derive( Deserialize , Debug ) ]
134
128
struct RollupscanResponse {
135
- batch : Option < RollupscanBatch > ,
129
+ batch_index : usize ,
130
+ chunks : Option < Vec < ChunkInfo > > ,
136
131
}
137
132
138
- #[ derive( Deserialize ) ]
139
- struct RollupscanBatch {
133
+ #[ derive( Deserialize , Debug ) ]
134
+ struct ChunkInfo {
135
+ index : i64 ,
136
+ created_at : String ,
137
+ total_tx_num : i64 ,
138
+ hash : String ,
140
139
start_block_number : i64 ,
141
140
end_block_number : i64 ,
142
141
}
143
142
144
143
#[ derive( Debug ) ]
145
144
struct Setting {
146
- prove_type : ProveType ,
147
145
begin_batch : i64 ,
148
146
end_batch : i64 ,
149
147
l2geth_api_url : String ,
@@ -154,13 +152,9 @@ impl Setting {
154
152
pub fn new ( ) -> Self {
155
153
let l2geth_api_url =
156
154
env:: var ( "L2GETH_API_URL" ) . expect ( "mock-testnet: Must set env L2GETH_API_URL" ) ;
157
- let prove_type = env:: var ( "PROVE_TYPE" ) . ok ( ) . unwrap_or_default ( ) . into ( ) ;
158
155
let rollupscan_api_url = env:: var ( "ROLLUPSCAN_API_URL" ) ;
159
- let rollupscan_api_url = match prove_type {
160
- ProveType :: Batch => rollupscan_api_url. unwrap_or_default ( ) ,
161
- ProveType :: Block => rollupscan_api_url
162
- . expect ( "mock-testnet: Must set env ROLLUPSCAN_API_URL for block type" ) ,
163
- } ;
156
+ let rollupscan_api_url =
157
+ rollupscan_api_url. unwrap_or_else ( |_| "http://10.0.3.119:8560/api/chunks" . to_string ( ) ) ;
164
158
let begin_batch = env:: var ( "PROVE_BEGIN_BATCH" )
165
159
. ok ( )
166
160
. and_then ( |n| n. parse ( ) . ok ( ) )
@@ -171,26 +165,10 @@ impl Setting {
171
165
. unwrap_or ( DEFAULT_END_BATCH ) ;
172
166
173
167
Self {
174
- prove_type,
175
168
begin_batch,
176
169
end_batch,
177
170
l2geth_api_url,
178
171
rollupscan_api_url,
179
172
}
180
173
}
181
174
}
182
-
183
- #[ derive( Debug ) ]
184
- enum ProveType {
185
- Batch ,
186
- Block ,
187
- }
188
-
189
- impl From < String > for ProveType {
190
- fn from ( s : String ) -> Self {
191
- match s. as_str ( ) {
192
- "batch" => Self :: Batch ,
193
- _ => Self :: Block ,
194
- }
195
- }
196
- }
0 commit comments