Skip to content

Commit

Permalink
Create genesis.toml file in init command
Browse files Browse the repository at this point in the history
  • Loading branch information
tomyrd committed Jun 26, 2024
1 parent 3266d82 commit eae1cf7
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 14 deletions.
44 changes: 38 additions & 6 deletions bin/node/src/commands/genesis/inputs.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,36 @@
use serde::Deserialize;
use std::time::{SystemTime, UNIX_EPOCH};

use serde::{Deserialize, Serialize};

// INPUT HELPER STRUCTS
// ================================================================================================

/// 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<AccountInput>,
}

#[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,
pub auth_seed: String,
pub storage_mode: String,
}

#[derive(Debug, Clone, Deserialize)]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct BasicFungibleFaucetInputs {
pub init_seed: String,
pub auth_scheme: AuthSchemeInput,
Expand All @@ -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(),
}),
],
}
}
}
2 changes: 1 addition & 1 deletion bin/node/src/commands/genesis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
26 changes: 20 additions & 6 deletions bin/node/src/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
2 changes: 1 addition & 1 deletion bin/node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
Expand Down

0 comments on commit eae1cf7

Please sign in to comment.