-
Notifications
You must be signed in to change notification settings - Fork 87
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
base: main
Are you sure you want to change the base?
Conversation
Lines of code reportTotal lines added: Detailed view
|
Path
instead of String
for path cli args
There was a problem hiding this 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 withPathBuf
across CLI options and function signatures - Renames
set_datadir
toinit_datadir
and addsdefault_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; |
There was a problem hiding this comment.
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.
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; |
There was a problem hiding this comment.
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"), |
There was a problem hiding this comment.
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.
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"), |
There was a problem hiding this comment.
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.
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.
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 |
There was a problem hiding this comment.
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.
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" | |||
)] |
There was a problem hiding this comment.
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)] |
There was a problem hiding this comment.
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.
#[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)] |
There was a problem hiding this comment.
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.
#[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"), |
There was a problem hiding this comment.
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.
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"), |
There was a problem hiding this comment.
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.
default_value = default_datadir("ethrex"), | |
default_value_t = default_datadir("ethrex"), |
Copilot uses AI. Check for mistakes.
There was a problem hiding this 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
good find. I think the PR behaviour is desirable. What do you think? |
Motivation
Lets use Paths that are a higher abstraction for dealing with paths.
Description
String
paths in favor ofPath
andPathBuf
set_datadir
toinit_datadir
.default_datadir
add added it to the default cli option so that you can see it when runningethrex --help
jwt_secret_path
is relative path, set it as part of the default path.