Skip to content

Commit c036fe3

Browse files
committed
core/state/snapshot: make difflayer account iterator seek operation inclusive
1 parent 7599999 commit c036fe3

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

‎core/state/snapshot/iterator.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (dl *diffLayer) AccountIterator(seek common.Hash) AccountIterator {
7070
// Seek out the requested starting account
7171
hashes := dl.AccountList()
7272
index := sort.Search(len(hashes), func(i int) bool {
73-
return bytes.Compare(seek[:], hashes[i][:]) < 0
73+
return bytes.Compare(seek[:], hashes[i][:]) <= 0
7474
})
7575
// Assemble and returned the already seeked iterator
7676
return &diffAccountIterator{

‎core/state/snapshot/iterator_test.go‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,34 +351,47 @@ func TestAccountIteratorSeek(t *testing.T) {
351351
snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"), nil,
352352
randomAccountSet("0xcc", "0xf0", "0xff"), nil)
353353

354-
// Construct various iterators and ensure their tranversal is correct
354+
// Account set is now
355+
// 02: aa, ee, f0, ff
356+
// 03: aa, bb, dd, ee, f0 (, f0), ff
357+
// 04: aa, bb, cc, dd, ee, f0 (, f0), ff (, ff)
358+
// Construct various iterators and ensure their traversal is correct
355359
it, _ := snaps.AccountIterator(common.HexToHash("0x02"), common.HexToHash("0xdd"))
356360
defer it.Release()
357361
verifyIterator(t, 3, it) // expected: ee, f0, ff
358362

359363
it, _ = snaps.AccountIterator(common.HexToHash("0x02"), common.HexToHash("0xaa"))
360364
defer it.Release()
361-
verifyIterator(t, 3, it) // expected: ee, f0, ff
365+
verifyIterator(t, 4, it) // expected: aa, ee, f0, ff
362366

363367
it, _ = snaps.AccountIterator(common.HexToHash("0x02"), common.HexToHash("0xff"))
364368
defer it.Release()
369+
verifyIterator(t, 1, it) // expected: ff
370+
371+
it, _ = snaps.AccountIterator(common.HexToHash("0x02"), common.HexToHash("0xff1"))
372+
defer it.Release()
365373
verifyIterator(t, 0, it) // expected: nothing
366374

367375
it, _ = snaps.AccountIterator(common.HexToHash("0x04"), common.HexToHash("0xbb"))
368376
defer it.Release()
369-
verifyIterator(t, 5, it) // expected: cc, dd, ee, f0, ff
377+
verifyIterator(t, 6, it) // expected: bb, cc, dd, ee, f0, ff
370378

371379
it, _ = snaps.AccountIterator(common.HexToHash("0x04"), common.HexToHash("0xef"))
372380
defer it.Release()
373381
verifyIterator(t, 2, it) // expected: f0, ff
374382

375383
it, _ = snaps.AccountIterator(common.HexToHash("0x04"), common.HexToHash("0xf0"))
376384
defer it.Release()
377-
verifyIterator(t, 1, it) // expected: ff
385+
verifyIterator(t, 2, it) // expected: f0, ff
378386

379387
it, _ = snaps.AccountIterator(common.HexToHash("0x04"), common.HexToHash("0xff"))
380388
defer it.Release()
389+
verifyIterator(t, 1, it) // expected: ff
390+
391+
it, _ = snaps.AccountIterator(common.HexToHash("0x04"), common.HexToHash("0xff1"))
392+
defer it.Release()
381393
verifyIterator(t, 0, it) // expected: nothing
394+
382395
}
383396

384397
// TestIteratorDeletions tests that the iterator behaves correct when there are

0 commit comments

Comments
 (0)