Skip to main content

reth_node_builder/launch/
engine.rs

1//! Engine node related functionality.
2
3use crate::{
4    common::{Attached, LaunchContextWith, WithConfigs},
5    hooks::NodeHooks,
6    rpc::{EngineShutdown, EngineValidatorAddOn, EngineValidatorBuilder, RethRpcAddOns, RpcHandle},
7    setup::build_networked_pipeline,
8    AddOns, AddOnsContext, FullNode, LaunchContext, LaunchNode, NodeAdapter,
9    NodeBuilderWithComponents, NodeComponents, NodeComponentsBuilder, NodeHandle, NodeTypesAdapter,
10};
11use alloy_consensus::BlockHeader;
12use futures::{stream::FusedStream, stream_select, FutureExt, StreamExt};
13use reth_chainspec::{EthChainSpec, EthereumHardforks};
14use reth_engine_tree::{
15    chain::{ChainEvent, FromOrchestrator},
16    engine::{EngineApiKind, EngineApiRequest, EngineRequestHandler},
17    launch::build_engine_orchestrator,
18    tree::TreeConfig,
19};
20use reth_engine_util::EngineMessageStreamExt;
21use reth_exex::ExExManagerHandle;
22use reth_network::{types::BlockRangeUpdate, NetworkSyncUpdater, SyncState};
23use reth_network_api::BlockDownloaderProvider;
24use reth_node_api::{
25    BuiltPayload, ConsensusEngineHandle, FullNodeTypes, NodeTypes, NodeTypesWithDBAdapter,
26};
27use reth_node_core::{
28    dirs::{ChainPath, DataDirPath},
29    exit::NodeExitFuture,
30    primitives::Head,
31};
32use reth_node_events::node;
33use reth_provider::{
34    providers::{BlockchainProvider, NodeTypesForProvider},
35    BlockNumReader, StorageSettingsCache,
36};
37use reth_tasks::TaskExecutor;
38use reth_tokio_util::EventSender;
39use reth_tracing::tracing::{debug, error, info};
40use reth_trie_db::ChangesetCache;
41use std::{future::Future, pin::Pin, sync::Arc};
42use tokio::sync::{mpsc::unbounded_channel, oneshot};
43use tokio_stream::wrappers::UnboundedReceiverStream;
44
45/// The engine node launcher.
46#[derive(Debug)]
47pub struct EngineNodeLauncher {
48    /// The task executor for the node.
49    pub ctx: LaunchContext,
50
51    /// Temporary configuration for engine tree.
52    /// After engine is stabilized, this should be configured through node builder.
53    pub engine_tree_config: TreeConfig,
54}
55
56impl EngineNodeLauncher {
57    /// Create a new instance of the ethereum node launcher.
58    pub const fn new(
59        task_executor: TaskExecutor,
60        data_dir: ChainPath<DataDirPath>,
61        engine_tree_config: TreeConfig,
62    ) -> Self {
63        Self { ctx: LaunchContext::new(task_executor, data_dir), engine_tree_config }
64    }
65
66    async fn launch_node<T, CB, AO>(
67        self,
68        target: NodeBuilderWithComponents<T, CB, AO>,
69    ) -> eyre::Result<NodeHandle<NodeAdapter<T, CB::Components>, AO>>
70    where
71        T: FullNodeTypes<
72            Types: NodeTypesForProvider,
73            Provider = BlockchainProvider<
74                NodeTypesWithDBAdapter<<T as FullNodeTypes>::Types, <T as FullNodeTypes>::DB>,
75            >,
76        >,
77        CB: NodeComponentsBuilder<T>,
78        AO: RethRpcAddOns<NodeAdapter<T, CB::Components>>
79            + EngineValidatorAddOn<NodeAdapter<T, CB::Components>>,
80    {
81        let Self { ctx, engine_tree_config } = self;
82        let NodeBuilderWithComponents {
83            adapter: NodeTypesAdapter { database },
84            rocksdb_provider,
85            components_builder,
86            add_ons: AddOns { hooks, exexs: installed_exex, add_ons },
87            config,
88        } = target;
89        let NodeHooks { on_component_initialized, on_node_started, .. } = hooks;
90
91        // Create changeset cache that will be shared across the engine
92        let changeset_cache = ChangesetCache::new();
93
94        // setup the launch context
95        let ctx = ctx
96            .with_configured_globals(engine_tree_config.reserved_cpu_cores())
97            // load the toml config
98            .with_loaded_toml_config(config)?
99            // add resolved peers
100            .with_resolved_peers()?
101            // attach the database
102            .attach(database.clone())
103            // ensure certain settings take effect
104            .with_adjusted_configs()
105            // Create the provider factory with changeset cache
106            .with_provider_factory::<_, <CB::Components as NodeComponents<T>>::Evm>(changeset_cache.clone(), rocksdb_provider).await?
107            .inspect(|_| {
108                info!(target: "reth::cli", "Database opened");
109            })
110            .with_prometheus_server().await?
111            .inspect(|this| {
112                debug!(target: "reth::cli", chain=%this.chain_id(), genesis=?this.genesis_hash(), "Initializing genesis");
113            })
114            .with_genesis()?
115            .inspect(|this: &LaunchContextWith<Attached<WithConfigs<<T::Types as NodeTypes>::ChainSpec>, _>>| {
116                info!(target: "reth::cli", "\n{}", this.chain_spec().display_hardforks());
117                let settings = this.provider_factory().cached_storage_settings();
118                info!(target: "reth::cli", ?settings, "Loaded storage settings");
119            })
120            .with_metrics_task()
121            // passing FullNodeTypes as type parameter here so that we can build
122            // later the components.
123            .with_blockchain_db::<T, _>(move |provider_factory| {
124                Ok(BlockchainProvider::new(provider_factory)?)
125            })?
126            .with_components(components_builder, on_component_initialized).await?;
127
128        // spawn exexs if any
129        let maybe_exex_manager_handle = ctx.launch_exex(installed_exex).await?;
130
131        // create pipeline
132        let network_handle = ctx.components().network().clone();
133        let network_client = network_handle.fetch_client().await?;
134        let (consensus_engine_tx, consensus_engine_rx) = unbounded_channel();
135
136        let node_config = ctx.node_config();
137
138        // We always assume that node is syncing after a restart
139        network_handle.update_sync_state(SyncState::Syncing);
140
141        let max_block = ctx.max_block(network_client.clone()).await?;
142
143        let static_file_producer = ctx.static_file_producer();
144        let static_file_producer_events = static_file_producer.lock().events();
145        info!(target: "reth::cli", "StaticFileProducer initialized");
146
147        let consensus = Arc::new(ctx.components().consensus().clone());
148
149        let pipeline = build_networked_pipeline(
150            &ctx.toml_config().stages,
151            network_client.clone(),
152            consensus.clone(),
153            ctx.provider_factory().clone(),
154            ctx.task_executor(),
155            ctx.sync_metrics_tx(),
156            ctx.prune_config(),
157            max_block,
158            static_file_producer,
159            ctx.components().evm_config().clone(),
160            maybe_exex_manager_handle.clone().unwrap_or_else(ExExManagerHandle::empty),
161            ctx.era_import_source(),
162        )?;
163
164        // The new engine writes directly to static files. This ensures that they're up to the tip.
165        pipeline.move_to_static_files()?;
166
167        let pipeline_events = pipeline.events();
168
169        let mut pruner_builder = ctx.pruner_builder();
170        if let Some(exex_manager_handle) = &maybe_exex_manager_handle {
171            pruner_builder =
172                pruner_builder.finished_exex_height(exex_manager_handle.finished_height());
173        }
174        let pruner = pruner_builder.build_with_provider_factory(ctx.provider_factory().clone());
175        let pruner_events = pruner.events();
176        info!(target: "reth::cli", prune_config=?ctx.prune_config(), "Pruner initialized");
177
178        let event_sender = EventSender::default();
179
180        let beacon_engine_handle = ConsensusEngineHandle::new(consensus_engine_tx.clone());
181
182        // extract the jwt secret from the args if possible
183        let jwt_secret = ctx.auth_jwt_secret()?;
184
185        let add_ons_ctx = AddOnsContext {
186            node: ctx.node_adapter().clone(),
187            config: ctx.node_config(),
188            beacon_engine_handle: beacon_engine_handle.clone(),
189            jwt_secret,
190            engine_events: event_sender.clone(),
191        };
192        let validator_builder = add_ons.engine_validator_builder();
193
194        // Build the engine validator with all required components
195        let engine_validator = validator_builder
196            .clone()
197            .build_tree_validator(&add_ons_ctx, engine_tree_config.clone(), changeset_cache.clone())
198            .await?;
199
200        // Create the consensus engine stream with optional reorg
201        let consensus_engine_stream = UnboundedReceiverStream::from(consensus_engine_rx)
202            .maybe_skip_fcu(node_config.debug.skip_fcu)
203            .maybe_skip_new_payload(node_config.debug.skip_new_payload)
204            .maybe_reorg(
205                ctx.blockchain_db().clone(),
206                ctx.components().evm_config().clone(),
207                || async {
208                    validator_builder
209                        .build_tree_validator(
210                            &add_ons_ctx,
211                            engine_tree_config.clone(),
212                            changeset_cache.clone(),
213                        )
214                        .await
215                },
216                node_config.debug.reorg_frequency,
217                node_config.debug.reorg_depth,
218            )
219            .await?
220            // Store messages _after_ skipping so that `replay-engine` command
221            // would replay only the messages that were observed by the engine
222            // during this run.
223            .maybe_store_messages(node_config.debug.engine_api_store.clone());
224
225        let engine_kind = if ctx.chain_spec().is_optimism() {
226            EngineApiKind::OpStack
227        } else {
228            EngineApiKind::Ethereum
229        };
230
231        let mut orchestrator = build_engine_orchestrator(
232            engine_kind,
233            consensus.clone(),
234            network_client.clone(),
235            Box::pin(consensus_engine_stream),
236            pipeline,
237            ctx.task_executor().clone(),
238            ctx.provider_factory().clone(),
239            ctx.blockchain_db().clone(),
240            pruner,
241            ctx.components().payload_builder_handle().clone(),
242            engine_validator,
243            engine_tree_config,
244            ctx.sync_metrics_tx(),
245            ctx.components().evm_config().clone(),
246            changeset_cache,
247            ctx.task_executor().clone(),
248        );
249
250        info!(target: "reth::cli", "Consensus engine initialized");
251
252        #[expect(clippy::needless_continue)]
253        let events = stream_select!(
254            event_sender.new_listener().map(Into::into),
255            pipeline_events.map(Into::into),
256            ctx.consensus_layer_events(),
257            pruner_events.map(Into::into),
258            static_file_producer_events.map(Into::into),
259        );
260
261        ctx.task_executor().spawn_critical_task(
262            "events task",
263            node::handle_events(
264                Some(Box::new(ctx.components().network().clone())),
265                Some(ctx.head().number),
266                events,
267            ),
268        );
269
270        let RpcHandle {
271            rpc_server_handles,
272            rpc_registry,
273            engine_events,
274            beacon_engine_handle,
275            engine_shutdown: _,
276        } = add_ons.launch_add_ons(add_ons_ctx).await?;
277
278        // Create engine shutdown handle
279        let (engine_shutdown, shutdown_rx) = EngineShutdown::new();
280
281        // Run consensus engine to completion
282        let initial_target = ctx.initial_backfill_target()?;
283        let mut built_payloads = ctx
284            .components()
285            .payload_builder_handle()
286            .subscribe()
287            .await
288            .map_err(|e| eyre::eyre!("Failed to subscribe to payload builder events: {:?}", e))?
289            .into_built_payload_stream()
290            .fuse();
291
292        let chainspec = ctx.chain_spec();
293        let provider = ctx.blockchain_db().clone();
294        let (exit, rx) = oneshot::channel();
295        let terminate_after_backfill = ctx.terminate_after_initial_backfill();
296        let startup_sync_state_idle = ctx.node_config().debug.startup_sync_state_idle;
297
298        info!(target: "reth::cli", "Starting consensus engine");
299        let consensus_engine = move |mut on_graceful_shutdown| async move {
300            if let Some(initial_target) = initial_target {
301                debug!(target: "reth::cli", %initial_target,  "start backfill sync");
302                // network_handle's sync state is already initialized at Syncing
303                orchestrator.start_backfill_sync(initial_target);
304            } else if startup_sync_state_idle {
305                network_handle.update_sync_state(SyncState::Idle);
306            }
307
308            let mut res = Ok(());
309            let mut shutdown_rx = shutdown_rx.fuse();
310
311            // advance the chain and await payloads built locally to add into the engine api
312            // tree handler to prevent re-execution if that block is received as payload from
313            // the CL
314            loop {
315                tokio::select! {
316                    event = orchestrator.next() => {
317                        let Some(event) = event else { break };
318                        debug!(target: "reth::cli", "Event: {event}");
319                        match event {
320                            ChainEvent::BackfillSyncFinished => {
321                                if terminate_after_backfill {
322                                    debug!(target: "reth::cli", "Terminating after initial backfill");
323                                    break
324                                }
325                                if startup_sync_state_idle {
326                                    network_handle.update_sync_state(SyncState::Idle);
327                                }
328                            }
329                            ChainEvent::BackfillSyncStarted => {
330                                network_handle.update_sync_state(SyncState::Syncing);
331                            }
332                            ChainEvent::FatalError => {
333                                error!(target: "reth::cli", "Fatal error in consensus engine");
334                                res = Err(eyre::eyre!("Fatal error in consensus engine"));
335                                break
336                            }
337                            ChainEvent::Handler(ev) => {
338                                if let Some(head) = ev.canonical_header() {
339                                    // Once we're progressing via live sync, we can consider the node is not syncing anymore
340                                    network_handle.update_sync_state(SyncState::Idle);
341                                    let head_block = Head {
342                                        number: head.number(),
343                                        hash: head.hash(),
344                                        difficulty: head.difficulty(),
345                                        timestamp: head.timestamp(),
346                                        total_difficulty: chainspec.final_paris_total_difficulty()
347                                            .filter(|_| chainspec.is_paris_active_at_block(head.number()))
348                                            .unwrap_or_default(),
349                                    };
350                                    network_handle.update_status(head_block);
351
352                                    let updated = BlockRangeUpdate {
353                                        earliest: provider.earliest_block_number().unwrap_or_default(),
354                                        latest: head.number(),
355                                        latest_hash: head.hash(),
356                                    };
357                                    network_handle.update_block_range(updated);
358                                }
359                                event_sender.notify(ev);
360                            }
361                        }
362                    }
363                    payload = built_payloads.select_next_some(), if !built_payloads.is_terminated() => {
364                        if let Some(executed_block) = payload.executed_block() {
365                            debug!(target: "reth::cli", block=?executed_block.recovered_block.num_hash(),  "inserting built payload");
366                            orchestrator.handler_mut().handler_mut().on_event(EngineApiRequest::InsertExecutedBlock(executed_block).into());
367                        }
368                    }
369                    shutdown_req = &mut shutdown_rx => {
370                        if let Ok(req) = shutdown_req {
371                            debug!(target: "reth::cli", "received engine shutdown request");
372                            orchestrator.handler_mut().handler_mut().on_event(
373                                FromOrchestrator::Terminate { tx: req.done_tx }.into()
374                            );
375                        }
376                    }
377                    _guard = &mut on_graceful_shutdown => {
378                        // Shutdown signal received.
379                        // Send Terminate so the engine OS thread can exit cleanly before we
380                        // drop the orchestrator.
381                        debug!(target: "reth::cli", "shutdown signal received, terminating engine");
382                        let (done_tx, done_rx) = oneshot::channel();
383                        orchestrator.handler_mut().handler_mut().on_event(
384                            FromOrchestrator::Terminate { tx: done_tx }.into()
385                        );
386                        let _ = done_rx.await;
387                        break;
388                    }
389                }
390            }
391
392            let _ = exit.send(res);
393        };
394        ctx.task_executor()
395            .spawn_critical_with_graceful_shutdown_signal("consensus engine", consensus_engine);
396
397        let engine_events_for_ethstats = engine_events.new_listener();
398
399        let full_node = FullNode {
400            evm_config: ctx.components().evm_config().clone(),
401            pool: ctx.components().pool().clone(),
402            network: ctx.components().network().clone(),
403            provider: ctx.node_adapter().provider.clone(),
404            payload_builder_handle: ctx.components().payload_builder_handle().clone(),
405            task_executor: ctx.task_executor().clone(),
406            config: ctx.node_config().clone(),
407            data_dir: ctx.data_dir().clone(),
408            add_ons_handle: RpcHandle {
409                rpc_server_handles,
410                rpc_registry,
411                engine_events,
412                beacon_engine_handle,
413                engine_shutdown,
414            },
415        };
416        // Notify on node started
417        on_node_started.on_event(FullNode::clone(&full_node))?;
418
419        ctx.spawn_ethstats(engine_events_for_ethstats).await?;
420
421        let handle = NodeHandle {
422            node_exit_future: NodeExitFuture::new(async { rx.await? }),
423            node: full_node,
424        };
425
426        Ok(handle)
427    }
428}
429
430impl<T, CB, AO> LaunchNode<NodeBuilderWithComponents<T, CB, AO>> for EngineNodeLauncher
431where
432    T: FullNodeTypes<
433        Types: NodeTypesForProvider,
434        Provider = BlockchainProvider<
435            NodeTypesWithDBAdapter<<T as FullNodeTypes>::Types, <T as FullNodeTypes>::DB>,
436        >,
437    >,
438    CB: NodeComponentsBuilder<T> + 'static,
439    AO: RethRpcAddOns<NodeAdapter<T, CB::Components>>
440        + EngineValidatorAddOn<NodeAdapter<T, CB::Components>>
441        + 'static,
442{
443    type Node = NodeHandle<NodeAdapter<T, CB::Components>, AO>;
444    type Future = Pin<Box<dyn Future<Output = eyre::Result<Self::Node>> + Send>>;
445
446    fn launch_node(self, target: NodeBuilderWithComponents<T, CB, AO>) -> Self::Future {
447        Box::pin(self.launch_node(target))
448    }
449}