From fb63f14fcbb10b35851adb0fb632542da8e7ac97 Mon Sep 17 00:00:00 2001 From: Adrian Sutton Date: Fri, 23 Aug 2024 10:53:51 +1000 Subject: [PATCH] Add override to avoid creating threads in StateDB.IntermediateRoot --- core/state/statedb.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/core/state/statedb.go b/core/state/statedb.go index 74f2e9518a..351ee28eea 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -167,6 +167,8 @@ type StateDB struct { StorageUpdated atomic.Int64 AccountDeleted int StorageDeleted atomic.Int64 + + singlethreaded bool } // New creates a new state from a given trie. @@ -196,6 +198,10 @@ func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error) return sdb, nil } +func (s *StateDB) MakeSingleThreaded() { + s.singlethreaded = true +} + // SetLogger sets the logger for account update hooks. func (s *StateDB) SetLogger(l *tracing.Hooks) { s.logger = l @@ -867,7 +873,7 @@ func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash { continue } obj := s.stateObjects[addr] // closure for the task runner below - workers.Go(func() error { + doUpdate := func() error { if s.db.TrieDB().IsVerkle() { obj.updateTrie() } else { @@ -880,7 +886,13 @@ func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash { } } return nil - }) + } + if s.singlethreaded { + // Ignores errors in the same way that the later workers.Wait() ignores errors + doUpdate() + } else { + workers.Go(doUpdate) + } } // If witness building is enabled, gather all the read-only accesses if s.witness != nil { @@ -913,7 +925,9 @@ func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash { } } } - workers.Wait() + if !s.singlethreaded { + workers.Wait() + } s.StorageUpdates += time.Since(start) // Now we're about to start to write changes to the trie. The trie is so far