From fbddfc00784bcc95f1b87c41fcfff6ba55f3e968 Mon Sep 17 00:00:00 2001 From: danda Date: Wed, 16 Nov 2022 21:42:14 -0800 Subject: [PATCH] feat(node): store state for any # of networks Addresses #163 Previously chain state was stored under a path such as: ~/.kindelia/state/{blocks,heaps} With this change, the data is stored at: ~/.kindelia/state//{blocks,heaps} This enables for example flipping back and forth between a testnet and mainnet just by changing network_id in the config, or even via cli arg. --- kindelia/src/cli.rs | 6 +++--- kindelia/src/main.rs | 23 ++++++++++++----------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/kindelia/src/cli.rs b/kindelia/src/cli.rs index 3a71c9c7..674b5ca9 100644 --- a/kindelia/src/cli.rs +++ b/kindelia/src/cli.rs @@ -203,6 +203,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 { @@ -225,9 +228,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/main.rs b/kindelia/src/main.rs index 3260323e..b5b31045 100644 --- a/kindelia/src/main.rs +++ b/kindelia/src/main.rs @@ -241,34 +241,35 @@ 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", + 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", 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",