From 9470943babf8afe98a10db729954a374470afcb7 Mon Sep 17 00:00:00 2001 From: Ikechukwu Ahiara Marvellous Date: Thu, 9 Feb 2023 11:31:48 +0100 Subject: [PATCH] Integrate RPC in CLI (#1227) Co-authored-by: Matthias Seitz --- bin/reth/src/lib.rs | 75 ++++++++++++++++++++++++++++++- bin/reth/src/node/mod.rs | 5 ++- crates/rpc/rpc-builder/src/lib.rs | 13 +++++- 3 files changed, 90 insertions(+), 3 deletions(-) diff --git a/bin/reth/src/lib.rs b/bin/reth/src/lib.rs index fbf0e3567b..2376783b9a 100644 --- a/bin/reth/src/lib.rs +++ b/bin/reth/src/lib.rs @@ -16,12 +16,15 @@ 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 granularly via CLI +/// Parameters for configuring the network more granularity via CLI #[derive(Debug, Args)] #[command(next_help_heading = "Networking")] struct NetworkOpts { @@ -54,3 +57,73 @@ struct NetworkOpts { #[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, + + /// Http server port to listen on + #[arg(long = "http.port")] + http_port: Option, + + /// Rpc Modules to be configured for http server + #[arg(long = "http.api")] + http_api: Option, + + /// Enable the WS-RPC server + #[arg(long)] + ws: bool, + + /// Ws server address to listen on + #[arg(long = "ws.addr")] + ws_addr: Option, + + /// Http server port to listen on + #[arg(long = "ws.port")] + ws_port: Option, + + /// Rpc Modules to be configured for Ws server + #[arg(long = "ws.api")] + ws_api: Option, + + /// Disable the IPC-RPC server + #[arg(long)] + ipcdisable: bool, + + /// Filename for IPC socket/pipe within the datadir + #[arg(long)] + ipcpath: Option, +} + +#[cfg(test)] +mod tests { + use super::*; + use clap::Parser; + + /// A helper type to parse Args more easily + #[derive(Parser)] + struct CommandParser { + #[clap(flatten)] + args: T, + } + + #[test] + fn test_rpc_server_opts_parser() { + let opts = + CommandParser::::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); + } +} diff --git a/bin/reth/src/node/mod.rs b/bin/reth/src/node/mod.rs index aa30d776f2..4bd923a692 100644 --- a/bin/reth/src/node/mod.rs +++ b/bin/reth/src/node/mod.rs @@ -5,7 +5,7 @@ use crate::{ dirs::{ConfigPath, DbPath, PlatformPath}, prometheus_exporter, utils::{chainspec::chain_spec_value_parser, init::init_db, parse_socket_address}, - NetworkOpts, + NetworkOpts, RpcServerOpts, }; use clap::{crate_version, Parser}; use eyre::Context; @@ -84,6 +84,9 @@ pub struct Command { /// Runs the sync only up to the specified block #[arg(long = "debug.max-block", help_heading = "Debug")] max_block: Option, + + #[clap(flatten)] + rpc: RpcServerOpts, } impl Command { diff --git a/crates/rpc/rpc-builder/src/lib.rs b/crates/rpc/rpc-builder/src/lib.rs index a1d0e53f65..2ed928f58e 100644 --- a/crates/rpc/rpc-builder/src/lib.rs +++ b/crates/rpc/rpc-builder/src/lib.rs @@ -69,8 +69,9 @@ use std::{ collections::HashMap, fmt, net::{Ipv4Addr, SocketAddr, SocketAddrV4}, + str::FromStr, }; -use strum::{AsRefStr, EnumString, EnumVariantNames, VariantNames}; +use strum::{AsRefStr, EnumString, EnumVariantNames, ParseError, VariantNames}; /// The default port for the http/ws server pub const DEFAULT_RPC_PORT: u16 = 8545; @@ -293,6 +294,16 @@ where } } +impl FromStr for RpcModuleConfig { + type Err = ParseError; + + fn from_str(s: &str) -> Result { + let modules = s.split(','); + + RpcModuleConfig::try_from_selection(modules) + } +} + /// Represents RPC modules that are supported by reth #[derive( Debug, Clone, Copy, Eq, PartialEq, Hash, AsRefStr, EnumVariantNames, EnumString, Deserialize,