From e9be6d8f40ce0efba6f239ba6332b7a5d6c3bf02 Mon Sep 17 00:00:00 2001 From: Gary Rong Date: Fri, 22 Mar 2024 20:03:49 +0800 Subject: [PATCH] triedb/pathdb: add lock in reader --- triedb/pathdb/reader.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/triedb/pathdb/reader.go b/triedb/pathdb/reader.go index 90f7ed0bb9..9c27eca0e2 100644 --- a/triedb/pathdb/reader.go +++ b/triedb/pathdb/reader.go @@ -18,6 +18,7 @@ package pathdb import ( "fmt" + "sync" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" @@ -50,6 +51,7 @@ func (loc *nodeLoc) string() string { type reader struct { layer layer state crypto.KeccakState + lock sync.Mutex } // 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 { 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 // is not found. switch loc.loc {