core: reduce lock contention in HasCanonicalTransaction

Signed-off-by: Csaba Kiraly <csaba.kiraly@gmail.com>
This commit is contained in:
Csaba Kiraly 2025-11-30 00:27:42 +01:00
parent 259705a9e8
commit 8197ceef16
No known key found for this signature in database
GPG key ID: 0FE274EE8C95166E

View file

@ -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