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};
910/// Enum to control transaction hash inclusion.
11///
12/// This serves as a hint to the provider to include or omit exclude 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.
17NoHash,
18/// Indicates that transactions should be processed along with their hashes.
19#[default]
20WithHash,
21}
2223/// Client trait for fetching transactions related data.
24#[auto_impl::auto_impl(&, Arc)]
25pub trait TransactionsProvider: BlockNumReader + Send + Sync {
26/// The transaction type this provider reads.
27type Transaction: Send + Sync + SignedTransaction;
2829/// 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.
33fn transaction_id(&self, tx_hash: TxHash) -> ProviderResult<Option<TxNumber>>;
3435/// Get transaction by id, computes hash every time so more expensive.
36fn transaction_by_id(&self, id: TxNumber) -> ProviderResult<Option<Self::Transaction>>;
3738/// Get transaction by id without computing the hash.
39fn transaction_by_id_unhashed(&self, id: TxNumber)
40 -> ProviderResult<Option<Self::Transaction>>;
4142/// Get transaction by transaction hash.
43fn transaction_by_hash(&self, hash: TxHash) -> ProviderResult<Option<Self::Transaction>>;
4445/// Get transaction by transaction hash and additional metadata of the block the transaction was
46 /// mined in
47fn transaction_by_hash_with_meta(
48&self,
49 hash: TxHash,
50 ) -> ProviderResult<Option<(Self::Transaction, TransactionMeta)>>;
5152/// Get transaction block number
53fn transaction_block(&self, id: TxNumber) -> ProviderResult<Option<BlockNumber>>;
5455/// Get transactions by block id.
56fn transactions_by_block(
57&self,
58 block: BlockHashOrNumber,
59 ) -> ProviderResult<Option<Vec<Self::Transaction>>>;
6061/// Get transactions by block range.
62fn transactions_by_block_range(
63&self,
64 range: impl RangeBounds<BlockNumber>,
65 ) -> ProviderResult<Vec<Vec<Self::Transaction>>>;
6667/// Get transactions by tx range.
68fn transactions_by_tx_range(
69&self,
70 range: impl RangeBounds<TxNumber>,
71 ) -> ProviderResult<Vec<Self::Transaction>>;
7273/// Get Senders from a tx range.
74fn senders_by_tx_range(
75&self,
76 range: impl RangeBounds<TxNumber>,
77 ) -> ProviderResult<Vec<Address>>;
7879/// Get transaction sender.
80 ///
81 /// Returns None if the transaction is not found.
82fn transaction_sender(&self, id: TxNumber) -> ProviderResult<Option<Address>>;
83}
8485/// A helper type alias to access [`TransactionsProvider::Transaction`].
86pub type ProviderTx<P> = <P as TransactionsProvider>::Transaction;
8788/// Client trait for fetching additional transactions related data.
89#[auto_impl::auto_impl(&, Arc)]
90pub trait TransactionsProviderExt: BlockReader {
91/// Get transactions range by block range.
92fn transaction_range_by_block_range(
93&self,
94 block_range: RangeInclusive<BlockNumber>,
95 ) -> ProviderResult<RangeInclusive<TxNumber>> {
96let from = self97.block_body_indices(*block_range.start())?
98.ok_or_else(|| ProviderError::BlockBodyIndicesNotFound(*block_range.start()))?
99.first_tx_num();
100101let to = self102.block_body_indices(*block_range.end())?
103.ok_or_else(|| ProviderError::BlockBodyIndicesNotFound(*block_range.end()))?
104.last_tx_num();
105106Ok(from..=to)
107 }
108109/// Get transaction hashes from a transaction range.
110fn transaction_hashes_by_range(
111&self,
112 tx_range: Range<TxNumber>,
113 ) -> ProviderResult<Vec<(TxHash, TxNumber)>>;
114}