From 31eaefdfc200df43d9b88abef6ed29f6f51e762d Mon Sep 17 00:00:00 2001 From: danda Date: Thu, 17 Nov 2022 18:42:28 -0800 Subject: [PATCH] feat(cli): enable chain specific config settings Addresses #163 node.network setting is now specified on a per network basis, eg: [node.network.0xCAFE0004] initial_peers = [ "64.227.110.69", "188.166.3.140", ] [node.network.0xCAFE0005] initial_peers = [ ... ] Additionally: * updated default.toml to use the new config syntax * AppSettings::prop is now a String instead of &'static str * fixed a typo in error string --- kindelia/default.toml | 2 ++ kindelia/src/config.rs | 10 +++++----- kindelia/src/main.rs | 12 ++++++------ 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/kindelia/default.toml b/kindelia/default.toml index 837100e8..5d1bccf8 100644 --- a/kindelia/default.toml +++ b/kindelia/default.toml @@ -3,6 +3,8 @@ dir = "~/.kindelia/state" [node.network] network_id = "0xCAFE0005" + +[node.network.0xCAFE0005] initial_peers = [ "64.227.110.69", "188.166.3.140", diff --git a/kindelia/src/config.rs b/kindelia/src/config.rs index 5ab5e503..dab851a3 100644 --- a/kindelia/src/config.rs +++ b/kindelia/src/config.rs @@ -15,7 +15,7 @@ where #[builder(default)] env: Option<&'static str>, #[builder(default)] - prop: Option<&'static str>, + prop: Option, default_value: F, } @@ -49,7 +49,7 @@ where } if let (Some(prop_path), Some(config_values)) = (self.prop, config_values) { // If config file and argument prop path are set, read from config file - return Self::resolve_from_config_aux(config_values, prop_path); + return Self::resolve_from_config_aux(config_values, &prop_path); } (self.default_value)() } @@ -65,7 +65,7 @@ where { if let Some(prop_path) = self.prop { if let Some(config_values) = config_values { - Self::resolve_from_config_aux(config_values, prop_path) + Self::resolve_from_config_aux(config_values, &prop_path) } else { (self.default_value)() } @@ -83,7 +83,7 @@ where { if let Some(prop_path) = self.prop { if let Some(config_values) = config_values { - let value = Self::get_prop(config_values, prop_path); + let value = Self::get_prop(config_values, &prop_path); if let Some(value) = value { return T::arg_from(value).map(|v| Some(v)).map_err(|e| { format!( @@ -107,7 +107,7 @@ where T: ArgumentFrom, { let value = Self::get_prop(config_values, prop_path) - .ok_or(format!("Could not found prop '{}' in config file.", prop_path))?; + .ok_or(format!("Could not find prop '{}' in config file.", prop_path))?; T::arg_from(value).map_err(|e| { format!( "Could not convert value of '{}' into desired type: {}", diff --git a/kindelia/src/main.rs b/kindelia/src/main.rs index b5b31045..8c6c8bbc 100644 --- a/kindelia/src/main.rs +++ b/kindelia/src/main.rs @@ -247,7 +247,7 @@ pub fn run_cli() -> Result<(), String> { let network_id = resolve_cfg!( env = "KINDELIA_NETWORK_ID", - prop = "node.network.network_id", + prop = "node.network.network_id".to_string(), no_default = "Missing `network_id` parameter.".to_string(), cli_val = network_id, cfg = config, @@ -255,7 +255,7 @@ pub fn run_cli() -> Result<(), String> { let data_path = resolve_cfg!( env = "KINDELIA_NODE_DATA_DIR", - prop = "node.data.dir", + prop = "node.data.dir".to_string(), default = default_node_data_path()?, cli_val = data_dir, cfg = config, @@ -272,7 +272,7 @@ pub fn run_cli() -> Result<(), String> { let initial_peers = resolve_cfg!( env = "KINDELIA_NODE_INITIAL_PEERS", - prop = "node.network.initial_peers", + prop = format!("node.network.{:#02X}.initial_peers", network_id), default = vec![], cli_val = initial_peers, cfg = config, @@ -280,7 +280,7 @@ pub fn run_cli() -> Result<(), String> { let mine = resolve_cfg!( env = "KINDELIA_MINE", - prop = "node.mining.enable", + prop = "node.mining.enable".to_string(), default = false, cli_val = flag_to_option(mine), cfg = config, @@ -288,14 +288,14 @@ pub fn run_cli() -> Result<(), String> { let slow_mining = ConfigSettingsBuilder::default() .env("KINDELIA_SLOW_MINING") - .prop("node.debug.slow_mining") + .prop("node.debug.slow_mining".to_string()) .default_value(|| Ok(0)) .build() .unwrap() .resolve_from_file_opt(config)?; let api_config = ConfigSettingsBuilder::default() - .prop("node.api") + .prop("node.api".to_string()) .default_value(|| Ok(ApiConfig::default())) .build() .unwrap()