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::{NodePrimitives, Recovered, SignedTransaction};
9use reth_rpc_convert::{RpcConvert, RpcTransaction};
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>(
43        self,
44        resp_builder: &Builder,
45    ) -> Result<RpcTransaction<Builder::Network>, Builder::Error>
46    where
47        Builder: RpcConvert<Primitives: NodePrimitives<SignedTx = T>>,
48    {
49        match self {
50            Self::Pool(tx) => resp_builder.fill_pending(tx),
51            Self::Block { transaction, index, block_hash, block_number, base_fee } => {
52                let tx_info = TransactionInfo {
53                    hash: Some(transaction.trie_hash()),
54                    index: Some(index),
55                    block_hash: Some(block_hash),
56                    block_number: Some(block_number),
57                    base_fee,
58                };
59
60                resp_builder.fill(transaction, tx_info)
61            }
62        }
63    }
64
65    /// Returns the transaction and block related info, if not pending
66    pub fn split(self) -> (Recovered<T>, TransactionInfo) {
67        match self {
68            Self::Pool(tx) => {
69                let hash = tx.trie_hash();
70                (tx, TransactionInfo { hash: Some(hash), ..Default::default() })
71            }
72            Self::Block { transaction, index, block_hash, block_number, base_fee } => {
73                let hash = transaction.trie_hash();
74                (
75                    transaction,
76                    TransactionInfo {
77                        hash: Some(hash),
78                        index: Some(index),
79                        block_hash: Some(block_hash),
80                        block_number: Some(block_number),
81                        base_fee,
82                    },
83                )
84            }
85        }
86    }
87}
88
89impl<T> From<TransactionSource<T>> for Recovered<T> {
90    fn from(value: TransactionSource<T>) -> Self {
91        match value {
92            TransactionSource::Pool(tx) => tx,
93            TransactionSource::Block { transaction, .. } => transaction,
94        }
95    }
96}