From c2838d6c3f8fa02f323cd27cec2ddb96b57b5fbf Mon Sep 17 00:00:00 2001 From: yjhmelody Date: Tue, 28 Mar 2023 17:09:08 +0800 Subject: [PATCH 1/9] refactor: extract TelemetryParams and PrometheusParams --- client/cli/src/commands/run_cmd.rs | 100 ++++----------------- client/cli/src/params/mod.rs | 4 +- client/cli/src/params/prometheus_params.rs | 45 ++++++++++ client/cli/src/params/telemetry_params.rs | 51 +++++++++++ 4 files changed, 114 insertions(+), 86 deletions(-) create mode 100644 client/cli/src/params/prometheus_params.rs create mode 100644 client/cli/src/params/telemetry_params.rs diff --git a/client/cli/src/commands/run_cmd.rs b/client/cli/src/commands/run_cmd.rs index 9441acecc4dfc..0be3b44b55ed2 100644 --- a/client/cli/src/commands/run_cmd.rs +++ b/client/cli/src/commands/run_cmd.rs @@ -16,15 +16,10 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use crate::{ - arg_enums::RpcMethods, - error::{Error, Result}, - params::{ - ImportParams, KeystoreParams, NetworkParams, OffchainWorkerParams, SharedParams, - TransactionPoolParams, - }, - CliConfiguration, -}; +use crate::{arg_enums::RpcMethods, error::{Error, Result}, params::{ + ImportParams, KeystoreParams, NetworkParams, OffchainWorkerParams, SharedParams, + TransactionPoolParams, +}, CliConfiguration, PrometheusParams, TelemetryParams}; use clap::Parser; use regex::Regex; use sc_service::{ @@ -116,12 +111,6 @@ pub struct RunCmd { #[arg(long)] pub rpc_max_subscriptions_per_connection: Option, - /// Expose Prometheus exporter on all interfaces. - /// - /// Default is local. - #[arg(long)] - pub prometheus_external: bool, - /// DEPRECATED, IPC support has been removed. #[arg(long, value_name = "PATH")] pub ipc_path: Option, @@ -151,36 +140,19 @@ pub struct RunCmd { #[arg(long, value_name = "ORIGINS", value_parser = parse_cors)] pub rpc_cors: Option, - /// Specify Prometheus exporter TCP Port. - #[arg(long, value_name = "PORT")] - pub prometheus_port: Option, - - /// Do not expose a Prometheus exporter endpoint. - /// - /// Prometheus metric endpoint is enabled by default. - #[arg(long)] - pub no_prometheus: bool, - /// The human-readable name for this node. /// /// The node name will be reported to the telemetry server, if enabled. #[arg(long, value_name = "NAME")] pub name: Option, - /// Disable connecting to the Substrate telemetry server. - /// - /// Telemetry is on by default on global chains. - #[arg(long)] - pub no_telemetry: bool, + #[allow(missing_docs)] + #[clap(flatten)] + pub telemetry_params: TelemetryParams, - /// The URL of the telemetry server to connect to. - /// - /// This flag can be passed multiple times as a means to specify multiple - /// telemetry endpoints. Verbosity levels range from 0-9, with 0 denoting - /// the least verbosity. - /// Expected format is 'URL VERBOSITY', e.g. `--telemetry-url 'wss://foo/bar 0'`. - #[arg(long = "telemetry-url", value_name = "URL VERBOSITY", value_parser = parse_telemetry_endpoints)] - pub telemetry_endpoints: Vec<(String, u8)>, + #[allow(missing_docs)] + #[clap(flatten)] + pub prometheus_params: PrometheusParams, #[allow(missing_docs)] #[clap(flatten)] @@ -345,11 +317,12 @@ impl CliConfiguration for RunCmd { &self, chain_spec: &Box, ) -> Result> { - Ok(if self.no_telemetry { + let params = &self.telemetry_params; + Ok(if params.no_telemetry { None - } else if !self.telemetry_endpoints.is_empty() { + } else if !params.telemetry_endpoints.is_empty() { Some( - TelemetryEndpoints::new(self.telemetry_endpoints.clone()) + TelemetryEndpoints::new(params.telemetry_endpoints.clone()) .map_err(|e| e.to_string())?, ) } else { @@ -374,20 +347,7 @@ impl CliConfiguration for RunCmd { default_listen_port: u16, chain_spec: &Box, ) -> Result> { - Ok(if self.no_prometheus { - None - } else { - let interface = - if self.prometheus_external { Ipv4Addr::UNSPECIFIED } else { Ipv4Addr::LOCALHOST }; - - Some(PrometheusConfig::new_with_default_registry( - SocketAddr::new( - interface.into(), - self.prometheus_port.unwrap_or(default_listen_port), - ), - chain_spec.id().into(), - )) - }) + Ok(self.prometheus_params.prometheus_config(default_listen_port, chain_spec.id().to_string())) } fn disable_grandpa(&self) -> Result { @@ -546,36 +506,6 @@ fn rpc_interface( } } -#[derive(Debug)] -enum TelemetryParsingError { - MissingVerbosity, - VerbosityParsingError(std::num::ParseIntError), -} - -impl std::error::Error for TelemetryParsingError {} - -impl std::fmt::Display for TelemetryParsingError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - TelemetryParsingError::MissingVerbosity => write!(f, "Verbosity level missing"), - TelemetryParsingError::VerbosityParsingError(e) => write!(f, "{}", e), - } - } -} - -fn parse_telemetry_endpoints(s: &str) -> std::result::Result<(String, u8), TelemetryParsingError> { - let pos = s.find(' '); - match pos { - None => Err(TelemetryParsingError::MissingVerbosity), - Some(pos_) => { - let url = s[..pos_].to_string(); - let verbosity = - s[pos_ + 1..].parse().map_err(TelemetryParsingError::VerbosityParsingError)?; - Ok((url, verbosity)) - }, - } -} - /// CORS setting /// /// The type is introduced to overcome `Option>` handling of `clap`. diff --git a/client/cli/src/params/mod.rs b/client/cli/src/params/mod.rs index f9c840d2d4865..e0cd0313e58b1 100644 --- a/client/cli/src/params/mod.rs +++ b/client/cli/src/params/mod.rs @@ -25,6 +25,8 @@ mod offchain_worker_params; mod pruning_params; mod shared_params; mod transaction_pool_params; +mod prometheus_params; +mod telemetry_params; use crate::arg_enums::{CryptoScheme, OutputType}; use clap::Args; @@ -38,7 +40,7 @@ use std::{fmt::Debug, str::FromStr}; pub use crate::params::{ database_params::*, import_params::*, keystore_params::*, message_params::*, network_params::*, node_key_params::*, offchain_worker_params::*, pruning_params::*, shared_params::*, - transaction_pool_params::*, + transaction_pool_params::*, prometheus_params::*, telemetry_params::*, }; /// Parse Ss58AddressFormat diff --git a/client/cli/src/params/prometheus_params.rs b/client/cli/src/params/prometheus_params.rs new file mode 100644 index 0000000000000..872a1677a49f9 --- /dev/null +++ b/client/cli/src/params/prometheus_params.rs @@ -0,0 +1,45 @@ +use clap::Args; +use sc_service::config::PrometheusConfig; +use std::net::{Ipv4Addr, SocketAddr}; + +/// Parameters used to config prometheus. +#[derive(Debug, Clone, Args)] +pub struct PrometheusParams { + /// Specify Prometheus exporter TCP Port. + #[arg(long, value_name = "PORT")] + pub prometheus_port: Option, + /// Expose Prometheus exporter on all interfaces. + /// + /// Default is local. + #[arg(long)] + pub prometheus_external: bool, + /// Do not expose a Prometheus exporter endpoint. + /// + /// Prometheus metric endpoint is enabled by default. + #[arg(long)] + pub no_prometheus: bool, +} + +impl PrometheusParams { + /// Creates configuration about Prometheus. + pub fn prometheus_config( + &self, + default_listen_port: u16, + chain_id: String, + ) -> Option { + if self.no_prometheus { + None + } else { + let interface = + if self.prometheus_external { Ipv4Addr::UNSPECIFIED } else { Ipv4Addr::LOCALHOST }; + + Some(PrometheusConfig::new_with_default_registry( + SocketAddr::new( + interface.into(), + self.prometheus_port.unwrap_or(default_listen_port), + ), + chain_id, + )) + } + } +} diff --git a/client/cli/src/params/telemetry_params.rs b/client/cli/src/params/telemetry_params.rs new file mode 100644 index 0000000000000..515a289560fb6 --- /dev/null +++ b/client/cli/src/params/telemetry_params.rs @@ -0,0 +1,51 @@ +use clap::Args; + +/// Parameters used to config telemetry. +#[derive(Debug, Clone, Args)] +pub struct TelemetryParams { + /// Disable connecting to the Substrate telemetry server. + /// + /// Telemetry is on by default on global chains. + #[arg(long)] + pub no_telemetry: bool, + + /// The URL of the telemetry server to connect to. + /// + /// This flag can be passed multiple times as a means to specify multiple + /// telemetry endpoints. Verbosity levels range from 0-9, with 0 denoting + /// the least verbosity. + /// Expected format is 'URL VERBOSITY', e.g. `--telemetry-url 'wss://foo/bar 0'`. + #[arg(long = "telemetry-url", value_name = "URL VERBOSITY", value_parser = parse_telemetry_endpoints)] + pub telemetry_endpoints: Vec<(String, u8)>, +} + +#[derive(Debug)] +enum TelemetryParsingError { + MissingVerbosity, + VerbosityParsingError(std::num::ParseIntError), +} + +impl std::error::Error for TelemetryParsingError {} + +impl std::fmt::Display for TelemetryParsingError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + TelemetryParsingError::MissingVerbosity => write!(f, "Verbosity level missing"), + TelemetryParsingError::VerbosityParsingError(e) => write!(f, "{}", e), + } + } +} + + +fn parse_telemetry_endpoints(s: &str) -> Result<(String, u8), TelemetryParsingError> { + let pos = s.find(' '); + match pos { + None => Err(TelemetryParsingError::MissingVerbosity), + Some(pos_) => { + let url = s[..pos_].to_string(); + let verbosity = + s[pos_ + 1..].parse().map_err(TelemetryParsingError::VerbosityParsingError)?; + Ok((url, verbosity)) + }, + } +} From 4c4d4a7f2125be6d2430b714e3e5fb8967fc35e7 Mon Sep 17 00:00:00 2001 From: yjhmelody Date: Tue, 28 Mar 2023 17:44:01 +0800 Subject: [PATCH 2/9] improve run_cmd docs --- client/cli/src/commands/run_cmd.rs | 12 ++++++------ client/cli/src/params/prometheus_params.rs | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/client/cli/src/commands/run_cmd.rs b/client/cli/src/commands/run_cmd.rs index 0be3b44b55ed2..6f2a3ee81e98a 100644 --- a/client/cli/src/commands/run_cmd.rs +++ b/client/cli/src/commands/run_cmd.rs @@ -142,7 +142,7 @@ pub struct RunCmd { /// The human-readable name for this node. /// - /// The node name will be reported to the telemetry server, if enabled. + /// It's used as network node name. #[arg(long, value_name = "NAME")] pub name: Option, @@ -174,6 +174,10 @@ pub struct RunCmd { #[clap(flatten)] pub pool_config: TransactionPoolParams, + #[allow(missing_docs)] + #[clap(flatten)] + pub keystore_params: KeystoreParams, + /// Shortcut for `--name Alice --validator` with session keys for `Alice` added to keystore. #[arg(long, conflicts_with_all = &["bob", "charlie", "dave", "eve", "ferdie", "one", "two"])] pub alice: bool, @@ -211,10 +215,6 @@ pub struct RunCmd { #[arg(long)] pub force_authoring: bool, - #[allow(missing_docs)] - #[clap(flatten)] - pub keystore_params: KeystoreParams, - /// The size of the instances cache for each runtime. /// /// The default value is 8 and the values higher than 256 are ignored. @@ -334,7 +334,7 @@ impl CliConfiguration for RunCmd { let keyring = self.get_keyring(); let is_authority = self.validator || is_dev || keyring.is_some(); - Ok(if is_authority { sc_service::Role::Authority } else { sc_service::Role::Full }) + Ok(if is_authority { Role::Authority } else { Role::Full }) } fn force_authoring(&self) -> Result { diff --git a/client/cli/src/params/prometheus_params.rs b/client/cli/src/params/prometheus_params.rs index 872a1677a49f9..9c5e1c5df7d1a 100644 --- a/client/cli/src/params/prometheus_params.rs +++ b/client/cli/src/params/prometheus_params.rs @@ -21,7 +21,7 @@ pub struct PrometheusParams { } impl PrometheusParams { - /// Creates configuration about Prometheus. + /// Creates [`PrometheusConfig`]. pub fn prometheus_config( &self, default_listen_port: u16, From f5d8774005ce38c2651de5b677c0eb3202b87da8 Mon Sep 17 00:00:00 2001 From: yjhmelody Date: Tue, 11 Apr 2023 11:46:40 +0800 Subject: [PATCH 3/9] extract `RuntimeParams` --- client/cli/src/commands/run_cmd.rs | 35 ++++++++++++----------- client/cli/src/params/mod.rs | 9 +++--- client/cli/src/params/runtime_params.rs | 22 ++++++++++++++ client/cli/src/params/telemetry_params.rs | 1 - 4 files changed, 45 insertions(+), 22 deletions(-) create mode 100644 client/cli/src/params/runtime_params.rs diff --git a/client/cli/src/commands/run_cmd.rs b/client/cli/src/commands/run_cmd.rs index 6f2a3ee81e98a..3e4f42d7cdc9c 100644 --- a/client/cli/src/commands/run_cmd.rs +++ b/client/cli/src/commands/run_cmd.rs @@ -16,10 +16,15 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use crate::{arg_enums::RpcMethods, error::{Error, Result}, params::{ - ImportParams, KeystoreParams, NetworkParams, OffchainWorkerParams, SharedParams, - TransactionPoolParams, -}, CliConfiguration, PrometheusParams, TelemetryParams}; +use crate::{ + arg_enums::RpcMethods, + error::{Error, Result}, + params::{ + ImportParams, KeystoreParams, NetworkParams, OffchainWorkerParams, SharedParams, + TransactionPoolParams, + }, + CliConfiguration, PrometheusParams, RuntimeParams, TelemetryParams, +}; use clap::Parser; use regex::Regex; use sc_service::{ @@ -154,6 +159,10 @@ pub struct RunCmd { #[clap(flatten)] pub prometheus_params: PrometheusParams, + #[allow(missing_docs)] + #[clap(flatten)] + pub runtime_params: RuntimeParams, + #[allow(missing_docs)] #[clap(flatten)] pub offchain_worker_params: OffchainWorkerParams, @@ -215,16 +224,6 @@ pub struct RunCmd { #[arg(long)] pub force_authoring: bool, - /// The size of the instances cache for each runtime. - /// - /// The default value is 8 and the values higher than 256 are ignored. - #[arg(long)] - pub max_runtime_instances: Option, - - /// Maximum number of different runtimes that can be cached. - #[arg(long, default_value_t = 2)] - pub runtime_cache_size: u8, - /// Run a temporary node. /// /// A temporary directory will be created to store the configuration and will be deleted @@ -347,7 +346,9 @@ impl CliConfiguration for RunCmd { default_listen_port: u16, chain_spec: &Box, ) -> Result> { - Ok(self.prometheus_params.prometheus_config(default_listen_port, chain_spec.id().to_string())) + Ok(self + .prometheus_params + .prometheus_config(default_listen_port, chain_spec.id().to_string())) } fn disable_grandpa(&self) -> Result { @@ -434,11 +435,11 @@ impl CliConfiguration for RunCmd { } fn max_runtime_instances(&self) -> Result> { - Ok(self.max_runtime_instances.map(|x| x.min(256))) + Ok(Some(self.runtime_params.max_runtime_instances.min(256))) } fn runtime_cache_size(&self) -> Result { - Ok(self.runtime_cache_size) + Ok(self.runtime_params.runtime_cache_size) } fn base_path(&self) -> Result> { diff --git a/client/cli/src/params/mod.rs b/client/cli/src/params/mod.rs index e0cd0313e58b1..247ffc0e04ba5 100644 --- a/client/cli/src/params/mod.rs +++ b/client/cli/src/params/mod.rs @@ -22,11 +22,12 @@ mod message_params; mod network_params; mod node_key_params; mod offchain_worker_params; +mod prometheus_params; mod pruning_params; +mod runtime_params; mod shared_params; -mod transaction_pool_params; -mod prometheus_params; mod telemetry_params; +mod transaction_pool_params; use crate::arg_enums::{CryptoScheme, OutputType}; use clap::Args; @@ -39,8 +40,8 @@ use std::{fmt::Debug, str::FromStr}; pub use crate::params::{ database_params::*, import_params::*, keystore_params::*, message_params::*, network_params::*, - node_key_params::*, offchain_worker_params::*, pruning_params::*, shared_params::*, - transaction_pool_params::*, prometheus_params::*, telemetry_params::*, + node_key_params::*, offchain_worker_params::*, prometheus_params::*, pruning_params::*, + runtime_params::*, shared_params::*, telemetry_params::*, transaction_pool_params::*, }; /// Parse Ss58AddressFormat diff --git a/client/cli/src/params/runtime_params.rs b/client/cli/src/params/runtime_params.rs new file mode 100644 index 0000000000000..92ee3fdfdf6d2 --- /dev/null +++ b/client/cli/src/params/runtime_params.rs @@ -0,0 +1,22 @@ +use clap::Args; + +/// Parameters used to config runtime. +#[derive(Debug, Clone, Args)] +pub struct RuntimeParams { + /// The size of the instances cache for each runtime. The values higher than 256 are ignored. + #[arg(long, default_value_t = 8)] + pub max_runtime_instances: usize, + + /// Maximum number of different runtimes that can be cached. + #[arg(long, default_value_t = 2)] + pub runtime_cache_size: u8, +} + +impl RuntimeParams { + /// Normalize the params. + pub fn normalize(&mut self) { + if self.max_runtime_instances > 256 { + self.max_runtime_instances = 8; + } + } +} diff --git a/client/cli/src/params/telemetry_params.rs b/client/cli/src/params/telemetry_params.rs index 515a289560fb6..9d0d9254bbcab 100644 --- a/client/cli/src/params/telemetry_params.rs +++ b/client/cli/src/params/telemetry_params.rs @@ -36,7 +36,6 @@ impl std::fmt::Display for TelemetryParsingError { } } - fn parse_telemetry_endpoints(s: &str) -> Result<(String, u8), TelemetryParsingError> { let pos = s.find(' '); match pos { From 94cd326fd71000ac9dc4ad0cafa8655da74d7f4d Mon Sep 17 00:00:00 2001 From: yjhmelody Date: Tue, 11 Apr 2023 11:52:54 +0800 Subject: [PATCH 4/9] use `normalize` --- client/cli/src/commands/run_cmd.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/client/cli/src/commands/run_cmd.rs b/client/cli/src/commands/run_cmd.rs index 3e4f42d7cdc9c..14f59f39b689c 100644 --- a/client/cli/src/commands/run_cmd.rs +++ b/client/cli/src/commands/run_cmd.rs @@ -435,7 +435,9 @@ impl CliConfiguration for RunCmd { } fn max_runtime_instances(&self) -> Result> { - Ok(Some(self.runtime_params.max_runtime_instances.min(256))) + let mut params = self.runtime_params.clone(); + params.normalize(); + Ok(Some(params.max_runtime_instances)) } fn runtime_cache_size(&self) -> Result { From 9f45d51e55eb711382dca2529130024262b3a5ca Mon Sep 17 00:00:00 2001 From: yjhmelody Date: Tue, 11 Apr 2023 11:56:21 +0800 Subject: [PATCH 5/9] keep params types same style --- client/cli/src/params/keystore_params.rs | 2 +- client/cli/src/params/message_params.rs | 5 ++--- client/cli/src/params/pruning_params.rs | 2 +- client/cli/src/params/shared_params.rs | 2 +- 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/client/cli/src/params/keystore_params.rs b/client/cli/src/params/keystore_params.rs index 8933110c400b7..a2fdd6b2218c4 100644 --- a/client/cli/src/params/keystore_params.rs +++ b/client/cli/src/params/keystore_params.rs @@ -61,7 +61,7 @@ pub struct KeystoreParams { pub password_filename: Option, } -/// Parse a sercret string, returning a displayable error. +/// Parse a secret string, returning a displayable error. pub fn secret_string_from_str(s: &str) -> std::result::Result { std::str::FromStr::from_str(s).map_err(|_| "Could not get SecretString".to_string()) } diff --git a/client/cli/src/params/message_params.rs b/client/cli/src/params/message_params.rs index a935bbb25bddd..3fcb6f2c6e8cc 100644 --- a/client/cli/src/params/message_params.rs +++ b/client/cli/src/params/message_params.rs @@ -20,14 +20,13 @@ use crate::error::Error; use array_bytes::{hex2bytes, hex_bytes2hex_str}; -use clap::Parser; +use clap::Args; use std::io::BufRead; /// Params to configure how a message should be passed into a command. -#[derive(Parser, Debug, Clone)] +#[derive(Debug, Clone, Args)] pub struct MessageParams { /// Message to process. Will be read from STDIN otherwise. - /// /// The message is assumed to be raw bytes per default. Use `--hex` for hex input. Can /// optionally be prefixed with `0x` in the hex case. #[arg(long)] diff --git a/client/cli/src/params/pruning_params.rs b/client/cli/src/params/pruning_params.rs index 59eeb13cd0379..757da2dd9cbb5 100644 --- a/client/cli/src/params/pruning_params.rs +++ b/client/cli/src/params/pruning_params.rs @@ -21,7 +21,7 @@ use clap::Args; use sc_service::{BlocksPruning, PruningMode}; /// Parameters to define the pruning mode -#[derive(Debug, Clone, PartialEq, Args)] +#[derive(Debug, Clone, Args)] pub struct PruningParams { /// Specify the state pruning mode. /// diff --git a/client/cli/src/params/shared_params.rs b/client/cli/src/params/shared_params.rs index 46f5390039d46..913a6c436185c 100644 --- a/client/cli/src/params/shared_params.rs +++ b/client/cli/src/params/shared_params.rs @@ -22,7 +22,7 @@ use sc_service::config::BasePath; use std::path::PathBuf; /// Shared parameters used by all `CoreParams`. -#[derive(Debug, Clone, PartialEq, Args)] +#[derive(Debug, Clone, Args)] pub struct SharedParams { /// Specify the chain specification. /// From d973b1d721d6395b2c5ed1db16dd3930bd17058d Mon Sep 17 00:00:00 2001 From: yjhmelody Date: Tue, 11 Apr 2023 12:12:44 +0800 Subject: [PATCH 6/9] improve `max_runtime_instances` --- client/cli/src/commands/run_cmd.rs | 4 +--- client/cli/src/params/runtime_params.rs | 14 +++++++++++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/client/cli/src/commands/run_cmd.rs b/client/cli/src/commands/run_cmd.rs index 14f59f39b689c..a38ba6f49e3dd 100644 --- a/client/cli/src/commands/run_cmd.rs +++ b/client/cli/src/commands/run_cmd.rs @@ -435,9 +435,7 @@ impl CliConfiguration for RunCmd { } fn max_runtime_instances(&self) -> Result> { - let mut params = self.runtime_params.clone(); - params.normalize(); - Ok(Some(params.max_runtime_instances)) + Ok(Some(self.runtime_params.max_runtime_instances)) } fn runtime_cache_size(&self) -> Result { diff --git a/client/cli/src/params/runtime_params.rs b/client/cli/src/params/runtime_params.rs index 92ee3fdfdf6d2..4902ce5d31676 100644 --- a/client/cli/src/params/runtime_params.rs +++ b/client/cli/src/params/runtime_params.rs @@ -1,10 +1,11 @@ +use std::str::FromStr; use clap::Args; /// Parameters used to config runtime. #[derive(Debug, Clone, Args)] pub struct RuntimeParams { /// The size of the instances cache for each runtime. The values higher than 256 are ignored. - #[arg(long, default_value_t = 8)] + #[arg(long, default_value_t = 8, value_parser = parse_max_runtime_instances)] pub max_runtime_instances: usize, /// Maximum number of different runtimes that can be cached. @@ -20,3 +21,14 @@ impl RuntimeParams { } } } + +fn parse_max_runtime_instances(s: &str) -> Result { + let max_runtime_instances = usize::from_str(s) + .map_err(|_err| format!("Illegal `max_runtime_instances` value: {s}"))?; + + if max_runtime_instances > 256 { + Err(format!("Illegal `max_runtime_instances` value: {max_runtime_instances}")) + } else { + Ok(max_runtime_instances) + } +} From 0afc3e0584992ae1ab34c1f297fd51e0dfacc191 Mon Sep 17 00:00:00 2001 From: yjhmelody Date: Tue, 11 Apr 2023 14:57:32 +0800 Subject: [PATCH 7/9] fmt --- client/cli/src/params/runtime_params.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/client/cli/src/params/runtime_params.rs b/client/cli/src/params/runtime_params.rs index 4902ce5d31676..03b7f07ab87c8 100644 --- a/client/cli/src/params/runtime_params.rs +++ b/client/cli/src/params/runtime_params.rs @@ -1,5 +1,5 @@ -use std::str::FromStr; use clap::Args; +use std::str::FromStr; /// Parameters used to config runtime. #[derive(Debug, Clone, Args)] @@ -23,8 +23,8 @@ impl RuntimeParams { } fn parse_max_runtime_instances(s: &str) -> Result { - let max_runtime_instances = usize::from_str(s) - .map_err(|_err| format!("Illegal `max_runtime_instances` value: {s}"))?; + let max_runtime_instances = + usize::from_str(s).map_err(|_err| format!("Illegal `max_runtime_instances` value: {s}"))?; if max_runtime_instances > 256 { Err(format!("Illegal `max_runtime_instances` value: {max_runtime_instances}")) From 201faba5349ed891e21ab02037cb57ab5f211b67 Mon Sep 17 00:00:00 2001 From: yjhmelody Date: Tue, 11 Apr 2023 20:53:41 +0800 Subject: [PATCH 8/9] add license and improve code --- client/cli/src/params/prometheus_params.rs | 18 +++++++++++ client/cli/src/params/runtime_params.rs | 35 ++++++++++++++-------- client/cli/src/params/telemetry_params.rs | 18 +++++++++++ 3 files changed, 58 insertions(+), 13 deletions(-) diff --git a/client/cli/src/params/prometheus_params.rs b/client/cli/src/params/prometheus_params.rs index 9c5e1c5df7d1a..69199ad5b2603 100644 --- a/client/cli/src/params/prometheus_params.rs +++ b/client/cli/src/params/prometheus_params.rs @@ -1,3 +1,21 @@ +// This file is part of Substrate. + +// Copyright (C) Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + use clap::Args; use sc_service::config::PrometheusConfig; use std::net::{Ipv4Addr, SocketAddr}; diff --git a/client/cli/src/params/runtime_params.rs b/client/cli/src/params/runtime_params.rs index 03b7f07ab87c8..276074557c0b5 100644 --- a/client/cli/src/params/runtime_params.rs +++ b/client/cli/src/params/runtime_params.rs @@ -1,10 +1,28 @@ +// This file is part of Substrate. + +// Copyright (C) Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + use clap::Args; use std::str::FromStr; /// Parameters used to config runtime. #[derive(Debug, Clone, Args)] pub struct RuntimeParams { - /// The size of the instances cache for each runtime. The values higher than 256 are ignored. + /// The size of the instances cache for each runtime. The values higher than 256 are illegal. #[arg(long, default_value_t = 8, value_parser = parse_max_runtime_instances)] pub max_runtime_instances: usize, @@ -13,21 +31,12 @@ pub struct RuntimeParams { pub runtime_cache_size: u8, } -impl RuntimeParams { - /// Normalize the params. - pub fn normalize(&mut self) { - if self.max_runtime_instances > 256 { - self.max_runtime_instances = 8; - } - } -} - fn parse_max_runtime_instances(s: &str) -> Result { - let max_runtime_instances = - usize::from_str(s).map_err(|_err| format!("Illegal `max_runtime_instances` value: {s}"))?; + let max_runtime_instances = usize::from_str(s) + .map_err(|_err| format!("Illegal `--max-runtime-instances` value: {s}"))?; if max_runtime_instances > 256 { - Err(format!("Illegal `max_runtime_instances` value: {max_runtime_instances}")) + Err(format!("Illegal `--max-runtime-instances` value: {max_runtime_instances}")) } else { Ok(max_runtime_instances) } diff --git a/client/cli/src/params/telemetry_params.rs b/client/cli/src/params/telemetry_params.rs index 9d0d9254bbcab..ca096419869af 100644 --- a/client/cli/src/params/telemetry_params.rs +++ b/client/cli/src/params/telemetry_params.rs @@ -1,3 +1,21 @@ +// This file is part of Substrate. + +// Copyright (C) Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + use clap::Args; /// Parameters used to config telemetry. From 677bbbe9535008ae9cfa8683c75c21a3f04fc20d Mon Sep 17 00:00:00 2001 From: yjh Date: Thu, 20 Apr 2023 17:52:07 +0800 Subject: [PATCH 9/9] Update client/cli/src/params/runtime_params.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- client/cli/src/params/runtime_params.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/cli/src/params/runtime_params.rs b/client/cli/src/params/runtime_params.rs index 276074557c0b5..79035fc7d4c5d 100644 --- a/client/cli/src/params/runtime_params.rs +++ b/client/cli/src/params/runtime_params.rs @@ -36,7 +36,7 @@ fn parse_max_runtime_instances(s: &str) -> Result { .map_err(|_err| format!("Illegal `--max-runtime-instances` value: {s}"))?; if max_runtime_instances > 256 { - Err(format!("Illegal `--max-runtime-instances` value: {max_runtime_instances}")) + Err(format!("Illegal `--max-runtime-instances` value: {max_runtime_instances} is more than the allowed maximum of `256` ")) } else { Ok(max_runtime_instances) }