Skip to content

Commit

Permalink
Expose method to force statedb to use singlethreaded mode even when m…
Browse files Browse the repository at this point in the history
…ultiple CPUs are available. (#373)
  • Loading branch information
ajsutton authored and sebastianst committed Sep 17, 2024
1 parent d781223 commit 02dae7f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
11 changes: 9 additions & 2 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
//
Expand Down
5 changes: 2 additions & 3 deletions core/state/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package state

import (
"errors"
"runtime"

"golang.org/x/sync/errgroup"
)
Expand All @@ -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
Expand Down

0 comments on commit 02dae7f

Please sign in to comment.