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, BlockTy, 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, EthPooledTransaction, EthTransactionPool, PoolPooledTx,
57 PoolTransaction, 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 let testing_skip_invalid_transactions = ctx.config.rpc.testing_skip_invalid_transactions;
307
308 self.inner
309 .launch_add_ons_with(ctx, move |container| {
310 container.modules.merge_if_module_configured(
311 RethRpcModule::Flashbots,
312 validation_api.into_rpc(),
313 )?;
314
315 container
316 .modules
317 .merge_if_module_configured(RethRpcModule::Eth, eth_config.into_rpc())?;
318
319 let mut testing_api = TestingApi::new(
322 container.registry.eth_api().clone(),
323 container.registry.evm_config().clone(),
324 );
325 if testing_skip_invalid_transactions {
326 testing_api = testing_api.with_skip_invalid_transactions();
327 }
328 container
329 .modules
330 .merge_if_module_configured(RethRpcModule::Testing, testing_api.into_rpc())?;
331
332 Ok(())
333 })
334 .await
335 }
336}
337
338impl<N, EthB, PVB, EB, EVB, RpcMiddleware> RethRpcAddOns<N>
339 for EthereumAddOns<N, EthB, PVB, EB, EVB, RpcMiddleware>
340where
341 N: FullNodeComponents<
342 Types: NodeTypes<
343 ChainSpec: Hardforks + EthereumHardforks,
344 Primitives = EthPrimitives,
345 Payload: EngineTypes<ExecutionData = ExecutionData>,
346 >,
347 Evm: ConfigureEvm<NextBlockEnvCtx = NextBlockEnvAttributes>,
348 >,
349 EthB: EthApiBuilder<N>,
350 PVB: PayloadValidatorBuilder<N>,
351 EB: EngineApiBuilder<N>,
352 EVB: EngineValidatorBuilder<N>,
353 EthApiError: FromEvmError<N::Evm>,
354 EvmFactoryFor<N::Evm>: EvmFactory<Tx = TxEnv>,
355 RpcMiddleware: RethRpcMiddleware,
356{
357 type EthApi = EthB::EthApi;
358
359 fn hooks_mut(&mut self) -> &mut reth_node_builder::rpc::RpcHooks<N, Self::EthApi> {
360 self.inner.hooks_mut()
361 }
362}
363
364impl<N, EthB, PVB, EB, EVB, RpcMiddleware> EngineValidatorAddOn<N>
365 for EthereumAddOns<N, EthB, PVB, EB, EVB, RpcMiddleware>
366where
367 N: FullNodeComponents<
368 Types: NodeTypes<
369 ChainSpec: EthChainSpec + EthereumHardforks,
370 Primitives = EthPrimitives,
371 Payload: EngineTypes<ExecutionData = ExecutionData>,
372 >,
373 Evm: ConfigureEvm<NextBlockEnvCtx = NextBlockEnvAttributes>,
374 >,
375 EthB: EthApiBuilder<N>,
376 PVB: Send,
377 EB: EngineApiBuilder<N>,
378 EVB: EngineValidatorBuilder<N>,
379 EthApiError: FromEvmError<N::Evm>,
380 EvmFactoryFor<N::Evm>: EvmFactory<Tx = TxEnv>,
381 RpcMiddleware: Send,
382{
383 type ValidatorBuilder = EVB;
384
385 fn engine_validator_builder(&self) -> Self::ValidatorBuilder {
386 self.inner.engine_validator_builder()
387 }
388}
389
390impl<N> Node<N> for EthereumNode
391where
392 N: FullNodeTypes<Types = Self>,
393{
394 type ComponentsBuilder = ComponentsBuilder<
395 N,
396 EthereumPoolBuilder,
397 BasicPayloadServiceBuilder<EthereumPayloadBuilder>,
398 EthereumNetworkBuilder,
399 EthereumExecutorBuilder,
400 EthereumConsensusBuilder,
401 >;
402
403 type AddOns =
404 EthereumAddOns<NodeAdapter<N>, EthereumEthApiBuilder, EthereumEngineValidatorBuilder>;
405
406 fn components_builder(&self) -> Self::ComponentsBuilder {
407 Self::components()
408 }
409
410 fn add_ons(&self) -> Self::AddOns {
411 EthereumAddOns::default()
412 }
413}
414
415impl<N: FullNodeComponents<Types = Self>> DebugNode<N> for EthereumNode {
416 type RpcBlock = alloy_rpc_types_eth::Block;
417
418 fn rpc_to_primitive_block(rpc_block: Self::RpcBlock) -> reth_ethereum_primitives::Block {
419 rpc_block.into_consensus().convert_transactions()
420 }
421
422 fn local_payload_attributes_builder(
423 chain_spec: &Self::ChainSpec,
424 ) -> impl PayloadAttributesBuilder<<Self::Payload as PayloadTypes>::PayloadAttributes> {
425 LocalPayloadAttributesBuilder::new(Arc::new(chain_spec.clone()))
426 }
427}
428
429#[derive(Debug, Default, Clone, Copy)]
431#[non_exhaustive]
432pub struct EthereumExecutorBuilder;
433
434impl<Types, Node> ExecutorBuilder<Node> for EthereumExecutorBuilder
435where
436 Types: NodeTypes<
437 ChainSpec: Hardforks + EthExecutorSpec + EthereumHardforks,
438 Primitives = EthPrimitives,
439 >,
440 Node: FullNodeTypes<Types = Types>,
441{
442 type EVM = EthEvmConfig<Types::ChainSpec>;
443
444 async fn build_evm(self, ctx: &BuilderContext<Node>) -> eyre::Result<Self::EVM> {
445 Ok(EthEvmConfig::new(ctx.chain_spec()))
446 }
447}
448
449#[derive(Debug, Default, Clone, Copy)]
454#[non_exhaustive]
455pub struct EthereumPoolBuilder {
456 }
458
459impl<Types, Node> PoolBuilder<Node> for EthereumPoolBuilder
460where
461 Types: NodeTypes<
462 ChainSpec: EthereumHardforks,
463 Primitives: NodePrimitives<SignedTx = TransactionSigned>,
464 >,
465 Node: FullNodeTypes<Types = Types>,
466{
467 type Pool =
468 EthTransactionPool<Node::Provider, DiskFileBlobStore, EthPooledTransaction, BlockTy<Types>>;
469
470 async fn build_pool(self, ctx: &BuilderContext<Node>) -> eyre::Result<Self::Pool> {
471 let pool_config = ctx.pool_config();
472
473 let blobs_disabled = ctx.config().txpool.disable_blobs_support ||
474 ctx.config().txpool.blobpool_max_count == 0;
475
476 let blob_cache_size = if let Some(blob_cache_size) = pool_config.blob_cache_size {
477 Some(blob_cache_size)
478 } else {
479 let current_timestamp =
482 SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?.as_secs();
483 let blob_params = ctx
484 .chain_spec()
485 .blob_params_at_timestamp(current_timestamp)
486 .unwrap_or_else(BlobParams::cancun);
487
488 Some((blob_params.target_blob_count * EPOCH_SLOTS * 2) as u32)
491 };
492
493 let blob_store =
494 reth_node_builder::components::create_blob_store_with_cache(ctx, blob_cache_size)?;
495
496 let validator = TransactionValidationTaskExecutor::eth_builder(ctx.provider().clone())
497 .with_head_timestamp(ctx.head().timestamp)
498 .set_eip4844(!blobs_disabled)
499 .kzg_settings(ctx.kzg_settings()?)
500 .with_max_tx_input_bytes(ctx.config().txpool.max_tx_input_bytes)
501 .with_local_transactions_config(pool_config.local_transactions_config.clone())
502 .set_tx_fee_cap(ctx.config().rpc.rpc_tx_fee_cap)
503 .with_max_tx_gas_limit(ctx.config().txpool.max_tx_gas_limit)
504 .with_minimum_priority_fee(ctx.config().txpool.minimum_priority_fee)
505 .with_additional_tasks(ctx.config().txpool.additional_validation_tasks)
506 .build_with_tasks(ctx.task_executor().clone(), blob_store.clone());
507
508 if validator.validator().eip4844() {
509 let kzg_settings = validator.validator().kzg_settings().clone();
513 ctx.task_executor().spawn_blocking(async move {
514 let _ = kzg_settings.get();
515 debug!(target: "reth::cli", "Initialized KZG settings");
516 });
517 }
518
519 let transaction_pool = TxPoolBuilder::new(ctx)
520 .with_validator(validator)
521 .build_and_spawn_maintenance_task(blob_store, pool_config)?;
522
523 info!(target: "reth::cli", "Transaction pool initialized");
524 debug!(target: "reth::cli", "Spawned txpool maintenance task");
525
526 Ok(transaction_pool)
527 }
528}
529
530#[derive(Debug, Default, Clone, Copy)]
532pub struct EthereumNetworkBuilder {
533 }
535
536impl<Node, Pool> NetworkBuilder<Node, Pool> for EthereumNetworkBuilder
537where
538 Node: FullNodeTypes<Types: NodeTypes<ChainSpec: Hardforks>>,
539 Pool: TransactionPool<Transaction: PoolTransaction<Consensus = TxTy<Node::Types>>>
540 + Unpin
541 + 'static,
542{
543 type Network =
544 NetworkHandle<BasicNetworkPrimitives<PrimitivesTy<Node::Types>, PoolPooledTx<Pool>>>;
545
546 async fn build_network(
547 self,
548 ctx: &BuilderContext<Node>,
549 pool: Pool,
550 ) -> eyre::Result<Self::Network> {
551 let network = ctx.network_builder().await?;
552 let handle = ctx.start_network(network, pool);
553 info!(target: "reth::cli", enode=%handle.local_node_record(), "P2P networking initialized");
554 Ok(handle)
555 }
556}
557
558#[derive(Debug, Default, Clone, Copy)]
560pub struct EthereumConsensusBuilder {
561 }
563
564impl<Node> ConsensusBuilder<Node> for EthereumConsensusBuilder
565where
566 Node: FullNodeTypes<
567 Types: NodeTypes<ChainSpec: EthChainSpec + EthereumHardforks, Primitives = EthPrimitives>,
568 >,
569{
570 type Consensus = Arc<EthBeaconConsensus<<Node::Types as NodeTypes>::ChainSpec>>;
571
572 async fn build_consensus(self, ctx: &BuilderContext<Node>) -> eyre::Result<Self::Consensus> {
573 Ok(Arc::new(EthBeaconConsensus::new(ctx.chain_spec())))
574 }
575}
576
577#[derive(Debug, Default, Clone)]
579#[non_exhaustive]
580pub struct EthereumEngineValidatorBuilder;
581
582impl<Node, Types> PayloadValidatorBuilder<Node> for EthereumEngineValidatorBuilder
583where
584 Types: NodeTypes<
585 ChainSpec: Hardforks + EthereumHardforks + Clone + 'static,
586 Payload: EngineTypes<ExecutionData = ExecutionData>
587 + PayloadTypes<PayloadAttributes = EthPayloadAttributes>,
588 Primitives = EthPrimitives,
589 >,
590 Node: FullNodeComponents<Types = Types>,
591{
592 type Validator = EthereumEngineValidator<Types::ChainSpec>;
593
594 async fn build(self, ctx: &AddOnsContext<'_, Node>) -> eyre::Result<Self::Validator> {
595 Ok(EthereumEngineValidator::new(ctx.config.chain.clone()))
596 }
597}