Skip to content

Commit 80efb72

Browse files
committed
core: reduce lock contention in HasCanonicalTransaction
Signed-off-by: Csaba Kiraly <[email protected]>
1 parent fc553de commit 80efb72

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

core/blockchain_reader.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,13 +328,14 @@ func (bc *BlockChain) GetAncestor(hash common.Hash, number, ancestor uint64, max
328328
// The cacheOnly flag restricts the check to the in-memory cache, avoiding database
329329
// access altogether.
330330
func (bc *BlockChain) HasCanonicalTransaction(hash common.Hash, cacheOnly bool) bool {
331-
bc.txLookupLock.RLock()
332-
defer bc.txLookupLock.RUnlock()
333-
334331
// Check in memory cache first
332+
bc.txLookupLock.RLock()
335333
if _, exist := bc.txLookupCache.Get(hash); exist {
334+
bc.txLookupLock.RUnlock()
336335
return true
337336
}
337+
bc.txLookupLock.RUnlock()
338+
338339
// Fallback to database lookup, without reading the transaction itself
339340
if !cacheOnly && rawdb.HasCanonicalTransaction(bc.db, hash) {
340341
return true

0 commit comments

Comments
 (0)