From 75a656165a2f977edafc4238d9d891c1644211d7 Mon Sep 17 00:00:00 2001 From: Niklas Adolfsson Date: Sat, 6 May 2023 13:24:44 +0200 Subject: [PATCH] fix cli: make port Option again --- bin/node/cli/benches/block_production.rs | 1 + bin/node/cli/benches/transaction_pool.rs | 1 + client/cli/src/commands/run_cmd.rs | 18 ++++++++++-------- client/cli/src/config.rs | 24 ++++++++++++++++++------ client/cli/src/runner.rs | 1 + client/service/src/config.rs | 2 ++ client/service/src/lib.rs | 2 +- client/service/test/src/lib.rs | 1 + 8 files changed, 35 insertions(+), 15 deletions(-) diff --git a/bin/node/cli/benches/block_production.rs b/bin/node/cli/benches/block_production.rs index fd0bbb71dd408..527b145c62c46 100644 --- a/bin/node/cli/benches/block_production.rs +++ b/bin/node/cli/benches/block_production.rs @@ -92,6 +92,7 @@ fn new_node(tokio_handle: Handle) -> node_cli::service::NewFullBase { rpc_max_response_size: Default::default(), rpc_id_provider: Default::default(), rpc_max_subs_per_conn: Default::default(), + rpc_port: 9944, prometheus_config: None, telemetry_endpoints: None, default_heap_pages: None, diff --git a/bin/node/cli/benches/transaction_pool.rs b/bin/node/cli/benches/transaction_pool.rs index dc00255308741..44ebe1e7d4fe6 100644 --- a/bin/node/cli/benches/transaction_pool.rs +++ b/bin/node/cli/benches/transaction_pool.rs @@ -86,6 +86,7 @@ fn new_node(tokio_handle: Handle) -> node_cli::service::NewFullBase { rpc_max_response_size: Default::default(), rpc_id_provider: Default::default(), rpc_max_subs_per_conn: Default::default(), + rpc_port: 9944, prometheus_config: None, telemetry_endpoints: None, default_heap_pages: None, diff --git a/client/cli/src/commands/run_cmd.rs b/client/cli/src/commands/run_cmd.rs index 8176643d73cd2..975f7638191ea 100644 --- a/client/cli/src/commands/run_cmd.rs +++ b/client/cli/src/commands/run_cmd.rs @@ -24,6 +24,8 @@ use crate::{ TransactionPoolParams, }, CliConfiguration, PrometheusParams, RuntimeParams, TelemetryParams, + RPC_DEFAULT_MAX_CONNECTIONS, RPC_DEFAULT_MAX_REQUEST_SIZE_MB, RPC_DEFAULT_MAX_RESPONSE_SIZE_MB, + RPC_DEFAULT_MAX_SUBS_PER_CONN, }; use clap::Parser; use regex::Regex; @@ -78,23 +80,23 @@ pub struct RunCmd { pub rpc_methods: RpcMethods, /// Set the the maximum RPC request payload size for both HTTP and WS in megabytes. - #[arg(long, default_value_t = 15)] + #[arg(long, default_value_t = RPC_DEFAULT_MAX_REQUEST_SIZE_MB)] pub rpc_max_request_size: u32, /// Set the the maximum RPC response payload size for both HTTP and WS in megabytes. - #[arg(long, default_value_t = 15)] + #[arg(long, default_value_t = RPC_DEFAULT_MAX_RESPONSE_SIZE_MB)] pub rpc_max_response_size: u32, /// Set the the maximum concurrent subscriptions per connection. - #[arg(long, default_value_t = 1024)] + #[arg(long, default_value_t = RPC_DEFAULT_MAX_SUBS_PER_CONN)] pub rpc_max_subscriptions_per_connection: u32, /// Specify JSON-RPC server TCP port. - #[arg(long, value_name = "PORT", default_value_t = 9944)] - pub rpc_port: u16, + #[arg(long, value_name = "PORT")] + pub rpc_port: Option, /// Maximum number of RPC server connections. - #[arg(long, value_name = "COUNT", default_value_t = 100)] + #[arg(long, value_name = "COUNT", default_value_t = RPC_DEFAULT_MAX_CONNECTIONS)] pub rpc_max_connections: u32, /// Specify browser Origins allowed to access the HTTP & WS RPC servers. @@ -336,7 +338,7 @@ impl CliConfiguration for RunCmd { .into()) } - fn rpc_addr(&self, _default_listen_port: u16) -> Result> { + fn rpc_addr(&self, default_listen_port: u16) -> Result> { let interface = rpc_interface( self.rpc_external, self.unsafe_rpc_external, @@ -344,7 +346,7 @@ impl CliConfiguration for RunCmd { self.validator, )?; - Ok(Some(SocketAddr::new(interface, self.rpc_port))) + Ok(Some(SocketAddr::new(interface, self.rpc_port.unwrap_or(default_listen_port)))) } fn rpc_methods(&self) -> Result { diff --git a/client/cli/src/config.rs b/client/cli/src/config.rs index 01f6adbba22fe..04c62a73b40ed 100644 --- a/client/cli/src/config.rs +++ b/client/cli/src/config.rs @@ -45,6 +45,17 @@ pub(crate) const DEFAULT_NETWORK_CONFIG_PATH: &str = "network"; /// The recommended open file descriptor limit to be configured for the process. const RECOMMENDED_OPEN_FILE_DESCRIPTOR_LIMIT: u64 = 10_000; +/// The default port. +pub const RPC_DEFAULT_PORT: u16 = 9944; +/// The default max number of subscriptions per connection. +pub const RPC_DEFAULT_MAX_SUBS_PER_CONN: u32 = 1024; +/// The default max request size in MB. +pub const RPC_DEFAULT_MAX_REQUEST_SIZE_MB: u32 = 15; +/// The default max response size in MB. +pub const RPC_DEFAULT_MAX_RESPONSE_SIZE_MB: u32 = 15; +/// The default number of connection.. +pub const RPC_DEFAULT_MAX_CONNECTIONS: u32 = 100; + /// Default configuration values used by Substrate /// /// These values will be used by [`CliConfiguration`] to set @@ -61,7 +72,7 @@ pub trait DefaultConfigurationValues { /// /// By default this is `9944`. fn rpc_listen_port() -> u16 { - 9944 + RPC_DEFAULT_PORT } /// The port Substrate should listen on for prometheus connections. @@ -303,14 +314,14 @@ pub trait CliConfiguration: Sized { /// Returns the RPC method set to expose. /// /// By default this is `RpcMethods::Auto` (unsafe RPCs are denied iff - /// `{rpc,ws}_external` returns true, respectively). + /// `rpc_external` returns true, respectively). fn rpc_methods(&self) -> Result { Ok(Default::default()) } /// Get the maximum number of RPC server connections. fn rpc_max_connections(&self) -> Result { - Ok(Default::default()) + Ok(RPC_DEFAULT_MAX_CONNECTIONS) } /// Get the RPC cors (`None` if disabled) @@ -322,17 +333,17 @@ pub trait CliConfiguration: Sized { /// Get maximum RPC request payload size. fn rpc_max_request_size(&self) -> Result { - Ok(Default::default()) + Ok(RPC_DEFAULT_MAX_REQUEST_SIZE_MB) } /// Get maximum RPC response payload size. fn rpc_max_response_size(&self) -> Result { - Ok(Default::default()) + Ok(RPC_DEFAULT_MAX_RESPONSE_SIZE_MB) } /// Get maximum number of subscriptions per connection. fn rpc_max_subscriptions_per_connection(&self) -> Result { - Ok(Default::default()) + Ok(RPC_DEFAULT_MAX_SUBS_PER_CONN) } /// Get the prometheus configuration (`None` if disabled) @@ -506,6 +517,7 @@ pub trait CliConfiguration: Sized { rpc_max_response_size: self.rpc_max_response_size()?, rpc_id_provider: None, rpc_max_subs_per_conn: self.rpc_max_subscriptions_per_connection()?, + rpc_port: DCV::rpc_listen_port(), prometheus_config: self .prometheus_config(DCV::prometheus_listen_port(), &chain_spec)?, telemetry_endpoints, diff --git a/client/cli/src/runner.rs b/client/cli/src/runner.rs index 1fa12e2a0eafe..7b534b37192ad 100644 --- a/client/cli/src/runner.rs +++ b/client/cli/src/runner.rs @@ -296,6 +296,7 @@ mod tests { rpc_max_response_size: Default::default(), rpc_id_provider: Default::default(), rpc_max_subs_per_conn: Default::default(), + rpc_port: 9944, prometheus_config: None, telemetry_endpoints: None, default_heap_pages: None, diff --git a/client/service/src/config.rs b/client/service/src/config.rs index 0ff2c96d848bf..c0fb2dc9c4c70 100644 --- a/client/service/src/config.rs +++ b/client/service/src/config.rs @@ -101,6 +101,8 @@ pub struct Configuration { pub rpc_id_provider: Option>, /// Maximum allowed subscriptions per rpc connection pub rpc_max_subs_per_conn: u32, + /// JSON-RPC server default port. + pub rpc_port: u16, /// Prometheus endpoint configuration. `None` if disabled. pub prometheus_config: Option, /// Telemetry service URL. `None` if disabled. diff --git a/client/service/src/lib.rs b/client/service/src/lib.rs index b90eb71c1ea75..e658c7f7d9c71 100644 --- a/client/service/src/lib.rs +++ b/client/service/src/lib.rs @@ -386,7 +386,7 @@ where addr }; - let addr = config.rpc_addr.unwrap_or(([127, 0, 0, 1], 9944).into()); + let addr = config.rpc_addr.unwrap_or_else(|| ([127, 0, 0, 1], config.rpc_port).into()); let backup_addr = backup_port(addr); let metrics = sc_rpc_server::RpcMetrics::new(config.prometheus_registry())?; diff --git a/client/service/test/src/lib.rs b/client/service/test/src/lib.rs index db8432e44c4be..11c672db8cb90 100644 --- a/client/service/test/src/lib.rs +++ b/client/service/test/src/lib.rs @@ -254,6 +254,7 @@ fn node_config< rpc_max_response_size: Default::default(), rpc_id_provider: Default::default(), rpc_max_subs_per_conn: Default::default(), + rpc_port: 9944, prometheus_config: None, telemetry_endpoints: None, default_heap_pages: None,