Skip to content

Commit fab0ee3

Browse files
committed
core/state/snapshot: fix various iteration issues due to destruct set
1 parent bc5d742 commit fab0ee3

4 files changed

Lines changed: 67 additions & 39 deletions

File tree

‎core/state/snapshot/difflayer.go‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -493,12 +493,14 @@ func (dl *diffLayer) AccountList() []common.Hash {
493493
defer dl.lock.Unlock()
494494

495495
dl.accountList = make([]common.Hash, 0, len(dl.destructSet)+len(dl.accountData))
496-
for hash := range dl.destructSet {
497-
dl.accountList = append(dl.accountList, hash)
498-
}
499496
for hash := range dl.accountData {
500497
dl.accountList = append(dl.accountList, hash)
501498
}
499+
for hash := range dl.destructSet {
500+
if _, ok := dl.accountData[hash]; !ok {
501+
dl.accountList = append(dl.accountList, hash)
502+
}
503+
}
502504
sort.Sort(hashes(dl.accountList))
503505
return dl.accountList
504506
}

‎core/state/snapshot/difflayer_test.go‎

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,33 @@ import (
2727
"github.com/ethereum/go-ethereum/ethdb/memorydb"
2828
)
2929

30+
func copyDestructs(destructs map[common.Hash]struct{}) map[common.Hash]struct{} {
31+
copy := make(map[common.Hash]struct{})
32+
for hash := range destructs {
33+
copy[hash] = struct{}{}
34+
}
35+
return copy
36+
}
37+
38+
func copyAccounts(accounts map[common.Hash][]byte) map[common.Hash][]byte {
39+
copy := make(map[common.Hash][]byte)
40+
for hash, blob := range accounts {
41+
copy[hash] = blob
42+
}
43+
return copy
44+
}
45+
46+
func copyStorage(storage map[common.Hash]map[common.Hash][]byte) map[common.Hash]map[common.Hash][]byte {
47+
copy := make(map[common.Hash]map[common.Hash][]byte)
48+
for accHash, slots := range storage {
49+
copy[accHash] = make(map[common.Hash][]byte)
50+
for slotHash, blob := range slots {
51+
copy[accHash][slotHash] = blob
52+
}
53+
}
54+
return copy
55+
}
56+
3057
// TestMergeBasics tests some simple merges
3158
func TestMergeBasics(t *testing.T) {
3259
var (
@@ -52,41 +79,41 @@ func TestMergeBasics(t *testing.T) {
5279
}
5380
}
5481
// Add some (identical) layers on top
55-
parent := newDiffLayer(emptyLayer(), common.Hash{}, destructs, accounts, storage)
56-
child := newDiffLayer(parent, common.Hash{}, destructs, accounts, storage)
57-
child = newDiffLayer(child, common.Hash{}, destructs, accounts, storage)
58-
child = newDiffLayer(child, common.Hash{}, destructs, accounts, storage)
59-
child = newDiffLayer(child, common.Hash{}, destructs, accounts, storage)
82+
parent := newDiffLayer(emptyLayer(), common.Hash{}, copyDestructs(destructs), copyAccounts(accounts), copyStorage(storage))
83+
child := newDiffLayer(parent, common.Hash{}, copyDestructs(destructs), copyAccounts(accounts), copyStorage(storage))
84+
child = newDiffLayer(child, common.Hash{}, copyDestructs(destructs), copyAccounts(accounts), copyStorage(storage))
85+
child = newDiffLayer(child, common.Hash{}, copyDestructs(destructs), copyAccounts(accounts), copyStorage(storage))
86+
child = newDiffLayer(child, common.Hash{}, copyDestructs(destructs), copyAccounts(accounts), copyStorage(storage))
6087
// And flatten
6188
merged := (child.flatten()).(*diffLayer)
6289

6390
{ // Check account lists
64-
if got, exp := len(merged.accountList), 0; got != exp {
65-
t.Errorf("accountList wrong, got %v exp %v", got, exp)
91+
if have, want := len(merged.accountList), 0; have != want {
92+
t.Errorf("accountList wrong: have %v, want %v", have, want)
6693
}
67-
if got, exp := len(merged.AccountList()), len(accounts); got != exp {
68-
t.Errorf("AccountList() wrong, got %v exp %v", got, exp)
94+
if have, want := len(merged.AccountList()), len(accounts); have != want {
95+
t.Errorf("AccountList() wrong: have %v, want %v", have, want)
6996
}
70-
if got, exp := len(merged.accountList), len(accounts); got != exp {
71-
t.Errorf("accountList [2] wrong, got %v exp %v", got, exp)
97+
if have, want := len(merged.accountList), len(accounts); have != want {
98+
t.Errorf("accountList [2] wrong: have %v, want %v", have, want)
7299
}
73100
}
74101
{ // Check account drops
75-
if got, exp := len(merged.destructSet), len(destructs); got != exp {
76-
t.Errorf("accountDrop wrong, got %v exp %v", got, exp)
102+
if have, want := len(merged.destructSet), len(destructs); have != want {
103+
t.Errorf("accountDrop wrong: have %v, want %v", have, want)
77104
}
78105
}
79106
{ // Check storage lists
80107
i := 0
81108
for aHash, sMap := range storage {
82-
if got, exp := len(merged.storageList), i; got != exp {
83-
t.Errorf("[1] storageList wrong, got %v exp %v", got, exp)
109+
if have, want := len(merged.storageList), i; have != want {
110+
t.Errorf("[1] storageList wrong: have %v, want %v", have, want)
84111
}
85-
if got, exp := len(merged.StorageList(aHash)), len(sMap); got != exp {
86-
t.Errorf("[2] StorageList() wrong, got %v exp %v", got, exp)
112+
if have, want := len(merged.StorageList(aHash)), len(sMap); have != want {
113+
t.Errorf("[2] StorageList() wrong: have %v, want %v", have, want)
87114
}
88-
if got, exp := len(merged.storageList[aHash]), len(sMap); got != exp {
89-
t.Errorf("storageList wrong, got %v exp %v", got, exp)
115+
if have, want := len(merged.storageList[aHash]), len(sMap); have != want {
116+
t.Errorf("storageList wrong: have %v, want %v", have, want)
90117
}
91118
i++
92119
}
@@ -160,8 +187,8 @@ func TestMergeDelete(t *testing.T) {
160187
}
161188
// If we add more granular metering of memory, we can enable this again,
162189
// but it's not implemented for now
163-
//if got, exp := merged.memory, child.memory; got != exp {
164-
// t.Errorf("mem wrong, got %d, exp %d", got, exp)
190+
//if have, want := merged.memory, child.memory; have != want {
191+
// t.Errorf("mem wrong: have %d, want %d", have, want)
165192
//}
166193
}
167194

@@ -197,9 +224,9 @@ func TestInsertAndMerge(t *testing.T) {
197224
// And flatten
198225
merged := (child.flatten()).(*diffLayer)
199226
{ // Check that slot value is present
200-
got, _ := merged.Storage(acc, slot)
201-
if exp := []byte{0x01}; !bytes.Equal(got, exp) {
202-
t.Errorf("merged slot value wrong, got %x, exp %x", got, exp)
227+
have, _ := merged.Storage(acc, slot)
228+
if want := []byte{0x01}; !bytes.Equal(have, want) {
229+
t.Errorf("merged slot value wrong: have %x, want %x", have, want)
203230
}
204231
}
205232
}

‎core/state/snapshot/iterator.go‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,6 @@ type diffAccountIterator struct {
6060
// hash as long as the iterator is not touched any more.
6161
curHash common.Hash
6262

63-
// curAccount is the current value the iterator is positioned on. The field
64-
// is explicitly tracked since the referenced diff layer might go stale after
65-
// the iterator was positioned and we don't want to fail accessing the old
66-
// value as long as the iterator is not touched any more.
67-
//curAccount []byte
68-
6963
layer *diffLayer // Live layer to retrieve values from
7064
keys []common.Hash // Keys left in the layer to iterate
7165
fail error // Any failures encountered (stale)
@@ -130,6 +124,9 @@ func (it *diffAccountIterator) Account() []byte {
130124
it.layer.lock.RLock()
131125
blob, ok := it.layer.accountData[it.curHash]
132126
if !ok {
127+
if _, ok := it.layer.destructSet[it.curHash]; ok {
128+
return nil
129+
}
133130
panic(fmt.Sprintf("iterator referenced non-existent account: %x", it.curHash))
134131
}
135132
it.layer.lock.RUnlock()

‎core/state/snapshot/iterator_test.go‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func TestAccountIteratorBasics(t *testing.T) {
5353
}
5454
}
5555
// Add some (identical) layers on top
56-
parent := newDiffLayer(emptyLayer(), common.Hash{}, destructs, accounts, storage)
56+
parent := newDiffLayer(emptyLayer(), common.Hash{}, copyDestructs(destructs), copyAccounts(accounts), copyStorage(storage))
5757
it := parent.AccountIterator(common.Hash{})
5858
verifyIterator(t, 100, it)
5959
}
@@ -398,15 +398,17 @@ func TestIteratorDeletions(t *testing.T) {
398398
}
399399
// Stack three diff layers on top with various overlaps
400400
snaps.Update(common.HexToHash("0x02"), common.HexToHash("0x01"),
401-
randomAccountSet("0x11", "0x22", "0x33"), nil)
401+
nil, randomAccountSet("0x11", "0x22", "0x33"), nil)
402402

403-
set := randomAccountSet("0x11", "0x22", "0x33")
404403
deleted := common.HexToHash("0x22")
405-
set[deleted] = nil
406-
snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"), set, nil)
404+
destructed := map[common.Hash]struct{}{
405+
deleted: struct{}{},
406+
}
407+
snaps.Update(common.HexToHash("0x03"), common.HexToHash("0x02"),
408+
destructed, randomAccountSet("0x11", "0x33"), nil)
407409

408410
snaps.Update(common.HexToHash("0x04"), common.HexToHash("0x03"),
409-
randomAccountSet("0x33", "0x44", "0x55"), nil)
411+
nil, randomAccountSet("0x33", "0x44", "0x55"), nil)
410412

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

0 commit comments

Comments
 (0)