Skip to content

Commit

Permalink
feat(cli): override static files datadir (#7212)
Browse files Browse the repository at this point in the history
Co-authored-by: Oliver Nordbjerg <[email protected]>
Co-authored-by: Alexey Shekhirin <[email protected]>
Co-authored-by: Oliver Nordbjerg <[email protected]>
  • Loading branch information
4 people authored Jun 3, 2024
1 parent 994f98f commit 4522fd8
Show file tree
Hide file tree
Showing 50 changed files with 460 additions and 646 deletions.
21 changes: 7 additions & 14 deletions bin/reth/src/commands/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ use crate::{
utils::{chain_help, genesis_value_parser, SUPPORTED_CHAINS},
DatabaseArgs,
},
dirs::{DataDirPath, MaybePlatformPath},
utils::DbTool,
};
use clap::{Parser, Subcommand};
use reth_db::{
open_db, open_db_read_only,
version::{get_db_version, DatabaseVersionError, DB_VERSION},
};
use reth_node_core::args::DatadirArgs;
use reth_primitives::ChainSpec;
use reth_provider::{providers::StaticFileProvider, ProviderFactory};
use std::{
Expand All @@ -32,16 +32,6 @@ mod tui;
/// `reth db` command
#[derive(Debug, Parser)]
pub struct Command {
/// The path to the data dir for all reth files and subdirectories.
///
/// Defaults to the OS-specific data directory:
///
/// - Linux: `$XDG_DATA_HOME/reth/` or `$HOME/.local/share/reth/`
/// - Windows: `{FOLDERID_RoamingAppData}/reth/`
/// - macOS: `$HOME/Library/Application Support/reth/`
#[arg(long, value_name = "DATA_DIR", verbatim_doc_comment, default_value_t, global = true)]
datadir: MaybePlatformPath<DataDirPath>,

/// The chain this node is running.
///
/// Possible values are either a built-in chain or the path to a chain specification file.
Expand All @@ -55,6 +45,9 @@ pub struct Command {
)]
chain: Arc<ChainSpec>,

#[command(flatten)]
datadir: DatadirArgs,

#[command(flatten)]
db: DatabaseArgs,

Expand Down Expand Up @@ -105,7 +98,7 @@ impl Command {
/// Execute `db` command
pub async fn execute(self) -> eyre::Result<()> {
// add network name to data dir
let data_dir = self.datadir.unwrap_or_chain_default(self.chain.chain);
let data_dir = self.datadir.resolve_datadir(self.chain.chain);
let db_path = data_dir.db();
let db_args = self.db.database_args();
let static_files_path = data_dir.static_files();
Expand Down Expand Up @@ -205,7 +198,7 @@ mod tests {
#[test]
fn parse_stats_globals() {
let path = format!("../{}", SUPPORTED_CHAINS[0]);
let cmd = Command::try_parse_from(["reth", "stats", "--datadir", &path]).unwrap();
assert_eq!(cmd.datadir.as_ref(), Some(Path::new(&path)));
let cmd = Command::try_parse_from(["reth", "--datadir", &path, "stats"]).unwrap();
assert_eq!(cmd.datadir.resolve_datadir(cmd.chain.chain).as_ref(), Path::new(&path));
}
}
18 changes: 5 additions & 13 deletions bin/reth/src/commands/debug_cmd/build_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
use crate::{
args::{
utils::{chain_help, genesis_value_parser, SUPPORTED_CHAINS},
DatabaseArgs,
DatabaseArgs, DatadirArgs,
},
dirs::{DataDirPath, MaybePlatformPath},
macros::block_executor,
};
use alloy_rlp::Decodable;
Expand Down Expand Up @@ -53,16 +52,6 @@ use tracing::*;
/// The script will then parse the block and attempt to build a similar one.
#[derive(Debug, Parser)]
pub struct Command {
/// The path to the data dir for all reth files and subdirectories.
///
/// Defaults to the OS-specific data directory:
///
/// - Linux: `$XDG_DATA_HOME/reth/` or `$HOME/.local/share/reth/`
/// - Windows: `{FOLDERID_RoamingAppData}/reth/`
/// - macOS: `$HOME/Library/Application Support/reth/`
#[arg(long, value_name = "DATA_DIR", verbatim_doc_comment, default_value_t)]
datadir: MaybePlatformPath<DataDirPath>,

/// The chain this node is running.
///
/// Possible values are either a built-in chain or the path to a chain specification file.
Expand All @@ -75,6 +64,9 @@ pub struct Command {
)]
chain: Arc<ChainSpec>,

#[command(flatten)]
datadir: DatadirArgs,

/// Database arguments.
#[command(flatten)]
db: DatabaseArgs,
Expand Down Expand Up @@ -145,7 +137,7 @@ impl Command {
/// Execute `debug in-memory-merkle` command
pub async fn execute(self, ctx: CliContext) -> eyre::Result<()> {
// add network name to data dir
let data_dir = self.datadir.unwrap_or_chain_default(self.chain.chain);
let data_dir = self.datadir.clone().resolve_datadir(self.chain.chain);
let db_path = data_dir.db();
fs::create_dir_all(&db_path)?;

Expand Down
20 changes: 6 additions & 14 deletions bin/reth/src/commands/debug_cmd/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ use crate::{
args::{
get_secret_key,
utils::{chain_help, genesis_value_parser, SUPPORTED_CHAINS},
DatabaseArgs, NetworkArgs,
DatabaseArgs, DatadirArgs, NetworkArgs,
},
dirs::{DataDirPath, MaybePlatformPath},
macros::block_executor,
utils::get_single_header,
};
Expand Down Expand Up @@ -48,16 +47,6 @@ use tracing::*;
/// `reth debug execution` command
#[derive(Debug, Parser)]
pub struct Command {
/// The path to the data dir for all reth files and subdirectories.
///
/// Defaults to the OS-specific data directory:
///
/// - Linux: `$XDG_DATA_HOME/reth/` or `$HOME/.local/share/reth/`
/// - Windows: `{FOLDERID_RoamingAppData}/reth/`
/// - macOS: `$HOME/Library/Application Support/reth/`
#[arg(long, value_name = "DATA_DIR", verbatim_doc_comment, default_value_t)]
datadir: MaybePlatformPath<DataDirPath>,

/// The chain this node is running.
///
/// Possible values are either a built-in chain or the path to a chain specification file.
Expand All @@ -70,6 +59,9 @@ pub struct Command {
)]
chain: Arc<ChainSpec>,

#[command(flatten)]
datadir: DatadirArgs,

#[command(flatten)]
network: NetworkArgs,

Expand Down Expand Up @@ -165,7 +157,7 @@ impl Command {
self.network.discovery.addr,
self.network.discovery.port,
))
.build(provider_factory.clone())
.build(provider_factory)
.start_network()
.await?;
info!(target: "reth::cli", peer_id = %network.peer_id(), local_addr = %network.local_addr(), "Connected to P2P network");
Expand Down Expand Up @@ -196,7 +188,7 @@ impl Command {
pub async fn execute(self, ctx: CliContext) -> eyre::Result<()> {
let mut config = Config::default();

let data_dir = self.datadir.unwrap_or_chain_default(self.chain.chain);
let data_dir = self.datadir.clone().resolve_datadir(self.chain.chain);
let db_path = data_dir.db();

// Make sure ETL doesn't default to /tmp/, but to whatever datadir is set to
Expand Down
18 changes: 5 additions & 13 deletions bin/reth/src/commands/debug_cmd/in_memory_merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ use crate::{
args::{
get_secret_key,
utils::{chain_help, genesis_value_parser, SUPPORTED_CHAINS},
DatabaseArgs, NetworkArgs,
DatabaseArgs, DatadirArgs, NetworkArgs,
},
dirs::{DataDirPath, MaybePlatformPath},
macros::block_executor,
utils::{get_single_body, get_single_header},
};
Expand Down Expand Up @@ -38,16 +37,6 @@ use tracing::*;
/// merkle root for it.
#[derive(Debug, Parser)]
pub struct Command {
/// The path to the data dir for all reth files and subdirectories.
///
/// Defaults to the OS-specific data directory:
///
/// - Linux: `$XDG_DATA_HOME/reth/` or `$HOME/.local/share/reth/`
/// - Windows: `{FOLDERID_RoamingAppData}/reth/`
/// - macOS: `$HOME/Library/Application Support/reth/`
#[arg(long, value_name = "DATA_DIR", verbatim_doc_comment, default_value_t)]
datadir: MaybePlatformPath<DataDirPath>,

/// The chain this node is running.
///
/// Possible values are either a built-in chain or the path to a chain specification file.
Expand All @@ -60,6 +49,9 @@ pub struct Command {
)]
chain: Arc<ChainSpec>,

#[command(flatten)]
datadir: DatadirArgs,

#[command(flatten)]
db: DatabaseArgs,

Expand Down Expand Up @@ -107,7 +99,7 @@ impl Command {
let config = Config::default();

// add network name to data dir
let data_dir = self.datadir.unwrap_or_chain_default(self.chain.chain);
let data_dir = self.datadir.clone().resolve_datadir(self.chain.chain);
let db_path = data_dir.db();
fs::create_dir_all(&db_path)?;

Expand Down
20 changes: 6 additions & 14 deletions bin/reth/src/commands/debug_cmd/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ use crate::{
args::{
get_secret_key,
utils::{chain_help, genesis_value_parser, SUPPORTED_CHAINS},
DatabaseArgs, NetworkArgs,
DatabaseArgs, DatadirArgs, NetworkArgs,
},
dirs::{DataDirPath, MaybePlatformPath},
macros::block_executor,
utils::get_single_header,
};
Expand Down Expand Up @@ -40,16 +39,6 @@ use tracing::*;
/// `reth debug merkle` command
#[derive(Debug, Parser)]
pub struct Command {
/// The path to the data dir for all reth files and subdirectories.
///
/// Defaults to the OS-specific data directory:
///
/// - Linux: `$XDG_DATA_HOME/reth/` or `$HOME/.local/share/reth/`
/// - Windows: `{FOLDERID_RoamingAppData}/reth/`
/// - macOS: `$HOME/Library/Application Support/reth/`
#[arg(long, value_name = "DATA_DIR", verbatim_doc_comment, default_value_t)]
datadir: MaybePlatformPath<DataDirPath>,

/// The chain this node is running.
///
/// Possible values are either a built-in chain or the path to a chain specification file.
Expand All @@ -62,6 +51,9 @@ pub struct Command {
)]
chain: Arc<ChainSpec>,

#[command(flatten)]
datadir: DatadirArgs,

#[command(flatten)]
db: DatabaseArgs,

Expand Down Expand Up @@ -100,7 +92,7 @@ impl Command {
self.network.discovery.addr,
self.network.discovery.port,
))
.build(provider_factory.clone())
.build(provider_factory)
.start_network()
.await?;
info!(target: "reth::cli", peer_id = %network.peer_id(), local_addr = %network.local_addr(), "Connected to P2P network");
Expand All @@ -113,7 +105,7 @@ impl Command {
let config = Config::default();

// add network name to data dir
let data_dir = self.datadir.unwrap_or_chain_default(self.chain.chain);
let data_dir = self.datadir.clone().resolve_datadir(self.chain.chain);
let db_path = data_dir.db();
fs::create_dir_all(&db_path)?;

Expand Down
20 changes: 6 additions & 14 deletions bin/reth/src/commands/debug_cmd/replay_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ use crate::{
args::{
get_secret_key,
utils::{chain_help, genesis_value_parser, SUPPORTED_CHAINS},
DatabaseArgs, NetworkArgs,
DatabaseArgs, DatadirArgs, NetworkArgs,
},
dirs::{DataDirPath, MaybePlatformPath},
macros::block_executor,
};
use clap::Parser;
Expand Down Expand Up @@ -41,16 +40,6 @@ use tracing::*;
/// It does not require
#[derive(Debug, Parser)]
pub struct Command {
/// The path to the data dir for all reth files and subdirectories.
///
/// Defaults to the OS-specific data directory:
///
/// - Linux: `$XDG_DATA_HOME/reth/` or `$HOME/.local/share/reth/`
/// - Windows: `{FOLDERID_RoamingAppData}/reth/`
/// - macOS: `$HOME/Library/Application Support/reth/`
#[arg(long, value_name = "DATA_DIR", verbatim_doc_comment, default_value_t)]
datadir: MaybePlatformPath<DataDirPath>,

/// The chain this node is running.
///
/// Possible values are either a built-in chain or the path to a chain specification file.
Expand All @@ -63,6 +52,9 @@ pub struct Command {
)]
chain: Arc<ChainSpec>,

#[command(flatten)]
datadir: DatadirArgs,

#[command(flatten)]
db: DatabaseArgs,

Expand Down Expand Up @@ -97,7 +89,7 @@ impl Command {
self.network.discovery.addr,
self.network.discovery.port,
))
.build(provider_factory.clone())
.build(provider_factory)
.start_network()
.await?;
info!(target: "reth::cli", peer_id = %network.peer_id(), local_addr = %network.local_addr(), "Connected to P2P network");
Expand All @@ -110,7 +102,7 @@ impl Command {
let config = Config::default();

// Add network name to data dir
let data_dir = self.datadir.unwrap_or_chain_default(self.chain.chain);
let data_dir = self.datadir.clone().resolve_datadir(self.chain.chain);
let db_path = data_dir.db();
fs::create_dir_all(&db_path)?;

Expand Down
18 changes: 5 additions & 13 deletions bin/reth/src/commands/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
use crate::{
args::{
utils::{chain_help, genesis_value_parser, SUPPORTED_CHAINS},
DatabaseArgs,
DatabaseArgs, DatadirArgs,
},
dirs::{DataDirPath, MaybePlatformPath},
macros::block_executor,
version::SHORT_VERSION,
};
Expand Down Expand Up @@ -46,16 +45,6 @@ pub struct ImportCommand {
#[arg(long, value_name = "FILE", verbatim_doc_comment)]
config: Option<PathBuf>,

/// The path to the data dir for all reth files and subdirectories.
///
/// Defaults to the OS-specific data directory:
///
/// - Linux: `$XDG_DATA_HOME/reth/` or `$HOME/.local/share/reth/`
/// - Windows: `{FOLDERID_RoamingAppData}/reth/`
/// - macOS: `$HOME/Library/Application Support/reth/`
#[arg(long, value_name = "DATA_DIR", verbatim_doc_comment, default_value_t)]
datadir: MaybePlatformPath<DataDirPath>,

/// The chain this node is running.
///
/// Possible values are either a built-in chain or the path to a chain specification file.
Expand All @@ -76,6 +65,9 @@ pub struct ImportCommand {
#[arg(long, value_name = "CHUNK_LEN", verbatim_doc_comment)]
chunk_len: Option<u64>,

#[command(flatten)]
datadir: DatadirArgs,

#[command(flatten)]
db: DatabaseArgs,

Expand All @@ -102,7 +94,7 @@ impl ImportCommand {
);

// add network name to data dir
let data_dir = self.datadir.unwrap_or_chain_default(self.chain.chain);
let data_dir = self.datadir.resolve_datadir(self.chain.chain);
let config_path = self.config.clone().unwrap_or_else(|| data_dir.config());

let mut config: Config = load_config(config_path.clone())?;
Expand Down
Loading

0 comments on commit 4522fd8

Please sign in to comment.