reth_rpc/eth/helpers/
transaction.rs

1//! Contains RPC handler implementations specific to transactions
2
3use std::time::Duration;
4
5use crate::EthApi;
6use alloy_primitives::{hex, Bytes, B256};
7use reth_rpc_convert::RpcConvert;
8use reth_rpc_eth_api::{
9    helpers::{spec::SignersForRpc, EthTransactions, LoadTransaction},
10    FromEvmError, RpcNodeCore,
11};
12use reth_rpc_eth_types::{utils::recover_raw_transaction, EthApiError};
13use reth_transaction_pool::{AddedTransactionOutcome, PoolTransaction, TransactionPool};
14
15impl<N, Rpc> EthTransactions for EthApi<N, Rpc>
16where
17    N: RpcNodeCore,
18    EthApiError: FromEvmError<N::Evm>,
19    Rpc: RpcConvert<Primitives = N::Primitives, Error = EthApiError>,
20{
21    #[inline]
22    fn signers(&self) -> &SignersForRpc<Self::Provider, Self::NetworkTypes> {
23        self.inner.signers()
24    }
25
26    #[inline]
27    fn send_raw_transaction_sync_timeout(&self) -> Duration {
28        self.inner.send_raw_transaction_sync_timeout()
29    }
30
31    /// Decodes and recovers the transaction and submits it to the pool.
32    ///
33    /// Returns the hash of the transaction.
34    async fn send_raw_transaction(&self, tx: Bytes) -> Result<B256, Self::Error> {
35        let recovered = recover_raw_transaction(&tx)?;
36
37        let pool_transaction = <Self::Pool as TransactionPool>::Transaction::from_pooled(recovered);
38
39        // forward the transaction to the specific endpoint if configured.
40        if let Some(client) = self.raw_tx_forwarder() {
41            tracing::debug!(target: "rpc::eth", hash = %pool_transaction.hash(), "forwarding raw transaction to forwarder");
42            let rlp_hex = hex::encode_prefixed(&tx);
43
44            // broadcast raw transaction to subscribers if there is any.
45            self.broadcast_raw_transaction(tx);
46
47            let hash =
48                client.request("eth_sendRawTransaction", (rlp_hex,)).await.inspect_err(|err| {
49                    tracing::debug!(target: "rpc::eth", %err, hash=% *pool_transaction.hash(), "failed to forward raw transaction");
50                }).map_err(EthApiError::other)?;
51
52            // Retain tx in local tx pool after forwarding, for local RPC usage.
53            let _ = self.inner.add_pool_transaction(pool_transaction).await;
54
55            return Ok(hash);
56        }
57
58        // broadcast raw transaction to subscribers if there is any.
59        self.broadcast_raw_transaction(tx);
60
61        // submit the transaction to the pool with a `Local` origin
62        let AddedTransactionOutcome { hash, .. } =
63            self.inner.add_pool_transaction(pool_transaction).await?;
64
65        Ok(hash)
66    }
67}
68
69impl<N, Rpc> LoadTransaction for EthApi<N, Rpc>
70where
71    N: RpcNodeCore,
72    EthApiError: FromEvmError<N::Evm>,
73    Rpc: RpcConvert<Primitives = N::Primitives, Error = EthApiError>,
74{
75}
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80    use alloy_primitives::{hex_literal::hex, Bytes};
81    use reth_chainspec::ChainSpecProvider;
82    use reth_evm_ethereum::EthEvmConfig;
83    use reth_network_api::noop::NoopNetwork;
84    use reth_provider::test_utils::NoopProvider;
85    use reth_rpc_eth_api::helpers::EthTransactions;
86    use reth_transaction_pool::{test_utils::testing_pool, TransactionPool};
87
88    #[tokio::test]
89    async fn send_raw_transaction() {
90        let noop_provider = NoopProvider::default();
91        let noop_network_provider = NoopNetwork::default();
92
93        let pool = testing_pool();
94
95        let evm_config = EthEvmConfig::new(noop_provider.chain_spec());
96        let eth_api =
97            EthApi::builder(noop_provider.clone(), pool.clone(), noop_network_provider, evm_config)
98                .build();
99
100        // https://etherscan.io/tx/0xa694b71e6c128a2ed8e2e0f6770bddbe52e3bb8f10e8472f9a79ab81497a8b5d
101        let tx_1 = Bytes::from(hex!(
102            "02f871018303579880850555633d1b82520894eee27662c2b8eba3cd936a23f039f3189633e4c887ad591c62bdaeb180c080a07ea72c68abfb8fca1bd964f0f99132ed9280261bdca3e549546c0205e800f7d0a05b4ef3039e9c9b9babc179a1878fb825b5aaf5aed2fa8744854150157b08d6f3"
103        ));
104
105        let tx_1_result = eth_api.send_raw_transaction(tx_1).await.unwrap();
106        assert_eq!(
107            pool.len(),
108            1,
109            "expect 1 transaction in the pool, but pool size is {}",
110            pool.len()
111        );
112
113        // https://etherscan.io/tx/0x48816c2f32c29d152b0d86ff706f39869e6c1f01dc2fe59a3c1f9ecf39384694
114        let tx_2 = Bytes::from(hex!(
115            "02f9043c018202b7843b9aca00850c807d37a08304d21d94ef1c6e67703c7bd7107eed8303fbe6ec2554bf6b881bc16d674ec80000b903c43593564c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000063e2d99f00000000000000000000000000000000000000000000000000000000000000030b000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001e0000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000001bc16d674ec80000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000065717fe021ea67801d1088cc80099004b05b64600000000000000000000000000000000000000000000000001bc16d674ec80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002bc02aaa39b223fe8d0a0e5c4f27ead9083c756cc20001f4a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000180000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009e95fd5965fd1f1a6f0d4600000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000428dca9537116148616a5a3e44035af17238fe9dc080a0c6ec1e41f5c0b9511c49b171ad4e04c6bb419c74d99fe9891d74126ec6e4e879a032069a753d7a2cfa158df95421724d24c0e9501593c09905abf3699b4a4405ce"
116        ));
117
118        let tx_2_result = eth_api.send_raw_transaction(tx_2).await.unwrap();
119        assert_eq!(
120            pool.len(),
121            2,
122            "expect 2 transactions in the pool, but pool size is {}",
123            pool.len()
124        );
125
126        assert!(pool.get(&tx_1_result).is_some(), "tx1 not found in the pool");
127        assert!(pool.get(&tx_2_result).is_some(), "tx2 not found in the pool");
128    }
129}