diff --git a/bin/node/src/commands/genesis/inputs.rs b/bin/node/src/commands/genesis/inputs.rs index d455e30a0..22fb86c98 100644 --- a/bin/node/src/commands/genesis/inputs.rs +++ b/bin/node/src/commands/genesis/inputs.rs @@ -1,4 +1,6 @@ -use serde::Deserialize; +use std::time::{SystemTime, UNIX_EPOCH}; + +use serde::{Deserialize, Serialize}; // INPUT HELPER STRUCTS // ================================================================================================ @@ -6,21 +8,21 @@ use serde::Deserialize; /// Input types are helper structures designed for parsing and deserializing genesis input files. /// They serve as intermediary representations, facilitating the conversion from /// placeholder types (like `GenesisInput`) to internal types (like `GenesisState`). -#[derive(Debug, Clone, Deserialize)] +#[derive(Debug, Clone, Deserialize, Serialize)] pub struct GenesisInput { pub version: u32, pub timestamp: u32, pub accounts: Vec, } -#[derive(Debug, Clone, Deserialize)] +#[derive(Debug, Clone, Deserialize, Serialize)] #[serde(tag = "type")] pub enum AccountInput { BasicWallet(BasicWalletInputs), BasicFungibleFaucet(BasicFungibleFaucetInputs), } -#[derive(Debug, Clone, Deserialize)] +#[derive(Debug, Clone, Deserialize, Serialize)] pub struct BasicWalletInputs { pub init_seed: String, pub auth_scheme: AuthSchemeInput, @@ -28,7 +30,7 @@ pub struct BasicWalletInputs { pub storage_mode: String, } -#[derive(Debug, Clone, Deserialize)] +#[derive(Debug, Clone, Deserialize, Serialize)] pub struct BasicFungibleFaucetInputs { pub init_seed: String, pub auth_scheme: AuthSchemeInput, @@ -39,7 +41,37 @@ pub struct BasicFungibleFaucetInputs { pub storage_mode: String, } -#[derive(Debug, Clone, Copy, Deserialize)] +#[derive(Debug, Clone, Copy, Deserialize, Serialize)] pub enum AuthSchemeInput { RpoFalcon512, } + +impl Default for GenesisInput { + fn default() -> Self { + const SEED_SUFFIX: &str = "123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; + Self { + version: 1, + timestamp: SystemTime::now() + .duration_since(UNIX_EPOCH) + .expect("Current timestamp should be greater than unix epoch") + .as_secs() as u32, + accounts: vec![ + AccountInput::BasicWallet(BasicWalletInputs { + init_seed: "0xa".to_string() + SEED_SUFFIX, + auth_scheme: AuthSchemeInput::RpoFalcon512, + auth_seed: "0xb".to_string() + SEED_SUFFIX, + storage_mode: "off-chain".to_string(), + }), + AccountInput::BasicFungibleFaucet(BasicFungibleFaucetInputs { + init_seed: "0xc".to_string() + SEED_SUFFIX, + auth_scheme: AuthSchemeInput::RpoFalcon512, + auth_seed: "0xd".to_string() + SEED_SUFFIX, + token_symbol: "POL".to_string(), + decimals: 12, + max_supply: 1000000, + storage_mode: "on-chain".to_string(), + }), + ], + } + } +} diff --git a/bin/node/src/commands/genesis/mod.rs b/bin/node/src/commands/genesis/mod.rs index 32c1942e0..d0a0fb886 100644 --- a/bin/node/src/commands/genesis/mod.rs +++ b/bin/node/src/commands/genesis/mod.rs @@ -4,7 +4,7 @@ use std::{ }; use anyhow::{anyhow, Result}; -use inputs::{AccountInput, AuthSchemeInput, GenesisInput}; +pub use inputs::{AccountInput, AuthSchemeInput, GenesisInput}; use miden_lib::{ accounts::{faucets::create_basic_fungible_faucet, wallets::create_basic_wallet}, AuthScheme, diff --git a/bin/node/src/commands/init.rs b/bin/node/src/commands/init.rs index 42c19ff37..13ea35c45 100644 --- a/bin/node/src/commands/init.rs +++ b/bin/node/src/commands/init.rs @@ -2,27 +2,41 @@ use std::{fs::File, io::Write, path::PathBuf}; use anyhow::{anyhow, Result}; -use crate::config::NodeConfig; +use crate::{commands::genesis::GenesisInput, config::NodeConfig}; // INIT // =================================================================================================== -pub fn init_config_files(config_file_path: PathBuf, _genesis_file_path: PathBuf) -> Result<()> { +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))?; + write_string_in_file(config_as_toml_string, &config_file_path)?; + + println!("Config file successfully created at: {:?}", config_file_path); + + let genesis = GenesisInput::default(); + let genesis_as_toml_string = toml::to_string(&genesis) + .map_err(|err| anyhow!("Failed to serialize default config: {}", err))?; + + write_string_in_file(genesis_as_toml_string, &genesis_file_path)?; + + println!("Genesis config file successfully created at: {:?}", genesis_file_path); + + Ok(()) +} + +fn write_string_in_file(content: String, path: &PathBuf) -> Result<()> { let mut file_handle = File::options() .write(true) .create_new(true) - .open(&config_file_path) + .open(path) .map_err(|err| anyhow!("Error opening the file: {err}"))?; file_handle - .write(config_as_toml_string.as_bytes()) + .write(content.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/main.rs b/bin/node/src/main.rs index 34f316de2..e8a014bd3 100644 --- a/bin/node/src/main.rs +++ b/bin/node/src/main.rs @@ -67,7 +67,7 @@ pub enum Command { #[arg(short, long, default_value = NODE_CONFIG_FILE_PATH)] config_path: String, - #[arg(short, long, default_value = DEFAULT_GENESIS_FILE_PATH)] + #[arg(short, long, default_value = DEFAULT_GENESIS_INPUTS_PATH)] genesis_path: String, }, }