From 9c5eded1ab2f003d5833f025dfc2363bcbf4eacc Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sat, 14 Sep 2019 22:57:46 -0400 Subject: [PATCH] Add websocket config to CLI --- beacon_node/client/src/config.rs | 1 + beacon_node/src/config.rs | 1 + beacon_node/src/main.rs | 25 +++++++++++- beacon_node/websocket_server/Cargo.toml | 1 + beacon_node/websocket_server/src/config.rs | 45 ++++++++++++++++++++++ beacon_node/websocket_server/src/lib.rs | 22 +---------- 6 files changed, 74 insertions(+), 21 deletions(-) create mode 100644 beacon_node/websocket_server/src/config.rs diff --git a/beacon_node/client/src/config.rs b/beacon_node/client/src/config.rs index 1e07e7cf2..997808cb4 100644 --- a/beacon_node/client/src/config.rs +++ b/beacon_node/client/src/config.rs @@ -173,6 +173,7 @@ impl Config { self.network.apply_cli_args(args)?; self.rpc.apply_cli_args(args)?; self.rest_api.apply_cli_args(args)?; + self.websocket_server.apply_cli_args(args)?; if let Some(log_file) = args.value_of("logfile") { self.log_file = PathBuf::from(log_file); diff --git a/beacon_node/src/config.rs b/beacon_node/src/config.rs index 978e029e7..5cfb45287 100644 --- a/beacon_node/src/config.rs +++ b/beacon_node/src/config.rs @@ -552,6 +552,7 @@ impl ConfigBuilder { self.client_config.network.discovery_port += bump; self.client_config.rpc.port += bump; self.client_config.rest_api.port += bump; + self.client_config.websocket_server.port += bump; } if self.eth2_config.spec_constants != self.client_config.spec_constants { diff --git a/beacon_node/src/main.rs b/beacon_node/src/main.rs index bb88e8f92..7bc7e8abe 100644 --- a/beacon_node/src/main.rs +++ b/beacon_node/src/main.rs @@ -147,7 +147,7 @@ fn main() { .conflicts_with("port-bump") .takes_value(true), ) - /* Client related arguments */ + /* REST API related arguments */ .arg( Arg::with_name("no-api") .long("no-api") @@ -169,6 +169,29 @@ fn main() { .conflicts_with("port-bump") .takes_value(true), ) + /* Websocket related arguments */ + .arg( + Arg::with_name("no-ws") + .long("no-ws") + .help("Disable websocket server.") + .takes_value(false), + ) + .arg( + Arg::with_name("ws-address") + .long("ws-address") + .value_name("ADDRESS") + .help("Set the listen address for the websocket server.") + .conflicts_with_all(&["no-ws"]) + .takes_value(true), + ) + .arg( + Arg::with_name("ws-port") + .long("ws-port") + .value_name("PORT") + .help("Set the listen TCP port for the websocket server.") + .conflicts_with_all(&["no-ws", "port-bump"]) + .takes_value(true), + ) /* * Eth1 Integration diff --git a/beacon_node/websocket_server/Cargo.toml b/beacon_node/websocket_server/Cargo.toml index a7bf85b12..48f046e07 100644 --- a/beacon_node/websocket_server/Cargo.toml +++ b/beacon_node/websocket_server/Cargo.toml @@ -8,6 +8,7 @@ edition = "2018" [dependencies] beacon_chain = { path = "../beacon_chain" } +clap = "2.32.0" exit-future = "0.1.3" futures = "0.1.25" serde = "1.0" diff --git a/beacon_node/websocket_server/src/config.rs b/beacon_node/websocket_server/src/config.rs new file mode 100644 index 000000000..c07f0da83 --- /dev/null +++ b/beacon_node/websocket_server/src/config.rs @@ -0,0 +1,45 @@ +use clap::ArgMatches; +use serde::{Deserialize, Serialize}; +use std::net::Ipv4Addr; + +/// The core configuration of a Lighthouse beacon node. +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Config { + pub enabled: bool, + /// The IPv4 address the REST API HTTP server will listen on. + pub listen_address: Ipv4Addr, + /// The port the REST API HTTP server will listen on. + pub port: u16, +} + +impl Default for Config { + fn default() -> Self { + Config { + enabled: true, + listen_address: Ipv4Addr::new(127, 0, 0, 1), + port: 5053, + } + } +} + +impl Config { + pub fn apply_cli_args(&mut self, args: &ArgMatches) -> Result<(), &'static str> { + if args.is_present("no-ws") { + self.enabled = false; + } + + if let Some(rpc_address) = args.value_of("ws-address") { + self.listen_address = rpc_address + .parse::() + .map_err(|_| "ws-address is not a valid IPv4 address.")?; + } + + if let Some(rpc_port) = args.value_of("ws-port") { + self.port = rpc_port + .parse::() + .map_err(|_| "ws-port is not a valid u16.")?; + } + + Ok(()) + } +} diff --git a/beacon_node/websocket_server/src/lib.rs b/beacon_node/websocket_server/src/lib.rs index 1c7d9ddb9..ad9cabf4a 100644 --- a/beacon_node/websocket_server/src/lib.rs +++ b/beacon_node/websocket_server/src/lib.rs @@ -1,31 +1,13 @@ use beacon_chain::events::{EventHandler, EventKind}; -use serde::{Deserialize, Serialize}; use slog::{error, info, Logger}; use std::marker::PhantomData; -use std::net::Ipv4Addr; use std::thread; use types::EthSpec; use ws::{Sender, WebSocket}; -/// The core configuration of a Lighthouse beacon node. -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct Config { - pub enabled: bool, - /// The IPv4 address the REST API HTTP server will listen on. - pub listen_address: Ipv4Addr, - /// The port the REST API HTTP server will listen on. - pub port: u16, -} +mod config; -impl Default for Config { - fn default() -> Self { - Config { - enabled: true, - listen_address: Ipv4Addr::new(127, 0, 0, 1), - port: 5053, - } - } -} +pub use config::Config; pub struct WebSocketSender { sender: Option,