Skip to main content

reth_e2e_test_utils/
rpc.rs

1use alloy_consensus::{EthereumTxEnvelope, TxEip4844Variant};
2use alloy_eips::eip7594::BlobTransactionSidecarVariant;
3use alloy_network::eip2718::Decodable2718;
4use alloy_primitives::{Bytes, B256};
5use reth_chainspec::EthereumHardforks;
6use reth_node_api::{BlockTy, FullNodeComponents};
7use reth_node_builder::{rpc::RpcRegistry, NodeTypes};
8use reth_provider::BlockReader;
9use reth_rpc_api::DebugApiServer;
10use reth_rpc_eth_api::{
11    helpers::{EthApiSpec, EthTransactions, LoadReceipt, TraceExt},
12    EthApiTypes, RpcReceipt,
13};
14
15#[expect(missing_debug_implementations)]
16pub struct RpcTestContext<Node: FullNodeComponents, EthApi: EthApiTypes> {
17    pub inner: RpcRegistry<Node, EthApi>,
18}
19
20impl<Node, EthApi> RpcTestContext<Node, EthApi>
21where
22    Node: FullNodeComponents<Types: NodeTypes<ChainSpec: EthereumHardforks>>,
23    EthApi: EthApiSpec<Provider: BlockReader<Block = BlockTy<Node::Types>>>
24        + EthTransactions
25        + TraceExt,
26{
27    /// Injects a raw transaction into the node tx pool via RPC server
28    pub async fn inject_tx(&self, raw_tx: Bytes) -> Result<B256, EthApi::Error> {
29        let eth_api = self.inner.eth_api();
30        eth_api.send_raw_transaction(raw_tx).await
31    }
32
33    /// Retrieves an Ethereum transaction envelope by its hash, including its blob sidecar if the
34    /// transaction is still pooled.
35    pub async fn envelope_by_hash(
36        &self,
37        hash: B256,
38    ) -> eyre::Result<EthereumTxEnvelope<TxEip4844Variant<BlobTransactionSidecarVariant>>> {
39        self.decoded_transaction_by_hash(hash).await
40    }
41
42    /// Retrieves the raw transaction with the given hash via `debug_getRawTransaction` and decodes
43    /// it as `T`.
44    ///
45    /// Returns an error if the node does not know the transaction.
46    pub async fn decoded_transaction_by_hash<T: Decodable2718>(
47        &self,
48        hash: B256,
49    ) -> eyre::Result<T> {
50        let tx = self
51            .inner
52            .debug_api()
53            .raw_transaction(hash)
54            .await?
55            .ok_or_else(|| eyre::eyre!("transaction {hash} not found"))?;
56        Ok(T::decode_2718_exact(&tx)?)
57    }
58
59    /// Returns the RPC receipt of the transaction with the given hash, if it is included in a
60    /// canonical block.
61    pub async fn transaction_receipt(
62        &self,
63        hash: B256,
64    ) -> Result<Option<RpcReceipt<EthApi::NetworkTypes>>, EthApi::Error>
65    where
66        EthApi: LoadReceipt + 'static,
67    {
68        self.inner.eth_api().transaction_receipt(hash).await
69    }
70}