2019-12-05 07:37:25 -06:00
|
|
|
// Copyright 2019 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package snapshot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-02-16 13:36:58 -06:00
|
|
|
crand "crypto/rand"
|
2019-12-05 07:37:25 -06:00
|
|
|
"encoding/binary"
|
2019-12-06 16:27:18 -06:00
|
|
|
"fmt"
|
2019-12-05 07:37:25 -06:00
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
|
2019-12-10 03:00:03 -06:00
|
|
|
"github.com/VictoriaMetrics/fastcache"
|
2019-12-05 07:37:25 -06:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-12-10 03:00:03 -06:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2019-12-05 07:37:25 -06:00
|
|
|
)
|
|
|
|
|
2020-04-29 04:53:08 -05:00
|
|
|
// TestAccountIteratorBasics tests some simple single-layer(diff and disk) iteration
|
2020-03-03 07:52:00 -06:00
|
|
|
func TestAccountIteratorBasics(t *testing.T) {
|
2019-12-05 07:37:25 -06:00
|
|
|
var (
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
accounts = make(map[common.Hash][]byte)
|
|
|
|
storage = make(map[common.Hash]map[common.Hash][]byte)
|
2019-12-05 07:37:25 -06:00
|
|
|
)
|
|
|
|
// Fill up a parent
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
h := randomHash()
|
|
|
|
data := randomAccount()
|
2020-03-03 07:52:00 -06:00
|
|
|
|
2019-12-05 07:37:25 -06:00
|
|
|
accounts[h] = data
|
2020-03-03 07:52:00 -06:00
|
|
|
if rand.Intn(2) == 0 {
|
2019-12-05 07:37:25 -06:00
|
|
|
accStorage := make(map[common.Hash][]byte)
|
|
|
|
value := make([]byte, 32)
|
2023-02-16 13:36:58 -06:00
|
|
|
crand.Read(value)
|
2019-12-05 07:37:25 -06:00
|
|
|
accStorage[randomHash()] = value
|
|
|
|
storage[h] = accStorage
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Add some (identical) layers on top
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
diffLayer := newDiffLayer(emptyLayer(), common.Hash{}, copyAccounts(accounts), copyStorage(storage))
|
2020-04-29 04:53:08 -05:00
|
|
|
it := diffLayer.AccountIterator(common.Hash{})
|
|
|
|
verifyIterator(t, 100, it, verifyNothing) // Nil is allowed for single layer iterator
|
|
|
|
|
2024-11-15 00:59:06 -06:00
|
|
|
it = diffLayer.newBinaryAccountIterator(common.Hash{})
|
|
|
|
verifyIterator(t, 100, it, verifyNothing) // Nil is allowed for single layer iterator
|
|
|
|
|
2020-04-29 04:53:08 -05:00
|
|
|
diskLayer := diffToDisk(diffLayer)
|
|
|
|
it = diskLayer.AccountIterator(common.Hash{})
|
|
|
|
verifyIterator(t, 100, it, verifyNothing) // Nil is allowed for single layer iterator
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestStorageIteratorBasics tests some simple single-layer(diff and disk) iteration for storage
|
|
|
|
func TestStorageIteratorBasics(t *testing.T) {
|
|
|
|
var (
|
|
|
|
nilStorage = make(map[common.Hash]int)
|
|
|
|
accounts = make(map[common.Hash][]byte)
|
|
|
|
storage = make(map[common.Hash]map[common.Hash][]byte)
|
|
|
|
)
|
|
|
|
// Fill some random data
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
h := randomHash()
|
|
|
|
accounts[h] = randomAccount()
|
|
|
|
|
|
|
|
accStorage := make(map[common.Hash][]byte)
|
|
|
|
value := make([]byte, 32)
|
|
|
|
|
|
|
|
var nilstorage int
|
|
|
|
for i := 0; i < 100; i++ {
|
2023-02-16 13:36:58 -06:00
|
|
|
crand.Read(value)
|
2020-04-29 04:53:08 -05:00
|
|
|
if rand.Intn(2) == 0 {
|
|
|
|
accStorage[randomHash()] = common.CopyBytes(value)
|
|
|
|
} else {
|
|
|
|
accStorage[randomHash()] = nil // delete slot
|
|
|
|
nilstorage += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
storage[h] = accStorage
|
|
|
|
nilStorage[h] = nilstorage
|
|
|
|
}
|
|
|
|
// Add some (identical) layers on top
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
diffLayer := newDiffLayer(emptyLayer(), common.Hash{}, copyAccounts(accounts), copyStorage(storage))
|
2020-04-29 04:53:08 -05:00
|
|
|
for account := range accounts {
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
it := diffLayer.StorageIterator(account, common.Hash{})
|
2020-04-29 04:53:08 -05:00
|
|
|
verifyIterator(t, 100, it, verifyNothing) // Nil is allowed for single layer iterator
|
|
|
|
}
|
|
|
|
|
|
|
|
diskLayer := diffToDisk(diffLayer)
|
|
|
|
for account := range accounts {
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
it := diskLayer.StorageIterator(account, common.Hash{})
|
2020-04-29 04:53:08 -05:00
|
|
|
verifyIterator(t, 100-nilStorage[account], it, verifyNothing) // Nil is allowed for single layer iterator
|
|
|
|
}
|
2019-12-05 07:37:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type testIterator struct {
|
|
|
|
values []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTestIterator(values ...byte) *testIterator {
|
|
|
|
return &testIterator{values}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ti *testIterator) Seek(common.Hash) {
|
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ti *testIterator) Next() bool {
|
|
|
|
ti.values = ti.values[1:]
|
2020-02-24 05:26:34 -06:00
|
|
|
return len(ti.values) > 0
|
2019-12-05 07:37:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ti *testIterator) Error() error {
|
2020-02-24 05:26:34 -06:00
|
|
|
return nil
|
2019-12-05 07:37:25 -06:00
|
|
|
}
|
|
|
|
|
2019-12-10 03:00:03 -06:00
|
|
|
func (ti *testIterator) Hash() common.Hash {
|
2019-12-05 07:37:25 -06:00
|
|
|
return common.BytesToHash([]byte{ti.values[0]})
|
|
|
|
}
|
|
|
|
|
2019-12-10 03:00:03 -06:00
|
|
|
func (ti *testIterator) Account() []byte {
|
2020-02-24 05:26:34 -06:00
|
|
|
return nil
|
2019-12-05 07:37:25 -06:00
|
|
|
}
|
|
|
|
|
2020-04-29 04:53:08 -05:00
|
|
|
func (ti *testIterator) Slot() []byte {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-10 03:00:03 -06:00
|
|
|
func (ti *testIterator) Release() {}
|
|
|
|
|
2019-12-05 07:37:25 -06:00
|
|
|
func TestFastIteratorBasics(t *testing.T) {
|
|
|
|
type testCase struct {
|
|
|
|
lists [][]byte
|
|
|
|
expKeys []byte
|
|
|
|
}
|
|
|
|
for i, tc := range []testCase{
|
|
|
|
{lists: [][]byte{{0, 1, 8}, {1, 2, 8}, {2, 9}, {4},
|
|
|
|
{7, 14, 15}, {9, 13, 15, 16}},
|
|
|
|
expKeys: []byte{0, 1, 2, 4, 7, 8, 9, 13, 14, 15, 16}},
|
|
|
|
{lists: [][]byte{{0, 8}, {1, 2, 8}, {7, 14, 15}, {8, 9},
|
|
|
|
{9, 10}, {10, 13, 15, 16}},
|
|
|
|
expKeys: []byte{0, 1, 2, 7, 8, 9, 10, 13, 14, 15, 16}},
|
|
|
|
} {
|
2020-04-29 04:53:08 -05:00
|
|
|
var iterators []*weightedIterator
|
2019-12-06 16:27:18 -06:00
|
|
|
for i, data := range tc.lists {
|
|
|
|
it := newTestIterator(data...)
|
2020-04-29 04:53:08 -05:00
|
|
|
iterators = append(iterators, &weightedIterator{it, i})
|
2019-12-05 07:37:25 -06:00
|
|
|
}
|
2020-04-29 04:53:08 -05:00
|
|
|
fi := &fastIterator{
|
2019-12-05 07:37:25 -06:00
|
|
|
iterators: iterators,
|
|
|
|
initiated: false,
|
|
|
|
}
|
|
|
|
count := 0
|
|
|
|
for fi.Next() {
|
2019-12-10 03:00:03 -06:00
|
|
|
if got, exp := fi.Hash()[31], tc.expKeys[count]; exp != got {
|
2019-12-05 07:37:25 -06:00
|
|
|
t.Errorf("tc %d, [%d]: got %d exp %d", i, count, got, exp)
|
|
|
|
}
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-29 04:53:08 -05:00
|
|
|
type verifyContent int
|
|
|
|
|
|
|
|
const (
|
|
|
|
verifyNothing verifyContent = iota
|
|
|
|
verifyAccount
|
|
|
|
verifyStorage
|
|
|
|
)
|
|
|
|
|
|
|
|
func verifyIterator(t *testing.T, expCount int, it Iterator, verify verifyContent) {
|
2019-12-10 03:00:03 -06:00
|
|
|
t.Helper()
|
|
|
|
|
2019-12-05 07:37:25 -06:00
|
|
|
var (
|
2019-12-10 03:00:03 -06:00
|
|
|
count = 0
|
|
|
|
last = common.Hash{}
|
2019-12-05 07:37:25 -06:00
|
|
|
)
|
|
|
|
for it.Next() {
|
2020-03-04 06:38:55 -06:00
|
|
|
hash := it.Hash()
|
|
|
|
if bytes.Compare(last[:], hash[:]) >= 0 {
|
2019-12-10 03:00:03 -06:00
|
|
|
t.Errorf("wrong order: %x >= %x", last, hash)
|
2019-12-05 07:37:25 -06:00
|
|
|
}
|
2020-04-29 04:53:08 -05:00
|
|
|
count++
|
|
|
|
if verify == verifyAccount && len(it.(AccountIterator).Account()) == 0 {
|
|
|
|
t.Errorf("iterator returned nil-value for hash %x", hash)
|
|
|
|
} else if verify == verifyStorage && len(it.(StorageIterator).Slot()) == 0 {
|
2020-03-04 06:38:55 -06:00
|
|
|
t.Errorf("iterator returned nil-value for hash %x", hash)
|
|
|
|
}
|
2020-04-29 04:53:08 -05:00
|
|
|
last = hash
|
2019-12-05 07:37:25 -06:00
|
|
|
}
|
2019-12-10 03:00:03 -06:00
|
|
|
if count != expCount {
|
|
|
|
t.Errorf("iterator count mismatch: have %d, want %d", count, expCount)
|
|
|
|
}
|
|
|
|
if err := it.Error(); err != nil {
|
|
|
|
t.Errorf("iterator failed: %v", err)
|
2019-12-05 07:37:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-03 07:52:00 -06:00
|
|
|
// TestAccountIteratorTraversal tests some simple multi-layer iteration.
|
|
|
|
func TestAccountIteratorTraversal(t *testing.T) {
|
2019-12-10 03:00:03 -06:00
|
|
|
// Create an empty base layer and a snapshot tree out of it
|
|
|
|
base := &diskLayer{
|
|
|
|
diskdb: rawdb.NewMemoryDatabase(),
|
|
|
|
root: common.HexToHash("0x01"),
|
|
|
|
cache: fastcache.New(1024 * 500),
|
2019-12-05 07:37:25 -06:00
|
|
|
}
|
2019-12-10 03:00:03 -06:00
|
|
|
snaps := &Tree{
|
|
|
|
layers: map[common.Hash]snapshot{
|
|
|
|
base.root: base,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// Stack three diff layers on top with various overlaps
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"),
|
2019-12-10 03:00:03 -06:00
|
|
|
randomAccountSet("0xaa", "0xee", "0xff", "0xf0"), nil)
|
|
|
|
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"),
|
2019-12-10 03:00:03 -06:00
|
|
|
randomAccountSet("0xbb", "0xdd", "0xf0"), nil)
|
2019-12-05 07:37:25 -06:00
|
|
|
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"),
|
2019-12-10 03:00:03 -06:00
|
|
|
randomAccountSet("0xcc", "0xf0", "0xff"), nil)
|
2019-12-05 07:37:25 -06:00
|
|
|
|
2019-12-10 03:00:03 -06:00
|
|
|
// Verify the single and multi-layer iterators
|
|
|
|
head := snaps.Snapshot(common.HexToHash("0x04"))
|
2019-12-05 07:37:25 -06:00
|
|
|
|
2020-04-29 04:53:08 -05:00
|
|
|
verifyIterator(t, 3, head.(snapshot).AccountIterator(common.Hash{}), verifyNothing)
|
2024-11-15 00:59:06 -06:00
|
|
|
verifyIterator(t, 7, head.(*diffLayer).newBinaryAccountIterator(common.Hash{}), verifyAccount)
|
2019-12-10 03:00:03 -06:00
|
|
|
|
|
|
|
it, _ := snaps.AccountIterator(common.HexToHash("0x04"), common.Hash{})
|
2020-04-29 04:53:08 -05:00
|
|
|
verifyIterator(t, 7, it, verifyAccount)
|
2020-04-24 06:43:49 -05:00
|
|
|
it.Release()
|
|
|
|
|
|
|
|
// Test after persist some bottom-most layers into the disk,
|
|
|
|
// the functionalities still work.
|
|
|
|
limit := aggregatorMemoryLimit
|
|
|
|
defer func() {
|
|
|
|
aggregatorMemoryLimit = limit
|
|
|
|
}()
|
|
|
|
aggregatorMemoryLimit = 0 // Force pushing the bottom-most layer into disk
|
|
|
|
snaps.Cap(common.HexToHash("0x04"), 2)
|
2024-11-15 00:59:06 -06:00
|
|
|
verifyIterator(t, 7, head.(*diffLayer).newBinaryAccountIterator(common.Hash{}), verifyAccount)
|
2019-12-10 03:00:03 -06:00
|
|
|
|
2020-04-24 06:43:49 -05:00
|
|
|
it, _ = snaps.AccountIterator(common.HexToHash("0x04"), common.Hash{})
|
2020-04-29 04:53:08 -05:00
|
|
|
verifyIterator(t, 7, it, verifyAccount)
|
|
|
|
it.Release()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStorageIteratorTraversal(t *testing.T) {
|
|
|
|
// Create an empty base layer and a snapshot tree out of it
|
|
|
|
base := &diskLayer{
|
|
|
|
diskdb: rawdb.NewMemoryDatabase(),
|
|
|
|
root: common.HexToHash("0x01"),
|
|
|
|
cache: fastcache.New(1024 * 500),
|
|
|
|
}
|
|
|
|
snaps := &Tree{
|
|
|
|
layers: map[common.Hash]snapshot{
|
|
|
|
base.root: base,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// Stack three diff layers on top with various overlaps
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"),
|
2020-04-29 04:53:08 -05:00
|
|
|
randomAccountSet("0xaa"), randomStorageSet([]string{"0xaa"}, [][]string{{"0x01", "0x02", "0x03"}}, nil))
|
|
|
|
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"),
|
2020-04-29 04:53:08 -05:00
|
|
|
randomAccountSet("0xaa"), randomStorageSet([]string{"0xaa"}, [][]string{{"0x04", "0x05", "0x06"}}, nil))
|
|
|
|
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"),
|
2020-04-29 04:53:08 -05:00
|
|
|
randomAccountSet("0xaa"), randomStorageSet([]string{"0xaa"}, [][]string{{"0x01", "0x02", "0x03"}}, nil))
|
|
|
|
|
|
|
|
// Verify the single and multi-layer iterators
|
|
|
|
head := snaps.Snapshot(common.HexToHash("0x04"))
|
|
|
|
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
diffIter := head.(snapshot).StorageIterator(common.HexToHash("0xaa"), common.Hash{})
|
2020-04-29 04:53:08 -05:00
|
|
|
verifyIterator(t, 3, diffIter, verifyNothing)
|
2024-11-15 00:59:06 -06:00
|
|
|
verifyIterator(t, 6, head.(*diffLayer).newBinaryStorageIterator(common.HexToHash("0xaa"), common.Hash{}), verifyStorage)
|
2020-04-29 04:53:08 -05:00
|
|
|
|
|
|
|
it, _ := snaps.StorageIterator(common.HexToHash("0x04"), common.HexToHash("0xaa"), common.Hash{})
|
|
|
|
verifyIterator(t, 6, it, verifyStorage)
|
|
|
|
it.Release()
|
|
|
|
|
|
|
|
// Test after persist some bottom-most layers into the disk,
|
|
|
|
// the functionalities still work.
|
|
|
|
limit := aggregatorMemoryLimit
|
|
|
|
defer func() {
|
|
|
|
aggregatorMemoryLimit = limit
|
|
|
|
}()
|
|
|
|
aggregatorMemoryLimit = 0 // Force pushing the bottom-most layer into disk
|
|
|
|
snaps.Cap(common.HexToHash("0x04"), 2)
|
2024-11-15 00:59:06 -06:00
|
|
|
verifyIterator(t, 6, head.(*diffLayer).newBinaryStorageIterator(common.HexToHash("0xaa"), common.Hash{}), verifyStorage)
|
2020-04-29 04:53:08 -05:00
|
|
|
|
|
|
|
it, _ = snaps.StorageIterator(common.HexToHash("0x04"), common.HexToHash("0xaa"), common.Hash{})
|
|
|
|
verifyIterator(t, 6, it, verifyStorage)
|
2020-04-24 06:43:49 -05:00
|
|
|
it.Release()
|
2019-12-05 07:37:25 -06:00
|
|
|
}
|
|
|
|
|
2020-03-03 07:52:00 -06:00
|
|
|
// TestAccountIteratorTraversalValues tests some multi-layer iteration, where we
|
2019-12-10 03:00:03 -06:00
|
|
|
// also expect the correct values to show up.
|
2020-03-03 07:52:00 -06:00
|
|
|
func TestAccountIteratorTraversalValues(t *testing.T) {
|
2019-12-10 03:00:03 -06:00
|
|
|
// Create an empty base layer and a snapshot tree out of it
|
|
|
|
base := &diskLayer{
|
|
|
|
diskdb: rawdb.NewMemoryDatabase(),
|
|
|
|
root: common.HexToHash("0x01"),
|
|
|
|
cache: fastcache.New(1024 * 500),
|
|
|
|
}
|
|
|
|
snaps := &Tree{
|
|
|
|
layers: map[common.Hash]snapshot{
|
|
|
|
base.root: base,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// Create a batch of account sets to seed subsequent layers with
|
2019-12-06 16:27:18 -06:00
|
|
|
var (
|
2019-12-10 03:00:03 -06:00
|
|
|
a = make(map[common.Hash][]byte)
|
|
|
|
b = make(map[common.Hash][]byte)
|
|
|
|
c = make(map[common.Hash][]byte)
|
|
|
|
d = make(map[common.Hash][]byte)
|
|
|
|
e = make(map[common.Hash][]byte)
|
|
|
|
f = make(map[common.Hash][]byte)
|
|
|
|
g = make(map[common.Hash][]byte)
|
|
|
|
h = make(map[common.Hash][]byte)
|
2019-12-06 16:27:18 -06:00
|
|
|
)
|
|
|
|
for i := byte(2); i < 0xff; i++ {
|
|
|
|
a[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 0, i))
|
|
|
|
if i > 20 && i%2 == 0 {
|
|
|
|
b[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 1, i))
|
|
|
|
}
|
|
|
|
if i%4 == 0 {
|
|
|
|
c[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 2, i))
|
|
|
|
}
|
|
|
|
if i%7 == 0 {
|
|
|
|
d[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 3, i))
|
|
|
|
}
|
|
|
|
if i%8 == 0 {
|
|
|
|
e[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 4, i))
|
|
|
|
}
|
|
|
|
if i > 50 || i < 85 {
|
|
|
|
f[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 5, i))
|
|
|
|
}
|
|
|
|
if i%64 == 0 {
|
|
|
|
g[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 6, i))
|
|
|
|
}
|
|
|
|
if i%128 == 0 {
|
|
|
|
h[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 7, i))
|
|
|
|
}
|
|
|
|
}
|
2019-12-10 03:00:03 -06:00
|
|
|
// Assemble a stack of snapshots from the account layers
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"), a, nil)
|
|
|
|
snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"), b, nil)
|
|
|
|
snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"), c, nil)
|
|
|
|
snaps.Update(common.HexToHash("0x05"), common.HexToHash("0x04"), d, nil)
|
|
|
|
snaps.Update(common.HexToHash("0x06"), common.HexToHash("0x05"), e, nil)
|
|
|
|
snaps.Update(common.HexToHash("0x07"), common.HexToHash("0x06"), f, nil)
|
|
|
|
snaps.Update(common.HexToHash("0x08"), common.HexToHash("0x07"), g, nil)
|
|
|
|
snaps.Update(common.HexToHash("0x09"), common.HexToHash("0x08"), h, nil)
|
2019-12-10 03:00:03 -06:00
|
|
|
|
|
|
|
it, _ := snaps.AccountIterator(common.HexToHash("0x09"), common.Hash{})
|
|
|
|
head := snaps.Snapshot(common.HexToHash("0x09"))
|
2019-12-06 16:27:18 -06:00
|
|
|
for it.Next() {
|
2019-12-10 03:00:03 -06:00
|
|
|
hash := it.Hash()
|
|
|
|
want, err := head.AccountRLP(hash)
|
2019-12-06 16:27:18 -06:00
|
|
|
if err != nil {
|
2019-12-10 03:00:03 -06:00
|
|
|
t.Fatalf("failed to retrieve expected account: %v", err)
|
2019-12-06 16:27:18 -06:00
|
|
|
}
|
2019-12-10 03:00:03 -06:00
|
|
|
if have := it.Account(); !bytes.Equal(want, have) {
|
|
|
|
t.Fatalf("hash %x: account mismatch: have %x, want %x", hash, have, want)
|
2019-12-06 16:27:18 -06:00
|
|
|
}
|
|
|
|
}
|
2020-04-24 06:43:49 -05:00
|
|
|
it.Release()
|
|
|
|
|
|
|
|
// Test after persist some bottom-most layers into the disk,
|
|
|
|
// the functionalities still work.
|
|
|
|
limit := aggregatorMemoryLimit
|
|
|
|
defer func() {
|
|
|
|
aggregatorMemoryLimit = limit
|
|
|
|
}()
|
|
|
|
aggregatorMemoryLimit = 0 // Force pushing the bottom-most layer into disk
|
|
|
|
snaps.Cap(common.HexToHash("0x09"), 2)
|
|
|
|
|
|
|
|
it, _ = snaps.AccountIterator(common.HexToHash("0x09"), common.Hash{})
|
|
|
|
for it.Next() {
|
|
|
|
hash := it.Hash()
|
|
|
|
want, err := head.AccountRLP(hash)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to retrieve expected account: %v", err)
|
|
|
|
}
|
|
|
|
if have := it.Account(); !bytes.Equal(want, have) {
|
|
|
|
t.Fatalf("hash %x: account mismatch: have %x, want %x", hash, have, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
it.Release()
|
2019-12-06 16:27:18 -06:00
|
|
|
}
|
|
|
|
|
2020-04-29 04:53:08 -05:00
|
|
|
func TestStorageIteratorTraversalValues(t *testing.T) {
|
|
|
|
// Create an empty base layer and a snapshot tree out of it
|
|
|
|
base := &diskLayer{
|
|
|
|
diskdb: rawdb.NewMemoryDatabase(),
|
|
|
|
root: common.HexToHash("0x01"),
|
|
|
|
cache: fastcache.New(1024 * 500),
|
|
|
|
}
|
|
|
|
snaps := &Tree{
|
|
|
|
layers: map[common.Hash]snapshot{
|
|
|
|
base.root: base,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
wrapStorage := func(storage map[common.Hash][]byte) map[common.Hash]map[common.Hash][]byte {
|
|
|
|
return map[common.Hash]map[common.Hash][]byte{
|
|
|
|
common.HexToHash("0xaa"): storage,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Create a batch of storage sets to seed subsequent layers with
|
|
|
|
var (
|
|
|
|
a = make(map[common.Hash][]byte)
|
|
|
|
b = make(map[common.Hash][]byte)
|
|
|
|
c = make(map[common.Hash][]byte)
|
|
|
|
d = make(map[common.Hash][]byte)
|
|
|
|
e = make(map[common.Hash][]byte)
|
|
|
|
f = make(map[common.Hash][]byte)
|
|
|
|
g = make(map[common.Hash][]byte)
|
|
|
|
h = make(map[common.Hash][]byte)
|
|
|
|
)
|
|
|
|
for i := byte(2); i < 0xff; i++ {
|
|
|
|
a[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 0, i))
|
|
|
|
if i > 20 && i%2 == 0 {
|
|
|
|
b[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 1, i))
|
|
|
|
}
|
|
|
|
if i%4 == 0 {
|
|
|
|
c[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 2, i))
|
|
|
|
}
|
|
|
|
if i%7 == 0 {
|
|
|
|
d[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 3, i))
|
|
|
|
}
|
|
|
|
if i%8 == 0 {
|
|
|
|
e[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 4, i))
|
|
|
|
}
|
|
|
|
if i > 50 || i < 85 {
|
|
|
|
f[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 5, i))
|
|
|
|
}
|
|
|
|
if i%64 == 0 {
|
|
|
|
g[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 6, i))
|
|
|
|
}
|
|
|
|
if i%128 == 0 {
|
|
|
|
h[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 7, i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Assemble a stack of snapshots from the account layers
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"), randomAccountSet("0xaa"), wrapStorage(a))
|
|
|
|
snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"), randomAccountSet("0xaa"), wrapStorage(b))
|
|
|
|
snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"), randomAccountSet("0xaa"), wrapStorage(c))
|
|
|
|
snaps.Update(common.HexToHash("0x05"), common.HexToHash("0x04"), randomAccountSet("0xaa"), wrapStorage(d))
|
|
|
|
snaps.Update(common.HexToHash("0x06"), common.HexToHash("0x05"), randomAccountSet("0xaa"), wrapStorage(e))
|
|
|
|
snaps.Update(common.HexToHash("0x07"), common.HexToHash("0x06"), randomAccountSet("0xaa"), wrapStorage(e))
|
|
|
|
snaps.Update(common.HexToHash("0x08"), common.HexToHash("0x07"), randomAccountSet("0xaa"), wrapStorage(g))
|
|
|
|
snaps.Update(common.HexToHash("0x09"), common.HexToHash("0x08"), randomAccountSet("0xaa"), wrapStorage(h))
|
2020-04-29 04:53:08 -05:00
|
|
|
|
|
|
|
it, _ := snaps.StorageIterator(common.HexToHash("0x09"), common.HexToHash("0xaa"), common.Hash{})
|
|
|
|
head := snaps.Snapshot(common.HexToHash("0x09"))
|
|
|
|
for it.Next() {
|
|
|
|
hash := it.Hash()
|
|
|
|
want, err := head.Storage(common.HexToHash("0xaa"), hash)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to retrieve expected storage slot: %v", err)
|
|
|
|
}
|
|
|
|
if have := it.Slot(); !bytes.Equal(want, have) {
|
|
|
|
t.Fatalf("hash %x: slot mismatch: have %x, want %x", hash, have, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
it.Release()
|
|
|
|
|
|
|
|
// Test after persist some bottom-most layers into the disk,
|
|
|
|
// the functionalities still work.
|
|
|
|
limit := aggregatorMemoryLimit
|
|
|
|
defer func() {
|
|
|
|
aggregatorMemoryLimit = limit
|
|
|
|
}()
|
|
|
|
aggregatorMemoryLimit = 0 // Force pushing the bottom-most layer into disk
|
|
|
|
snaps.Cap(common.HexToHash("0x09"), 2)
|
|
|
|
|
|
|
|
it, _ = snaps.StorageIterator(common.HexToHash("0x09"), common.HexToHash("0xaa"), common.Hash{})
|
|
|
|
for it.Next() {
|
|
|
|
hash := it.Hash()
|
|
|
|
want, err := head.Storage(common.HexToHash("0xaa"), hash)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to retrieve expected slot: %v", err)
|
|
|
|
}
|
|
|
|
if have := it.Slot(); !bytes.Equal(want, have) {
|
|
|
|
t.Fatalf("hash %x: slot mismatch: have %x, want %x", hash, have, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
it.Release()
|
|
|
|
}
|
|
|
|
|
2019-12-10 03:00:03 -06:00
|
|
|
// This testcase is notorious, all layers contain the exact same 200 accounts.
|
2020-03-03 07:52:00 -06:00
|
|
|
func TestAccountIteratorLargeTraversal(t *testing.T) {
|
2019-12-10 03:00:03 -06:00
|
|
|
// Create a custom account factory to recreate the same addresses
|
|
|
|
makeAccounts := func(num int) map[common.Hash][]byte {
|
2019-12-05 07:37:25 -06:00
|
|
|
accounts := make(map[common.Hash][]byte)
|
|
|
|
for i := 0; i < num; i++ {
|
|
|
|
h := common.Hash{}
|
|
|
|
binary.BigEndian.PutUint64(h[:], uint64(i+1))
|
|
|
|
accounts[h] = randomAccount()
|
|
|
|
}
|
|
|
|
return accounts
|
|
|
|
}
|
2019-12-10 03:00:03 -06:00
|
|
|
// Build up a large stack of snapshots
|
|
|
|
base := &diskLayer{
|
|
|
|
diskdb: rawdb.NewMemoryDatabase(),
|
|
|
|
root: common.HexToHash("0x01"),
|
|
|
|
cache: fastcache.New(1024 * 500),
|
|
|
|
}
|
|
|
|
snaps := &Tree{
|
|
|
|
layers: map[common.Hash]snapshot{
|
|
|
|
base.root: base,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for i := 1; i < 128; i++ {
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
snaps.Update(common.HexToHash(fmt.Sprintf("0x%02x", i+1)), common.HexToHash(fmt.Sprintf("0x%02x", i)), makeAccounts(200), nil)
|
2019-12-10 03:00:03 -06:00
|
|
|
}
|
|
|
|
// Iterate the entire stack and ensure everything is hit only once
|
|
|
|
head := snaps.Snapshot(common.HexToHash("0x80"))
|
2020-04-29 04:53:08 -05:00
|
|
|
verifyIterator(t, 200, head.(snapshot).AccountIterator(common.Hash{}), verifyNothing)
|
2024-11-15 00:59:06 -06:00
|
|
|
verifyIterator(t, 200, head.(*diffLayer).newBinaryAccountIterator(common.Hash{}), verifyAccount)
|
2019-12-10 03:00:03 -06:00
|
|
|
|
|
|
|
it, _ := snaps.AccountIterator(common.HexToHash("0x80"), common.Hash{})
|
2020-04-29 04:53:08 -05:00
|
|
|
verifyIterator(t, 200, it, verifyAccount)
|
2020-04-24 06:43:49 -05:00
|
|
|
it.Release()
|
|
|
|
|
|
|
|
// Test after persist some bottom-most layers into the disk,
|
|
|
|
// the functionalities still work.
|
|
|
|
limit := aggregatorMemoryLimit
|
|
|
|
defer func() {
|
|
|
|
aggregatorMemoryLimit = limit
|
|
|
|
}()
|
|
|
|
aggregatorMemoryLimit = 0 // Force pushing the bottom-most layer into disk
|
|
|
|
snaps.Cap(common.HexToHash("0x80"), 2)
|
|
|
|
|
2024-11-15 00:59:06 -06:00
|
|
|
verifyIterator(t, 200, head.(*diffLayer).newBinaryAccountIterator(common.Hash{}), verifyAccount)
|
2019-12-10 03:00:03 -06:00
|
|
|
|
2020-04-24 06:43:49 -05:00
|
|
|
it, _ = snaps.AccountIterator(common.HexToHash("0x80"), common.Hash{})
|
2020-04-29 04:53:08 -05:00
|
|
|
verifyIterator(t, 200, it, verifyAccount)
|
2020-04-24 06:43:49 -05:00
|
|
|
it.Release()
|
2019-12-05 07:37:25 -06:00
|
|
|
}
|
|
|
|
|
2020-03-03 07:52:00 -06:00
|
|
|
// TestAccountIteratorFlattening tests what happens when we
|
2019-12-10 03:00:03 -06:00
|
|
|
// - have a live iterator on child C (parent C1 -> C2 .. CN)
|
|
|
|
// - flattens C2 all the way into CN
|
|
|
|
// - continues iterating
|
2020-03-03 07:52:00 -06:00
|
|
|
func TestAccountIteratorFlattening(t *testing.T) {
|
2024-11-15 00:59:06 -06:00
|
|
|
t.Run("fast", func(t *testing.T) {
|
|
|
|
testAccountIteratorFlattening(t, func(snaps *Tree, root, seek common.Hash) AccountIterator {
|
|
|
|
it, _ := snaps.AccountIterator(root, seek)
|
|
|
|
return it
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("binary", func(t *testing.T) {
|
|
|
|
testAccountIteratorFlattening(t, func(snaps *Tree, root, seek common.Hash) AccountIterator {
|
|
|
|
return snaps.layers[root].(*diffLayer).newBinaryAccountIterator(seek)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccountIteratorFlattening(t *testing.T, newIterator func(snaps *Tree, root, seek common.Hash) AccountIterator) {
|
2019-12-10 03:00:03 -06:00
|
|
|
// Create an empty base layer and a snapshot tree out of it
|
|
|
|
base := &diskLayer{
|
|
|
|
diskdb: rawdb.NewMemoryDatabase(),
|
|
|
|
root: common.HexToHash("0x01"),
|
|
|
|
cache: fastcache.New(1024 * 500),
|
|
|
|
}
|
|
|
|
snaps := &Tree{
|
|
|
|
layers: map[common.Hash]snapshot{
|
|
|
|
base.root: base,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// Create a stack of diffs on top
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"),
|
2019-12-10 03:00:03 -06:00
|
|
|
randomAccountSet("0xaa", "0xee", "0xff", "0xf0"), nil)
|
|
|
|
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"),
|
2019-12-10 03:00:03 -06:00
|
|
|
randomAccountSet("0xbb", "0xdd", "0xf0"), nil)
|
|
|
|
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"),
|
2019-12-10 03:00:03 -06:00
|
|
|
randomAccountSet("0xcc", "0xf0", "0xff"), nil)
|
|
|
|
|
|
|
|
// Create an iterator and flatten the data from underneath it
|
2024-11-15 00:59:06 -06:00
|
|
|
it := newIterator(snaps, common.HexToHash("0x04"), common.Hash{})
|
2019-12-10 03:00:03 -06:00
|
|
|
defer it.Release()
|
|
|
|
|
|
|
|
if err := snaps.Cap(common.HexToHash("0x04"), 1); err != nil {
|
|
|
|
t.Fatalf("failed to flatten snapshot stack: %v", err)
|
|
|
|
}
|
|
|
|
//verifyIterator(t, 7, it)
|
|
|
|
}
|
|
|
|
|
2020-03-03 07:52:00 -06:00
|
|
|
func TestAccountIteratorSeek(t *testing.T) {
|
2024-11-15 00:59:06 -06:00
|
|
|
t.Run("fast", func(t *testing.T) {
|
|
|
|
testAccountIteratorSeek(t, func(snaps *Tree, root, seek common.Hash) AccountIterator {
|
|
|
|
it, _ := snaps.AccountIterator(root, seek)
|
|
|
|
return it
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("binary", func(t *testing.T) {
|
|
|
|
testAccountIteratorSeek(t, func(snaps *Tree, root, seek common.Hash) AccountIterator {
|
|
|
|
it := snaps.layers[root].(*diffLayer).newBinaryAccountIterator(seek)
|
|
|
|
return it
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccountIteratorSeek(t *testing.T, newIterator func(snaps *Tree, root, seek common.Hash) AccountIterator) {
|
2019-12-10 03:00:03 -06:00
|
|
|
// Create a snapshot stack with some initial data
|
|
|
|
base := &diskLayer{
|
|
|
|
diskdb: rawdb.NewMemoryDatabase(),
|
|
|
|
root: common.HexToHash("0x01"),
|
|
|
|
cache: fastcache.New(1024 * 500),
|
|
|
|
}
|
|
|
|
snaps := &Tree{
|
|
|
|
layers: map[common.Hash]snapshot{
|
|
|
|
base.root: base,
|
|
|
|
},
|
|
|
|
}
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"),
|
2019-12-10 03:00:03 -06:00
|
|
|
randomAccountSet("0xaa", "0xee", "0xff", "0xf0"), nil)
|
|
|
|
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"),
|
2019-12-10 03:00:03 -06:00
|
|
|
randomAccountSet("0xbb", "0xdd", "0xf0"), nil)
|
|
|
|
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"),
|
2019-12-10 03:00:03 -06:00
|
|
|
randomAccountSet("0xcc", "0xf0", "0xff"), nil)
|
|
|
|
|
2020-04-21 09:26:02 -05:00
|
|
|
// Account set is now
|
|
|
|
// 02: aa, ee, f0, ff
|
|
|
|
// 03: aa, bb, dd, ee, f0 (, f0), ff
|
|
|
|
// 04: aa, bb, cc, dd, ee, f0 (, f0), ff (, ff)
|
|
|
|
// Construct various iterators and ensure their traversal is correct
|
2024-11-15 00:59:06 -06:00
|
|
|
it := newIterator(snaps, common.HexToHash("0x02"), common.HexToHash("0xdd"))
|
2019-12-10 03:00:03 -06:00
|
|
|
defer it.Release()
|
2020-04-29 04:53:08 -05:00
|
|
|
verifyIterator(t, 3, it, verifyAccount) // expected: ee, f0, ff
|
2019-12-10 03:00:03 -06:00
|
|
|
|
2024-11-15 00:59:06 -06:00
|
|
|
it = newIterator(snaps, common.HexToHash("0x02"), common.HexToHash("0xaa"))
|
2019-12-10 03:00:03 -06:00
|
|
|
defer it.Release()
|
2020-04-29 04:53:08 -05:00
|
|
|
verifyIterator(t, 4, it, verifyAccount) // expected: aa, ee, f0, ff
|
2019-12-10 03:00:03 -06:00
|
|
|
|
2024-11-15 00:59:06 -06:00
|
|
|
it = newIterator(snaps, common.HexToHash("0x02"), common.HexToHash("0xff"))
|
2019-12-10 03:00:03 -06:00
|
|
|
defer it.Release()
|
2020-04-29 04:53:08 -05:00
|
|
|
verifyIterator(t, 1, it, verifyAccount) // expected: ff
|
2020-04-21 09:26:02 -05:00
|
|
|
|
2024-11-15 00:59:06 -06:00
|
|
|
it = newIterator(snaps, common.HexToHash("0x02"), common.HexToHash("0xff1"))
|
2020-04-21 09:26:02 -05:00
|
|
|
defer it.Release()
|
2020-04-29 04:53:08 -05:00
|
|
|
verifyIterator(t, 0, it, verifyAccount) // expected: nothing
|
2019-12-10 03:00:03 -06:00
|
|
|
|
2024-11-15 00:59:06 -06:00
|
|
|
it = newIterator(snaps, common.HexToHash("0x04"), common.HexToHash("0xbb"))
|
2019-12-10 03:00:03 -06:00
|
|
|
defer it.Release()
|
2020-04-29 04:53:08 -05:00
|
|
|
verifyIterator(t, 6, it, verifyAccount) // expected: bb, cc, dd, ee, f0, ff
|
2019-12-10 03:00:03 -06:00
|
|
|
|
2024-11-15 00:59:06 -06:00
|
|
|
it = newIterator(snaps, common.HexToHash("0x04"), common.HexToHash("0xef"))
|
2019-12-10 03:00:03 -06:00
|
|
|
defer it.Release()
|
2020-04-29 04:53:08 -05:00
|
|
|
verifyIterator(t, 2, it, verifyAccount) // expected: f0, ff
|
2019-12-10 03:00:03 -06:00
|
|
|
|
2024-11-15 00:59:06 -06:00
|
|
|
it = newIterator(snaps, common.HexToHash("0x04"), common.HexToHash("0xf0"))
|
2019-12-10 03:00:03 -06:00
|
|
|
defer it.Release()
|
2020-04-29 04:53:08 -05:00
|
|
|
verifyIterator(t, 2, it, verifyAccount) // expected: f0, ff
|
2019-12-10 03:00:03 -06:00
|
|
|
|
2024-11-15 00:59:06 -06:00
|
|
|
it = newIterator(snaps, common.HexToHash("0x04"), common.HexToHash("0xff"))
|
2019-12-10 03:00:03 -06:00
|
|
|
defer it.Release()
|
2020-04-29 04:53:08 -05:00
|
|
|
verifyIterator(t, 1, it, verifyAccount) // expected: ff
|
2020-04-21 09:26:02 -05:00
|
|
|
|
2024-11-15 00:59:06 -06:00
|
|
|
it = newIterator(snaps, common.HexToHash("0x04"), common.HexToHash("0xff1"))
|
2020-04-21 09:26:02 -05:00
|
|
|
defer it.Release()
|
2020-04-29 04:53:08 -05:00
|
|
|
verifyIterator(t, 0, it, verifyAccount) // expected: nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStorageIteratorSeek(t *testing.T) {
|
2024-11-15 00:59:06 -06:00
|
|
|
t.Run("fast", func(t *testing.T) {
|
|
|
|
testStorageIteratorSeek(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) {
|
|
|
|
testStorageIteratorSeek(t, func(snaps *Tree, root, account, seek common.Hash) StorageIterator {
|
|
|
|
return snaps.layers[root].(*diffLayer).newBinaryStorageIterator(account, seek)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testStorageIteratorSeek(t *testing.T, newIterator func(snaps *Tree, root, account, seek common.Hash) StorageIterator) {
|
2020-04-29 04:53:08 -05:00
|
|
|
// Create a snapshot stack with some initial data
|
|
|
|
base := &diskLayer{
|
|
|
|
diskdb: rawdb.NewMemoryDatabase(),
|
|
|
|
root: common.HexToHash("0x01"),
|
|
|
|
cache: fastcache.New(1024 * 500),
|
|
|
|
}
|
|
|
|
snaps := &Tree{
|
|
|
|
layers: map[common.Hash]snapshot{
|
|
|
|
base.root: base,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// Stack three diff layers on top with various overlaps
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"),
|
2020-04-29 04:53:08 -05:00
|
|
|
randomAccountSet("0xaa"), randomStorageSet([]string{"0xaa"}, [][]string{{"0x01", "0x03", "0x05"}}, nil))
|
|
|
|
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"),
|
2020-04-29 04:53:08 -05:00
|
|
|
randomAccountSet("0xaa"), randomStorageSet([]string{"0xaa"}, [][]string{{"0x02", "0x05", "0x06"}}, nil))
|
2020-04-21 09:26:02 -05:00
|
|
|
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"),
|
2020-04-29 04:53:08 -05:00
|
|
|
randomAccountSet("0xaa"), randomStorageSet([]string{"0xaa"}, [][]string{{"0x01", "0x05", "0x08"}}, nil))
|
|
|
|
|
|
|
|
// Account set is now
|
|
|
|
// 02: 01, 03, 05
|
|
|
|
// 03: 01, 02, 03, 05 (, 05), 06
|
|
|
|
// 04: 01(, 01), 02, 03, 05(, 05, 05), 06, 08
|
|
|
|
// Construct various iterators and ensure their traversal is correct
|
2024-11-15 00:59:06 -06:00
|
|
|
it := newIterator(snaps, common.HexToHash("0x02"), common.HexToHash("0xaa"), common.HexToHash("0x01"))
|
2020-04-29 04:53:08 -05:00
|
|
|
defer it.Release()
|
|
|
|
verifyIterator(t, 3, it, verifyStorage) // expected: 01, 03, 05
|
|
|
|
|
2024-11-15 00:59:06 -06:00
|
|
|
it = newIterator(snaps, common.HexToHash("0x02"), common.HexToHash("0xaa"), common.HexToHash("0x02"))
|
2020-04-29 04:53:08 -05:00
|
|
|
defer it.Release()
|
|
|
|
verifyIterator(t, 2, it, verifyStorage) // expected: 03, 05
|
|
|
|
|
2024-11-15 00:59:06 -06:00
|
|
|
it = newIterator(snaps, common.HexToHash("0x02"), common.HexToHash("0xaa"), common.HexToHash("0x5"))
|
2020-04-29 04:53:08 -05:00
|
|
|
defer it.Release()
|
|
|
|
verifyIterator(t, 1, it, verifyStorage) // expected: 05
|
|
|
|
|
2024-11-15 00:59:06 -06:00
|
|
|
it = newIterator(snaps, common.HexToHash("0x02"), common.HexToHash("0xaa"), common.HexToHash("0x6"))
|
2020-04-29 04:53:08 -05:00
|
|
|
defer it.Release()
|
|
|
|
verifyIterator(t, 0, it, verifyStorage) // expected: nothing
|
|
|
|
|
2024-11-15 00:59:06 -06:00
|
|
|
it = newIterator(snaps, common.HexToHash("0x04"), common.HexToHash("0xaa"), common.HexToHash("0x01"))
|
2020-04-29 04:53:08 -05:00
|
|
|
defer it.Release()
|
|
|
|
verifyIterator(t, 6, it, verifyStorage) // expected: 01, 02, 03, 05, 06, 08
|
|
|
|
|
2024-11-15 00:59:06 -06:00
|
|
|
it = newIterator(snaps, common.HexToHash("0x04"), common.HexToHash("0xaa"), common.HexToHash("0x05"))
|
2020-04-29 04:53:08 -05:00
|
|
|
defer it.Release()
|
|
|
|
verifyIterator(t, 3, it, verifyStorage) // expected: 05, 06, 08
|
|
|
|
|
2024-11-15 00:59:06 -06:00
|
|
|
it = newIterator(snaps, common.HexToHash("0x04"), common.HexToHash("0xaa"), common.HexToHash("0x08"))
|
2020-04-29 04:53:08 -05:00
|
|
|
defer it.Release()
|
|
|
|
verifyIterator(t, 1, it, verifyStorage) // expected: 08
|
|
|
|
|
2024-11-15 00:59:06 -06:00
|
|
|
it = newIterator(snaps, common.HexToHash("0x04"), common.HexToHash("0xaa"), common.HexToHash("0x09"))
|
2020-04-29 04:53:08 -05:00
|
|
|
defer it.Release()
|
|
|
|
verifyIterator(t, 0, it, verifyStorage) // expected: nothing
|
2019-12-10 03:00:03 -06:00
|
|
|
}
|
|
|
|
|
2020-04-29 04:53:08 -05:00
|
|
|
// TestAccountIteratorDeletions tests that the iterator behaves correct when there are
|
2020-03-04 06:38:55 -06:00
|
|
|
// deleted accounts (where the Account() value is nil). The iterator
|
|
|
|
// should not output any accounts or nil-values for those cases.
|
2020-04-29 04:53:08 -05:00
|
|
|
func TestAccountIteratorDeletions(t *testing.T) {
|
2024-11-15 00:59:06 -06:00
|
|
|
t.Run("fast", func(t *testing.T) {
|
|
|
|
testAccountIteratorDeletions(t, func(snaps *Tree, root, seek common.Hash) AccountIterator {
|
|
|
|
it, _ := snaps.AccountIterator(root, seek)
|
|
|
|
return it
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("binary", func(t *testing.T) {
|
|
|
|
testAccountIteratorDeletions(t, func(snaps *Tree, root, seek common.Hash) AccountIterator {
|
|
|
|
return snaps.layers[root].(*diffLayer).newBinaryAccountIterator(seek)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccountIteratorDeletions(t *testing.T, newIterator func(snaps *Tree, root, seek common.Hash) AccountIterator) {
|
2020-03-04 06:38:55 -06:00
|
|
|
// Create an empty base layer and a snapshot tree out of it
|
|
|
|
base := &diskLayer{
|
|
|
|
diskdb: rawdb.NewMemoryDatabase(),
|
|
|
|
root: common.HexToHash("0x01"),
|
|
|
|
cache: fastcache.New(1024 * 500),
|
|
|
|
}
|
|
|
|
snaps := &Tree{
|
|
|
|
layers: map[common.Hash]snapshot{
|
|
|
|
base.root: base,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// Stack three diff layers on top with various overlaps
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"), randomAccountSet("0x11", "0x22", "0x33"), nil)
|
2020-03-04 06:38:55 -06:00
|
|
|
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
set := randomAccountSet("0x11", "0x33")
|
|
|
|
set[common.HexToHash("0x22")] = nil
|
|
|
|
snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"), set, nil)
|
2020-03-04 06:38:55 -06:00
|
|
|
snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"),
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
randomAccountSet("0x33", "0x44", "0x55"), nil)
|
2020-03-04 06:38:55 -06:00
|
|
|
|
|
|
|
// The output should be 11,33,44,55
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
it := newIterator(snaps, common.HexToHash("0x04"), common.Hash{})
|
2024-11-15 00:59:06 -06:00
|
|
|
|
2020-03-04 06:38:55 -06:00
|
|
|
// Do a quick check
|
2020-04-29 04:53:08 -05:00
|
|
|
verifyIterator(t, 4, it, verifyAccount)
|
2020-03-04 06:38:55 -06:00
|
|
|
it.Release()
|
|
|
|
|
|
|
|
// And a more detailed verification that we indeed do not see '0x22'
|
|
|
|
it, _ = snaps.AccountIterator(common.HexToHash("0x04"), common.Hash{})
|
|
|
|
defer it.Release()
|
|
|
|
for it.Next() {
|
|
|
|
hash := it.Hash()
|
|
|
|
if it.Account() == nil {
|
|
|
|
t.Errorf("iterator returned nil-value for hash %x", hash)
|
|
|
|
}
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
if hash == common.HexToHash("0x22") {
|
|
|
|
t.Errorf("expected deleted elem %x to not be returned by iterator", common.HexToHash("0x22"))
|
2020-03-04 06:38:55 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-29 04:53:08 -05:00
|
|
|
func TestStorageIteratorDeletions(t *testing.T) {
|
2024-11-15 00:59:06 -06:00
|
|
|
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, seek)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testStorageIteratorDeletions(t *testing.T, newIterator func(snaps *Tree, root, account, seek common.Hash) StorageIterator) {
|
2020-04-29 04:53:08 -05:00
|
|
|
// Create an empty base layer and a snapshot tree out of it
|
|
|
|
base := &diskLayer{
|
|
|
|
diskdb: rawdb.NewMemoryDatabase(),
|
|
|
|
root: common.HexToHash("0x01"),
|
|
|
|
cache: fastcache.New(1024 * 500),
|
|
|
|
}
|
|
|
|
snaps := &Tree{
|
|
|
|
layers: map[common.Hash]snapshot{
|
|
|
|
base.root: base,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// Stack three diff layers on top with various overlaps
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"),
|
2020-04-29 04:53:08 -05:00
|
|
|
randomAccountSet("0xaa"), randomStorageSet([]string{"0xaa"}, [][]string{{"0x01", "0x03", "0x05"}}, nil))
|
|
|
|
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"),
|
2020-04-29 04:53:08 -05:00
|
|
|
randomAccountSet("0xaa"), randomStorageSet([]string{"0xaa"}, [][]string{{"0x02", "0x04", "0x06"}}, [][]string{{"0x01", "0x03"}}))
|
|
|
|
|
|
|
|
// The output should be 02,04,05,06
|
2024-11-15 00:59:06 -06:00
|
|
|
it := newIterator(snaps, common.HexToHash("0x03"), common.HexToHash("0xaa"), common.Hash{})
|
2020-04-29 04:53:08 -05:00
|
|
|
verifyIterator(t, 4, it, verifyStorage)
|
|
|
|
it.Release()
|
|
|
|
|
|
|
|
// The output should be 04,05,06
|
2024-11-15 00:59:06 -06:00
|
|
|
it = newIterator(snaps, common.HexToHash("0x03"), common.HexToHash("0xaa"), common.HexToHash("0x03"))
|
2020-04-29 04:53:08 -05:00
|
|
|
verifyIterator(t, 3, it, verifyStorage)
|
|
|
|
it.Release()
|
|
|
|
|
|
|
|
// Destruct the whole storage
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"),
|
|
|
|
map[common.Hash][]byte{common.HexToHash("0xaa"): nil},
|
|
|
|
randomStorageSet([]string{"0xaa"}, nil, [][]string{{"0x02", "0x04", "0x05", "0x06"}}))
|
2020-04-29 04:53:08 -05:00
|
|
|
|
2024-11-15 00:59:06 -06:00
|
|
|
it = newIterator(snaps, common.HexToHash("0x04"), common.HexToHash("0xaa"), common.Hash{})
|
2020-04-29 04:53:08 -05:00
|
|
|
verifyIterator(t, 0, it, verifyStorage)
|
|
|
|
it.Release()
|
|
|
|
|
|
|
|
// Re-insert the slots of the same account
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
snaps.Update(common.HexToHash("0x05"), common.HexToHash("0x04"),
|
2020-04-29 04:53:08 -05:00
|
|
|
randomAccountSet("0xaa"), randomStorageSet([]string{"0xaa"}, [][]string{{"0x07", "0x08", "0x09"}}, nil))
|
|
|
|
|
|
|
|
// The output should be 07,08,09
|
2024-11-15 00:59:06 -06:00
|
|
|
|
|
|
|
it = newIterator(snaps, common.HexToHash("0x05"), common.HexToHash("0xaa"), common.Hash{})
|
2020-04-29 04:53:08 -05:00
|
|
|
verifyIterator(t, 3, it, verifyStorage)
|
|
|
|
it.Release()
|
|
|
|
|
|
|
|
// Destruct the whole storage but re-create the account in the same layer
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
snaps.Update(common.HexToHash("0x06"), common.HexToHash("0x05"),
|
|
|
|
randomAccountSet("0xaa"),
|
|
|
|
randomStorageSet([]string{"0xaa"}, [][]string{{"0x11", "0x12"}}, [][]string{{"0x07", "0x08", "0x09"}}))
|
2024-11-15 00:59:06 -06:00
|
|
|
it = newIterator(snaps, common.HexToHash("0x06"), common.HexToHash("0xaa"), common.Hash{})
|
2020-04-29 04:53:08 -05:00
|
|
|
verifyIterator(t, 2, it, verifyStorage) // The output should be 11,12
|
|
|
|
it.Release()
|
|
|
|
|
2024-11-15 00:59:06 -06:00
|
|
|
verifyIterator(t, 2, snaps.Snapshot(
|
|
|
|
common.HexToHash("0x06")).(*diffLayer).
|
|
|
|
newBinaryStorageIterator(common.HexToHash("0xaa"), common.Hash{}),
|
|
|
|
verifyStorage)
|
2020-04-29 04:53:08 -05:00
|
|
|
}
|
|
|
|
|
2024-06-10 07:55:47 -05:00
|
|
|
// BenchmarkAccountIteratorTraversal is a bit notorious -- all layers contain the
|
2019-12-10 03:00:03 -06:00
|
|
|
// exact same 200 accounts. That means that we need to process 2000 items, but
|
|
|
|
// only spit out 200 values eventually.
|
2019-12-05 07:37:25 -06:00
|
|
|
//
|
2019-12-06 16:27:18 -06:00
|
|
|
// The value-fetching benchmark is easy on the binary iterator, since it never has to reach
|
2022-10-11 02:37:00 -05:00
|
|
|
// down at any depth for retrieving the values -- all are on the topmost layer
|
2019-12-06 16:27:18 -06:00
|
|
|
//
|
2020-03-03 07:52:00 -06:00
|
|
|
// BenchmarkAccountIteratorTraversal/binary_iterator_keys-6 2239 483674 ns/op
|
|
|
|
// BenchmarkAccountIteratorTraversal/binary_iterator_values-6 2403 501810 ns/op
|
|
|
|
// BenchmarkAccountIteratorTraversal/fast_iterator_keys-6 1923 677966 ns/op
|
|
|
|
// BenchmarkAccountIteratorTraversal/fast_iterator_values-6 1741 649967 ns/op
|
|
|
|
func BenchmarkAccountIteratorTraversal(b *testing.B) {
|
2019-12-10 03:00:03 -06:00
|
|
|
// Create a custom account factory to recreate the same addresses
|
|
|
|
makeAccounts := func(num int) map[common.Hash][]byte {
|
2019-12-05 07:37:25 -06:00
|
|
|
accounts := make(map[common.Hash][]byte)
|
|
|
|
for i := 0; i < num; i++ {
|
|
|
|
h := common.Hash{}
|
|
|
|
binary.BigEndian.PutUint64(h[:], uint64(i+1))
|
|
|
|
accounts[h] = randomAccount()
|
|
|
|
}
|
|
|
|
return accounts
|
|
|
|
}
|
2019-12-10 03:00:03 -06:00
|
|
|
// Build up a large stack of snapshots
|
|
|
|
base := &diskLayer{
|
|
|
|
diskdb: rawdb.NewMemoryDatabase(),
|
|
|
|
root: common.HexToHash("0x01"),
|
|
|
|
cache: fastcache.New(1024 * 500),
|
|
|
|
}
|
|
|
|
snaps := &Tree{
|
|
|
|
layers: map[common.Hash]snapshot{
|
|
|
|
base.root: base,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for i := 1; i <= 100; i++ {
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
snaps.Update(common.HexToHash(fmt.Sprintf("0x%02x", i+1)), common.HexToHash(fmt.Sprintf("0x%02x", i)), makeAccounts(200), nil)
|
2019-12-05 07:37:25 -06:00
|
|
|
}
|
|
|
|
// We call this once before the benchmark, so the creation of
|
|
|
|
// sorted accountlists are not included in the results.
|
2019-12-10 03:00:03 -06:00
|
|
|
head := snaps.Snapshot(common.HexToHash("0x65"))
|
2024-11-15 00:59:06 -06:00
|
|
|
head.(*diffLayer).newBinaryAccountIterator(common.Hash{})
|
2019-12-10 03:00:03 -06:00
|
|
|
|
2019-12-06 16:27:18 -06:00
|
|
|
b.Run("binary iterator keys", func(b *testing.B) {
|
2019-12-05 07:37:25 -06:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
got := 0
|
2024-11-15 00:59:06 -06:00
|
|
|
it := head.(*diffLayer).newBinaryAccountIterator(common.Hash{})
|
2019-12-05 07:37:25 -06:00
|
|
|
for it.Next() {
|
|
|
|
got++
|
|
|
|
}
|
|
|
|
if exp := 200; got != exp {
|
|
|
|
b.Errorf("iterator len wrong, expected %d, got %d", exp, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-12-06 16:27:18 -06:00
|
|
|
b.Run("binary iterator values", func(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
got := 0
|
2024-11-15 00:59:06 -06:00
|
|
|
it := head.(*diffLayer).newBinaryAccountIterator(common.Hash{})
|
2019-12-06 16:27:18 -06:00
|
|
|
for it.Next() {
|
|
|
|
got++
|
2019-12-10 03:00:03 -06:00
|
|
|
head.(*diffLayer).accountRLP(it.Hash(), 0)
|
2019-12-06 16:27:18 -06:00
|
|
|
}
|
|
|
|
if exp := 200; got != exp {
|
|
|
|
b.Errorf("iterator len wrong, expected %d, got %d", exp, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
b.Run("fast iterator keys", func(b *testing.B) {
|
2019-12-05 07:37:25 -06:00
|
|
|
for i := 0; i < b.N; i++ {
|
2019-12-10 03:00:03 -06:00
|
|
|
it, _ := snaps.AccountIterator(common.HexToHash("0x65"), common.Hash{})
|
|
|
|
defer it.Release()
|
|
|
|
|
2019-12-05 07:37:25 -06:00
|
|
|
got := 0
|
|
|
|
for it.Next() {
|
|
|
|
got++
|
|
|
|
}
|
|
|
|
if exp := 200; got != exp {
|
|
|
|
b.Errorf("iterator len wrong, expected %d, got %d", exp, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-12-06 16:27:18 -06:00
|
|
|
b.Run("fast iterator values", func(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
2019-12-10 03:00:03 -06:00
|
|
|
it, _ := snaps.AccountIterator(common.HexToHash("0x65"), common.Hash{})
|
|
|
|
defer it.Release()
|
|
|
|
|
2019-12-06 16:27:18 -06:00
|
|
|
got := 0
|
|
|
|
for it.Next() {
|
|
|
|
got++
|
2019-12-10 03:00:03 -06:00
|
|
|
it.Account()
|
2019-12-06 16:27:18 -06:00
|
|
|
}
|
|
|
|
if exp := 200; got != exp {
|
|
|
|
b.Errorf("iterator len wrong, expected %d, got %d", exp, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-12-05 07:37:25 -06:00
|
|
|
}
|
|
|
|
|
2020-03-03 07:52:00 -06:00
|
|
|
// BenchmarkAccountIteratorLargeBaselayer is a pretty realistic benchmark, where
|
2019-12-05 07:37:25 -06:00
|
|
|
// the baselayer is a lot larger than the upper layer.
|
|
|
|
//
|
|
|
|
// This is heavy on the binary iterator, which in most cases will have to
|
|
|
|
// call recursively 100 times for the majority of the values
|
|
|
|
//
|
2020-03-03 07:52:00 -06:00
|
|
|
// BenchmarkAccountIteratorLargeBaselayer/binary_iterator_(keys)-6 514 1971999 ns/op
|
|
|
|
// BenchmarkAccountIteratorLargeBaselayer/binary_iterator_(values)-6 61 18997492 ns/op
|
|
|
|
// BenchmarkAccountIteratorLargeBaselayer/fast_iterator_(keys)-6 10000 114385 ns/op
|
|
|
|
// BenchmarkAccountIteratorLargeBaselayer/fast_iterator_(values)-6 4047 296823 ns/op
|
|
|
|
func BenchmarkAccountIteratorLargeBaselayer(b *testing.B) {
|
2019-12-10 03:00:03 -06:00
|
|
|
// Create a custom account factory to recreate the same addresses
|
|
|
|
makeAccounts := func(num int) map[common.Hash][]byte {
|
2019-12-05 07:37:25 -06:00
|
|
|
accounts := make(map[common.Hash][]byte)
|
|
|
|
for i := 0; i < num; i++ {
|
|
|
|
h := common.Hash{}
|
|
|
|
binary.BigEndian.PutUint64(h[:], uint64(i+1))
|
|
|
|
accounts[h] = randomAccount()
|
|
|
|
}
|
|
|
|
return accounts
|
|
|
|
}
|
2019-12-10 03:00:03 -06:00
|
|
|
// Build up a large stack of snapshots
|
|
|
|
base := &diskLayer{
|
|
|
|
diskdb: rawdb.NewMemoryDatabase(),
|
|
|
|
root: common.HexToHash("0x01"),
|
|
|
|
cache: fastcache.New(1024 * 500),
|
|
|
|
}
|
|
|
|
snaps := &Tree{
|
|
|
|
layers: map[common.Hash]snapshot{
|
|
|
|
base.root: base,
|
|
|
|
},
|
|
|
|
}
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"), makeAccounts(2000), nil)
|
2019-12-10 03:00:03 -06:00
|
|
|
for i := 2; i <= 100; i++ {
|
core, triedb: remove destruct flag in state snapshot (#30752)
This pull request removes the destruct flag from the state snapshot to
simplify the code.
Previously, this flag indicated that an account was removed during a
state transition, making all associated storage slots inaccessible.
Because storage deletion can involve a large number of slots, the actual
deletion is deferred until the end of the process, where it is handled
in batches.
With the deprecation of self-destruct in the Cancun fork, storage
deletions are no longer expected. Historically, the largest storage
deletion event in Ethereum was around 15 megabytes—manageable in memory.
In this pull request, the single destruct flag is replaced by a set of
deletion markers for individual storage slots. Each deleted storage slot
will now appear in the Storage set with a nil value.
This change will simplify a lot logics, such as storage accessing,
storage flushing, storage iteration and so on.
2024-11-22 02:55:43 -06:00
|
|
|
snaps.Update(common.HexToHash(fmt.Sprintf("0x%02x", i+1)), common.HexToHash(fmt.Sprintf("0x%02x", i)), makeAccounts(20), nil)
|
2019-12-05 07:37:25 -06:00
|
|
|
}
|
|
|
|
// We call this once before the benchmark, so the creation of
|
|
|
|
// sorted accountlists are not included in the results.
|
2019-12-10 03:00:03 -06:00
|
|
|
head := snaps.Snapshot(common.HexToHash("0x65"))
|
2024-11-15 00:59:06 -06:00
|
|
|
head.(*diffLayer).newBinaryAccountIterator(common.Hash{})
|
2019-12-10 03:00:03 -06:00
|
|
|
|
2019-12-06 16:27:18 -06:00
|
|
|
b.Run("binary iterator (keys)", func(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
got := 0
|
2024-11-15 00:59:06 -06:00
|
|
|
it := head.(*diffLayer).newBinaryAccountIterator(common.Hash{})
|
2019-12-06 16:27:18 -06:00
|
|
|
for it.Next() {
|
|
|
|
got++
|
|
|
|
}
|
|
|
|
if exp := 2000; got != exp {
|
|
|
|
b.Errorf("iterator len wrong, expected %d, got %d", exp, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-12-10 03:00:03 -06:00
|
|
|
b.Run("binary iterator (values)", func(b *testing.B) {
|
2019-12-06 16:27:18 -06:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
got := 0
|
2024-11-15 00:59:06 -06:00
|
|
|
it := head.(*diffLayer).newBinaryAccountIterator(common.Hash{})
|
2019-12-06 16:27:18 -06:00
|
|
|
for it.Next() {
|
|
|
|
got++
|
2019-12-10 03:00:03 -06:00
|
|
|
v := it.Hash()
|
|
|
|
head.(*diffLayer).accountRLP(v, 0)
|
2019-12-06 16:27:18 -06:00
|
|
|
}
|
|
|
|
if exp := 2000; got != exp {
|
|
|
|
b.Errorf("iterator len wrong, expected %d, got %d", exp, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-12-10 03:00:03 -06:00
|
|
|
b.Run("fast iterator (keys)", func(b *testing.B) {
|
2019-12-05 07:37:25 -06:00
|
|
|
for i := 0; i < b.N; i++ {
|
2019-12-10 03:00:03 -06:00
|
|
|
it, _ := snaps.AccountIterator(common.HexToHash("0x65"), common.Hash{})
|
|
|
|
defer it.Release()
|
|
|
|
|
2019-12-05 07:37:25 -06:00
|
|
|
got := 0
|
|
|
|
for it.Next() {
|
|
|
|
got++
|
|
|
|
}
|
|
|
|
if exp := 2000; got != exp {
|
|
|
|
b.Errorf("iterator len wrong, expected %d, got %d", exp, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-12-06 16:27:18 -06:00
|
|
|
b.Run("fast iterator (values)", func(b *testing.B) {
|
2019-12-05 07:37:25 -06:00
|
|
|
for i := 0; i < b.N; i++ {
|
2019-12-10 03:00:03 -06:00
|
|
|
it, _ := snaps.AccountIterator(common.HexToHash("0x65"), common.Hash{})
|
|
|
|
defer it.Release()
|
|
|
|
|
2019-12-05 07:37:25 -06:00
|
|
|
got := 0
|
|
|
|
for it.Next() {
|
2019-12-10 03:00:03 -06:00
|
|
|
it.Account()
|
2019-12-05 07:37:25 -06:00
|
|
|
got++
|
|
|
|
}
|
|
|
|
if exp := 2000; got != exp {
|
|
|
|
b.Errorf("iterator len wrong, expected %d, got %d", exp, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-10 03:00:03 -06:00
|
|
|
/*
|
|
|
|
func BenchmarkBinaryAccountIteration(b *testing.B) {
|
|
|
|
benchmarkAccountIteration(b, func(snap snapshot) AccountIterator {
|
2024-11-15 00:59:06 -06:00
|
|
|
return snap.(*diffLayer).newBinaryAccountIterator(common.Hash{})
|
2019-12-10 03:00:03 -06:00
|
|
|
})
|
|
|
|
}
|
2019-12-05 07:37:25 -06:00
|
|
|
|
2019-12-10 03:00:03 -06:00
|
|
|
func BenchmarkFastAccountIteration(b *testing.B) {
|
|
|
|
benchmarkAccountIteration(b, newFastAccountIterator)
|
2019-12-05 07:37:25 -06:00
|
|
|
}
|
|
|
|
|
2019-12-10 03:00:03 -06:00
|
|
|
func benchmarkAccountIteration(b *testing.B, iterator func(snap snapshot) AccountIterator) {
|
|
|
|
// Create a diff stack and randomize the accounts across them
|
|
|
|
layers := make([]map[common.Hash][]byte, 128)
|
|
|
|
for i := 0; i < len(layers); i++ {
|
|
|
|
layers[i] = make(map[common.Hash][]byte)
|
2019-12-05 07:37:25 -06:00
|
|
|
}
|
2019-12-10 03:00:03 -06:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
depth := rand.Intn(len(layers))
|
|
|
|
layers[depth][randomHash()] = randomAccount()
|
|
|
|
}
|
|
|
|
stack := snapshot(emptyLayer())
|
|
|
|
for _, layer := range layers {
|
2020-03-03 07:52:00 -06:00
|
|
|
stack = stack.Update(common.Hash{}, layer, nil, nil)
|
2019-12-10 03:00:03 -06:00
|
|
|
}
|
|
|
|
// Reset the timers and report all the stats
|
|
|
|
it := iterator(stack)
|
2019-12-06 16:27:18 -06:00
|
|
|
|
2019-12-10 03:00:03 -06:00
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
2019-12-06 16:27:18 -06:00
|
|
|
|
2019-12-10 03:00:03 -06:00
|
|
|
for it.Next() {
|
2019-12-06 16:27:18 -06:00
|
|
|
}
|
|
|
|
}
|
2019-12-10 03:00:03 -06:00
|
|
|
*/
|