feat: add find-transaction-by-hash (#14008)

This commit is contained in:
Matthias Seitz
2025-01-27 17:17:29 +01:00
committed by GitHub
parent cde951732e
commit 515c367c8b
2 changed files with 14 additions and 29 deletions

View File

@ -545,17 +545,10 @@ impl<N: NodePrimitives> CanonicalInMemoryState<N> {
}
/// Returns [`SignedTransaction`] type for the given `TxHash` if found.
pub fn transaction_by_hash(&self, hash: TxHash) -> Option<N::SignedTx>
where
N::SignedTx: Encodable2718,
{
pub fn transaction_by_hash(&self, hash: TxHash) -> Option<N::SignedTx> {
for block_state in self.canonical_chain() {
if let Some(tx) = block_state
.block_ref()
.recovered_block()
.body()
.transactions_iter()
.find(|tx| tx.trie_hash() == hash)
if let Some(tx) =
block_state.block_ref().recovered_block().body().transaction_by_hash(&hash)
{
return Some(tx.clone())
}
@ -568,10 +561,7 @@ impl<N: NodePrimitives> CanonicalInMemoryState<N> {
pub fn transaction_by_hash_with_meta(
&self,
tx_hash: TxHash,
) -> Option<(N::SignedTx, TransactionMeta)>
where
N::SignedTx: Encodable2718,
{
) -> Option<(N::SignedTx, TransactionMeta)> {
for block_state in self.canonical_chain() {
if let Some((index, tx)) = block_state
.block_ref()
@ -734,18 +724,9 @@ impl<N: NodePrimitives> BlockState<N> {
}
/// Tries to find a transaction by [`TxHash`] in the chain ending at this block.
pub fn transaction_on_chain(&self, hash: TxHash) -> Option<N::SignedTx>
where
N::SignedTx: Encodable2718,
{
pub fn transaction_on_chain(&self, hash: TxHash) -> Option<N::SignedTx> {
self.chain().find_map(|block_state| {
block_state
.block_ref()
.recovered_block()
.body()
.transactions_iter()
.find(|tx| tx.trie_hash() == hash)
.cloned()
block_state.block_ref().recovered_block().body().transaction_by_hash(&hash).cloned()
})
}
@ -753,10 +734,7 @@ impl<N: NodePrimitives> BlockState<N> {
pub fn transaction_meta_on_chain(
&self,
tx_hash: TxHash,
) -> Option<(N::SignedTx, TransactionMeta)>
where
N::SignedTx: Encodable2718,
{
) -> Option<(N::SignedTx, TransactionMeta)> {
self.chain().find_map(|block_state| {
block_state
.block_ref()

View File

@ -47,6 +47,13 @@ pub trait BlockBody:
self.transactions().iter()
}
/// Returns the transaction with the matching hash.
///
/// This is a convenience function for `transactions_iter().find()`
fn transaction_by_hash(&self, hash: &B256) -> Option<&Self::Transaction> {
self.transactions_iter().find(|tx| tx.tx_hash() == hash)
}
/// Clones the transactions in the block.
///
/// This is a convenience function for `transactions().to_vec()`