Skip to content

Commit

Permalink
feat(cli): enable chain specific config settings
Browse files Browse the repository at this point in the history
Addresses kindelia#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
  • Loading branch information
dan-da committed Nov 28, 2022
1 parent a8ffb65 commit 31eaefd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
2 changes: 2 additions & 0 deletions kindelia/default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 5 additions & 5 deletions kindelia/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ where
#[builder(default)]
env: Option<&'static str>,
#[builder(default)]
prop: Option<&'static str>,
prop: Option<String>,
default_value: F,
}

Expand Down Expand Up @@ -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)()
}
Expand All @@ -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)()
}
Expand All @@ -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!(
Expand All @@ -107,7 +107,7 @@ where
T: ArgumentFrom<toml::Value>,
{
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: {}",
Expand Down
12 changes: 6 additions & 6 deletions kindelia/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,15 @@ 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,
);

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,
Expand All @@ -272,30 +272,30 @@ 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,
);

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,
);

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()
Expand Down

0 comments on commit 31eaefd

Please sign in to comment.