Skip to content

Commit

Permalink
feat(iota-genesis-builder): add cli command to disable global snapsho…
Browse files Browse the repository at this point in the history
…t verification (#911)

* add cli command to disable global snapshot verification

* refactor: Pass `VERIFY` generic type

* refactor: Remove generic type from outputs()

* move comment

---------

Co-authored-by: samuel_rufi <[email protected]>
  • Loading branch information
Alex6323 and samuel-rufi authored Jul 3, 2024
1 parent 7bbc502 commit 53b55ec
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! and verifying the total supply.
use std::fs::File;

use iota_genesis_builder::stardust::parse::HornetGenesisSnapshotParser;
use iota_genesis_builder::stardust::parse::HornetSnapshotParser;
use iota_types::gas_coin::TOTAL_SUPPLY_IOTA;

fn main() -> anyhow::Result<()> {
Expand All @@ -14,7 +14,7 @@ fn main() -> anyhow::Result<()> {
};
let file = File::open(path)?;

let mut parser = HornetGenesisSnapshotParser::new(file)?;
let mut parser = HornetSnapshotParser::new::<true>(file)?;
println!("Output count: {}", parser.header.output_count());

let total_supply = parser.outputs().try_fold(0, |acc, output| {
Expand Down
12 changes: 6 additions & 6 deletions crates/iota-genesis-builder/examples/snapshot_test_outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
use std::{fs::File, path::Path};

use iota_genesis_builder::stardust::{
parse::HornetGenesisSnapshotParser, test_outputs::add_snapshot_test_outputs,
parse::HornetSnapshotParser, test_outputs::add_snapshot_test_outputs,
};
use iota_types::gas_coin::TOTAL_SUPPLY_IOTA;

fn parse_snapshot<P: AsRef<Path>>(path: P) -> anyhow::Result<()> {
fn parse_snapshot<P: AsRef<Path>, const VERIFY: bool>(path: P) -> anyhow::Result<()> {
let file = File::open(path)?;
let mut parser = HornetGenesisSnapshotParser::new(file)?;
let mut parser = HornetSnapshotParser::new::<VERIFY>(file)?;

println!("Output count: {}", parser.header.output_count());

Expand Down Expand Up @@ -43,11 +43,11 @@ async fn main() -> anyhow::Result<()> {
new_path.push_str(&current_path);
}

parse_snapshot(&current_path)?;
parse_snapshot::<_, true>(&current_path)?;

add_snapshot_test_outputs(&current_path, &new_path).await?;
add_snapshot_test_outputs::<_, true>(&current_path, &new_path).await?;

parse_snapshot(&new_path)?;
parse_snapshot::<_, true>(&new_path)?;

Ok(())
}
15 changes: 13 additions & 2 deletions crates/iota-genesis-builder/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use clap::{Parser, Subcommand};
use iota_genesis_builder::{
stardust::{
migration::{Migration, MigrationTargetNetwork},
parse::HornetGenesisSnapshotParser,
parse::HornetSnapshotParser,
},
BROTLI_COMPRESSOR_BUFFER_SIZE, BROTLI_COMPRESSOR_LG_WINDOW_SIZE, BROTLI_COMPRESSOR_QUALITY,
OBJECT_SNAPSHOT_FILE_PATH,
Expand All @@ -40,6 +40,12 @@ struct Cli {
help = "Compress the resulting object snapshot"
)]
compress: bool,
#[clap(
long,
help = "Enable global snapshot verification",
default_value_t = true
)]
global_snapshot_verification: bool,
}

#[derive(Subcommand, Debug)]
Expand Down Expand Up @@ -81,7 +87,11 @@ fn main() -> Result<()> {
};

// Start the Hornet snapshot parser
let mut snapshot_parser = HornetGenesisSnapshotParser::new(File::open(snapshot_path)?)?;
let mut snapshot_parser = if cli.global_snapshot_verification {
HornetSnapshotParser::new::<true>(File::open(snapshot_path)?)?
} else {
HornetSnapshotParser::new::<false>(File::open(snapshot_path)?)?
};
let total_supply = match coin_type {
CoinType::Iota => scale_amount_for_iota(snapshot_parser.total_supply()?)?,
CoinType::Shimmer => snapshot_parser.total_supply()?,
Expand Down Expand Up @@ -121,6 +131,7 @@ fn main() -> Result<()> {
}
})
.process_results(|outputs| migration.run(outputs, object_snapshot_writer))??;

Ok(())
}

Expand Down
13 changes: 7 additions & 6 deletions crates/iota-genesis-builder/src/stardust/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@ use packable::{
use super::types::{output_header::OutputHeader, snapshot::FullSnapshotHeader};

/// Parse a Hornet genesis snapshot using a [`BufReader`] internally.
pub struct HornetGenesisSnapshotParser<R: Read> {
pub struct HornetSnapshotParser<R: Read> {
reader: IoUnpacker<BufReader<R>>,
/// The full-snapshot header
pub header: FullSnapshotHeader,
}

impl<R: Read> HornetGenesisSnapshotParser<R> {
pub fn new(reader: R) -> Result<Self> {
impl<R: Read> HornetSnapshotParser<R> {
/// Creates a new [`HornetSnapshotParser`].
///
/// `VERIFY = true` ensures that only global snapshots parse successfully.
pub fn new<const VERIFY: bool>(reader: R) -> Result<Self> {
let mut reader = IoUnpacker::new(std::io::BufReader::new(reader));
// `true` ensures that only genesis snapshots unpack successfully
let header = FullSnapshotHeader::unpack::<_, true>(&mut reader, &())?;

let header = FullSnapshotHeader::unpack::<_, VERIFY>(&mut reader, &())?;
Ok(Self { reader, header })
}

Expand Down
6 changes: 3 additions & 3 deletions crates/iota-genesis-builder/src/stardust/test_outputs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use packable::{
};

use crate::stardust::{
parse::HornetGenesisSnapshotParser,
parse::HornetSnapshotParser,
types::{output_header::OutputHeader, output_index::random_output_index},
};

Expand Down Expand Up @@ -65,7 +65,7 @@ pub(crate) fn new_vested_output(
}

/// Adds outputs to test specific and intricate scenario in the full snapshot.
pub async fn add_snapshot_test_outputs<P: AsRef<Path> + core::fmt::Debug>(
pub async fn add_snapshot_test_outputs<P: AsRef<Path> + core::fmt::Debug, const VERIFY: bool>(
current_path: P,
new_path: P,
) -> anyhow::Result<()> {
Expand All @@ -76,7 +76,7 @@ pub async fn add_snapshot_test_outputs<P: AsRef<Path> + core::fmt::Debug>(
.truncate(true)
.open(new_path)?;
let mut writer = IoPacker::new(BufWriter::new(new_file));
let mut parser = HornetGenesisSnapshotParser::new(current_file)?;
let mut parser = HornetSnapshotParser::new::<VERIFY>(current_file)?;
let output_to_decrease_amount_from = OutputId::from_str(OUTPUT_TO_DECREASE_AMOUNT_FROM)?;
let mut new_header = parser.header.clone();
let mut vested_index = u32::MAX;
Expand Down

0 comments on commit 53b55ec

Please sign in to comment.