Skip to content

Commit e72b07c

Browse files
committed
core/state: separate hashes and committer, collapse on commit (ethereum#20481)
1 parent a36dcc7 commit e72b07c

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

core/state/state_object.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,13 @@ func (s *stateObject) finalise() {
291291
}
292292

293293
// updateTrie writes cached storage modifications into the object's storage trie.
294+
// It will return nil if the trie has not been loaded and no changes have been made
294295
func (s *stateObject) updateTrie(db Database) Trie {
295296
// Make sure all dirty slots are finalized into the pending storage area
296297
s.finalise()
297-
298+
if len(s.pendingStorage) == 0 {
299+
return s.trie
300+
}
298301
// Track the amount of time wasted on updating the storage trie
299302
defer func(start time.Time) { s.db.StorageUpdates += time.Since(start) }(time.Now())
300303
// Insert all the pending updates into the trie
@@ -322,8 +325,10 @@ func (s *stateObject) updateTrie(db Database) Trie {
322325

323326
// UpdateRoot sets the trie root to the current root hash of
324327
func (s *stateObject) updateRoot(db Database) {
325-
s.updateTrie(db)
326-
328+
// If nothing changed, don't bother with hashing anything
329+
if s.updateTrie(db) == nil {
330+
return
331+
}
327332
// Track the amount of time wasted on hashing the storage trie
328333
defer func(start time.Time) { s.db.StorageHashes += time.Since(start) }(time.Now())
329334

@@ -333,7 +338,10 @@ func (s *stateObject) updateRoot(db Database) {
333338
// CommitTrie the storage trie of the object to dwb.
334339
// This updates the trie root.
335340
func (s *stateObject) CommitTrie(db Database) error {
336-
s.updateTrie(db)
341+
// If nothing changed, don't bother with hashing anything
342+
if s.updateTrie(db) == nil {
343+
return nil
344+
}
337345
if s.dbErr != nil {
338346
return s.dbErr
339347
}

core/state/statedb.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,8 @@ func (s *StateDB) StorageTrie(addr common.Address) Trie {
342342
return nil
343343
}
344344
cpy := stateObject.deepCopy(s, nil)
345-
return cpy.updateTrie(s.db)
345+
cpy.updateTrie(s.db)
346+
return cpy.getTrie(s.db)
346347
}
347348

348349
func (s *StateDB) HasSelfDestructed(addr common.Address) bool {

0 commit comments

Comments
 (0)