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> {
131 ProviderFactoryBuilder::default()
132 }
133}
134
135impl NodeTypes for EthereumNode {
136 type Primitives = EthPrimitives;
137 type ChainSpec = ChainSpec;
138 type Storage = EthStorage;
139 type Payload = EthEngineTypes;
140}
141
142#[derive(Debug)]
144pub struct EthereumEthApiBuilder<NetworkT = Ethereum>(PhantomData<NetworkT>);
145
146impl<NetworkT> Default for EthereumEthApiBuilder<NetworkT> {
147 fn default() -> Self {
148 Self(Default::default())
149 }
150}
151
152impl<N, NetworkT> EthApiBuilder<N> for EthereumEthApiBuilder<NetworkT>
153where
154 N: FullNodeComponents<
155 Types: NodeTypes<ChainSpec: Hardforks + EthereumHardforks>,
156 Evm: ConfigureEvm<NextBlockEnvCtx: BuildPendingEnv<HeaderTy<N::Types>>>,
157 >,
158 NetworkT: RpcTypes<TransactionRequest: SignableTxRequest<TxTy<N::Types>>>,
159 EthRpcConverterFor<N, NetworkT>: RpcConvert<
160 Primitives = PrimitivesTy<N::Types>,
161 Error = EthApiError,
162 Network = NetworkT,
163 Evm = N::Evm,
164 >,
165 EthApiError: FromEvmError<N::Evm>,
166{
167 type EthApi = EthApiFor<N, NetworkT>;
168
169 async fn build_eth_api(self, ctx: EthApiCtx<'_, N>) -> eyre::Result<Self::EthApi> {
170 Ok(ctx.eth_api_builder().map_converter(|r| r.with_network()).build())
171 }
172}
173
174#[derive(Debug)]
176pub struct EthereumAddOns<
177 N: FullNodeComponents,
178 EthB: EthApiBuilder<N>,
179 PVB,
180 EB = BasicEngineApiBuilder<PVB>,
181 EVB = BasicEngineValidatorBuilder<PVB>,
182 RpcMiddleware = Identity,
183> {
184 inner: RpcAddOns<N, EthB, PVB, EB, EVB, RpcMiddleware>,
185}
186
187impl<N, EthB, PVB, EB, EVB, RpcMiddleware> EthereumAddOns<N, EthB, PVB, EB, EVB, RpcMiddleware>
188where
189 N: FullNodeComponents,
190 EthB: EthApiBuilder<N>,
191{
192 pub const fn new(inner: RpcAddOns<N, EthB, PVB, EB, EVB, RpcMiddleware>) -> Self {
194 Self { inner }
195 }
196}
197
198impl<N> Default for EthereumAddOns<N, EthereumEthApiBuilder, EthereumEngineValidatorBuilder>
199where
200 N: FullNodeComponents<
201 Types: NodeTypes<
202 ChainSpec: EthereumHardforks + Clone + 'static,
203 Payload: EngineTypes<ExecutionData = ExecutionData>
204 + PayloadTypes<PayloadAttributes = EthPayloadAttributes>,
205 Primitives = EthPrimitives,
206 >,
207 >,
208 EthereumEthApiBuilder: EthApiBuilder<N>,
209{
210 fn default() -> Self {
211 Self::new(RpcAddOns::new(
212 EthereumEthApiBuilder::default(),
213 EthereumEngineValidatorBuilder::default(),
214 BasicEngineApiBuilder::default(),
215 BasicEngineValidatorBuilder::default(),
216 Default::default(),
217 ))
218 }
219}
220
221impl<N, EthB, PVB, EB, EVB, RpcMiddleware> EthereumAddOns<N, EthB, PVB, EB, EVB, RpcMiddleware>
222where
223 N: FullNodeComponents,
224 EthB: EthApiBuilder<N>,
225{
226 pub fn with_engine_api<T>(
228 self,
229 engine_api_builder: T,
230 ) -> EthereumAddOns<N, EthB, PVB, T, EVB, RpcMiddleware>
231 where
232 T: Send,
233 {
234 let Self { inner } = self;
235 EthereumAddOns::new(inner.with_engine_api(engine_api_builder))
236 }
237
238 pub fn with_payload_validator<V, T>(
240 self,
241 payload_validator_builder: T,
242 ) -> EthereumAddOns<N, EthB, T, EB, EVB, RpcMiddleware> {
243 let Self { inner } = self;
244 EthereumAddOns::new(inner.with_payload_validator(payload_validator_builder))
245 }
246
247 pub fn with_rpc_middleware<T>(
249 self,
250 rpc_middleware: T,
251 ) -> EthereumAddOns<N, EthB, PVB, EB, EVB, T>
252 where
253 T: Send,
254 {
255 let Self { inner } = self;
256 EthereumAddOns::new(inner.with_rpc_middleware(rpc_middleware))
257 }
258
259 pub fn with_tokio_runtime(self, tokio_runtime: Option<tokio::runtime::Handle>) -> Self {
263 let Self { inner } = self;
264 Self { inner: inner.with_tokio_runtime(tokio_runtime) }
265 }
266}
267
268impl<N, EthB, PVB, EB, EVB, RpcMiddleware> NodeAddOns<N>
269 for EthereumAddOns<N, EthB, PVB, EB, EVB, RpcMiddleware>
270where
271 N: FullNodeComponents<
272 Types: NodeTypes<
273 ChainSpec: Hardforks + EthereumHardforks,
274 Primitives = EthPrimitives,
275 Payload: EngineTypes<ExecutionData = ExecutionData>,
276 >,
277 Evm: ConfigureEvm<NextBlockEnvCtx = NextBlockEnvAttributes>,
278 >,
279 EthB: EthApiBuilder<N>,
280 PVB: Send,
281 EB: EngineApiBuilder<N>,
282 EVB: EngineValidatorBuilder<N>,
283 EthApiError: FromEvmError<N::Evm>,
284 EvmFactoryFor<N::Evm>: EvmFactory<Tx = TxEnv>,
285 RpcMiddleware: RethRpcMiddleware,
286{
287 type Handle = RpcHandle<N, EthB::EthApi>;
288
289 async fn launch_add_ons(
290 self,
291 ctx: reth_node_api::AddOnsContext<'_, N>,
292 ) -> eyre::Result<Self::Handle> {
293 let validation_api = ValidationApi::<_, _, <N::Types as NodeTypes>::Payload>::new(
294 ctx.node.provider().clone(),
295 Arc::new(ctx.node.consensus().clone()),
296 ctx.node.evm_config().clone(),
297 ctx.config.rpc.flashbots_config(),
298 Box::new(ctx.node.task_executor().clone()),
299 Arc::new(EthereumEngineValidator::new(ctx.config.chain.clone())),
300 );
301
302 let eth_config =
303 EthConfigHandler::new(ctx.node.provider().clone(), ctx.node.evm_config().clone());
304
305 self.inner
306 .launch_add_ons_with(ctx, move |container| {
307 container.modules.merge_if_module_configured(
308 RethRpcModule::Flashbots,
309 validation_api.into_rpc(),
310 )?;
311
312 container
313 .modules
314 .merge_if_module_configured(RethRpcModule::Eth, eth_config.into_rpc())?;
315
316 let testing_api = TestingApi::new(
319 container.registry.eth_api().clone(),
320 container.registry.evm_config().clone(),
321 )
322 .into_rpc();
323 container
324 .modules
325 .merge_if_module_configured(RethRpcModule::Testing, testing_api)?;
326
327 Ok(())
328 })
329 .await
330 }
331}
332
333impl<N, EthB, PVB, EB, EVB, RpcMiddleware> RethRpcAddOns<N>
334 for EthereumAddOns<N, EthB, PVB, EB, EVB, RpcMiddleware>
335where
336 N: FullNodeComponents<
337 Types: NodeTypes<
338 ChainSpec: Hardforks + EthereumHardforks,
339 Primitives = EthPrimitives,
340 Payload: EngineTypes<ExecutionData = ExecutionData>,
341 >,
342 Evm: ConfigureEvm<NextBlockEnvCtx = NextBlockEnvAttributes>,
343 >,
344 EthB: EthApiBuilder<N>,
345 PVB: PayloadValidatorBuilder<N>,
346 EB: EngineApiBuilder<N>,
347 EVB: EngineValidatorBuilder<N>,
348 EthApiError: FromEvmError<N::Evm>,
349 EvmFactoryFor<N::Evm>: EvmFactory<Tx = TxEnv>,
350 RpcMiddleware: RethRpcMiddleware,
351{
352 type EthApi = EthB::EthApi;
353
354 fn hooks_mut(&mut self) -> &mut reth_node_builder::rpc::RpcHooks<N, Self::EthApi> {
355 self.inner.hooks_mut()
356 }
357}
358
359impl<N, EthB, PVB, EB, EVB, RpcMiddleware> EngineValidatorAddOn<N>
360 for EthereumAddOns<N, EthB, PVB, EB, EVB, RpcMiddleware>
361where
362 N: FullNodeComponents<
363 Types: NodeTypes<
364 ChainSpec: EthChainSpec + EthereumHardforks,
365 Primitives = EthPrimitives,
366 Payload: EngineTypes<ExecutionData = ExecutionData>,
367 >,
368 Evm: ConfigureEvm<NextBlockEnvCtx = NextBlockEnvAttributes>,
369 >,
370 EthB: EthApiBuilder<N>,
371 PVB: Send,
372 EB: EngineApiBuilder<N>,
373 EVB: EngineValidatorBuilder<N>,
374 EthApiError: FromEvmError<N::Evm>,
375 EvmFactoryFor<N::Evm>: EvmFactory<Tx = TxEnv>,
376 RpcMiddleware: Send,
377{
378 type ValidatorBuilder = EVB;
379
380 fn engine_validator_builder(&self) -> Self::ValidatorBuilder {
381 self.inner.engine_validator_builder()
382 }
383}
384
385impl<N> Node<N> for EthereumNode
386where
387 N: FullNodeTypes<Types = Self>,
388{
389 type ComponentsBuilder = ComponentsBuilder<
390 N,
391 EthereumPoolBuilder,
392 BasicPayloadServiceBuilder<EthereumPayloadBuilder>,
393 EthereumNetworkBuilder,
394 EthereumExecutorBuilder,
395 EthereumConsensusBuilder,
396 >;
397
398 type AddOns =
399 EthereumAddOns<NodeAdapter<N>, EthereumEthApiBuilder, EthereumEngineValidatorBuilder>;
400
401 fn components_builder(&self) -> Self::ComponentsBuilder {
402 Self::components()
403 }
404
405 fn add_ons(&self) -> Self::AddOns {
406 EthereumAddOns::default()
407 }
408}
409
410impl<N: FullNodeComponents<Types = Self>> DebugNode<N> for EthereumNode {
411 type RpcBlock = alloy_rpc_types_eth::Block;
412
413 fn rpc_to_primitive_block(rpc_block: Self::RpcBlock) -> reth_ethereum_primitives::Block {
414 rpc_block.into_consensus().convert_transactions()
415 }
416
417 fn local_payload_attributes_builder(
418 chain_spec: &Self::ChainSpec,
419 ) -> impl PayloadAttributesBuilder<<Self::Payload as PayloadTypes>::PayloadAttributes> {
420 LocalPayloadAttributesBuilder::new(Arc::new(chain_spec.clone()))
421 }
422}
423
424#[derive(Debug, Default, Clone, Copy)]
426#[non_exhaustive]
427pub struct EthereumExecutorBuilder;
428
429impl<Types, Node> ExecutorBuilder<Node> for EthereumExecutorBuilder
430where
431 Types: NodeTypes<
432 ChainSpec: Hardforks + EthExecutorSpec + EthereumHardforks,
433 Primitives = EthPrimitives,
434 >,
435 Node: FullNodeTypes<Types = Types>,
436{
437 type EVM = EthEvmConfig<Types::ChainSpec>;
438
439 async fn build_evm(self, ctx: &BuilderContext<Node>) -> eyre::Result<Self::EVM> {
440 Ok(EthEvmConfig::new(ctx.chain_spec()))
441 }
442}
443
444#[derive(Debug, Default, Clone, Copy)]
449#[non_exhaustive]
450pub struct EthereumPoolBuilder {
451 }
453
454impl<Types, Node> PoolBuilder<Node> for EthereumPoolBuilder
455where
456 Types: NodeTypes<
457 ChainSpec: EthereumHardforks,
458 Primitives: NodePrimitives<SignedTx = TransactionSigned>,
459 >,
460 Node: FullNodeTypes<Types = Types>,
461{
462 type Pool = EthTransactionPool<Node::Provider, DiskFileBlobStore>;
463
464 async fn build_pool(self, ctx: &BuilderContext<Node>) -> eyre::Result<Self::Pool> {
465 let pool_config = ctx.pool_config();
466
467 let blobs_disabled = ctx.config().txpool.disable_blobs_support ||
468 ctx.config().txpool.blobpool_max_count == 0;
469
470 let blob_cache_size = if let Some(blob_cache_size) = pool_config.blob_cache_size {
471 Some(blob_cache_size)
472 } else {
473 let current_timestamp =
476 SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?.as_secs();
477 let blob_params = ctx
478 .chain_spec()
479 .blob_params_at_timestamp(current_timestamp)
480 .unwrap_or_else(BlobParams::cancun);
481
482 Some((blob_params.target_blob_count * EPOCH_SLOTS * 2) as u32)
485 };
486
487 let blob_store =
488 reth_node_builder::components::create_blob_store_with_cache(ctx, blob_cache_size)?;
489
490 let validator = TransactionValidationTaskExecutor::eth_builder(ctx.provider().clone())
491 .with_head_timestamp(ctx.head().timestamp)
492 .set_eip4844(!blobs_disabled)
493 .kzg_settings(ctx.kzg_settings()?)
494 .with_max_tx_input_bytes(ctx.config().txpool.max_tx_input_bytes)
495 .with_local_transactions_config(pool_config.local_transactions_config.clone())
496 .set_tx_fee_cap(ctx.config().rpc.rpc_tx_fee_cap)
497 .with_max_tx_gas_limit(ctx.config().txpool.max_tx_gas_limit)
498 .with_minimum_priority_fee(ctx.config().txpool.minimum_priority_fee)
499 .with_additional_tasks(ctx.config().txpool.additional_validation_tasks)
500 .build_with_tasks(ctx.task_executor().clone(), blob_store.clone());
501
502 if validator.validator().eip4844() {
503 let kzg_settings = validator.validator().kzg_settings().clone();
507 ctx.task_executor().spawn_blocking(async move {
508 let _ = kzg_settings.get();
509 debug!(target: "reth::cli", "Initialized KZG settings");
510 });
511 }
512
513 let transaction_pool = TxPoolBuilder::new(ctx)
514 .with_validator(validator)
515 .build_and_spawn_maintenance_task(blob_store, pool_config)?;
516
517 info!(target: "reth::cli", "Transaction pool initialized");
518 debug!(target: "reth::cli", "Spawned txpool maintenance task");
519
520 Ok(transaction_pool)
521 }
522}
523
524#[derive(Debug, Default, Clone, Copy)]
526pub struct EthereumNetworkBuilder {
527 }
529
530impl<Node, Pool> NetworkBuilder<Node, Pool> for EthereumNetworkBuilder
531where
532 Node: FullNodeTypes<Types: NodeTypes<ChainSpec: Hardforks>>,
533 Pool: TransactionPool<Transaction: PoolTransaction<Consensus = TxTy<Node::Types>>>
534 + Unpin
535 + 'static,
536{
537 type Network =
538 NetworkHandle<BasicNetworkPrimitives<PrimitivesTy<Node::Types>, PoolPooledTx<Pool>>>;
539
540 async fn build_network(
541 self,
542 ctx: &BuilderContext<Node>,
543 pool: Pool,
544 ) -> eyre::Result<Self::Network> {
545 let network = ctx.network_builder().await?;
546 let handle = ctx.start_network(network, pool);
547 info!(target: "reth::cli", enode=%handle.local_node_record(), "P2P networking initialized");
548 Ok(handle)
549 }
550}
551
552#[derive(Debug, Default, Clone, Copy)]
554pub struct EthereumConsensusBuilder {
555 }
557
558impl<Node> ConsensusBuilder<Node> for EthereumConsensusBuilder
559where
560 Node: FullNodeTypes<
561 Types: NodeTypes<ChainSpec: EthChainSpec + EthereumHardforks, Primitives = EthPrimitives>,
562 >,
563{
564 type Consensus = Arc<EthBeaconConsensus<<Node::Types as NodeTypes>::ChainSpec>>;
565
566 async fn build_consensus(self, ctx: &BuilderContext<Node>) -> eyre::Result<Self::Consensus> {
567 Ok(Arc::new(EthBeaconConsensus::new(ctx.chain_spec())))
568 }
569}
570
571#[derive(Debug, Default, Clone)]
573#[non_exhaustive]
574pub struct EthereumEngineValidatorBuilder;
575
576impl<Node, Types> PayloadValidatorBuilder<Node> for EthereumEngineValidatorBuilder
577where
578 Types: NodeTypes<
579 ChainSpec: Hardforks + EthereumHardforks + Clone + 'static,
580 Payload: EngineTypes<ExecutionData = ExecutionData>
581 + PayloadTypes<PayloadAttributes = EthPayloadAttributes>,
582 Primitives = EthPrimitives,
583 >,
584 Node: FullNodeComponents<Types = Types>,
585{
586 type Validator = EthereumEngineValidator<Types::ChainSpec>;
587
588 async fn build(self, ctx: &AddOnsContext<'_, Node>) -> eyre::Result<Self::Validator> {
589 Ok(EthereumEngineValidator::new(ctx.config.chain.clone()))
590 }
591}