Skip to content

Commit

Permalink
chore: add verification for specific UTXO and inscription cases in ge…
Browse files Browse the repository at this point in the history
…nesis-verify process (#2429)

Enhanced the GenesisVerifyCommand to handle specific UTXO and inscription cases by adding new path options. Introduced `UTXOCases` and `OrdCases` structs to manage these cases and updated verification functions to use these case lists.
  • Loading branch information
popcnt1 committed Aug 13, 2024
1 parent 14d256c commit 59651a5
Showing 1 changed file with 88 additions and 2 deletions.
90 changes: 88 additions & 2 deletions crates/rooch/src/commands/statedb/commands/genesis_verify.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

use std::collections::HashSet;
use std::fs::File;
use std::io::{BufRead, BufReader, BufWriter, Read, Write};
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::Arc;
use std::thread;
use std::time::Instant;

use bitcoin::OutPoint;
use clap::Parser;
use move_core_types::account_address::AccountAddress;
use move_vm_types::values::Value;
Expand Down Expand Up @@ -64,6 +67,16 @@ pub struct GenesisVerifyCommand {
pub utxo_mismatched_output: PathBuf,
#[clap(long, help = "mismatched ord output path")]
pub ord_mismatched_output: PathBuf,
#[clap(
long,
help = "inscription cases must be verified, file is outpoint list"
)]
pub utxo_cases: Option<PathBuf>,
#[clap(
long,
help = "utxo cases must be verified, file is inscription_number list"
)]
pub ord_cases: Option<PathBuf>,

#[clap(long = "data-dir", short = 'd')]
/// Path to data dir, this dir is base dir, the final data_dir is base_dir/chain_network_name
Expand All @@ -75,6 +88,64 @@ pub struct GenesisVerifyCommand {
pub chain_id: Option<RoochChainID>,
}

struct UTXOCases {
cases: HashSet<OutPoint>,
}

impl UTXOCases {
fn load(path: Option<PathBuf>) -> Self {
let path = match path {
None => {
return Self {
cases: HashSet::new(),
}
}
Some(path) => path,
};
let mut cases = HashSet::new();
let file = File::open(path).expect("Unable to open utxo cases file");
let reader = BufReader::new(file);
for line in reader.lines() {
let line = line.unwrap();
let outpoint = OutPoint::from_str(&line).unwrap();
cases.insert(outpoint);
}
Self { cases }
}
fn contains(&self, outpoint: &OutPoint) -> bool {
self.cases.contains(outpoint)
}
}

struct OrdCases {
cases: HashSet<u32>,
}

impl OrdCases {
fn load(path: Option<PathBuf>) -> Self {
let path = match path {
None => {
return Self {
cases: HashSet::new(),
}
}
Some(path) => path,
};
let mut cases = HashSet::new();
let file = File::open(path).expect("Unable to open ord cases file");
let reader = BufReader::new(file);
for line in reader.lines() {
let line = line.unwrap();
let ord = line.parse::<u32>().unwrap();
cases.insert(ord);
}
Self { cases }
}
fn contains(&self, inscription_number: u32) -> bool {
self.cases.contains(&inscription_number)
}
}

impl GenesisVerifyCommand {
pub async fn execute(self) -> RoochResult<()> {
let (root, moveos_store, start_time) =
Expand All @@ -96,6 +167,7 @@ impl GenesisVerifyCommand {
.spawn(move || {
verify_inscription(
inscription_source_path,
self.ord_cases,
moveos_store_clone,
root_clone_0,
random_mode,
Expand All @@ -110,6 +182,7 @@ impl GenesisVerifyCommand {
.spawn(move || {
verify_utxo(
self.utxo_source,
self.utxo_cases,
moveos_store_clone,
root.clone(),
outpoint_inscriptions_map,
Expand All @@ -129,6 +202,7 @@ impl GenesisVerifyCommand {

fn verify_utxo(
input: PathBuf,
case_path: Option<PathBuf>,
moveos_store_arc: Arc<MoveOSStore>,
root: ObjectMeta,
outpoint_inscriptions_map: Arc<OutpointInscriptionsMap>,
Expand Down Expand Up @@ -165,6 +239,8 @@ fn verify_utxo(
let file = File::create(mismatched_output.clone()).expect("Unable to create utxo output file");
let mut output_writer = BufWriter::with_capacity(1 << 23, file.try_clone().unwrap());

let cases = UTXOCases::load(case_path);

let mut utxo_total: u32 = 0;
let mut utxo_checked_count: u32 = 0;
let mut utxo_mismatched_count: u32 = 0;
Expand Down Expand Up @@ -207,7 +283,12 @@ fn verify_utxo(
);
}

if random_mode && rand::random::<u32>() % sample_rate != 0 {
let is_case = cases.contains(&OutPoint {
txid: utxo_raw.txid,
vout: utxo_raw.vout,
});

if (random_mode && rand::random::<u32>() % sample_rate != 0) && !is_case {
continue;
}
// check utxo
Expand Down Expand Up @@ -291,6 +372,7 @@ fn verify_utxo(

fn verify_inscription(
input: PathBuf,
case_path: Option<PathBuf>,
moveos_store_arc: Arc<MoveOSStore>,
root: ObjectMeta,
random_mode: bool,
Expand Down Expand Up @@ -324,6 +406,8 @@ fn verify_inscription(
File::create(mismatched_output.clone()).expect("Unable to create inscription output file");
let mut output_writer = BufWriter::with_capacity(1 << 23, file.try_clone().unwrap());

let cases = OrdCases::load(case_path);

let mut checked_count: u32 = 0;
let mut mismatched_count: u32 = 0;
let mut not_found_count: u32 = 0;
Expand Down Expand Up @@ -364,7 +448,9 @@ fn verify_inscription(
);
}

if random_mode && rand::random::<u32>() % sample_rate != 0 {
let is_case = cases.contains(source.inscription_number as u32);

if (random_mode && rand::random::<u32>() % sample_rate != 0) && !is_case {
continue;
}
// check inscription
Expand Down

0 comments on commit 59651a5

Please sign in to comment.