triedb/pathdb: add lock in reader

This commit is contained in:
Gary Rong 2024-03-22 20:03:49 +08:00
parent 504f36d68b
commit e9be6d8f40

View file

@ -18,6 +18,7 @@ package pathdb
import ( import (
"fmt" "fmt"
"sync"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
@ -50,6 +51,7 @@ func (loc *nodeLoc) string() string {
type reader struct { type reader struct {
layer layer layer layer
state crypto.KeccakState state crypto.KeccakState
lock sync.Mutex
} }
// Node implements trie.Reader interface, retrieving the node with specified // Node implements trie.Reader interface, retrieving the node with specified
@ -60,7 +62,14 @@ func (r *reader) Node(owner common.Hash, path []byte, hash common.Hash) ([]byte,
if err != nil { if err != nil {
return nil, err return nil, err
} }
if got := crypto.HashData(r.state, blob); got != hash { // The function might be called concurrently, protect the
// hash buffer with lock.
r.lock.Lock()
got := crypto.HashData(r.state, blob)
r.lock.Unlock()
// Error out if the local one is inconsistent with the target.
if got != hash {
// Location is always available even if the node // Location is always available even if the node
// is not found. // is not found.
switch loc.loc { switch loc.loc {