refactor: move structs to args mod (#1250)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
Chirag Baghasingh
2023-02-11 10:51:14 +05:30
committed by GitHub
parent ea5633b3c3
commit b23bd7c609
9 changed files with 158 additions and 134 deletions

9
bin/reth/src/args/mod.rs Normal file
View File

@ -0,0 +1,9 @@
//! Parameters for configuring the rpc more granularity via CLI
/// NetworkArg struct
mod network_args;
pub use network_args::NetworkArgs;
/// RpcServerArg struct
mod rpc_server_args;
pub use rpc_server_args::RpcServerArgs;

View File

@ -0,0 +1,39 @@
//! clap [Args](clap::Args) for network related arguments.
use crate::dirs::{KnownPeersPath, PlatformPath};
use clap::Args;
use reth_primitives::NodeRecord;
/// Parameters for configuring the network more granularity via CLI
#[derive(Debug, Args)]
#[command(next_help_heading = "Networking")]
pub struct NetworkArgs {
/// Disable the discovery service.
#[arg(short, long)]
pub disable_discovery: bool,
/// Target trusted peer enodes
/// --trusted-peers enode://abcd@192.168.0.1:30303
#[arg(long)]
pub trusted_peers: Vec<NodeRecord>,
/// Connect only to trusted peers
#[arg(long)]
pub trusted_only: bool,
/// Nodes to bootstrap network discovery.
///
/// Will fall back to a network-specific default if not specified.
#[arg(long, value_delimiter = ',')]
pub bootnodes: Option<Vec<NodeRecord>>,
/// The path to the known peers file. Connected peers are
/// dumped to this file on node shutdown, and read on startup.
/// Cannot be used with --no-persist-peers
#[arg(long, value_name = "FILE", verbatim_doc_comment, default_value_t)]
pub peers_file: PlatformPath<KnownPeersPath>,
/// Do not persist peers. Cannot be used with --peers-file
#[arg(long, verbatim_doc_comment, conflicts_with = "peers_file")]
pub no_persist_peers: bool,
}

View File

@ -0,0 +1,75 @@
//! clap [Args](clap::Args) for RPC related arguments.
use clap::Args;
use reth_rpc_builder::RpcModuleConfig;
use std::net::IpAddr;
/// Parameters for configuring the rpc more granularity via CLI
#[derive(Debug, Args, PartialEq, Default)]
#[command(next_help_heading = "Rpc")]
pub struct RpcServerArgs {
/// Enable the HTTP-RPC server
#[arg(long)]
pub http: bool,
/// Http server address to listen on
#[arg(long = "http.addr")]
pub http_addr: Option<IpAddr>,
/// Http server port to listen on
#[arg(long = "http.port")]
pub http_port: Option<u16>,
/// Rpc Modules to be configured for http server
#[arg(long = "http.api")]
pub http_api: Option<RpcModuleConfig>,
/// Enable the WS-RPC server
#[arg(long)]
pub ws: bool,
/// Ws server address to listen on
#[arg(long = "ws.addr")]
pub ws_addr: Option<IpAddr>,
/// Http server port to listen on
#[arg(long = "ws.port")]
pub ws_port: Option<u16>,
/// Rpc Modules to be configured for Ws server
#[arg(long = "ws.api")]
pub ws_api: Option<RpcModuleConfig>,
/// Disable the IPC-RPC server
#[arg(long)]
pub ipcdisable: bool,
/// Filename for IPC socket/pipe within the datadir
#[arg(long)]
pub ipcpath: Option<String>,
}
#[cfg(test)]
mod tests {
use super::*;
use clap::Parser;
/// A helper type to parse Args more easily
#[derive(Parser)]
struct CommandParser<T: Args> {
#[clap(flatten)]
args: T,
}
#[test]
fn test_rpc_server_args_parser() {
let args =
CommandParser::<RpcServerArgs>::parse_from(["reth", "--http.api", "eth,admin,debug"])
.args;
let apis = args.http_api.unwrap();
let expected = RpcModuleConfig::try_from_selection(["eth", "admin", "debug"]).unwrap();
assert_eq!(apis, expected);
}
}

View File

@ -1,7 +1,6 @@
use crate::{
dirs::{ConfigPath, DbPath, PlatformPath},
node::{handle_events, NodeEvent},
utils::{chainspec::genesis_value_parser, init::init_db},
};
use clap::{crate_version, Parser};
use eyre::Context;
@ -17,7 +16,13 @@ use reth_interfaces::{
sync::SyncStateUpdater,
};
use reth_primitives::ChainSpec;
use reth_staged_sync::{utils::init::init_genesis, Config};
use reth_staged_sync::{
utils::{
chainspec::genesis_value_parser,
init::{init_db, init_genesis},
},
Config,
};
use reth_stages::{
prelude::*,
stages::{ExecutionStage, SenderRecoveryStage, TotalDifficultyStage},

View File

@ -1,10 +1,10 @@
use crate::{
dirs::{DbPath, PlatformPath},
utils::chainspec::genesis_value_parser,
};
use crate::dirs::{DbPath, PlatformPath};
use clap::Parser;
use reth_primitives::ChainSpec;
use reth_staged_sync::utils::init::{init_db, init_genesis};
use reth_staged_sync::utils::{
chainspec::genesis_value_parser,
init::{init_db, init_genesis},
};
use std::sync::Arc;
use tracing::info;

View File

@ -6,6 +6,7 @@
))]
//! Rust Ethereum (reth) binary executable.
pub mod args;
pub mod chain;
pub mod cli;
pub mod db;
@ -16,115 +17,3 @@ pub mod prometheus_exporter;
pub mod stage;
pub mod test_eth_chain;
pub mod test_vectors;
use dirs::{KnownPeersPath, PlatformPath};
use std::net::IpAddr;
use reth_rpc_builder::RpcModuleConfig;
pub use reth_staged_sync::utils;
use clap::Args;
use reth_primitives::NodeRecord;
/// Parameters for configuring the network more granularity via CLI
#[derive(Debug, Args)]
#[command(next_help_heading = "Networking")]
struct NetworkOpts {
/// Disable the discovery service.
#[arg(short, long)]
disable_discovery: bool,
/// Target trusted peer enodes
/// --trusted-peers enode://abcd@192.168.0.1:30303
#[arg(long)]
trusted_peers: Vec<NodeRecord>,
/// Connect only to trusted peers
#[arg(long)]
trusted_only: bool,
/// Bootnodes to connect to initially.
///
/// Will fall back to a network-specific default if not specified.
#[arg(long, value_delimiter = ',')]
bootnodes: Option<Vec<NodeRecord>>,
/// The path to the known peers file. Connected peers are
/// dumped to this file on node shutdown, and read on startup.
/// Cannot be used with --no-persist-peers
#[arg(long, value_name = "FILE", verbatim_doc_comment, default_value_t)]
peers_file: PlatformPath<KnownPeersPath>,
/// Do not persist peers. Cannot be used with --peers-file
#[arg(long, verbatim_doc_comment, conflicts_with = "peers_file")]
no_persist_peers: bool,
}
/// Parameters for configuring the rpc more granularity via CLI
#[derive(Debug, Args, PartialEq, Default)]
#[command(next_help_heading = "Rpc")]
struct RpcServerOpts {
/// Enable the HTTP-RPC server
#[arg(long)]
http: bool,
/// Http server address to listen on
#[arg(long = "http.addr")]
http_addr: Option<IpAddr>,
/// Http server port to listen on
#[arg(long = "http.port")]
http_port: Option<u16>,
/// Rpc Modules to be configured for http server
#[arg(long = "http.api")]
http_api: Option<RpcModuleConfig>,
/// Enable the WS-RPC server
#[arg(long)]
ws: bool,
/// Ws server address to listen on
#[arg(long = "ws.addr")]
ws_addr: Option<IpAddr>,
/// Http server port to listen on
#[arg(long = "ws.port")]
ws_port: Option<u16>,
/// Rpc Modules to be configured for Ws server
#[arg(long = "ws.api")]
ws_api: Option<RpcModuleConfig>,
/// Disable the IPC-RPC server
#[arg(long)]
ipcdisable: bool,
/// Filename for IPC socket/pipe within the datadir
#[arg(long)]
ipcpath: Option<String>,
}
#[cfg(test)]
mod tests {
use super::*;
use clap::Parser;
/// A helper type to parse Args more easily
#[derive(Parser)]
struct CommandParser<T: Args> {
#[clap(flatten)]
args: T,
}
#[test]
fn test_rpc_server_opts_parser() {
let opts =
CommandParser::<RpcServerOpts>::parse_from(["reth", "--http.api", "eth,admin,debug"])
.args;
let apis = opts.http_api.unwrap();
let expected = RpcModuleConfig::try_from_selection(["eth", "admin", "debug"]).unwrap();
assert_eq!(apis, expected);
}
}

View File

@ -2,10 +2,9 @@
//!
//! Starts the client
use crate::{
args::{NetworkArgs, RpcServerArgs},
dirs::{ConfigPath, DbPath, PlatformPath},
prometheus_exporter,
utils::{chainspec::genesis_value_parser, init::init_db, parse_socket_address},
NetworkOpts, RpcServerOpts,
};
use clap::{crate_version, Parser};
use eyre::Context;
@ -28,7 +27,14 @@ use reth_network_api::NetworkInfo;
use reth_primitives::{BlockNumber, ChainSpec, H256};
use reth_provider::ShareableDatabase;
use reth_rpc_builder::{RethRpcModule, RpcServerConfig, TransportRpcModuleConfig};
use reth_staged_sync::{utils::init::init_genesis, Config};
use reth_staged_sync::{
utils::{
chainspec::genesis_value_parser,
init::{init_db, init_genesis},
parse_socket_address,
},
Config,
};
use reth_stages::{
prelude::*,
stages::{ExecutionStage, SenderRecoveryStage, TotalDifficultyStage},
@ -77,7 +83,7 @@ pub struct Command {
metrics: Option<SocketAddr>,
#[clap(flatten)]
network: NetworkOpts,
network: NetworkArgs,
#[arg(long, default_value = "any")]
nat: NatResolver,
@ -93,7 +99,7 @@ pub struct Command {
max_block: Option<u64>,
#[clap(flatten)]
rpc: RpcServerOpts,
rpc: RpcServerArgs,
}
impl Command {

View File

@ -1,8 +1,5 @@
//! P2P Debugging tool
use crate::{
dirs::{ConfigPath, PlatformPath},
utils::{chainspec::chain_spec_value_parser, hash_or_num_value_parser},
};
use crate::dirs::{ConfigPath, PlatformPath};
use backon::{ConstantBackoff, Retryable};
use clap::{Parser, Subcommand};
use reth_db::mdbx::{Env, EnvKind, WriteMap};
@ -13,7 +10,10 @@ use reth_interfaces::p2p::{
};
use reth_network::FetchClient;
use reth_primitives::{BlockHashOrNumber, ChainSpec, NodeRecord, SealedHeader};
use reth_staged_sync::Config;
use reth_staged_sync::{
utils::{chainspec::chain_spec_value_parser, hash_or_num_value_parser},
Config,
};
use std::sync::Arc;
/// `reth p2p` command

View File

@ -2,17 +2,18 @@
//!
//! Stage debugging tool
use crate::{
args::NetworkArgs,
dirs::{ConfigPath, DbPath, PlatformPath},
prometheus_exporter,
utils::{chainspec::chain_spec_value_parser, init::init_db},
NetworkOpts,
};
use reth_consensus::beacon::BeaconConsensus;
use reth_downloaders::bodies::bodies::BodiesDownloaderBuilder;
use reth_net_nat::NatResolver;
use reth_primitives::ChainSpec;
use reth_staged_sync::Config;
use reth_staged_sync::{
utils::{chainspec::chain_spec_value_parser, init::init_db},
Config,
};
use reth_stages::{
stages::{BodyStage, ExecutionStage, SenderRecoveryStage},
ExecInput, Stage, StageId, Transaction, UnwindInput,
@ -83,7 +84,7 @@ pub struct Command {
skip_unwind: bool,
#[clap(flatten)]
network: NetworkOpts,
network: NetworkArgs,
#[arg(long, default_value = "any")]
nat: NatResolver,