reth_rpc_eth_types/
block.rs

1//! Block related types for RPC API.
2
3use std::sync::Arc;
4
5use alloy_primitives::TxHash;
6use reth_primitives_traits::{
7    BlockTy, IndexedTx, NodePrimitives, ReceiptTy, RecoveredBlock, SealedBlock,
8};
9
10/// A pair of an [`Arc`] wrapped [`RecoveredBlock`] and its corresponding receipts.
11///
12/// This type is used throughout the RPC layer to efficiently pass around
13/// blocks with their execution receipts, avoiding unnecessary cloning.
14#[derive(Debug, Clone)]
15pub struct BlockAndReceipts<N: NodePrimitives> {
16    /// The recovered block.
17    pub block: Arc<RecoveredBlock<BlockTy<N>>>,
18    /// The receipts for the block.
19    pub receipts: Arc<Vec<ReceiptTy<N>>>,
20}
21
22impl<N: NodePrimitives> BlockAndReceipts<N> {
23    /// Creates a new [`BlockAndReceipts`] instance.
24    pub const fn new(
25        block: Arc<RecoveredBlock<BlockTy<N>>>,
26        receipts: Arc<Vec<ReceiptTy<N>>>,
27    ) -> Self {
28        Self { block, receipts }
29    }
30
31    /// Finds a transaction by hash and returns it along with its corresponding receipt.
32    ///
33    /// Returns `None` if the transaction is not found in this block.
34    pub fn find_transaction_and_receipt_by_hash(
35        &self,
36        tx_hash: TxHash,
37    ) -> Option<(IndexedTx<'_, N::Block>, &N::Receipt)> {
38        let indexed_tx = self.block.find_indexed(tx_hash)?;
39        let receipt = self.receipts.get(indexed_tx.index())?;
40        Some((indexed_tx, receipt))
41    }
42
43    /// Returns the underlying sealed block.
44    pub fn sealed_block(&self) -> &SealedBlock<BlockTy<N>> {
45        self.block.sealed_block()
46    }
47}