Skip to content

Commit ddadf40

Browse files
fjlkaralabe
authored andcommitted
[release/1.4.14] core, trie: replace state caches with trie journal
(cherry picked from commit cd791bd)
1 parent 7c17a67 commit ddadf40

File tree

15 files changed

+419
-654
lines changed

15 files changed

+419
-654
lines changed

build/update-license.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ var (
4949
// don't relicense vendored sources
5050
"crypto/sha3/", "crypto/ecies/", "logger/glog/",
5151
"crypto/secp256k1/curve.go",
52-
"trie/arc.go",
5352
}
5453

5554
// paths with this prefix are licensed as GPL. all other files are LGPL.

core/state/iterator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (it *NodeIterator) step() error {
7676
}
7777
// Initialize the iterator if we've just started
7878
if it.stateIt == nil {
79-
it.stateIt = trie.NewNodeIterator(it.state.trie.Trie)
79+
it.stateIt = it.state.trie.NodeIterator()
8080
}
8181
// If we had data nodes previously, we surely have at least state nodes
8282
if it.dataIt != nil {

core/state/state_object.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,6 @@ type Account struct {
9595
Balance *big.Int
9696
Root common.Hash // merkle root of the storage trie
9797
CodeHash []byte
98-
99-
codeSize *int
10098
}
10199

102100
// NewObject creates a state object.
@@ -275,20 +273,9 @@ func (self *StateObject) Code(db trie.Database) []byte {
275273
return code
276274
}
277275

278-
// CodeSize returns the size of the contract code associated with this object.
279-
func (self *StateObject) CodeSize(db trie.Database) int {
280-
if self.data.codeSize == nil {
281-
self.data.codeSize = new(int)
282-
*self.data.codeSize = len(self.Code(db))
283-
}
284-
return *self.data.codeSize
285-
}
286-
287276
func (self *StateObject) SetCode(code []byte) {
288277
self.code = code
289278
self.data.CodeHash = crypto.Keccak256(code)
290-
self.data.codeSize = new(int)
291-
*self.data.codeSize = len(code)
292279
self.dirtyCode = true
293280
if self.onDirty != nil {
294281
self.onDirty(self.Address())

core/state/statedb.go

Lines changed: 68 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,32 @@ import (
2828
"github.com/ethereum/go-ethereum/logger/glog"
2929
"github.com/ethereum/go-ethereum/rlp"
3030
"github.com/ethereum/go-ethereum/trie"
31+
lru "github.com/hashicorp/golang-lru"
3132
)
3233

3334
// The starting nonce determines the default nonce when new accounts are being
3435
// created.
3536
var StartingNonce uint64
3637

38+
const (
39+
// Number of past tries to keep. The arbitrarily chosen value here
40+
// is max uncle depth + 1.
41+
maxJournalLength = 8
42+
43+
// Number of codehash->size associations to keep.
44+
codeSizeCacheSize = 100000
45+
)
46+
3747
// StateDBs within the ethereum protocol are used to store anything
3848
// within the merkle trie. StateDBs take care of caching and storing
3949
// nested states. It's the general query interface to retrieve:
4050
// * Contracts
4151
// * Accounts
4252
type StateDB struct {
43-
db ethdb.Database
44-
trie *trie.SecureTrie
45-
46-
// This map caches canon state accounts.
47-
all map[common.Address]Account
53+
db ethdb.Database
54+
trie *trie.SecureTrie
55+
pastTries []*trie.SecureTrie
56+
codeSizeCache *lru.Cache
4857

4958
// This map holds 'live' objects, which will get modified while processing a state transition.
5059
stateObjects map[common.Address]*StateObject
@@ -65,10 +74,11 @@ func New(root common.Hash, db ethdb.Database) (*StateDB, error) {
6574
if err != nil {
6675
return nil, err
6776
}
77+
csc, _ := lru.New(codeSizeCacheSize)
6878
return &StateDB{
6979
db: db,
7080
trie: tr,
71-
all: make(map[common.Address]Account),
81+
codeSizeCache: csc,
7282
stateObjects: make(map[common.Address]*StateObject),
7383
stateObjectsDirty: make(map[common.Address]struct{}),
7484
refund: new(big.Int),
@@ -79,19 +89,15 @@ func New(root common.Hash, db ethdb.Database) (*StateDB, error) {
7989
// Reset clears out all emphemeral state objects from the state db, but keeps
8090
// the underlying state trie to avoid reloading data for the next operations.
8191
func (self *StateDB) Reset(root common.Hash) error {
82-
tr, err := trie.NewSecure(root, self.db)
92+
tr, err := self.openTrie(root)
8393
if err != nil {
8494
return err
8595
}
86-
all := self.all
87-
if self.trie.Hash() != root {
88-
// The root has changed, invalidate canon state.
89-
all = make(map[common.Address]Account)
90-
}
9196
*self = StateDB{
9297
db: self.db,
9398
trie: tr,
94-
all: all,
99+
pastTries: self.pastTries,
100+
codeSizeCache: self.codeSizeCache,
95101
stateObjects: make(map[common.Address]*StateObject),
96102
stateObjectsDirty: make(map[common.Address]struct{}),
97103
refund: new(big.Int),
@@ -100,6 +106,30 @@ func (self *StateDB) Reset(root common.Hash) error {
100106
return nil
101107
}
102108

109+
// openTrie creates a trie. It uses an existing trie if one is available
110+
// from the journal if available.
111+
func (self *StateDB) openTrie(root common.Hash) (*trie.SecureTrie, error) {
112+
if self.trie != nil && self.trie.Hash() == root {
113+
return self.trie, nil
114+
}
115+
for i := len(self.pastTries) - 1; i >= 0; i-- {
116+
if self.pastTries[i].Hash() == root {
117+
tr := *self.pastTries[i]
118+
return &tr, nil
119+
}
120+
}
121+
return trie.NewSecure(root, self.db)
122+
}
123+
124+
func (self *StateDB) pushTrie(t *trie.SecureTrie) {
125+
if len(self.pastTries) >= maxJournalLength {
126+
copy(self.pastTries, self.pastTries[1:])
127+
self.pastTries[len(self.pastTries)-1] = t
128+
} else {
129+
self.pastTries = append(self.pastTries, t)
130+
}
131+
}
132+
103133
func (self *StateDB) StartRecord(thash, bhash common.Hash, ti int) {
104134
self.thash = thash
105135
self.bhash = bhash
@@ -165,17 +195,28 @@ func (self *StateDB) GetNonce(addr common.Address) uint64 {
165195
func (self *StateDB) GetCode(addr common.Address) []byte {
166196
stateObject := self.GetStateObject(addr)
167197
if stateObject != nil {
168-
return stateObject.Code(self.db)
198+
code := stateObject.Code(self.db)
199+
key := common.BytesToHash(stateObject.CodeHash())
200+
self.codeSizeCache.Add(key, len(code))
201+
return code
169202
}
170203
return nil
171204
}
172205

173206
func (self *StateDB) GetCodeSize(addr common.Address) int {
174207
stateObject := self.GetStateObject(addr)
175-
if stateObject != nil {
176-
return stateObject.CodeSize(self.db)
208+
if stateObject == nil {
209+
return 0
177210
}
178-
return 0
211+
key := common.BytesToHash(stateObject.CodeHash())
212+
if cached, ok := self.codeSizeCache.Get(key); ok {
213+
return cached.(int)
214+
}
215+
size := len(stateObject.Code(self.db))
216+
if stateObject.dbErr == nil {
217+
self.codeSizeCache.Add(key, size)
218+
}
219+
return size
179220
}
180221

181222
func (self *StateDB) GetState(a common.Address, b common.Hash) common.Hash {
@@ -269,13 +310,6 @@ func (self *StateDB) GetStateObject(addr common.Address) (stateObject *StateObje
269310
return obj
270311
}
271312

272-
// Use cached account data from the canon state if possible.
273-
if data, ok := self.all[addr]; ok {
274-
obj := NewObject(addr, data, self.MarkStateObjectDirty)
275-
self.SetStateObject(obj)
276-
return obj
277-
}
278-
279313
// Load the object from the database.
280314
enc := self.trie.Get(addr[:])
281315
if len(enc) == 0 {
@@ -286,10 +320,6 @@ func (self *StateDB) GetStateObject(addr common.Address) (stateObject *StateObje
286320
glog.Errorf("can't decode object at %x: %v", addr[:], err)
287321
return nil
288322
}
289-
// Update the all cache. Content in DB always corresponds
290-
// to the current head state so this is ok to do here.
291-
// The object we just loaded has no storage trie and code yet.
292-
self.all[addr] = data
293323
// Insert into the live set.
294324
obj := NewObject(addr, data, self.MarkStateObjectDirty)
295325
self.SetStateObject(obj)
@@ -355,7 +385,8 @@ func (self *StateDB) Copy() *StateDB {
355385
state := &StateDB{
356386
db: self.db,
357387
trie: self.trie,
358-
all: self.all,
388+
pastTries: self.pastTries,
389+
codeSizeCache: self.codeSizeCache,
359390
stateObjects: make(map[common.Address]*StateObject, len(self.stateObjectsDirty)),
360391
stateObjectsDirty: make(map[common.Address]struct{}, len(self.stateObjectsDirty)),
361392
refund: new(big.Int).Set(self.refund),
@@ -375,11 +406,12 @@ func (self *StateDB) Copy() *StateDB {
375406
}
376407

377408
func (self *StateDB) Set(state *StateDB) {
409+
self.db = state.db
378410
self.trie = state.trie
411+
self.pastTries = state.pastTries
379412
self.stateObjects = state.stateObjects
380413
self.stateObjectsDirty = state.stateObjectsDirty
381-
self.all = state.all
382-
414+
self.codeSizeCache = state.codeSizeCache
383415
self.refund = state.refund
384416
self.logs = state.logs
385417
self.logSize = state.logSize
@@ -444,20 +476,13 @@ func (s *StateDB) CommitBatch() (root common.Hash, batch ethdb.Batch) {
444476

445477
func (s *StateDB) commit(dbw trie.DatabaseWriter) (root common.Hash, err error) {
446478
s.refund = new(big.Int)
447-
defer func() {
448-
if err != nil {
449-
// Committing failed, any updates to the canon state are invalid.
450-
s.all = make(map[common.Address]Account)
451-
}
452-
}()
453479

454480
// Commit objects to the trie.
455481
for addr, stateObject := range s.stateObjects {
456482
if stateObject.remove {
457483
// If the object has been removed, don't bother syncing it
458484
// and just mark it for deletion in the trie.
459485
s.DeleteStateObject(stateObject)
460-
delete(s.all, addr)
461486
} else if _, ok := s.stateObjectsDirty[addr]; ok {
462487
// Write any contract code associated with the state object
463488
if stateObject.code != nil && stateObject.dirtyCode {
@@ -472,12 +497,15 @@ func (s *StateDB) commit(dbw trie.DatabaseWriter) (root common.Hash, err error)
472497
}
473498
// Update the object in the main account trie.
474499
s.UpdateStateObject(stateObject)
475-
s.all[addr] = stateObject.data
476500
}
477501
delete(s.stateObjectsDirty, addr)
478502
}
479503
// Write trie changes.
480-
return s.trie.CommitTo(dbw)
504+
root, err = s.trie.CommitTo(dbw)
505+
if err == nil {
506+
s.pushTrie(s.trie)
507+
}
508+
return root, err
481509
}
482510

483511
func (self *StateDB) Refunds() *big.Int {

core/state/sync_test.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,13 @@ func makeTestState() (ethdb.Database, common.Hash, []*testAccount) {
6262
}
6363
root, _ := state.Commit()
6464

65-
// Remove any potentially cached data from the test state creation
66-
trie.ClearGlobalCache()
67-
6865
// Return the generated state
6966
return db, root, accounts
7067
}
7168

7269
// checkStateAccounts cross references a reconstructed state with an expected
7370
// account array.
7471
func checkStateAccounts(t *testing.T, db ethdb.Database, root common.Hash, accounts []*testAccount) {
75-
// Remove any potentially cached data from the state synchronisation
76-
trie.ClearGlobalCache()
77-
7872
// Check root availability and state contents
7973
state, err := New(root, db)
8074
if err != nil {
@@ -98,9 +92,6 @@ func checkStateAccounts(t *testing.T, db ethdb.Database, root common.Hash, accou
9892

9993
// checkStateConsistency checks that all nodes in a state trie are indeed present.
10094
func checkStateConsistency(db ethdb.Database, root common.Hash) error {
101-
// Remove any potentially cached data from the test state creation or previous checks
102-
trie.ClearGlobalCache()
103-
10495
// Create and iterate a state trie rooted in a sub-node
10596
if _, err := db.Get(root.Bytes()); err != nil {
10697
return nil // Consider a non existent state consistent

light/state_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ func (odr *testOdr) Retrieve(ctx context.Context, req OdrRequest) error {
4242
case *TrieRequest:
4343
t, _ := trie.New(req.root, odr.sdb)
4444
req.proof = t.Prove(req.key)
45-
trie.ClearGlobalCache()
4645
case *NodeDataRequest:
4746
req.data, _ = odr.sdb.Get(req.hash[:])
4847
}
@@ -75,7 +74,6 @@ func TestLightStateOdr(t *testing.T) {
7574
odr := &testOdr{sdb: sdb, ldb: ldb}
7675
ls := NewLightState(root, odr)
7776
ctx := context.Background()
78-
trie.ClearGlobalCache()
7977

8078
for i := byte(0); i < 100; i++ {
8179
addr := common.Address{i}
@@ -160,7 +158,6 @@ func TestLightStateSetCopy(t *testing.T) {
160158
odr := &testOdr{sdb: sdb, ldb: ldb}
161159
ls := NewLightState(root, odr)
162160
ctx := context.Background()
163-
trie.ClearGlobalCache()
164161

165162
for i := byte(0); i < 100; i++ {
166163
addr := common.Address{i}
@@ -237,7 +234,6 @@ func TestLightStateDelete(t *testing.T) {
237234
odr := &testOdr{sdb: sdb, ldb: ldb}
238235
ls := NewLightState(root, odr)
239236
ctx := context.Background()
240-
trie.ClearGlobalCache()
241237

242238
addr := common.Address{42}
243239

0 commit comments

Comments
 (0)