From 6729c931ac885d6bb7bed0b80cd7df9b240faa37 Mon Sep 17 00:00:00 2001 From: tomyrd Date: Tue, 25 Jun 2024 14:32:30 -0300 Subject: [PATCH] Add `init` command to `miden-node` --- Cargo.lock | 1 + bin/node/Cargo.toml | 1 + bin/node/src/commands/init.rs | 28 ++++++++++++++++++++++++++++ bin/node/src/commands/mod.rs | 1 + bin/node/src/main.rs | 29 ++++++++++++++++++++++++++++- 5 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 bin/node/src/commands/init.rs diff --git a/Cargo.lock b/Cargo.lock index d0241ae62..baae50ae6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1721,6 +1721,7 @@ dependencies = [ "rand_chacha", "serde", "tokio", + "toml", "tracing", "tracing-subscriber", ] diff --git a/bin/node/Cargo.toml b/bin/node/Cargo.toml index 0c522e35e..4934d9891 100644 --- a/bin/node/Cargo.toml +++ b/bin/node/Cargo.toml @@ -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 } diff --git a/bin/node/src/commands/init.rs b/bin/node/src/commands/init.rs new file mode 100644 index 000000000..42c19ff37 --- /dev/null +++ b/bin/node/src/commands/init.rs @@ -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(()) +} diff --git a/bin/node/src/commands/mod.rs b/bin/node/src/commands/mod.rs index 2dede7e49..642a71584 100644 --- a/bin/node/src/commands/mod.rs +++ b/bin/node/src/commands/mod.rs @@ -1,3 +1,4 @@ mod genesis; +pub mod init; pub mod start; pub use genesis::make_genesis; diff --git a/bin/node/src/main.rs b/bin/node/src/main.rs index f0a1426ee..34f316de2 100644 --- a/bin/node/src/main.rs +++ b/bin/node/src/main.rs @@ -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; @@ -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)] @@ -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) + }, } }