From 02dae7f379f92679dd2e85913bc3c04791241a24 Mon Sep 17 00:00:00 2001 From: Adrian Sutton Date: Tue, 27 Aug 2024 14:23:33 +1000 Subject: [PATCH] Expose method to force statedb to use singlethreaded mode even when multiple CPUs are available. (#373) --- core/state/statedb.go | 11 +++++++++-- core/state/workers.go | 5 ++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/core/state/statedb.go b/core/state/statedb.go index bbd4eba339..20994d6cfe 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -166,6 +166,9 @@ type StateDB struct { StorageUpdated atomic.Int64 AccountDeleted int StorageDeleted atomic.Int64 + + // singlethreaded avoids creation of additional threads when set to true for compatibility with cannon. + singlethreaded bool } // New creates a new state from a given trie. @@ -195,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 @@ -852,7 +859,7 @@ func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash { var ( start = time.Now() ) - workers := newWorkerGroup() + workers := newWorkerGroup(s.singlethreaded) if s.db.TrieDB().IsVerkle() { // Whilst MPT storage tries are independent, Verkle has one single trie // for all the accounts and all the storage slots merged together. The @@ -1227,7 +1234,7 @@ func (s *StateDB) commit(deleteEmptyObjects bool) (*stateUpdate, error) { start = time.Now() root common.Hash ) - workers := newWorkerGroup() + workers := newWorkerGroup(s.singlethreaded) // Schedule the account trie first since that will be the biggest, so give // it the most time to crunch. // diff --git a/core/state/workers.go b/core/state/workers.go index d145cfe8a5..72b93cd7ad 100644 --- a/core/state/workers.go +++ b/core/state/workers.go @@ -2,7 +2,6 @@ package state import ( "errors" - "runtime" "golang.org/x/sync/errgroup" ) @@ -13,8 +12,8 @@ type workerGroup interface { Wait() error } -func newWorkerGroup() workerGroup { - if runtime.NumCPU() <= 1 { +func newWorkerGroup(singlethreaded bool) workerGroup { + if singlethreaded { return &inlineWorkerGroup{} } else { var grp errgroup.Group