Skip to content

Commit

Permalink
Add init command to miden-node
Browse files Browse the repository at this point in the history
  • Loading branch information
tomyrd committed Jun 26, 2024
1 parent a53f515 commit 6729c93
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions bin/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ miden-objects = { workspace = true }
rand_chacha = "0.3"
serde = { version = "1.0", features = ["derive"] }
tokio = { version = "1.29", features = ["rt-multi-thread", "net", "macros"] }
toml = { version = "0.8" }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }

Expand Down
28 changes: 28 additions & 0 deletions bin/node/src/commands/init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use std::{fs::File, io::Write, path::PathBuf};

use anyhow::{anyhow, Result};

use crate::config::NodeConfig;

// INIT
// ===================================================================================================

pub fn init_config_files(config_file_path: PathBuf, _genesis_file_path: PathBuf) -> Result<()> {
let config = NodeConfig::default();
let config_as_toml_string = toml::to_string(&config)
.map_err(|err| anyhow!("Failed to serialize default config: {}", err))?;

let mut file_handle = File::options()
.write(true)
.create_new(true)
.open(&config_file_path)
.map_err(|err| anyhow!("Error opening the file: {err}"))?;

file_handle
.write(config_as_toml_string.as_bytes())
.map_err(|err| anyhow!("Error writing to file: {err}"))?;

println!("Config file successfully created at: {:?}", config_file_path);

Ok(())
}
1 change: 1 addition & 0 deletions bin/node/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod genesis;
pub mod init;
pub mod start;
pub use genesis::make_genesis;
29 changes: 28 additions & 1 deletion bin/node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use std::path::PathBuf;

use anyhow::{anyhow, Context};
use clap::{Parser, Subcommand};
use commands::start::{start_block_producer, start_node, start_rpc, start_store};
use commands::{
init::init_config_files,
start::{start_block_producer, start_node, start_rpc, start_store},
};
use config::NodeConfig;
use miden_node_utils::config::load_config;

Expand Down Expand Up @@ -55,6 +58,18 @@ pub enum Command {
#[arg(short, long)]
force: bool,
},

/// Generates default configuration files for the node
///
/// This command creates two files (miden-node.toml and genesis.toml) that provide configuration
/// details to the node. These files may be modified to change the node behavior.
Init {
#[arg(short, long, default_value = NODE_CONFIG_FILE_PATH)]
config_path: String,

#[arg(short, long, default_value = DEFAULT_GENESIS_FILE_PATH)]
genesis_path: String,
},
}

#[derive(Subcommand)]
Expand Down Expand Up @@ -95,5 +110,17 @@ async fn main() -> anyhow::Result<()> {
Command::MakeGenesis { output_path, force, inputs_path } => {
commands::make_genesis(inputs_path, output_path, force)
},
Command::Init { config_path, genesis_path } => {
let current_dir = std::env::current_dir()
.map_err(|err| anyhow!("failed to open current directory: {err}"))?;

let mut config = current_dir.clone();
let mut genesis = current_dir.clone();

config.push(config_path);
genesis.push(genesis_path);

init_config_files(config, genesis)
},
}
}

0 comments on commit 6729c93

Please sign in to comment.