Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: move stage command to reth-cli-commands #9384

Merged
merged 3 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions bin/reth/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ use crate::{
commands::{
debug_cmd, import,
node::{self, NoArgs},
stage,
},
version::{LONG_VERSION, SHORT_VERSION},
};
use clap::{value_parser, Parser, Subcommand};
use reth_chainspec::ChainSpec;
use reth_cli_commands::{config_cmd, db, dump_genesis, init_cmd, init_state, p2p, prune, recover};
use reth_cli_commands::{
config_cmd, db, dump_genesis, init_cmd, init_state, p2p, prune, recover, stage,
};
use reth_cli_runner::CliRunner;
use reth_db::DatabaseEnv;
use reth_node_builder::{NodeBuilder, WithLaunchContext};
Expand Down Expand Up @@ -159,7 +160,14 @@ impl<Ext: clap::Args + fmt::Debug> Cli<Ext> {
}
Commands::DumpGenesis(command) => runner.run_blocking_until_ctrl_c(command.execute()),
Commands::Db(command) => runner.run_blocking_until_ctrl_c(command.execute()),
Commands::Stage(command) => runner.run_command_until_exit(|ctx| command.execute(ctx)),
Commands::Stage(command) => runner.run_command_until_exit(|ctx| {
command.execute(ctx, |chain_spec| {
#[cfg(feature = "optimism")]
return reth_node_optimism::OpExecutorProvider::optimism(chain_spec);
#[cfg(not(feature = "optimism"))]
reth_node_ethereum::EthExecutorProvider::ethereum(chain_spec)
})
joshieDo marked this conversation as resolved.
Show resolved Hide resolved
}),
Commands::P2P(command) => runner.run_until_ctrl_c(command.execute()),
#[cfg(feature = "dev")]
Commands::TestVectors(command) => runner.run_until_ctrl_c(command.execute()),
Expand Down
1 change: 0 additions & 1 deletion bin/reth/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
pub mod debug_cmd;
pub mod import;
pub mod node;
pub mod stage;
8 changes: 7 additions & 1 deletion crates/cli/commands/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ reth-chainspec.workspace = true
reth-cli-runner.workspace = true
reth-cli-util.workspace = true
reth-config.workspace = true
reth-consensus.workspace = true
reth-db = { workspace = true, features = ["mdbx"] }
reth-db-api.workspace = true
reth-db-common.workspace = true
reth-downloaders.workspace = true
reth-evm.workspace = true
reth-exex.workspace = true
reth-fs-util.workspace = true
reth-network = { workspace = true, features = ["serde"] }
reth-network-p2p.workspace = true
Expand All @@ -46,6 +48,7 @@ tracing.workspace = true
backon.workspace = true

# io
fdlimit.workspace = true
confy.workspace = true
toml = { workspace = true, features = ["display"] }

Expand All @@ -56,6 +59,9 @@ ratatui = { version = "0.27", default-features = false, features = [
"crossterm",
] }

# metrics
metrics-process.workspace = true

# reth test-vectors
proptest = { workspace = true, optional = true }
arbitrary = { workspace = true, optional = true }
Expand All @@ -69,4 +75,4 @@ dev = [
"dep:proptest-arbitrary-interop",
"reth-primitives/arbitrary",
"reth-db-api/arbitrary"
]
]
1 change: 1 addition & 0 deletions crates/cli/commands/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ pub mod init_state;
pub mod p2p;
pub mod prune;
pub mod recover;
pub mod stage;
#[cfg(feature = "dev")]
pub mod test_vectors;
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//! Database debugging tool
use crate::args::StageEnum;
use crate::common::{AccessRights, Environment, EnvironmentArgs};
use clap::Parser;
use itertools::Itertools;
use reth_cli_commands::common::{AccessRights, Environment, EnvironmentArgs};
use reth_db::{static_file::iter_static_files, tables, DatabaseEnv};
use reth_db_api::transaction::DbTxMut;
use reth_db_common::{
init::{insert_genesis_header, insert_genesis_history, insert_genesis_state},
DbTool,
};
use reth_node_core::args::StageEnum;
use reth_provider::{providers::StaticFileWriter, StaticFileProviderFactory};
use reth_stages::StageId;
use reth_static_file_types::{find_fixed_range, StaticFileSegment};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
use super::setup;
use crate::macros::block_executor;
use reth_db::{tables, DatabaseEnv};
use reth_db_api::{
cursor::DbCursorRO, database::Database, table::TableImporter, transaction::DbTx,
};
use reth_db_common::DbTool;
use reth_evm::{execute::BlockExecutorProvider, noop::NoopBlockExecutorProvider};
use reth_node_core::dirs::{ChainPath, DataDirPath};
use reth_provider::{providers::StaticFileProvider, ChainSpecProvider, ProviderFactory};
use reth_provider::{providers::StaticFileProvider, ProviderFactory};
use reth_stages::{stages::ExecutionStage, Stage, StageCheckpoint, UnwindInput};
use tracing::info;

pub(crate) async fn dump_execution_stage<DB: Database>(
pub(crate) async fn dump_execution_stage<DB, E>(
db_tool: &DbTool<DB>,
from: u64,
to: u64,
output_datadir: ChainPath<DataDirPath>,
should_run: bool,
) -> eyre::Result<()> {
executor: E,
) -> eyre::Result<()>
where
DB: Database,
E: BlockExecutorProvider,
{
let (output_db, tip_block_number) = setup(from, to, &output_datadir.db(), db_tool)?;

import_tables_with_range(&output_db, db_tool, from, to)?;
Expand All @@ -32,6 +37,7 @@ pub(crate) async fn dump_execution_stage<DB: Database>(
),
to,
from,
executor,
)?;
}

Expand Down Expand Up @@ -127,8 +133,7 @@ fn unwind_and_copy<DB: Database>(
) -> eyre::Result<()> {
let provider = db_tool.provider_factory.provider_rw()?;

let executor = block_executor!(db_tool.chain());
let mut exec_stage = ExecutionStage::new_with_executor(executor);
let mut exec_stage = ExecutionStage::new_with_executor(NoopBlockExecutorProvider::default());

exec_stage.unwind(
&provider,
Expand All @@ -150,14 +155,18 @@ fn unwind_and_copy<DB: Database>(
}

/// Try to re-execute the stage without committing
fn dry_run<DB: Database>(
fn dry_run<DB, E>(
output_provider_factory: ProviderFactory<DB>,
to: u64,
from: u64,
) -> eyre::Result<()> {
executor: E,
) -> eyre::Result<()>
where
DB: Database,
E: BlockExecutorProvider,
{
info!(target: "reth::cli", "Executing stage. [dry-run]");

let executor = block_executor!(output_provider_factory.chain_spec());
let mut exec_stage = ExecutionStage::new_with_executor(executor);

let input =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use super::setup;
use crate::macros::block_executor;
use eyre::Result;
use reth_config::config::EtlConfig;
use reth_db::{tables, DatabaseEnv};
use reth_db_api::{database::Database, table::TableImporter};
use reth_db_common::DbTool;
use reth_evm::noop::NoopBlockExecutorProvider;
use reth_exex::ExExManagerHandle;
use reth_node_core::dirs::{ChainPath, DataDirPath};
use reth_primitives::BlockNumber;
Expand Down Expand Up @@ -86,11 +86,9 @@ fn unwind_and_copy<DB: Database>(

MerkleStage::default_unwind().unwind(&provider, unwind)?;

let executor = block_executor!(db_tool.chain());

// Bring Plainstate to TO (hashing stage execution requires it)
let mut exec_stage = ExecutionStage::new(
executor,
NoopBlockExecutorProvider::default(), // Not necessary for unwinding.
ExecutionStageThresholds {
max_blocks: Some(u64::MAX),
max_changes: None,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
//! Database debugging tool
use crate::{args::DatadirArgs, dirs::DataDirPath};
use crate::common::{AccessRights, Environment, EnvironmentArgs};
use clap::Parser;
use reth_cli_commands::common::{AccessRights, Environment, EnvironmentArgs};
use reth_chainspec::ChainSpec;
use reth_db::{init_db, mdbx::DatabaseArguments, tables, DatabaseEnv};
use reth_db_api::{
cursor::DbCursorRO, database::Database, models::ClientVersion, table::TableImporter,
transaction::DbTx,
};
use reth_db_common::DbTool;
use reth_node_core::dirs::PlatformPath;
use std::path::PathBuf;
use reth_evm::execute::BlockExecutorProvider;
use reth_node_core::{
args::DatadirArgs,
dirs::{DataDirPath, PlatformPath},
};
use std::{path::PathBuf, sync::Arc};
use tracing::info;

mod hashing_storage;
Expand Down Expand Up @@ -72,16 +76,29 @@ macro_rules! handle_stage {
let output_datadir = output_datadir.with_chain($tool.chain().chain, DatadirArgs::default());
$stage_fn($tool, *from, *to, output_datadir, *dry_run).await?
}};

($stage_fn:ident, $tool:expr, $command:expr, $executor:expr) => {{
let StageCommand { output_datadir, from, to, dry_run, .. } = $command;
let output_datadir = output_datadir.with_chain($tool.chain().chain, DatadirArgs::default());
$stage_fn($tool, *from, *to, output_datadir, *dry_run, $executor).await?
}};
}

impl Command {
/// Execute `dump-stage` command
pub async fn execute(self) -> eyre::Result<()> {
pub async fn execute<E, F>(self, executor: F) -> eyre::Result<()>
where
E: BlockExecutorProvider,
F: FnOnce(Arc<ChainSpec>) -> E,
{
let Environment { provider_factory, .. } = self.env.init(AccessRights::RO)?;
let tool = DbTool::new(provider_factory)?;

match &self.command {
Stages::Execution(cmd) => handle_stage!(dump_execution_stage, &tool, cmd),
Stages::Execution(cmd) => {
let executor = executor(tool.chain());
handle_stage!(dump_execution_stage, &tool, cmd, executor)
}
Stages::StorageHashing(cmd) => handle_stage!(dump_hashing_storage_stage, &tool, cmd),
Stages::AccountHashing(cmd) => handle_stage!(dump_hashing_account_stage, &tool, cmd),
Stages::Merkle(cmd) => handle_stage!(dump_merkle_stage, &tool, cmd),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
//! `reth stage` command

use std::sync::Arc;

use clap::{Parser, Subcommand};
use reth_chainspec::ChainSpec;
use reth_cli_runner::CliContext;
use reth_evm::execute::BlockExecutorProvider;

pub mod drop;
pub mod dump;
Expand Down Expand Up @@ -35,11 +39,15 @@ pub enum Subcommands {

impl Command {
/// Execute `stage` command
pub async fn execute(self, ctx: CliContext) -> eyre::Result<()> {
pub async fn execute<E, F>(self, ctx: CliContext, executor: F) -> eyre::Result<()>
where
E: BlockExecutorProvider,
F: FnOnce(Arc<ChainSpec>) -> E,
{
match self.command {
Subcommands::Run(command) => command.execute(ctx).await,
Subcommands::Run(command) => command.execute(ctx, executor).await,
Subcommands::Drop(command) => command.execute().await,
Subcommands::Dump(command) => command.execute().await,
Subcommands::Dump(command) => command.execute(executor).await,
Subcommands::Unwind(command) => command.execute().await,
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
//! Main `stage` command
//!
//! Stage debugging tool
use crate::{
args::{NetworkArgs, StageEnum},
macros::block_executor,
prometheus_exporter,
};
use crate::common::{AccessRights, Environment, EnvironmentArgs};
use clap::Parser;
use reth_beacon_consensus::EthBeaconConsensus;
use reth_cli_commands::common::{AccessRights, Environment, EnvironmentArgs};
use reth_chainspec::ChainSpec;
use reth_cli_runner::CliContext;
use reth_cli_util::get_secret_key;
use reth_config::config::{HashingConfig, SenderRecoveryConfig, TransactionLookupConfig};
use reth_downloaders::bodies::bodies::BodiesDownloaderBuilder;
use reth_evm::execute::BlockExecutorProvider;
use reth_exex::ExExManagerHandle;
use reth_node_core::{
args::{NetworkArgs, StageEnum},
prometheus_exporter,
};
use reth_provider::{
ChainSpecProvider, StageCheckpointReader, StageCheckpointWriter, StaticFileProviderFactory,
StaticFileWriter,
Expand Down Expand Up @@ -83,7 +84,11 @@ pub struct Command {

impl Command {
/// Execute `stage` command
pub async fn execute(self, ctx: CliContext) -> eyre::Result<()> {
pub async fn execute<E, F>(self, ctx: CliContext, executor: F) -> eyre::Result<()>
where
E: BlockExecutorProvider,
F: FnOnce(Arc<ChainSpec>) -> E,
{
// Raise the fd limit of the process.
// Does not do anything on windows.
let _ = fdlimit::raise_fd_limit();
Expand Down Expand Up @@ -163,24 +168,21 @@ impl Command {
})),
None,
),
StageEnum::Execution => {
let executor = block_executor!(provider_factory.chain_spec());
(
Box::new(ExecutionStage::new(
executor,
ExecutionStageThresholds {
max_blocks: Some(batch_size),
max_changes: None,
max_cumulative_gas: None,
max_duration: None,
},
config.stages.merkle.clean_threshold,
prune_modes,
ExExManagerHandle::empty(),
)),
None,
)
}
StageEnum::Execution => (
Box::new(ExecutionStage::new(
executor(provider_factory.chain_spec()),
ExecutionStageThresholds {
max_blocks: Some(batch_size),
max_changes: None,
max_cumulative_gas: None,
max_duration: None,
},
config.stages.merkle.clean_threshold,
prune_modes,
ExExManagerHandle::empty(),
)),
None,
),
StageEnum::TxLookup => (
Box::new(TransactionLookupStage::new(
TransactionLookupConfig { chunk_size: batch_size },
Expand Down
Loading
Loading