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. (ethereum-optimism#373)
  • Loading branch information
ajsutton authored and kangsorang committed Oct 16, 2024
1 parent d4ff6e0 commit 65a1b18
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
12 changes: 9 additions & 3 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import (
"github.com/ethereum/go-ethereum/trie/triestate"
"github.com/ethereum/go-ethereum/trie/utils"
"github.com/holiman/uint256"
"golang.org/x/sync/errgroup"
)

// TriesInMemory represents the number of layers that are kept in RAM.
Expand Down Expand Up @@ -167,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 @@ -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
Expand Down Expand Up @@ -852,7 +858,7 @@ func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash {
// so there's no need to explicitly wait for the prefetchers to finish.
var (
start = time.Now()
workers errgroup.Group
workers = newWorkerGroup(s.singlethreaded)
)
if s.db.TrieDB().IsVerkle() {
// Whilst MPT storage tries are independent, Verkle has one single trie
Expand Down Expand Up @@ -1227,7 +1233,7 @@ func (s *StateDB) commit(deleteEmptyObjects bool) (*stateUpdate, error) {
var (
start = time.Now()
root common.Hash
workers errgroup.Group
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
37 changes: 37 additions & 0 deletions core/state/workers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package state

import (
"errors"

"golang.org/x/sync/errgroup"
)

type workerGroup interface {
Go(func() error)
SetLimit(int)
Wait() error
}

func newWorkerGroup(singlethreaded bool) workerGroup {
if singlethreaded {
return &inlineWorkerGroup{}
} else {
var grp errgroup.Group
return &grp
}
}

type inlineWorkerGroup struct {
err error
}

func (i *inlineWorkerGroup) Go(action func() error) {
i.err = errors.Join(i.err, action())
}

func (i *inlineWorkerGroup) SetLimit(_ int) {
}

func (i *inlineWorkerGroup) Wait() error {
return i.err
}

0 comments on commit 65a1b18

Please sign in to comment.