reth_rpc_eth_types/
transaction.rs

1//! Helper types for `reth_rpc_eth_api::EthApiServer` implementation.
2//!
3//! Transaction wrapper that labels transaction with its origin.
4
5use alloy_primitives::B256;
6use alloy_rpc_types_eth::TransactionInfo;
7use reth_ethereum_primitives::TransactionSigned;
8use reth_primitives_traits::{Recovered, SignedTransaction};
9use reth_rpc_types_compat::TransactionCompat;
10
11/// Represents from where a transaction was fetched.
12#[derive(Debug, Clone, Eq, PartialEq)]
13pub enum TransactionSource<T = TransactionSigned> {
14    /// Transaction exists in the pool (Pending)
15    Pool(Recovered<T>),
16    /// Transaction already included in a block
17    ///
18    /// This can be a historical block or a pending block (received from the CL)
19    Block {
20        /// Transaction fetched via provider
21        transaction: Recovered<T>,
22        /// Index of the transaction in the block
23        index: u64,
24        /// Hash of the block.
25        block_hash: B256,
26        /// Number of the block.
27        block_number: u64,
28        /// base fee of the block.
29        base_fee: Option<u64>,
30    },
31}
32
33// === impl TransactionSource ===
34
35impl<T: SignedTransaction> TransactionSource<T> {
36    /// Consumes the type and returns the wrapped transaction.
37    pub fn into_recovered(self) -> Recovered<T> {
38        self.into()
39    }
40
41    /// Conversion into network specific transaction type.
42    pub fn into_transaction<Builder: TransactionCompat<T>>(
43        self,
44        resp_builder: &Builder,
45    ) -> Result<Builder::Transaction, Builder::Error> {
46        match self {
47            Self::Pool(tx) => resp_builder.fill_pending(tx),
48            Self::Block { transaction, index, block_hash, block_number, base_fee } => {
49                let tx_info = TransactionInfo {
50                    hash: Some(transaction.trie_hash()),
51                    index: Some(index),
52                    block_hash: Some(block_hash),
53                    block_number: Some(block_number),
54                    base_fee,
55                };
56
57                resp_builder.fill(transaction, tx_info)
58            }
59        }
60    }
61
62    /// Returns the transaction and block related info, if not pending
63    pub fn split(self) -> (Recovered<T>, TransactionInfo) {
64        match self {
65            Self::Pool(tx) => {
66                let hash = tx.trie_hash();
67                (tx, TransactionInfo { hash: Some(hash), ..Default::default() })
68            }
69            Self::Block { transaction, index, block_hash, block_number, base_fee } => {
70                let hash = transaction.trie_hash();
71                (
72                    transaction,
73                    TransactionInfo {
74                        hash: Some(hash),
75                        index: Some(index),
76                        block_hash: Some(block_hash),
77                        block_number: Some(block_number),
78                        base_fee,
79                    },
80                )
81            }
82        }
83    }
84}
85
86impl<T> From<TransactionSource<T>> for Recovered<T> {
87    fn from(value: TransactionSource<T>) -> Self {
88        match value {
89            TransactionSource::Pool(tx) => tx,
90            TransactionSource::Block { transaction, .. } => transaction,
91        }
92    }
93}