Skip to content

Commit eff7cfb

Browse files
authored
core/state/snapshot: handle deleted accounts in fast iterator
1 parent 328de18 commit eff7cfb

2 files changed

Lines changed: 78 additions & 9 deletions

File tree

‎core/state/snapshot/iterator_fast.go‎

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,35 @@ func (fi *fastAccountIterator) Next() bool {
164164
fi.curAccount = fi.iterators[0].it.Account()
165165
if innerErr := fi.iterators[0].it.Error(); innerErr != nil {
166166
fi.fail = innerErr
167+
return false
167168
}
168-
return fi.Error() == nil
169-
}
170-
if !fi.next(0) {
171-
return false
169+
if fi.curAccount != nil {
170+
return true
171+
}
172+
// Implicit else: we've hit a nil-account, and need to fall through to the
173+
// loop below to land on something non-nil
172174
}
173-
fi.curAccount = fi.iterators[0].it.Account()
174-
if innerErr := fi.iterators[0].it.Error(); innerErr != nil {
175-
fi.fail = innerErr
175+
// If an account is deleted in one of the layers, the key will still be there,
176+
// but the actual value will be nil. However, the iterator should not
177+
// export nil-values (but instead simply omit the key), so we need to loop
178+
// here until we either
179+
// - get a non-nil value,
180+
// - hit an error,
181+
// - or exhaust the iterator
182+
for {
183+
if !fi.next(0) {
184+
return false // exhausted
185+
}
186+
fi.curAccount = fi.iterators[0].it.Account()
187+
if innerErr := fi.iterators[0].it.Error(); innerErr != nil {
188+
fi.fail = innerErr
189+
return false // error
190+
}
191+
if fi.curAccount != nil {
192+
break // non-nil value found
193+
}
176194
}
177-
return fi.Error() == nil
195+
return true
178196
}
179197

180198
// next handles the next operation internally and should be invoked when we know

‎core/state/snapshot/iterator_test.go‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)