reth_cli_commands/stage/dump/
execution.rs

1use super::setup;
2use reth_consensus::{noop::NoopConsensus, ConsensusError, FullConsensus};
3use reth_db::DatabaseEnv;
4use reth_db_api::{
5    cursor::DbCursorRO, database::Database, table::TableImporter, tables, transaction::DbTx,
6};
7use reth_db_common::DbTool;
8use reth_evm::ConfigureEvm;
9use reth_node_builder::NodeTypesWithDB;
10use reth_node_core::dirs::{ChainPath, DataDirPath};
11use reth_provider::{
12    providers::{ProviderNodeTypes, StaticFileProvider},
13    DatabaseProviderFactory, ProviderFactory,
14};
15use reth_stages::{stages::ExecutionStage, Stage, StageCheckpoint, UnwindInput};
16use std::sync::Arc;
17use tracing::info;
18
19pub(crate) async fn dump_execution_stage<N, E, C>(
20    db_tool: &DbTool<N>,
21    from: u64,
22    to: u64,
23    output_datadir: ChainPath<DataDirPath>,
24    should_run: bool,
25    evm_config: E,
26    consensus: C,
27) -> eyre::Result<()>
28where
29    N: ProviderNodeTypes<DB = Arc<DatabaseEnv>>,
30    E: ConfigureEvm<Primitives = N::Primitives> + 'static,
31    C: FullConsensus<E::Primitives, Error = ConsensusError> + 'static,
32{
33    let (output_db, tip_block_number) = setup(from, to, &output_datadir.db(), db_tool)?;
34
35    import_tables_with_range(&output_db, db_tool, from, to)?;
36
37    unwind_and_copy(db_tool, from, tip_block_number, &output_db, evm_config.clone())?;
38
39    if should_run {
40        dry_run(
41            ProviderFactory::<N>::new(
42                Arc::new(output_db),
43                db_tool.chain(),
44                StaticFileProvider::read_write(output_datadir.static_files())?,
45            ),
46            to,
47            from,
48            evm_config,
49            consensus,
50        )?;
51    }
52
53    Ok(())
54}
55
56/// Imports all the tables that can be copied over a range.
57fn import_tables_with_range<N: NodeTypesWithDB>(
58    output_db: &DatabaseEnv,
59    db_tool: &DbTool<N>,
60    from: u64,
61    to: u64,
62) -> eyre::Result<()> {
63    //  We're not sharing the transaction in case the memory grows too much.
64
65    output_db.update(|tx| {
66        tx.import_table_with_range::<tables::CanonicalHeaders, _>(
67            &db_tool.provider_factory.db_ref().tx()?,
68            Some(from),
69            to,
70        )
71    })??;
72    output_db.update(|tx| {
73        tx.import_table_with_range::<tables::Headers, _>(
74            &db_tool.provider_factory.db_ref().tx()?,
75            Some(from),
76            to,
77        )
78    })??;
79    output_db.update(|tx| {
80        tx.import_table_with_range::<tables::BlockBodyIndices, _>(
81            &db_tool.provider_factory.db_ref().tx()?,
82            Some(from),
83            to,
84        )
85    })??;
86    output_db.update(|tx| {
87        tx.import_table_with_range::<tables::BlockOmmers, _>(
88            &db_tool.provider_factory.db_ref().tx()?,
89            Some(from),
90            to,
91        )
92    })??;
93
94    // Find range of transactions that need to be copied over
95    let (from_tx, to_tx) = db_tool.provider_factory.db_ref().view(|read_tx| {
96        let mut read_cursor = read_tx.cursor_read::<tables::BlockBodyIndices>()?;
97        let (_, from_block) =
98            read_cursor.seek(from)?.ok_or(eyre::eyre!("BlockBody {from} does not exist."))?;
99        let (_, to_block) =
100            read_cursor.seek(to)?.ok_or(eyre::eyre!("BlockBody {to} does not exist."))?;
101
102        Ok::<(u64, u64), eyre::ErrReport>((
103            from_block.first_tx_num,
104            to_block.first_tx_num + to_block.tx_count,
105        ))
106    })??;
107
108    output_db.update(|tx| {
109        tx.import_table_with_range::<tables::Transactions, _>(
110            &db_tool.provider_factory.db_ref().tx()?,
111            Some(from_tx),
112            to_tx,
113        )
114    })??;
115
116    output_db.update(|tx| {
117        tx.import_table_with_range::<tables::TransactionSenders, _>(
118            &db_tool.provider_factory.db_ref().tx()?,
119            Some(from_tx),
120            to_tx,
121        )
122    })??;
123
124    Ok(())
125}
126
127/// Dry-run an unwind to FROM block, so we can get the `PlainStorageState` and
128/// `PlainAccountState` safely. There might be some state dependency from an address
129/// which hasn't been changed in the given range.
130fn unwind_and_copy<N: ProviderNodeTypes>(
131    db_tool: &DbTool<N>,
132    from: u64,
133    tip_block_number: u64,
134    output_db: &DatabaseEnv,
135    evm_config: impl ConfigureEvm<Primitives = N::Primitives>,
136) -> eyre::Result<()> {
137    let provider = db_tool.provider_factory.database_provider_rw()?;
138
139    let mut exec_stage = ExecutionStage::new_with_executor(evm_config, NoopConsensus::arc());
140
141    exec_stage.unwind(
142        &provider,
143        UnwindInput {
144            unwind_to: from,
145            checkpoint: StageCheckpoint::new(tip_block_number),
146            bad_block: None,
147        },
148    )?;
149
150    let unwind_inner_tx = provider.into_tx();
151
152    output_db
153        .update(|tx| tx.import_dupsort::<tables::PlainStorageState, _>(&unwind_inner_tx))??;
154    output_db.update(|tx| tx.import_table::<tables::PlainAccountState, _>(&unwind_inner_tx))??;
155    output_db.update(|tx| tx.import_table::<tables::Bytecodes, _>(&unwind_inner_tx))??;
156
157    Ok(())
158}
159
160/// Try to re-execute the stage without committing
161fn dry_run<N, E, C>(
162    output_provider_factory: ProviderFactory<N>,
163    to: u64,
164    from: u64,
165    evm_config: E,
166    consensus: C,
167) -> eyre::Result<()>
168where
169    N: ProviderNodeTypes,
170    E: ConfigureEvm<Primitives = N::Primitives> + 'static,
171    C: FullConsensus<E::Primitives, Error = ConsensusError> + 'static,
172{
173    info!(target: "reth::cli", "Executing stage. [dry-run]");
174
175    let mut exec_stage = ExecutionStage::new_with_executor(evm_config, Arc::new(consensus));
176
177    let input =
178        reth_stages::ExecInput { target: Some(to), checkpoint: Some(StageCheckpoint::new(from)) };
179    exec_stage.execute(&output_provider_factory.database_provider_rw()?, input)?;
180
181    info!(target: "reth::cli", "Success");
182
183    Ok(())
184}