refactor: move cli utils to new reth-cli-utils crate (#790)

* Move bin/src/util to reth-cli-utils

* Add reth-cli-utils to workspace members

* Fix imports in bin/src

* Create reth-cli-utils crate

* Add utils import
This commit is contained in:
David Kulman
2023-01-10 01:13:41 +01:00
committed by GitHub
parent 7c9c2fea50
commit 515590faa8
14 changed files with 87 additions and 38 deletions

17
Cargo.lock generated
View File

@ -3541,6 +3541,7 @@ dependencies = [
"metrics", "metrics",
"metrics-exporter-prometheus", "metrics-exporter-prometheus",
"metrics-util", "metrics-util",
"reth-cli-utils",
"reth-consensus", "reth-consensus",
"reth-db", "reth-db",
"reth-downloaders", "reth-downloaders",
@ -3565,6 +3566,22 @@ dependencies = [
"walkdir", "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]] [[package]]
name = "reth-codecs" name = "reth-codecs"
version = "0.1.0" version = "0.1.0"

View File

@ -31,5 +31,6 @@ members = [
"crates/tasks", "crates/tasks",
"crates/transaction-pool", "crates/transaction-pool",
"crates/metrics/metrics-derive", "crates/metrics/metrics-derive",
"crates/cli/utils",
] ]
default-members = ["bin/reth"] default-members = ["bin/reth"]

View File

@ -21,6 +21,7 @@ reth-executor = { path = "../../crates/executor" }
reth-rlp = { path = "../../crates/common/rlp" } reth-rlp = { path = "../../crates/common/rlp" }
reth-network = {path = "../../crates/net/network", features = ["serde"] } reth-network = {path = "../../crates/net/network", features = ["serde"] }
reth-downloaders = {path = "../../crates/net/downloaders" } reth-downloaders = {path = "../../crates/net/downloaders" }
reth-cli-utils = { path = "../../crates/cli/utils" }
# tracing # tracing
tracing = "0.1" tracing = "0.1"

View File

@ -1,10 +1,9 @@
//! CLI definition and entrypoint to executable //! CLI definition and entrypoint to executable
use crate::{ use crate::{db, node, p2p, stage, test_eth_chain};
db, node, p2p, stage, test_eth_chain,
util::reth_tracing::{self, TracingMode},
};
use clap::{ArgAction, Parser, Subcommand}; use clap::{ArgAction, Parser, Subcommand};
use reth_cli_utils::reth_tracing::{self, TracingMode};
use tracing_subscriber::util::SubscriberInitExt; use tracing_subscriber::util::SubscriberInitExt;
/// main function that parses cli and runs command /// main function that parses cli and runs command

View File

@ -1,5 +1,5 @@
//! reth data directories. //! reth data directories.
use crate::util::parse_path; use reth_cli_utils::parse_path;
use std::{ use std::{
env::VarError, env::VarError,
fmt::{Debug, Display, Formatter}, fmt::{Debug, Display, Formatter},

View File

@ -15,9 +15,9 @@ pub mod p2p;
pub mod prometheus_exporter; pub mod prometheus_exporter;
pub mod stage; pub mod stage;
pub mod test_eth_chain; pub mod test_eth_chain;
pub mod util;
use clap::Parser; use clap::Parser;
pub use reth_cli_utils as utils;
use reth_primitives::NodeRecord; use reth_primitives::NodeRecord;
#[derive(Debug, Parser)] #[derive(Debug, Parser)]

View File

@ -4,16 +4,15 @@
use crate::{ use crate::{
config::Config, config::Config,
dirs::{ConfigPath, DbPath}, dirs::{ConfigPath, DbPath},
prometheus_exporter, prometheus_exporter, NetworkOpts,
util::{
chainspec::{chain_spec_value_parser, ChainSpecification},
init::{init_db, init_genesis},
parse_socket_address,
},
NetworkOpts,
}; };
use clap::{crate_version, Parser}; use clap::{crate_version, Parser};
use fdlimit::raise_fd_limit; 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_consensus::BeaconConsensus;
use reth_downloaders::{bodies, headers}; use reth_downloaders::{bodies, headers};
use reth_executor::Config as ExecutorConfig; use reth_executor::Config as ExecutorConfig;

View File

@ -1,6 +1,10 @@
//! P2P Debugging tool //! P2P Debugging tool
use backon::{ConstantBackoff, Retryable}; use backon::{ConstantBackoff, Retryable};
use clap::{Parser, Subcommand}; 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_db::mdbx::{Env, EnvKind, WriteMap};
use reth_interfaces::p2p::{ use reth_interfaces::p2p::{
bodies::client::BodiesClient, bodies::client::BodiesClient,
@ -10,14 +14,7 @@ use reth_network::FetchClient;
use reth_primitives::{BlockHashOrNumber, Header, NodeRecord, SealedHeader}; use reth_primitives::{BlockHashOrNumber, Header, NodeRecord, SealedHeader};
use std::sync::Arc; use std::sync::Arc;
use crate::{ use crate::{config::Config, dirs::ConfigPath};
config::Config,
dirs::ConfigPath,
util::{
chainspec::{chain_spec_value_parser, ChainSpecification},
hash_or_num_value_parser,
},
};
/// `reth p2p` command /// `reth p2p` command
#[derive(Debug, Parser)] #[derive(Debug, Parser)]

View File

@ -4,12 +4,11 @@
use crate::{ use crate::{
config::Config, config::Config,
dirs::{ConfigPath, DbPath}, dirs::{ConfigPath, DbPath},
prometheus_exporter, prometheus_exporter, NetworkOpts,
util::{ };
chainspec::{chain_spec_value_parser, ChainSpecification}, use reth_cli_utils::{
init::{init_db, init_genesis}, chainspec::{chain_spec_value_parser, ChainSpecification},
}, init::{init_db, init_genesis},
NetworkOpts,
}; };
use reth_consensus::BeaconConsensus; use reth_consensus::BeaconConsensus;
use reth_downloaders::bodies::concurrent::ConcurrentDownloader; use reth_downloaders::bodies::concurrent::ConcurrentDownloader;

View File

@ -1,6 +1,5 @@
//! Command for running Ethereum chain tests. //! Command for running Ethereum chain tests.
use crate::util;
use clap::Parser; use clap::Parser;
use eyre::eyre; use eyre::eyre;
use std::path::PathBuf; use std::path::PathBuf;
@ -24,7 +23,7 @@ impl Command {
let futs: Vec<_> = self let futs: Vec<_> = self
.path .path
.iter() .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) }) .map(|file| async { (runner::run_test(file.clone()).await, file) })
.collect(); .collect();

View File

@ -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"] }

View File

@ -72,9 +72,15 @@ pub struct GenesisAccount {
/// to a custom one. /// to a custom one.
pub fn chain_spec_value_parser(s: &str) -> Result<ChainSpecification, eyre::Error> { pub fn chain_spec_value_parser(s: &str) -> Result<ChainSpecification, eyre::Error> {
Ok(match s { Ok(match s {
"mainnet" => serde_json::from_str(include_str!("../../res/chainspec/mainnet.json"))?, "mainnet" => {
"goerli" => serde_json::from_str(include_str!("../../res/chainspec/goerli.json"))?, serde_json::from_str(include_str!("../../../../bin/reth/res/chainspec/mainnet.json"))?
"sepolia" => serde_json::from_str(include_str!("../../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()))?; let raw = std::fs::read_to_string(PathBuf::from(shellexpand::full(s)?.into_owned()))?;
serde_json::from_str(&raw)? serde_json::from_str(&raw)?

View File

@ -1,4 +1,4 @@
use crate::util::chainspec::Genesis; use crate::chainspec::Genesis;
use reth_db::{ use reth_db::{
cursor::DbCursorRO, cursor::DbCursorRO,
database::Database, database::Database,

View File

@ -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. //! Utility functions.
use reth_primitives::{BlockHashOrNumber, H256}; use reth_primitives::{BlockHashOrNumber, H256};
use std::{ use std::{
@ -15,7 +22,7 @@ pub mod chainspec;
pub mod init; pub mod init;
/// Finds all files in a directory with a given postfix. /// Finds all files in a directory with a given postfix.
pub(crate) fn find_all_files_with_postfix(path: &Path, postfix: &str) -> Vec<PathBuf> { pub fn find_all_files_with_postfix(path: &Path, postfix: &str) -> Vec<PathBuf> {
WalkDir::new(path) WalkDir::new(path)
.into_iter() .into_iter()
.filter_map(|e| e.ok()) .filter_map(|e| e.ok())
@ -26,12 +33,12 @@ pub(crate) fn find_all_files_with_postfix(path: &Path, postfix: &str) -> Vec<Pat
/// Parses a user-specified path with support for environment variables and common shorthands (e.g. /// Parses a user-specified path with support for environment variables and common shorthands (e.g.
/// ~ for the user's home directory). /// ~ for the user's home directory).
pub(crate) fn parse_path(value: &str) -> Result<PathBuf, shellexpand::LookupError<VarError>> { pub fn parse_path(value: &str) -> Result<PathBuf, shellexpand::LookupError<VarError>> {
shellexpand::full(value).map(|path| PathBuf::from(path.into_owned())) shellexpand::full(value).map(|path| PathBuf::from(path.into_owned()))
} }
/// Parse [BlockHashOrNumber] /// Parse [BlockHashOrNumber]
pub(crate) fn hash_or_num_value_parser(value: &str) -> Result<BlockHashOrNumber, eyre::Error> { pub fn hash_or_num_value_parser(value: &str) -> Result<BlockHashOrNumber, eyre::Error> {
match H256::from_str(value) { match H256::from_str(value) {
Ok(hash) => Ok(BlockHashOrNumber::Hash(hash)), Ok(hash) => Ok(BlockHashOrNumber::Hash(hash)),
Err(_) => Ok(BlockHashOrNumber::Number(value.parse()?)), Err(_) => Ok(BlockHashOrNumber::Number(value.parse()?)),
@ -48,7 +55,7 @@ pub(crate) fn hash_or_num_value_parser(value: &str) -> Result<BlockHashOrNumber,
/// - Otherwise it is assumed to be a hostname /// - Otherwise it is assumed to be a hostname
/// ///
/// An error is returned if the value is empty. /// An error is returned if the value is empty.
pub(crate) fn parse_socket_address(value: &str) -> Result<SocketAddr, eyre::Error> { pub fn parse_socket_address(value: &str) -> Result<SocketAddr, eyre::Error> {
if value.is_empty() { if value.is_empty() {
eyre::bail!("Cannot parse socket address from an empty string"); eyre::bail!("Cannot parse socket address from an empty string");
} }