From 4ecbc2b14cdaca7ea6f4d2636effdf6a8aa38167 Mon Sep 17 00:00:00 2001 From: Benjamin Kampmann Date: Wed, 23 Oct 2019 11:25:37 +0200 Subject: [PATCH 1/7] Move config path generation into the service config for reusability --- core/cli/src/lib.rs | 104 ++++++++++--------------------------- core/service/src/config.rs | 31 +++++++++++ node/cli/src/lib.rs | 6 ++- 3 files changed, 63 insertions(+), 78 deletions(-) diff --git a/core/cli/src/lib.rs b/core/cli/src/lib.rs index 7b96788433ed2..9abccac2f547b 100644 --- a/core/cli/src/lib.rs +++ b/core/cli/src/lib.rs @@ -307,18 +307,33 @@ pub struct ParseAndPrepareBuildSpec<'a> { impl<'a> ParseAndPrepareBuildSpec<'a> { /// Runs the command and build the chain specs. - pub fn run( + pub fn run( self, spec_factory: S ) -> error::Result<()> where S: FnOnce(&str) -> Result>, String>, + C: Default, G: RuntimeGenesis, E: ChainSpecExtension, { info!("Building chain spec"); let raw_output = self.params.raw; let mut spec = load_spec(&self.params.shared_params, spec_factory)?; - with_default_boot_node(&mut spec, self.params, self.version)?; + + if spec.boot_nodes().is_empty() { + let base_path = base_path(&self.params.shared_params, self.version); + let config = service::Configuration::::default_with_spec_and_base_path(spec.clone(), base_path); + let node_key = node_key_config(self.params.node_key_params, &Some(config.network_path()))?; + let keys = node_key.into_keypair()?; + let peer_id = keys.public().into_peer_id(); + let addr = build_multiaddr![ + Ip4([127, 0, 0, 1]), + Tcp(30333u16), + P2p(peer_id) + ]; + spec.add_boot_node(addr) + } + let json = service::chain_ops::build_spec(spec, raw_output)?; print!("{}", json); @@ -556,18 +571,16 @@ fn fill_transaction_pool_configuration( } /// Fill the given `NetworkConfiguration` by looking at the cli parameters. -fn fill_network_configuration( +fn fill_network_configuration +( cli: NetworkConfigurationParams, - base_path: &Path, - chain_spec_id: &str, + config_path: PathBuf, config: &mut NetworkConfiguration, client_id: String, is_dev: bool, ) -> error::Result<()> { config.boot_nodes.extend(cli.bootnodes.into_iter()); - config.config_path = Some( - network_path(&base_path, chain_spec_id).to_string_lossy().into() - ); + config.config_path = Some(config_path.to_string_lossy().into()); config.net_config_path = config.config_path.clone(); config.reserved_nodes.extend(cli.reserved_nodes.into_iter()); @@ -642,7 +655,8 @@ where S: FnOnce(&str) -> Result>, String>, { let spec = load_spec(&cli.shared_params, spec_factory)?; - let mut config = service::Configuration::default_with_spec(spec.clone()); + let base_path = base_path(&cli.shared_params, &version); + let mut config = service::Configuration::default_with_spec_and_base_path(spec.clone(), base_path); fill_config_keystore_password(&mut config, &cli)?; @@ -666,14 +680,10 @@ where )? } - let base_path = base_path(&cli.shared_params, version); - - config.keystore_path = cli.keystore_path.unwrap_or_else( - || keystore_path(&base_path, config.chain_spec.id()) - ); + config.keystore_path = cli.keystore_path.unwrap_or_else( || config.in_chain_config_dir("keystore")); config.database = DatabaseConfig::Path { - path: db_path(&base_path, config.chain_spec.id()), + path: config.database_path(), cache_size: cli.database_cache_size, }; config.state_cache_size = cli.state_cache_size; @@ -740,8 +750,7 @@ where let client_id = config.client_id(); fill_network_configuration( cli.network_config, - &base_path, - spec.id(), + config.network_path(), &mut config.network, client_id, is_dev, @@ -792,39 +801,6 @@ where Ok(config) } -// -// IANA unassigned port ranges that we could use: -// 6717-6766 Unassigned -// 8504-8553 Unassigned -// 9556-9591 Unassigned -// 9803-9874 Unassigned -// 9926-9949 Unassigned - -fn with_default_boot_node( - spec: &mut ChainSpec, - cli: BuildSpecCmd, - version: &VersionInfo, -) -> error::Result<()> -where - G: RuntimeGenesis, - E: ChainSpecExtension, -{ - if spec.boot_nodes().is_empty() && !cli.disable_default_bootnode { - let base_path = base_path(&cli.shared_params, version); - let storage_path = network_path(&base_path, spec.id()); - let node_key = node_key_config(cli.node_key_params, &Some(storage_path))?; - let keys = node_key.into_keypair()?; - let peer_id = keys.public().into_peer_id(); - let addr = build_multiaddr![ - Ip4([127, 0, 0, 1]), - Tcp(30333u16), - P2p(peer_id) - ]; - spec.add_boot_node(addr) - } - Ok(()) -} - /// Creates a configuration including the database path. pub fn create_config_with_db_path( spec_factory: S, cli: &SharedParams, version: &VersionInfo, @@ -838,9 +814,9 @@ where let spec = load_spec(cli, spec_factory)?; let base_path = base_path(cli, version); - let mut config = service::Configuration::default_with_spec(spec.clone()); + let mut config = service::Configuration::default_with_spec_and_base_path(spec.clone(), base_path); config.database = DatabaseConfig::Path { - path: db_path(&base_path, spec.id()), + path: config.database_path(), cache_size: None, }; @@ -866,30 +842,6 @@ fn parse_address( Ok(address) } -fn keystore_path(base_path: &Path, chain_id: &str) -> PathBuf { - let mut path = base_path.to_owned(); - path.push("chains"); - path.push(chain_id); - path.push("keystore"); - path -} - -fn db_path(base_path: &Path, chain_id: &str) -> PathBuf { - let mut path = base_path.to_owned(); - path.push("chains"); - path.push(chain_id); - path.push("db"); - path -} - -fn network_path(base_path: &Path, chain_id: &str) -> PathBuf { - let mut path = base_path.to_owned(); - path.push("chains"); - path.push(chain_id); - path.push("network"); - path -} - fn init_logger(pattern: &str) { use ansi_term::Colour; diff --git a/core/service/src/config.rs b/core/service/src/config.rs index 21acae3cab051..11002afd4deb1 100644 --- a/core/service/src/config.rs +++ b/core/service/src/config.rs @@ -43,6 +43,8 @@ pub struct Configuration { pub transaction_pool: transaction_pool::txpool::Options, /// Network configuration. pub network: NetworkConfiguration, + /// Path to the base configuration directory. + pub config_dir: PathBuf, /// Path to key files. pub keystore_path: PathBuf, /// Configuration for the database. @@ -120,11 +122,17 @@ impl Configuration where { /// Create default config for given chain spec. pub fn default_with_spec(chain_spec: ChainSpec) -> Self { + Self::default_with_spec_and_base_path(chain_spec, Default::default()) + } + + /// Create a default config for given chain spec and path to configuration dir + pub fn default_with_spec_and_base_path(chain_spec: ChainSpec, config_dir: PathBuf) -> Self { let mut configuration = Configuration { impl_name: "parity-substrate", impl_version: "0.0.0", impl_commit: "", chain_spec, + config_dir, name: Default::default(), roles: Roles::FULL, transaction_pool: Default::default(), @@ -161,6 +169,10 @@ impl Configuration where configuration } +} + +impl Configuration { + /// Returns full version string of this configuration. pub fn full_version(&self) -> String { full_version_from_strs(self.impl_version, self.impl_commit) @@ -170,6 +182,25 @@ impl Configuration where pub fn client_id(&self) -> String { format!("{}/v{}", self.impl_name, self.full_version()) } + + /// Generate a PathBuf to sub in the chain configuration directory + pub fn in_chain_config_dir(&self, sub: &str) -> PathBuf { + let mut path = self.config_dir.clone(); + path.push("chains"); + path.push(self.chain_spec.id()); + path.push(sub); + path + } + + /// The basepath for the db directory + pub fn database_path(&self) -> PathBuf { + self.in_chain_config_dir("db") + } + + /// The basepath for the network file + pub fn network_path(&self) -> PathBuf { + self.in_chain_config_dir("network") + } } /// Returns platform info diff --git a/node/cli/src/lib.rs b/node/cli/src/lib.rs index 5125ba216a2cc..9fa3249845eb2 100644 --- a/node/cli/src/lib.rs +++ b/node/cli/src/lib.rs @@ -150,13 +150,15 @@ fn load_spec(id: &str) -> Result, String> { }) } +type CustomConf = (); + /// Parse command line arguments into service configuration. pub fn run(args: I, exit: E, version: cli::VersionInfo) -> error::Result<()> where I: IntoIterator, T: Into + Clone, E: IntoExit, { - type Config = Configuration<(), A, B>; + type Config = Configuration; match parse_and_prepare::(&version, "substrate-node", args) { ParseAndPrepare::Run(cmd) => cmd.run(load_spec, exit, @@ -182,7 +184,7 @@ pub fn run(args: I, exit: E, version: cli::VersionInfo) -> error::Resul ), } }), - ParseAndPrepare::BuildSpec(cmd) => cmd.run(load_spec), + ParseAndPrepare::BuildSpec(cmd) => cmd.run::(load_spec), ParseAndPrepare::ExportBlocks(cmd) => cmd.run_with_builder(|config: Config<_, _>| Ok(new_full_start!(config).0), load_spec, exit), ParseAndPrepare::ImportBlocks(cmd) => cmd.run_with_builder(|config: Config<_, _>| From fc9f1b036ce99e00145fd04214498bb3babbc286 Mon Sep 17 00:00:00 2001 From: Benjamin Kampmann Date: Thu, 31 Oct 2019 14:54:13 +0100 Subject: [PATCH 2/7] Make NoCostum Default and fix tests --- core/cli/src/params.rs | 2 +- core/service/test/src/lib.rs | 1 + node-template/src/cli.rs | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/core/cli/src/params.rs b/core/cli/src/params.rs index 007bdabf0b7f5..4439dc1c4df19 100644 --- a/core/cli/src/params.rs +++ b/core/cli/src/params.rs @@ -835,7 +835,7 @@ impl GetLogFilter for CoreParams where CC: GetLogFilter { /// A special commandline parameter that expands to nothing. /// Should be used as custom subcommand/run arguments if no custom values are required. -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Default)] pub struct NoCustom {} impl StructOpt for NoCustom { diff --git a/core/service/test/src/lib.rs b/core/service/test/src/lib.rs index 27f03e09633b5..cc1da4a9f5aa5 100644 --- a/core/service/test/src/lib.rs +++ b/core/service/test/src/lib.rs @@ -171,6 +171,7 @@ fn node_config ( network: network_config, keystore_path: root.join("key"), keystore_password: None, + config_dir: root.clone(), database: DatabaseConfig::Path { path: root.join("db"), cache_size: None diff --git a/node-template/src/cli.rs b/node-template/src/cli.rs index ceb51504e29ba..ec463a236b2e0 100644 --- a/node-template/src/cli.rs +++ b/node-template/src/cli.rs @@ -39,7 +39,7 @@ pub fn run(args: I, exit: E, version: VersionInfo) -> error::Result<()> ), } }), - ParseAndPrepare::BuildSpec(cmd) => cmd.run(load_spec), + ParseAndPrepare::BuildSpec(cmd) => cmd.run::(load_spec), ParseAndPrepare::ExportBlocks(cmd) => cmd.run_with_builder(|config: Config<_>| Ok(new_full_start!(config).0), load_spec, exit), ParseAndPrepare::ImportBlocks(cmd) => cmd.run_with_builder(|config: Config<_>| From 8a1a25d9741616fd06c3ca645dae7d450a8b2e8c Mon Sep 17 00:00:00 2001 From: Benjamin Kampmann Date: Fri, 1 Nov 2019 16:33:06 +0100 Subject: [PATCH 3/7] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Bastian Köcher --- core/cli/src/lib.rs | 9 ++++----- core/service/src/config.rs | 1 - 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/core/cli/src/lib.rs b/core/cli/src/lib.rs index 9abccac2f547b..4471245f1f55a 100644 --- a/core/cli/src/lib.rs +++ b/core/cli/src/lib.rs @@ -307,7 +307,7 @@ pub struct ParseAndPrepareBuildSpec<'a> { impl<'a> ParseAndPrepareBuildSpec<'a> { /// Runs the command and build the chain specs. - pub fn run( + pub fn run( self, spec_factory: S ) -> error::Result<()> where @@ -320,7 +320,7 @@ impl<'a> ParseAndPrepareBuildSpec<'a> { let raw_output = self.params.raw; let mut spec = load_spec(&self.params.shared_params, spec_factory)?; - if spec.boot_nodes().is_empty() { + if spec.boot_nodes().is_empty() && !self.params.disable_default_bootnode { let base_path = base_path(&self.params.shared_params, self.version); let config = service::Configuration::::default_with_spec_and_base_path(spec.clone(), base_path); let node_key = node_key_config(self.params.node_key_params, &Some(config.network_path()))?; @@ -571,8 +571,7 @@ fn fill_transaction_pool_configuration( } /// Fill the given `NetworkConfiguration` by looking at the cli parameters. -fn fill_network_configuration -( +fn fill_network_configuration( cli: NetworkConfigurationParams, config_path: PathBuf, config: &mut NetworkConfiguration, @@ -680,7 +679,7 @@ where )? } - config.keystore_path = cli.keystore_path.unwrap_or_else( || config.in_chain_config_dir("keystore")); + config.keystore_path = cli.keystore_path.unwrap_or_else(|| config.in_chain_config_dir("keystore")); config.database = DatabaseConfig::Path { path: config.database_path(), diff --git a/core/service/src/config.rs b/core/service/src/config.rs index 11002afd4deb1..30958e5e7fb92 100644 --- a/core/service/src/config.rs +++ b/core/service/src/config.rs @@ -172,7 +172,6 @@ impl Configuration where } impl Configuration { - /// Returns full version string of this configuration. pub fn full_version(&self) -> String { full_version_from_strs(self.impl_version, self.impl_commit) From 3f12f758b42136e50bb45a9660cedae41a9dbc11 Mon Sep 17 00:00:00 2001 From: Benjamin Kampmann Date: Fri, 1 Nov 2019 16:55:27 +0100 Subject: [PATCH 4/7] remove function not used anymore --- core/service/src/config.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/core/service/src/config.rs b/core/service/src/config.rs index 30958e5e7fb92..f2402bc0d22e1 100644 --- a/core/service/src/config.rs +++ b/core/service/src/config.rs @@ -120,11 +120,6 @@ impl Configuration where G: RuntimeGenesis, E: Extension, { - /// Create default config for given chain spec. - pub fn default_with_spec(chain_spec: ChainSpec) -> Self { - Self::default_with_spec_and_base_path(chain_spec, Default::default()) - } - /// Create a default config for given chain spec and path to configuration dir pub fn default_with_spec_and_base_path(chain_spec: ChainSpec, config_dir: PathBuf) -> Self { let mut configuration = Configuration { From 0113741827eb3cb30ec20ee15fdb19f1c76ec0ba Mon Sep 17 00:00:00 2001 From: Benjamin Kampmann Date: Fri, 1 Nov 2019 17:52:36 +0100 Subject: [PATCH 5/7] Make path into an option --- core/cli/src/lib.rs | 19 +++++++++++-------- core/service/src/builder.rs | 10 ++++++++-- core/service/src/config.rs | 28 +++++++++++++++------------- core/service/test/src/lib.rs | 4 ++-- node/cli/src/browser.rs | 2 +- 5 files changed, 37 insertions(+), 26 deletions(-) diff --git a/core/cli/src/lib.rs b/core/cli/src/lib.rs index 0d9d86a1c939d..68a2c3f14b8ee 100644 --- a/core/cli/src/lib.rs +++ b/core/cli/src/lib.rs @@ -322,8 +322,11 @@ impl<'a> ParseAndPrepareBuildSpec<'a> { if spec.boot_nodes().is_empty() && !self.params.disable_default_bootnode { let base_path = base_path(&self.params.shared_params, self.version); - let config = service::Configuration::::default_with_spec_and_base_path(spec.clone(), base_path); - let node_key = node_key_config(self.params.node_key_params, &Some(config.network_path()))?; + let cfg = service::Configuration::::default_with_spec_and_base_path(spec.clone(), Some(base_path)); + let node_key = node_key_config( + self.params.node_key_params, + &Some(cfg.network_path().expect("We provided a base_path")) + )?; let keys = node_key.into_keypair()?; let peer_id = keys.public().into_peer_id(); let addr = build_multiaddr![ @@ -655,7 +658,7 @@ where { let spec = load_spec(&cli.shared_params, spec_factory)?; let base_path = base_path(&cli.shared_params, &version); - let mut config = service::Configuration::default_with_spec_and_base_path(spec.clone(), base_path); + let mut config = service::Configuration::default_with_spec_and_base_path(spec.clone(), Some(base_path)); fill_config_keystore_password(&mut config, &cli)?; @@ -679,10 +682,10 @@ where )? } - config.keystore_path = cli.keystore_path.unwrap_or_else(|| config.in_chain_config_dir("keystore")); + config.keystore_path = cli.keystore_path.or_else(|| config.in_chain_config_dir("keystore")); config.database = DatabaseConfig::Path { - path: config.database_path(), + path: config.database_path().expect("We provided a base_path."), cache_size: cli.database_cache_size, }; config.state_cache_size = cli.state_cache_size; @@ -749,7 +752,7 @@ where let client_id = config.client_id(); fill_network_configuration( cli.network_config, - config.network_path(), + config.network_path().expect("We provided a basepath"), &mut config.network, client_id, is_dev, @@ -813,9 +816,9 @@ where let spec = load_spec(cli, spec_factory)?; let base_path = base_path(cli, version); - let mut config = service::Configuration::default_with_spec_and_base_path(spec.clone(), base_path); + let mut config = service::Configuration::default_with_spec_and_base_path(spec.clone(), Some(base_path)); config.database = DatabaseConfig::Path { - path: config.database_path(), + path: config.database_path().expect("We provided a base_path."), cache_size: None, }; diff --git a/core/service/src/builder.rs b/core/service/src/builder.rs index a9a85faab21c4..03db6e385b025 100644 --- a/core/service/src/builder.rs +++ b/core/service/src/builder.rs @@ -155,7 +155,10 @@ where TGen: RuntimeGenesis, TCSExt: Extension { (), TFullBackend, >, Error> { - let keystore = Keystore::open(config.keystore_path.clone(), config.keystore_password.clone())?; + let keystore = Keystore::open( + config.keystore_path.clone().ok_or("No basepath configured")?, + config.keystore_password.clone() + )?; let executor = NativeExecutor::::new( config.wasm_method, @@ -236,7 +239,10 @@ where TGen: RuntimeGenesis, TCSExt: Extension { (), TLightBackend, >, Error> { - let keystore = Keystore::open(config.keystore_path.clone(), config.keystore_password.clone())?; + let keystore = Keystore::open( + config.keystore_path.clone().ok_or("No basepath configured")?, + config.keystore_password.clone() + )?; let executor = NativeExecutor::::new( config.wasm_method, diff --git a/core/service/src/config.rs b/core/service/src/config.rs index f2402bc0d22e1..2363767e7625e 100644 --- a/core/service/src/config.rs +++ b/core/service/src/config.rs @@ -44,9 +44,9 @@ pub struct Configuration { /// Network configuration. pub network: NetworkConfiguration, /// Path to the base configuration directory. - pub config_dir: PathBuf, + pub config_dir: Option, /// Path to key files. - pub keystore_path: PathBuf, + pub keystore_path: Option, /// Configuration for the database. pub database: DatabaseConfig, /// Size of internal state cache in Bytes @@ -121,18 +121,18 @@ impl Configuration where E: Extension, { /// Create a default config for given chain spec and path to configuration dir - pub fn default_with_spec_and_base_path(chain_spec: ChainSpec, config_dir: PathBuf) -> Self { + pub fn default_with_spec_and_base_path(chain_spec: ChainSpec, config_dir: Option) -> Self { let mut configuration = Configuration { impl_name: "parity-substrate", impl_version: "0.0.0", impl_commit: "", chain_spec, - config_dir, + config_dir: config_dir.clone(), name: Default::default(), roles: Roles::FULL, transaction_pool: Default::default(), network: Default::default(), - keystore_path: Default::default(), + keystore_path: config_dir.map(|c| c.join("keystore")), database: DatabaseConfig::Path { path: Default::default(), cache_size: Default::default(), @@ -178,21 +178,23 @@ impl Configuration { } /// Generate a PathBuf to sub in the chain configuration directory - pub fn in_chain_config_dir(&self, sub: &str) -> PathBuf { - let mut path = self.config_dir.clone(); - path.push("chains"); - path.push(self.chain_spec.id()); - path.push(sub); - path + /// if given + pub fn in_chain_config_dir(&self, sub: &str) -> Option { + self.config_dir.clone().map(|mut path| { + path.push("chains"); + path.push(self.chain_spec.id()); + path.push(sub); + path + }) } /// The basepath for the db directory - pub fn database_path(&self) -> PathBuf { + pub fn database_path(&self) -> Option { self.in_chain_config_dir("db") } /// The basepath for the network file - pub fn network_path(&self) -> PathBuf { + pub fn network_path(&self) -> Option { self.in_chain_config_dir("network") } } diff --git a/core/service/test/src/lib.rs b/core/service/test/src/lib.rs index cc1da4a9f5aa5..c147f8051065e 100644 --- a/core/service/test/src/lib.rs +++ b/core/service/test/src/lib.rs @@ -169,9 +169,9 @@ fn node_config ( roles: role, transaction_pool: Default::default(), network: network_config, - keystore_path: root.join("key"), + keystore_path: Some(root.join("key")), keystore_password: None, - config_dir: root.clone(), + config_dir: Some(root.clone()), database: DatabaseConfig::Path { path: root.join("db"), cache_size: None diff --git a/node/cli/src/browser.rs b/node/cli/src/browser.rs index 1c84efa107ea7..702d67b55afa5 100644 --- a/node/cli/src/browser.rs +++ b/node/cli/src/browser.rs @@ -39,7 +39,7 @@ fn start_inner(wasm_ext: wasm_ext::ffi::Transport) -> Result::default_with_spec(chain_spec); + let mut config = Configuration::<(), _, _>::default_with_spec_and_base_path(chain_spec, None); config.network.transport = network::config::TransportConfig::Normal { wasm_external_transport: Some(wasm_ext.clone()), enable_mdns: false, From e31e2049613eedd54395fe72b4a893d5e058adec Mon Sep 17 00:00:00 2001 From: Benjamin Kampmann Date: Fri, 1 Nov 2019 18:08:13 +0100 Subject: [PATCH 6/7] remove database_path function and call it directly --- core/cli/src/lib.rs | 4 ++-- core/service/src/config.rs | 5 ----- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/core/cli/src/lib.rs b/core/cli/src/lib.rs index 68a2c3f14b8ee..4ff106cabdc92 100644 --- a/core/cli/src/lib.rs +++ b/core/cli/src/lib.rs @@ -685,7 +685,7 @@ where config.keystore_path = cli.keystore_path.or_else(|| config.in_chain_config_dir("keystore")); config.database = DatabaseConfig::Path { - path: config.database_path().expect("We provided a base_path."), + path: config.in_chain_config_dir("db").expect("We provided a base_path."), cache_size: cli.database_cache_size, }; config.state_cache_size = cli.state_cache_size; @@ -818,7 +818,7 @@ where let mut config = service::Configuration::default_with_spec_and_base_path(spec.clone(), Some(base_path)); config.database = DatabaseConfig::Path { - path: config.database_path().expect("We provided a base_path."), + path: config.in_chain_config_dir("db").expect("We provided a base_path."), cache_size: None, }; diff --git a/core/service/src/config.rs b/core/service/src/config.rs index 2363767e7625e..423e36247e21a 100644 --- a/core/service/src/config.rs +++ b/core/service/src/config.rs @@ -187,11 +187,6 @@ impl Configuration { path }) } - - /// The basepath for the db directory - pub fn database_path(&self) -> Option { - self.in_chain_config_dir("db") - } /// The basepath for the network file pub fn network_path(&self) -> Option { From bd0f5de90cb61bc8b8c425e06afaf68e80693dd4 Mon Sep 17 00:00:00 2001 From: Benjamin Kampmann Date: Fri, 1 Nov 2019 18:25:02 +0100 Subject: [PATCH 7/7] remove helper functions, use consts --- core/cli/src/lib.rs | 17 ++++++++++++----- core/service/src/config.rs | 5 ----- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/core/cli/src/lib.rs b/core/cli/src/lib.rs index 4ff106cabdc92..e445addf5ddd7 100644 --- a/core/cli/src/lib.rs +++ b/core/cli/src/lib.rs @@ -65,6 +65,13 @@ use lazy_static::lazy_static; use futures::Future; use substrate_telemetry::TelemetryEndpoints; +/// default sub directory to store network config +const DEFAULT_NETWORK_CONFIG_PATH : &'static str = "network"; +/// default sub directory to store database +const DEFAULT_DB_CONFIG_PATH : &'static str = "db"; +/// default sub directory for the key store +const DEFAULT_KEYSTORE_CONFIG_PATH : &'static str = "keystore"; + /// The maximum number of characters for a node name. const NODE_NAME_MAX_LENGTH: usize = 32; @@ -325,7 +332,7 @@ impl<'a> ParseAndPrepareBuildSpec<'a> { let cfg = service::Configuration::::default_with_spec_and_base_path(spec.clone(), Some(base_path)); let node_key = node_key_config( self.params.node_key_params, - &Some(cfg.network_path().expect("We provided a base_path")) + &Some(cfg.in_chain_config_dir(DEFAULT_NETWORK_CONFIG_PATH).expect("We provided a base_path")) )?; let keys = node_key.into_keypair()?; let peer_id = keys.public().into_peer_id(); @@ -682,10 +689,10 @@ where )? } - config.keystore_path = cli.keystore_path.or_else(|| config.in_chain_config_dir("keystore")); + config.keystore_path = cli.keystore_path.or_else(|| config.in_chain_config_dir(DEFAULT_KEYSTORE_CONFIG_PATH)); config.database = DatabaseConfig::Path { - path: config.in_chain_config_dir("db").expect("We provided a base_path."), + path: config.in_chain_config_dir(DEFAULT_DB_CONFIG_PATH).expect("We provided a base_path."), cache_size: cli.database_cache_size, }; config.state_cache_size = cli.state_cache_size; @@ -752,7 +759,7 @@ where let client_id = config.client_id(); fill_network_configuration( cli.network_config, - config.network_path().expect("We provided a basepath"), + config.in_chain_config_dir(DEFAULT_NETWORK_CONFIG_PATH).expect("We provided a basepath"), &mut config.network, client_id, is_dev, @@ -818,7 +825,7 @@ where let mut config = service::Configuration::default_with_spec_and_base_path(spec.clone(), Some(base_path)); config.database = DatabaseConfig::Path { - path: config.in_chain_config_dir("db").expect("We provided a base_path."), + path: config.in_chain_config_dir(DEFAULT_DB_CONFIG_PATH).expect("We provided a base_path."), cache_size: None, }; diff --git a/core/service/src/config.rs b/core/service/src/config.rs index 423e36247e21a..e9f002c21f442 100644 --- a/core/service/src/config.rs +++ b/core/service/src/config.rs @@ -187,11 +187,6 @@ impl Configuration { path }) } - - /// The basepath for the network file - pub fn network_path(&self) -> Option { - self.in_chain_config_dir("network") - } } /// Returns platform info