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, TraceExt},
12    EthApiTypes,
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 a transaction envelope by its hash
34    pub async fn envelope_by_hash(
35        &self,
36        hash: B256,
37    ) -> eyre::Result<EthereumTxEnvelope<TxEip4844Variant<BlobTransactionSidecarVariant>>> {
38        let tx = self.inner.debug_api().raw_transaction(hash).await?.unwrap();
39        let tx = tx.to_vec();
40        Ok(EthereumTxEnvelope::decode_2718(&mut tx.as_ref()).unwrap())
41    }
42}