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(cli): move utils to reth-cli-utils crate #9297

Merged
merged 4 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
17 changes: 16 additions & 1 deletion Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ members = [
"crates/cli/cli/",
"crates/cli/commands/",
"crates/cli/runner/",
"crates/cli/utils/",
loocapro marked this conversation as resolved.
Show resolved Hide resolved
"crates/config/",
"crates/consensus/auto-seal/",
"crates/consensus/beacon/",
Expand Down Expand Up @@ -275,6 +276,7 @@ reth-chainspec = { path = "crates/chainspec" }
reth-cli = { path = "crates/cli/cli" }
reth-cli-commands = { path = "crates/cli/commands" }
reth-cli-runner = { path = "crates/cli/runner" }
reth-cli-utils = { path = "crates/cli/utils" }
reth-codecs = { path = "crates/storage/codecs" }
reth-codecs-derive = { path = "crates/storage/codecs/derive" }
reth-config = { path = "crates/config" }
Expand Down
1 change: 1 addition & 0 deletions bin/reth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ reth-transaction-pool.workspace = true
reth-beacon-consensus.workspace = true
reth-cli-runner.workspace = true
reth-cli-commands.workspace = true
reth-cli-utils.workspace = true
reth-consensus-common.workspace = true
reth-blockchain-tree.workspace = true
reth-rpc-builder.workspace = true
Expand Down
7 changes: 2 additions & 5 deletions bin/reth/src/commands/debug_cmd/execution.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
//! Command for debugging execution.

use crate::{
args::{get_secret_key, NetworkArgs},
macros::block_executor,
utils::get_single_header,
};
use crate::{args::NetworkArgs, macros::block_executor, utils::get_single_header};
use clap::Parser;
use futures::{stream::select as stream_select, StreamExt};
use reth_beacon_consensus::EthBeaconConsensus;
use reth_cli_commands::common::{AccessRights, Environment, EnvironmentArgs};
use reth_cli_runner::CliContext;
use reth_cli_utils::get_secret_key;
use reth_config::Config;
use reth_consensus::Consensus;
use reth_db::DatabaseEnv;
Expand Down
3 changes: 2 additions & 1 deletion bin/reth/src/commands/debug_cmd/in_memory_merkle.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
//! Command for debugging in-memory merkle trie calculation.

use crate::{
args::{get_secret_key, NetworkArgs},
args::NetworkArgs,
macros::block_executor,
utils::{get_single_body, get_single_header},
};
use backon::{ConstantBuilder, Retryable};
use clap::Parser;
use reth_cli_commands::common::{AccessRights, Environment, EnvironmentArgs};
use reth_cli_runner::CliContext;
use reth_cli_utils::get_secret_key;
use reth_config::Config;
use reth_db::DatabaseEnv;
use reth_errors::BlockValidationError;
Expand Down
7 changes: 2 additions & 5 deletions bin/reth/src/commands/debug_cmd/merkle.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
//! Command for debugging merkle trie calculation.
use crate::{
args::{get_secret_key, NetworkArgs},
macros::block_executor,
utils::get_single_header,
};
use crate::{args::NetworkArgs, macros::block_executor, utils::get_single_header};
use backon::{ConstantBuilder, Retryable};
use clap::Parser;
use reth_beacon_consensus::EthBeaconConsensus;
use reth_cli_commands::common::{AccessRights, Environment, EnvironmentArgs};
use reth_cli_runner::CliContext;
use reth_cli_utils::get_secret_key;
use reth_config::Config;
use reth_consensus::Consensus;
use reth_db::{tables, DatabaseEnv};
Expand Down
6 changes: 2 additions & 4 deletions bin/reth/src/commands/debug_cmd/replay_engine.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::{
args::{get_secret_key, NetworkArgs},
macros::block_executor,
};
use crate::{args::NetworkArgs, macros::block_executor};
use clap::Parser;
use eyre::Context;
use reth_basic_payload_builder::{BasicPayloadJobGenerator, BasicPayloadJobGeneratorConfig};
Expand All @@ -11,6 +8,7 @@ use reth_blockchain_tree::{
};
use reth_cli_commands::common::{AccessRights, Environment, EnvironmentArgs};
use reth_cli_runner::CliContext;
use reth_cli_utils::get_secret_key;
use reth_config::Config;
use reth_consensus::Consensus;
use reth_db::DatabaseEnv;
Expand Down
3 changes: 2 additions & 1 deletion bin/reth/src/commands/node/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
//! Main node command for launching a node

use crate::args::{
utils::{chain_help, chain_value_parser, parse_socket_address, SUPPORTED_CHAINS},
utils::{chain_help, chain_value_parser, SUPPORTED_CHAINS},
DatabaseArgs, DatadirArgs, DebugArgs, DevArgs, NetworkArgs, PayloadBuilderArgs, PruningArgs,
RpcServerArgs, TxPoolArgs,
};
use clap::{value_parser, Args, Parser};
use reth_chainspec::ChainSpec;
use reth_cli_runner::CliContext;
use reth_cli_utils::parse_socket_address;
use reth_db::{init_db, DatabaseEnv};
use reth_node_builder::{NodeBuilder, WithLaunchContext};
use reth_node_core::{node_config::NodeConfig, version};
Expand Down
4 changes: 2 additions & 2 deletions bin/reth/src/commands/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

use crate::{
args::{
get_secret_key,
utils::{chain_help, chain_value_parser, hash_or_num_value_parser, SUPPORTED_CHAINS},
utils::{chain_help, chain_value_parser, SUPPORTED_CHAINS},
DatabaseArgs, NetworkArgs,
},
utils::get_single_header,
};
use backon::{ConstantBuilder, Retryable};
use clap::{Parser, Subcommand};
use reth_chainspec::ChainSpec;
use reth_cli_utils::{get_secret_key, hash_or_num_value_parser};
use reth_config::Config;
use reth_network::NetworkConfigBuilder;
use reth_network_p2p::bodies::client::BodiesClient;
Expand Down
3 changes: 2 additions & 1 deletion bin/reth/src/commands/stage/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
//!
//! Stage debugging tool
use crate::{
args::{get_secret_key, NetworkArgs, StageEnum},
args::{NetworkArgs, StageEnum},
macros::block_executor,
prometheus_exporter,
};
use clap::Parser;
use reth_beacon_consensus::EthBeaconConsensus;
use reth_cli_commands::common::{AccessRights, Environment, EnvironmentArgs};
use reth_cli_runner::CliContext;
use reth_cli_utils::get_secret_key;
use reth_config::config::{HashingConfig, SenderRecoveryConfig, TransactionLookupConfig};
use reth_downloaders::bodies::bodies::BodiesDownloaderBuilder;
use reth_exex::ExExManagerHandle;
Expand Down
23 changes: 23 additions & 0 deletions crates/cli/utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "reth-cli-utils"
version.workspace = true
edition.workspace = true
rust-version.workspace = true
license.workspace = true
homepage.workspace = true
repository.workspace = true
exclude.workspace = true

[dependencies]
reth-fs-util.workspace = true
reth-network.workspace = true
reth-primitives.workspace = true
secp256k1.workspace = true
thiserror.workspace = true
eyre.workspace = true

[dev-dependencies]
proptest.workspace = true

[lints]
workspace = true
17 changes: 17 additions & 0 deletions crates/cli/utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//! This crate defines a set of commonly used cli utils.

#![doc(
html_logo_url = "https://raw.githubusercontent.com/paradigmxyz/reth/main/assets/reth-docs.png",
html_favicon_url = "https://avatars0.githubusercontent.com/u/97369466?s=256",
issue_tracker_base_url = "https://github.com/paradigmxyz/reth/issues/"
)]
#![cfg_attr(not(test), warn(unused_crate_dependencies))]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

/// Helper function to load a secret key from a file.
pub mod load_secret_key;
pub use load_secret_key::get_secret_key;

/// Cli parsers functions.
pub mod parsers;
pub use parsers::{hash_or_num_value_parser, parse_duration_from_secs, parse_socket_address};
96 changes: 96 additions & 0 deletions crates/cli/utils/src/parsers/mod.rs
loocapro marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use reth_primitives::{BlockHashOrNumber, B256};
use std::{
net::{IpAddr, Ipv4Addr, SocketAddr, ToSocketAddrs},
str::FromStr,
time::Duration,
};

/// Helper to parse a [Duration] from seconds
pub fn parse_duration_from_secs(arg: &str) -> eyre::Result<Duration, std::num::ParseIntError> {
let seconds = arg.parse()?;
Ok(Duration::from_secs(seconds))
}

/// Parse [`BlockHashOrNumber`]
pub fn hash_or_num_value_parser(value: &str) -> eyre::Result<BlockHashOrNumber, eyre::Error> {
match B256::from_str(value) {
Ok(hash) => Ok(BlockHashOrNumber::Hash(hash)),
Err(_) => Ok(BlockHashOrNumber::Number(value.parse()?)),
}
}

/// Error thrown while parsing a socket address.
#[derive(thiserror::Error, Debug)]
pub enum SocketAddressParsingError {
/// Failed to convert the string into a socket addr
#[error("could not parse socket address: {0}")]
Io(#[from] std::io::Error),
/// Input must not be empty
#[error("cannot parse socket address from empty string")]
Empty,
/// Failed to parse the address
#[error("could not parse socket address from {0}")]
Parse(String),
/// Failed to parse port
#[error("could not parse port: {0}")]
Port(#[from] std::num::ParseIntError),
}

/// Parse a [`SocketAddr`] from a `str`.
///
/// The following formats are checked:
///
/// - If the value can be parsed as a `u16` or starts with `:` it is considered a port, and the
/// hostname is set to `localhost`.
/// - If the value contains `:` it is assumed to be the format `<host>:<port>`
/// - Otherwise it is assumed to be a hostname
///
/// An error is returned if the value is empty.
pub fn parse_socket_address(value: &str) -> eyre::Result<SocketAddr, SocketAddressParsingError> {
if value.is_empty() {
return Err(SocketAddressParsingError::Empty)
}

if let Some(port) = value.strip_prefix(':').or_else(|| value.strip_prefix("localhost:")) {
let port: u16 = port.parse()?;
return Ok(SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), port))
}
if let Ok(port) = value.parse::<u16>() {
return Ok(SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), port))
}
value
.to_socket_addrs()?
.next()
.ok_or_else(|| SocketAddressParsingError::Parse(value.to_string()))
}

#[cfg(test)]
mod tests {
use super::*;
use proptest::prelude::Rng;
use secp256k1::rand::thread_rng;

#[test]
fn parse_socket_addresses() {
for value in ["localhost:9000", ":9000", "9000"] {
let socket_addr = parse_socket_address(value)
.unwrap_or_else(|_| panic!("could not parse socket address: {value}"));

assert!(socket_addr.ip().is_loopback());
assert_eq!(socket_addr.port(), 9000);
}
}

#[test]
fn parse_socket_address_random() {
let port: u16 = thread_rng().gen();

for value in [format!("localhost:{port}"), format!(":{port}"), port.to_string()] {
let socket_addr = parse_socket_address(&value)
.unwrap_or_else(|_| panic!("could not parse socket address: {value}"));

assert!(socket_addr.ip().is_loopback());
assert_eq!(socket_addr.port(), port);
}
}
}
1 change: 1 addition & 0 deletions crates/node/builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ reth-consensus.workspace = true
reth-consensus-debug-client.workspace = true
reth-rpc-types.workspace = true
reth-engine-util.workspace = true
reth-cli-utils.workspace = true

## async
futures.workspace = true
Expand Down
3 changes: 2 additions & 1 deletion crates/node/builder/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{
};
use futures::Future;
use reth_chainspec::ChainSpec;
use reth_cli_utils::get_secret_key;
use reth_db::{
test_utils::{create_test_rw_db_with_path, tempdir_path, TempDatabase},
DatabaseEnv,
Expand All @@ -25,7 +26,7 @@ use reth_network::{
};
use reth_node_api::{FullNodeTypes, FullNodeTypesAdapter, NodeTypes};
use reth_node_core::{
args::{get_secret_key, DatadirArgs},
args::DatadirArgs,
cli::config::{PayloadBuilderConfig, RethTransactionPoolConfig},
dirs::{ChainPath, DataDirPath, MaybePlatformPath},
node_config::NodeConfig,
Expand Down
2 changes: 1 addition & 1 deletion crates/node/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ workspace = true
# reth
reth-chainspec.workspace = true
reth-primitives.workspace = true
reth-cli-utils.workspace = true
reth-fs-util.workspace = true
reth-db = { workspace = true, features = ["mdbx"] }
reth-db-api.workspace = true
Expand Down Expand Up @@ -57,7 +58,6 @@ metrics-util.workspace = true
eyre.workspace = true
clap = { workspace = true, features = ["derive"] }
humantime.workspace = true
thiserror.workspace = true
const_format.workspace = true
rand.workspace = true
derive_more.workspace = true
Expand Down
3 changes: 0 additions & 3 deletions crates/node/core/src/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ pub use database::DatabaseArgs;
mod log;
pub use log::{ColorMode, LogArgs};

mod secret_key;
pub use secret_key::{get_secret_key, SecretKeyError};

/// `PayloadBuilderArgs` struct for configuring the payload builder
mod payload_builder;
pub use payload_builder::PayloadBuilderArgs;
Expand Down
6 changes: 2 additions & 4 deletions crates/node/core/src/args/payload_builder.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use crate::{
args::utils::parse_duration_from_secs, cli::config::PayloadBuilderConfig,
version::default_extradata,
};
use crate::{cli::config::PayloadBuilderConfig, version::default_extradata};
use clap::{
builder::{RangedU64ValueParser, TypedValueParser},
Arg, Args, Command,
};
use reth_cli_utils::parse_duration_from_secs;
use reth_primitives::constants::{
ETHEREUM_BLOCK_GAS_LIMIT, MAXIMUM_EXTRA_DATA_SIZE, SLOT_DURATION,
};
Expand Down
Loading
Loading