@@ -211,27 +211,27 @@ func TestAccountIteratorTraversalValues(t *testing.T) {
211
211
h = make (map [common.Hash ][]byte )
212
212
)
213
213
for i := byte (2 ); i < 0xff ; i ++ {
214
- a [common.Hash {i }] = [] byte ( fmt .Sprintf ( "layer-%d, key %d" , 0 , i ) )
214
+ a [common.Hash {i }] = fmt .Appendf ( nil , "layer-%d, key %d" , 0 , i )
215
215
if i > 20 && i % 2 == 0 {
216
- b [common.Hash {i }] = [] byte ( fmt .Sprintf ( "layer-%d, key %d" , 1 , i ) )
216
+ b [common.Hash {i }] = fmt .Appendf ( nil , "layer-%d, key %d" , 1 , i )
217
217
}
218
218
if i % 4 == 0 {
219
- c [common.Hash {i }] = [] byte ( fmt .Sprintf ( "layer-%d, key %d" , 2 , i ) )
219
+ c [common.Hash {i }] = fmt .Appendf ( nil , "layer-%d, key %d" , 2 , i )
220
220
}
221
221
if i % 7 == 0 {
222
- d [common.Hash {i }] = [] byte ( fmt .Sprintf ( "layer-%d, key %d" , 3 , i ) )
222
+ d [common.Hash {i }] = fmt .Appendf ( nil , "layer-%d, key %d" , 3 , i )
223
223
}
224
224
if i % 8 == 0 {
225
- e [common.Hash {i }] = [] byte ( fmt .Sprintf ( "layer-%d, key %d" , 4 , i ) )
225
+ e [common.Hash {i }] = fmt .Appendf ( nil , "layer-%d, key %d" , 4 , i )
226
226
}
227
227
if i > 50 || i < 85 {
228
- f [common.Hash {i }] = [] byte ( fmt .Sprintf ( "layer-%d, key %d" , 5 , i ) )
228
+ f [common.Hash {i }] = fmt .Appendf ( nil , "layer-%d, key %d" , 5 , i )
229
229
}
230
230
if i % 64 == 0 {
231
- g [common.Hash {i }] = [] byte ( fmt .Sprintf ( "layer-%d, key %d" , 6 , i ) )
231
+ g [common.Hash {i }] = fmt .Appendf ( nil , "layer-%d, key %d" , 6 , i )
232
232
}
233
233
if i % 128 == 0 {
234
- h [common.Hash {i }] = [] byte ( fmt .Sprintf ( "layer-%d, key %d" , 7 , i ) )
234
+ h [common.Hash {i }] = fmt .Appendf ( nil , "layer-%d, key %d" , 7 , i )
235
235
}
236
236
}
237
237
// Assemble a stack of snapshots from the account layers
@@ -258,6 +258,106 @@ func TestAccountIteratorTraversalValues(t *testing.T) {
258
258
t .Fatalf ("hash %x: account mismatch: have %x, want %x" , hash , have , want )
259
259
}
260
260
}
261
+ it .Release ()
262
+ }
263
+
264
+ func TestStorageIteratorTraversalValues (t * testing.T ) {
265
+ // Create an empty base layer and a snapshot tree out of it
266
+ base := & diskLayer {
267
+ diskdb : rawdb .NewMemoryDatabase (),
268
+ root : common .HexToHash ("0x01" ),
269
+ cache : fastcache .New (1024 * 500 ),
270
+ }
271
+ snaps := & Tree {
272
+ layers : map [common.Hash ]snapshot {
273
+ base .root : base ,
274
+ },
275
+ }
276
+ wrapStorage := func (storage map [common.Hash ][]byte ) map [common.Hash ]map [common.Hash ][]byte {
277
+ return map [common.Hash ]map [common.Hash ][]byte {
278
+ common .HexToHash ("0xaa" ): storage ,
279
+ }
280
+ }
281
+ // Create a batch of storage sets to seed subsequent layers with
282
+ var (
283
+ a = make (map [common.Hash ][]byte )
284
+ b = make (map [common.Hash ][]byte )
285
+ c = make (map [common.Hash ][]byte )
286
+ d = make (map [common.Hash ][]byte )
287
+ e = make (map [common.Hash ][]byte )
288
+ f = make (map [common.Hash ][]byte )
289
+ g = make (map [common.Hash ][]byte )
290
+ h = make (map [common.Hash ][]byte )
291
+ )
292
+ for i := byte (2 ); i < 0xff ; i ++ {
293
+ a [common.Hash {i }] = fmt .Appendf (nil , "layer-%d, key %d" , 0 , i )
294
+ if i > 20 && i % 2 == 0 {
295
+ b [common.Hash {i }] = fmt .Appendf (nil , "layer-%d, key %d" , 1 , i )
296
+ }
297
+ if i % 4 == 0 {
298
+ c [common.Hash {i }] = fmt .Appendf (nil , "layer-%d, key %d" , 2 , i )
299
+ }
300
+ if i % 7 == 0 {
301
+ d [common.Hash {i }] = fmt .Appendf (nil , "layer-%d, key %d" , 3 , i )
302
+ }
303
+ if i % 8 == 0 {
304
+ e [common.Hash {i }] = fmt .Appendf (nil , "layer-%d, key %d" , 4 , i )
305
+ }
306
+ if i > 50 || i < 85 {
307
+ f [common.Hash {i }] = fmt .Appendf (nil , "layer-%d, key %d" , 5 , i )
308
+ }
309
+ if i % 64 == 0 {
310
+ g [common.Hash {i }] = fmt .Appendf (nil , "layer-%d, key %d" , 6 , i )
311
+ }
312
+ if i % 128 == 0 {
313
+ h [common.Hash {i }] = fmt .Appendf (nil , "layer-%d, key %d" , 7 , i )
314
+ }
315
+ }
316
+ // Assemble a stack of snapshots from the account layers
317
+ snaps .Update (common .HexToHash ("0x02" ), common .HexToHash ("0x01" ), nil , randomAccountSet ("0xaa" ), wrapStorage (a ))
318
+ snaps .Update (common .HexToHash ("0x03" ), common .HexToHash ("0x02" ), nil , randomAccountSet ("0xaa" ), wrapStorage (b ))
319
+ snaps .Update (common .HexToHash ("0x04" ), common .HexToHash ("0x03" ), nil , randomAccountSet ("0xaa" ), wrapStorage (c ))
320
+ snaps .Update (common .HexToHash ("0x05" ), common .HexToHash ("0x04" ), nil , randomAccountSet ("0xaa" ), wrapStorage (d ))
321
+ snaps .Update (common .HexToHash ("0x06" ), common .HexToHash ("0x05" ), nil , randomAccountSet ("0xaa" ), wrapStorage (e ))
322
+ snaps .Update (common .HexToHash ("0x07" ), common .HexToHash ("0x06" ), nil , randomAccountSet ("0xaa" ), wrapStorage (e ))
323
+ snaps .Update (common .HexToHash ("0x08" ), common .HexToHash ("0x07" ), nil , randomAccountSet ("0xaa" ), wrapStorage (g ))
324
+ snaps .Update (common .HexToHash ("0x09" ), common .HexToHash ("0x08" ), nil , randomAccountSet ("0xaa" ), wrapStorage (h ))
325
+
326
+ it , _ := snaps .StorageIterator (common .HexToHash ("0x09" ), common .HexToHash ("0xaa" ), common.Hash {})
327
+ head := snaps .Snapshot (common .HexToHash ("0x09" ))
328
+ for it .Next () {
329
+ hash := it .Hash ()
330
+ want , err := head .Storage (common .HexToHash ("0xaa" ), hash )
331
+ if err != nil {
332
+ t .Fatalf ("failed to retrieve expected storage slot: %v" , err )
333
+ }
334
+ if have := it .Slot (); ! bytes .Equal (want , have ) {
335
+ t .Fatalf ("hash %x: slot mismatch: have %x, want %x" , hash , have , want )
336
+ }
337
+ }
338
+ it .Release ()
339
+
340
+ // Test after persist some bottom-most layers into the disk,
341
+ // the functionalities still work.
342
+ limit := aggregatorMemoryLimit
343
+ defer func () {
344
+ aggregatorMemoryLimit = limit
345
+ }()
346
+ aggregatorMemoryLimit = 0 // Force pushing the bottom-most layer into disk
347
+ snaps .Cap (common .HexToHash ("0x09" ), 2 )
348
+
349
+ it , _ = snaps .StorageIterator (common .HexToHash ("0x09" ), common .HexToHash ("0xaa" ), common.Hash {})
350
+ for it .Next () {
351
+ hash := it .Hash ()
352
+ want , err := head .Storage (common .HexToHash ("0xaa" ), hash )
353
+ if err != nil {
354
+ t .Fatalf ("failed to retrieve expected slot: %v" , err )
355
+ }
356
+ if have := it .Slot (); ! bytes .Equal (want , have ) {
357
+ t .Fatalf ("hash %x: slot mismatch: have %x, want %x" , hash , have , want )
358
+ }
359
+ }
360
+ it .Release ()
261
361
}
262
362
263
363
// This testcase is notorious, all layers contain the exact same 200 accounts.
0 commit comments