reth_storage_api/receipts.rs
1use crate::BlockIdReader;
2use alloc::vec::Vec;
3use alloy_eips::{BlockHashOrNumber, BlockId, BlockNumberOrTag};
4use alloy_primitives::{TxHash, TxNumber};
5use core::ops::RangeBounds;
6use reth_primitives_traits::Receipt;
7use reth_storage_errors::provider::ProviderResult;
8
9/// A helper type alias to access [`ReceiptProvider::Receipt`].
10pub type ProviderReceipt<P> = <P as ReceiptProvider>::Receipt;
11
12/// Client trait for fetching receipt data.
13#[auto_impl::auto_impl(&, Arc)]
14pub trait ReceiptProvider: Send + Sync {
15 /// The receipt type.
16 type Receipt: Receipt;
17
18 /// Get receipt by transaction number
19 ///
20 /// Returns `None` if the transaction is not found.
21 fn receipt(&self, id: TxNumber) -> ProviderResult<Option<Self::Receipt>>;
22
23 /// Get receipt by transaction hash.
24 ///
25 /// Returns `None` if the transaction is not found.
26 fn receipt_by_hash(&self, hash: TxHash) -> ProviderResult<Option<Self::Receipt>>;
27
28 /// Get receipts by block num or hash.
29 ///
30 /// Returns `None` if the block is not found.
31 fn receipts_by_block(
32 &self,
33 block: BlockHashOrNumber,
34 ) -> ProviderResult<Option<Vec<Self::Receipt>>>;
35
36 /// Get receipts by tx range.
37 fn receipts_by_tx_range(
38 &self,
39 range: impl RangeBounds<TxNumber>,
40 ) -> ProviderResult<Vec<Self::Receipt>>;
41}
42
43/// Trait extension for `ReceiptProvider`, for types that implement `BlockId` conversion.
44///
45/// The `Receipt` trait should be implemented on types that can retrieve receipts from either
46/// a block number or hash. However, it might be desirable to fetch receipts from a `BlockId` type,
47/// which can be a number, hash, or tag such as `BlockNumberOrTag::Safe`.
48///
49/// Resolving tags requires keeping track of block hashes or block numbers associated with the tag,
50/// so this trait can only be implemented for types that implement `BlockIdReader`. The
51/// `BlockIdReader` methods should be used to resolve `BlockId`s to block numbers or hashes, and
52/// retrieving the receipts should be done using the type's `ReceiptProvider` methods.
53pub trait ReceiptProviderIdExt: ReceiptProvider + BlockIdReader {
54 /// Get receipt by block id
55 fn receipts_by_block_id(&self, block: BlockId) -> ProviderResult<Option<Vec<Self::Receipt>>> {
56 let id = match block {
57 BlockId::Hash(hash) => BlockHashOrNumber::Hash(hash.block_hash),
58 BlockId::Number(num_tag) => {
59 if let Some(num) = self.convert_block_number(num_tag)? {
60 BlockHashOrNumber::Number(num)
61 } else {
62 return Ok(None)
63 }
64 }
65 };
66
67 self.receipts_by_block(id)
68 }
69
70 /// Returns the block with the matching `BlockId` from the database.
71 ///
72 /// Returns `None` if block is not found.
73 fn receipts_by_number_or_tag(
74 &self,
75 number_or_tag: BlockNumberOrTag,
76 ) -> ProviderResult<Option<Vec<Self::Receipt>>> {
77 self.receipts_by_block_id(number_or_tag.into())
78 }
79}