Skip to main content

reth_cli_commands/
common.rs

1//! Contains common `reth` arguments
2
3pub use reth_primitives_traits::header::HeaderMut;
4
5use alloy_primitives::B256;
6use clap::Parser;
7use reth_chainspec::EthChainSpec;
8use reth_cli::chainspec::ChainSpecParser;
9use reth_config::{config::EtlConfig, Config};
10use reth_consensus::noop::NoopConsensus;
11use reth_db::{init_db, open_db_read_only, DatabaseEnv};
12use reth_db_common::init::init_genesis_with_settings;
13use reth_downloaders::{bodies::noop::NoopBodiesDownloader, headers::noop::NoopHeaderDownloader};
14use reth_eth_wire::NetPrimitivesFor;
15use reth_evm::{noop::NoopEvmConfig, ConfigureEvm};
16use reth_network::NetworkEventListenerProvider;
17use reth_node_api::FullNodeTypesAdapter;
18use reth_node_builder::{
19    Node, NodeComponents, NodeComponentsBuilder, NodeTypes, NodeTypesWithDBAdapter,
20};
21use reth_node_core::{
22    args::{DatabaseArgs, DatadirArgs, StaticFilesArgs, StorageArgs},
23    dirs::{ChainPath, DataDirPath},
24};
25use reth_provider::{
26    providers::{
27        BlockchainProvider, NodeTypesForProvider, RocksDBProvider, StaticFileProvider,
28        StaticFileProviderBuilder,
29    },
30    ProviderFactory, StaticFileProviderFactory, StorageSettings,
31};
32use reth_stages::{sets::DefaultStages, Pipeline, PipelineTarget};
33use reth_static_file::StaticFileProducer;
34use std::{path::PathBuf, sync::Arc};
35use tokio::sync::watch;
36use tracing::{debug, info, warn};
37
38/// Struct to hold config and datadir paths
39#[derive(Debug, Parser)]
40pub struct EnvironmentArgs<C: ChainSpecParser> {
41    /// Parameters for datadir configuration
42    #[command(flatten)]
43    pub datadir: DatadirArgs,
44
45    /// The path to the configuration file to use.
46    #[arg(long, value_name = "FILE")]
47    pub config: Option<PathBuf>,
48
49    /// The chain this node is running.
50    ///
51    /// Possible values are either a built-in chain or the path to a chain specification file.
52    #[arg(
53        long,
54        value_name = "CHAIN_OR_PATH",
55        long_help = C::help_message(),
56        default_value = C::default_value(),
57        value_parser = C::parser(),
58        global = true
59    )]
60    pub chain: Arc<C::ChainSpec>,
61
62    /// All database related arguments
63    #[command(flatten)]
64    pub db: DatabaseArgs,
65
66    /// All static files related arguments
67    #[command(flatten)]
68    pub static_files: StaticFilesArgs,
69
70    /// Storage mode configuration (v2 vs v1/legacy)
71    #[command(flatten)]
72    pub storage: StorageArgs,
73}
74
75impl<C: ChainSpecParser> EnvironmentArgs<C> {
76    /// Returns the effective storage settings derived from `--storage.v2`.
77    ///
78    /// The base storage mode is determined by `--storage.v2`:
79    /// - When `--storage.v2` is set: uses [`StorageSettings::v2()`] defaults
80    /// - Otherwise: uses [`StorageSettings::base()`] defaults
81    pub fn storage_settings(&self) -> StorageSettings {
82        if self.storage.v2 {
83            StorageSettings::v2()
84        } else {
85            StorageSettings::base()
86        }
87    }
88
89    /// Initializes environment according to [`AccessRights`] and returns an instance of
90    /// [`Environment`].
91    ///
92    /// The provided `runtime` is used for parallel storage I/O.
93    pub fn init<N: CliNodeTypes>(
94        &self,
95        access: AccessRights,
96        runtime: reth_tasks::Runtime,
97    ) -> eyre::Result<Environment<N>>
98    where
99        C: ChainSpecParser<ChainSpec = N::ChainSpec>,
100    {
101        let data_dir = self.datadir.clone().resolve_datadir(self.chain.chain());
102        let db_path = data_dir.db();
103        let sf_path = data_dir.static_files();
104        let rocksdb_path = data_dir.rocksdb();
105
106        if access.is_read_write() {
107            reth_fs_util::create_dir_all(&db_path)?;
108            reth_fs_util::create_dir_all(&sf_path)?;
109            reth_fs_util::create_dir_all(&rocksdb_path)?;
110        }
111
112        let config_path = self.config.clone().unwrap_or_else(|| data_dir.config());
113
114        let mut config = Config::from_path(config_path)
115            .inspect_err(
116                |err| warn!(target: "reth::cli", %err, "Failed to load config file, using default"),
117            )
118            .unwrap_or_default();
119
120        // Make sure ETL doesn't default to /tmp/, but to whatever datadir is set to
121        if config.stages.etl.dir.is_none() {
122            config.stages.etl.dir = Some(EtlConfig::from_datadir(data_dir.data_dir()));
123        }
124        if config.stages.era.folder.is_none() {
125            config.stages.era = config.stages.era.with_datadir(data_dir.data_dir());
126        }
127
128        info!(target: "reth::cli", ?db_path, ?sf_path, "Opening storage");
129        let genesis_block_number = self.chain.genesis().number.unwrap_or_default();
130        let (db, sfp) = match access {
131            AccessRights::RW => (
132                init_db(db_path, self.db.database_args())?,
133                StaticFileProviderBuilder::read_write(sf_path)
134                    .with_metrics()
135                    .with_genesis_block_number(genesis_block_number)
136                    .build()?,
137            ),
138            AccessRights::RO | AccessRights::RoInconsistent => {
139                (open_db_read_only(&db_path, self.db.database_args())?, {
140                    let provider = StaticFileProviderBuilder::read_only(sf_path)
141                        .with_metrics()
142                        .with_genesis_block_number(genesis_block_number)
143                        .build()?;
144                    provider.watch_directory();
145                    provider
146                })
147            }
148        };
149        let rocksdb_provider = if !access.is_read_write() && !RocksDBProvider::exists(&rocksdb_path)
150        {
151            // RocksDB database doesn't exist yet (e.g. datadir restored from a snapshot
152            // or created before RocksDB storage). Create an empty one so read-only
153            // commands can proceed.
154            debug!(target: "reth::cli", ?rocksdb_path, "RocksDB not found, initializing empty database");
155            reth_fs_util::create_dir_all(&rocksdb_path)?;
156            RocksDBProvider::builder(data_dir.rocksdb())
157                .with_default_tables()
158                .with_database_log_level(self.db.log_level)
159                .build()?
160        } else {
161            RocksDBProvider::builder(data_dir.rocksdb())
162                .with_default_tables()
163                .with_database_log_level(self.db.log_level)
164                .with_read_only(!access.is_read_write())
165                .build()?
166        };
167
168        let provider_factory =
169            self.create_provider_factory(&config, db, sfp, rocksdb_provider, access, runtime)?;
170        if access.is_read_write() {
171            debug!(target: "reth::cli", chain=%self.chain.chain(), genesis=?self.chain.genesis_hash(), "Initializing genesis");
172            init_genesis_with_settings(&provider_factory, self.storage_settings())?;
173        }
174
175        Ok(Environment { config, provider_factory, data_dir })
176    }
177
178    /// Returns a [`ProviderFactory`] after executing consistency checks.
179    ///
180    /// If it's a read-write environment and an issue is found, it will attempt to heal (including a
181    /// pipeline unwind). Otherwise, it will print out a warning, advising the user to restart the
182    /// node to heal.
183    fn create_provider_factory<N: CliNodeTypes>(
184        &self,
185        config: &Config,
186        db: DatabaseEnv,
187        static_file_provider: StaticFileProvider<N::Primitives>,
188        rocksdb_provider: RocksDBProvider,
189        access: AccessRights,
190        runtime: reth_tasks::Runtime,
191    ) -> eyre::Result<ProviderFactory<NodeTypesWithDBAdapter<N, DatabaseEnv>>>
192    where
193        C: ChainSpecParser<ChainSpec = N::ChainSpec>,
194    {
195        let prune_modes = config.prune.segments.clone();
196        let factory = ProviderFactory::<NodeTypesWithDBAdapter<N, DatabaseEnv>>::new(
197            db,
198            self.chain.clone(),
199            static_file_provider,
200            rocksdb_provider,
201            runtime,
202        )?
203        .with_prune_modes(prune_modes.clone());
204
205        // Check for consistency between database and static files.
206        if !access.is_read_only_inconsistent() &&
207            let Some(unwind_target) =
208                factory.static_file_provider().check_consistency(&factory.provider()?)?
209        {
210            if factory.db_ref().is_read_only()? {
211                warn!(target: "reth::cli", ?unwind_target, "Inconsistent storage. Restart node to heal.");
212                return Ok(factory)
213            }
214
215            // Highly unlikely to happen, and given its destructive nature, it's better to panic
216            // instead.
217            assert_ne!(
218                unwind_target,
219                PipelineTarget::Unwind(0),
220                "A static file <> database inconsistency was found that would trigger an unwind to block 0"
221            );
222
223            info!(target: "reth::cli", unwind_target = %unwind_target, "Executing an unwind after a failed storage consistency check.");
224
225            let (_tip_tx, tip_rx) = watch::channel(B256::ZERO);
226
227            // Builds and executes an unwind-only pipeline
228            let mut pipeline = Pipeline::<NodeTypesWithDBAdapter<N, DatabaseEnv>>::builder()
229                .add_stages(DefaultStages::new(
230                    factory.clone(),
231                    tip_rx,
232                    Arc::new(NoopConsensus::default()),
233                    NoopHeaderDownloader::default(),
234                    NoopBodiesDownloader::default(),
235                    NoopEvmConfig::<N::Evm>::default(),
236                    config.stages.clone(),
237                    prune_modes.clone(),
238                    None,
239                ))
240                .build(factory.clone(), StaticFileProducer::new(factory.clone(), prune_modes));
241
242            // Move all applicable data from database to static files.
243            pipeline.move_to_static_files()?;
244            pipeline.unwind(unwind_target.unwind_target().expect("should exist"), None)?;
245        }
246
247        Ok(factory)
248    }
249}
250
251/// Environment built from [`EnvironmentArgs`].
252#[derive(Debug)]
253pub struct Environment<N: NodeTypes> {
254    /// Configuration for reth node
255    pub config: Config,
256    /// Provider factory.
257    pub provider_factory: ProviderFactory<NodeTypesWithDBAdapter<N, DatabaseEnv>>,
258    /// Datadir path.
259    pub data_dir: ChainPath<DataDirPath>,
260}
261
262/// Environment access rights.
263#[derive(Debug, Copy, Clone)]
264pub enum AccessRights {
265    /// Read-write access
266    RW,
267    /// Read-only access
268    RO,
269    /// Read-only access with possibly inconsistent data
270    RoInconsistent,
271}
272
273impl AccessRights {
274    /// Returns `true` if it requires read-write access to the environment.
275    pub const fn is_read_write(&self) -> bool {
276        matches!(self, Self::RW)
277    }
278
279    /// Returns `true` if it requires read-only access to the environment with possibly inconsistent
280    /// data.
281    pub const fn is_read_only_inconsistent(&self) -> bool {
282        matches!(self, Self::RoInconsistent)
283    }
284}
285
286/// Helper alias to satisfy `FullNodeTypes` bound on [`Node`] trait generic.
287type FullTypesAdapter<T> = FullNodeTypesAdapter<
288    T,
289    DatabaseEnv,
290    BlockchainProvider<NodeTypesWithDBAdapter<T, DatabaseEnv>>,
291>;
292
293/// Helper trait with a common set of requirements for the
294/// [`NodeTypes`] in CLI.
295pub trait CliNodeTypes: Node<FullTypesAdapter<Self>> + NodeTypesForProvider {
296    type Evm: ConfigureEvm<Primitives = Self::Primitives>;
297    type NetworkPrimitives: NetPrimitivesFor<Self::Primitives>;
298}
299
300impl<N> CliNodeTypes for N
301where
302    N: Node<FullTypesAdapter<Self>> + NodeTypesForProvider,
303{
304    type Evm = <<N::ComponentsBuilder as NodeComponentsBuilder<FullTypesAdapter<Self>>>::Components as NodeComponents<FullTypesAdapter<Self>>>::Evm;
305    type NetworkPrimitives = <<<N::ComponentsBuilder as NodeComponentsBuilder<FullTypesAdapter<Self>>>::Components as NodeComponents<FullTypesAdapter<Self>>>::Network as NetworkEventListenerProvider>::Primitives;
306}
307
308type EvmFor<N> = <<<N as Node<FullTypesAdapter<N>>>::ComponentsBuilder as NodeComponentsBuilder<
309    FullTypesAdapter<N>,
310>>::Components as NodeComponents<FullTypesAdapter<N>>>::Evm;
311
312type ConsensusFor<N> =
313    <<<N as Node<FullTypesAdapter<N>>>::ComponentsBuilder as NodeComponentsBuilder<
314        FullTypesAdapter<N>,
315    >>::Components as NodeComponents<FullTypesAdapter<N>>>::Consensus;
316
317/// Helper trait aggregating components required for the CLI.
318pub trait CliNodeComponents<N: CliNodeTypes>: Send + Sync + 'static {
319    /// Returns the configured EVM.
320    fn evm_config(&self) -> &EvmFor<N>;
321    /// Returns the consensus implementation.
322    fn consensus(&self) -> &ConsensusFor<N>;
323}
324
325impl<N: CliNodeTypes> CliNodeComponents<N> for (EvmFor<N>, ConsensusFor<N>) {
326    fn evm_config(&self) -> &EvmFor<N> {
327        &self.0
328    }
329
330    fn consensus(&self) -> &ConsensusFor<N> {
331        &self.1
332    }
333}
334
335/// Helper trait alias for an [`FnOnce`] producing [`CliNodeComponents`].
336pub trait CliComponentsBuilder<N: CliNodeTypes>:
337    FnOnce(Arc<N::ChainSpec>) -> Self::Components + Send + Sync + 'static
338{
339    type Components: CliNodeComponents<N>;
340}
341
342impl<N: CliNodeTypes, F, Comp> CliComponentsBuilder<N> for F
343where
344    F: FnOnce(Arc<N::ChainSpec>) -> Comp + Send + Sync + 'static,
345    Comp: CliNodeComponents<N>,
346{
347    type Components = Comp;
348}