Skip to content

refactor(l1, l2): use Path instead of String for path cli args #3770

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

Open
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

mpaulucci
Copy link
Collaborator

@mpaulucci mpaulucci commented Jul 22, 2025

Motivation
Lets use Paths that are a higher abstraction for dealing with paths.

Description

  • Removed String paths in favor of Path and PathBuf
  • Renamed set_datadir to init_datadir.
  • Added default_datadir add added it to the default cli option so that you can see it when running ethrex --help
  • If jwt_secret_path is relative path, set it as part of the default path.

Copy link

github-actions bot commented Jul 22, 2025

Lines of code report

Total lines added: 38
Total lines removed: 8
Total lines changed: 46

Detailed view
+----------------------------------------------------+-------+------+
| File                                               | Lines | Diff |
+----------------------------------------------------+-------+------+
| ethrex/cmd/ethrex/bench/import_blocks_benchmark.rs | 37    | -1   |
+----------------------------------------------------+-------+------+
| ethrex/cmd/ethrex/cli.rs                           | 451   | -4   |
+----------------------------------------------------+-------+------+
| ethrex/cmd/ethrex/ethrex.rs                        | 134   | +1   |
+----------------------------------------------------+-------+------+
| ethrex/cmd/ethrex/initializers.rs                  | 320   | +18  |
+----------------------------------------------------+-------+------+
| ethrex/cmd/ethrex/l2/command.rs                    | 474   | +3   |
+----------------------------------------------------+-------+------+
| ethrex/cmd/ethrex/lib.rs                           | 6     | -2   |
+----------------------------------------------------+-------+------+
| ethrex/cmd/ethrex/utils.rs                         | 162   | +16  |
+----------------------------------------------------+-------+------+
| ethrex/tooling/archive_sync/src/main.rs            | 287   | -1   |
+----------------------------------------------------+-------+------+

@mpaulucci mpaulucci changed the title refactor(l1, l2): use PathBuf instead of String for path cli args refactor(l1, l2): use Path instead of String for path cli args Jul 24, 2025
@mpaulucci mpaulucci marked this pull request as ready for review July 24, 2025 16:58
@Copilot Copilot AI review requested due to automatic review settings July 24, 2025 16:58
@mpaulucci mpaulucci requested a review from a team as a code owner July 24, 2025 16:58
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors CLI path arguments to use Rust's Path and PathBuf types instead of raw strings, improving type safety and path handling. It also improves the datadir initialization process and JWT secret path resolution.

  • Replaces String path arguments with PathBuf across CLI options and function signatures
  • Renames set_datadir to init_datadir and adds default_datadir function for better path management
  • Updates error handling to use proper error types instead of panics

Reviewed Changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 10 comments.

Show a summary per file
File Description
tooling/archive_sync/src/main.rs Updates datadir argument to use PathBuf and new path utility functions
docs/l1/dev-setup-l1.md Updates documentation to show expanded default datadir path
crates/storage/store.rs Changes panic to proper error handling for genesis mismatch
cmd/ethrex/utils.rs Adds path-based functions and improves JWT secret path handling
cmd/ethrex/lib.rs Removes constant definitions that are now handled by functions
cmd/ethrex/l2/command.rs Updates L2 commands to use PathBuf and new path utilities
cmd/ethrex/initializers.rs Updates function signatures to accept Path types and improves logging
cmd/ethrex/ethrex.rs Updates main ethrex binary to use new path handling
cmd/ethrex/cli.rs Updates CLI argument types and path handling throughout
cmd/ethrex/bench/import_blocks_benchmark.rs Updates benchmark to use new path utilities

let rollup_store = l2::initializers::init_rollup_store(&rollup_store_dir).await;
let store = init_store(&datadir, genesis).await;
let rollup_store =
l2::initializers::init_rollup_store(rollup_store_dir.to_str().unwrap()).await;
Copy link
Preview

Copilot AI Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using .to_str().unwrap() can panic if the path contains invalid UTF-8. Consider using a more robust approach or handling the error case explicitly.

Suggested change
l2::initializers::init_rollup_store(rollup_store_dir.to_str().unwrap()).await;
l2::initializers::init_rollup_store(
rollup_store_dir
.to_str()
.ok_or_else(|| eyre::eyre!("Invalid UTF-8 in rollup_store_dir path"))?
).await;

Copilot uses AI. Check for mistakes.

@@ -518,15 +520,16 @@ impl Command {
info!("Private key not given, not updating contract.");
}
info!("Updating store...");
let rollup_store = l2::initializers::init_rollup_store(&rollup_store_dir).await;
let rollup_store =
l2::initializers::init_rollup_store(rollup_store_dir.to_str().unwrap()).await;
Copy link
Preview

Copilot AI Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using .to_str().unwrap() can panic if the path contains invalid UTF-8. Consider using a more robust approach or handling the error case explicitly.

Copilot uses AI. Check for mistakes.

pub fn open_store(data_dir: &Path) -> Store {
if data_dir.ends_with("memory") {
Store::new(
data_dir.to_str().expect("Invalid data directory path"),
Copy link
Preview

Copilot AI Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using .to_str().expect() can panic if the path contains invalid UTF-8. Consider using a more robust approach or handling the error case explicitly.

Suggested change
data_dir.to_str().expect("Invalid data directory path"),
data_dir.to_str().ok_or_else(|| {
error!("Invalid data directory path: {:?}", data_dir);
"Invalid data directory path"
})?,

Copilot uses AI. Check for mistakes.

Store::new(data_dir, engine_type).expect("Failed to create Store")
info!("Opening store at path: {:?}", data_dir);
Store::new(
data_dir.to_str().expect("Invalid data directory path"),
Copy link
Preview

Copilot AI Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using .to_str().expect() can panic if the path contains invalid UTF-8. Consider using a more robust approach or handling the error case explicitly.

Suggested change
data_dir.to_str().expect("Invalid data directory path"),
match data_dir.to_str() {
Some(path) => path,
None => {
error!("Data directory path contains invalid UTF-8: {:?}", data_dir);
panic!("Invalid data directory path");
}
},

Copilot uses AI. Check for mistakes.

Comment on lines +123 to +131
pub fn init_datadir(datadir: PathBuf) -> PathBuf {
if datadir.exists() {
if !datadir.is_dir() {
panic!("Datadir {:?} exists but is not a directory", datadir);
}
} else {
std::fs::create_dir_all(&datadir).expect("Failed to create data directory");
}
datadir
Copy link
Preview

Copilot AI Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using panic for error handling is not recommended. Consider returning a Result type and handling this error case gracefully.

Suggested change
pub fn init_datadir(datadir: PathBuf) -> PathBuf {
if datadir.exists() {
if !datadir.is_dir() {
panic!("Datadir {:?} exists but is not a directory", datadir);
}
} else {
std::fs::create_dir_all(&datadir).expect("Failed to create data directory");
}
datadir
pub fn init_datadir(datadir: PathBuf) -> io::Result<PathBuf> {
if datadir.exists() {
if !datadir.is_dir() {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
format!("Datadir {:?} exists but is not a directory", datadir),
));
}
} else {
std::fs::create_dir_all(&datadir)?;
}
Ok(datadir)

Copilot uses AI. Check for mistakes.

@@ -49,14 +48,13 @@ pub struct Options {
#[arg(
long = "datadir",
value_name = "DATABASE_DIRECTORY",
help = "If the datadir is the word `memory`, ethrex will use the InMemory Engine",
default_value = DEFAULT_DATADIR,
default_value = default_datadir("ethrex"),
help = "Receives the name of the directory where the Database is located.",
long_help = "If the datadir is the word `memory`, ethrex will use the `InMemory Engine`.",
help_heading = "Node options",
env = "ETHREX_DATADIR"
)]
Copy link
Preview

Copilot AI Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling default_datadir() at compile time in the default_value attribute may not work as expected since it requires runtime evaluation. Consider using a default value function or handling this in the parsing logic.

Copilot uses AI. Check for mistakes.

@@ -223,8 +221,8 @@ impl Default for Options {
pub enum Subcommand {
#[command(name = "removedb", about = "Remove the database")]
RemoveDB {
#[arg(long = "datadir", value_name = "DATABASE_DIRECTORY", default_value = DEFAULT_DATADIR, required = false)]
datadir: String,
#[arg(long = "datadir", value_name = "DATABASE_DIRECTORY", default_value = default_datadir("ethrex"), required = false)]
Copy link
Preview

Copilot AI Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling default_datadir() at compile time in the default_value attribute may not work as expected since it requires runtime evaluation. Consider using a default value function or handling this in the parsing logic.

Suggested change
#[arg(long = "datadir", value_name = "DATABASE_DIRECTORY", default_value = default_datadir("ethrex"), required = false)]
#[arg(long = "datadir", value_name = "DATABASE_DIRECTORY", default_value_t = default_datadir("ethrex"), required = false)]

Copilot uses AI. Check for mistakes.

@@ -54,8 +55,8 @@ pub enum Command {
},
#[command(name = "removedb", about = "Remove the database", visible_aliases = ["rm", "clean"])]
RemoveDB {
#[arg(long = "datadir", value_name = "DATABASE_DIRECTORY", default_value = DEFAULT_L2_DATADIR, required = false)]
datadir: String,
#[arg(long = "datadir", value_name = "DATABASE_DIRECTORY", default_value = default_datadir("ethrex"), required = false)]
Copy link
Preview

Copilot AI Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling default_datadir() at compile time in the default_value attribute may not work as expected since it requires runtime evaluation. Consider using a default value function or handling this in the parsing logic.

Suggested change
#[arg(long = "datadir", value_name = "DATABASE_DIRECTORY", default_value = default_datadir("ethrex"), required = false)]
#[arg(long = "datadir", value_name = "DATABASE_DIRECTORY", default_value_t = default_datadir("ethrex"), required = false)]

Copilot uses AI. Check for mistakes.

@@ -111,12 +112,12 @@ pub enum Command {
network: Network,
#[arg(
long = "datadir",
default_value = default_datadir("ethrex-l2"),
Copy link
Preview

Copilot AI Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling default_datadir() at compile time in the default_value attribute may not work as expected since it requires runtime evaluation. Consider using a default value function or handling this in the parsing logic.

Suggested change
default_value = default_datadir("ethrex-l2"),
default_value_t = default_ethrex_l2_datadir(),

Copilot uses AI. Check for mistakes.

@@ -302,22 +302,21 @@ struct Args {
#[arg(
long = "datadir",
value_name = "DATABASE_DIRECTORY",
help = "If the datadir is the word `memory`, ethrex will use the InMemory Engine",
default_value = DEFAULT_DATADIR,
default_value = default_datadir("ethrex"),
Copy link
Preview

Copilot AI Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling default_datadir() at compile time in the default_value attribute may not work as expected since it requires runtime evaluation. Consider using a default value function or handling this in the parsing logic.

Suggested change
default_value = default_datadir("ethrex"),
default_value_t = default_datadir("ethrex"),

Copilot uses AI. Check for mistakes.

Copy link
Contributor

@fmoletta fmoletta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes the behaviour of the node:
For example, If I run:

cargo run --release --bin ethrex -- --http.addr 0.0.0.0 --network sepolia --authrpc.jwtsecret jwt.hex --datadir example

On main: A directory named example will be created at /Users/username/Library/Application Support(on Mac)
On this branch: A folder named example will be created at the crate's root

@mpaulucci
Copy link
Collaborator Author

This changes the behaviour of the node: For example, If I run:

cargo run --release --bin ethrex -- --http.addr 0.0.0.0 --network sepolia --authrpc.jwtsecret jwt.hex --datadir example

On main: A directory named example will be created at /Users/username/Library/Application Support(on Mac) On this branch: A folder named example will be created at the crate's root

good find. I think the PR behaviour is desirable. What do you think?

@ilitteri ilitteri moved this to In Review in ethrex_l2 Jul 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
L2 Rollup client
Projects
Status: In Review
Development

Successfully merging this pull request may close these issues.

2 participants