Integrate RPC in CLI (#1227)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
Ikechukwu Ahiara Marvellous
2023-02-09 11:31:48 +01:00
committed by GitHub
parent 92ff3f961d
commit 9470943bab
3 changed files with 90 additions and 3 deletions

View File

@@ -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<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

@@ -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<u64>,
#[clap(flatten)]
rpc: RpcServerOpts,
}
impl Command {

View File

@@ -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<Self, Self::Err> {
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,