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/cli.rs b/kindelia/src/cli.rs index a69ef6de..044d166a 100644 --- a/kindelia/src/cli.rs +++ b/kindelia/src/cli.rs @@ -196,6 +196,9 @@ pub enum CliCommand { /// Base path to store the node's data in. #[clap(long)] data_dir: Option, + /// Network id / magic number. + #[clap(long)] + network_id: Option, }, /// Generate auto-completion for a shell. Completion { @@ -218,9 +221,6 @@ pub enum NodeCommand { }, /// Starts a Kindelia node. Start { - /// Network id / magic number. - #[clap(long)] - network_id: Option, /// Initial peer nodes. #[clap(long, short = 'p')] initial_peers: Option>, 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 8b9e87f5..0f177972 100644 --- a/kindelia/src/main.rs +++ b/kindelia/src/main.rs @@ -28,12 +28,12 @@ use kindelia_core::events; use kindelia_core::hvm::{ self, view_statement, view_statement_header, Statement, }; -use kindelia_core::parser; use kindelia_core::net; use kindelia_core::net::ProtoComm; use kindelia_core::node::{ spawn_miner, Node, Transaction, TransactionError, MAX_TRANSACTION_SIZE, }; +use kindelia_core::parser; use kindelia_core::persistence::{ get_ordered_blocks_path, SimpleFileStorage, BLOCKS_DIR, }; @@ -241,37 +241,38 @@ pub fn run_cli() -> Result<(), String> { init_config_file(&path)?; Ok(()) } - CliCommand::Node { command, data_dir } => { + CliCommand::Node { command, data_dir, network_id } => { let config = handle_config_file(&config_path)?; let config = Some(&config); + let network_id = resolve_cfg!( + env = "KINDELIA_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, - ); + ) + .join(format!("{:#02X}", network_id)); match command { NodeCommand::Clean { command } => clean(&data_path, command) .map_err(|err| format!("Could not clean kindelia's data: {}", err)), - NodeCommand::Start { initial_peers, network_id, mine, json } => { + NodeCommand::Start { initial_peers, mine, json } => { // TODO: refactor config resolution out of command handling (how?) // Get arguments from cli, env or config - let network_id = resolve_cfg!( - env = "KINDELIA_NETWORK_ID", - prop = "node.network.network_id", - no_default = "Missing `network_id` paramenter.".to_string(), - cli_val = network_id, - cfg = config, - ); - 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, @@ -279,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, @@ -287,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()