core/state/snapshot: fix binary iterator

This commit is contained in:
Martin Holst Swende 2024-11-14 15:39:06 +01:00
parent e9fc202fa1
commit 5fcf093668
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
2 changed files with 38 additions and 27 deletions

View file

@ -117,10 +117,23 @@ func (dl *diffLayer) initBinaryStorageIterator(account, seek common.Hash) Iterat
// or an error if iteration failed for some reason (e.g. root being iterated
// becomes stale and garbage collected).
func (it *binaryIterator) Next() bool {
for {
ok := it.next()
if !ok {
return ok
}
if len(it.Account()) == 0 && len(it.Slot()) == 0 {
continue
}
return ok
}
}
func (it *binaryIterator) next() bool {
if it.aDone && it.bDone {
return false
}
first:
for {
if it.aDone {
it.k = it.b.Hash()
it.bDone = !it.b.Next()
@ -139,12 +152,13 @@ first:
} else if diff == 0 {
// Now we need to advance one of them
it.aDone = !it.a.Next()
goto first
continue
}
it.bDone = !it.b.Next()
it.k = nextB
return true
}
}
// Error returns any failure that occurred during iteration, which might have
// caused a premature iteration exit (e.g. snapshot stack becoming stale).

View file

@ -99,9 +99,6 @@ func TestStorageIteratorBasics(t *testing.T) {
for account := range accounts {
it, _ := diffLayer.StorageIterator(account, common.Hash{})
verifyIterator(t, 100, it, verifyNothing) // Nil is allowed for single layer iterator
it = diffLayer.newBinaryStorageIterator(account, common.Hash{})
verifyIterator(t, 100, it, verifyNothing) // Nil is allowed for single layer iterator
}
diskLayer := diffToDisk(diffLayer)
@ -569,7 +566,7 @@ func TestAccountIteratorFlattening(t *testing.T) {
})
t.Run("binary", func(t *testing.T) {
testAccountIteratorFlattening(t, func(snaps *Tree, root, seek common.Hash) AccountIterator {
return snaps.layers[root].(*diffLayer).newBinaryAccountIterator(common.Hash{})
return snaps.layers[root].(*diffLayer).newBinaryAccountIterator(seek)
})
})
@ -771,7 +768,7 @@ func TestAccountIteratorDeletions(t *testing.T) {
})
t.Run("binary", func(t *testing.T) {
testAccountIteratorDeletions(t, func(snaps *Tree, root, seek common.Hash) AccountIterator {
return snaps.layers[root].(*diffLayer).newBinaryAccountIterator(common.Hash{})
return snaps.layers[root].(*diffLayer).newBinaryAccountIterator(seek)
})
})
@ -833,7 +830,7 @@ func TestStorageIteratorDeletions(t *testing.T) {
})
t.Run("binary", func(t *testing.T) {
testStorageIteratorDeletions(t, func(snaps *Tree, root, account, seek common.Hash) StorageIterator {
return snaps.layers[root].(*diffLayer).newBinaryStorageIterator(account, common.Hash{})
return snaps.layers[root].(*diffLayer).newBinaryStorageIterator(account, seek)
})
})
}