From ff7bcf8d159674ccddc25661a8cf6d7b54d0ab79 Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Thu, 27 Mar 2025 11:12:49 +0800 Subject: [PATCH] core/state: separate hashes and committer, collapse on commit (#20481) --- core/state/state_object.go | 16 ++++++++++++---- core/state/statedb.go | 3 ++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/core/state/state_object.go b/core/state/state_object.go index 5c7dab8b4c..f24702e4c2 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -291,10 +291,13 @@ func (s *stateObject) finalise() { } // updateTrie writes cached storage modifications into the object's storage trie. +// It will return nil if the trie has not been loaded and no changes have been made func (s *stateObject) updateTrie(db Database) Trie { // Make sure all dirty slots are finalized into the pending storage area s.finalise() - + if len(s.pendingStorage) == 0 { + return s.trie + } // Track the amount of time wasted on updating the storage trie defer func(start time.Time) { s.db.StorageUpdates += time.Since(start) }(time.Now()) // Insert all the pending updates into the trie @@ -322,8 +325,10 @@ func (s *stateObject) updateTrie(db Database) Trie { // UpdateRoot sets the trie root to the current root hash of func (s *stateObject) updateRoot(db Database) { - s.updateTrie(db) - + // If nothing changed, don't bother with hashing anything + if s.updateTrie(db) == nil { + return + } // Track the amount of time wasted on hashing the storage trie defer func(start time.Time) { s.db.StorageHashes += time.Since(start) }(time.Now()) @@ -333,7 +338,10 @@ func (s *stateObject) updateRoot(db Database) { // CommitTrie the storage trie of the object to dwb. // This updates the trie root. func (s *stateObject) CommitTrie(db Database) error { - s.updateTrie(db) + // If nothing changed, don't bother with hashing anything + if s.updateTrie(db) == nil { + return nil + } if s.dbErr != nil { return s.dbErr } diff --git a/core/state/statedb.go b/core/state/statedb.go index a2781d8776..8661982cb3 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -342,7 +342,8 @@ func (s *StateDB) StorageTrie(addr common.Address) Trie { return nil } cpy := stateObject.deepCopy(s, nil) - return cpy.updateTrie(s.db) + cpy.updateTrie(s.db) + return cpy.getTrie(s.db) } func (s *StateDB) HasSelfDestructed(addr common.Address) bool {