1pub use crate::{payload::EthereumPayloadBuilder, EthereumEngineValidator};
4use crate::{EthEngineTypes, EthEvmConfig};
5use alloy_eips::{eip7840::BlobParams, merge::EPOCH_SLOTS};
6use alloy_network::Ethereum;
7use alloy_rpc_types_engine::ExecutionData;
8use reth_chainspec::{ChainSpec, EthChainSpec, EthereumHardforks, Hardforks};
9use reth_engine_local::LocalPayloadAttributesBuilder;
10use reth_engine_primitives::EngineTypes;
11use reth_ethereum_consensus::EthBeaconConsensus;
12use reth_ethereum_engine_primitives::{
13 EthBuiltPayload, EthPayloadAttributes, EthPayloadBuilderAttributes,
14};
15use reth_ethereum_primitives::{EthPrimitives, TransactionSigned};
16use reth_evm::{
17 eth::spec::EthExecutorSpec, ConfigureEvm, EvmFactory, EvmFactoryFor, NextBlockEnvAttributes,
18};
19use reth_network::{primitives::BasicNetworkPrimitives, NetworkHandle, PeersInfo};
20use reth_node_api::{
21 AddOnsContext, FullNodeComponents, HeaderTy, NodeAddOns, NodePrimitives,
22 PayloadAttributesBuilder, PrimitivesTy, TxTy,
23};
24use reth_node_builder::{
25 components::{
26 BasicPayloadServiceBuilder, ComponentsBuilder, ConsensusBuilder, ExecutorBuilder,
27 NetworkBuilder, PoolBuilder, TxPoolBuilder,
28 },
29 node::{FullNodeTypes, NodeTypes},
30 rpc::{
31 BasicEngineApiBuilder, BasicEngineValidatorBuilder, EngineApiBuilder, EngineValidatorAddOn,
32 EngineValidatorBuilder, EthApiBuilder, EthApiCtx, Identity, PayloadValidatorBuilder,
33 RethRpcAddOns, RpcAddOns, RpcHandle,
34 },
35 BuilderContext, DebugNode, Node, NodeAdapter,
36};
37use reth_payload_primitives::PayloadTypes;
38use reth_provider::{providers::ProviderFactoryBuilder, EthStorage};
39use reth_rpc::{
40 eth::core::{EthApiFor, EthRpcConverterFor},
41 TestingApi, ValidationApi,
42};
43use reth_rpc_api::servers::{BlockSubmissionValidationApiServer, TestingApiServer};
44use reth_rpc_builder::{config::RethRpcServerConfig, middleware::RethRpcMiddleware};
45use reth_rpc_eth_api::{
46 helpers::{
47 config::{EthConfigApiServer, EthConfigHandler},
48 pending_block::BuildPendingEnv,
49 },
50 RpcConvert, RpcTypes, SignableTxRequest,
51};
52use reth_rpc_eth_types::{error::FromEvmError, EthApiError};
53use reth_rpc_server_types::RethRpcModule;
54use reth_tracing::tracing::{debug, info};
55use reth_transaction_pool::{
56 blobstore::DiskFileBlobStore, EthTransactionPool, PoolPooledTx, PoolTransaction,
57 TransactionPool, TransactionValidationTaskExecutor,
58};
59use revm::context::TxEnv;
60use std::{marker::PhantomData, sync::Arc, time::SystemTime};
61
62#[derive(Debug, Default, Clone, Copy)]
64#[non_exhaustive]
65pub struct EthereumNode;
66
67impl EthereumNode {
68 pub fn components<Node>() -> ComponentsBuilder<
70 Node,
71 EthereumPoolBuilder,
72 BasicPayloadServiceBuilder<EthereumPayloadBuilder>,
73 EthereumNetworkBuilder,
74 EthereumExecutorBuilder,
75 EthereumConsensusBuilder,
76 >
77 where
78 Node: FullNodeTypes<
79 Types: NodeTypes<
80 ChainSpec: Hardforks + EthereumHardforks + EthExecutorSpec,
81 Primitives = EthPrimitives,
82 >,
83 >,
84 <Node::Types as NodeTypes>::Payload: PayloadTypes<
85 BuiltPayload = EthBuiltPayload,
86 PayloadAttributes = EthPayloadAttributes,
87 PayloadBuilderAttributes = EthPayloadBuilderAttributes,
88 >,
89 {
90 ComponentsBuilder::default()
91 .node_types::<Node>()
92 .pool(EthereumPoolBuilder::default())
93 .executor(EthereumExecutorBuilder::default())
94 .payload(BasicPayloadServiceBuilder::default())
95 .network(EthereumNetworkBuilder::default())
96 .consensus(EthereumConsensusBuilder::default())
97 }
98
99 pub fn provider_factory_builder() -> ProviderFactoryBuilder<Self> {
132 ProviderFactoryBuilder::default()
133 }
134}
135
136impl NodeTypes for EthereumNode {
137 type Primitives = EthPrimitives;
138 type ChainSpec = ChainSpec;
139 type Storage = EthStorage;
140 type Payload = EthEngineTypes;
141}
142
143#[derive(Debug)]
145pub struct EthereumEthApiBuilder<NetworkT = Ethereum>(PhantomData<NetworkT>);
146
147impl<NetworkT> Default for EthereumEthApiBuilder<NetworkT> {
148 fn default() -> Self {
149 Self(Default::default())
150 }
151}
152
153impl<N, NetworkT> EthApiBuilder<N> for EthereumEthApiBuilder<NetworkT>
154where
155 N: FullNodeComponents<
156 Types: NodeTypes<ChainSpec: Hardforks + EthereumHardforks>,
157 Evm: ConfigureEvm<NextBlockEnvCtx: BuildPendingEnv<HeaderTy<N::Types>>>,
158 >,
159 NetworkT: RpcTypes<TransactionRequest: SignableTxRequest<TxTy<N::Types>>>,
160 EthRpcConverterFor<N, NetworkT>: RpcConvert<
161 Primitives = PrimitivesTy<N::Types>,
162 Error = EthApiError,
163 Network = NetworkT,
164 Evm = N::Evm,
165 >,
166 EthApiError: FromEvmError<N::Evm>,
167{
168 type EthApi = EthApiFor<N, NetworkT>;
169
170 async fn build_eth_api(self, ctx: EthApiCtx<'_, N>) -> eyre::Result<Self::EthApi> {
171 Ok(ctx.eth_api_builder().map_converter(|r| r.with_network()).build())
172 }
173}
174
175#[derive(Debug)]
177pub struct EthereumAddOns<
178 N: FullNodeComponents,
179 EthB: EthApiBuilder<N>,
180 PVB,
181 EB = BasicEngineApiBuilder<PVB>,
182 EVB = BasicEngineValidatorBuilder<PVB>,
183 RpcMiddleware = Identity,
184> {
185 inner: RpcAddOns<N, EthB, PVB, EB, EVB, RpcMiddleware>,
186}
187
188impl<N, EthB, PVB, EB, EVB, RpcMiddleware> EthereumAddOns<N, EthB, PVB, EB, EVB, RpcMiddleware>
189where
190 N: FullNodeComponents,
191 EthB: EthApiBuilder<N>,
192{
193 pub const fn new(inner: RpcAddOns<N, EthB, PVB, EB, EVB, RpcMiddleware>) -> Self {
195 Self { inner }
196 }
197}
198
199impl<N> Default for EthereumAddOns<N, EthereumEthApiBuilder, EthereumEngineValidatorBuilder>
200where
201 N: FullNodeComponents<
202 Types: NodeTypes<
203 ChainSpec: EthereumHardforks + Clone + 'static,
204 Payload: EngineTypes<ExecutionData = ExecutionData>
205 + PayloadTypes<PayloadAttributes = EthPayloadAttributes>,
206 Primitives = EthPrimitives,
207 >,
208 >,
209 EthereumEthApiBuilder: EthApiBuilder<N>,
210{
211 fn default() -> Self {
212 Self::new(RpcAddOns::new(
213 EthereumEthApiBuilder::default(),
214 EthereumEngineValidatorBuilder::default(),
215 BasicEngineApiBuilder::default(),
216 BasicEngineValidatorBuilder::default(),
217 Default::default(),
218 ))
219 }
220}
221
222impl<N, EthB, PVB, EB, EVB, RpcMiddleware> EthereumAddOns<N, EthB, PVB, EB, EVB, RpcMiddleware>
223where
224 N: FullNodeComponents,
225 EthB: EthApiBuilder<N>,
226{
227 pub fn with_engine_api<T>(
229 self,
230 engine_api_builder: T,
231 ) -> EthereumAddOns<N, EthB, PVB, T, EVB, RpcMiddleware>
232 where
233 T: Send,
234 {
235 let Self { inner } = self;
236 EthereumAddOns::new(inner.with_engine_api(engine_api_builder))
237 }
238
239 pub fn with_payload_validator<V, T>(
241 self,
242 payload_validator_builder: T,
243 ) -> EthereumAddOns<N, EthB, T, EB, EVB, RpcMiddleware> {
244 let Self { inner } = self;
245 EthereumAddOns::new(inner.with_payload_validator(payload_validator_builder))
246 }
247
248 pub fn with_rpc_middleware<T>(
250 self,
251 rpc_middleware: T,
252 ) -> EthereumAddOns<N, EthB, PVB, EB, EVB, T>
253 where
254 T: Send,
255 {
256 let Self { inner } = self;
257 EthereumAddOns::new(inner.with_rpc_middleware(rpc_middleware))
258 }
259
260 pub fn with_tokio_runtime(self, tokio_runtime: Option<tokio::runtime::Handle>) -> Self {
264 let Self { inner } = self;
265 Self { inner: inner.with_tokio_runtime(tokio_runtime) }
266 }
267}
268
269impl<N, EthB, PVB, EB, EVB, RpcMiddleware> NodeAddOns<N>
270 for EthereumAddOns<N, EthB, PVB, EB, EVB, RpcMiddleware>
271where
272 N: FullNodeComponents<
273 Types: NodeTypes<
274 ChainSpec: Hardforks + EthereumHardforks,
275 Primitives = EthPrimitives,
276 Payload: EngineTypes<ExecutionData = ExecutionData>,
277 >,
278 Evm: ConfigureEvm<NextBlockEnvCtx = NextBlockEnvAttributes>,
279 >,
280 EthB: EthApiBuilder<N>,
281 PVB: Send,
282 EB: EngineApiBuilder<N>,
283 EVB: EngineValidatorBuilder<N>,
284 EthApiError: FromEvmError<N::Evm>,
285 EvmFactoryFor<N::Evm>: EvmFactory<Tx = TxEnv>,
286 RpcMiddleware: RethRpcMiddleware,
287{
288 type Handle = RpcHandle<N, EthB::EthApi>;
289
290 async fn launch_add_ons(
291 self,
292 ctx: reth_node_api::AddOnsContext<'_, N>,
293 ) -> eyre::Result<Self::Handle> {
294 let validation_api = ValidationApi::<_, _, <N::Types as NodeTypes>::Payload>::new(
295 ctx.node.provider().clone(),
296 Arc::new(ctx.node.consensus().clone()),
297 ctx.node.evm_config().clone(),
298 ctx.config.rpc.flashbots_config(),
299 Box::new(ctx.node.task_executor().clone()),
300 Arc::new(EthereumEngineValidator::new(ctx.config.chain.clone())),
301 );
302
303 let eth_config =
304 EthConfigHandler::new(ctx.node.provider().clone(), ctx.node.evm_config().clone());
305
306 self.inner
307 .launch_add_ons_with(ctx, move |container| {
308 container.modules.merge_if_module_configured(
309 RethRpcModule::Flashbots,
310 validation_api.into_rpc(),
311 )?;
312
313 container
314 .modules
315 .merge_if_module_configured(RethRpcModule::Eth, eth_config.into_rpc())?;
316
317 let testing_api = TestingApi::new(
320 container.registry.eth_api().clone(),
321 container.registry.evm_config().clone(),
322 )
323 .into_rpc();
324 container
325 .modules
326 .merge_if_module_configured(RethRpcModule::Testing, testing_api)?;
327
328 Ok(())
329 })
330 .await
331 }
332}
333
334impl<N, EthB, PVB, EB, EVB, RpcMiddleware> RethRpcAddOns<N>
335 for EthereumAddOns<N, EthB, PVB, EB, EVB, RpcMiddleware>
336where
337 N: FullNodeComponents<
338 Types: NodeTypes<
339 ChainSpec: Hardforks + EthereumHardforks,
340 Primitives = EthPrimitives,
341 Payload: EngineTypes<ExecutionData = ExecutionData>,
342 >,
343 Evm: ConfigureEvm<NextBlockEnvCtx = NextBlockEnvAttributes>,
344 >,
345 EthB: EthApiBuilder<N>,
346 PVB: PayloadValidatorBuilder<N>,
347 EB: EngineApiBuilder<N>,
348 EVB: EngineValidatorBuilder<N>,
349 EthApiError: FromEvmError<N::Evm>,
350 EvmFactoryFor<N::Evm>: EvmFactory<Tx = TxEnv>,
351 RpcMiddleware: RethRpcMiddleware,
352{
353 type EthApi = EthB::EthApi;
354
355 fn hooks_mut(&mut self) -> &mut reth_node_builder::rpc::RpcHooks<N, Self::EthApi> {
356 self.inner.hooks_mut()
357 }
358}
359
360impl<N, EthB, PVB, EB, EVB, RpcMiddleware> EngineValidatorAddOn<N>
361 for EthereumAddOns<N, EthB, PVB, EB, EVB, RpcMiddleware>
362where
363 N: FullNodeComponents<
364 Types: NodeTypes<
365 ChainSpec: EthChainSpec + EthereumHardforks,
366 Primitives = EthPrimitives,
367 Payload: EngineTypes<ExecutionData = ExecutionData>,
368 >,
369 Evm: ConfigureEvm<NextBlockEnvCtx = NextBlockEnvAttributes>,
370 >,
371 EthB: EthApiBuilder<N>,
372 PVB: Send,
373 EB: EngineApiBuilder<N>,
374 EVB: EngineValidatorBuilder<N>,
375 EthApiError: FromEvmError<N::Evm>,
376 EvmFactoryFor<N::Evm>: EvmFactory<Tx = TxEnv>,
377 RpcMiddleware: Send,
378{
379 type ValidatorBuilder = EVB;
380
381 fn engine_validator_builder(&self) -> Self::ValidatorBuilder {
382 self.inner.engine_validator_builder()
383 }
384}
385
386impl<N> Node<N> for EthereumNode
387where
388 N: FullNodeTypes<Types = Self>,
389{
390 type ComponentsBuilder = ComponentsBuilder<
391 N,
392 EthereumPoolBuilder,
393 BasicPayloadServiceBuilder<EthereumPayloadBuilder>,
394 EthereumNetworkBuilder,
395 EthereumExecutorBuilder,
396 EthereumConsensusBuilder,
397 >;
398
399 type AddOns =
400 EthereumAddOns<NodeAdapter<N>, EthereumEthApiBuilder, EthereumEngineValidatorBuilder>;
401
402 fn components_builder(&self) -> Self::ComponentsBuilder {
403 Self::components()
404 }
405
406 fn add_ons(&self) -> Self::AddOns {
407 EthereumAddOns::default()
408 }
409}
410
411impl<N: FullNodeComponents<Types = Self>> DebugNode<N> for EthereumNode {
412 type RpcBlock = alloy_rpc_types_eth::Block;
413
414 fn rpc_to_primitive_block(rpc_block: Self::RpcBlock) -> reth_ethereum_primitives::Block {
415 rpc_block.into_consensus().convert_transactions()
416 }
417
418 fn local_payload_attributes_builder(
419 chain_spec: &Self::ChainSpec,
420 ) -> impl PayloadAttributesBuilder<<Self::Payload as PayloadTypes>::PayloadAttributes> {
421 LocalPayloadAttributesBuilder::new(Arc::new(chain_spec.clone()))
422 }
423}
424
425#[derive(Debug, Default, Clone, Copy)]
427#[non_exhaustive]
428pub struct EthereumExecutorBuilder;
429
430impl<Types, Node> ExecutorBuilder<Node> for EthereumExecutorBuilder
431where
432 Types: NodeTypes<
433 ChainSpec: Hardforks + EthExecutorSpec + EthereumHardforks,
434 Primitives = EthPrimitives,
435 >,
436 Node: FullNodeTypes<Types = Types>,
437{
438 type EVM = EthEvmConfig<Types::ChainSpec>;
439
440 async fn build_evm(self, ctx: &BuilderContext<Node>) -> eyre::Result<Self::EVM> {
441 Ok(EthEvmConfig::new(ctx.chain_spec()))
442 }
443}
444
445#[derive(Debug, Default, Clone, Copy)]
450#[non_exhaustive]
451pub struct EthereumPoolBuilder {
452 }
454
455impl<Types, Node> PoolBuilder<Node> for EthereumPoolBuilder
456where
457 Types: NodeTypes<
458 ChainSpec: EthereumHardforks,
459 Primitives: NodePrimitives<SignedTx = TransactionSigned>,
460 >,
461 Node: FullNodeTypes<Types = Types>,
462{
463 type Pool = EthTransactionPool<Node::Provider, DiskFileBlobStore>;
464
465 async fn build_pool(self, ctx: &BuilderContext<Node>) -> eyre::Result<Self::Pool> {
466 let pool_config = ctx.pool_config();
467
468 let blobs_disabled = ctx.config().txpool.disable_blobs_support ||
469 ctx.config().txpool.blobpool_max_count == 0;
470
471 let blob_cache_size = if let Some(blob_cache_size) = pool_config.blob_cache_size {
472 Some(blob_cache_size)
473 } else {
474 let current_timestamp =
477 SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?.as_secs();
478 let blob_params = ctx
479 .chain_spec()
480 .blob_params_at_timestamp(current_timestamp)
481 .unwrap_or_else(BlobParams::cancun);
482
483 Some((blob_params.target_blob_count * EPOCH_SLOTS * 2) as u32)
486 };
487
488 let blob_store =
489 reth_node_builder::components::create_blob_store_with_cache(ctx, blob_cache_size)?;
490
491 let validator = TransactionValidationTaskExecutor::eth_builder(ctx.provider().clone())
492 .with_head_timestamp(ctx.head().timestamp)
493 .set_eip4844(!blobs_disabled)
494 .kzg_settings(ctx.kzg_settings()?)
495 .with_max_tx_input_bytes(ctx.config().txpool.max_tx_input_bytes)
496 .with_local_transactions_config(pool_config.local_transactions_config.clone())
497 .set_tx_fee_cap(ctx.config().rpc.rpc_tx_fee_cap)
498 .with_max_tx_gas_limit(ctx.config().txpool.max_tx_gas_limit)
499 .with_minimum_priority_fee(ctx.config().txpool.minimum_priority_fee)
500 .with_additional_tasks(ctx.config().txpool.additional_validation_tasks)
501 .build_with_tasks(ctx.task_executor().clone(), blob_store.clone());
502
503 if validator.validator().eip4844() {
504 let kzg_settings = validator.validator().kzg_settings().clone();
508 ctx.task_executor().spawn_blocking(async move {
509 let _ = kzg_settings.get();
510 debug!(target: "reth::cli", "Initialized KZG settings");
511 });
512 }
513
514 let transaction_pool = TxPoolBuilder::new(ctx)
515 .with_validator(validator)
516 .build_and_spawn_maintenance_task(blob_store, pool_config)?;
517
518 info!(target: "reth::cli", "Transaction pool initialized");
519 debug!(target: "reth::cli", "Spawned txpool maintenance task");
520
521 Ok(transaction_pool)
522 }
523}
524
525#[derive(Debug, Default, Clone, Copy)]
527pub struct EthereumNetworkBuilder {
528 }
530
531impl<Node, Pool> NetworkBuilder<Node, Pool> for EthereumNetworkBuilder
532where
533 Node: FullNodeTypes<Types: NodeTypes<ChainSpec: Hardforks>>,
534 Pool: TransactionPool<Transaction: PoolTransaction<Consensus = TxTy<Node::Types>>>
535 + Unpin
536 + 'static,
537{
538 type Network =
539 NetworkHandle<BasicNetworkPrimitives<PrimitivesTy<Node::Types>, PoolPooledTx<Pool>>>;
540
541 async fn build_network(
542 self,
543 ctx: &BuilderContext<Node>,
544 pool: Pool,
545 ) -> eyre::Result<Self::Network> {
546 let network = ctx.network_builder().await?;
547 let handle = ctx.start_network(network, pool);
548 info!(target: "reth::cli", enode=%handle.local_node_record(), "P2P networking initialized");
549 Ok(handle)
550 }
551}
552
553#[derive(Debug, Default, Clone, Copy)]
555pub struct EthereumConsensusBuilder {
556 }
558
559impl<Node> ConsensusBuilder<Node> for EthereumConsensusBuilder
560where
561 Node: FullNodeTypes<
562 Types: NodeTypes<ChainSpec: EthChainSpec + EthereumHardforks, Primitives = EthPrimitives>,
563 >,
564{
565 type Consensus = Arc<EthBeaconConsensus<<Node::Types as NodeTypes>::ChainSpec>>;
566
567 async fn build_consensus(self, ctx: &BuilderContext<Node>) -> eyre::Result<Self::Consensus> {
568 Ok(Arc::new(EthBeaconConsensus::new(ctx.chain_spec())))
569 }
570}
571
572#[derive(Debug, Default, Clone)]
574#[non_exhaustive]
575pub struct EthereumEngineValidatorBuilder;
576
577impl<Node, Types> PayloadValidatorBuilder<Node> for EthereumEngineValidatorBuilder
578where
579 Types: NodeTypes<
580 ChainSpec: Hardforks + EthereumHardforks + Clone + 'static,
581 Payload: EngineTypes<ExecutionData = ExecutionData>
582 + PayloadTypes<PayloadAttributes = EthPayloadAttributes>,
583 Primitives = EthPrimitives,
584 >,
585 Node: FullNodeComponents<Types = Types>,
586{
587 type Validator = EthereumEngineValidator<Types::ChainSpec>;
588
589 async fn build(self, ctx: &AddOnsContext<'_, Node>) -> eyre::Result<Self::Validator> {
590 Ok(EthereumEngineValidator::new(ctx.config.chain.clone()))
591 }
592}