Skip to content

Commit

Permalink
Merge pull request #1466 from maticnetwork/lmartins/bug-concurrent-ma…
Browse files Browse the repository at this point in the history
…p-writes-triereader

Bug fix: concurrent map writes
  • Loading branch information
lucca30 authored Feb 28, 2025
2 parents 6519ce6 + 375260e commit 08f81df
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions core/state/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package state
import (
"errors"
"maps"
"sync"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state/snapshot"
Expand Down Expand Up @@ -139,12 +140,14 @@ func (r *stateReader) Copy() Reader {
// trieReader implements the Reader interface, providing functions to access
// state from the referenced trie.
type trieReader struct {
root common.Hash // State root which uniquely represent a state
db *triedb.Database // Database for loading trie
buff crypto.KeccakState // Buffer for keccak256 hashing
mainTrie Trie // Main trie, resolved in constructor
subRoots map[common.Address]common.Hash // Set of storage roots, cached when the account is resolved
subTries map[common.Address]Trie // Group of storage tries, cached when it's resolved
root common.Hash // State root which uniquely represent a state
db *triedb.Database // Database for loading trie
buff crypto.KeccakState // Buffer for keccak256 hashing
mainTrie Trie // Main trie, resolved in constructor
subRoots map[common.Address]common.Hash // Set of storage roots, cached when the account is resolved
subTries map[common.Address]Trie // Group of storage tries, cached when it's resolved
muSubRoot sync.Mutex
muSubTries sync.Mutex
}

// trieReader constructs a trie reader of the specific state. An error will be
Expand Down Expand Up @@ -181,11 +184,14 @@ func (r *trieReader) Account(addr common.Address) (*types.StateAccount, error) {
if err != nil {
return nil, err
}
r.muSubRoot.Lock()
if account == nil {
r.subRoots[addr] = types.EmptyRootHash
} else {
r.subRoots[addr] = account.Root
}
r.muSubRoot.Unlock()

return account, nil
}

Expand Down Expand Up @@ -221,7 +227,9 @@ func (r *trieReader) Storage(addr common.Address, key common.Hash) (common.Hash,
if err != nil {
return common.Hash{}, err
}
r.muSubTries.Lock()
r.subTries[addr] = tr
r.muSubTries.Unlock()
}
}
ret, err := tr.GetStorage(addr, key.Bytes())
Expand Down

0 comments on commit 08f81df

Please sign in to comment.