diff --git a/Cargo.lock b/Cargo.lock index 176dd27fa..29ccb0b53 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3541,6 +3541,7 @@ dependencies = [ "metrics", "metrics-exporter-prometheus", "metrics-util", + "reth-cli-utils", "reth-consensus", "reth-db", "reth-downloaders", @@ -3565,6 +3566,22 @@ dependencies = [ "walkdir", ] +[[package]] +name = "reth-cli-utils" +version = "0.1.0" +dependencies = [ + "eyre", + "reth-consensus", + "reth-db", + "reth-primitives", + "serde", + "serde_json", + "shellexpand", + "tracing", + "tracing-subscriber", + "walkdir", +] + [[package]] name = "reth-codecs" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index b9d44f274..766fe3b9f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,5 +31,6 @@ members = [ "crates/tasks", "crates/transaction-pool", "crates/metrics/metrics-derive", + "crates/cli/utils", ] -default-members = ["bin/reth"] \ No newline at end of file +default-members = ["bin/reth"] diff --git a/bin/reth/Cargo.toml b/bin/reth/Cargo.toml index 6b1256556..e20d8cb40 100644 --- a/bin/reth/Cargo.toml +++ b/bin/reth/Cargo.toml @@ -21,6 +21,7 @@ reth-executor = { path = "../../crates/executor" } reth-rlp = { path = "../../crates/common/rlp" } reth-network = {path = "../../crates/net/network", features = ["serde"] } reth-downloaders = {path = "../../crates/net/downloaders" } +reth-cli-utils = { path = "../../crates/cli/utils" } # tracing tracing = "0.1" diff --git a/bin/reth/src/cli.rs b/bin/reth/src/cli.rs index 0102e90bc..d2cce283b 100644 --- a/bin/reth/src/cli.rs +++ b/bin/reth/src/cli.rs @@ -1,10 +1,9 @@ //! CLI definition and entrypoint to executable -use crate::{ - db, node, p2p, stage, test_eth_chain, - util::reth_tracing::{self, TracingMode}, -}; +use crate::{db, node, p2p, stage, test_eth_chain}; + use clap::{ArgAction, Parser, Subcommand}; +use reth_cli_utils::reth_tracing::{self, TracingMode}; use tracing_subscriber::util::SubscriberInitExt; /// main function that parses cli and runs command diff --git a/bin/reth/src/dirs.rs b/bin/reth/src/dirs.rs index 55bab7836..79af6e9ef 100644 --- a/bin/reth/src/dirs.rs +++ b/bin/reth/src/dirs.rs @@ -1,5 +1,5 @@ //! reth data directories. -use crate::util::parse_path; +use reth_cli_utils::parse_path; use std::{ env::VarError, fmt::{Debug, Display, Formatter}, diff --git a/bin/reth/src/lib.rs b/bin/reth/src/lib.rs index db76381c4..af97b408d 100644 --- a/bin/reth/src/lib.rs +++ b/bin/reth/src/lib.rs @@ -15,9 +15,9 @@ pub mod p2p; pub mod prometheus_exporter; pub mod stage; pub mod test_eth_chain; -pub mod util; use clap::Parser; +pub use reth_cli_utils as utils; use reth_primitives::NodeRecord; #[derive(Debug, Parser)] diff --git a/bin/reth/src/node/mod.rs b/bin/reth/src/node/mod.rs index f07b891fc..66d1953c8 100644 --- a/bin/reth/src/node/mod.rs +++ b/bin/reth/src/node/mod.rs @@ -4,16 +4,15 @@ use crate::{ config::Config, dirs::{ConfigPath, DbPath}, - prometheus_exporter, - util::{ - chainspec::{chain_spec_value_parser, ChainSpecification}, - init::{init_db, init_genesis}, - parse_socket_address, - }, - NetworkOpts, + prometheus_exporter, NetworkOpts, }; use clap::{crate_version, Parser}; use fdlimit::raise_fd_limit; +use reth_cli_utils::{ + chainspec::{chain_spec_value_parser, ChainSpecification}, + init::{init_db, init_genesis}, + parse_socket_address, +}; use reth_consensus::BeaconConsensus; use reth_downloaders::{bodies, headers}; use reth_executor::Config as ExecutorConfig; diff --git a/bin/reth/src/p2p/mod.rs b/bin/reth/src/p2p/mod.rs index 792fc8951..d9d28e3c9 100644 --- a/bin/reth/src/p2p/mod.rs +++ b/bin/reth/src/p2p/mod.rs @@ -1,6 +1,10 @@ //! P2P Debugging tool use backon::{ConstantBackoff, Retryable}; use clap::{Parser, Subcommand}; +use reth_cli_utils::{ + chainspec::{chain_spec_value_parser, ChainSpecification}, + hash_or_num_value_parser, +}; use reth_db::mdbx::{Env, EnvKind, WriteMap}; use reth_interfaces::p2p::{ bodies::client::BodiesClient, @@ -10,14 +14,7 @@ use reth_network::FetchClient; use reth_primitives::{BlockHashOrNumber, Header, NodeRecord, SealedHeader}; use std::sync::Arc; -use crate::{ - config::Config, - dirs::ConfigPath, - util::{ - chainspec::{chain_spec_value_parser, ChainSpecification}, - hash_or_num_value_parser, - }, -}; +use crate::{config::Config, dirs::ConfigPath}; /// `reth p2p` command #[derive(Debug, Parser)] diff --git a/bin/reth/src/stage/mod.rs b/bin/reth/src/stage/mod.rs index 8b1dcda17..1c706f164 100644 --- a/bin/reth/src/stage/mod.rs +++ b/bin/reth/src/stage/mod.rs @@ -4,12 +4,11 @@ use crate::{ config::Config, dirs::{ConfigPath, DbPath}, - prometheus_exporter, - util::{ - chainspec::{chain_spec_value_parser, ChainSpecification}, - init::{init_db, init_genesis}, - }, - NetworkOpts, + prometheus_exporter, NetworkOpts, +}; +use reth_cli_utils::{ + chainspec::{chain_spec_value_parser, ChainSpecification}, + init::{init_db, init_genesis}, }; use reth_consensus::BeaconConsensus; use reth_downloaders::bodies::concurrent::ConcurrentDownloader; diff --git a/bin/reth/src/test_eth_chain/mod.rs b/bin/reth/src/test_eth_chain/mod.rs index 5714d210d..5b8cda28b 100644 --- a/bin/reth/src/test_eth_chain/mod.rs +++ b/bin/reth/src/test_eth_chain/mod.rs @@ -1,6 +1,5 @@ //! Command for running Ethereum chain tests. -use crate::util; use clap::Parser; use eyre::eyre; use std::path::PathBuf; @@ -24,7 +23,7 @@ impl Command { let futs: Vec<_> = self .path .iter() - .flat_map(|item| util::find_all_files_with_postfix(item, ".json")) + .flat_map(|item| reth_cli_utils::find_all_files_with_postfix(item, ".json")) .map(|file| async { (runner::run_test(file.clone()).await, file) }) .collect(); diff --git a/crates/cli/utils/Cargo.toml b/crates/cli/utils/Cargo.toml new file mode 100644 index 000000000..60ec1848f --- /dev/null +++ b/crates/cli/utils/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "reth-cli-utils" +version = "0.1.0" +edition = "2021" +license = "MIT" +repository = "https://github.com/paradigmxyz/reth" +readme = "README.md" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +reth-primitives = { path = "../../primitives" } +reth-consensus = { path = "../../consensus", features = ["serde"] } +reth-db = {path = "../../storage/db", features = ["mdbx", "test-utils"] } + + +serde = "1.0" +serde_json = "1.0" +eyre = "0.6.8" +shellexpand = "2.1" +walkdir = "2.3" + +tracing = "0.1" +tracing-subscriber = { version = "0.3", features = ["env-filter"] } diff --git a/bin/reth/src/util/chainspec.rs b/crates/cli/utils/src/chainspec.rs similarity index 87% rename from bin/reth/src/util/chainspec.rs rename to crates/cli/utils/src/chainspec.rs index c5f67f9ad..4923f264c 100644 --- a/bin/reth/src/util/chainspec.rs +++ b/crates/cli/utils/src/chainspec.rs @@ -72,9 +72,15 @@ pub struct GenesisAccount { /// to a custom one. pub fn chain_spec_value_parser(s: &str) -> Result { Ok(match s { - "mainnet" => serde_json::from_str(include_str!("../../res/chainspec/mainnet.json"))?, - "goerli" => serde_json::from_str(include_str!("../../res/chainspec/goerli.json"))?, - "sepolia" => serde_json::from_str(include_str!("../../res/chainspec/mainnet.json"))?, + "mainnet" => { + serde_json::from_str(include_str!("../../../../bin/reth/res/chainspec/mainnet.json"))? + } + "goerli" => { + serde_json::from_str(include_str!("../../../../bin/reth/res/chainspec/goerli.json"))? + } + "sepolia" => { + serde_json::from_str(include_str!("../../../../bin/reth/res/chainspec/sepolia.json"))? + } _ => { let raw = std::fs::read_to_string(PathBuf::from(shellexpand::full(s)?.into_owned()))?; serde_json::from_str(&raw)? diff --git a/bin/reth/src/util/init.rs b/crates/cli/utils/src/init.rs similarity index 98% rename from bin/reth/src/util/init.rs rename to crates/cli/utils/src/init.rs index 7bbfa5f4d..e72558ccb 100644 --- a/bin/reth/src/util/init.rs +++ b/crates/cli/utils/src/init.rs @@ -1,4 +1,4 @@ -use crate::util::chainspec::Genesis; +use crate::chainspec::Genesis; use reth_db::{ cursor::DbCursorRO, database::Database, diff --git a/bin/reth/src/util/mod.rs b/crates/cli/utils/src/lib.rs similarity index 88% rename from bin/reth/src/util/mod.rs rename to crates/cli/utils/src/lib.rs index 00b945653..b2361cbab 100644 --- a/bin/reth/src/util/mod.rs +++ b/crates/cli/utils/src/lib.rs @@ -1,3 +1,10 @@ +#![warn(missing_docs, unreachable_pub)] +#![deny(unused_must_use, rust_2018_idioms)] +#![doc(test( + no_crate_inject, + attr(deny(warnings, rust_2018_idioms), allow(dead_code, unused_variables)) +))] + //! Utility functions. use reth_primitives::{BlockHashOrNumber, H256}; use std::{ @@ -15,7 +22,7 @@ pub mod chainspec; pub mod init; /// Finds all files in a directory with a given postfix. -pub(crate) fn find_all_files_with_postfix(path: &Path, postfix: &str) -> Vec { +pub fn find_all_files_with_postfix(path: &Path, postfix: &str) -> Vec { WalkDir::new(path) .into_iter() .filter_map(|e| e.ok()) @@ -26,12 +33,12 @@ pub(crate) fn find_all_files_with_postfix(path: &Path, postfix: &str) -> Vec Result> { +pub fn parse_path(value: &str) -> Result> { shellexpand::full(value).map(|path| PathBuf::from(path.into_owned())) } /// Parse [BlockHashOrNumber] -pub(crate) fn hash_or_num_value_parser(value: &str) -> Result { +pub fn hash_or_num_value_parser(value: &str) -> Result { match H256::from_str(value) { Ok(hash) => Ok(BlockHashOrNumber::Hash(hash)), Err(_) => Ok(BlockHashOrNumber::Number(value.parse()?)), @@ -48,7 +55,7 @@ pub(crate) fn hash_or_num_value_parser(value: &str) -> Result Result { +pub fn parse_socket_address(value: &str) -> Result { if value.is_empty() { eyre::bail!("Cannot parse socket address from an empty string"); }