Skip to content

Commit

Permalink
fix: make statedb hasher thread-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
andyzhang2023 committed Oct 10, 2024
1 parent 0c924b3 commit 2a2fe96
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ type StateDB struct {
trie Trie
noTrie bool
hasher crypto.KeccakState
hasherLock sync.Mutex
snaps *snapshot.Tree // Nil if snapshot is not available
snap snapshot.Snapshot // Nil if snapshot is not available

Expand Down Expand Up @@ -877,7 +878,11 @@ func (s *StateDB) getStateObjectFromSnapshotOrTrie(addr common.Address) (data *t
// If no live objects are available, attempt to use snapshots
if s.snap != nil {
start := time.Now()
acc, err := s.snap.Account(crypto.HashData(s.hasher, addr.Bytes()))
// the s.hasher is not thread-safe, let's use a mutex to protect it
s.hasherLock.Lock()
hash := crypto.HashData(s.hasher, addr.Bytes())
s.hasherLock.Unlock()
acc, err := s.snap.Account(hash)
if metrics.EnabledExpensive {
s.SnapshotAccountReads += time.Since(start)
}
Expand Down

0 comments on commit 2a2fe96

Please sign in to comment.