diff --git a/core/blockchain_reader.go b/core/blockchain_reader.go index a594f9f065..51ca115c80 100644 --- a/core/blockchain_reader.go +++ b/core/blockchain_reader.go @@ -325,7 +325,9 @@ func (bc *BlockChain) GetAncestor(hash common.Hash, number, ancestor uint64, max // in the indexed part of the canonical chain without retrieving the transaction itself. // It's view is limited to the indexed part of the chain, so very old transactions // might fail the check if the indexer was constrained, or indexing is still in progress. -func (bc *BlockChain) HasCanonicalTransaction(hash common.Hash) bool { +// 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() @@ -334,7 +336,7 @@ func (bc *BlockChain) HasCanonicalTransaction(hash common.Hash) bool { return true } // Fallback to database lookup, without reading the transaction itself - if rawdb.HasCanonicalTransaction(bc.db, hash) { + if !cacheOnly && rawdb.HasCanonicalTransaction(bc.db, hash) { return true } return false diff --git a/eth/handler.go b/eth/handler.go index 1278dceed8..4b5825d06c 100644 --- a/eth/handler.go +++ b/eth/handler.go @@ -180,13 +180,15 @@ func newHandler(config *handlerConfig) (*handler, error) { return h.txpool.Add(txs, false) } hasTx := func(hash common.Hash) bool { - txpoolHas := h.txpool.Has(hash) - // check on chain as well (no need to check limbo separately, as chain checks limbo too) - _, tx := h.chain.GetCanonicalTransaction(hash) - if !txpoolHas && tx != nil { - log.Trace("handler: hasTx found tx on chain", "txhash", hash) + if h.txpool.Has(hash) { + return true } - return txpoolHas || tx != nil + // check on chain as well (no need to check limbo separately, as chain checks limbo too) + if h.chain.HasCanonicalTransaction(hash, false) { + return true + } + // tx not found + return false } validateMeta := func(tx common.Hash, kind byte) error { if hasTx(tx) {