Skip to main content

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                    ..Default::default()
59                };
60
61                resp_builder.fill(transaction, tx_info)
62            }
63        }
64    }
65
66    /// Returns the transaction and block related info, if not pending
67    pub fn split(self) -> (Recovered<T>, TransactionInfo) {
68        match self {
69            Self::Pool(tx) => {
70                let hash = tx.trie_hash();
71                (tx, TransactionInfo { hash: Some(hash), ..Default::default() })
72            }
73            Self::Block { transaction, index, block_hash, block_number, base_fee } => {
74                let hash = transaction.trie_hash();
75                (
76                    transaction,
77                    TransactionInfo {
78                        hash: Some(hash),
79                        index: Some(index),
80                        block_hash: Some(block_hash),
81                        block_number: Some(block_number),
82                        base_fee,
83                        ..Default::default()
84                    },
85                )
86            }
87        }
88    }
89}
90
91impl<T> From<TransactionSource<T>> for Recovered<T> {
92    fn from(value: TransactionSource<T>) -> Self {
93        match value {
94            TransactionSource::Pool(tx) => tx,
95            TransactionSource::Block { transaction, .. } => transaction,
96        }
97    }
98}