Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

store state for any # of networks #235

Merged
merged 2 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 3 additions & 3 deletions kindelia/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ pub enum CliCommand {
/// Base path to store the node's data in.
#[clap(long)]
data_dir: Option<PathBuf>,
/// Network id / magic number.
#[clap(long)]
network_id: Option<u32>,
},
/// Generate auto-completion for a shell.
Completion {
Expand All @@ -218,9 +221,6 @@ pub enum NodeCommand {
},
/// Starts a Kindelia node.
Start {
/// Network id / magic number.
#[clap(long)]
network_id: Option<u32>,
/// Initial peer nodes.
#[clap(long, short = 'p')]
initial_peers: Option<Vec<String>>,
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
35 changes: 18 additions & 17 deletions kindelia/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -241,60 +241,61 @@ 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,
);

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