diff --git a/client/cli/src/commands/mod.rs b/client/cli/src/commands/mod.rs index a4d5c8ca7f20b..96c8f0451cfdf 100644 --- a/client/cli/src/commands/mod.rs +++ b/client/cli/src/commands/mod.rs @@ -285,15 +285,29 @@ macro_rules! substrate_cli_subcommands { } } - fn rpc_http(&self) -> $crate::Result<::std::option::Option<::std::net::SocketAddr>> { + fn rpc_http(&self, default_port: u16) + -> $crate::Result> { match self { - $($enum::$variant(cmd) => cmd.rpc_http()),* + $($enum::$variant(cmd) => cmd.rpc_http(default_port)),* } } - fn rpc_ws(&self) -> $crate::Result<::std::option::Option<::std::net::SocketAddr>> { + fn default_rpc_http_port(&self) -> $crate::Result { match self { - $($enum::$variant(cmd) => cmd.rpc_ws()),* + $($enum::$variant(cmd) => cmd.default_rpc_http_port()),* + } + } + + fn rpc_ws(&self, default_port: u16) + -> $crate::Result> { + match self { + $($enum::$variant(cmd) => cmd.rpc_ws(default_port)),* + } + } + + fn default_rpc_ws_port(&self) -> $crate::Result { + match self { + $($enum::$variant(cmd) => cmd.default_rpc_ws_port()),* } } @@ -316,10 +330,16 @@ macro_rules! substrate_cli_subcommands { } } - fn prometheus_config(&self) + fn prometheus_config(&self, default_port: u16) -> $crate::Result<::std::option::Option<::sc_service::config::PrometheusConfig>> { match self { - $($enum::$variant(cmd) => cmd.prometheus_config()),* + $($enum::$variant(cmd) => cmd.prometheus_config(default_port)),* + } + } + + fn default_prometheus_port(&self) -> $crate::Result { + match self { + $($enum::$variant(cmd) => cmd.default_prometheus_port()),* } } diff --git a/client/cli/src/commands/run_cmd.rs b/client/cli/src/commands/run_cmd.rs index 82d40e6a73f07..fce65e935f76b 100644 --- a/client/cli/src/commands/run_cmd.rs +++ b/client/cli/src/commands/run_cmd.rs @@ -378,7 +378,7 @@ impl CliConfiguration for RunCmd { Ok(self.shared_params.dev || self.force_authoring) } - fn prometheus_config(&self) -> Result> { + fn prometheus_config(&self, default_port: u16) -> Result> { Ok(if self.no_prometheus { None } else { @@ -389,7 +389,7 @@ impl CliConfiguration for RunCmd { }; Some(PrometheusConfig::new_with_default_registry( - SocketAddr::new(interface.into(), self.prometheus_port.unwrap_or(9615)) + SocketAddr::new(interface.into(), self.prometheus_port.unwrap_or(default_port)) )) }) } @@ -423,7 +423,7 @@ impl CliConfiguration for RunCmd { .into()) } - fn rpc_http(&self) -> Result> { + fn rpc_http(&self, default_port: u16) -> Result> { let interface = rpc_interface( self.rpc_external, self.unsafe_rpc_external, @@ -431,10 +431,10 @@ impl CliConfiguration for RunCmd { self.validator )?; - Ok(Some(SocketAddr::new(interface, self.rpc_port.unwrap_or(9933)))) + Ok(Some(SocketAddr::new(interface, self.rpc_port.unwrap_or(default_port)))) } - fn rpc_ws(&self) -> Result> { + fn rpc_ws(&self, default_port: u16) -> Result> { let interface = rpc_interface( self.ws_external, self.unsafe_ws_external, @@ -442,7 +442,7 @@ impl CliConfiguration for RunCmd { self.validator )?; - Ok(Some(SocketAddr::new(interface, self.ws_port.unwrap_or(9944)))) + Ok(Some(SocketAddr::new(interface, self.ws_port.unwrap_or(default_port)))) } fn rpc_methods(&self) -> Result { diff --git a/client/cli/src/config.rs b/client/cli/src/config.rs index 1374e75daf9c3..d0b00641b080c 100644 --- a/client/cli/src/config.rs +++ b/client/cli/src/config.rs @@ -255,17 +255,31 @@ pub trait CliConfiguration: Sized { /// Get the RPC HTTP address (`None` if disabled). /// /// By default this is `None`. - fn rpc_http(&self) -> Result> { + fn rpc_http(&self, _default_port: u16) -> Result> { Ok(Default::default()) } + /// Get the default RPC HTTP port. + /// + /// By default this is 9933. + fn default_rpc_http_port(&self) -> Result { + Ok(9933) + } + /// Get the RPC websocket address (`None` if disabled). /// /// By default this is `None`. - fn rpc_ws(&self) -> Result> { + fn rpc_ws(&self, _default_port: u16) -> Result> { Ok(Default::default()) } + /// Get the default RPC websocket port. + /// + /// By default this is 9944. + fn default_rpc_ws_port(&self) -> Result { + Ok(9944) + } + /// Returns the RPC method set to expose. /// /// By default this is `RpcMethods::Auto` (unsafe RPCs are denied iff @@ -291,10 +305,17 @@ pub trait CliConfiguration: Sized { /// Get the prometheus configuration (`None` if disabled) /// /// By default this is `None`. - fn prometheus_config(&self) -> Result> { + fn prometheus_config(&self, _default_port: u16) -> Result> { Ok(Default::default()) } + /// Get the default prometheus port + /// + /// By default this 9615. + fn default_prometheus_port(&self) -> Result { + Ok(9615) + } + /// Get the telemetry endpoints (if any) /// /// By default this is retrieved from the chain spec loaded by `load_spec`. @@ -443,12 +464,12 @@ pub trait CliConfiguration: Sized { pruning: self.pruning(unsafe_pruning, &role)?, wasm_method: self.wasm_method()?, execution_strategies: self.execution_strategies(is_dev)?, - rpc_http: self.rpc_http()?, - rpc_ws: self.rpc_ws()?, + rpc_http: self.rpc_http(self.default_rpc_http_port()?)?, + rpc_ws: self.rpc_ws(self.default_rpc_ws_port()?)?, rpc_methods: self.rpc_methods()?, rpc_ws_max_connections: self.rpc_ws_max_connections()?, rpc_cors: self.rpc_cors(is_dev)?, - prometheus_config: self.prometheus_config()?, + prometheus_config: self.prometheus_config(self.default_prometheus_port()?)?, telemetry_endpoints: self.telemetry_endpoints(&chain_spec)?, telemetry_external_transport: self.telemetry_external_transport()?, default_heap_pages: self.default_heap_pages()?, diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 6f558398813a2..1bb6bdea30242 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -6,6 +6,11 @@ The format is based on [Keep a Changelog]. ## Unreleased +Client +------ + +* Add new methods to set defaults ports for RPC, RPC WebSocket and Prometheus (#6250) + ## 2.0.0-rc2 -> 2.0.0-rc3 Runtime