1pub 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#[derive(Debug, Parser)]
40pub struct EnvironmentArgs<C: ChainSpecParser> {
41 #[command(flatten)]
43 pub datadir: DatadirArgs,
44
45 #[arg(long, value_name = "FILE")]
47 pub config: Option<PathBuf>,
48
49 #[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 #[command(flatten)]
64 pub db: DatabaseArgs,
65
66 #[command(flatten)]
68 pub static_files: StaticFilesArgs,
69
70 #[command(flatten)]
72 pub storage: StorageArgs,
73}
74
75impl<C: ChainSpecParser> EnvironmentArgs<C> {
76 pub fn storage_settings(&self) -> StorageSettings {
82 if self.storage.v2 {
83 StorageSettings::v2()
84 } else {
85 StorageSettings::v1()
86 }
87 }
88
89 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 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 StaticFileProviderBuilder::read_only(sf_path)
141 .with_metrics()
142 .with_genesis_block_number(genesis_block_number)
143 .build()?,
144 ),
145 };
146 let rocksdb_provider = if !access.is_read_write() && !RocksDBProvider::exists(&rocksdb_path)
147 {
148 debug!(target: "reth::cli", ?rocksdb_path, "RocksDB not found, initializing empty database");
152 reth_fs_util::create_dir_all(&rocksdb_path)?;
153 RocksDBProvider::builder(data_dir.rocksdb())
154 .with_default_tables()
155 .with_database_log_level(self.db.log_level)
156 .build()?
157 } else {
158 RocksDBProvider::builder(data_dir.rocksdb())
159 .with_default_tables()
160 .with_database_log_level(self.db.log_level)
161 .with_read_only(!access.is_read_write())
162 .build()?
163 };
164
165 let provider_factory =
166 self.create_provider_factory(&config, db, sfp, rocksdb_provider, access, runtime)?;
167 if access.is_read_write() {
168 debug!(target: "reth::cli", chain=%self.chain.chain(), genesis=?self.chain.genesis_hash(), "Initializing genesis");
169 init_genesis_with_settings(&provider_factory, self.storage_settings())?;
170 }
171
172 Ok(Environment { config, provider_factory, data_dir })
173 }
174
175 fn create_provider_factory<N: CliNodeTypes>(
181 &self,
182 config: &Config,
183 db: DatabaseEnv,
184 static_file_provider: StaticFileProvider<N::Primitives>,
185 rocksdb_provider: RocksDBProvider,
186 access: AccessRights,
187 runtime: reth_tasks::Runtime,
188 ) -> eyre::Result<ProviderFactory<NodeTypesWithDBAdapter<N, DatabaseEnv>>>
189 where
190 C: ChainSpecParser<ChainSpec = N::ChainSpec>,
191 {
192 let factory = ProviderFactory::<NodeTypesWithDBAdapter<N, DatabaseEnv>>::new(
193 db,
194 self.chain.clone(),
195 static_file_provider,
196 rocksdb_provider,
197 runtime,
198 )?
199 .with_prune_modes(config.prune.segments.clone())
200 .with_minimum_pruning_distance(config.prune.minimum_pruning_distance);
201
202 if !access.is_read_only_inconsistent() &&
204 let Some(unwind_target) =
205 factory.static_file_provider().check_consistency(&factory.provider()?)?
206 {
207 if factory.db_ref().is_read_only()? {
208 warn!(target: "reth::cli", ?unwind_target, "Inconsistent storage. Restart node to heal.");
209 return Ok(factory)
210 }
211
212 assert_ne!(
215 unwind_target,
216 PipelineTarget::Unwind(0),
217 "A static file <> database inconsistency was found that would trigger an unwind to block 0"
218 );
219
220 info!(target: "reth::cli", unwind_target = %unwind_target, "Executing an unwind after a failed storage consistency check.");
221
222 let (_tip_tx, tip_rx) = watch::channel(B256::ZERO);
223
224 let mut pipeline = Pipeline::<NodeTypesWithDBAdapter<N, DatabaseEnv>>::builder()
226 .add_stages(DefaultStages::new(
227 factory.clone(),
228 tip_rx,
229 Arc::new(NoopConsensus::default()),
230 NoopHeaderDownloader::default(),
231 NoopBodiesDownloader::default(),
232 NoopEvmConfig::<N::Evm>::default(),
233 config.stages.clone(),
234 config.prune.segments.clone(),
235 None,
236 ))
237 .build(
238 factory.clone(),
239 StaticFileProducer::new(factory.clone(), config.prune.segments.clone()),
240 );
241
242 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#[derive(Debug)]
253pub struct Environment<N: NodeTypes> {
254 pub config: Config,
256 pub provider_factory: ProviderFactory<NodeTypesWithDBAdapter<N, DatabaseEnv>>,
258 pub data_dir: ChainPath<DataDirPath>,
260}
261
262#[derive(Debug, Copy, Clone)]
264pub enum AccessRights {
265 RW,
267 RO,
269 RoInconsistent,
271}
272
273impl AccessRights {
274 pub const fn is_read_write(&self) -> bool {
276 matches!(self, Self::RW)
277 }
278
279 pub const fn is_read_only_inconsistent(&self) -> bool {
282 matches!(self, Self::RoInconsistent)
283 }
284}
285
286type FullTypesAdapter<T> = FullNodeTypesAdapter<
288 T,
289 DatabaseEnv,
290 BlockchainProvider<NodeTypesWithDBAdapter<T, DatabaseEnv>>,
291>;
292
293pub 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
317pub trait CliNodeComponents<N: CliNodeTypes>: Send + Sync + 'static {
319 fn evm_config(&self) -> &EvmFor<N>;
321 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
335pub 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}