Skip to content

Commit

Permalink
core/state: tests on the binary iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
holiman committed Nov 14, 2024
1 parent 77f3ef3 commit 3a9c1de
Showing 1 changed file with 37 additions and 6 deletions.
43 changes: 37 additions & 6 deletions core/state/snapshot/iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,21 @@ func TestStorageIteratorSeek(t *testing.T) {
// deleted accounts (where the Account() value is nil). The iterator
// should not output any accounts or nil-values for those cases.
func TestAccountIteratorDeletions(t *testing.T) {
t.Run("fast", func(t *testing.T) {
testAccountIteratorDeletions(t, func(root common.Hash, snaps *Tree) AccountIterator {
it, _ := snaps.AccountIterator(common.HexToHash("0x04"), common.Hash{})
return it
})
})
t.Run("binary", func(t *testing.T) {
testAccountIteratorDeletions(t, func(root common.Hash, snaps *Tree) AccountIterator {
return snaps.layers[root].(*diffLayer).newBinaryAccountIterator()

})
})
}

func testAccountIteratorDeletions(t *testing.T, newIterator func(root common.Hash, snaps *Tree) AccountIterator) {
// Create an empty base layer and a snapshot tree out of it
base := &diskLayer{
diskdb: rawdb.NewMemoryDatabase(),
Expand All @@ -739,7 +754,8 @@ func TestAccountIteratorDeletions(t *testing.T) {
nil, randomAccountSet("0x33", "0x44", "0x55"), nil)

// The output should be 11,33,44,55
it, _ := snaps.AccountIterator(common.HexToHash("0x04"), common.Hash{})
it := newIterator(common.HexToHash("0x04"), snaps)

// Do a quick check
verifyIterator(t, 4, it, verifyAccount)
it.Release()
Expand All @@ -759,6 +775,20 @@ func TestAccountIteratorDeletions(t *testing.T) {
}

func TestStorageIteratorDeletions(t *testing.T) {
t.Run("fast", func(t *testing.T) {
testStorageIteratorDeletions(t, func(snaps *Tree, root, account, seek common.Hash) StorageIterator {
it, _ := snaps.StorageIterator(root, account, seek)
return it
})
})
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)
})
})
}

func testStorageIteratorDeletions(t *testing.T, newIterator func(snaps *Tree, root, account, seek common.Hash) StorageIterator) {
// Create an empty base layer and a snapshot tree out of it
base := &diskLayer{
diskdb: rawdb.NewMemoryDatabase(),
Expand All @@ -778,12 +808,12 @@ func TestStorageIteratorDeletions(t *testing.T) {
randomAccountSet("0xaa"), randomStorageSet([]string{"0xaa"}, [][]string{{"0x02", "0x04", "0x06"}}, [][]string{{"0x01", "0x03"}}))

// The output should be 02,04,05,06
it, _ := snaps.StorageIterator(common.HexToHash("0x03"), common.HexToHash("0xaa"), common.Hash{})
it := newIterator(snaps, common.HexToHash("0x03"), common.HexToHash("0xaa"), common.Hash{})
verifyIterator(t, 4, it, verifyStorage)
it.Release()

// The output should be 04,05,06
it, _ = snaps.StorageIterator(common.HexToHash("0x03"), common.HexToHash("0xaa"), common.HexToHash("0x03"))
it = newIterator(snaps, common.HexToHash("0x03"), common.HexToHash("0xaa"), common.HexToHash("0x03"))
verifyIterator(t, 3, it, verifyStorage)
it.Release()

Expand All @@ -793,7 +823,7 @@ func TestStorageIteratorDeletions(t *testing.T) {
}
snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"), destructed, nil, nil)

it, _ = snaps.StorageIterator(common.HexToHash("0x04"), common.HexToHash("0xaa"), common.Hash{})
it = newIterator(snaps, common.HexToHash("0x04"), common.HexToHash("0xaa"), common.Hash{})
verifyIterator(t, 0, it, verifyStorage)
it.Release()

Expand All @@ -802,13 +832,14 @@ func TestStorageIteratorDeletions(t *testing.T) {
randomAccountSet("0xaa"), randomStorageSet([]string{"0xaa"}, [][]string{{"0x07", "0x08", "0x09"}}, nil))

// The output should be 07,08,09
it, _ = snaps.StorageIterator(common.HexToHash("0x05"), common.HexToHash("0xaa"), common.Hash{})

it = newIterator(snaps, common.HexToHash("0x05"), common.HexToHash("0xaa"), common.Hash{})
verifyIterator(t, 3, it, verifyStorage)
it.Release()

// Destruct the whole storage but re-create the account in the same layer
snaps.Update(common.HexToHash("0x06"), common.HexToHash("0x05"), destructed, randomAccountSet("0xaa"), randomStorageSet([]string{"0xaa"}, [][]string{{"0x11", "0x12"}}, nil))
it, _ = snaps.StorageIterator(common.HexToHash("0x06"), common.HexToHash("0xaa"), common.Hash{})
it = newIterator(snaps, common.HexToHash("0x06"), common.HexToHash("0xaa"), common.Hash{})
verifyIterator(t, 2, it, verifyStorage) // The output should be 11,12
it.Release()

Expand Down

0 comments on commit 3a9c1de

Please sign in to comment.