1use crate::{
33 components::{NodeComponents, NodeComponentsBuilder},
34 hooks::OnComponentInitializedHook,
35 BuilderContext, ExExLauncher, NodeAdapter, PrimitivesTy,
36};
37use alloy_eips::eip2124::Head;
38use alloy_primitives::{BlockNumber, B256};
39use eyre::Context;
40use rayon::ThreadPoolBuilder;
41use reth_chainspec::{Chain, EthChainSpec, EthereumHardforks};
42use reth_config::{config::EtlConfig, PruneConfig};
43use reth_consensus::noop::NoopConsensus;
44use reth_db_api::{
45 database::Database, database_metrics::DatabaseMetrics, models::PartialStateTrieUnwindMarker,
46};
47use reth_db_common::init::{
48 init_genesis_with_settings, init_genesis_with_settings_and_validate, InitStorageError,
49};
50use reth_downloaders::{bodies::noop::NoopBodiesDownloader, headers::noop::NoopHeaderDownloader};
51use reth_engine_local::MiningMode;
52use reth_evm::{noop::NoopEvmConfig, ConfigureEvm, SenderRecoveryCache};
53use reth_exex::ExExManagerHandle;
54use reth_fs_util as fs;
55use reth_network_p2p::headers::client::HeadersClient;
56use reth_node_api::{FullNodeTypes, NodeTypes, NodeTypesWithDB, NodeTypesWithDBAdapter};
57use reth_node_core::{
58 args::{DefaultEraHost, PruneConfigKind},
59 dirs::{ChainPath, DataDirPath},
60 node_config::NodeConfig,
61 primitives::BlockHeader,
62 version::version_metadata,
63};
64use reth_node_metrics::{
65 chain::ChainSpecInfo,
66 hooks::Hooks,
67 recorder::install_prometheus_recorder,
68 server::{MetricServer, MetricServerConfig},
69 storage::StorageSettingsInfo,
70 version::VersionInfo,
71};
72use reth_provider::{
73 providers::{NodeTypesForProvider, ProviderNodeTypes, RocksDBProvider, StaticFileProvider},
74 BalStoreHandle, BlockHashReader, BlockNumReader, DBProvider, DatabaseProviderFactory,
75 MetadataProvider, MetadataWriter, ProviderError, ProviderFactory, ProviderResult,
76 RocksDBBalStore, RocksDBProviderFactory, StageCheckpointReader, StaticFileProviderBuilder,
77 StaticFileProviderFactory, StorageSettingsCache,
78};
79use reth_prune::{PruneMode, PruneModes, PrunerBuilder};
80use reth_rpc_builder::config::RethRpcServerConfig;
81use reth_rpc_layer::JwtSecret;
82use reth_stages::{
83 sets::DefaultStages,
84 stages::{EraImportSource, EraStage, MerkleStage},
85 MetricEvent, PipelineBuilder, PipelineTarget, StageId, StageSet,
86};
87use reth_static_file::{blocks_per_file_for_prune_distance, StaticFileProducer, StaticFileSegment};
88use reth_storage_overlay::OverlayManager;
89use reth_tasks::TaskExecutor;
90use reth_tracing::tracing::{debug, error, info, warn};
91use reth_transaction_pool::TransactionPool;
92use std::{sync::Arc, time::Duration};
93use tokio::sync::{
94 mpsc::{unbounded_channel, UnboundedSender},
95 oneshot, watch,
96};
97
98use futures::{future::Either, stream, Stream, StreamExt};
99use reth_engine_primitives::ConsensusEngineEvent;
100use reth_node_ethstats::EthStatsService;
101use reth_node_events::{cl::ConsensusLayerHealthEvents, node::NodeEvent};
102
103#[derive(Debug, Clone)]
122pub struct LaunchContext {
123 pub task_executor: TaskExecutor,
125 pub data_dir: ChainPath<DataDirPath>,
127}
128
129impl LaunchContext {
130 pub const fn new(task_executor: TaskExecutor, data_dir: ChainPath<DataDirPath>) -> Self {
132 Self { task_executor, data_dir }
133 }
134
135 pub const fn with<T>(self, attachment: T) -> LaunchContextWith<T> {
137 LaunchContextWith { inner: self, attachment }
138 }
139
140 pub fn with_loaded_toml_config<ChainSpec>(
145 self,
146 config: NodeConfig<ChainSpec>,
147 ) -> eyre::Result<LaunchContextWith<WithConfigs<ChainSpec>>>
148 where
149 ChainSpec: EthChainSpec + reth_chainspec::EthereumHardforks,
150 {
151 let toml_config = self.load_toml_config(&config)?;
152 Ok(self.with(WithConfigs { config, toml_config }))
153 }
154
155 pub fn load_toml_config<ChainSpec>(
160 &self,
161 config: &NodeConfig<ChainSpec>,
162 ) -> eyre::Result<reth_config::Config>
163 where
164 ChainSpec: EthChainSpec + reth_chainspec::EthereumHardforks,
165 {
166 let config_path = config.config.clone().unwrap_or_else(|| self.data_dir.config());
167
168 let mut toml_config = reth_config::Config::from_path(&config_path)
169 .wrap_err_with(|| format!("Could not load config file {config_path:?}"))?;
170
171 Self::save_pruning_config(&mut toml_config, config, &config_path)?;
172
173 info!(target: "reth::cli", path = ?config_path, "Configuration loaded");
174
175 toml_config.peers.trusted_nodes_only |= config.network.trusted_only;
178
179 toml_config.static_files =
181 config.static_files.merge_with_config(toml_config.static_files, config.pruning.minimal);
182
183 Ok(toml_config)
184 }
185
186 fn save_pruning_config<ChainSpec>(
189 reth_config: &mut reth_config::Config,
190 config: &NodeConfig<ChainSpec>,
191 config_path: impl AsRef<std::path::Path>,
192 ) -> eyre::Result<()>
193 where
194 ChainSpec: EthChainSpec + reth_chainspec::EthereumHardforks,
195 {
196 let mut should_save = reth_config.prune.segments.migrate();
197
198 if let Some(prune_config) = config.prune_config() {
199 if reth_config.prune != prune_config {
200 reth_config.set_prune_config(prune_config);
201 should_save = true;
202 }
203 } else if !reth_config.prune.is_default() {
204 info!(target: "reth::cli", "Pruning configuration is present in the config file, but no CLI arguments are provided. Using config from file.");
205 }
206
207 if should_save {
208 info!(target: "reth::cli", "Saving prune config to toml file");
209 reth_config.save(config_path.as_ref())?;
210 }
211
212 Ok(())
213 }
214
215 pub fn with_configured_globals(self, reserved_cpu_cores: usize) -> Self {
217 self.configure_globals(reserved_cpu_cores);
218 self
219 }
220
221 pub fn configure_globals(&self, reserved_cpu_cores: usize) {
226 match fdlimit::raise_fd_limit() {
229 Ok(fdlimit::Outcome::LimitRaised { from, to }) => {
230 debug!(from, to, "Raised file descriptor limit");
231 }
232 Ok(fdlimit::Outcome::Unsupported) => {}
233 Err(err) => warn!(%err, "Failed to raise file descriptor limit"),
234 }
235
236 let _ = reserved_cpu_cores;
240 let num_threads = self.task_executor.cpu_pool().current_num_threads();
241 if let Err(err) = ThreadPoolBuilder::new()
242 .num_threads(num_threads)
243 .thread_name(|i| format!("rayon-{i:02}"))
244 .build_global()
245 {
246 warn!(%err, "Failed to build global thread pool")
247 }
248 }
249}
250
251#[derive(Debug, Clone)]
262pub struct LaunchContextWith<T> {
263 pub inner: LaunchContext,
265 pub attachment: T,
267}
268
269impl<T> LaunchContextWith<T> {
270 pub fn configure_globals(&self, reserved_cpu_cores: u64) {
275 self.inner.configure_globals(reserved_cpu_cores.try_into().unwrap());
276 }
277
278 pub const fn data_dir(&self) -> &ChainPath<DataDirPath> {
280 &self.inner.data_dir
281 }
282
283 pub const fn task_executor(&self) -> &TaskExecutor {
285 &self.inner.task_executor
286 }
287
288 pub fn attach<A>(self, attachment: A) -> LaunchContextWith<Attached<T, A>> {
290 LaunchContextWith {
291 inner: self.inner,
292 attachment: Attached::new(self.attachment, attachment),
293 }
294 }
295
296 pub fn inspect<F>(self, f: F) -> Self
299 where
300 F: FnOnce(&Self),
301 {
302 f(&self);
303 self
304 }
305}
306
307impl<ChainSpec> LaunchContextWith<WithConfigs<ChainSpec>> {
308 pub fn with_resolved_peers(mut self) -> eyre::Result<Self> {
310 if !self.attachment.config.network.trusted_peers.is_empty() {
311 info!(target: "reth::cli", "Adding trusted nodes");
312
313 self.attachment
314 .toml_config
315 .peers
316 .trusted_nodes
317 .extend(self.attachment.config.network.trusted_peers.clone());
318 }
319 Ok(self)
320 }
321}
322
323impl<L, R> LaunchContextWith<Attached<L, R>> {
324 pub const fn left(&self) -> &L {
326 &self.attachment.left
327 }
328
329 pub const fn right(&self) -> &R {
331 &self.attachment.right
332 }
333
334 pub const fn left_mut(&mut self) -> &mut L {
336 &mut self.attachment.left
337 }
338
339 pub const fn right_mut(&mut self) -> &mut R {
341 &mut self.attachment.right
342 }
343}
344impl<R, ChainSpec: EthChainSpec> LaunchContextWith<Attached<WithConfigs<ChainSpec>, R>> {
345 pub fn with_adjusted_configs(self) -> Self {
351 self.ensure_etl_datadir().with_adjusted_instance_ports()
352 }
353
354 pub fn ensure_etl_datadir(mut self) -> Self {
356 if self.toml_config_mut().stages.etl.dir.is_none() {
357 let etl_path = EtlConfig::from_datadir(self.data_dir().data_dir());
358 if etl_path.exists() {
359 if let Err(err) = fs::remove_dir_all(&etl_path) {
361 warn!(target: "reth::cli", ?etl_path, %err, "Failed to remove ETL path on launch");
362 }
363 }
364 self.toml_config_mut().stages.etl.dir = Some(etl_path);
365 }
366
367 self
368 }
369
370 pub fn with_adjusted_instance_ports(mut self) -> Self {
372 self.node_config_mut().adjust_instance_ports();
373 self
374 }
375
376 pub const fn configs(&self) -> &WithConfigs<ChainSpec> {
378 self.attachment.left()
379 }
380
381 pub const fn node_config(&self) -> &NodeConfig<ChainSpec> {
383 &self.left().config
384 }
385
386 pub const fn node_config_mut(&mut self) -> &mut NodeConfig<ChainSpec> {
388 &mut self.left_mut().config
389 }
390
391 pub const fn toml_config(&self) -> &reth_config::Config {
393 &self.left().toml_config
394 }
395
396 pub const fn toml_config_mut(&mut self) -> &mut reth_config::Config {
398 &mut self.left_mut().toml_config
399 }
400
401 pub fn chain_spec(&self) -> Arc<ChainSpec> {
403 self.node_config().chain.clone()
404 }
405
406 pub fn genesis_hash(&self) -> B256 {
408 self.node_config().chain.genesis_hash()
409 }
410
411 pub fn chain_id(&self) -> Chain {
413 self.node_config().chain.chain()
414 }
415
416 pub const fn is_dev(&self) -> bool {
418 self.node_config().dev.dev
419 }
420
421 pub fn prune_config(&self) -> PruneConfig
425 where
426 ChainSpec: reth_chainspec::EthereumHardforks,
427 {
428 let Some(mut node_prune_config) = self.node_config().prune_config() else {
429 return self.toml_config().prune.clone();
431 };
432
433 node_prune_config.merge(self.toml_config().prune.clone());
435 node_prune_config
436 }
437
438 pub fn prune_modes(&self) -> PruneModes
440 where
441 ChainSpec: reth_chainspec::EthereumHardforks,
442 {
443 self.prune_config().segments
444 }
445
446 pub fn pruner_builder(&self) -> PrunerBuilder
448 where
449 ChainSpec: reth_chainspec::EthereumHardforks,
450 {
451 PrunerBuilder::new(self.prune_config())
452 }
453
454 pub fn auth_jwt_secret(&self) -> eyre::Result<JwtSecret> {
456 let default_jwt_path = self.data_dir().jwt();
457 let secret = self.node_config().rpc.auth_jwt_secret(default_jwt_path)?;
458 Ok(secret)
459 }
460
461 pub fn dev_mining_mode<Pool>(&self, pool: Pool) -> MiningMode<Pool>
463 where
464 Pool: TransactionPool + Unpin,
465 {
466 self.node_config().dev_mining_mode(pool)
467 }
468}
469
470impl<DB, ChainSpec> LaunchContextWith<Attached<WithConfigs<ChainSpec>, DB>>
471where
472 DB: Database + Clone + 'static,
473 ChainSpec: EthChainSpec + EthereumHardforks + 'static,
474{
475 pub async fn create_provider_factory<N, Evm>(
479 &self,
480 overlay_manager: OverlayManager<N::Primitives>,
481 rocksdb_provider: Option<RocksDBProvider>,
482 disabled_stages: &[StageId],
483 ) -> eyre::Result<ProviderFactory<N>>
484 where
485 N: ProviderNodeTypes<DB = DB, ChainSpec = ChainSpec>,
486 Evm: ConfigureEvm<Primitives = N::Primitives> + 'static,
487 {
488 self.create_provider_factory_with_recovery::<N, Evm>(
489 overlay_manager,
490 rocksdb_provider,
491 disabled_stages,
492 |_| Ok(()),
493 )
494 .await
495 }
496
497 pub async fn create_provider_factory_with_recovery<N, Evm>(
500 &self,
501 overlay_manager: OverlayManager<N::Primitives>,
502 rocksdb_provider: Option<RocksDBProvider>,
503 disabled_stages: &[StageId],
504 recover: impl FnOnce(&ProviderFactory<N>) -> eyre::Result<()>,
505 ) -> eyre::Result<ProviderFactory<N>>
506 where
507 N: ProviderNodeTypes<DB = DB, ChainSpec = ChainSpec>,
508 Evm: ConfigureEvm<Primitives = N::Primitives> + 'static,
509 {
510 let static_files_config = &self.toml_config().static_files;
512 static_files_config.validate()?;
513
514 let prune_config = self.prune_config();
515
516 let mut blocks_per_file = static_files_config.as_blocks_per_file_map();
517 if blocks_per_file.get(StaticFileSegment::Receipts).is_none() &&
522 let Some(PruneMode::Distance(distance)) = prune_config.segments.receipts
523 {
524 blocks_per_file
525 .insert(StaticFileSegment::Receipts, blocks_per_file_for_prune_distance(distance));
526 }
527
528 let static_file_provider =
530 StaticFileProviderBuilder::read_write(self.data_dir().static_files())
531 .with_metrics()
532 .with_blocks_per_file_for_segments(&blocks_per_file)
533 .with_genesis_block_number(self.chain_spec().genesis().number.unwrap_or_default())
534 .build()?;
535
536 let rocksdb_provider = if let Some(provider) = rocksdb_provider {
538 provider
539 } else {
540 RocksDBProvider::builder(self.data_dir().rocksdb())
541 .with_default_tables()
542 .with_metrics()
543 .with_statistics()
544 .with_block_cache_size_opt(self.node_config().db.rocksdb_block_cache_size)
545 .build()?
546 };
547
548 let bal_store = self
549 .node_config()
550 .db
551 .balstore_cache_size
552 .map(|distance| {
553 RocksDBBalStore::with_buffer_retention_distance(rocksdb_provider.clone(), distance)
554 })
555 .unwrap_or_else(|| RocksDBBalStore::new(rocksdb_provider.clone()));
556 let bal_store = BalStoreHandle::new(bal_store);
557 let factory = ProviderFactory::new(
558 self.right().clone(),
559 self.chain_spec(),
560 static_file_provider,
561 rocksdb_provider,
562 self.task_executor().clone(),
563 )?
564 .with_prune_modes(prune_config.segments)
565 .with_minimum_pruning_distance(prune_config.minimum_pruning_distance)
566 .with_overlay_manager(overlay_manager)
567 .with_bal_store(bal_store);
568
569 recover(&factory)?;
570
571 let (rocksdb_unwind, static_file_unwind) = factory.check_consistency()?;
575 let provider_ro = factory.database_provider_ro()?;
576 let (partial_trie_unwind, has_persisted_partial_trie_unwind) =
579 get_partial_trie_unwind_marker(&provider_ro)?;
580 let pending_pipeline_unwind = provider_ro.pipeline_unwind()?;
581 drop(provider_ro);
582 let persist_partial_trie_unwind =
583 !has_persisted_partial_trie_unwind && partial_trie_unwind.is_some();
584 let partial_trie_unwind_target =
585 partial_trie_unwind.map(|marker| marker.partial_state_trie);
586 let storage_unwind = [rocksdb_unwind, static_file_unwind].into_iter().flatten().min();
591 let storage_unwind = storage_unwind.filter(|unwind_block| {
592 partial_trie_unwind_target.is_none_or(|partial_trie| *unwind_block < partial_trie)
593 });
594
595 if partial_trie_unwind_target.is_some() ||
596 storage_unwind.is_some() ||
597 pending_pipeline_unwind.is_some()
598 {
599 let build_unwind_pipeline = |walk_all_changed_branch_children| {
600 let (_tip_tx, tip_rx) = watch::channel(B256::ZERO);
601 let mut stages = DefaultStages::new(
602 factory.clone(),
603 tip_rx,
604 Arc::new(NoopConsensus::default()),
605 NoopHeaderDownloader::default(),
606 NoopBodiesDownloader::default(),
607 NoopEvmConfig::<Evm>::default(),
608 self.toml_config().stages.clone(),
609 self.prune_modes(),
610 None,
611 )
612 .builder()
613 .add_before(
615 EraStage::new(None::<EraImportSource>, self.toml_config().stages.etl.clone()),
616 StageId::Headers,
617 )
618 .disable_all(disabled_stages);
619
620 if walk_all_changed_branch_children {
621 stages =
623 stages.set(MerkleStage::new_unwind(true)).enable(StageId::MerkleUnwind);
624 }
625
626 PipelineBuilder::default().add_stages(stages).build(
627 factory.clone(),
628 StaticFileProducer::new(factory.clone(), self.prune_modes()),
629 )
630 };
631 let pending_unwind_target =
632 pending_pipeline_unwind.as_ref().map(|unwind| unwind.target);
633 let recovery_pipeline = pending_pipeline_unwind
634 .map(|_| build_unwind_pipeline(partial_trie_unwind_target.is_some()));
635 let mut unwinds = Vec::with_capacity(2);
636
637 if let Some(unwind_block) = partial_trie_unwind_target {
638 unwinds.push((
639 PipelineTarget::Unwind(unwind_block),
640 "partial state trie".to_owned(),
641 build_unwind_pipeline(true),
642 true,
643 ));
644 }
645
646 if let Some(unwind_block) = storage_unwind {
647 let inconsistency_source = match (rocksdb_unwind, static_file_unwind) {
650 (Some(_), Some(_)) => "RocksDB and static file",
651 (Some(_), None) => "RocksDB",
652 (None, Some(_)) => "static file",
653 (None, None) => unreachable!(),
654 };
655 assert!(
656 unwind_block != 0 || pending_unwind_target == Some(0),
657 "A {inconsistency_source} inconsistency was found that would trigger an unwind to block 0"
658 );
659 unwinds.push((
660 PipelineTarget::Unwind(unwind_block),
661 inconsistency_source.to_owned(),
662 build_unwind_pipeline(false),
663 false,
664 ));
665 }
666
667 if persist_partial_trie_unwind {
668 let provider_rw = factory.database_provider_rw()?;
670 write_partial_trie_unwind_marker(
671 &provider_rw,
672 partial_trie_unwind.expect("partial trie unwind marker must exist"),
673 )?;
674 provider_rw.commit()?;
675 }
676
677 let (tx, rx) = oneshot::channel();
678 let factory = factory.clone();
679
680 self.task_executor().spawn_critical_blocking_task("pipeline task", async move {
682 let result: Result<(), reth_stages::PipelineError> = async {
683 if let Some(mut pipeline) = recovery_pipeline {
684 pipeline.resume_unwind()?;
685 }
686 for (unwind_target, inconsistency_source, pipeline, clear_partial_trie_unwind) in
687 unwinds
688 {
689 info!(target: "reth::cli", %unwind_target, %inconsistency_source, "Executing unwind after consistency check.");
690 let (_, result) = pipeline.run_as_fut(Some(unwind_target)).await;
691 result.inspect_err(|err| {
692 error!(target: "reth::cli", %unwind_target, %inconsistency_source, %err, "failed to run unwind");
693 })?;
694
695 if clear_partial_trie_unwind {
696 let provider_rw = factory.database_provider_rw()?;
697 delete_partial_trie_unwind_marker(&provider_rw)?;
698 provider_rw.commit()?;
699 }
700 }
701 Ok(())
702 }
703 .await;
704 let _ = tx.send(result);
705 });
706 rx.await??;
707 }
708
709 Ok(factory)
710 }
711
712 pub async fn with_provider_factory<N, Evm>(
714 self,
715 overlay_manager: OverlayManager<N::Primitives>,
716 rocksdb_provider: Option<RocksDBProvider>,
717 disabled_stages: &[StageId],
718 ) -> eyre::Result<LaunchContextWith<Attached<WithConfigs<ChainSpec>, ProviderFactory<N>>>>
719 where
720 N: ProviderNodeTypes<DB = DB, ChainSpec = ChainSpec>,
721 Evm: ConfigureEvm<Primitives = N::Primitives> + 'static,
722 {
723 self.with_provider_factory_and_recovery::<N, Evm>(
724 overlay_manager,
725 rocksdb_provider,
726 disabled_stages,
727 |_| Ok(()),
728 )
729 .await
730 }
731
732 pub async fn with_provider_factory_and_recovery<N, Evm>(
735 self,
736 overlay_manager: OverlayManager<N::Primitives>,
737 rocksdb_provider: Option<RocksDBProvider>,
738 disabled_stages: &[StageId],
739 recover: impl FnOnce(&ProviderFactory<N>) -> eyre::Result<()>,
740 ) -> eyre::Result<LaunchContextWith<Attached<WithConfigs<ChainSpec>, ProviderFactory<N>>>>
741 where
742 N: ProviderNodeTypes<DB = DB, ChainSpec = ChainSpec>,
743 Evm: ConfigureEvm<Primitives = N::Primitives> + 'static,
744 {
745 let factory = self
746 .create_provider_factory_with_recovery::<N, Evm>(
747 overlay_manager,
748 rocksdb_provider,
749 disabled_stages,
750 recover,
751 )
752 .await?;
753 let ctx = LaunchContextWith {
754 inner: self.inner,
755 attachment: self.attachment.map_right(|_| factory),
756 };
757
758 Ok(ctx)
759 }
760}
761
762impl<T> LaunchContextWith<Attached<WithConfigs<T::ChainSpec>, ProviderFactory<T>>>
763where
764 T: ProviderNodeTypes,
765{
766 pub const fn database(&self) -> &T::DB {
768 self.right().db_ref()
769 }
770
771 pub const fn provider_factory(&self) -> &ProviderFactory<T> {
773 self.right()
774 }
775
776 pub fn static_file_provider(&self) -> StaticFileProvider<T::Primitives> {
778 self.right().static_file_provider()
779 }
780
781 pub async fn with_prometheus_server(self) -> eyre::Result<Self>
785 where
786 T::ChainSpec: EthereumHardforks,
787 {
788 self.start_prometheus_endpoint().await?;
789 Ok(self)
790 }
791
792 pub async fn start_prometheus_endpoint(&self) -> eyre::Result<()>
794 where
795 T::ChainSpec: EthereumHardforks,
796 {
797 install_prometheus_recorder().spawn_upkeep();
799
800 let listen_addr = self.node_config().metrics.prometheus;
801 if let Some(addr) = listen_addr {
802 let prune_config = self.prune_config();
803 let pruning_mode =
804 PruneConfigKind::from_config(&prune_config, self.chain_spec().as_ref()).as_str();
805 let storage_settings =
809 if self.provider_factory().get_stage_checkpoint(StageId::Headers)?.is_some() {
810 self.provider_factory().cached_storage_settings()
811 } else {
812 self.node_config().storage_settings()
813 };
814 let config = MetricServerConfig::new(
815 addr,
816 VersionInfo {
817 version: version_metadata().cargo_pkg_version.as_ref(),
818 build_timestamp: version_metadata().vergen_build_timestamp.as_ref(),
819 cargo_features: version_metadata().vergen_cargo_features.as_ref(),
820 git_sha: version_metadata().vergen_git_sha.as_ref(),
821 target_triple: version_metadata().vergen_cargo_target_triple.as_ref(),
822 build_profile: version_metadata().build_profile_name.as_ref(),
823 },
824 ChainSpecInfo { name: self.chain_id().to_string() },
825 self.task_executor().clone(),
826 metrics_hooks(self.provider_factory()),
827 self.data_dir().pprof_dumps(),
828 )
829 .with_storage_settings_info(StorageSettingsInfo {
830 storage_v2: storage_settings.storage_v2,
831 pruning_mode,
832 prune_config: serde_json::to_string(&prune_config)
833 .expect("serializing PruneConfig should not fail"),
834 })
835 .with_push_gateway(
836 self.node_config().metrics.push_gateway_url.clone(),
837 self.node_config().metrics.push_gateway_interval,
838 );
839
840 MetricServer::new(config).serve().await?;
841 }
842
843 Ok(())
844 }
845
846 pub fn with_genesis(self) -> Result<Self, InitStorageError> {
848 init_genesis_with_settings_and_validate(
849 self.provider_factory(),
850 self.node_config().storage_settings(),
851 !self.node_config().debug.skip_genesis_validation,
852 )?;
853 Ok(self)
854 }
855
856 pub fn init_genesis(&self) -> Result<B256, InitStorageError> {
858 init_genesis_with_settings(self.provider_factory(), self.node_config().storage_settings())
859 }
860
861 pub fn with_metrics_task(
867 self,
868 ) -> LaunchContextWith<Attached<WithConfigs<T::ChainSpec>, WithMeteredProvider<T>>> {
869 let (metrics_sender, metrics_receiver) = unbounded_channel();
870
871 let with_metrics =
872 WithMeteredProvider { provider_factory: self.right().clone(), metrics_sender };
873
874 debug!(target: "reth::cli", "Spawning stages metrics listener task");
875 let sync_metrics_listener = reth_stages::MetricsListener::new(metrics_receiver);
876 self.task_executor()
877 .spawn_critical_task("stages metrics listener task", sync_metrics_listener);
878
879 LaunchContextWith {
880 inner: self.inner,
881 attachment: self.attachment.map_right(|_| with_metrics),
882 }
883 }
884}
885
886impl<N, DB>
887 LaunchContextWith<
888 Attached<WithConfigs<N::ChainSpec>, WithMeteredProvider<NodeTypesWithDBAdapter<N, DB>>>,
889 >
890where
891 N: NodeTypes,
892 DB: Database + DatabaseMetrics + Clone + Unpin + 'static,
893{
894 const fn provider_factory(&self) -> &ProviderFactory<NodeTypesWithDBAdapter<N, DB>> {
896 &self.right().provider_factory
897 }
898
899 fn sync_metrics_tx(&self) -> UnboundedSender<MetricEvent> {
901 self.right().metrics_sender.clone()
902 }
903
904 #[expect(clippy::complexity)]
906 pub fn with_blockchain_db<T, F>(
907 self,
908 create_blockchain_provider: F,
909 ) -> eyre::Result<LaunchContextWith<Attached<WithConfigs<N::ChainSpec>, WithMeteredProviders<T>>>>
910 where
911 T: FullNodeTypes<Types = N, DB = DB>,
912 F: FnOnce(ProviderFactory<NodeTypesWithDBAdapter<N, DB>>) -> eyre::Result<T::Provider>,
913 {
914 let blockchain_db = create_blockchain_provider(self.provider_factory().clone())?;
915
916 let metered_providers = WithMeteredProviders {
917 db_provider_container: WithMeteredProvider {
918 provider_factory: self.provider_factory().clone(),
919 metrics_sender: self.sync_metrics_tx(),
920 },
921 blockchain_db,
922 };
923
924 let ctx = LaunchContextWith {
925 inner: self.inner,
926 attachment: self.attachment.map_right(|_| metered_providers),
927 };
928
929 Ok(ctx)
930 }
931}
932
933impl<T>
934 LaunchContextWith<
935 Attached<WithConfigs<<T::Types as NodeTypes>::ChainSpec>, WithMeteredProviders<T>>,
936 >
937where
938 T: FullNodeTypes<Types: NodeTypesForProvider>,
939{
940 pub const fn database(&self) -> &T::DB {
942 self.provider_factory().db_ref()
943 }
944
945 pub const fn provider_factory(
947 &self,
948 ) -> &ProviderFactory<NodeTypesWithDBAdapter<T::Types, T::DB>> {
949 &self.right().db_provider_container.provider_factory
950 }
951
952 pub fn lookup_head(&self) -> eyre::Result<Head> {
956 self.node_config()
957 .lookup_head(self.provider_factory())
958 .wrap_err("the head block is missing")
959 }
960
961 pub fn sync_metrics_tx(&self) -> UnboundedSender<MetricEvent> {
963 self.right().db_provider_container.metrics_sender.clone()
964 }
965
966 pub const fn blockchain_db(&self) -> &T::Provider {
968 &self.right().blockchain_db
969 }
970
971 pub async fn with_components<CB>(
973 self,
974 components_builder: CB,
975 on_component_initialized: Box<
976 dyn OnComponentInitializedHook<NodeAdapter<T, CB::Components>>,
977 >,
978 ) -> eyre::Result<
979 LaunchContextWith<
980 Attached<WithConfigs<<T::Types as NodeTypes>::ChainSpec>, WithComponents<T, CB>>,
981 >,
982 >
983 where
984 CB: NodeComponentsBuilder<T>,
985 {
986 let head = self.lookup_head()?;
988
989 let builder_ctx = BuilderContext::new(
990 head,
991 self.blockchain_db().clone(),
992 self.task_executor().clone(),
993 self.configs().clone(),
994 );
995
996 debug!(target: "reth::cli", "creating components");
997 let components = components_builder.build_components(&builder_ctx).await?;
998
999 let blockchain_db = self.blockchain_db().clone();
1000
1001 let node_adapter = NodeAdapter {
1002 components,
1003 task_executor: self.task_executor().clone(),
1004 provider: blockchain_db,
1005 };
1006
1007 debug!(target: "reth::cli", "calling on_component_initialized hook");
1008 on_component_initialized.on_event(node_adapter.clone())?;
1009
1010 let components_container = WithComponents {
1011 db_provider_container: WithMeteredProvider {
1012 provider_factory: self.provider_factory().clone(),
1013 metrics_sender: self.sync_metrics_tx(),
1014 },
1015 node_adapter,
1016 head,
1017 sender_recovery_cache: builder_ctx.sender_recovery_cache().cloned(),
1018 };
1019
1020 let ctx = LaunchContextWith {
1021 inner: self.inner,
1022 attachment: self.attachment.map_right(|_| components_container),
1023 };
1024
1025 Ok(ctx)
1026 }
1027}
1028
1029impl<T, CB>
1030 LaunchContextWith<
1031 Attached<WithConfigs<<T::Types as NodeTypes>::ChainSpec>, WithComponents<T, CB>>,
1032 >
1033where
1034 T: FullNodeTypes<Types: NodeTypesForProvider>,
1035 CB: NodeComponentsBuilder<T>,
1036{
1037 pub const fn provider_factory(
1039 &self,
1040 ) -> &ProviderFactory<NodeTypesWithDBAdapter<T::Types, T::DB>> {
1041 &self.right().db_provider_container.provider_factory
1042 }
1043
1044 pub async fn max_block<C>(&self, client: C) -> eyre::Result<Option<BlockNumber>>
1047 where
1048 C: HeadersClient<Header: BlockHeader>,
1049 {
1050 self.node_config().max_block(client, self.provider_factory().clone()).await
1051 }
1052
1053 pub fn static_file_provider(&self) -> StaticFileProvider<<T::Types as NodeTypes>::Primitives> {
1055 self.provider_factory().static_file_provider()
1056 }
1057
1058 pub fn static_file_producer(
1060 &self,
1061 ) -> StaticFileProducer<ProviderFactory<NodeTypesWithDBAdapter<T::Types, T::DB>>> {
1062 StaticFileProducer::new(self.provider_factory().clone(), self.prune_modes())
1063 }
1064
1065 pub const fn head(&self) -> Head {
1067 self.right().head
1068 }
1069
1070 pub const fn node_adapter(&self) -> &NodeAdapter<T, CB::Components> {
1072 &self.right().node_adapter
1073 }
1074
1075 pub const fn node_adapter_mut(&mut self) -> &mut NodeAdapter<T, CB::Components> {
1077 &mut self.right_mut().node_adapter
1078 }
1079
1080 pub const fn sender_recovery_cache(&self) -> Option<&SenderRecoveryCache> {
1082 self.right().sender_recovery_cache.as_ref()
1083 }
1084
1085 pub const fn blockchain_db(&self) -> &T::Provider {
1087 &self.node_adapter().provider
1088 }
1089
1090 pub fn initial_backfill_target(
1096 &self,
1097 disabled_stages: &[StageId],
1098 ) -> ProviderResult<Option<B256>> {
1099 let mut initial_target = self.node_config().debug.tip;
1100
1101 if initial_target.is_none() {
1102 initial_target = self.check_pipeline_consistency(disabled_stages)?;
1103 }
1104
1105 Ok(initial_target)
1106 }
1107
1108 pub const fn terminate_after_initial_backfill(&self) -> bool {
1114 self.node_config().debug.terminate || self.node_config().debug.max_block.is_some()
1115 }
1116
1117 fn ensure_chain_specific_db_checks(&self) -> ProviderResult<()> {
1122 if self.chain_spec().is_optimism() &&
1123 !self.is_dev() &&
1124 self.chain_id() == Chain::optimism_mainnet()
1125 {
1126 let latest = self.blockchain_db().last_block_number()?;
1127 if latest < 105235063 {
1129 error!(
1130 "Op-mainnet has been launched without importing the pre-Bedrock state. The chain can't progress without this. See also https://reth.rs/run/sync-op-mainnet.html?minimal-bootstrap-recommended"
1131 );
1132 return Err(ProviderError::BestBlockNotFound);
1133 }
1134 }
1135
1136 Ok(())
1137 }
1138
1139 pub fn check_pipeline_consistency(
1151 &self,
1152 disabled_stages: &[StageId],
1153 ) -> ProviderResult<Option<B256>> {
1154 let era_enabled = self.era_import_source().is_some();
1156 let mut all_stages = StageId::ALL
1157 .into_iter()
1158 .filter(|id| (era_enabled || id != &StageId::Era) && !disabled_stages.contains(id));
1159
1160 let first_stage = all_stages.next().expect("there must be at least one stage");
1162
1163 let first_stage_checkpoint = self
1166 .blockchain_db()
1167 .get_stage_checkpoint(first_stage)?
1168 .unwrap_or_default()
1169 .block_number;
1170
1171 for stage_id in all_stages {
1173 let stage_checkpoint = self
1174 .blockchain_db()
1175 .get_stage_checkpoint(stage_id)?
1176 .unwrap_or_default()
1177 .block_number;
1178
1179 debug!(
1182 target: "consensus::engine",
1183 first_stage_id = %first_stage,
1184 first_stage_checkpoint,
1185 stage_id = %stage_id,
1186 stage_checkpoint = stage_checkpoint,
1187 "Checking stage against first stage",
1188 );
1189 if stage_checkpoint < first_stage_checkpoint {
1190 debug!(
1191 target: "consensus::engine",
1192 first_stage_id = %first_stage,
1193 first_stage_checkpoint,
1194 inconsistent_stage_id = %stage_id,
1195 inconsistent_stage_checkpoint = stage_checkpoint,
1196 "Pipeline sync progress is inconsistent"
1197 );
1198 return self.blockchain_db().block_hash(first_stage_checkpoint);
1199 }
1200 }
1201
1202 self.ensure_chain_specific_db_checks()?;
1203
1204 Ok(None)
1205 }
1206
1207 pub fn sync_metrics_tx(&self) -> UnboundedSender<MetricEvent> {
1209 self.right().db_provider_container.metrics_sender.clone()
1210 }
1211
1212 pub const fn components(&self) -> &CB::Components {
1214 &self.node_adapter().components
1215 }
1216
1217 #[expect(clippy::type_complexity)]
1219 pub async fn launch_exex(
1220 &self,
1221 installed_exex: Vec<(
1222 String,
1223 Box<dyn crate::exex::BoxedLaunchExEx<NodeAdapter<T, CB::Components>>>,
1224 )>,
1225 ) -> eyre::Result<Option<ExExManagerHandle<PrimitivesTy<T::Types>>>> {
1226 self.exex_launcher(installed_exex).launch().await
1227 }
1228
1229 #[expect(clippy::type_complexity)]
1241 pub fn exex_launcher(
1242 &self,
1243 installed_exex: Vec<(
1244 String,
1245 Box<dyn crate::exex::BoxedLaunchExEx<NodeAdapter<T, CB::Components>>>,
1246 )>,
1247 ) -> ExExLauncher<NodeAdapter<T, CB::Components>> {
1248 ExExLauncher::new(
1249 self.head(),
1250 self.node_adapter().clone(),
1251 installed_exex,
1252 self.configs().clone(),
1253 )
1254 }
1255
1256 pub fn era_import_source(&self) -> Option<EraImportSource> {
1260 let node_config = self.node_config();
1261 if !node_config.era.enabled {
1262 return None;
1263 }
1264
1265 EraImportSource::maybe_new(
1266 node_config.era.source.path.clone(),
1267 node_config.era.source.url.clone(),
1268 || node_config.chain.chain().kind().default_era_host(),
1269 || node_config.datadir().data_dir().join("era").into(),
1270 )
1271 }
1272
1273 pub fn consensus_layer_events(
1281 &self,
1282 ) -> impl Stream<Item = NodeEvent<PrimitivesTy<T::Types>>> + 'static
1283 where
1284 T::Provider: reth_provider::CanonChainTracker,
1285 {
1286 if self.node_config().debug.tip.is_none() && !self.is_dev() {
1287 Either::Left(
1288 ConsensusLayerHealthEvents::new(Box::new(self.blockchain_db().clone()))
1289 .map(Into::into),
1290 )
1291 } else {
1292 Either::Right(stream::empty())
1293 }
1294 }
1295
1296 pub async fn spawn_ethstats<St>(&self, mut engine_events: St) -> eyre::Result<()>
1298 where
1299 St: Stream<Item = reth_engine_primitives::ConsensusEngineEvent<PrimitivesTy<T::Types>>>
1300 + Send
1301 + Unpin
1302 + 'static,
1303 {
1304 let Some(url) = self.node_config().debug.ethstats.as_ref() else { return Ok(()) };
1305
1306 let network = self.components().network().clone();
1307 let pool = self.components().pool().clone();
1308 let provider = self.node_adapter().provider.clone();
1309
1310 info!(target: "reth::cli", "Starting EthStats service at {}", url);
1311
1312 let ethstats = EthStatsService::new(url, network, provider, pool).await?;
1313
1314 let ethstats_for_events = ethstats.clone();
1316 let task_executor = self.task_executor().clone();
1317 task_executor.spawn_task(async move {
1318 while let Some(event) = engine_events.next().await {
1319 match event {
1320 ConsensusEngineEvent::ForkBlockAdded(executed, duration) |
1321 ConsensusEngineEvent::CanonicalBlockAdded(executed, duration) => {
1322 let block_hash = executed.recovered_block.num_hash().hash;
1323 let block_number = executed.recovered_block.num_hash().number;
1324 if let Err(e) = ethstats_for_events
1325 .report_new_payload(block_hash, block_number, duration)
1326 .await
1327 {
1328 debug!(
1329 target: "ethstats",
1330 "Failed to report new payload: {}", e
1331 );
1332 }
1333 }
1334 _ => {
1335 }
1337 }
1338 }
1339 });
1340
1341 task_executor.spawn_task(async move { ethstats.run().await });
1343
1344 Ok(())
1345 }
1346}
1347
1348#[derive(Clone, Copy, Debug)]
1354pub struct Attached<L, R> {
1355 left: L,
1356 right: R,
1357}
1358
1359impl<L, R> Attached<L, R> {
1360 pub const fn new(left: L, right: R) -> Self {
1362 Self { left, right }
1363 }
1364
1365 pub fn map_left<F, T>(self, f: F) -> Attached<T, R>
1367 where
1368 F: FnOnce(L) -> T,
1369 {
1370 Attached::new(f(self.left), self.right)
1371 }
1372
1373 pub fn map_right<F, T>(self, f: F) -> Attached<L, T>
1375 where
1376 F: FnOnce(R) -> T,
1377 {
1378 Attached::new(self.left, f(self.right))
1379 }
1380
1381 pub const fn left(&self) -> &L {
1383 &self.left
1384 }
1385
1386 pub const fn right(&self) -> &R {
1388 &self.right
1389 }
1390
1391 pub const fn left_mut(&mut self) -> &mut L {
1393 &mut self.left
1394 }
1395
1396 pub const fn right_mut(&mut self) -> &mut R {
1398 &mut self.right
1399 }
1400}
1401
1402#[derive(Debug)]
1405pub struct WithConfigs<ChainSpec> {
1406 pub config: NodeConfig<ChainSpec>,
1408 pub toml_config: reth_config::Config,
1410}
1411
1412impl<ChainSpec> Clone for WithConfigs<ChainSpec> {
1413 fn clone(&self) -> Self {
1414 Self { config: self.config.clone(), toml_config: self.toml_config.clone() }
1415 }
1416}
1417
1418#[derive(Debug, Clone)]
1421pub struct WithMeteredProvider<N: NodeTypesWithDB> {
1422 provider_factory: ProviderFactory<N>,
1423 metrics_sender: UnboundedSender<MetricEvent>,
1424}
1425
1426#[expect(missing_debug_implementations)]
1429pub struct WithMeteredProviders<T>
1430where
1431 T: FullNodeTypes,
1432{
1433 db_provider_container: WithMeteredProvider<NodeTypesWithDBAdapter<T::Types, T::DB>>,
1434 blockchain_db: T::Provider,
1435}
1436
1437#[expect(missing_debug_implementations)]
1439pub struct WithComponents<T, CB>
1440where
1441 T: FullNodeTypes,
1442 CB: NodeComponentsBuilder<T>,
1443{
1444 db_provider_container: WithMeteredProvider<NodeTypesWithDBAdapter<T::Types, T::DB>>,
1445 node_adapter: NodeAdapter<T, CB::Components>,
1446 head: Head,
1447 sender_recovery_cache: Option<SenderRecoveryCache>,
1449}
1450
1451pub fn metrics_hooks<N: NodeTypesWithDB>(provider_factory: &ProviderFactory<N>) -> Hooks {
1457 Hooks::builder()
1458 .with_background_interval(Duration::from_secs(5 * 60))
1459 .with_background_hook({
1460 let db = provider_factory.db_ref().clone();
1461 move || db.report_metrics()
1462 })
1463 .with_background_hook({
1464 let sfp = provider_factory.static_file_provider();
1465 move || {
1466 if let Err(error) = sfp.report_metrics() {
1467 error!(%error, "Failed to report metrics from static file provider");
1468 }
1469 }
1470 })
1471 .with_background_hook({
1472 let rocksdb = provider_factory.rocksdb_provider();
1473 move || rocksdb.report_metrics()
1474 })
1475 .build()
1476}
1477
1478fn get_partial_trie_unwind_marker(
1479 provider: &(impl MetadataProvider + StageCheckpointReader),
1480) -> ProviderResult<(Option<PartialStateTrieUnwindMarker>, bool)> {
1481 if let Some(marker) = provider.get_metadata(PARTIAL_STATE_TRIE_UNWIND_METADATA_KEY)? {
1482 let marker = serde_json::from_slice::<PartialStateTrieUnwindMarker>(&marker)
1483 .map_err(ProviderError::other)?;
1484 if marker.partial_state_trie >= marker.finish_block_number {
1485 return Err(ProviderError::other(std::io::Error::other(format!(
1486 "partial state trie unwind target #{} is not below original Finish #{}",
1487 marker.partial_state_trie, marker.finish_block_number,
1488 ))))
1489 }
1490 return Ok((Some(marker), true))
1491 }
1492
1493 let Some(finish_checkpoint) = provider.get_stage_checkpoint(StageId::Finish)? else {
1494 return Ok((None, false))
1495 };
1496 let Some(partial_state_trie) =
1497 finish_checkpoint.finish_stage_checkpoint().and_then(|finish| finish.partial_state_trie())
1498 else {
1499 return Ok((None, false))
1500 };
1501
1502 if partial_state_trie > finish_checkpoint.block_number {
1503 return Err(ProviderError::other(std::io::Error::other(format!(
1504 "partial state trie frontier #{partial_state_trie} is ahead of Finish #{}",
1505 finish_checkpoint.block_number,
1506 ))))
1507 }
1508
1509 Ok((
1510 (partial_state_trie < finish_checkpoint.block_number).then_some(
1511 PartialStateTrieUnwindMarker {
1512 finish_block_number: finish_checkpoint.block_number,
1513 partial_state_trie,
1514 },
1515 ),
1516 false,
1517 ))
1518}
1519
1520const PARTIAL_STATE_TRIE_UNWIND_METADATA_KEY: &str = "partial_state_trie_unwind";
1522
1523fn write_partial_trie_unwind_marker(
1524 provider: &impl MetadataWriter,
1525 marker: PartialStateTrieUnwindMarker,
1526) -> ProviderResult<()> {
1527 provider.write_metadata(
1528 PARTIAL_STATE_TRIE_UNWIND_METADATA_KEY,
1529 serde_json::to_vec(&marker).map_err(ProviderError::other)?,
1530 )
1531}
1532
1533fn delete_partial_trie_unwind_marker(provider: &impl MetadataWriter) -> ProviderResult<()> {
1534 provider.delete_metadata(PARTIAL_STATE_TRIE_UNWIND_METADATA_KEY)
1535}
1536
1537#[cfg(test)]
1538mod tests {
1539 use super::{get_partial_trie_unwind_marker, LaunchContext, NodeConfig};
1540 use reth_config::Config;
1541 use reth_db_api::models::PartialStateTrieUnwindMarker;
1542 use reth_node_core::args::PruningArgs;
1543 use reth_provider::{MetadataProvider, ProviderResult, StageCheckpointReader};
1544 use reth_stages::{FinishCheckpoint, StageCheckpoint, StageId};
1545
1546 const EXTENSION: &str = "toml";
1547
1548 struct MockProvider(Option<Vec<u8>>, Option<StageCheckpoint>);
1549
1550 impl MetadataProvider for MockProvider {
1551 fn get_metadata(&self, _: &str) -> ProviderResult<Option<Vec<u8>>> {
1552 Ok(self.0.clone())
1553 }
1554 }
1555
1556 impl StageCheckpointReader for MockProvider {
1557 fn get_stage_checkpoint(&self, id: StageId) -> ProviderResult<Option<StageCheckpoint>> {
1558 assert_eq!(id, StageId::Finish);
1559 Ok(self.1)
1560 }
1561
1562 fn get_stage_checkpoint_progress(&self, _: StageId) -> ProviderResult<Option<Vec<u8>>> {
1563 Ok(None)
1564 }
1565
1566 fn get_all_checkpoints(&self) -> ProviderResult<Vec<(String, StageCheckpoint)>> {
1567 Ok(Vec::new())
1568 }
1569 }
1570
1571 fn with_tempdir(filename: &str, proc: fn(&std::path::Path)) {
1572 let temp_dir = tempfile::tempdir().unwrap();
1573 let config_path = temp_dir.path().join(filename).with_extension(EXTENSION);
1574 proc(&config_path);
1575 temp_dir.close().unwrap()
1576 }
1577
1578 #[test]
1579 fn test_save_prune_config() {
1580 with_tempdir("prune-store-test", |config_path| {
1581 let mut reth_config = Config::default();
1582 let node_config = NodeConfig {
1583 pruning: PruningArgs {
1584 full: true,
1585 minimal: false,
1586 block_interval: None,
1587 sender_recovery_full: false,
1588 sender_recovery_distance: None,
1589 sender_recovery_before: None,
1590 transaction_lookup_full: false,
1591 transaction_lookup_distance: None,
1592 transaction_lookup_before: None,
1593 receipts_full: false,
1594 receipts_pre_merge: false,
1595 receipts_distance: None,
1596 receipts_before: None,
1597 account_history_full: false,
1598 account_history_distance: None,
1599 account_history_before: None,
1600 storage_history_full: false,
1601 storage_history_distance: None,
1602 storage_history_before: None,
1603 bodies_pre_merge: false,
1604 bodies_distance: None,
1605 receipts_log_filter: None,
1606 bodies_before: None,
1607 minimum_distance: None,
1608 },
1609 ..NodeConfig::test()
1610 };
1611 LaunchContext::save_pruning_config(&mut reth_config, &node_config, config_path)
1612 .unwrap();
1613
1614 let loaded_config = Config::from_path(config_path).unwrap();
1615
1616 assert_eq!(reth_config, loaded_config);
1617 })
1618 }
1619
1620 #[test]
1621 fn get_partial_trie_unwind_marker_uses_partial_finish_checkpoint() {
1622 let finish_checkpoint = StageCheckpoint::new(42)
1623 .with_finish_stage_checkpoint(FinishCheckpoint { partial_state_trie: Some(21) });
1624 let expected =
1625 finish_checkpoint.finish_stage_checkpoint().unwrap().partial_state_trie().map(
1626 |partial_state_trie| PartialStateTrieUnwindMarker {
1627 finish_block_number: finish_checkpoint.block_number,
1628 partial_state_trie,
1629 },
1630 );
1631
1632 assert_eq!(
1633 get_partial_trie_unwind_marker(&MockProvider(None, Some(finish_checkpoint))).unwrap(),
1634 (expected, false)
1635 );
1636
1637 let genesis_checkpoint = StageCheckpoint::new(42)
1638 .with_finish_stage_checkpoint(FinishCheckpoint { partial_state_trie: Some(0) });
1639 let expected =
1640 genesis_checkpoint.finish_stage_checkpoint().unwrap().partial_state_trie().map(
1641 |partial_state_trie| PartialStateTrieUnwindMarker {
1642 finish_block_number: genesis_checkpoint.block_number,
1643 partial_state_trie,
1644 },
1645 );
1646
1647 assert_eq!(
1648 get_partial_trie_unwind_marker(&MockProvider(None, Some(genesis_checkpoint))).unwrap(),
1649 (expected, false)
1650 );
1651 }
1652
1653 #[test]
1654 fn get_partial_trie_unwind_marker_resumes_persisted_unwind() {
1655 let marker =
1656 PartialStateTrieUnwindMarker { finish_block_number: 42, partial_state_trie: 21 };
1657
1658 assert_eq!(
1659 get_partial_trie_unwind_marker(&MockProvider(
1660 Some(serde_json::to_vec(&marker).unwrap()),
1661 Some(StageCheckpoint::new(21)),
1662 ),)
1663 .unwrap(),
1664 (Some(marker), true)
1665 );
1666 assert_eq!(
1667 get_partial_trie_unwind_marker(&MockProvider(
1668 Some(serde_json::to_vec(&marker).unwrap()),
1669 None
1670 ),)
1671 .unwrap(),
1672 (Some(marker), true)
1673 );
1674 }
1675
1676 #[test]
1677 fn get_partial_trie_unwind_marker_ignores_non_lagging_or_missing_partial_checkpoint() {
1678 let matching_finish_checkpoint = StageCheckpoint::new(42)
1679 .with_finish_stage_checkpoint(FinishCheckpoint { partial_state_trie: Some(42) });
1680 let ahead_finish_checkpoint = StageCheckpoint::new(42)
1681 .with_finish_stage_checkpoint(FinishCheckpoint { partial_state_trie: Some(43) });
1682 let missing_partial_finish_checkpoint = StageCheckpoint::new(42)
1683 .with_finish_stage_checkpoint(FinishCheckpoint { partial_state_trie: None });
1684
1685 assert_eq!(
1686 get_partial_trie_unwind_marker(&MockProvider(None, Some(matching_finish_checkpoint)),)
1687 .unwrap(),
1688 (None, false)
1689 );
1690 assert_eq!(
1691 get_partial_trie_unwind_marker(&MockProvider(
1692 None,
1693 Some(missing_partial_finish_checkpoint)
1694 ),)
1695 .unwrap(),
1696 (None, false)
1697 );
1698 assert_eq!(
1699 get_partial_trie_unwind_marker(&MockProvider(None, None)).unwrap(),
1700 (None, false)
1701 );
1702
1703 let partial_frontier = ahead_finish_checkpoint
1704 .finish_stage_checkpoint()
1705 .and_then(|finish| finish.partial_state_trie());
1706 let result =
1707 get_partial_trie_unwind_marker(&MockProvider(None, Some(ahead_finish_checkpoint)));
1708 if partial_frontier.is_some() {
1709 let error = result.unwrap_err();
1710 assert!(error.to_string().contains("ahead of Finish"), "unexpected error: {error}");
1711 } else {
1712 assert_eq!(result.unwrap(), (None, false));
1713 }
1714 }
1715
1716 #[test]
1717 fn get_partial_trie_unwind_marker_rejects_invalid_persisted_marker() {
1718 let marker =
1719 PartialStateTrieUnwindMarker { finish_block_number: 42, partial_state_trie: 42 };
1720 let error = get_partial_trie_unwind_marker(&MockProvider(
1721 Some(serde_json::to_vec(&marker).unwrap()),
1722 None,
1723 ))
1724 .unwrap_err();
1725
1726 assert!(error.to_string().contains("is not below original Finish"));
1727 }
1728
1729 #[test]
1730 fn get_partial_trie_unwind_marker_rejects_malformed_metadata() {
1731 assert!(get_partial_trie_unwind_marker(&MockProvider(Some(vec![0xff]), None)).is_err());
1732 }
1733}