Skip to main content

reth_storage_api/
transactions.rs

1use crate::{BlockNumReader, BlockReader};
2use alloc::vec::Vec;
3use alloy_consensus::transaction::TransactionMeta;
4use alloy_eips::BlockHashOrNumber;
5use alloy_primitives::{Address, BlockNumber, TxHash, TxNumber};
6use core::ops::{Range, RangeBounds, RangeInclusive};
7use reth_primitives_traits::SignedTransaction;
8use reth_storage_errors::provider::{ProviderError, ProviderResult};
9
10/// Enum to control transaction hash inclusion.
11///
12/// This serves as a hint to the provider to include or omit hashes because hashes are
13/// stored separately and are not always needed.
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
15pub enum TransactionVariant {
16    /// Indicates that transactions should be processed without including their hashes.
17    NoHash,
18    /// Indicates that transactions should be processed along with their hashes.
19    #[default]
20    WithHash,
21}
22
23///  Client trait for fetching transactions related data.
24#[auto_impl::auto_impl(&, Arc)]
25pub trait TransactionsProvider: BlockNumReader {
26    /// The transaction type this provider reads.
27    type Transaction: Send + Sync + SignedTransaction;
28
29    /// Get internal transaction identifier by transaction hash.
30    ///
31    /// This is the inverse of [`TransactionsProvider::transaction_by_id`].
32    /// Returns None if the transaction is not found.
33    fn transaction_id(&self, tx_hash: TxHash) -> ProviderResult<Option<TxNumber>>;
34
35    /// Get transaction by id, computes hash every time so more expensive.
36    fn transaction_by_id(&self, id: TxNumber) -> ProviderResult<Option<Self::Transaction>>;
37
38    /// Get transaction by id without computing the hash.
39    fn transaction_by_id_unhashed(&self, id: TxNumber)
40        -> ProviderResult<Option<Self::Transaction>>;
41
42    /// Get transaction by transaction hash.
43    fn transaction_by_hash(&self, hash: TxHash) -> ProviderResult<Option<Self::Transaction>>;
44
45    /// Get transaction by transaction hash and additional metadata of the block the transaction was
46    /// mined in
47    fn transaction_by_hash_with_meta(
48        &self,
49        hash: TxHash,
50    ) -> ProviderResult<Option<(Self::Transaction, TransactionMeta)>>;
51
52    /// Same as [`TransactionsProvider::transaction_by_hash_with_meta`], but also returns the
53    /// transaction sender if it is stored.
54    ///
55    /// The sender is `None` if senders are pruned or not stored by this provider, in which case it
56    /// must be recovered from the signature.
57    #[expect(clippy::type_complexity)]
58    fn transaction_by_hash_with_meta_and_sender(
59        &self,
60        hash: TxHash,
61    ) -> ProviderResult<Option<(Self::Transaction, TransactionMeta, Option<Address>)>> {
62        Ok(self.transaction_by_hash_with_meta(hash)?.map(|(tx, meta)| (tx, meta, None)))
63    }
64
65    /// Get transactions by block id.
66    fn transactions_by_block(
67        &self,
68        block: BlockHashOrNumber,
69    ) -> ProviderResult<Option<Vec<Self::Transaction>>>;
70
71    /// Get transactions by block range.
72    fn transactions_by_block_range(
73        &self,
74        range: impl RangeBounds<BlockNumber>,
75    ) -> ProviderResult<Vec<Vec<Self::Transaction>>>;
76
77    /// Get transactions by tx range.
78    fn transactions_by_tx_range(
79        &self,
80        range: impl RangeBounds<TxNumber>,
81    ) -> ProviderResult<Vec<Self::Transaction>>;
82
83    /// Get Senders from a tx range.
84    fn senders_by_tx_range(
85        &self,
86        range: impl RangeBounds<TxNumber>,
87    ) -> ProviderResult<Vec<Address>>;
88
89    /// Get transaction sender.
90    ///
91    /// Returns None if the transaction is not found.
92    fn transaction_sender(&self, id: TxNumber) -> ProviderResult<Option<Address>>;
93}
94
95/// A helper type alias to access [`TransactionsProvider::Transaction`].
96pub type ProviderTx<P> = <P as TransactionsProvider>::Transaction;
97
98///  Client trait for fetching additional transactions related data.
99#[auto_impl::auto_impl(&, Arc)]
100pub trait TransactionsProviderExt: BlockReader {
101    /// Get transactions range by block range.
102    fn transaction_range_by_block_range(
103        &self,
104        block_range: RangeInclusive<BlockNumber>,
105    ) -> ProviderResult<RangeInclusive<TxNumber>> {
106        let from = self
107            .block_body_indices(*block_range.start())?
108            .ok_or_else(|| ProviderError::BlockBodyIndicesNotFound(*block_range.start()))?
109            .first_tx_num();
110
111        let to = self
112            .block_body_indices(*block_range.end())?
113            .ok_or_else(|| ProviderError::BlockBodyIndicesNotFound(*block_range.end()))?
114            .last_tx_num();
115
116        Ok(from..=to)
117    }
118
119    /// Get transaction hashes from a transaction range.
120    fn transaction_hashes_by_range(
121        &self,
122        tx_range: Range<TxNumber>,
123    ) -> ProviderResult<Vec<(TxHash, TxNumber)>>;
124}