mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-22 04:36:42 +00:00
core: add lightweight HasCanonicalTransaction check
We only had the getter exposed, which is heavy compared to just checking the existence. Signed-off-by: Csaba Kiraly <csaba.kiraly@gmail.com>
This commit is contained in:
parent
cc11ffd463
commit
8ae608bd16
2 changed files with 25 additions and 0 deletions
|
|
@ -321,6 +321,25 @@ func (bc *BlockChain) GetAncestor(hash common.Hash, number, ancestor uint64, max
|
||||||
return bc.hc.GetAncestor(hash, number, ancestor, maxNonCanonical)
|
return bc.hc.GetAncestor(hash, number, ancestor, maxNonCanonical)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HasCanonicalTransaction is a lightweight check to see if a transaction is present
|
||||||
|
// 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 {
|
||||||
|
bc.txLookupLock.RLock()
|
||||||
|
defer bc.txLookupLock.RUnlock()
|
||||||
|
|
||||||
|
// Check in memory cache first
|
||||||
|
if _, exist := bc.txLookupCache.Get(hash); exist {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
// Fallback to database lookup, without reading the transaction itself
|
||||||
|
if rawdb.HasCanonicalTransaction(bc.db, hash) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// GetCanonicalTransaction retrieves the lookup along with the transaction
|
// GetCanonicalTransaction retrieves the lookup along with the transaction
|
||||||
// itself associate with the given transaction hash.
|
// itself associate with the given transaction hash.
|
||||||
//
|
//
|
||||||
|
|
|
||||||
|
|
@ -174,6 +174,12 @@ func findTxInBlockBody(blockbody rlp.RawValue, target common.Hash) (*types.Trans
|
||||||
return nil, 0, errors.New("transaction not found")
|
return nil, 0, errors.New("transaction not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HasCanonicalTransaction checks whether a specific transaction is in the database.
|
||||||
|
func HasCanonicalTransaction(db ethdb.Reader, hash common.Hash) bool {
|
||||||
|
blockNumber := ReadTxLookupEntry(db, hash)
|
||||||
|
return blockNumber != nil
|
||||||
|
}
|
||||||
|
|
||||||
// ReadCanonicalTransaction retrieves a specific transaction from the database, along
|
// ReadCanonicalTransaction retrieves a specific transaction from the database, along
|
||||||
// with its added positional metadata. Notably, only the transaction in the canonical
|
// with its added positional metadata. Notably, only the transaction in the canonical
|
||||||
// chain is visible.
|
// chain is visible.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue