Skip to content

Commit 1691d39

Browse files
committed
core: add cacheOnly flag to HasCanonicalTransaction
Signed-off-by: Csaba Kiraly <[email protected]>
1 parent 29d719d commit 1691d39

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

core/blockchain_reader.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,9 @@ func (bc *BlockChain) GetAncestor(hash common.Hash, number, ancestor uint64, max
325325
// in the indexed part of the canonical chain without retrieving the transaction itself.
326326
// It's view is limited to the indexed part of the chain, so very old transactions
327327
// might fail the check if the indexer was constrained, or indexing is still in progress.
328-
func (bc *BlockChain) HasCanonicalTransaction(hash common.Hash) bool {
328+
// The cacheOnly flag restricts the check to the in-memory cache, avoiding database
329+
// access altogether.
330+
func (bc *BlockChain) HasCanonicalTransaction(hash common.Hash, cacheOnly bool) bool {
329331
bc.txLookupLock.RLock()
330332
defer bc.txLookupLock.RUnlock()
331333

@@ -334,7 +336,7 @@ func (bc *BlockChain) HasCanonicalTransaction(hash common.Hash) bool {
334336
return true
335337
}
336338
// Fallback to database lookup, without reading the transaction itself
337-
if rawdb.HasCanonicalTransaction(bc.db, hash) {
339+
if !cacheOnly && rawdb.HasCanonicalTransaction(bc.db, hash) {
338340
return true
339341
}
340342
return false

eth/handler.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,15 @@ func newHandler(config *handlerConfig) (*handler, error) {
206206
return h.txpool.Add(txs, false)
207207
}
208208
hasTx := func(hash common.Hash) bool {
209-
txpoolHas := h.txpool.Has(hash)
209+
if h.txpool.Has(hash) {
210+
return true
211+
}
210212
// check on chain as well (no need to check limbo separately, as chain checks limbo too)
211-
_, tx := h.chain.GetCanonicalTransaction(hash)
212-
if !txpoolHas && tx != nil {
213-
log.Trace("handler: hasTx found tx on chain", "txhash", hash)
213+
if h.chain.HasCanonicalTransaction(hash, false) {
214+
return true
214215
}
215-
return txpoolHas || tx != nil
216+
// tx not found
217+
return false
216218
}
217219
h.txFetcher = fetcher.NewTxFetcher(hasTx, addTxs, fetchTx, h.removePeer)
218220
return h, nil

0 commit comments

Comments
 (0)