Skip to content

Commit

Permalink
Add a tool to trim genesis configs
Browse files Browse the repository at this point in the history
  • Loading branch information
hrxi authored and jsdanielh committed Nov 15, 2024
1 parent c83f232 commit e170770
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
7 changes: 7 additions & 0 deletions tools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ path = "src/signtx/main.rs"
name = "nimiq-rpc-schema"
path = "src/rpc-schema/main.rs"

[[bin]]
name = "nimiq-trim-genesis-config"
path = "src/trim-genesis-config/main.rs"

[dependencies]
anyhow = "1.0"
clap = { version = "4.5", features = ["cargo"] }
Expand All @@ -42,8 +46,11 @@ serde = "1.0"
serde_json = "1.0"
syn = { version = "2.0", features = ["full"] }
thiserror = "1.0"
toml = "0.8"

nimiq-bls = { workspace = true }
nimiq-database = { workspace = true }
nimiq-genesis-builder = { workspace = true }
nimiq-hash = { workspace = true }
nimiq-keys = { workspace = true }
nimiq-primitives = { workspace = true }
Expand Down
80 changes: 80 additions & 0 deletions tools/src/trim-genesis-config/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use std::process;

use clap::{Arg, ArgAction, Command};
use nimiq_database::mdbx::{DatabaseConfig, MdbxDatabase};
use nimiq_genesis_builder::{config::GenesisConfig, GenesisBuilder};

fn db() -> MdbxDatabase {
MdbxDatabase::new_volatile(DatabaseConfig {
size: Some(0..100 * 1024 * 1024 * 1024),
..Default::default()
})
.expect("couldn't open volatile database")
}

fn main() {
let matches = Command::new("nimiq-trim-genesis-config")
.about("Trims genesis config to not contain the state")
.arg(
Arg::new("genesis-config")
.value_name("GENESIS_INFO")
.help("Path to genesis config toml")
.required(true),
)
.arg(
Arg::new("verbose")
.short('v')
.long("verbose")
.action(ArgAction::SetTrue)
.help("Show status information"),
)
.arg(
Arg::new("no-verify")
.long("no-verify")
.action(ArgAction::SetTrue)
.help("Don't check that the generated config file is equivalent"),
)
.get_matches();

let genesis_config = matches.get_one::<String>("genesis-config").unwrap();
let verbose = matches.get_flag("verbose");
let no_verify = matches.get_flag("no-verify");

macro_rules! log {
($($args:tt)*) => {
if verbose {
eprintln!($($args)*);
}
}
}

log!("reading genesis config...");
let genesis_builder = GenesisBuilder::from_config_file(genesis_config).unwrap();
log!("generating genesis block...");
let genesis_info = genesis_builder.generate(db()).unwrap();
let hash = genesis_info.block.hash();
log!("{}: generated genesis block hash", hash);
log!("generating config...");
let genesis_config_trimmed =
GenesisConfig::trimmed_from_genesis(&genesis_info.block.unwrap_macro().header);

if !no_verify {
log!("reading trimmed config...");
let genesis_builder = GenesisBuilder::from_config(genesis_config_trimmed.clone()).unwrap();
log!("generating genesis block from trimmed config...");
let genesis_info = genesis_builder.generate(db()).unwrap();
let hash_trimmed = genesis_info.block.hash();
log!("{}: genesis block hash generated from trimmed config", hash);
if hash_trimmed == hash {
log!("hashes match");
} else {
eprintln!("hash mismatch, {} != {}", hash_trimmed, hash);
eprintln!("aborting");
process::exit(1);
}
}
log!("done");
log!("");

print!("{}", toml::to_string(&genesis_config_trimmed).unwrap());
}

0 comments on commit e170770

Please sign in to comment.