From 8197ceef167eb25ab9d1227cf7645627f93c48ee Mon Sep 17 00:00:00 2001 From: Csaba Kiraly Date: Sun, 30 Nov 2025 00:27:42 +0100 Subject: [PATCH] core: reduce lock contention in HasCanonicalTransaction Signed-off-by: Csaba Kiraly --- core/blockchain_reader.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/core/blockchain_reader.go b/core/blockchain_reader.go index 51ca115c80..0b9689b8bd 100644 --- a/core/blockchain_reader.go +++ b/core/blockchain_reader.go @@ -328,13 +328,14 @@ func (bc *BlockChain) GetAncestor(hash common.Hash, number, ancestor uint64, max // The cacheOnly flag restricts the check to the in-memory cache, avoiding database // access altogether. func (bc *BlockChain) HasCanonicalTransaction(hash common.Hash, cacheOnly bool) bool { - bc.txLookupLock.RLock() - defer bc.txLookupLock.RUnlock() - // Check in memory cache first + bc.txLookupLock.RLock() if _, exist := bc.txLookupCache.Get(hash); exist { + bc.txLookupLock.RUnlock() return true } + bc.txLookupLock.RUnlock() + // Fallback to database lookup, without reading the transaction itself if !cacheOnly && rawdb.HasCanonicalTransaction(bc.db, hash) { return true