aboutsummaryrefslogtreecommitdiff
path: root/core/state
diff options
context:
space:
mode:
Diffstat (limited to 'core/state')
-rw-r--r--core/state/snapshot/difflayer_test.go400
-rw-r--r--core/state/snapshot/disklayer_test.go511
-rw-r--r--core/state/snapshot/iterator_test.go1046
-rw-r--r--core/state/snapshot/snapshot_test.go371
-rw-r--r--core/state/snapshot/wipe_test.go124
5 files changed, 0 insertions, 2452 deletions
diff --git a/core/state/snapshot/difflayer_test.go b/core/state/snapshot/difflayer_test.go
deleted file mode 100644
index 31636ee..0000000
--- a/core/state/snapshot/difflayer_test.go
+++ /dev/null
@@ -1,400 +0,0 @@
-// 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"
- "math/rand"
- "testing"
-
- "github.com/VictoriaMetrics/fastcache"
- "github.com/ethereum/go-ethereum/common"
- "github.com/ethereum/go-ethereum/crypto"
- "github.com/ethereum/go-ethereum/ethdb/memorydb"
-)
-
-func copyDestructs(destructs map[common.Hash]struct{}) map[common.Hash]struct{} {
- copy := make(map[common.Hash]struct{})
- for hash := range destructs {
- copy[hash] = struct{}{}
- }
- return copy
-}
-
-func copyAccounts(accounts map[common.Hash][]byte) map[common.Hash][]byte {
- copy := make(map[common.Hash][]byte)
- for hash, blob := range accounts {
- copy[hash] = blob
- }
- return copy
-}
-
-func copyStorage(storage map[common.Hash]map[common.Hash][]byte) map[common.Hash]map[common.Hash][]byte {
- copy := make(map[common.Hash]map[common.Hash][]byte)
- for accHash, slots := range storage {
- copy[accHash] = make(map[common.Hash][]byte)
- for slotHash, blob := range slots {
- copy[accHash][slotHash] = blob
- }
- }
- return copy
-}
-
-// TestMergeBasics tests some simple merges
-func TestMergeBasics(t *testing.T) {
- var (
- destructs = make(map[common.Hash]struct{})
- accounts = make(map[common.Hash][]byte)
- storage = make(map[common.Hash]map[common.Hash][]byte)
- )
- // Fill up a parent
- for i := 0; i < 100; i++ {
- h := randomHash()
- data := randomAccount()
-
- accounts[h] = data
- if rand.Intn(4) == 0 {
- destructs[h] = struct{}{}
- }
- if rand.Intn(2) == 0 {
- accStorage := make(map[common.Hash][]byte)
- value := make([]byte, 32)
- rand.Read(value)
- accStorage[randomHash()] = value
- storage[h] = accStorage
- }
- }
- // Add some (identical) layers on top
- parent := newDiffLayer(emptyLayer(), common.Hash{}, copyDestructs(destructs), copyAccounts(accounts), copyStorage(storage))
- child := newDiffLayer(parent, common.Hash{}, copyDestructs(destructs), copyAccounts(accounts), copyStorage(storage))
- child = newDiffLayer(child, common.Hash{}, copyDestructs(destructs), copyAccounts(accounts), copyStorage(storage))
- child = newDiffLayer(child, common.Hash{}, copyDestructs(destructs), copyAccounts(accounts), copyStorage(storage))
- child = newDiffLayer(child, common.Hash{}, copyDestructs(destructs), copyAccounts(accounts), copyStorage(storage))
- // And flatten
- merged := (child.flatten()).(*diffLayer)
-
- { // Check account lists
- if have, want := len(merged.accountList), 0; have != want {
- t.Errorf("accountList wrong: have %v, want %v", have, want)
- }
- if have, want := len(merged.AccountList()), len(accounts); have != want {
- t.Errorf("AccountList() wrong: have %v, want %v", have, want)
- }
- if have, want := len(merged.accountList), len(accounts); have != want {
- t.Errorf("accountList [2] wrong: have %v, want %v", have, want)
- }
- }
- { // Check account drops
- if have, want := len(merged.destructSet), len(destructs); have != want {
- t.Errorf("accountDrop wrong: have %v, want %v", have, want)
- }
- }
- { // Check storage lists
- i := 0
- for aHash, sMap := range storage {
- if have, want := len(merged.storageList), i; have != want {
- t.Errorf("[1] storageList wrong: have %v, want %v", have, want)
- }
- list, _ := merged.StorageList(aHash)
- if have, want := len(list), len(sMap); have != want {
- t.Errorf("[2] StorageList() wrong: have %v, want %v", have, want)
- }
- if have, want := len(merged.storageList[aHash]), len(sMap); have != want {
- t.Errorf("storageList wrong: have %v, want %v", have, want)
- }
- i++
- }
- }
-}
-
-// TestMergeDelete tests some deletion
-func TestMergeDelete(t *testing.T) {
- var (
- storage = make(map[common.Hash]map[common.Hash][]byte)
- )
- // Fill up a parent
- h1 := common.HexToHash("0x01")
- h2 := common.HexToHash("0x02")
-
- flipDrops := func() map[common.Hash]struct{} {
- return map[common.Hash]struct{}{
- h2: {},
- }
- }
- flipAccs := func() map[common.Hash][]byte {
- return map[common.Hash][]byte{
- h1: randomAccount(),
- }
- }
- flopDrops := func() map[common.Hash]struct{} {
- return map[common.Hash]struct{}{
- h1: {},
- }
- }
- flopAccs := func() map[common.Hash][]byte {
- return map[common.Hash][]byte{
- h2: randomAccount(),
- }
- }
- // Add some flipAccs-flopping layers on top
- parent := newDiffLayer(emptyLayer(), common.Hash{}, flipDrops(), flipAccs(), storage)
- child := parent.Update(common.Hash{}, flopDrops(), flopAccs(), storage)
- child = child.Update(common.Hash{}, flipDrops(), flipAccs(), storage)
- child = child.Update(common.Hash{}, flopDrops(), flopAccs(), storage)
- child = child.Update(common.Hash{}, flipDrops(), flipAccs(), storage)
- child = child.Update(common.Hash{}, flopDrops(), flopAccs(), storage)
- child = child.Update(common.Hash{}, flipDrops(), flipAccs(), storage)
-
- if data, _ := child.Account(h1); data == nil {
- t.Errorf("last diff layer: expected %x account to be non-nil", h1)
- }
- if data, _ := child.Account(h2); data != nil {
- t.Errorf("last diff layer: expected %x account to be nil", h2)
- }
- if _, ok := child.destructSet[h1]; ok {
- t.Errorf("last diff layer: expected %x drop to be missing", h1)
- }
- if _, ok := child.destructSet[h2]; !ok {
- t.Errorf("last diff layer: expected %x drop to be present", h1)
- }
- // And flatten
- merged := (child.flatten()).(*diffLayer)
-
- if data, _ := merged.Account(h1); data == nil {
- t.Errorf("merged layer: expected %x account to be non-nil", h1)
- }
- if data, _ := merged.Account(h2); data != nil {
- t.Errorf("merged layer: expected %x account to be nil", h2)
- }
- if _, ok := merged.destructSet[h1]; !ok { // Note, drops stay alive until persisted to disk!
- t.Errorf("merged diff layer: expected %x drop to be present", h1)
- }
- if _, ok := merged.destructSet[h2]; !ok { // Note, drops stay alive until persisted to disk!
- t.Errorf("merged diff layer: expected %x drop to be present", h1)
- }
- // If we add more granular metering of memory, we can enable this again,
- // but it's not implemented for now
- //if have, want := merged.memory, child.memory; have != want {
- // t.Errorf("mem wrong: have %d, want %d", have, want)
- //}
-}
-
-// This tests that if we create a new account, and set a slot, and then merge
-// it, the lists will be correct.
-func TestInsertAndMerge(t *testing.T) {
- // Fill up a parent
- var (
- acc = common.HexToHash("0x01")
- slot = common.HexToHash("0x02")
- parent *diffLayer
- child *diffLayer
- )
- {
- var (
- destructs = make(map[common.Hash]struct{})
- accounts = make(map[common.Hash][]byte)
- storage = make(map[common.Hash]map[common.Hash][]byte)
- )
- parent = newDiffLayer(emptyLayer(), common.Hash{}, destructs, accounts, storage)
- }
- {
- var (
- destructs = make(map[common.Hash]struct{})
- accounts = make(map[common.Hash][]byte)
- storage = make(map[common.Hash]map[common.Hash][]byte)
- )
- accounts[acc] = randomAccount()
- storage[acc] = make(map[common.Hash][]byte)
- storage[acc][slot] = []byte{0x01}
- child = newDiffLayer(parent, common.Hash{}, destructs, accounts, storage)
- }
- // And flatten
- merged := (child.flatten()).(*diffLayer)
- { // Check that slot value is present
- have, _ := merged.Storage(acc, slot)
- if want := []byte{0x01}; !bytes.Equal(have, want) {
- t.Errorf("merged slot value wrong: have %x, want %x", have, want)
- }
- }
-}
-
-func emptyLayer() *diskLayer {
- return &diskLayer{
- diskdb: memorydb.New(),
- cache: fastcache.New(500 * 1024),
- }
-}
-
-// BenchmarkSearch checks how long it takes to find a non-existing key
-// BenchmarkSearch-6 200000 10481 ns/op (1K per layer)
-// BenchmarkSearch-6 200000 10760 ns/op (10K per layer)
-// BenchmarkSearch-6 100000 17866 ns/op
-//
-// BenchmarkSearch-6 500000 3723 ns/op (10k per layer, only top-level RLock()
-func BenchmarkSearch(b *testing.B) {
- // First, we set up 128 diff layers, with 1K items each
- fill := func(parent snapshot) *diffLayer {
- var (
- destructs = make(map[common.Hash]struct{})
- accounts = make(map[common.Hash][]byte)
- storage = make(map[common.Hash]map[common.Hash][]byte)
- )
- for i := 0; i < 10000; i++ {
- accounts[randomHash()] = randomAccount()
- }
- return newDiffLayer(parent, common.Hash{}, destructs, accounts, storage)
- }
- var layer snapshot
- layer = emptyLayer()
- for i := 0; i < 128; i++ {
- layer = fill(layer)
- }
- key := crypto.Keccak256Hash([]byte{0x13, 0x38})
- b.ResetTimer()
- for i := 0; i < b.N; i++ {
- layer.AccountRLP(key)
- }
-}
-
-// BenchmarkSearchSlot checks how long it takes to find a non-existing key
-// - Number of layers: 128
-// - Each layers contains the account, with a couple of storage slots
-// BenchmarkSearchSlot-6 100000 14554 ns/op
-// BenchmarkSearchSlot-6 100000 22254 ns/op (when checking parent root using mutex)
-// BenchmarkSearchSlot-6 100000 14551 ns/op (when checking parent number using atomic)
-// With bloom filter:
-// BenchmarkSearchSlot-6 3467835 351 ns/op
-func BenchmarkSearchSlot(b *testing.B) {
- // First, we set up 128 diff layers, with 1K items each
- accountKey := crypto.Keccak256Hash([]byte{0x13, 0x37})
- storageKey := crypto.Keccak256Hash([]byte{0x13, 0x37})
- accountRLP := randomAccount()
- fill := func(parent snapshot) *diffLayer {
- var (
- destructs = make(map[common.Hash]struct{})
- accounts = make(map[common.Hash][]byte)
- storage = make(map[common.Hash]map[common.Hash][]byte)
- )
- accounts[accountKey] = accountRLP
-
- accStorage := make(map[common.Hash][]byte)
- for i := 0; i < 5; i++ {
- value := make([]byte, 32)
- rand.Read(value)
- accStorage[randomHash()] = value
- storage[accountKey] = accStorage
- }
- return newDiffLayer(parent, common.Hash{}, destructs, accounts, storage)
- }
- var layer snapshot
- layer = emptyLayer()
- for i := 0; i < 128; i++ {
- layer = fill(layer)
- }
- b.ResetTimer()
- for i := 0; i < b.N; i++ {
- layer.Storage(accountKey, storageKey)
- }
-}
-
-// With accountList and sorting
-// BenchmarkFlatten-6 50 29890856 ns/op
-//
-// Without sorting and tracking accountlist
-// BenchmarkFlatten-6 300 5511511 ns/op
-func BenchmarkFlatten(b *testing.B) {
- fill := func(parent snapshot) *diffLayer {
- var (
- destructs = make(map[common.Hash]struct{})
- accounts = make(map[common.Hash][]byte)
- storage = make(map[common.Hash]map[common.Hash][]byte)
- )
- for i := 0; i < 100; i++ {
- accountKey := randomHash()
- accounts[accountKey] = randomAccount()
-
- accStorage := make(map[common.Hash][]byte)
- for i := 0; i < 20; i++ {
- value := make([]byte, 32)
- rand.Read(value)
- accStorage[randomHash()] = value
-
- }
- storage[accountKey] = accStorage
- }
- return newDiffLayer(parent, common.Hash{}, destructs, accounts, storage)
- }
- b.ResetTimer()
- for i := 0; i < b.N; i++ {
- b.StopTimer()
- var layer snapshot
- layer = emptyLayer()
- for i := 1; i < 128; i++ {
- layer = fill(layer)
- }
- b.StartTimer()
-
- for i := 1; i < 128; i++ {
- dl, ok := layer.(*diffLayer)
- if !ok {
- break
- }
- layer = dl.flatten()
- }
- b.StopTimer()
- }
-}
-
-// This test writes ~324M of diff layers to disk, spread over
-// - 128 individual layers,
-// - each with 200 accounts
-// - containing 200 slots
-//
-// BenchmarkJournal-6 1 1471373923 ns/ops
-// BenchmarkJournal-6 1 1208083335 ns/op // bufio writer
-func BenchmarkJournal(b *testing.B) {
- fill := func(parent snapshot) *diffLayer {
- var (
- destructs = make(map[common.Hash]struct{})
- accounts = make(map[common.Hash][]byte)
- storage = make(map[common.Hash]map[common.Hash][]byte)
- )
- for i := 0; i < 200; i++ {
- accountKey := randomHash()
- accounts[accountKey] = randomAccount()
-
- accStorage := make(map[common.Hash][]byte)
- for i := 0; i < 200; i++ {
- value := make([]byte, 32)
- rand.Read(value)
- accStorage[randomHash()] = value
-
- }
- storage[accountKey] = accStorage
- }
- return newDiffLayer(parent, common.Hash{}, destructs, accounts, storage)
- }
- layer := snapshot(new(diskLayer))
- for i := 1; i < 128; i++ {
- layer = fill(layer)
- }
- b.ResetTimer()
-
- for i := 0; i < b.N; i++ {
- layer.Journal(new(bytes.Buffer))
- }
-}
diff --git a/core/state/snapshot/disklayer_test.go b/core/state/snapshot/disklayer_test.go
deleted file mode 100644
index 5df5efc..0000000
--- a/core/state/snapshot/disklayer_test.go
+++ /dev/null
@@ -1,511 +0,0 @@
-// 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"
- "io/ioutil"
- "os"
- "testing"
-
- "github.com/VictoriaMetrics/fastcache"
- "github.com/ava-labs/coreth/core/rawdb"
- "github.com/ethereum/go-ethereum/common"
- "github.com/ethereum/go-ethereum/ethdb"
- "github.com/ethereum/go-ethereum/ethdb/leveldb"
- "github.com/ethereum/go-ethereum/ethdb/memorydb"
-)
-
-// reverse reverses the contents of a byte slice. It's used to update random accs
-// with deterministic changes.
-func reverse(blob []byte) []byte {
- res := make([]byte, len(blob))
- for i, b := range blob {
- res[len(blob)-1-i] = b
- }
- return res
-}
-
-// Tests that merging something into a disk layer persists it into the database
-// and invalidates any previously written and cached values.
-func TestDiskMerge(t *testing.T) {
- // Create some accounts in the disk layer
- db := memorydb.New()
-
- var (
- accNoModNoCache = common.Hash{0x1}
- accNoModCache = common.Hash{0x2}
- accModNoCache = common.Hash{0x3}
- accModCache = common.Hash{0x4}
- accDelNoCache = common.Hash{0x5}
- accDelCache = common.Hash{0x6}
- conNoModNoCache = common.Hash{0x7}
- conNoModNoCacheSlot = common.Hash{0x70}
- conNoModCache = common.Hash{0x8}
- conNoModCacheSlot = common.Hash{0x80}
- conModNoCache = common.Hash{0x9}
- conModNoCacheSlot = common.Hash{0x90}
- conModCache = common.Hash{0xa}
- conModCacheSlot = common.Hash{0xa0}
- conDelNoCache = common.Hash{0xb}
- conDelNoCacheSlot = common.Hash{0xb0}
- conDelCache = common.Hash{0xc}
- conDelCacheSlot = common.Hash{0xc0}
- conNukeNoCache = common.Hash{0xd}
- conNukeNoCacheSlot = common.Hash{0xd0}
- conNukeCache = common.Hash{0xe}
- conNukeCacheSlot = common.Hash{0xe0}
- baseRoot = randomHash()
- diffRoot = randomHash()
- )
-
- rawdb.WriteAccountSnapshot(db, accNoModNoCache, accNoModNoCache[:])
- rawdb.WriteAccountSnapshot(db, accNoModCache, accNoModCache[:])
- rawdb.WriteAccountSnapshot(db, accModNoCache, accModNoCache[:])
- rawdb.WriteAccountSnapshot(db, accModCache, accModCache[:])
- rawdb.WriteAccountSnapshot(db, accDelNoCache, accDelNoCache[:])
- rawdb.WriteAccountSnapshot(db, accDelCache, accDelCache[:])
-
- rawdb.WriteAccountSnapshot(db, conNoModNoCache, conNoModNoCache[:])
- rawdb.WriteStorageSnapshot(db, conNoModNoCache, conNoModNoCacheSlot, conNoModNoCacheSlot[:])
- rawdb.WriteAccountSnapshot(db, conNoModCache, conNoModCache[:])
- rawdb.WriteStorageSnapshot(db, conNoModCache, conNoModCacheSlot, conNoModCacheSlot[:])
- rawdb.WriteAccountSnapshot(db, conModNoCache, conModNoCache[:])
- rawdb.WriteStorageSnapshot(db, conModNoCache, conModNoCacheSlot, conModNoCacheSlot[:])
- rawdb.WriteAccountSnapshot(db, conModCache, conModCache[:])
- rawdb.WriteStorageSnapshot(db, conModCache, conModCacheSlot, conModCacheSlot[:])
- rawdb.WriteAccountSnapshot(db, conDelNoCache, conDelNoCache[:])
- rawdb.WriteStorageSnapshot(db, conDelNoCache, conDelNoCacheSlot, conDelNoCacheSlot[:])
- rawdb.WriteAccountSnapshot(db, conDelCache, conDelCache[:])
- rawdb.WriteStorageSnapshot(db, conDelCache, conDelCacheSlot, conDelCacheSlot[:])
-
- rawdb.WriteAccountSnapshot(db, conNukeNoCache, conNukeNoCache[:])
- rawdb.WriteStorageSnapshot(db, conNukeNoCache, conNukeNoCacheSlot, conNukeNoCacheSlot[:])
- rawdb.WriteAccountSnapshot(db, conNukeCache, conNukeCache[:])
- rawdb.WriteStorageSnapshot(db, conNukeCache, conNukeCacheSlot, conNukeCacheSlot[:])
-
- rawdb.WriteSnapshotRoot(db, baseRoot)
-
- // Create a disk layer based on the above and cache in some data
- snaps := &Tree{
- layers: map[common.Hash]snapshot{
- baseRoot: &diskLayer{
- diskdb: db,
- cache: fastcache.New(500 * 1024),
- root: baseRoot,
- },
- },
- }
- base := snaps.Snapshot(baseRoot)
- base.AccountRLP(accNoModCache)
- base.AccountRLP(accModCache)
- base.AccountRLP(accDelCache)
- base.Storage(conNoModCache, conNoModCacheSlot)
- base.Storage(conModCache, conModCacheSlot)
- base.Storage(conDelCache, conDelCacheSlot)
- base.Storage(conNukeCache, conNukeCacheSlot)
-
- // Modify or delete some accounts, flatten everything onto disk
- if err := snaps.Update(diffRoot, baseRoot, map[common.Hash]struct{}{
- accDelNoCache: {},
- accDelCache: {},
- conNukeNoCache: {},
- conNukeCache: {},
- }, map[common.Hash][]byte{
- accModNoCache: reverse(accModNoCache[:]),
- accModCache: reverse(accModCache[:]),
- }, map[common.Hash]map[common.Hash][]byte{
- conModNoCache: {conModNoCacheSlot: reverse(conModNoCacheSlot[:])},
- conModCache: {conModCacheSlot: reverse(conModCacheSlot[:])},
- conDelNoCache: {conDelNoCacheSlot: nil},
- conDelCache: {conDelCacheSlot: nil},
- }); err != nil {
- t.Fatalf("failed to update snapshot tree: %v", err)
- }
- if err := snaps.Cap(diffRoot, 0); err != nil {
- t.Fatalf("failed to flatten snapshot tree: %v", err)
- }
- // Retrieve all the data through the disk layer and validate it
- base = snaps.Snapshot(diffRoot)
- if _, ok := base.(*diskLayer); !ok {
- t.Fatalf("update not flattend into the disk layer")
- }
-
- // assertAccount ensures that an account matches the given blob.
- assertAccount := func(account common.Hash, data []byte) {
- t.Helper()
- blob, err := base.AccountRLP(account)
- if err != nil {
- t.Errorf("account access (%x) failed: %v", account, err)
- } else if !bytes.Equal(blob, data) {
- t.Errorf("account access (%x) mismatch: have %x, want %x", account, blob, data)
- }
- }
- assertAccount(accNoModNoCache, accNoModNoCache[:])
- assertAccount(accNoModCache, accNoModCache[:])
- assertAccount(accModNoCache, reverse(accModNoCache[:]))
- assertAccount(accModCache, reverse(accModCache[:]))
- assertAccount(accDelNoCache, nil)
- assertAccount(accDelCache, nil)
-
- // assertStorage ensures that a storage slot matches the given blob.
- assertStorage := func(account common.Hash, slot common.Hash, data []byte) {
- t.Helper()
- blob, err := base.Storage(account, slot)
- if err != nil {
- t.Errorf("storage access (%x:%x) failed: %v", account, slot, err)
- } else if !bytes.Equal(blob, data) {
- t.Errorf("storage access (%x:%x) mismatch: have %x, want %x", account, slot, blob, data)
- }
- }
- assertStorage(conNoModNoCache, conNoModNoCacheSlot, conNoModNoCacheSlot[:])
- assertStorage(conNoModCache, conNoModCacheSlot, conNoModCacheSlot[:])
- assertStorage(conModNoCache, conModNoCacheSlot, reverse(conModNoCacheSlot[:]))
- assertStorage(conModCache, conModCacheSlot, reverse(conModCacheSlot[:]))
- assertStorage(conDelNoCache, conDelNoCacheSlot, nil)
- assertStorage(conDelCache, conDelCacheSlot, nil)
- assertStorage(conNukeNoCache, conNukeNoCacheSlot, nil)
- assertStorage(conNukeCache, conNukeCacheSlot, nil)
-
- // Retrieve all the data directly from the database and validate it
-
- // assertDatabaseAccount ensures that an account from the database matches the given blob.
- assertDatabaseAccount := func(account common.Hash, data []byte) {
- t.Helper()
- if blob := rawdb.ReadAccountSnapshot(db, account); !bytes.Equal(blob, data) {
- t.Errorf("account database access (%x) mismatch: have %x, want %x", account, blob, data)
- }
- }
- assertDatabaseAccount(accNoModNoCache, accNoModNoCache[:])
- assertDatabaseAccount(accNoModCache, accNoModCache[:])
- assertDatabaseAccount(accModNoCache, reverse(accModNoCache[:]))
- assertDatabaseAccount(accModCache, reverse(accModCache[:]))
- assertDatabaseAccount(accDelNoCache, nil)
- assertDatabaseAccount(accDelCache, nil)
-
- // assertDatabaseStorage ensures that a storage slot from the database matches the given blob.
- assertDatabaseStorage := func(account common.Hash, slot common.Hash, data []byte) {
- t.Helper()
- if blob := rawdb.ReadStorageSnapshot(db, account, slot); !bytes.Equal(blob, data) {
- t.Errorf("storage database access (%x:%x) mismatch: have %x, want %x", account, slot, blob, data)
- }
- }
- assertDatabaseStorage(conNoModNoCache, conNoModNoCacheSlot, conNoModNoCacheSlot[:])
- assertDatabaseStorage(conNoModCache, conNoModCacheSlot, conNoModCacheSlot[:])
- assertDatabaseStorage(conModNoCache, conModNoCacheSlot, reverse(conModNoCacheSlot[:]))
- assertDatabaseStorage(conModCache, conModCacheSlot, reverse(conModCacheSlot[:]))
- assertDatabaseStorage(conDelNoCache, conDelNoCacheSlot, nil)
- assertDatabaseStorage(conDelCache, conDelCacheSlot, nil)
- assertDatabaseStorage(conNukeNoCache, conNukeNoCacheSlot, nil)
- assertDatabaseStorage(conNukeCache, conNukeCacheSlot, nil)
-}
-
-// Tests that merging something into a disk layer persists it into the database
-// and invalidates any previously written and cached values, discarding anything
-// after the in-progress generation marker.
-func TestDiskPartialMerge(t *testing.T) {
- // Iterate the test a few times to ensure we pick various internal orderings
- // for the data slots as well as the progress marker.
- for i := 0; i < 1024; i++ {
- // Create some accounts in the disk layer
- db := memorydb.New()
-
- var (
- accNoModNoCache = randomHash()
- accNoModCache = randomHash()
- accModNoCache = randomHash()
- accModCache = randomHash()
- accDelNoCache = randomHash()
- accDelCache = randomHash()
- conNoModNoCache = randomHash()
- conNoModNoCacheSlot = randomHash()
- conNoModCache = randomHash()
- conNoModCacheSlot = randomHash()
- conModNoCache = randomHash()
- conModNoCacheSlot = randomHash()
- conModCache = randomHash()
- conModCacheSlot = randomHash()
- conDelNoCache = randomHash()
- conDelNoCacheSlot = randomHash()
- conDelCache = randomHash()
- conDelCacheSlot = randomHash()
- conNukeNoCache = randomHash()
- conNukeNoCacheSlot = randomHash()
- conNukeCache = randomHash()
- conNukeCacheSlot = randomHash()
- baseRoot = randomHash()
- diffRoot = randomHash()
- genMarker = append(randomHash().Bytes(), randomHash().Bytes()...)
- )
-
- // insertAccount injects an account into the database if it's after the
- // generator marker, drops the op otherwise. This is needed to seed the
- // database with a valid starting snapshot.
- insertAccount := func(account common.Hash, data []byte) {
- if bytes.Compare(account[:], genMarker) <= 0 {
- rawdb.WriteAccountSnapshot(db, account, data[:])
- }
- }
- insertAccount(accNoModNoCache, accNoModNoCache[:])
- insertAccount(accNoModCache, accNoModCache[:])
- insertAccount(accModNoCache, accModNoCache[:])
- insertAccount(accModCache, accModCache[:])
- insertAccount(accDelNoCache, accDelNoCache[:])
- insertAccount(accDelCache, accDelCache[:])
-
- // insertStorage injects a storage slot into the database if it's after
- // the generator marker, drops the op otherwise. This is needed to seed
- // the database with a valid starting snapshot.
- insertStorage := func(account common.Hash, slot common.Hash, data []byte) {
- if bytes.Compare(append(account[:], slot[:]...), genMarker) <= 0 {
- rawdb.WriteStorageSnapshot(db, account, slot, data[:])
- }
- }
- insertAccount(conNoModNoCache, conNoModNoCache[:])
- insertStorage(conNoModNoCache, conNoModNoCacheSlot, conNoModNoCacheSlot[:])
- insertAccount(conNoModCache, conNoModCache[:])
- insertStorage(conNoModCache, conNoModCacheSlot, conNoModCacheSlot[:])
- insertAccount(conModNoCache, conModNoCache[:])
- insertStorage(conModNoCache, conModNoCacheSlot, conModNoCacheSlot[:])
- insertAccount(conModCache, conModCache[:])
- insertStorage(conModCache, conModCacheSlot, conModCacheSlot[:])
- insertAccount(conDelNoCache, conDelNoCache[:])
- insertStorage(conDelNoCache, conDelNoCacheSlot, conDelNoCacheSlot[:])
- insertAccount(conDelCache, conDelCache[:])
- insertStorage(conDelCache, conDelCacheSlot, conDelCacheSlot[:])
-
- insertAccount(conNukeNoCache, conNukeNoCache[:])
- insertStorage(conNukeNoCache, conNukeNoCacheSlot, conNukeNoCacheSlot[:])
- insertAccount(conNukeCache, conNukeCache[:])
- insertStorage(conNukeCache, conNukeCacheSlot, conNukeCacheSlot[:])
-
- rawdb.WriteSnapshotRoot(db, baseRoot)
-
- // Create a disk layer based on the above using a random progress marker
- // and cache in some data.
- snaps := &Tree{
- layers: map[common.Hash]snapshot{
- baseRoot: &diskLayer{
- diskdb: db,
- cache: fastcache.New(500 * 1024),
- root: baseRoot,
- },
- },
- }
- snaps.layers[baseRoot].(*diskLayer).genMarker = genMarker
- base := snaps.Snapshot(baseRoot)
-
- // assertAccount ensures that an account matches the given blob if it's
- // already covered by the disk snapshot, and errors out otherwise.
- assertAccount := func(account common.Hash, data []byte) {
- t.Helper()
- blob, err := base.AccountRLP(account)
- if bytes.Compare(account[:], genMarker) > 0 && err != ErrNotCoveredYet {
- t.Fatalf("test %d: post-marker (%x) account access (%x) succeeded: %x", i, genMarker, account, blob)
- }
- if bytes.Compare(account[:], genMarker) <= 0 && !bytes.Equal(blob, data) {
- t.Fatalf("test %d: pre-marker (%x) account access (%x) mismatch: have %x, want %x", i, genMarker, account, blob, data)
- }
- }
- assertAccount(accNoModCache, accNoModCache[:])
- assertAccount(accModCache, accModCache[:])
- assertAccount(accDelCache, accDelCache[:])
-
- // assertStorage ensures that a storage slot matches the given blob if
- // it's already covered by the disk snapshot, and errors out otherwise.
- assertStorage := func(account common.Hash, slot common.Hash, data []byte) {
- t.Helper()
- blob, err := base.Storage(account, slot)
- if bytes.Compare(append(account[:], slot[:]...), genMarker) > 0 && err != ErrNotCoveredYet {
- t.Fatalf("test %d: post-marker (%x) storage access (%x:%x) succeeded: %x", i, genMarker, account, slot, blob)
- }
- if bytes.Compare(append(account[:], slot[:]...), genMarker) <= 0 && !bytes.Equal(blob, data) {
- t.Fatalf("test %d: pre-marker (%x) storage access (%x:%x) mismatch: have %x, want %x", i, genMarker, account, slot, blob, data)
- }
- }
- assertStorage(conNoModCache, conNoModCacheSlot, conNoModCacheSlot[:])
- assertStorage(conModCache, conModCacheSlot, conModCacheSlot[:])
- assertStorage(conDelCache, conDelCacheSlot, conDelCacheSlot[:])
- assertStorage(conNukeCache, conNukeCacheSlot, conNukeCacheSlot[:])
-
- // Modify or delete some accounts, flatten everything onto disk
- if err := snaps.Update(diffRoot, baseRoot, map[common.Hash]struct{}{
- accDelNoCache: {},
- accDelCache: {},
- conNukeNoCache: {},
- conNukeCache: {},
- }, map[common.Hash][]byte{
- accModNoCache: reverse(accModNoCache[:]),
- accModCache: reverse(accModCache[:]),
- }, map[common.Hash]map[common.Hash][]byte{
- conModNoCache: {conModNoCacheSlot: reverse(conModNoCacheSlot[:])},
- conModCache: {conModCacheSlot: reverse(conModCacheSlot[:])},
- conDelNoCache: {conDelNoCacheSlot: nil},
- conDelCache: {conDelCacheSlot: nil},
- }); err != nil {
- t.Fatalf("test %d: failed to update snapshot tree: %v", i, err)
- }
- if err := snaps.Cap(diffRoot, 0); err != nil {
- t.Fatalf("test %d: failed to flatten snapshot tree: %v", i, err)
- }
- // Retrieve all the data through the disk layer and validate it
- base = snaps.Snapshot(diffRoot)
- if _, ok := base.(*diskLayer); !ok {
- t.Fatalf("test %d: update not flattend into the disk layer", i)
- }
- assertAccount(accNoModNoCache, accNoModNoCache[:])
- assertAccount(accNoModCache, accNoModCache[:])
- assertAccount(accModNoCache, reverse(accModNoCache[:]))
- assertAccount(accModCache, reverse(accModCache[:]))
- assertAccount(accDelNoCache, nil)
- assertAccount(accDelCache, nil)
-
- assertStorage(conNoModNoCache, conNoModNoCacheSlot, conNoModNoCacheSlot[:])
- assertStorage(conNoModCache, conNoModCacheSlot, conNoModCacheSlot[:])
- assertStorage(conModNoCache, conModNoCacheSlot, reverse(conModNoCacheSlot[:]))
- assertStorage(conModCache, conModCacheSlot, reverse(conModCacheSlot[:]))
- assertStorage(conDelNoCache, conDelNoCacheSlot, nil)
- assertStorage(conDelCache, conDelCacheSlot, nil)
- assertStorage(conNukeNoCache, conNukeNoCacheSlot, nil)
- assertStorage(conNukeCache, conNukeCacheSlot, nil)
-
- // Retrieve all the data directly from the database and validate it
-
- // assertDatabaseAccount ensures that an account inside the database matches
- // the given blob if it's already covered by the disk snapshot, and does not
- // exist otherwise.
- assertDatabaseAccount := func(account common.Hash, data []byte) {
- t.Helper()
- blob := rawdb.ReadAccountSnapshot(db, account)
- if bytes.Compare(account[:], genMarker) > 0 && blob != nil {
- t.Fatalf("test %d: post-marker (%x) account database access (%x) succeeded: %x", i, genMarker, account, blob)
- }
- if bytes.Compare(account[:], genMarker) <= 0 && !bytes.Equal(blob, data) {
- t.Fatalf("test %d: pre-marker (%x) account database access (%x) mismatch: have %x, want %x", i, genMarker, account, blob, data)
- }
- }
- assertDatabaseAccount(accNoModNoCache, accNoModNoCache[:])
- assertDatabaseAccount(accNoModCache, accNoModCache[:])
- assertDatabaseAccount(accModNoCache, reverse(accModNoCache[:]))
- assertDatabaseAccount(accModCache, reverse(accModCache[:]))
- assertDatabaseAccount(accDelNoCache, nil)
- assertDatabaseAccount(accDelCache, nil)
-
- // assertDatabaseStorage ensures that a storage slot inside the database
- // matches the given blob if it's already covered by the disk snapshot,
- // and does not exist otherwise.
- assertDatabaseStorage := func(account common.Hash, slot common.Hash, data []byte) {
- t.Helper()
- blob := rawdb.ReadStorageSnapshot(db, account, slot)
- if bytes.Compare(append(account[:], slot[:]...), genMarker) > 0 && blob != nil {
- t.Fatalf("test %d: post-marker (%x) storage database access (%x:%x) succeeded: %x", i, genMarker, account, slot, blob)
- }
- if bytes.Compare(append(account[:], slot[:]...), genMarker) <= 0 && !bytes.Equal(blob, data) {
- t.Fatalf("test %d: pre-marker (%x) storage database access (%x:%x) mismatch: have %x, want %x", i, genMarker, account, slot, blob, data)
- }
- }
- assertDatabaseStorage(conNoModNoCache, conNoModNoCacheSlot, conNoModNoCacheSlot[:])
- assertDatabaseStorage(conNoModCache, conNoModCacheSlot, conNoModCacheSlot[:])
- assertDatabaseStorage(conModNoCache, conModNoCacheSlot, reverse(conModNoCacheSlot[:]))
- assertDatabaseStorage(conModCache, conModCacheSlot, reverse(conModCacheSlot[:]))
- assertDatabaseStorage(conDelNoCache, conDelNoCacheSlot, nil)
- assertDatabaseStorage(conDelCache, conDelCacheSlot, nil)
- assertDatabaseStorage(conNukeNoCache, conNukeNoCacheSlot, nil)
- assertDatabaseStorage(conNukeCache, conNukeCacheSlot, nil)
- }
-}
-
-// Tests that merging something into a disk layer persists it into the database
-// and invalidates any previously written and cached values, discarding anything
-// after the in-progress generation marker.
-//
-// This test case is a tiny specialized case of TestDiskPartialMerge, which tests
-// some very specific cornercases that random tests won't ever trigger.
-func TestDiskMidAccountPartialMerge(t *testing.T) {
- // TODO(@karalabe) ?
-}
-
-// TestDiskSeek tests that seek-operations work on the disk layer
-func TestDiskSeek(t *testing.T) {
- // Create some accounts in the disk layer
- var db ethdb.Database
-
- if dir, err := ioutil.TempDir("", "disklayer-test"); err != nil {
- t.Fatal(err)
- } else {
- defer os.RemoveAll(dir)
- diskdb, err := leveldb.New(dir, 256, 0, "")
- if err != nil {
- t.Fatal(err)
- }
- db = rawdb.NewDatabase(diskdb)
- }
- // Fill even keys [0,2,4...]
- for i := 0; i < 0xff; i += 2 {
- acc := common.Hash{byte(i)}
- rawdb.WriteAccountSnapshot(db, acc, acc[:])
- }
- // Add an 'higher' key, with incorrect (higher) prefix
- highKey := []byte{rawdb.SnapshotAccountPrefix[0] + 1}
- db.Put(highKey, []byte{0xff, 0xff})
-
- baseRoot := randomHash()
- rawdb.WriteSnapshotRoot(db, baseRoot)
-
- snaps := &Tree{
- layers: map[common.Hash]snapshot{
- baseRoot: &diskLayer{
- diskdb: db,
- cache: fastcache.New(500 * 1024),
- root: baseRoot,
- },
- },
- }
- // Test some different seek positions
- type testcase struct {
- pos byte
- expkey byte
- }
- var cases = []testcase{
- {0xff, 0x55}, // this should exit immediately without checking key
- {0x01, 0x02},
- {0xfe, 0xfe},
- {0xfd, 0xfe},
- {0x00, 0x00},
- }
- for i, tc := range cases {
- it, err := snaps.AccountIterator(baseRoot, common.Hash{tc.pos})
- if err != nil {
- t.Fatalf("case %d, error: %v", i, err)
- }
- count := 0
- for it.Next() {
- k, v, err := it.Hash()[0], it.Account()[0], it.Error()
- if err != nil {
- t.Fatalf("test %d, item %d, error: %v", i, count, err)
- }
- // First item in iterator should have the expected key
- if count == 0 && k != tc.expkey {
- t.Fatalf("test %d, item %d, got %v exp %v", i, count, k, tc.expkey)
- }
- count++
- if v != k {
- t.Fatalf("test %d, item %d, value wrong, got %v exp %v", i, count, v, k)
- }
- }
- }
-}
diff --git a/core/state/snapshot/iterator_test.go b/core/state/snapshot/iterator_test.go
deleted file mode 100644
index ef4859c..0000000
--- a/core/state/snapshot/iterator_test.go
+++ /dev/null
@@ -1,1046 +0,0 @@
-// 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"
- "encoding/binary"
- "fmt"
- "math/rand"
- "testing"
-
- "github.com/VictoriaMetrics/fastcache"
- "github.com/ava-labs/coreth/core/rawdb"
- "github.com/ethereum/go-ethereum/common"
-)
-
-// TestAccountIteratorBasics tests some simple single-layer(diff and disk) iteration
-func TestAccountIteratorBasics(t *testing.T) {
- var (
- destructs = make(map[common.Hash]struct{})
- accounts = make(map[common.Hash][]byte)
- storage = make(map[common.Hash]map[common.Hash][]byte)
- )
- // Fill up a parent
- for i := 0; i < 100; i++ {
- h := randomHash()
- data := randomAccount()
-
- accounts[h] = data
- if rand.Intn(4) == 0 {
- destructs[h] = struct{}{}
- }
- if rand.Intn(2) == 0 {
- accStorage := make(map[common.Hash][]byte)
- value := make([]byte, 32)
- rand.Read(value)
- accStorage[randomHash()] = value
- storage[h] = accStorage
- }
- }
- // Add some (identical) layers on top
- diffLayer := newDiffLayer(emptyLayer(), common.Hash{}, copyDestructs(destructs), copyAccounts(accounts), copyStorage(storage))
- it := diffLayer.AccountIterator(common.Hash{})
- verifyIterator(t, 100, it, verifyNothing) // Nil is allowed for single layer iterator
-
- 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++ {
- rand.Read(value)
- 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
- diffLayer := newDiffLayer(emptyLayer(), common.Hash{}, nil, copyAccounts(accounts), copyStorage(storage))
- for account := range accounts {
- it, _ := diffLayer.StorageIterator(account, common.Hash{})
- verifyIterator(t, 100, it, verifyNothing) // Nil is allowed for single layer iterator
- }
-
- diskLayer := diffToDisk(diffLayer)
- for account := range accounts {
- it, _ := diskLayer.StorageIterator(account, common.Hash{})
- verifyIterator(t, 100-nilStorage[account], it, verifyNothing) // Nil is allowed for single layer iterator
- }
-}
-
-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:]
- return len(ti.values) > 0
-}
-
-func (ti *testIterator) Error() error {
- return nil
-}
-
-func (ti *testIterator) Hash() common.Hash {
- return common.BytesToHash([]byte{ti.values[0]})
-}
-
-func (ti *testIterator) Account() []byte {
- return nil
-}
-
-func (ti *testIterator) Slot() []byte {
- return nil
-}
-
-func (ti *testIterator) Release() {}
-
-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}},
- } {
- var iterators []*weightedIterator
- for i, data := range tc.lists {
- it := newTestIterator(data...)
- iterators = append(iterators, &weightedIterator{it, i})
- }
- fi := &fastIterator{
- iterators: iterators,
- initiated: false,
- }
- count := 0
- for fi.Next() {
- if got, exp := fi.Hash()[31], tc.expKeys[count]; exp != got {
- t.Errorf("tc %d, [%d]: got %d exp %d", i, count, got, exp)
- }
- count++
- }
- }
-}
-
-type verifyContent int
-
-const (
- verifyNothing verifyContent = iota
- verifyAccount
- verifyStorage
-)
-
-func verifyIterator(t *testing.T, expCount int, it Iterator, verify verifyContent) {
- t.Helper()
-
- var (
- count = 0
- last = common.Hash{}
- )
- for it.Next() {
- hash := it.Hash()
- if bytes.Compare(last[:], hash[:]) >= 0 {
- t.Errorf("wrong order: %x >= %x", last, hash)
- }
- 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 {
- t.Errorf("iterator returned nil-value for hash %x", hash)
- }
- last = hash
- }
- 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)
- }
-}
-
-// TestAccountIteratorTraversal tests some simple multi-layer iteration.
-func TestAccountIteratorTraversal(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
- snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"), nil,
- randomAccountSet("0xaa", "0xee", "0xff", "0xf0"), nil)
-
- snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"), nil,
- randomAccountSet("0xbb", "0xdd", "0xf0"), nil)
-
- snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"), nil,
- randomAccountSet("0xcc", "0xf0", "0xff"), nil)
-
- // Verify the single and multi-layer iterators
- head := snaps.Snapshot(common.HexToHash("0x04"))
-
- verifyIterator(t, 3, head.(snapshot).AccountIterator(common.Hash{}), verifyNothing)
- verifyIterator(t, 7, head.(*diffLayer).newBinaryAccountIterator(), verifyAccount)
-
- it, _ := snaps.AccountIterator(common.HexToHash("0x04"), common.Hash{})
- verifyIterator(t, 7, it, verifyAccount)
- 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)
- verifyIterator(t, 7, head.(*diffLayer).newBinaryAccountIterator(), verifyAccount)
-
- it, _ = snaps.AccountIterator(common.HexToHash("0x04"), common.Hash{})
- 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
- snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"), nil,
- randomAccountSet("0xaa"), randomStorageSet([]string{"0xaa"}, [][]string{{"0x01", "0x02", "0x03"}}, nil))
-
- snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"), nil,
- randomAccountSet("0xaa"), randomStorageSet([]string{"0xaa"}, [][]string{{"0x04", "0x05", "0x06"}}, nil))
-
- snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"), nil,
- randomAccountSet("0xaa"), randomStorageSet([]string{"0xaa"}, [][]string{{"0x01", "0x02", "0x03"}}, nil))
-
- // Verify the single and multi-layer iterators
- head := snaps.Snapshot(common.HexToHash("0x04"))
-
- diffIter, _ := head.(snapshot).StorageIterator(common.HexToHash("0xaa"), common.Hash{})
- verifyIterator(t, 3, diffIter, verifyNothing)
- verifyIterator(t, 6, head.(*diffLayer).newBinaryStorageIterator(common.HexToHash("0xaa")), verifyStorage)
-
- 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)
- verifyIterator(t, 6, head.(*diffLayer).newBinaryStorageIterator(common.HexToHash("0xaa")), verifyStorage)
-
- it, _ = snaps.StorageIterator(common.HexToHash("0x04"), common.HexToHash("0xaa"), common.Hash{})
- verifyIterator(t, 6, it, verifyStorage)
- it.Release()
-}
-
-// TestAccountIteratorTraversalValues tests some multi-layer iteration, where we
-// also expect the correct values to show up.
-func TestAccountIteratorTraversalValues(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,
- },
- }
- // Create a batch of account 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
- snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"), nil, a, nil)
- snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"), nil, b, nil)
- snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"), nil, c, nil)
- snaps.Update(common.HexToHash("0x05"), common.HexToHash("0x04"), nil, d, nil)
- snaps.Update(common.HexToHash("0x06"), common.HexToHash("0x05"), nil, e, nil)
- snaps.Update(common.HexToHash("0x07"), common.HexToHash("0x06"), nil, f, nil)
- snaps.Update(common.HexToHash("0x08"), common.HexToHash("0x07"), nil, g, nil)
- snaps.Update(common.HexToHash("0x09"), common.HexToHash("0x08"), nil, h, nil)
-
- it, _ := snaps.AccountIterator(common.HexToHash("0x09"), common.Hash{})
- head := snaps.Snapshot(common.HexToHash("0x09"))
- 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()
-
- // 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()
-}
-
-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
- snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"), nil, randomAccountSet("0xaa"), wrapStorage(a))
- snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"), nil, randomAccountSet("0xaa"), wrapStorage(b))
- snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"), nil, randomAccountSet("0xaa"), wrapStorage(c))
- snaps.Update(common.HexToHash("0x05"), common.HexToHash("0x04"), nil, randomAccountSet("0xaa"), wrapStorage(d))
- snaps.Update(common.HexToHash("0x06"), common.HexToHash("0x05"), nil, randomAccountSet("0xaa"), wrapStorage(e))
- snaps.Update(common.HexToHash("0x07"), common.HexToHash("0x06"), nil, randomAccountSet("0xaa"), wrapStorage(e))
- snaps.Update(common.HexToHash("0x08"), common.HexToHash("0x07"), nil, randomAccountSet("0xaa"), wrapStorage(g))
- snaps.Update(common.HexToHash("0x09"), common.HexToHash("0x08"), nil, randomAccountSet("0xaa"), wrapStorage(h))
-
- 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()
-}
-
-// This testcase is notorious, all layers contain the exact same 200 accounts.
-func TestAccountIteratorLargeTraversal(t *testing.T) {
- // Create a custom account factory to recreate the same addresses
- makeAccounts := func(num int) map[common.Hash][]byte {
- 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
- }
- // 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++ {
- snaps.Update(common.HexToHash(fmt.Sprintf("0x%02x", i+1)), common.HexToHash(fmt.Sprintf("0x%02x", i)), nil, makeAccounts(200), nil)
- }
- // Iterate the entire stack and ensure everything is hit only once
- head := snaps.Snapshot(common.HexToHash("0x80"))
- verifyIterator(t, 200, head.(snapshot).AccountIterator(common.Hash{}), verifyNothing)
- verifyIterator(t, 200, head.(*diffLayer).newBinaryAccountIterator(), verifyAccount)
-
- it, _ := snaps.AccountIterator(common.HexToHash("0x80"), common.Hash{})
- verifyIterator(t, 200, it, verifyAccount)
- 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)
-
- verifyIterator(t, 200, head.(*diffLayer).newBinaryAccountIterator(), verifyAccount)
-
- it, _ = snaps.AccountIterator(common.HexToHash("0x80"), common.Hash{})
- verifyIterator(t, 200, it, verifyAccount)
- it.Release()
-}
-
-// TestAccountIteratorFlattening tests what happens when we
-// - have a live iterator on child C (parent C1 -> C2 .. CN)
-// - flattens C2 all the way into CN
-// - continues iterating
-func TestAccountIteratorFlattening(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,
- },
- }
- // Create a stack of diffs on top
- snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"), nil,
- randomAccountSet("0xaa", "0xee", "0xff", "0xf0"), nil)
-
- snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"), nil,
- randomAccountSet("0xbb", "0xdd", "0xf0"), nil)
-
- snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"), nil,
- randomAccountSet("0xcc", "0xf0", "0xff"), nil)
-
- // Create an iterator and flatten the data from underneath it
- it, _ := snaps.AccountIterator(common.HexToHash("0x04"), common.Hash{})
- 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)
-}
-
-func TestAccountIteratorSeek(t *testing.T) {
- // 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,
- },
- }
- snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"), nil,
- randomAccountSet("0xaa", "0xee", "0xff", "0xf0"), nil)
-
- snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"), nil,
- randomAccountSet("0xbb", "0xdd", "0xf0"), nil)
-
- snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"), nil,
- randomAccountSet("0xcc", "0xf0", "0xff"), nil)
-
- // 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
- it, _ := snaps.AccountIterator(common.HexToHash("0x02"), common.HexToHash("0xdd"))
- defer it.Release()
- verifyIterator(t, 3, it, verifyAccount) // expected: ee, f0, ff
-
- it, _ = snaps.AccountIterator(common.HexToHash("0x02"), common.HexToHash("0xaa"))
- defer it.Release()
- verifyIterator(t, 4, it, verifyAccount) // expected: aa, ee, f0, ff
-
- it, _ = snaps.AccountIterator(common.HexToHash("0x02"), common.HexToHash("0xff"))
- defer it.Release()
- verifyIterator(t, 1, it, verifyAccount) // expected: ff
-
- it, _ = snaps.AccountIterator(common.HexToHash("0x02"), common.HexToHash("0xff1"))
- defer it.Release()
- verifyIterator(t, 0, it, verifyAccount) // expected: nothing
-
- it, _ = snaps.AccountIterator(common.HexToHash("0x04"), common.HexToHash("0xbb"))
- defer it.Release()
- verifyIterator(t, 6, it, verifyAccount) // expected: bb, cc, dd, ee, f0, ff
-
- it, _ = snaps.AccountIterator(common.HexToHash("0x04"), common.HexToHash("0xef"))
- defer it.Release()
- verifyIterator(t, 2, it, verifyAccount) // expected: f0, ff
-
- it, _ = snaps.AccountIterator(common.HexToHash("0x04"), common.HexToHash("0xf0"))
- defer it.Release()
- verifyIterator(t, 2, it, verifyAccount) // expected: f0, ff
-
- it, _ = snaps.AccountIterator(common.HexToHash("0x04"), common.HexToHash("0xff"))
- defer it.Release()
- verifyIterator(t, 1, it, verifyAccount) // expected: ff
-
- it, _ = snaps.AccountIterator(common.HexToHash("0x04"), common.HexToHash("0xff1"))
- defer it.Release()
- verifyIterator(t, 0, it, verifyAccount) // expected: nothing
-}
-
-func TestStorageIteratorSeek(t *testing.T) {
- // 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
- snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"), nil,
- randomAccountSet("0xaa"), randomStorageSet([]string{"0xaa"}, [][]string{{"0x01", "0x03", "0x05"}}, nil))
-
- snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"), nil,
- randomAccountSet("0xaa"), randomStorageSet([]string{"0xaa"}, [][]string{{"0x02", "0x05", "0x06"}}, nil))
-
- snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"), nil,
- 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
- it, _ := snaps.StorageIterator(common.HexToHash("0x02"), common.HexToHash("0xaa"), common.HexToHash("0x01"))
- defer it.Release()
- verifyIterator(t, 3, it, verifyStorage) // expected: 01, 03, 05
-
- it, _ = snaps.StorageIterator(common.HexToHash("0x02"), common.HexToHash("0xaa"), common.HexToHash("0x02"))
- defer it.Release()
- verifyIterator(t, 2, it, verifyStorage) // expected: 03, 05
-
- it, _ = snaps.StorageIterator(common.HexToHash("0x02"), common.HexToHash("0xaa"), common.HexToHash("0x5"))
- defer it.Release()
- verifyIterator(t, 1, it, verifyStorage) // expected: 05
-
- it, _ = snaps.StorageIterator(common.HexToHash("0x02"), common.HexToHash("0xaa"), common.HexToHash("0x6"))
- defer it.Release()
- verifyIterator(t, 0, it, verifyStorage) // expected: nothing
-
- it, _ = snaps.StorageIterator(common.HexToHash("0x04"), common.HexToHash("0xaa"), common.HexToHash("0x01"))
- defer it.Release()
- verifyIterator(t, 6, it, verifyStorage) // expected: 01, 02, 03, 05, 06, 08
-
- it, _ = snaps.StorageIterator(common.HexToHash("0x04"), common.HexToHash("0xaa"), common.HexToHash("0x05"))
- defer it.Release()
- verifyIterator(t, 3, it, verifyStorage) // expected: 05, 06, 08
-
- it, _ = snaps.StorageIterator(common.HexToHash("0x04"), common.HexToHash("0xaa"), common.HexToHash("0x08"))
- defer it.Release()
- verifyIterator(t, 1, it, verifyStorage) // expected: 08
-
- it, _ = snaps.StorageIterator(common.HexToHash("0x04"), common.HexToHash("0xaa"), common.HexToHash("0x09"))
- defer it.Release()
- verifyIterator(t, 0, it, verifyStorage) // expected: nothing
-}
-
-// TestAccountIteratorDeletions tests that the iterator behaves correct when there are
-// 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) {
- // 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
- snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"),
- nil, randomAccountSet("0x11", "0x22", "0x33"), nil)
-
- deleted := common.HexToHash("0x22")
- destructed := map[common.Hash]struct{}{
- deleted: {},
- }
- snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"),
- destructed, randomAccountSet("0x11", "0x33"), nil)
-
- snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"),
- nil, randomAccountSet("0x33", "0x44", "0x55"), nil)
-
- // The output should be 11,33,44,55
- it, _ := snaps.AccountIterator(common.HexToHash("0x04"), common.Hash{})
- // Do a quick check
- verifyIterator(t, 4, it, verifyAccount)
- 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)
- }
- if hash == deleted {
- t.Errorf("expected deleted elem %x to not be returned by iterator", deleted)
- }
- }
-}
-
-func TestStorageIteratorDeletions(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
- snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"), nil,
- randomAccountSet("0xaa"), randomStorageSet([]string{"0xaa"}, [][]string{{"0x01", "0x03", "0x05"}}, nil))
-
- snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"), nil,
- 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{})
- 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"))
- verifyIterator(t, 3, it, verifyStorage)
- it.Release()
-
- // Destruct the whole storage
- destructed := map[common.Hash]struct{}{
- common.HexToHash("0xaa"): {},
- }
- snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"), destructed, nil, nil)
-
- it, _ = snaps.StorageIterator(common.HexToHash("0x04"), common.HexToHash("0xaa"), common.Hash{})
- verifyIterator(t, 0, it, verifyStorage)
- it.Release()
-
- // Re-insert the slots of the same account
- snaps.Update(common.HexToHash("0x05"), common.HexToHash("0x04"), nil,
- 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{})
- 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{})
- verifyIterator(t, 2, it, verifyStorage) // The output should be 11,12
- it.Release()
-
- verifyIterator(t, 2, snaps.Snapshot(common.HexToHash("0x06")).(*diffLayer).newBinaryStorageIterator(common.HexToHash("0xaa")), verifyStorage)
-}
-
-// BenchmarkAccountIteratorTraversal is a bit a bit notorious -- all layers contain the
-// exact same 200 accounts. That means that we need to process 2000 items, but
-// only spit out 200 values eventually.
-//
-// The value-fetching benchmark is easy on the binary iterator, since it never has to reach
-// down at any depth for retrieving the values -- all are on the toppmost layer
-//
-// 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) {
- // Create a custom account factory to recreate the same addresses
- makeAccounts := func(num int) map[common.Hash][]byte {
- 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
- }
- // 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++ {
- snaps.Update(common.HexToHash(fmt.Sprintf("0x%02x", i+1)), common.HexToHash(fmt.Sprintf("0x%02x", i)), nil, makeAccounts(200), nil)
- }
- // We call this once before the benchmark, so the creation of
- // sorted accountlists are not included in the results.
- head := snaps.Snapshot(common.HexToHash("0x65"))
- head.(*diffLayer).newBinaryAccountIterator()
-
- b.Run("binary iterator keys", func(b *testing.B) {
- for i := 0; i < b.N; i++ {
- got := 0
- it := head.(*diffLayer).newBinaryAccountIterator()
- for it.Next() {
- got++
- }
- if exp := 200; got != exp {
- b.Errorf("iterator len wrong, expected %d, got %d", exp, got)
- }
- }
- })
- b.Run("binary iterator values", func(b *testing.B) {
- for i := 0; i < b.N; i++ {
- got := 0
- it := head.(*diffLayer).newBinaryAccountIterator()
- for it.Next() {
- got++
- head.(*diffLayer).accountRLP(it.Hash(), 0)
- }
- 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) {
- for i := 0; i < b.N; i++ {
- it, _ := snaps.AccountIterator(common.HexToHash("0x65"), common.Hash{})
- defer it.Release()
-
- got := 0
- for it.Next() {
- got++
- }
- if exp := 200; got != exp {
- b.Errorf("iterator len wrong, expected %d, got %d", exp, got)
- }
- }
- })
- b.Run("fast iterator values", func(b *testing.B) {
- for i := 0; i < b.N; i++ {
- it, _ := snaps.AccountIterator(common.HexToHash("0x65"), common.Hash{})
- defer it.Release()
-
- got := 0
- for it.Next() {
- got++
- it.Account()
- }
- if exp := 200; got != exp {
- b.Errorf("iterator len wrong, expected %d, got %d", exp, got)
- }
- }
- })
-}
-
-// BenchmarkAccountIteratorLargeBaselayer is a pretty realistic benchmark, where
-// 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
-//
-// 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) {
- // Create a custom account factory to recreate the same addresses
- makeAccounts := func(num int) map[common.Hash][]byte {
- 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
- }
- // 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,
- },
- }
- snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"), nil, makeAccounts(2000), nil)
- for i := 2; i <= 100; i++ {
- snaps.Update(common.HexToHash(fmt.Sprintf("0x%02x", i+1)), common.HexToHash(fmt.Sprintf("0x%02x", i)), nil, makeAccounts(20), nil)
- }
- // We call this once before the benchmark, so the creation of
- // sorted accountlists are not included in the results.
- head := snaps.Snapshot(common.HexToHash("0x65"))
- head.(*diffLayer).newBinaryAccountIterator()
-
- b.Run("binary iterator (keys)", func(b *testing.B) {
- for i := 0; i < b.N; i++ {
- got := 0
- it := head.(*diffLayer).newBinaryAccountIterator()
- for it.Next() {
- got++
- }
- if exp := 2000; got != exp {
- b.Errorf("iterator len wrong, expected %d, got %d", exp, got)
- }
- }
- })
- b.Run("binary iterator (values)", func(b *testing.B) {
- for i := 0; i < b.N; i++ {
- got := 0
- it := head.(*diffLayer).newBinaryAccountIterator()
- for it.Next() {
- got++
- v := it.Hash()
- head.(*diffLayer).accountRLP(v, 0)
- }
- if exp := 2000; got != exp {
- b.Errorf("iterator len wrong, expected %d, got %d", exp, got)
- }
- }
- })
- b.Run("fast iterator (keys)", func(b *testing.B) {
- for i := 0; i < b.N; i++ {
- it, _ := snaps.AccountIterator(common.HexToHash("0x65"), common.Hash{})
- defer it.Release()
-
- got := 0
- for it.Next() {
- got++
- }
- if exp := 2000; got != exp {
- b.Errorf("iterator len wrong, expected %d, got %d", exp, got)
- }
- }
- })
- b.Run("fast iterator (values)", func(b *testing.B) {
- for i := 0; i < b.N; i++ {
- it, _ := snaps.AccountIterator(common.HexToHash("0x65"), common.Hash{})
- defer it.Release()
-
- got := 0
- for it.Next() {
- it.Account()
- got++
- }
- if exp := 2000; got != exp {
- b.Errorf("iterator len wrong, expected %d, got %d", exp, got)
- }
- }
- })
-}
-
-/*
-func BenchmarkBinaryAccountIteration(b *testing.B) {
- benchmarkAccountIteration(b, func(snap snapshot) AccountIterator {
- return snap.(*diffLayer).newBinaryAccountIterator()
- })
-}
-
-func BenchmarkFastAccountIteration(b *testing.B) {
- benchmarkAccountIteration(b, newFastAccountIterator)
-}
-
-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)
- }
- for i := 0; i < b.N; i++ {
- depth := rand.Intn(len(layers))
- layers[depth][randomHash()] = randomAccount()
- }
- stack := snapshot(emptyLayer())
- for _, layer := range layers {
- stack = stack.Update(common.Hash{}, layer, nil, nil)
- }
- // Reset the timers and report all the stats
- it := iterator(stack)
-
- b.ResetTimer()
- b.ReportAllocs()
-
- for it.Next() {
- }
-}
-*/
diff --git a/core/state/snapshot/snapshot_test.go b/core/state/snapshot/snapshot_test.go
deleted file mode 100644
index 94e3610..0000000
--- a/core/state/snapshot/snapshot_test.go
+++ /dev/null
@@ -1,371 +0,0 @@
-// 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 (
- "fmt"
- "math/big"
- "math/rand"
- "testing"
-
- "github.com/VictoriaMetrics/fastcache"
- "github.com/ava-labs/coreth/core/rawdb"
- "github.com/ethereum/go-ethereum/common"
- "github.com/ethereum/go-ethereum/rlp"
-)
-
-// randomHash generates a random blob of data and returns it as a hash.
-func randomHash() common.Hash {
- var hash common.Hash
- if n, err := rand.Read(hash[:]); n != common.HashLength || err != nil {
- panic(err)
- }
- return hash
-}
-
-// randomAccount generates a random account and returns it RLP encoded.
-func randomAccount() []byte {
- root := randomHash()
- a := Account{
- Balance: big.NewInt(rand.Int63()),
- Nonce: rand.Uint64(),
- Root: root[:],
- CodeHash: emptyCode[:],
- }
- data, _ := rlp.EncodeToBytes(a)
- return data
-}
-
-// randomAccountSet generates a set of random accounts with the given strings as
-// the account address hashes.
-func randomAccountSet(hashes ...string) map[common.Hash][]byte {
- accounts := make(map[common.Hash][]byte)
- for _, hash := range hashes {
- accounts[common.HexToHash(hash)] = randomAccount()
- }
- return accounts
-}
-
-// randomStorageSet generates a set of random slots with the given strings as
-// the slot addresses.
-func randomStorageSet(accounts []string, hashes [][]string, nilStorage [][]string) map[common.Hash]map[common.Hash][]byte {
- storages := make(map[common.Hash]map[common.Hash][]byte)
- for index, account := range accounts {
- storages[common.HexToHash(account)] = make(map[common.Hash][]byte)
-
- if index < len(hashes) {
- hashes := hashes[index]
- for _, hash := range hashes {
- storages[common.HexToHash(account)][common.HexToHash(hash)] = randomHash().Bytes()
- }
- }
- if index < len(nilStorage) {
- nils := nilStorage[index]
- for _, hash := range nils {
- storages[common.HexToHash(account)][common.HexToHash(hash)] = nil
- }
- }
- }
- return storages
-}
-
-// Tests that if a disk layer becomes stale, no active external references will
-// be returned with junk data. This version of the test flattens every diff layer
-// to check internal corner case around the bottom-most memory accumulator.
-func TestDiskLayerExternalInvalidationFullFlatten(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,
- },
- }
- // Retrieve a reference to the base and commit a diff on top
- ref := snaps.Snapshot(base.root)
-
- accounts := map[common.Hash][]byte{
- common.HexToHash("0xa1"): randomAccount(),
- }
- if err := snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"), nil, accounts, nil); err != nil {
- t.Fatalf("failed to create a diff layer: %v", err)
- }
- if n := len(snaps.layers); n != 2 {
- t.Errorf("pre-cap layer count mismatch: have %d, want %d", n, 2)
- }
- // Commit the diff layer onto the disk and ensure it's persisted
- if err := snaps.Cap(common.HexToHash("0x02"), 0); err != nil {
- t.Fatalf("failed to merge diff layer onto disk: %v", err)
- }
- // Since the base layer was modified, ensure that data retrieval on the external reference fail
- if acc, err := ref.Account(common.HexToHash("0x01")); err != ErrSnapshotStale {
- t.Errorf("stale reference returned account: %#x (err: %v)", acc, err)
- }
- if slot, err := ref.Storage(common.HexToHash("0xa1"), common.HexToHash("0xb1")); err != ErrSnapshotStale {
- t.Errorf("stale reference returned storage slot: %#x (err: %v)", slot, err)
- }
- if n := len(snaps.layers); n != 1 {
- t.Errorf("post-cap layer count mismatch: have %d, want %d", n, 1)
- fmt.Println(snaps.layers)
- }
-}
-
-// Tests that if a disk layer becomes stale, no active external references will
-// be returned with junk data. This version of the test retains the bottom diff
-// layer to check the usual mode of operation where the accumulator is retained.
-func TestDiskLayerExternalInvalidationPartialFlatten(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,
- },
- }
- // Retrieve a reference to the base and commit two diffs on top
- ref := snaps.Snapshot(base.root)
-
- accounts := map[common.Hash][]byte{
- common.HexToHash("0xa1"): randomAccount(),
- }
- if err := snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"), nil, accounts, nil); err != nil {
- t.Fatalf("failed to create a diff layer: %v", err)
- }
- if err := snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"), nil, accounts, nil); err != nil {
- t.Fatalf("failed to create a diff layer: %v", err)
- }
- if n := len(snaps.layers); n != 3 {
- t.Errorf("pre-cap layer count mismatch: have %d, want %d", n, 3)
- }
- // Commit the diff layer onto the disk and ensure it's persisted
- defer func(memcap uint64) { aggregatorMemoryLimit = memcap }(aggregatorMemoryLimit)
- aggregatorMemoryLimit = 0
-
- if err := snaps.Cap(common.HexToHash("0x03"), 2); err != nil {
- t.Fatalf("failed to merge diff layer onto disk: %v", err)
- }
- // Since the base layer was modified, ensure that data retrievald on the external reference fail
- if acc, err := ref.Account(common.HexToHash("0x01")); err != ErrSnapshotStale {
- t.Errorf("stale reference returned account: %#x (err: %v)", acc, err)
- }
- if slot, err := ref.Storage(common.HexToHash("0xa1"), common.HexToHash("0xb1")); err != ErrSnapshotStale {
- t.Errorf("stale reference returned storage slot: %#x (err: %v)", slot, err)
- }
- if n := len(snaps.layers); n != 2 {
- t.Errorf("post-cap layer count mismatch: have %d, want %d", n, 2)
- fmt.Println(snaps.layers)
- }
-}
-
-// Tests that if a diff layer becomes stale, no active external references will
-// be returned with junk data. This version of the test flattens every diff layer
-// to check internal corner case around the bottom-most memory accumulator.
-func TestDiffLayerExternalInvalidationFullFlatten(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,
- },
- }
- // Commit two diffs on top and retrieve a reference to the bottommost
- accounts := map[common.Hash][]byte{
- common.HexToHash("0xa1"): randomAccount(),
- }
- if err := snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"), nil, accounts, nil); err != nil {
- t.Fatalf("failed to create a diff layer: %v", err)
- }
- if err := snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"), nil, accounts, nil); err != nil {
- t.Fatalf("failed to create a diff layer: %v", err)
- }
- if n := len(snaps.layers); n != 3 {
- t.Errorf("pre-cap layer count mismatch: have %d, want %d", n, 3)
- }
- ref := snaps.Snapshot(common.HexToHash("0x02"))
-
- // Flatten the diff layer into the bottom accumulator
- if err := snaps.Cap(common.HexToHash("0x03"), 1); err != nil {
- t.Fatalf("failed to flatten diff layer into accumulator: %v", err)
- }
- // Since the accumulator diff layer was modified, ensure that data retrievald on the external reference fail
- if acc, err := ref.Account(common.HexToHash("0x01")); err != ErrSnapshotStale {
- t.Errorf("stale reference returned account: %#x (err: %v)", acc, err)
- }
- if slot, err := ref.Storage(common.HexToHash("0xa1"), common.HexToHash("0xb1")); err != ErrSnapshotStale {
- t.Errorf("stale reference returned storage slot: %#x (err: %v)", slot, err)
- }
- if n := len(snaps.layers); n != 2 {
- t.Errorf("post-cap layer count mismatch: have %d, want %d", n, 2)
- fmt.Println(snaps.layers)
- }
-}
-
-// Tests that if a diff layer becomes stale, no active external references will
-// be returned with junk data. This version of the test retains the bottom diff
-// layer to check the usual mode of operation where the accumulator is retained.
-func TestDiffLayerExternalInvalidationPartialFlatten(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,
- },
- }
- // Commit three diffs on top and retrieve a reference to the bottommost
- accounts := map[common.Hash][]byte{
- common.HexToHash("0xa1"): randomAccount(),
- }
- if err := snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"), nil, accounts, nil); err != nil {
- t.Fatalf("failed to create a diff layer: %v", err)
- }
- if err := snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"), nil, accounts, nil); err != nil {
- t.Fatalf("failed to create a diff layer: %v", err)
- }
- if err := snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"), nil, accounts, nil); err != nil {
- t.Fatalf("failed to create a diff layer: %v", err)
- }
- if n := len(snaps.layers); n != 4 {
- t.Errorf("pre-cap layer count mismatch: have %d, want %d", n, 4)
- }
- ref := snaps.Snapshot(common.HexToHash("0x02"))
-
- // Doing a Cap operation with many allowed layers should be a no-op
- exp := len(snaps.layers)
- if err := snaps.Cap(common.HexToHash("0x04"), 2000); err != nil {
- t.Fatalf("failed to flatten diff layer into accumulator: %v", err)
- }
- if got := len(snaps.layers); got != exp {
- t.Errorf("layers modified, got %d exp %d", got, exp)
- }
- // Flatten the diff layer into the bottom accumulator
- if err := snaps.Cap(common.HexToHash("0x04"), 2); err != nil {
- t.Fatalf("failed to flatten diff layer into accumulator: %v", err)
- }
- // Since the accumulator diff layer was modified, ensure that data retrievald on the external reference fail
- if acc, err := ref.Account(common.HexToHash("0x01")); err != ErrSnapshotStale {
- t.Errorf("stale reference returned account: %#x (err: %v)", acc, err)
- }
- if slot, err := ref.Storage(common.HexToHash("0xa1"), common.HexToHash("0xb1")); err != ErrSnapshotStale {
- t.Errorf("stale reference returned storage slot: %#x (err: %v)", slot, err)
- }
- if n := len(snaps.layers); n != 3 {
- t.Errorf("post-cap layer count mismatch: have %d, want %d", n, 3)
- fmt.Println(snaps.layers)
- }
-}
-
-// TestPostCapBasicDataAccess tests some functionality regarding capping/flattening.
-func TestPostCapBasicDataAccess(t *testing.T) {
- // setAccount is a helper to construct a random account entry and assign it to
- // an account slot in a snapshot
- setAccount := func(accKey string) map[common.Hash][]byte {
- return map[common.Hash][]byte{
- common.HexToHash(accKey): randomAccount(),
- }
- }
- // Create a starting 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,
- },
- }
- // The lowest difflayer
- snaps.Update(common.HexToHash("0xa1"), common.HexToHash("0x01"), nil, setAccount("0xa1"), nil)
- snaps.Update(common.HexToHash("0xa2"), common.HexToHash("0xa1"), nil, setAccount("0xa2"), nil)
- snaps.Update(common.HexToHash("0xb2"), common.HexToHash("0xa1"), nil, setAccount("0xb2"), nil)
-
- snaps.Update(common.HexToHash("0xa3"), common.HexToHash("0xa2"), nil, setAccount("0xa3"), nil)
- snaps.Update(common.HexToHash("0xb3"), common.HexToHash("0xb2"), nil, setAccount("0xb3"), nil)
-
- // checkExist verifies if an account exiss in a snapshot
- checkExist := func(layer *diffLayer, key string) error {
- if data, _ := layer.Account(common.HexToHash(key)); data == nil {
- return fmt.Errorf("expected %x to exist, got nil", common.HexToHash(key))
- }
- return nil
- }
- // shouldErr checks that an account access errors as expected
- shouldErr := func(layer *diffLayer, key string) error {
- if data, err := layer.Account(common.HexToHash(key)); err == nil {
- return fmt.Errorf("expected error, got data %x", data)
- }
- return nil
- }
- // check basics
- snap := snaps.Snapshot(common.HexToHash("0xb3")).(*diffLayer)
-
- if err := checkExist(snap, "0xa1"); err != nil {
- t.Error(err)
- }
- if err := checkExist(snap, "0xb2"); err != nil {
- t.Error(err)
- }
- if err := checkExist(snap, "0xb3"); err != nil {
- t.Error(err)
- }
- // Cap to a bad root should fail
- if err := snaps.Cap(common.HexToHash("0x1337"), 0); err == nil {
- t.Errorf("expected error, got none")
- }
- // Now, merge the a-chain
- snaps.Cap(common.HexToHash("0xa3"), 0)
-
- // At this point, a2 got merged into a1. Thus, a1 is now modified, and as a1 is
- // the parent of b2, b2 should no longer be able to iterate into parent.
-
- // These should still be accessible
- if err := checkExist(snap, "0xb2"); err != nil {
- t.Error(err)
- }
- if err := checkExist(snap, "0xb3"); err != nil {
- t.Error(err)
- }
- // But these would need iteration into the modified parent
- if err := shouldErr(snap, "0xa1"); err != nil {
- t.Error(err)
- }
- if err := shouldErr(snap, "0xa2"); err != nil {
- t.Error(err)
- }
- if err := shouldErr(snap, "0xa3"); err != nil {
- t.Error(err)
- }
- // Now, merge it again, just for fun. It should now error, since a3
- // is a disk layer
- if err := snaps.Cap(common.HexToHash("0xa3"), 0); err == nil {
- t.Error("expected error capping the disk layer, got none")
- }
-}
diff --git a/core/state/snapshot/wipe_test.go b/core/state/snapshot/wipe_test.go
deleted file mode 100644
index a656982..0000000
--- a/core/state/snapshot/wipe_test.go
+++ /dev/null
@@ -1,124 +0,0 @@
-// 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 (
- "math/rand"
- "testing"
-
- "github.com/ava-labs/coreth/core/rawdb"
- "github.com/ethereum/go-ethereum/common"
- "github.com/ethereum/go-ethereum/ethdb/memorydb"
-)
-
-// Tests that given a database with random data content, all parts of a snapshot
-// can be crrectly wiped without touching anything else.
-func TestWipe(t *testing.T) {
- // Create a database with some random snapshot data
- db := memorydb.New()
-
- for i := 0; i < 128; i++ {
- account := randomHash()
- rawdb.WriteAccountSnapshot(db, account, randomHash().Bytes())
- for j := 0; j < 1024; j++ {
- rawdb.WriteStorageSnapshot(db, account, randomHash(), randomHash().Bytes())
- }
- }
- rawdb.WriteSnapshotRoot(db, randomHash())
-
- // Add some random non-snapshot data too to make wiping harder
- for i := 0; i < 65536; i++ {
- // Generate a key that's the wrong length for a state snapshot item
- var keysize int
- for keysize == 0 || keysize == 32 || keysize == 64 {
- keysize = 8 + rand.Intn(64) // +8 to ensure we will "never" randomize duplicates
- }
- // Randomize the suffix, dedup and inject it under the snapshot namespace
- keysuffix := make([]byte, keysize)
- rand.Read(keysuffix)
-
- if rand.Int31n(2) == 0 {
- db.Put(append(rawdb.SnapshotAccountPrefix, keysuffix...), randomHash().Bytes())
- } else {
- db.Put(append(rawdb.SnapshotStoragePrefix, keysuffix...), randomHash().Bytes())
- }
- }
- // Sanity check that all the keys are present
- var items int
-
- it := db.NewIterator(rawdb.SnapshotAccountPrefix, nil)
- defer it.Release()
-
- for it.Next() {
- key := it.Key()
- if len(key) == len(rawdb.SnapshotAccountPrefix)+common.HashLength {
- items++
- }
- }
- it = db.NewIterator(rawdb.SnapshotStoragePrefix, nil)
- defer it.Release()
-
- for it.Next() {
- key := it.Key()
- if len(key) == len(rawdb.SnapshotStoragePrefix)+2*common.HashLength {
- items++
- }
- }
- if items != 128+128*1024 {
- t.Fatalf("snapshot size mismatch: have %d, want %d", items, 128+128*1024)
- }
- if hash := rawdb.ReadSnapshotRoot(db); hash == (common.Hash{}) {
- t.Errorf("snapshot block marker mismatch: have %#x, want <not-nil>", hash)
- }
- // Wipe all snapshot entries from the database
- <-wipeSnapshot(db, true)
-
- // Iterate over the database end ensure no snapshot information remains
- it = db.NewIterator(rawdb.SnapshotAccountPrefix, nil)
- defer it.Release()
-
- for it.Next() {
- key := it.Key()
- if len(key) == len(rawdb.SnapshotAccountPrefix)+common.HashLength {
- t.Errorf("snapshot entry remained after wipe: %x", key)
- }
- }
- it = db.NewIterator(rawdb.SnapshotStoragePrefix, nil)
- defer it.Release()
-
- for it.Next() {
- key := it.Key()
- if len(key) == len(rawdb.SnapshotStoragePrefix)+2*common.HashLength {
- t.Errorf("snapshot entry remained after wipe: %x", key)
- }
- }
- if hash := rawdb.ReadSnapshotRoot(db); hash != (common.Hash{}) {
- t.Errorf("snapshot block marker remained after wipe: %#x", hash)
- }
- // Iterate over the database and ensure miscellaneous items are present
- items = 0
-
- it = db.NewIterator(nil, nil)
- defer it.Release()
-
- for it.Next() {
- items++
- }
- if items != 65536 {
- t.Fatalf("misc item count mismatch: have %d, want %d", items, 65536)
- }
-}