From 515c367c8b0b43ab052f8e9b622076b28ad29a09 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 27 Jan 2025 17:17:29 +0100 Subject: [PATCH] feat: add find-transaction-by-hash (#14008) --- crates/chain-state/src/in_memory.rs | 36 +++++----------------- crates/primitives-traits/src/block/body.rs | 7 +++++ 2 files changed, 14 insertions(+), 29 deletions(-) diff --git a/crates/chain-state/src/in_memory.rs b/crates/chain-state/src/in_memory.rs index 60a0c80b3..f4f0094f0 100644 --- a/crates/chain-state/src/in_memory.rs +++ b/crates/chain-state/src/in_memory.rs @@ -545,17 +545,10 @@ impl CanonicalInMemoryState { } /// Returns [`SignedTransaction`] type for the given `TxHash` if found. - pub fn transaction_by_hash(&self, hash: TxHash) -> Option - where - N::SignedTx: Encodable2718, - { + pub fn transaction_by_hash(&self, hash: TxHash) -> Option { 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 CanonicalInMemoryState { 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 BlockState { } /// Tries to find a transaction by [`TxHash`] in the chain ending at this block. - pub fn transaction_on_chain(&self, hash: TxHash) -> Option - where - N::SignedTx: Encodable2718, - { + pub fn transaction_on_chain(&self, hash: TxHash) -> Option { 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 BlockState { 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() diff --git a/crates/primitives-traits/src/block/body.rs b/crates/primitives-traits/src/block/body.rs index 43bb0a3fa..783480c02 100644 --- a/crates/primitives-traits/src/block/body.rs +++ b/crates/primitives-traits/src/block/body.rs @@ -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()`