Skip to content

Commit

Permalink
feat(node): store state for any # of networks
Browse files Browse the repository at this point in the history
Addresses kindelia#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/<network_id>/{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.
  • Loading branch information
dan-da committed Nov 28, 2022
1 parent 2357f80 commit fbddfc0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
6 changes: 3 additions & 3 deletions kindelia/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,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 @@ -225,9 +228,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
23 changes: 12 additions & 11 deletions kindelia/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit fbddfc0

Please sign in to comment.