Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Closed
Prev Previous commit
Next Next commit
Move default ports to CliConfiguration
This allows overriding the ports without having to rewrite the whole logic.
  • Loading branch information
cecton committed Jun 4, 2020
commit 1ef6fa83e987a4d763f5f2bede1376570631c623
32 changes: 26 additions & 6 deletions client/cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<::std::option::Option<::std::net::SocketAddr>> {
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<::std::option::Option<u16>> {
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<::std::option::Option<::std::net::SocketAddr>> {
match self {
$($enum::$variant(cmd) => cmd.rpc_ws(default_port)),*
}
}

fn default_rpc_ws_port(&self) -> $crate::Result<::std::option::Option<u16>> {
match self {
$($enum::$variant(cmd) => cmd.default_rpc_ws_port()),*
}
}

Expand All @@ -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<::std::option::Option<u16>> {
match self {
$($enum::$variant(cmd) => cmd.default_prometheus_port()),*
}
}

Expand Down
12 changes: 6 additions & 6 deletions client/cli/src/commands/run_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ impl CliConfiguration for RunCmd {
Ok(self.shared_params.dev || self.force_authoring)
}

fn prometheus_config(&self) -> Result<Option<PrometheusConfig>> {
fn prometheus_config(&self, default_port: u16) -> Result<Option<PrometheusConfig>> {
Ok(if self.no_prometheus {
None
} else {
Expand All @@ -379,7 +379,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))
))
})
}
Expand Down Expand Up @@ -413,26 +413,26 @@ impl CliConfiguration for RunCmd {
.into())
}

fn rpc_http(&self) -> Result<Option<SocketAddr>> {
fn rpc_http(&self, default_port: u16) -> Result<Option<SocketAddr>> {
let interface = rpc_interface(
self.rpc_external,
self.unsafe_rpc_external,
self.rpc_methods,
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<Option<SocketAddr>> {
fn rpc_ws(&self, default_port: u16) -> Result<Option<SocketAddr>> {
let interface = rpc_interface(
self.ws_external,
self.unsafe_ws_external,
self.rpc_methods,
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<sc_service::config::RpcMethods> {
Expand Down
35 changes: 29 additions & 6 deletions client/cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,14 +255,28 @@ pub trait CliConfiguration: Sized {
/// Get the RPC HTTP address (`None` if disabled).
///
/// By default this is `None`.
fn rpc_http(&self) -> Result<Option<SocketAddr>> {
fn rpc_http(&self, _default_port: u16) -> Result<Option<SocketAddr>> {
Ok(Default::default())
}

/// Get the default RPC HTTP port.
///
/// By default this is `None` which will result 9933.
fn default_rpc_http_port(&self) -> Result<Option<u16>> {
Ok(Default::default())
}

/// Get the RPC websocket address (`None` if disabled).
///
/// By default this is `None`.
fn rpc_ws(&self) -> Result<Option<SocketAddr>> {
fn rpc_ws(&self, _default_port: u16) -> Result<Option<SocketAddr>> {
Ok(Default::default())
}

/// Get the default RPC websocket port.
///
/// By default this is `None` which will result to 9944.
fn default_rpc_ws_port(&self) -> Result<Option<u16>> {
Ok(Default::default())
}

Expand Down Expand Up @@ -291,7 +305,14 @@ pub trait CliConfiguration: Sized {
/// Get the prometheus configuration (`None` if disabled)
///
/// By default this is `None`.
fn prometheus_config(&self) -> Result<Option<PrometheusConfig>> {
fn prometheus_config(&self, _default_port: u16) -> Result<Option<PrometheusConfig>> {
Ok(Default::default())
}

/// Get the default prometheus port
///
/// By default this is `None` which will result to 9615.
fn default_prometheus_port(&self) -> Result<Option<u16>> {
Ok(Default::default())
}

Expand Down Expand Up @@ -445,12 +466,14 @@ 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()?.unwrap_or(9933))?,
rpc_ws: self.rpc_ws(self.default_rpc_ws_port()?.unwrap_or(9944))?,
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()?.unwrap_or(9615),
)?,
telemetry_endpoints: self.telemetry_endpoints(&chain_spec)?,
telemetry_external_transport: self.telemetry_external_transport()?,
default_heap_pages: self.default_heap_pages()?,
Expand Down