diff --git a/crates/ethereum/node/src/node.rs b/crates/ethereum/node/src/node.rs index ccaf9e209a..202c496d33 100644 --- a/crates/ethereum/node/src/node.rs +++ b/crates/ethereum/node/src/node.rs @@ -5,7 +5,6 @@ use crate::{EthEngineTypes, EthEvmConfig}; use alloy_eips::{eip7840::BlobParams, merge::EPOCH_SLOTS}; use alloy_network::Ethereum; use alloy_rpc_types_engine::ExecutionData; -use alloy_rpc_types_eth::TransactionRequest; use reth_chainspec::{ChainSpec, EthChainSpec, EthereumHardforks, Hardforks}; use reth_engine_local::LocalPayloadAttributesBuilder; use reth_engine_primitives::EngineTypes; @@ -42,7 +41,9 @@ use reth_rpc::{ }; use reth_rpc_api::servers::BlockSubmissionValidationApiServer; use reth_rpc_builder::{config::RethRpcServerConfig, middleware::RethRpcMiddleware}; -use reth_rpc_eth_api::{helpers::pending_block::BuildPendingEnv, RpcConvert, SignableTxRequest}; +use reth_rpc_eth_api::{ + helpers::pending_block::BuildPendingEnv, RpcConvert, RpcTypes, SignableTxRequest, +}; use reth_rpc_eth_types::{error::FromEvmError, EthApiError}; use reth_rpc_server_types::RethRpcModule; use reth_tracing::tracing::{debug, info}; @@ -52,7 +53,7 @@ use reth_transaction_pool::{ }; use reth_trie_db::MerklePatriciaTrie; use revm::context::TxEnv; -use std::{default::Default, sync::Arc, time::SystemTime}; +use std::{default::Default, marker::PhantomData, sync::Arc, time::SystemTime}; /// Type configuration for a regular Ethereum node. #[derive(Debug, Default, Clone, Copy)] @@ -136,28 +137,34 @@ impl NodeTypes for EthereumNode { } /// Builds [`EthApi`](reth_rpc::EthApi) for Ethereum. -#[derive(Debug, Default)] -pub struct EthereumEthApiBuilder; +#[derive(Debug)] +pub struct EthereumEthApiBuilder(PhantomData); -impl EthApiBuilder for EthereumEthApiBuilder +impl Default for EthereumEthApiBuilder { + fn default() -> Self { + Self(Default::default()) + } +} + +impl EthApiBuilder for EthereumEthApiBuilder where N: FullNodeComponents< Types: NodeTypes, Evm: ConfigureEvm>>, >, - EthRpcConverterFor: RpcConvert< + NetworkT: RpcTypes>>, + EthRpcConverterFor: RpcConvert< Primitives = PrimitivesTy, TxEnv = TxEnvFor, Error = EthApiError, - Network = Ethereum, + Network = NetworkT, >, - TransactionRequest: SignableTxRequest>, EthApiError: FromEvmError, { - type EthApi = EthApiFor; + type EthApi = EthApiFor; async fn build_eth_api(self, ctx: EthApiCtx<'_, N>) -> eyre::Result { - Ok(ctx.eth_api_builder().build()) + Ok(ctx.eth_api_builder().map_converter(|r| r.with_network()).build()) } } @@ -181,7 +188,7 @@ where fn default() -> Self { Self { inner: RpcAddOns::new( - EthereumEthApiBuilder, + EthereumEthApiBuilder::default(), EthereumEngineValidatorBuilder::default(), BasicEngineApiBuilder::default(), Default::default(), diff --git a/crates/rpc/rpc-convert/src/transaction.rs b/crates/rpc/rpc-convert/src/transaction.rs index 2654bde047..302b8b10f1 100644 --- a/crates/rpc/rpc-convert/src/transaction.rs +++ b/crates/rpc/rpc-convert/src/transaction.rs @@ -387,7 +387,7 @@ impl TryIntoTxEnv for TransactionRequest { #[error("Failed to convert transaction into RPC response: {0}")] pub struct TransactionConversionError(String); -/// Generic RPC response object converter for `Evm` and network `E`. +/// Generic RPC response object converter for `Evm` and network `Network`. /// /// The main purpose of this struct is to provide an implementation of [`RpcConvert`] for generic /// associated types. This struct can then be used for conversions in RPC method handlers. @@ -402,41 +402,61 @@ pub struct TransactionConversionError(String); /// is [`TransactionInfo`] then `()` can be used as `Map` which trivially passes over the input /// object. #[derive(Debug)] -pub struct RpcConverter { - phantom: PhantomData<(E, Evm)>, +pub struct RpcConverter { + network: PhantomData, + evm: PhantomData, receipt_converter: Receipt, header_converter: Header, mapper: Map, } -impl RpcConverter { +impl RpcConverter { /// Creates a new [`RpcConverter`] with `receipt_converter` and `mapper`. pub const fn new(receipt_converter: Receipt) -> Self { - Self { phantom: PhantomData, receipt_converter, header_converter: (), mapper: () } + Self { + network: PhantomData, + evm: PhantomData, + receipt_converter, + header_converter: (), + mapper: (), + } } } -impl RpcConverter { +impl RpcConverter { + /// Converts the network type + pub fn with_network(self) -> RpcConverter { + let Self { receipt_converter, header_converter, mapper, evm, .. } = self; + RpcConverter { + receipt_converter, + header_converter, + mapper, + network: Default::default(), + evm, + } + } + /// Configures the header converter. pub fn with_header_converter( self, header_converter: HeaderNew, - ) -> RpcConverter { - let Self { receipt_converter, header_converter: _, mapper, phantom } = self; - RpcConverter { receipt_converter, header_converter, mapper, phantom } + ) -> RpcConverter { + let Self { receipt_converter, header_converter: _, mapper, network, evm } = self; + RpcConverter { receipt_converter, header_converter, mapper, network, evm } } /// Configures the mapper. pub fn with_mapper( self, mapper: MapNew, - ) -> RpcConverter { - let Self { receipt_converter, header_converter, mapper: _, phantom } = self; - RpcConverter { receipt_converter, header_converter, mapper, phantom } + ) -> RpcConverter { + let Self { receipt_converter, header_converter, mapper: _, network, evm } = self; + RpcConverter { receipt_converter, header_converter, mapper, network, evm } } } -impl Default for RpcConverter +impl Default + for RpcConverter where Receipt: Default, Header: Default, @@ -444,7 +464,8 @@ where { fn default() -> Self { Self { - phantom: PhantomData, + network: Default::default(), + evm: Default::default(), receipt_converter: Default::default(), header_converter: Default::default(), mapper: Default::default(), @@ -452,12 +473,13 @@ where } } -impl Clone - for RpcConverter +impl Clone + for RpcConverter { fn clone(&self) -> Self { Self { - phantom: PhantomData, + network: Default::default(), + evm: Default::default(), receipt_converter: self.receipt_converter.clone(), header_converter: self.header_converter.clone(), mapper: self.mapper.clone(), @@ -465,18 +487,19 @@ impl Clone } } -impl RpcConvert for RpcConverter +impl RpcConvert + for RpcConverter where N: NodePrimitives, - E: RpcTypes + Send + Sync + Unpin + Clone + Debug, + Network: RpcTypes + Send + Sync + Unpin + Clone + Debug, Evm: ConfigureEvm + 'static, - TxTy: IntoRpcTx + Clone + Debug, - RpcTxReq: TryIntoSimTx> + TryIntoTxEnv>, + TxTy: IntoRpcTx + Clone + Debug, + RpcTxReq: TryIntoSimTx> + TryIntoTxEnv>, Receipt: ReceiptConverter< N, - RpcReceipt = RpcReceipt, + RpcReceipt = RpcReceipt, Error: From - + From< as TryIntoTxEnv>>::Err> + + From< as TryIntoTxEnv>>::Err> + for<'a> From<>>::Err> + Error + Unpin @@ -488,10 +511,10 @@ where + Unpin + Clone + Debug, - Header: HeaderConverter, RpcHeader>, + Header: HeaderConverter, RpcHeader>, Map: for<'a> TxInfoMapper< &'a TxTy, - Out = as IntoRpcTx>::TxInfo, + Out = as IntoRpcTx>::TxInfo, > + Clone + Debug + Unpin @@ -500,7 +523,7 @@ where + 'static, { type Primitives = N; - type Network = E; + type Network = Network; type TxEnv = TxEnvFor; type Error = Receipt::Error; @@ -508,20 +531,23 @@ where &self, tx: Recovered>, tx_info: TransactionInfo, - ) -> Result { + ) -> Result { let (tx, signer) = tx.into_parts(); let tx_info = self.mapper.try_map(&tx, tx_info)?; Ok(tx.into_rpc_tx(signer, tx_info)) } - fn build_simulate_v1_transaction(&self, request: RpcTxReq) -> Result, Self::Error> { + fn build_simulate_v1_transaction( + &self, + request: RpcTxReq, + ) -> Result, Self::Error> { Ok(request.try_into_sim_tx().map_err(|e| TransactionConversionError(e.to_string()))?) } fn tx_env( &self, - request: RpcTxReq, + request: RpcTxReq, cfg_env: &CfgEnv, block_env: &BlockEnv, ) -> Result { diff --git a/crates/rpc/rpc-eth-types/src/receipt.rs b/crates/rpc/rpc-eth-types/src/receipt.rs index 37700ffcd1..4ea4ad1daf 100644 --- a/crates/rpc/rpc-eth-types/src/receipt.rs +++ b/crates/rpc/rpc-eth-types/src/receipt.rs @@ -1,5 +1,6 @@ //! RPC receipt response builder, extends a layer one receipt with layer two data. +use crate::EthApiError; use alloy_consensus::{ReceiptEnvelope, Transaction, TxReceipt}; use alloy_eips::eip7840::BlobParams; use alloy_primitives::{Address, TxKind}; @@ -10,8 +11,6 @@ use reth_primitives_traits::NodePrimitives; use reth_rpc_convert::transaction::{ConvertReceiptInput, ReceiptConverter}; use std::{borrow::Cow, sync::Arc}; -use crate::EthApiError; - /// Builds an [`TransactionReceipt`] obtaining the inner receipt envelope from the given closure. pub fn build_receipt( input: &ConvertReceiptInput<'_, N>, @@ -88,8 +87,8 @@ where N: NodePrimitives, ChainSpec: EthChainSpec + 'static, { - type Error = EthApiError; type RpcReceipt = TransactionReceipt; + type Error = EthApiError; fn convert_receipts( &self, diff --git a/crates/rpc/rpc/src/eth/builder.rs b/crates/rpc/rpc/src/eth/builder.rs index 283722701c..2e6a6dcf91 100644 --- a/crates/rpc/rpc/src/eth/builder.rs +++ b/crates/rpc/rpc/src/eth/builder.rs @@ -57,6 +57,47 @@ where } } +impl EthApiBuilder { + /// Converts the RPC converter type of this builder + pub fn map_converter(self, f: F) -> EthApiBuilder + where + F: FnOnce(Rpc) -> R, + { + let Self { + components, + rpc_converter, + gas_cap, + max_simulate_blocks, + eth_proof_window, + fee_history_cache_config, + proof_permits, + eth_state_cache_config, + eth_cache, + gas_oracle_config, + gas_oracle, + blocking_task_pool, + task_spawner, + next_env, + } = self; + EthApiBuilder { + components, + rpc_converter: f(rpc_converter), + gas_cap, + max_simulate_blocks, + eth_proof_window, + fee_history_cache_config, + proof_permits, + eth_state_cache_config, + eth_cache, + gas_oracle_config, + gas_oracle, + blocking_task_pool, + task_spawner, + next_env, + } + } +} + impl EthApiBuilder>> where N: RpcNodeCore>, diff --git a/crates/rpc/rpc/src/eth/core.rs b/crates/rpc/rpc/src/eth/core.rs index a5fa5d3f65..ffad40b011 100644 --- a/crates/rpc/rpc/src/eth/core.rs +++ b/crates/rpc/rpc/src/eth/core.rs @@ -34,17 +34,18 @@ use tokio::sync::{broadcast, Mutex}; const DEFAULT_BROADCAST_CAPACITY: usize = 2000; /// Helper type alias for [`RpcConverter`] with components from the given [`FullNodeComponents`]. -pub type EthRpcConverterFor = RpcConverter< - Ethereum, +pub type EthRpcConverterFor = RpcConverter< + NetworkT, ::Evm, EthReceiptConverter<<::Provider as ChainSpecProvider>::ChainSpec>, >; /// Helper type alias for [`EthApi`] with components from the given [`FullNodeComponents`]. -pub type EthApiFor = EthApi>; +pub type EthApiFor = EthApi>; /// Helper type alias for [`EthApi`] with components from the given [`FullNodeComponents`]. -pub type EthApiBuilderFor = EthApiBuilder>; +pub type EthApiBuilderFor = + EthApiBuilder>; /// `Eth` API implementation. ///