Skip to main content

reth_cli_commands/stage/dump/
mod.rs

1//! Database debugging tool
2use crate::common::{AccessRights, CliNodeComponents, CliNodeTypes, Environment, EnvironmentArgs};
3use clap::Parser;
4use reth_chainspec::{EthChainSpec, EthereumHardforks};
5use reth_cli::chainspec::ChainSpecParser;
6use reth_db::{init_db, mdbx::DatabaseArguments, DatabaseEnv};
7use reth_db_api::{
8    cursor::DbCursorRO, database::Database, models::ClientVersion, table::TableImporter, tables,
9    transaction::DbTx,
10};
11use reth_db_common::DbTool;
12use reth_node_builder::NodeTypesWithDB;
13use reth_node_core::{
14    args::DatadirArgs,
15    dirs::{DataDirPath, PlatformPath},
16};
17use std::{path::PathBuf, sync::Arc};
18use tracing::info;
19
20mod hashing_storage;
21use hashing_storage::dump_hashing_storage_stage;
22
23mod hashing_account;
24use hashing_account::dump_hashing_account_stage;
25
26mod execution;
27use execution::dump_execution_stage;
28
29mod merkle;
30use merkle::dump_merkle_stage;
31
32/// `reth dump-stage` command.
33///
34/// Note: mutates the source datadir (unwinds hashing/merkle/execution before copying tables).
35/// Stop the node and back up the datadir first.
36#[derive(Debug, Parser)]
37pub struct Command<C: ChainSpecParser> {
38    #[command(flatten)]
39    env: EnvironmentArgs<C>,
40
41    #[command(subcommand)]
42    command: Stages,
43}
44
45/// Supported stages to be dumped
46#[derive(Debug, Clone, Parser)]
47pub enum Stages {
48    /// Execution stage.
49    Execution(StageCommand),
50    /// `StorageHashing` stage.
51    StorageHashing(StageCommand),
52    /// `AccountHashing` stage.
53    AccountHashing(StageCommand),
54    /// Merkle stage.
55    Merkle(StageCommand),
56}
57
58/// Stage command that takes a range
59#[derive(Debug, Clone, Parser)]
60pub struct StageCommand {
61    /// The path to the new datadir folder.
62    #[arg(long, value_name = "OUTPUT_PATH", verbatim_doc_comment)]
63    output_datadir: PlatformPath<DataDirPath>,
64
65    /// From which block.
66    #[arg(long, short)]
67    from: u64,
68    /// To which block.
69    #[arg(long, short)]
70    to: u64,
71    /// If passed, it will dry-run a stage execution from the newly created database right after
72    /// dumping.
73    #[arg(long, short, default_value = "false")]
74    dry_run: bool,
75}
76
77macro_rules! handle_stage {
78    ($stage_fn:ident, $tool:expr, $command:expr, $runtime:expr) => {{
79        let StageCommand { output_datadir, from, to, dry_run, .. } = $command;
80        let output_datadir =
81            output_datadir.with_chain($tool.chain().chain(), DatadirArgs::default());
82        $stage_fn($tool, *from, *to, output_datadir, *dry_run, $runtime).await?
83    }};
84
85    ($stage_fn:ident, $tool:expr, $command:expr, $executor:expr, $consensus:expr, $runtime:expr) => {{
86        let StageCommand { output_datadir, from, to, dry_run, .. } = $command;
87        let output_datadir =
88            output_datadir.with_chain($tool.chain().chain(), DatadirArgs::default());
89        $stage_fn($tool, *from, *to, output_datadir, *dry_run, $executor, $consensus, $runtime)
90            .await?
91    }};
92}
93
94impl<C: ChainSpecParser<ChainSpec: EthChainSpec + EthereumHardforks>> Command<C> {
95    /// Execute `dump-stage` command
96    pub async fn execute<N, Comp, F>(
97        self,
98        components: F,
99        runtime: reth_tasks::Runtime,
100    ) -> eyre::Result<()>
101    where
102        N: CliNodeTypes<ChainSpec = C::ChainSpec>,
103        Comp: CliNodeComponents<N>,
104        F: FnOnce(Arc<C::ChainSpec>) -> Comp,
105    {
106        // `unwind_and_copy` opens a RW provider on the source datadir, so open RW here.
107        let Environment { provider_factory, .. } =
108            self.env.init::<N>(AccessRights::RW, runtime.clone())?;
109        let tool = DbTool::new(provider_factory)?;
110        let components = components(tool.chain());
111        let evm_config = components.evm_config().clone();
112        let consensus = components.consensus().clone();
113
114        match &self.command {
115            Stages::Execution(cmd) => {
116                handle_stage!(
117                    dump_execution_stage,
118                    &tool,
119                    cmd,
120                    evm_config,
121                    consensus,
122                    runtime.clone()
123                )
124            }
125            Stages::StorageHashing(cmd) => {
126                handle_stage!(dump_hashing_storage_stage, &tool, cmd, runtime.clone())
127            }
128            Stages::AccountHashing(cmd) => {
129                handle_stage!(dump_hashing_account_stage, &tool, cmd, runtime.clone())
130            }
131            Stages::Merkle(cmd) => {
132                handle_stage!(dump_merkle_stage, &tool, cmd, evm_config, consensus, runtime.clone())
133            }
134        }
135
136        Ok(())
137    }
138}
139
140impl<C: ChainSpecParser> Command<C> {
141    /// Returns the underlying chain being used to run this command
142    pub fn chain_spec(&self) -> Option<&Arc<C::ChainSpec>> {
143        Some(&self.env.chain)
144    }
145}
146
147/// Sets up the database and initial state on [`tables::BlockBodyIndices`]. Also returns the tip
148/// block number.
149pub(crate) fn setup<N: NodeTypesWithDB>(
150    from: u64,
151    to: u64,
152    output_db: &PathBuf,
153    db_tool: &DbTool<N>,
154) -> eyre::Result<(DatabaseEnv, u64)> {
155    assert!(from < to, "FROM block should be lower than TO block.");
156
157    info!(target: "reth::cli", ?output_db, "Creating separate db");
158
159    let output_datadir = init_db(output_db, DatabaseArguments::new(ClientVersion::default()))?;
160
161    output_datadir.update(|tx| {
162        tx.import_table_with_range::<tables::BlockBodyIndices, _>(
163            &db_tool.provider_factory.db_ref().tx()?,
164            Some(from - 1),
165            to + 1,
166        )
167    })??;
168
169    let (tip_block_number, _) = db_tool
170        .provider_factory
171        .db_ref()
172        .view(|tx| tx.cursor_read::<tables::BlockBodyIndices>()?.last())??
173        .expect("some");
174
175    Ok((output_datadir, tip_block_number))
176}