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, RocksDbArgs, StaticFilesArgs},
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    /// All `RocksDB` related arguments
71    #[command(flatten)]
72    pub rocksdb: RocksDbArgs,
73}
74
75impl<C: ChainSpecParser> EnvironmentArgs<C> {
76    /// Returns the effective storage settings derived from static-file and `RocksDB` CLI args.
77    pub fn storage_settings(&self) -> StorageSettings {
78        StorageSettings::base()
79            .with_receipts_in_static_files(self.static_files.receipts)
80            .with_transaction_senders_in_static_files(self.static_files.transaction_senders)
81            .with_account_changesets_in_static_files(self.static_files.account_changesets)
82            .with_transaction_hash_numbers_in_rocksdb(self.rocksdb.all || self.rocksdb.tx_hash)
83            .with_storages_history_in_rocksdb(self.rocksdb.all || self.rocksdb.storages_history)
84            .with_account_history_in_rocksdb(self.rocksdb.all || self.rocksdb.account_history)
85    }
86
87    /// Initializes environment according to [`AccessRights`] and returns an instance of
88    /// [`Environment`].
89    pub fn init<N: CliNodeTypes>(&self, access: AccessRights) -> eyre::Result<Environment<N>>
90    where
91        C: ChainSpecParser<ChainSpec = N::ChainSpec>,
92    {
93        let data_dir = self.datadir.clone().resolve_datadir(self.chain.chain());
94        let db_path = data_dir.db();
95        let sf_path = data_dir.static_files();
96        let rocksdb_path = data_dir.rocksdb();
97
98        if access.is_read_write() {
99            reth_fs_util::create_dir_all(&db_path)?;
100            reth_fs_util::create_dir_all(&sf_path)?;
101            reth_fs_util::create_dir_all(&rocksdb_path)?;
102        }
103
104        let config_path = self.config.clone().unwrap_or_else(|| data_dir.config());
105
106        let mut config = Config::from_path(config_path)
107            .inspect_err(
108                |err| warn!(target: "reth::cli", %err, "Failed to load config file, using default"),
109            )
110            .unwrap_or_default();
111
112        // Make sure ETL doesn't default to /tmp/, but to whatever datadir is set to
113        if config.stages.etl.dir.is_none() {
114            config.stages.etl.dir = Some(EtlConfig::from_datadir(data_dir.data_dir()));
115        }
116        if config.stages.era.folder.is_none() {
117            config.stages.era = config.stages.era.with_datadir(data_dir.data_dir());
118        }
119
120        info!(target: "reth::cli", ?db_path, ?sf_path, "Opening storage");
121        let genesis_block_number = self.chain.genesis().number.unwrap_or_default();
122        let (db, sfp) = match access {
123            AccessRights::RW => (
124                Arc::new(init_db(db_path, self.db.database_args())?),
125                StaticFileProviderBuilder::read_write(sf_path)
126                    .with_genesis_block_number(genesis_block_number)
127                    .build()?,
128            ),
129            AccessRights::RO | AccessRights::RoInconsistent => {
130                (Arc::new(open_db_read_only(&db_path, self.db.database_args())?), {
131                    let provider = StaticFileProviderBuilder::read_only(sf_path)
132                        .with_genesis_block_number(genesis_block_number)
133                        .build()?;
134                    provider.watch_directory();
135                    provider
136                })
137            }
138        };
139        let rocksdb_provider = RocksDBProvider::builder(data_dir.rocksdb())
140            .with_default_tables()
141            .with_database_log_level(self.db.log_level)
142            .with_read_only(!access.is_read_write())
143            .build()?;
144
145        let provider_factory =
146            self.create_provider_factory(&config, db, sfp, rocksdb_provider, access)?;
147        if access.is_read_write() {
148            debug!(target: "reth::cli", chain=%self.chain.chain(), genesis=?self.chain.genesis_hash(), "Initializing genesis");
149            init_genesis_with_settings(&provider_factory, self.storage_settings())?;
150        }
151
152        Ok(Environment { config, provider_factory, data_dir })
153    }
154
155    /// Returns a [`ProviderFactory`] after executing consistency checks.
156    ///
157    /// If it's a read-write environment and an issue is found, it will attempt to heal (including a
158    /// pipeline unwind). Otherwise, it will print out a warning, advising the user to restart the
159    /// node to heal.
160    fn create_provider_factory<N: CliNodeTypes>(
161        &self,
162        config: &Config,
163        db: Arc<DatabaseEnv>,
164        static_file_provider: StaticFileProvider<N::Primitives>,
165        rocksdb_provider: RocksDBProvider,
166        access: AccessRights,
167    ) -> eyre::Result<ProviderFactory<NodeTypesWithDBAdapter<N, Arc<DatabaseEnv>>>>
168    where
169        C: ChainSpecParser<ChainSpec = N::ChainSpec>,
170    {
171        let prune_modes = config.prune.segments.clone();
172        let factory = ProviderFactory::<NodeTypesWithDBAdapter<N, Arc<DatabaseEnv>>>::new(
173            db,
174            self.chain.clone(),
175            static_file_provider,
176            rocksdb_provider,
177        )?
178        .with_prune_modes(prune_modes.clone());
179
180        // Check for consistency between database and static files.
181        if !access.is_read_only_inconsistent() &&
182            let Some(unwind_target) =
183                factory.static_file_provider().check_consistency(&factory.provider()?)?
184        {
185            if factory.db_ref().is_read_only()? {
186                warn!(target: "reth::cli", ?unwind_target, "Inconsistent storage. Restart node to heal.");
187                return Ok(factory)
188            }
189
190            // Highly unlikely to happen, and given its destructive nature, it's better to panic
191            // instead.
192            assert_ne!(
193                unwind_target,
194                PipelineTarget::Unwind(0),
195                "A static file <> database inconsistency was found that would trigger an unwind to block 0"
196            );
197
198            info!(target: "reth::cli", unwind_target = %unwind_target, "Executing an unwind after a failed storage consistency check.");
199
200            let (_tip_tx, tip_rx) = watch::channel(B256::ZERO);
201
202            // Builds and executes an unwind-only pipeline
203            let mut pipeline = Pipeline::<NodeTypesWithDBAdapter<N, Arc<DatabaseEnv>>>::builder()
204                .add_stages(DefaultStages::new(
205                    factory.clone(),
206                    tip_rx,
207                    Arc::new(NoopConsensus::default()),
208                    NoopHeaderDownloader::default(),
209                    NoopBodiesDownloader::default(),
210                    NoopEvmConfig::<N::Evm>::default(),
211                    config.stages.clone(),
212                    prune_modes.clone(),
213                    None,
214                ))
215                .build(factory.clone(), StaticFileProducer::new(factory.clone(), prune_modes));
216
217            // Move all applicable data from database to static files.
218            pipeline.move_to_static_files()?;
219            pipeline.unwind(unwind_target.unwind_target().expect("should exist"), None)?;
220        }
221
222        Ok(factory)
223    }
224}
225
226/// Environment built from [`EnvironmentArgs`].
227#[derive(Debug)]
228pub struct Environment<N: NodeTypes> {
229    /// Configuration for reth node
230    pub config: Config,
231    /// Provider factory.
232    pub provider_factory: ProviderFactory<NodeTypesWithDBAdapter<N, Arc<DatabaseEnv>>>,
233    /// Datadir path.
234    pub data_dir: ChainPath<DataDirPath>,
235}
236
237/// Environment access rights.
238#[derive(Debug, Copy, Clone)]
239pub enum AccessRights {
240    /// Read-write access
241    RW,
242    /// Read-only access
243    RO,
244    /// Read-only access with possibly inconsistent data
245    RoInconsistent,
246}
247
248impl AccessRights {
249    /// Returns `true` if it requires read-write access to the environment.
250    pub const fn is_read_write(&self) -> bool {
251        matches!(self, Self::RW)
252    }
253
254    /// Returns `true` if it requires read-only access to the environment with possibly inconsistent
255    /// data.
256    pub const fn is_read_only_inconsistent(&self) -> bool {
257        matches!(self, Self::RoInconsistent)
258    }
259}
260
261/// Helper alias to satisfy `FullNodeTypes` bound on [`Node`] trait generic.
262type FullTypesAdapter<T> = FullNodeTypesAdapter<
263    T,
264    Arc<DatabaseEnv>,
265    BlockchainProvider<NodeTypesWithDBAdapter<T, Arc<DatabaseEnv>>>,
266>;
267
268/// Helper trait with a common set of requirements for the
269/// [`NodeTypes`] in CLI.
270pub trait CliNodeTypes: Node<FullTypesAdapter<Self>> + NodeTypesForProvider {
271    type Evm: ConfigureEvm<Primitives = Self::Primitives>;
272    type NetworkPrimitives: NetPrimitivesFor<Self::Primitives>;
273}
274
275impl<N> CliNodeTypes for N
276where
277    N: Node<FullTypesAdapter<Self>> + NodeTypesForProvider,
278{
279    type Evm = <<N::ComponentsBuilder as NodeComponentsBuilder<FullTypesAdapter<Self>>>::Components as NodeComponents<FullTypesAdapter<Self>>>::Evm;
280    type NetworkPrimitives = <<<N::ComponentsBuilder as NodeComponentsBuilder<FullTypesAdapter<Self>>>::Components as NodeComponents<FullTypesAdapter<Self>>>::Network as NetworkEventListenerProvider>::Primitives;
281}
282
283type EvmFor<N> = <<<N as Node<FullTypesAdapter<N>>>::ComponentsBuilder as NodeComponentsBuilder<
284    FullTypesAdapter<N>,
285>>::Components as NodeComponents<FullTypesAdapter<N>>>::Evm;
286
287type ConsensusFor<N> =
288    <<<N as Node<FullTypesAdapter<N>>>::ComponentsBuilder as NodeComponentsBuilder<
289        FullTypesAdapter<N>,
290    >>::Components as NodeComponents<FullTypesAdapter<N>>>::Consensus;
291
292/// Helper trait aggregating components required for the CLI.
293pub trait CliNodeComponents<N: CliNodeTypes>: Send + Sync + 'static {
294    /// Returns the configured EVM.
295    fn evm_config(&self) -> &EvmFor<N>;
296    /// Returns the consensus implementation.
297    fn consensus(&self) -> &ConsensusFor<N>;
298}
299
300impl<N: CliNodeTypes> CliNodeComponents<N> for (EvmFor<N>, ConsensusFor<N>) {
301    fn evm_config(&self) -> &EvmFor<N> {
302        &self.0
303    }
304
305    fn consensus(&self) -> &ConsensusFor<N> {
306        &self.1
307    }
308}
309
310/// Helper trait alias for an [`FnOnce`] producing [`CliNodeComponents`].
311pub trait CliComponentsBuilder<N: CliNodeTypes>:
312    FnOnce(Arc<N::ChainSpec>) -> Self::Components + Send + Sync + 'static
313{
314    type Components: CliNodeComponents<N>;
315}
316
317impl<N: CliNodeTypes, F, Comp> CliComponentsBuilder<N> for F
318where
319    F: FnOnce(Arc<N::ChainSpec>) -> Comp + Send + Sync + 'static,
320    Comp: CliNodeComponents<N>,
321{
322    type Components = Comp;
323}