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