From 53b55ec0271d3e369e7b61775516970174508763 Mon Sep 17 00:00:00 2001 From: /alex/ Date: Wed, 3 Jul 2024 09:34:13 +0200 Subject: [PATCH] feat(iota-genesis-builder): add cli command to disable global snapshot 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 --- .../examples/parse_hornet_genesis_snapshot.rs | 4 ++-- .../examples/snapshot_test_outputs.rs | 12 ++++++------ crates/iota-genesis-builder/src/main.rs | 15 +++++++++++++-- crates/iota-genesis-builder/src/stardust/parse.rs | 13 +++++++------ .../src/stardust/test_outputs/mod.rs | 6 +++--- 5 files changed, 31 insertions(+), 19 deletions(-) diff --git a/crates/iota-genesis-builder/examples/parse_hornet_genesis_snapshot.rs b/crates/iota-genesis-builder/examples/parse_hornet_genesis_snapshot.rs index 5b29717754e..8af57f46797 100644 --- a/crates/iota-genesis-builder/examples/parse_hornet_genesis_snapshot.rs +++ b/crates/iota-genesis-builder/examples/parse_hornet_genesis_snapshot.rs @@ -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<()> { @@ -14,7 +14,7 @@ fn main() -> anyhow::Result<()> { }; let file = File::open(path)?; - let mut parser = HornetGenesisSnapshotParser::new(file)?; + let mut parser = HornetSnapshotParser::new::(file)?; println!("Output count: {}", parser.header.output_count()); let total_supply = parser.outputs().try_fold(0, |acc, output| { diff --git a/crates/iota-genesis-builder/examples/snapshot_test_outputs.rs b/crates/iota-genesis-builder/examples/snapshot_test_outputs.rs index 430238dcf8a..1bedab191d0 100644 --- a/crates/iota-genesis-builder/examples/snapshot_test_outputs.rs +++ b/crates/iota-genesis-builder/examples/snapshot_test_outputs.rs @@ -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>(path: P) -> anyhow::Result<()> { +fn parse_snapshot, const VERIFY: bool>(path: P) -> anyhow::Result<()> { let file = File::open(path)?; - let mut parser = HornetGenesisSnapshotParser::new(file)?; + let mut parser = HornetSnapshotParser::new::(file)?; println!("Output count: {}", parser.header.output_count()); @@ -43,11 +43,11 @@ async fn main() -> anyhow::Result<()> { new_path.push_str(¤t_path); } - parse_snapshot(¤t_path)?; + parse_snapshot::<_, true>(¤t_path)?; - add_snapshot_test_outputs(¤t_path, &new_path).await?; + add_snapshot_test_outputs::<_, true>(¤t_path, &new_path).await?; - parse_snapshot(&new_path)?; + parse_snapshot::<_, true>(&new_path)?; Ok(()) } diff --git a/crates/iota-genesis-builder/src/main.rs b/crates/iota-genesis-builder/src/main.rs index 0143e527958..a836563e01e 100644 --- a/crates/iota-genesis-builder/src/main.rs +++ b/crates/iota-genesis-builder/src/main.rs @@ -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, @@ -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)] @@ -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::(File::open(snapshot_path)?)? + } else { + HornetSnapshotParser::new::(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()?, @@ -121,6 +131,7 @@ fn main() -> Result<()> { } }) .process_results(|outputs| migration.run(outputs, object_snapshot_writer))??; + Ok(()) } diff --git a/crates/iota-genesis-builder/src/stardust/parse.rs b/crates/iota-genesis-builder/src/stardust/parse.rs index fac8dab5d86..baaba645b59 100644 --- a/crates/iota-genesis-builder/src/stardust/parse.rs +++ b/crates/iota-genesis-builder/src/stardust/parse.rs @@ -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 { +pub struct HornetSnapshotParser { reader: IoUnpacker>, /// The full-snapshot header pub header: FullSnapshotHeader, } -impl HornetGenesisSnapshotParser { - pub fn new(reader: R) -> Result { +impl HornetSnapshotParser { + /// Creates a new [`HornetSnapshotParser`]. + /// + /// `VERIFY = true` ensures that only global snapshots parse successfully. + pub fn new(reader: R) -> Result { 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 }) } diff --git a/crates/iota-genesis-builder/src/stardust/test_outputs/mod.rs b/crates/iota-genesis-builder/src/stardust/test_outputs/mod.rs index 507b0091d0b..f28ad357d59 100644 --- a/crates/iota-genesis-builder/src/stardust/test_outputs/mod.rs +++ b/crates/iota-genesis-builder/src/stardust/test_outputs/mod.rs @@ -25,7 +25,7 @@ use packable::{ }; use crate::stardust::{ - parse::HornetGenesisSnapshotParser, + parse::HornetSnapshotParser, types::{output_header::OutputHeader, output_index::random_output_index}, }; @@ -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 + core::fmt::Debug>( +pub async fn add_snapshot_test_outputs + core::fmt::Debug, const VERIFY: bool>( current_path: P, new_path: P, ) -> anyhow::Result<()> { @@ -76,7 +76,7 @@ pub async fn add_snapshot_test_outputs + 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::(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;