@@ -130,9 +130,13 @@ func verifyIterator(t *testing.T, expCount int, it AccountIterator) {
130130 last = common.Hash {}
131131 )
132132 for it .Next () {
133- if hash := it .Hash (); bytes .Compare (last [:], hash [:]) >= 0 {
133+ hash := it .Hash ()
134+ if bytes .Compare (last [:], hash [:]) >= 0 {
134135 t .Errorf ("wrong order: %x >= %x" , last , hash )
135136 }
137+ if it .Account () == nil {
138+ t .Errorf ("iterator returned nil-value for hash %x" , hash )
139+ }
136140 count ++
137141 }
138142 if count != expCount {
@@ -377,6 +381,53 @@ func TestAccountIteratorSeek(t *testing.T) {
377381 verifyIterator (t , 0 , it ) // expected: nothing
378382}
379383
384+ // TestIteratorDeletions tests that the iterator behaves correct when there are
385+ // deleted accounts (where the Account() value is nil). The iterator
386+ // should not output any accounts or nil-values for those cases.
387+ func TestIteratorDeletions (t * testing.T ) {
388+ // Create an empty base layer and a snapshot tree out of it
389+ base := & diskLayer {
390+ diskdb : rawdb .NewMemoryDatabase (),
391+ root : common .HexToHash ("0x01" ),
392+ cache : fastcache .New (1024 * 500 ),
393+ }
394+ snaps := & Tree {
395+ layers : map [common.Hash ]snapshot {
396+ base .root : base ,
397+ },
398+ }
399+ // Stack three diff layers on top with various overlaps
400+ snaps .Update (common .HexToHash ("0x02" ), common .HexToHash ("0x01" ),
401+ randomAccountSet ("0x11" , "0x22" , "0x33" ), nil )
402+
403+ set := randomAccountSet ("0x11" , "0x22" , "0x33" )
404+ deleted := common .HexToHash ("0x22" )
405+ set [deleted ] = nil
406+ snaps .Update (common .HexToHash ("0x03" ), common .HexToHash ("0x02" ), set , nil )
407+
408+ snaps .Update (common .HexToHash ("0x04" ), common .HexToHash ("0x03" ),
409+ randomAccountSet ("0x33" , "0x44" , "0x55" ), nil )
410+
411+ // The output should be 11,33,44,55
412+ it , _ := snaps .AccountIterator (common .HexToHash ("0x04" ), common.Hash {})
413+ // Do a quick check
414+ verifyIterator (t , 4 , it )
415+ it .Release ()
416+
417+ // And a more detailed verification that we indeed do not see '0x22'
418+ it , _ = snaps .AccountIterator (common .HexToHash ("0x04" ), common.Hash {})
419+ defer it .Release ()
420+ for it .Next () {
421+ hash := it .Hash ()
422+ if it .Account () == nil {
423+ t .Errorf ("iterator returned nil-value for hash %x" , hash )
424+ }
425+ if hash == deleted {
426+ t .Errorf ("expected deleted elem %x to not be returned by iterator" , deleted )
427+ }
428+ }
429+ }
430+
380431// BenchmarkAccountIteratorTraversal is a bit a bit notorious -- all layers contain the
381432// exact same 200 accounts. That means that we need to process 2000 items, but
382433// only spit out 200 values eventually.
0 commit comments