1
1
use crate :: Block ;
2
2
use alloy:: primitives:: { Address , Bytes , B256 , U256 } ;
3
- use serde:: { Deserialize , Serialize } ;
3
+ use rkyv:: Archive ;
4
+ use serde:: { Deserialize , Deserializer , Serialize } ;
4
5
use serde_with:: { serde_as, Map } ;
6
+ use std:: collections:: { BTreeSet , HashMap } ;
7
+ use std:: fmt:: Debug ;
8
+ use std:: hash:: Hash ;
5
9
6
10
mod tx;
7
11
pub use tx:: { ArchivedTransactionTrace , TransactionTrace , TxL1Msg , TypedTransaction } ;
@@ -58,16 +62,15 @@ pub struct BytecodeTrace {
58
62
}
59
63
60
64
/// storage trace
61
- #[ serde_as]
62
65
#[ derive(
63
66
rkyv:: Archive ,
64
67
rkyv:: Serialize ,
65
68
rkyv:: Deserialize ,
66
69
Serialize ,
67
- Deserialize ,
68
70
Default ,
69
71
Debug ,
70
72
Clone ,
73
+ Hash ,
71
74
Eq ,
72
75
PartialEq ,
73
76
) ]
@@ -82,8 +85,45 @@ pub struct StorageTrace {
82
85
pub root_after : B256 ,
83
86
/// proofs
84
87
#[ serde( rename = "flattenProofs" ) ]
88
+ pub flatten_proofs : Vec < Bytes > ,
89
+ }
90
+
91
+ /// legacy storage trace
92
+ #[ serde_as]
93
+ #[ derive(
94
+ rkyv:: Archive ,
95
+ rkyv:: Serialize ,
96
+ rkyv:: Deserialize ,
97
+ Serialize ,
98
+ Deserialize ,
99
+ Default ,
100
+ Debug ,
101
+ Clone ,
102
+ Hash ,
103
+ Eq ,
104
+ PartialEq ,
105
+ ) ]
106
+ #[ archive( check_bytes) ]
107
+ #[ archive_attr( derive( Debug , Hash , PartialEq , Eq ) ) ]
108
+ #[ allow( clippy:: type_complexity) ]
109
+ pub struct LegacyStorageTrace {
110
+ /// root before
111
+ #[ serde( rename = "rootBefore" ) ]
112
+ pub root_before : B256 ,
113
+ /// root after
114
+ #[ serde( rename = "rootAfter" ) ]
115
+ pub root_after : B256 ,
116
+ /// account proofs
117
+ #[ serde( default ) ]
85
118
#[ serde_as( as = "Map<_, _>" ) ]
86
- pub flatten_proofs : Vec < ( B256 , Bytes ) > ,
119
+ pub proofs : Vec < ( Address , Vec < Bytes > ) > ,
120
+ #[ serde( rename = "storageProofs" , default ) ]
121
+ #[ serde_as( as = "Map<_, Map<_, _>>" ) ]
122
+ /// storage proofs for each account
123
+ pub storage_proofs : Vec < ( Address , Vec < ( B256 , Vec < Bytes > ) > ) > ,
124
+ #[ serde( rename = "deletionProofs" , default ) ]
125
+ /// additional deletion proofs
126
+ pub deletion_proofs : Vec < Bytes > ,
87
127
}
88
128
89
129
/// Block trace format
@@ -94,7 +134,11 @@ pub struct StorageTrace {
94
134
) ]
95
135
#[ archive( check_bytes) ]
96
136
#[ archive_attr( derive( Debug , Hash , PartialEq , Eq ) ) ]
97
- pub struct BlockTrace {
137
+ pub struct BlockTrace < S = StorageTrace >
138
+ where
139
+ S : Archive ,
140
+ <S as Archive >:: Archived : Debug + Hash + PartialEq + Eq ,
141
+ {
98
142
/// chain id
99
143
#[ serde( rename = "chainID" , default ) ]
100
144
pub chain_id : u64 ,
@@ -108,14 +152,86 @@ pub struct BlockTrace {
108
152
pub codes : Vec < BytecodeTrace > ,
109
153
/// storage trace BEFORE execution
110
154
#[ serde( rename = "storageTrace" ) ]
111
- pub storage_trace : StorageTrace ,
155
+ pub storage_trace : S ,
112
156
/// l1 tx queue
113
157
#[ serde( rename = "startL1QueueIndex" , default ) ]
114
158
pub start_l1_queue_index : u64 ,
115
159
/// Withdraw root
116
160
pub withdraw_trie_root : B256 ,
117
161
}
118
162
163
+ impl < ' de > Deserialize < ' de > for StorageTrace {
164
+ fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
165
+ where
166
+ D : Deserializer < ' de > ,
167
+ {
168
+ #[ derive( Deserialize ) ]
169
+ #[ serde( untagged) ]
170
+ enum FlattenProofs {
171
+ Map ( HashMap < B256 , Bytes > ) ,
172
+ Vec ( Vec < Bytes > ) ,
173
+ }
174
+ #[ derive( Deserialize ) ]
175
+ struct StorageTraceDe {
176
+ #[ serde( rename = "rootBefore" ) ]
177
+ pub root_before : B256 ,
178
+ #[ serde( rename = "rootAfter" ) ]
179
+ pub root_after : B256 ,
180
+ #[ serde( rename = "flattenProofs" ) ]
181
+ pub flatten_proofs : FlattenProofs ,
182
+ }
183
+
184
+ let de = StorageTraceDe :: deserialize ( deserializer) ?;
185
+ let mut flatten_proofs = match de. flatten_proofs {
186
+ FlattenProofs :: Map ( map) => map. into_values ( ) . collect ( ) ,
187
+ FlattenProofs :: Vec ( vec) => vec,
188
+ } ;
189
+ flatten_proofs. sort ( ) ;
190
+
191
+ Ok ( StorageTrace {
192
+ root_before : de. root_before ,
193
+ root_after : de. root_after ,
194
+ flatten_proofs,
195
+ } )
196
+ }
197
+ }
198
+
199
+ impl From < LegacyStorageTrace > for StorageTrace {
200
+ fn from ( trace : LegacyStorageTrace ) -> Self {
201
+ let mut flatten_proofs = BTreeSet :: new ( ) ;
202
+ for ( _, proofs) in trace. proofs {
203
+ flatten_proofs. extend ( proofs) ;
204
+ }
205
+ for ( _, proofs) in trace. storage_proofs {
206
+ for ( _, proofs) in proofs {
207
+ flatten_proofs. extend ( proofs) ;
208
+ }
209
+ }
210
+ flatten_proofs. extend ( trace. deletion_proofs ) ;
211
+
212
+ StorageTrace {
213
+ root_before : trace. root_before ,
214
+ root_after : trace. root_after ,
215
+ flatten_proofs : flatten_proofs. into_iter ( ) . collect ( ) ,
216
+ }
217
+ }
218
+ }
219
+
220
+ impl From < BlockTrace < LegacyStorageTrace > > for BlockTrace {
221
+ fn from ( trace : BlockTrace < LegacyStorageTrace > ) -> Self {
222
+ BlockTrace {
223
+ chain_id : trace. chain_id ,
224
+ coinbase : trace. coinbase ,
225
+ header : trace. header ,
226
+ transactions : trace. transactions ,
227
+ codes : trace. codes ,
228
+ storage_trace : trace. storage_trace . into ( ) ,
229
+ start_l1_queue_index : trace. start_l1_queue_index ,
230
+ withdraw_trie_root : trace. withdraw_trie_root ,
231
+ }
232
+ }
233
+ }
234
+
119
235
impl Block for BlockTrace {
120
236
type Tx = TransactionTrace ;
121
237
@@ -179,11 +295,8 @@ impl Block for BlockTrace {
179
295
self . start_l1_queue_index
180
296
}
181
297
182
- fn flatten_proofs ( & self ) -> impl Iterator < Item = ( & B256 , & [ u8 ] ) > {
183
- self . storage_trace
184
- . flatten_proofs
185
- . iter ( )
186
- . map ( |( k, v) | ( k, v. as_ref ( ) ) )
298
+ fn flatten_proofs ( & self ) -> impl Iterator < Item = & [ u8 ] > {
299
+ self . storage_trace . flatten_proofs . iter ( ) . map ( |v| v. as_ref ( ) )
187
300
}
188
301
}
189
302
@@ -254,11 +367,8 @@ impl Block for ArchivedBlockTrace {
254
367
self . start_l1_queue_index
255
368
}
256
369
257
- fn flatten_proofs ( & self ) -> impl Iterator < Item = ( & B256 , & [ u8 ] ) > {
258
- self . storage_trace
259
- . flatten_proofs
260
- . iter ( )
261
- . map ( |( k, v) | ( k, v. as_ref ( ) ) )
370
+ fn flatten_proofs ( & self ) -> impl Iterator < Item = & [ u8 ] > {
371
+ self . storage_trace . flatten_proofs . iter ( ) . map ( |v| v. as_ref ( ) )
262
372
}
263
373
}
264
374
@@ -404,8 +514,7 @@ mod tests {
404
514
. iter ( )
405
515
. zip ( archived_block. storage_trace . flatten_proofs . iter ( ) )
406
516
{
407
- assert_eq ! ( proof. 0 , archived_proof. 0 ) ;
408
- assert_eq ! ( proof. 1 . as_ref( ) , archived_proof. 1 . as_ref( ) ) ;
517
+ assert_eq ! ( proof. as_ref( ) , archived_proof. as_ref( ) ) ;
409
518
}
410
519
411
520
assert_eq ! (
0 commit comments