Skip to content

Commit 2133f18

Browse files
zcheng9holiman
authored andcommitted
core/state: fix database leak and copy tests (#19306)
1 parent 1a6ef5a commit 2133f18

File tree

1 file changed

+59
-19
lines changed

1 file changed

+59
-19
lines changed

core/state/statedb_test.go

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ import (
2525
"math/rand"
2626
"reflect"
2727
"strings"
28+
"sync"
2829
"testing"
2930
"testing/quick"
3031

31-
check "gopkg.in/check.v1"
32+
"gopkg.in/check.v1"
3233

3334
"github.com/ethereum/go-ethereum/common"
3435
"github.com/ethereum/go-ethereum/core/rawdb"
@@ -53,8 +54,13 @@ func TestUpdateLeaks(t *testing.T) {
5354
if i%3 == 0 {
5455
state.SetCode(addr, []byte{i, i, i, i, i})
5556
}
56-
state.IntermediateRoot(false)
5757
}
58+
59+
root := state.IntermediateRoot(false)
60+
if err := state.Database().TrieDB().Commit(root, false); err != nil {
61+
t.Errorf("can not commit trie %v to persistent database", root.Hex())
62+
}
63+
5864
// Ensure that no data was leaked into the database
5965
it := db.NewIterator()
6066
for it.Next() {
@@ -98,27 +104,45 @@ func TestIntermediateLeaks(t *testing.T) {
98104
}
99105

100106
// Commit and cross check the databases.
101-
if _, err := transState.Commit(false); err != nil {
107+
transRoot, err := transState.Commit(false)
108+
if err != nil {
102109
t.Fatalf("failed to commit transition state: %v", err)
103110
}
104-
if _, err := finalState.Commit(false); err != nil {
111+
if err = transState.Database().TrieDB().Commit(transRoot, false); err != nil {
112+
t.Errorf("can not commit trie %v to persistent database", transRoot.Hex())
113+
}
114+
115+
finalRoot, err := finalState.Commit(false)
116+
if err != nil {
105117
t.Fatalf("failed to commit final state: %v", err)
106118
}
119+
if err = finalState.Database().TrieDB().Commit(finalRoot, false); err != nil {
120+
t.Errorf("can not commit trie %v to persistent database", finalRoot.Hex())
121+
}
122+
107123
it := finalDb.NewIterator()
108124
for it.Next() {
109-
key := it.Key()
110-
if _, err := transDb.Get(key); err != nil {
111-
t.Errorf("entry missing from the transition database: %x -> %x", key, it.Value())
125+
key, fvalue := it.Key(), it.Value()
126+
tvalue, err := transDb.Get(key)
127+
if err != nil {
128+
t.Errorf("entry missing from the transition database: %x -> %x", key, fvalue)
129+
}
130+
if !bytes.Equal(fvalue, tvalue) {
131+
t.Errorf("the value associate key %x is mismatch,: %x in transition database ,%x in final database", key, tvalue, fvalue)
112132
}
113133
}
114134
it.Release()
115135

116136
it = transDb.NewIterator()
117137
for it.Next() {
118-
key := it.Key()
119-
if _, err := finalDb.Get(key); err != nil {
138+
key, tvalue := it.Key(), it.Value()
139+
fvalue, err := finalDb.Get(key)
140+
if err != nil {
120141
t.Errorf("extra entry in the transition database: %x -> %x", key, it.Value())
121142
}
143+
if !bytes.Equal(fvalue, tvalue) {
144+
t.Errorf("the value associate key %x is mismatch,: %x in transition database ,%x in final database", key, tvalue, fvalue)
145+
}
122146
}
123147
}
124148

@@ -136,39 +160,55 @@ func TestCopy(t *testing.T) {
136160
}
137161
orig.Finalise(false)
138162

139-
// Copy the state, modify both in-memory
163+
// Copy the state
140164
copy := orig.Copy()
141165

166+
// Copy the copy state
167+
ccopy := copy.Copy()
168+
169+
// modify all in memory
142170
for i := byte(0); i < 255; i++ {
143171
origObj := orig.GetOrNewStateObject(common.BytesToAddress([]byte{i}))
144172
copyObj := copy.GetOrNewStateObject(common.BytesToAddress([]byte{i}))
173+
ccopyObj := ccopy.GetOrNewStateObject(common.BytesToAddress([]byte{i}))
145174

146175
origObj.AddBalance(big.NewInt(2 * int64(i)))
147176
copyObj.AddBalance(big.NewInt(3 * int64(i)))
177+
ccopyObj.AddBalance(big.NewInt(4 * int64(i)))
148178

149179
orig.updateStateObject(origObj)
150180
copy.updateStateObject(copyObj)
181+
ccopy.updateStateObject(copyObj)
151182
}
152-
// Finalise the changes on both concurrently
153-
done := make(chan struct{})
154-
go func() {
155-
orig.Finalise(true)
156-
close(done)
157-
}()
158-
copy.Finalise(true)
159-
<-done
160183

161-
// Verify that the two states have been updated independently
184+
// Finalise the changes on all concurrently
185+
finalise := func(wg *sync.WaitGroup, db *StateDB) {
186+
defer wg.Done()
187+
db.Finalise(true)
188+
}
189+
190+
var wg sync.WaitGroup
191+
wg.Add(3)
192+
go finalise(&wg, orig)
193+
go finalise(&wg, copy)
194+
go finalise(&wg, ccopy)
195+
wg.Wait()
196+
197+
// Verify that the three states have been updated independently
162198
for i := byte(0); i < 255; i++ {
163199
origObj := orig.GetOrNewStateObject(common.BytesToAddress([]byte{i}))
164200
copyObj := copy.GetOrNewStateObject(common.BytesToAddress([]byte{i}))
201+
ccopyObj := ccopy.GetOrNewStateObject(common.BytesToAddress([]byte{i}))
165202

166203
if want := big.NewInt(3 * int64(i)); origObj.Balance().Cmp(want) != 0 {
167204
t.Errorf("orig obj %d: balance mismatch: have %v, want %v", i, origObj.Balance(), want)
168205
}
169206
if want := big.NewInt(4 * int64(i)); copyObj.Balance().Cmp(want) != 0 {
170207
t.Errorf("copy obj %d: balance mismatch: have %v, want %v", i, copyObj.Balance(), want)
171208
}
209+
if want := big.NewInt(5 * int64(i)); ccopyObj.Balance().Cmp(want) != 0 {
210+
t.Errorf("copy obj %d: balance mismatch: have %v, want %v", i, ccopyObj.Balance(), want)
211+
}
172212
}
173213
}
174214

0 commit comments

Comments
 (0)