Skip to content

Commit

Permalink
refactor: move ValidationApi setup to EthereumAddOns (#14342)
Browse files Browse the repository at this point in the history
  • Loading branch information
klkvr authored Feb 9, 2025
1 parent b48426e commit 104bd6e
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 143 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions crates/ethereum/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ reth-evm.workspace = true
reth-evm-ethereum.workspace = true
reth-consensus.workspace = true
reth-rpc.workspace = true
reth-rpc-builder.workspace = true
reth-rpc-api.workspace = true
reth-rpc-server-types.workspace = true
reth-node-api.workspace = true
reth-chainspec.workspace = true
reth-revm = { workspace = true, features = ["std"] }
reth-trie-db.workspace = true
reth-rpc-eth-types.workspace = true

# revm with required ethereum features
revm = { workspace = true, features = ["secp256k1", "blst", "c-kzg"] }
Expand Down
107 changes: 94 additions & 13 deletions crates/ethereum/node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,31 @@ use reth_ethereum_engine_primitives::{
EthBuiltPayload, EthPayloadAttributes, EthPayloadBuilderAttributes,
};
use reth_ethereum_primitives::{EthPrimitives, PooledTransaction};
use reth_evm::execute::BasicBlockExecutorProvider;
use reth_evm::{execute::BasicBlockExecutorProvider, ConfigureEvm};
use reth_evm_ethereum::execute::EthExecutionStrategyFactory;
use reth_network::{EthNetworkPrimitives, NetworkHandle, PeersInfo};
use reth_node_api::{AddOnsContext, FullNodeComponents, TxTy};
use reth_node_api::{AddOnsContext, FullNodeComponents, NodeAddOns, TxTy};
use reth_node_builder::{
components::{
ComponentsBuilder, ConsensusBuilder, ExecutorBuilder, NetworkBuilder, PoolBuilder,
},
node::{FullNodeTypes, NodeTypes, NodeTypesWithEngine},
rpc::{EngineValidatorBuilder, RpcAddOns},
rpc::{EngineValidatorAddOn, EngineValidatorBuilder, RethRpcAddOns, RpcAddOns, RpcHandle},
BuilderContext, Node, NodeAdapter, NodeComponentsBuilder, PayloadTypes,
};
use reth_provider::{providers::ProviderFactoryBuilder, CanonStateSubscriptions, EthStorage};
use reth_rpc::EthApi;
use reth_rpc::{eth::core::EthApiFor, ValidationApi};
use reth_rpc_api::servers::BlockSubmissionValidationApiServer;
use reth_rpc_builder::config::RethRpcServerConfig;
use reth_rpc_eth_types::{error::FromEvmError, EthApiError};
use reth_rpc_server_types::RethRpcModule;
use reth_tracing::tracing::{debug, info};
use reth_transaction_pool::{
blobstore::DiskFileBlobStore, EthTransactionPool, PoolTransaction, TransactionPool,
TransactionValidationTaskExecutor,
};
use reth_trie_db::MerklePatriciaTrie;
use revm::primitives::TxEnv;
use std::sync::Arc;

/// Type configuration for a regular Ethereum node.
Expand Down Expand Up @@ -112,16 +117,92 @@ impl NodeTypesWithEngine for EthereumNode {
}

/// Add-ons w.r.t. l1 ethereum.
pub type EthereumAddOns<N> = RpcAddOns<
N,
EthApi<
<N as FullNodeTypes>::Provider,
<N as FullNodeComponents>::Pool,
NetworkHandle,
<N as FullNodeComponents>::Evm,
#[derive(Debug)]
pub struct EthereumAddOns<N: FullNodeComponents> {
inner: RpcAddOns<N, EthApiFor<N>, EthereumEngineValidatorBuilder>,
}

impl<N: FullNodeComponents> Default for EthereumAddOns<N> {
fn default() -> Self {
Self { inner: Default::default() }
}
}

impl<N> NodeAddOns<N> for EthereumAddOns<N>
where
N: FullNodeComponents<
Types: NodeTypesWithEngine<
ChainSpec = ChainSpec,
Primitives = EthPrimitives,
Engine = EthEngineTypes,
>,
Evm: ConfigureEvm<TxEnv = TxEnv>,
>,
EthereumEngineValidatorBuilder,
>;
EthApiError: FromEvmError<N::Evm>,
{
type Handle = RpcHandle<N, EthApiFor<N>>;

async fn launch_add_ons(
self,
ctx: reth_node_api::AddOnsContext<'_, N>,
) -> eyre::Result<Self::Handle> {
let validation_api = ValidationApi::new(
ctx.node.provider().clone(),
Arc::new(ctx.node.consensus().clone()),
ctx.node.block_executor().clone(),
ctx.config.rpc.flashbots_config(),
Box::new(ctx.node.task_executor().clone()),
Arc::new(EthereumEngineValidator::new(ctx.config.chain.clone())),
);

self.inner
.launch_add_ons_with(ctx, move |modules, _| {
modules.merge_if_module_configured(
RethRpcModule::Flashbots,
validation_api.into_rpc(),
)?;

Ok(())
})
.await
}
}

impl<N> RethRpcAddOns<N> for EthereumAddOns<N>
where
N: FullNodeComponents<
Types: NodeTypesWithEngine<
ChainSpec = ChainSpec,
Primitives = EthPrimitives,
Engine = EthEngineTypes,
>,
Evm: ConfigureEvm<TxEnv = TxEnv>,
>,
EthApiError: FromEvmError<N::Evm>,
{
type EthApi = EthApiFor<N>;

fn hooks_mut(&mut self) -> &mut reth_node_builder::rpc::RpcHooks<N, Self::EthApi> {
self.inner.hooks_mut()
}
}

impl<N> EngineValidatorAddOn<N> for EthereumAddOns<N>
where
N: FullNodeComponents<
Types: NodeTypesWithEngine<
ChainSpec = ChainSpec,
Primitives = EthPrimitives,
Engine = EthEngineTypes,
>,
>,
{
type Validator = EthereumEngineValidator;

async fn engine_validator(&self, ctx: &AddOnsContext<'_, N>) -> eyre::Result<Self::Validator> {
EthereumEngineValidatorBuilder::default().build(ctx).await
}
}

impl<N> Node<N> for EthereumNode
where
Expand Down
17 changes: 3 additions & 14 deletions crates/node/builder/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use reth_node_core::{
version::{CARGO_PKG_VERSION, CLIENT_CODE, NAME_CLIENT, VERGEN_GIT_SHA},
};
use reth_payload_builder::PayloadStore;
use reth_primitives::EthPrimitives;
use reth_provider::ChainSpecProvider;
use reth_rpc::{
eth::{EthApiTypes, FullEthApiServer},
Expand All @@ -35,7 +34,6 @@ use reth_rpc_engine_api::{capabilities::EngineCapabilities, EngineApi};
use reth_tasks::TaskExecutor;
use reth_tokio_util::EventSender;
use reth_tracing::tracing::{debug, info};
use std::sync::Arc;

/// Contains the handles to the spawned RPC servers.
///
Expand Down Expand Up @@ -473,12 +471,7 @@ where
.with_evm_config(node.evm_config().clone())
.with_block_executor(node.block_executor().clone())
.with_consensus(node.consensus().clone())
.build_with_auth_server(
module_config,
engine_api,
eth_api_builder,
Arc::new(engine_validator),
);
.build_with_auth_server(module_config, engine_api, eth_api_builder);

// in dev mode we generate 20 random dev-signer accounts
if config.dev.dev {
Expand Down Expand Up @@ -591,12 +584,8 @@ pub trait EthApiBuilder<N: FullNodeComponents>: 'static {
fn build(ctx: &EthApiBuilderCtx<N>) -> Self;
}

impl<
N: FullNodeComponents<
Provider: ChainSpecProvider,
Types: NodeTypes<Primitives = EthPrimitives>,
>,
> EthApiBuilder<N> for EthApi<N::Provider, N::Pool, N::Network, N::Evm>
impl<N: FullNodeComponents<Provider: ChainSpecProvider>> EthApiBuilder<N>
for EthApi<N::Provider, N::Pool, N::Network, N::Evm>
{
fn build(ctx: &EthApiBuilderCtx<N>) -> Self {
Self::with_spawner(ctx)
Expand Down
2 changes: 1 addition & 1 deletion crates/rpc/rpc-builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ reth-rpc-server-types.workspace = true
reth-tasks = { workspace = true, features = ["rayon"] }
reth-transaction-pool.workspace = true
reth-evm.workspace = true
reth-engine-primitives.workspace = true

# rpc/net
jsonrpsee = { workspace = true, features = ["server"] }
Expand Down Expand Up @@ -66,6 +65,7 @@ reth-tracing.workspace = true
reth-transaction-pool = { workspace = true, features = ["test-utils"] }
reth-rpc-types-compat.workspace = true
reth-primitives.workspace = true
reth-engine-primitives.workspace = true

alloy-primitives.workspace = true
alloy-rpc-types-eth.workspace = true
Expand Down
Loading

0 comments on commit 104bd6e

Please sign in to comment.