reth_rpc/eth/helpers/
block.rs

1//! Contains RPC handler implementations specific to blocks.
2
3use alloy_consensus::BlockHeader;
4use alloy_rpc_types_eth::{BlockId, TransactionReceipt};
5use reth_primitives::TransactionMeta;
6use reth_primitives_traits::{BlockBody, SignedTransaction};
7use reth_provider::BlockReader;
8use reth_rpc_eth_api::{
9    helpers::{EthBlocks, LoadBlock, LoadPendingBlock, LoadReceipt, SpawnBlocking},
10    RpcNodeCoreExt, RpcReceipt,
11};
12use reth_rpc_eth_types::{EthApiError, EthReceiptBuilder};
13
14use crate::EthApi;
15
16impl<Provider, Pool, Network, EvmConfig> EthBlocks for EthApi<Provider, Pool, Network, EvmConfig>
17where
18    Self: LoadBlock<
19        Error = EthApiError,
20        NetworkTypes: alloy_network::Network<ReceiptResponse = TransactionReceipt>,
21        Provider: BlockReader<
22            Transaction = reth_primitives::TransactionSigned,
23            Receipt = reth_primitives::Receipt,
24        >,
25    >,
26    Provider: BlockReader,
27{
28    async fn block_receipts(
29        &self,
30        block_id: BlockId,
31    ) -> Result<Option<Vec<RpcReceipt<Self::NetworkTypes>>>, Self::Error>
32    where
33        Self: LoadReceipt,
34    {
35        if let Some((block, receipts)) = self.load_block_and_receipts(block_id).await? {
36            let block_number = block.number();
37            let base_fee = block.base_fee_per_gas();
38            let block_hash = block.hash();
39            let excess_blob_gas = block.excess_blob_gas();
40            let timestamp = block.timestamp();
41
42            return block
43                .body
44                .transactions()
45                .iter()
46                .zip(receipts.iter())
47                .enumerate()
48                .map(|(idx, (tx, receipt))| {
49                    let meta = TransactionMeta {
50                        tx_hash: *tx.tx_hash(),
51                        index: idx as u64,
52                        block_hash,
53                        block_number,
54                        base_fee,
55                        excess_blob_gas,
56                        timestamp,
57                    };
58                    EthReceiptBuilder::new(tx, meta, receipt, &receipts)
59                        .map(|builder| builder.build())
60                })
61                .collect::<Result<Vec<_>, Self::Error>>()
62                .map(Some)
63        }
64
65        Ok(None)
66    }
67}
68
69impl<Provider, Pool, Network, EvmConfig> LoadBlock for EthApi<Provider, Pool, Network, EvmConfig>
70where
71    Self: LoadPendingBlock + SpawnBlocking + RpcNodeCoreExt,
72    Provider: BlockReader,
73{
74}