diff --git a/crates/executor/src/blockchain_tree/mod.rs b/crates/executor/src/blockchain_tree/mod.rs index ad7c651bf8..0b2bbfceb0 100644 --- a/crates/executor/src/blockchain_tree/mod.rs +++ b/crates/executor/src/blockchain_tree/mod.rs @@ -147,6 +147,13 @@ impl BlockchainTree &self.block_indices } + /// Returns the block with matching hash. + pub fn block_by_hash(&self, block_hash: BlockHash) -> Option<&SealedBlock> { + let id = self.block_indices.get_blocks_chain_id(&block_hash)?; + let chain = self.chains.get(&id)?; + chain.block(block_hash) + } + /// Return items needed to execute on the pending state. /// This includes: /// * `BlockHash` of canonical block that chain connects to. Needed for creating database diff --git a/crates/executor/src/blockchain_tree/shareable.rs b/crates/executor/src/blockchain_tree/shareable.rs index 4e661027a5..5de2cbaae0 100644 --- a/crates/executor/src/blockchain_tree/shareable.rs +++ b/crates/executor/src/blockchain_tree/shareable.rs @@ -7,7 +7,7 @@ use reth_interfaces::{ provider::ProviderError, Error, }; -use reth_primitives::{BlockHash, BlockNumHash, BlockNumber, SealedBlockWithSenders}; +use reth_primitives::{BlockHash, BlockNumHash, BlockNumber, SealedBlock, SealedBlockWithSenders}; use reth_provider::{ BlockchainTreePendingStateProvider, CanonStateSubscriptions, ExecutorFactory, PostStateDataProvider, @@ -67,6 +67,10 @@ impl BlockchainTreeViewer self.tree.read().block_indices().index_of_number_to_pending_blocks().clone() } + fn block_by_hash(&self, block_hash: BlockHash) -> Option { + self.tree.read().block_by_hash(block_hash).cloned() + } + fn canonical_blocks(&self) -> BTreeMap { self.tree.read().block_indices().canonical_chain().clone() } diff --git a/crates/interfaces/src/blockchain_tree.rs b/crates/interfaces/src/blockchain_tree.rs index 8b0c83e752..4d74cda2c6 100644 --- a/crates/interfaces/src/blockchain_tree.rs +++ b/crates/interfaces/src/blockchain_tree.rs @@ -82,6 +82,9 @@ pub trait BlockchainTreeViewer: Send + Sync { /// Returns both pending and sidechain block numbers and their hashes. fn blocks(&self) -> BTreeMap>; + /// Returns the block with matching hash. + fn block_by_hash(&self, hash: BlockHash) -> Option; + /// Canonical block number and hashes best known by the tree. fn canonical_blocks(&self) -> BTreeMap; diff --git a/crates/storage/provider/src/chain.rs b/crates/storage/provider/src/chain.rs index 1614f60331..c57a510e18 100644 --- a/crates/storage/provider/src/chain.rs +++ b/crates/storage/provider/src/chain.rs @@ -3,8 +3,8 @@ use crate::PostState; use reth_interfaces::{executor::Error as ExecError, Error}; use reth_primitives::{ - BlockHash, BlockNumHash, BlockNumber, ForkBlock, Receipt, SealedBlockWithSenders, TransitionId, - TxHash, + BlockHash, BlockNumHash, BlockNumber, ForkBlock, Receipt, SealedBlock, SealedBlockWithSenders, + TransitionId, TxHash, }; use std::collections::BTreeMap; @@ -51,6 +51,13 @@ impl Chain { &self.block_transitions } + /// Returns the block with matching hash. + pub fn block(&self, block_hash: BlockHash) -> Option<&SealedBlock> { + self.blocks + .iter() + .find_map(|(_num, block)| (block.hash() == block_hash).then_some(&block.block)) + } + /// Return post state of the block at the `block_number` or None if block is not known pub fn state_at_block(&self, block_number: BlockNumber) -> Option { let mut state = self.state.clone();