core: add cacheOnly flag to HasCanonicalTransaction

Signed-off-by: Csaba Kiraly <csaba.kiraly@gmail.com>
This commit is contained in:
Csaba Kiraly 2025-11-28 15:44:04 +01:00
parent 2c0f815ca9
commit f5ff0e837d
No known key found for this signature in database
GPG key ID: 0FE274EE8C95166E
2 changed files with 12 additions and 8 deletions

View file

@ -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. // 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 // 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. // 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() bc.txLookupLock.RLock()
defer bc.txLookupLock.RUnlock() defer bc.txLookupLock.RUnlock()
@ -334,7 +336,7 @@ func (bc *BlockChain) HasCanonicalTransaction(hash common.Hash) bool {
return true return true
} }
// Fallback to database lookup, without reading the transaction itself // 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 true
} }
return false return false

View file

@ -180,13 +180,15 @@ func newHandler(config *handlerConfig) (*handler, error) {
return h.txpool.Add(txs, false) return h.txpool.Add(txs, false)
} }
hasTx := func(hash common.Hash) bool { hasTx := func(hash common.Hash) bool {
txpoolHas := h.txpool.Has(hash) if h.txpool.Has(hash) {
// check on chain as well (no need to check limbo separately, as chain checks limbo too) return true
_, tx := h.chain.GetCanonicalTransaction(hash)
if !txpoolHas && tx != nil {
log.Trace("handler: hasTx found tx on chain", "txhash", hash)
} }
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 { validateMeta := func(tx common.Hash, kind byte) error {
if hasTx(tx) { if hasTx(tx) {