reth_cli_commands/stage/dump/
execution.rs1use super::setup;
2use reth_consensus::{noop::NoopConsensus, 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_api::{HeaderTy, TxTy};
10use reth_node_core::dirs::{ChainPath, DataDirPath};
11use reth_provider::{
12 providers::{ProviderNodeTypes, RocksDBProvider, StaticFileProvider},
13 DatabaseProviderFactory, ProviderFactory,
14};
15use reth_stages::{stages::ExecutionStage, Stage, StageCheckpoint, UnwindInput};
16use reth_storage_overlay::OverlayManager;
17use std::sync::Arc;
18use tracing::info;
19
20#[expect(clippy::too_many_arguments)]
21pub(crate) async fn dump_execution_stage<N, E, C>(
22 db_tool: &DbTool<N>,
23 from: u64,
24 to: u64,
25 output_datadir: ChainPath<DataDirPath>,
26 should_run: bool,
27 evm_config: E,
28 consensus: C,
29 runtime: reth_tasks::Runtime,
30) -> eyre::Result<()>
31where
32 N: ProviderNodeTypes<DB = DatabaseEnv>,
33 E: ConfigureEvm<Primitives = N::Primitives> + 'static,
34 C: FullConsensus<E::Primitives> + 'static,
35{
36 let (output_db, tip_block_number) = setup(from, to, &output_datadir.db(), db_tool)?;
37
38 import_tables_with_range(&output_db, db_tool, from, to)?;
39
40 unwind_and_copy(db_tool, from, tip_block_number, &output_db, evm_config.clone())?;
41
42 if should_run {
43 dry_run(
44 ProviderFactory::<N>::new(
45 output_db,
46 db_tool.chain(),
47 StaticFileProvider::read_write(output_datadir.static_files())?,
48 RocksDBProvider::builder(output_datadir.rocksdb()).build()?,
49 OverlayManager::default(),
50 runtime,
51 )?,
52 to,
53 from,
54 evm_config,
55 consensus,
56 )?;
57 }
58
59 Ok(())
60}
61
62fn import_tables_with_range<N: ProviderNodeTypes>(
64 output_db: &DatabaseEnv,
65 db_tool: &DbTool<N>,
66 from: u64,
67 to: u64,
68) -> eyre::Result<()> {
69 output_db.update(|tx| {
72 tx.import_table_with_range::<tables::CanonicalHeaders, _>(
73 &db_tool.provider_factory.db_ref().tx()?,
74 Some(from),
75 to,
76 )
77 })??;
78 output_db.update(|tx| {
79 tx.import_table_with_range::<tables::Headers<HeaderTy<N>>, _>(
80 &db_tool.provider_factory.db_ref().tx()?,
81 Some(from),
82 to,
83 )
84 })??;
85 output_db.update(|tx| {
86 tx.import_table_with_range::<tables::BlockBodyIndices, _>(
87 &db_tool.provider_factory.db_ref().tx()?,
88 Some(from),
89 to,
90 )
91 })??;
92 output_db.update(|tx| {
93 tx.import_table_with_range::<tables::BlockOmmers<HeaderTy<N>>, _>(
94 &db_tool.provider_factory.db_ref().tx()?,
95 Some(from),
96 to,
97 )
98 })??;
99
100 let (from_tx, to_tx) = db_tool.provider_factory.db_ref().view(|read_tx| {
102 let mut read_cursor = read_tx.cursor_read::<tables::BlockBodyIndices>()?;
103 let (_, from_block) =
104 read_cursor.seek(from)?.ok_or(eyre::eyre!("BlockBody {from} does not exist."))?;
105 let (_, to_block) =
106 read_cursor.seek(to)?.ok_or(eyre::eyre!("BlockBody {to} does not exist."))?;
107
108 Ok::<(u64, u64), eyre::ErrReport>((
109 from_block.first_tx_num,
110 to_block.first_tx_num + to_block.tx_count,
111 ))
112 })??;
113
114 output_db.update(|tx| {
115 tx.import_table_with_range::<tables::Transactions<TxTy<N>>, _>(
116 &db_tool.provider_factory.db_ref().tx()?,
117 Some(from_tx),
118 to_tx,
119 )
120 })??;
121
122 output_db.update(|tx| {
123 tx.import_table_with_range::<tables::TransactionSenders, _>(
124 &db_tool.provider_factory.db_ref().tx()?,
125 Some(from_tx),
126 to_tx,
127 )
128 })??;
129
130 Ok(())
131}
132
133fn unwind_and_copy<N: ProviderNodeTypes>(
137 db_tool: &DbTool<N>,
138 from: u64,
139 tip_block_number: u64,
140 output_db: &DatabaseEnv,
141 evm_config: impl ConfigureEvm<Primitives = N::Primitives>,
142) -> eyre::Result<()> {
143 let provider = db_tool.provider_factory.database_provider_rw()?;
144
145 let mut exec_stage = ExecutionStage::new_with_executor(evm_config, NoopConsensus::arc());
146
147 exec_stage.unwind(
148 &provider,
149 UnwindInput {
150 unwind_to: from,
151 checkpoint: StageCheckpoint::new(tip_block_number),
152 bad_block: None,
153 },
154 )?;
155
156 let unwind_inner_tx = provider.into_tx();
157
158 output_db
159 .update(|tx| tx.import_dupsort::<tables::PlainStorageState, _>(&unwind_inner_tx))??;
160 output_db.update(|tx| tx.import_table::<tables::PlainAccountState, _>(&unwind_inner_tx))??;
161 output_db.update(|tx| tx.import_table::<tables::Bytecodes, _>(&unwind_inner_tx))??;
162
163 Ok(())
164}
165
166fn dry_run<N, E, C>(
168 output_provider_factory: ProviderFactory<N>,
169 to: u64,
170 from: u64,
171 evm_config: E,
172 consensus: C,
173) -> eyre::Result<()>
174where
175 N: ProviderNodeTypes,
176 E: ConfigureEvm<Primitives = N::Primitives> + 'static,
177 C: FullConsensus<E::Primitives> + 'static,
178{
179 info!(target: "reth::cli", "Executing stage. [dry-run]");
180
181 let mut exec_stage = ExecutionStage::new_with_executor(evm_config, Arc::new(consensus));
182
183 let input =
184 reth_stages::ExecInput { target: Some(to), checkpoint: Some(StageCheckpoint::new(from)) };
185 exec_stage.execute(&output_provider_factory.database_provider_rw()?, input)?;
186
187 info!(target: "reth::cli", "Success");
188
189 Ok(())
190}