Skip to main content

reth_cli_commands/stage/
run.rs

1//! Main `stage` command
2//!
3//! Stage debugging tool
4
5use crate::common::{AccessRights, CliNodeComponents, CliNodeTypes, Environment, EnvironmentArgs};
6use alloy_eips::BlockHashOrNumber;
7use alloy_primitives::Sealable;
8use clap::Parser;
9use reth_chainspec::{EthChainSpec, EthereumHardforks, Hardforks};
10use reth_cli::chainspec::ChainSpecParser;
11use reth_cli_runner::CliContext;
12use reth_cli_util::get_secret_key;
13use reth_config::config::{HashingConfig, SenderRecoveryConfig, TransactionLookupConfig};
14use reth_downloaders::{
15    bodies::bodies::BodiesDownloaderBuilder,
16    headers::reverse_headers::ReverseHeadersDownloaderBuilder,
17};
18use reth_exex::ExExManagerHandle;
19use reth_network::BlockDownloaderProvider;
20use reth_network_p2p::HeadersClient;
21use reth_node_builder::common::metrics_hooks;
22use reth_node_core::{
23    args::{NetworkArgs, StageEnum},
24    version::version_metadata,
25};
26use reth_node_metrics::{
27    chain::ChainSpecInfo,
28    server::{MetricServer, MetricServerConfig},
29    version::VersionInfo,
30};
31use reth_provider::{
32    ChainSpecProvider, DBProvider, DatabaseProviderFactory, StageCheckpointReader,
33    StageCheckpointWriter,
34};
35use reth_stages::{
36    stages::{
37        AccountHashingStage, BodyStage, ExecutionStage, HeaderStage, IndexAccountHistoryStage,
38        IndexStorageHistoryStage, MerkleStage, SenderRecoveryStage, StorageHashingStage,
39        TransactionLookupStage,
40    },
41    ExecInput, ExecOutput, ExecutionStageThresholds, Stage, StageExt, UnwindInput, UnwindOutput,
42};
43use std::{any::Any, net::SocketAddr, sync::Arc, time::Instant};
44use tokio::sync::watch;
45use tracing::*;
46
47/// `reth stage` command
48#[derive(Debug, Parser)]
49pub struct Command<C: ChainSpecParser> {
50    #[command(flatten)]
51    env: EnvironmentArgs<C>,
52
53    /// Enable Prometheus metrics.
54    ///
55    /// The metrics will be served at the given interface and port.
56    #[arg(long, value_name = "SOCKET")]
57    metrics: Option<SocketAddr>,
58
59    /// The name of the stage to run
60    #[arg(value_enum)]
61    stage: StageEnum,
62
63    /// The height to start at
64    #[arg(long)]
65    from: u64,
66
67    /// The end of the stage
68    #[arg(long, short)]
69    to: u64,
70
71    /// Batch size for stage execution and unwind
72    #[arg(long)]
73    batch_size: Option<u64>,
74
75    /// Normally, running the stage requires unwinding for stages that already
76    /// have been run, in order to not rewrite to the same database slots.
77    ///
78    /// You can optionally skip the unwinding phase if you're syncing a block
79    /// range that has not been synced before.
80    #[arg(long, short)]
81    skip_unwind: bool,
82
83    /// Commits the changes in the database. WARNING: potentially destructive.
84    ///
85    /// Useful when you want to run diagnostics on the database.
86    ///
87    /// NOTE: This flag is currently required for the headers, bodies, and execution stages because
88    /// they use static files and must commit to properly unwind and run.
89    // TODO: We should consider allowing to run hooks at the end of the stage run,
90    // e.g. query the DB size, or any table data.
91    #[arg(long, short)]
92    commit: bool,
93
94    /// Save stage checkpoints
95    #[arg(long)]
96    checkpoints: bool,
97
98    #[command(flatten)]
99    network: NetworkArgs,
100}
101
102impl<C: ChainSpecParser<ChainSpec: EthChainSpec + Hardforks + EthereumHardforks>> Command<C> {
103    /// Execute `stage` command
104    pub async fn execute<N, Comp, F>(self, ctx: CliContext, components: F) -> eyre::Result<()>
105    where
106        N: CliNodeTypes<ChainSpec = C::ChainSpec>,
107        Comp: CliNodeComponents<N>,
108        F: FnOnce(Arc<C::ChainSpec>) -> Comp,
109    {
110        // Quit early if the stages requires a commit and `--commit` is not provided.
111        if self.requires_commit() && !self.commit {
112            return Err(eyre::eyre!(
113                "The stage {} requires overwriting existing static files and must commit, but `--commit` was not provided. Please pass `--commit` and try again.",
114                self.stage.to_string()
115            ));
116        }
117
118        // Raise the fd limit of the process.
119        // Does not do anything on windows.
120        let _ = fdlimit::raise_fd_limit();
121
122        let Environment { provider_factory, config, data_dir } =
123            self.env.init::<N>(AccessRights::RW)?;
124
125        let mut provider_rw = provider_factory.database_provider_rw()?;
126        let components = components(provider_factory.chain_spec());
127
128        if let Some(listen_addr) = self.metrics {
129            let config = MetricServerConfig::new(
130                listen_addr,
131                VersionInfo {
132                    version: version_metadata().cargo_pkg_version.as_ref(),
133                    build_timestamp: version_metadata().vergen_build_timestamp.as_ref(),
134                    cargo_features: version_metadata().vergen_cargo_features.as_ref(),
135                    git_sha: version_metadata().vergen_git_sha.as_ref(),
136                    target_triple: version_metadata().vergen_cargo_target_triple.as_ref(),
137                    build_profile: version_metadata().build_profile_name.as_ref(),
138                },
139                ChainSpecInfo { name: provider_factory.chain_spec().chain().to_string() },
140                ctx.task_executor,
141                metrics_hooks(&provider_factory),
142                data_dir.pprof_dumps(),
143            );
144
145            MetricServer::new(config).serve().await?;
146        }
147
148        let batch_size = self.batch_size.unwrap_or(self.to.saturating_sub(self.from) + 1);
149
150        let etl_config = config.stages.etl.clone();
151        let prune_modes = config.prune.segments.clone();
152
153        let (mut exec_stage, mut unwind_stage): (Box<dyn Stage<_>>, Option<Box<dyn Stage<_>>>) =
154            match self.stage {
155                StageEnum::Headers => {
156                    let consensus = Arc::new(components.consensus().clone());
157
158                    let network_secret_path = self
159                        .network
160                        .p2p_secret_key
161                        .clone()
162                        .unwrap_or_else(|| data_dir.p2p_secret());
163                    let p2p_secret_key = get_secret_key(&network_secret_path)?;
164
165                    let default_peers_path = data_dir.known_peers();
166
167                    let network = self
168                        .network
169                        .network_config::<N::NetworkPrimitives>(
170                            &config,
171                            provider_factory.chain_spec(),
172                            p2p_secret_key,
173                            default_peers_path,
174                        )
175                        .build(provider_factory.clone())
176                        .start_network()
177                        .await?;
178                    let fetch_client = Arc::new(network.fetch_client().await?);
179
180                    // Use `to` as the tip for the stage
181                    let tip = loop {
182                        match fetch_client.get_header(BlockHashOrNumber::Number(self.to)).await {
183                            Ok(header) => {
184                                if let Some(header) = header.into_data() {
185                                    break header
186                                }
187                            }
188                            Err(error) if error.is_retryable() => {
189                                warn!(target: "reth::cli", "Error requesting header: {error}. Retrying...")
190                            }
191                            Err(error) => return Err(error.into()),
192                        }
193                    };
194                    let (_, rx) = watch::channel(tip.hash_slow());
195                    (
196                        Box::new(HeaderStage::new(
197                            provider_factory.clone(),
198                            ReverseHeadersDownloaderBuilder::new(config.stages.headers)
199                                .build(fetch_client, consensus.clone()),
200                            rx,
201                            etl_config,
202                        )),
203                        None,
204                    )
205                }
206                StageEnum::Bodies => {
207                    let consensus = Arc::new(components.consensus().clone());
208
209                    let mut config = config;
210                    config.peers.trusted_nodes_only = self.network.trusted_only;
211                    config.peers.trusted_nodes.extend(self.network.trusted_peers.clone());
212
213                    let network_secret_path = self
214                        .network
215                        .p2p_secret_key
216                        .clone()
217                        .unwrap_or_else(|| data_dir.p2p_secret());
218                    let p2p_secret_key = get_secret_key(&network_secret_path)?;
219
220                    let default_peers_path = data_dir.known_peers();
221
222                    let network = self
223                        .network
224                        .network_config::<N::NetworkPrimitives>(
225                            &config,
226                            provider_factory.chain_spec(),
227                            p2p_secret_key,
228                            default_peers_path,
229                        )
230                        .build(provider_factory.clone())
231                        .start_network()
232                        .await?;
233                    let fetch_client = Arc::new(network.fetch_client().await?);
234
235                    let stage = BodyStage::new(
236                        BodiesDownloaderBuilder::default()
237                            .with_stream_batch_size(batch_size as usize)
238                            .with_request_limit(config.stages.bodies.downloader_request_limit)
239                            .with_max_buffered_blocks_size_bytes(
240                                config.stages.bodies.downloader_max_buffered_blocks_size_bytes,
241                            )
242                            .with_concurrent_requests_range(
243                                config.stages.bodies.downloader_min_concurrent_requests..=
244                                    config.stages.bodies.downloader_max_concurrent_requests,
245                            )
246                            .build(fetch_client, consensus.clone(), provider_factory.clone()),
247                    );
248                    (Box::new(stage), None)
249                }
250                StageEnum::Senders => (
251                    Box::new(SenderRecoveryStage::new(
252                        SenderRecoveryConfig { commit_threshold: batch_size },
253                        None,
254                    )),
255                    None,
256                ),
257                StageEnum::Execution => (
258                    Box::new(ExecutionStage::new(
259                        components.evm_config().clone(),
260                        Arc::new(components.consensus().clone()),
261                        ExecutionStageThresholds {
262                            max_blocks: Some(batch_size),
263                            max_changes: None,
264                            max_cumulative_gas: None,
265                            max_duration: None,
266                        },
267                        config.stages.merkle.incremental_threshold,
268                        ExExManagerHandle::empty(),
269                    )),
270                    None,
271                ),
272                StageEnum::TxLookup => (
273                    Box::new(TransactionLookupStage::new(
274                        TransactionLookupConfig { chunk_size: batch_size },
275                        etl_config,
276                        prune_modes.transaction_lookup,
277                    )),
278                    None,
279                ),
280                StageEnum::AccountHashing => (
281                    Box::new(AccountHashingStage::new(
282                        HashingConfig { clean_threshold: 1, commit_threshold: batch_size },
283                        etl_config,
284                    )),
285                    None,
286                ),
287                StageEnum::StorageHashing => (
288                    Box::new(StorageHashingStage::new(
289                        HashingConfig { clean_threshold: 1, commit_threshold: batch_size },
290                        etl_config,
291                    )),
292                    None,
293                ),
294                StageEnum::Merkle => (
295                    Box::new(MerkleStage::new_execution(
296                        config.stages.merkle.rebuild_threshold,
297                        config.stages.merkle.incremental_threshold,
298                    )),
299                    Some(Box::new(MerkleStage::default_unwind())),
300                ),
301                StageEnum::AccountHistory => (
302                    Box::new(IndexAccountHistoryStage::new(
303                        config.stages.index_account_history,
304                        etl_config,
305                        prune_modes.account_history,
306                    )),
307                    None,
308                ),
309                StageEnum::StorageHistory => (
310                    Box::new(IndexStorageHistoryStage::new(
311                        config.stages.index_storage_history,
312                        etl_config,
313                        prune_modes.storage_history,
314                    )),
315                    None,
316                ),
317                _ => return Ok(()),
318            };
319        if let Some(unwind_stage) = &unwind_stage {
320            assert_eq!((*exec_stage).type_id(), (**unwind_stage).type_id());
321        }
322
323        let checkpoint = provider_rw.get_stage_checkpoint(exec_stage.id())?.unwrap_or_default();
324
325        let unwind_stage = unwind_stage.as_mut().unwrap_or(&mut exec_stage);
326
327        let mut unwind = UnwindInput {
328            checkpoint: checkpoint.with_block_number(self.to),
329            unwind_to: self.from,
330            bad_block: None,
331        };
332
333        if !self.skip_unwind {
334            while unwind.checkpoint.block_number > self.from {
335                let UnwindOutput { checkpoint } = unwind_stage.unwind(&provider_rw, unwind)?;
336                unwind.checkpoint = checkpoint;
337
338                if self.checkpoints {
339                    provider_rw.save_stage_checkpoint(unwind_stage.id(), checkpoint)?;
340                }
341
342                if self.commit {
343                    provider_rw.commit()?;
344                    provider_rw = provider_factory.database_provider_rw()?;
345                }
346            }
347        }
348
349        let mut input = ExecInput {
350            target: Some(self.to),
351            checkpoint: Some(checkpoint.with_block_number(self.from)),
352        };
353
354        let start = Instant::now();
355        info!(target: "reth::cli", stage = %self.stage, "Executing stage");
356        loop {
357            exec_stage.execute_ready(input).await?;
358            let ExecOutput { checkpoint, done } = exec_stage.execute(&provider_rw, input)?;
359
360            input.checkpoint = Some(checkpoint);
361
362            if self.checkpoints {
363                provider_rw.save_stage_checkpoint(exec_stage.id(), checkpoint)?;
364            }
365            if self.commit {
366                provider_rw.commit()?;
367                provider_rw = provider_factory.database_provider_rw()?;
368            }
369
370            if done {
371                break
372            }
373        }
374        info!(target: "reth::cli", stage = %self.stage, time = ?start.elapsed(), "Finished stage");
375
376        Ok(())
377    }
378}
379
380impl<C: ChainSpecParser> Command<C> {
381    /// Returns the underlying chain being used to run this command
382    pub fn chain_spec(&self) -> Option<&Arc<C::ChainSpec>> {
383        Some(&self.env.chain)
384    }
385
386    /// Returns whether or not the configured stage requires committing.
387    ///
388    /// This is the case for stages that mainly modify static files, as there is no way to unwind
389    /// these stages without committing anyways. This is because static files do not have
390    /// transactions and we cannot change the view of headers without writing.
391    pub fn requires_commit(&self) -> bool {
392        matches!(self.stage, StageEnum::Headers | StageEnum::Bodies | StageEnum::Execution)
393    }
394}