Skip to content

Commit

Permalink
refactor(statedb/genesis-verify): reserve UTXO got from statedb to ra…
Browse files Browse the repository at this point in the history
…w UTXO for verifying (#2379)

has verified: raw_data -> data_in_statedb <- raw_data

added in this PR: raw_data -> data_in_statedb -> raw_data

reverse UTXO got from statedb to raw UTXO, then comparing them:

UTXO has more complicated struct than Inscirption in state_db: complex struct like SimpleMap.
  • Loading branch information
popcnt1 authored Aug 7, 2024
1 parent 30eb0e3 commit 3553aa0
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
32 changes: 28 additions & 4 deletions crates/rooch/src/commands/statedb/commands/genesis_verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,36 @@
use std::fs::File;
use std::io::{BufRead, BufReader, Read};
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_vm_types::values::Value;
use rustc_hash::FxHashSet;

use moveos_store::MoveOSStore;
use moveos_types::move_std::string::MoveString;
use moveos_types::moveos_std::object::ObjectMeta;
use moveos_types::state::MoveState;
use moveos_types::state_resolver::{RootObjectResolver, StatelessResolver};
use rooch_config::R_OPT_NET_HELP;
use rooch_types::bitcoin::ord::InscriptionStore;
use rooch_types::bitcoin::utxo::BitcoinUTXOStore;
use rooch_types::bitcoin::utxo::{BitcoinUTXOStore, UTXO};
use rooch_types::error::RoochResult;
use rooch_types::framework::address_mapping::RoochToBitcoinAddressMapping;
use rooch_types::into_address::IntoAddress;
use rooch_types::rooch_network::RoochChainID;

use crate::commands::statedb::commands::inscription::{
gen_inscription_ids_update, InscriptionSource,
derive_inscription_ids, gen_inscription_ids_update, InscriptionSource,
};
use crate::commands::statedb::commands::utxo::UTXORawData;
use crate::commands::statedb::commands::{init_job, OutpointInscriptionsMap};
use crate::commands::statedb::commands::{
get_values_by_key, init_job, OutpointInscriptionsMap, UTXO_SEAL_INSCRIPTION_PROTOCOL,
};

/// Import BTC ordinals & UTXO for genesis
#[derive(Debug, Parser)]
Expand Down Expand Up @@ -172,7 +178,25 @@ fn verify_utxo(
.unwrap()
.unwrap();
assert_eq!(act_utxo_state, state);

let act_utxo_value =
Value::simple_deserialize(&act_utxo_state.value, &UTXO::type_layout()).unwrap();
let act_utxo = UTXO::from_runtime_value(act_utxo_value).unwrap();
assert_eq!(utxo_raw.amount, act_utxo.value);
assert_eq!(utxo_raw.vout, act_utxo.vout);
assert_eq!(utxo_raw.txid.into_address(), act_utxo.txid);
let inscriptions =
outpoint_inscriptions_map.search(&OutPoint::new(utxo_raw.txid, utxo_raw.vout));
let inscriptions_obj_ids = derive_inscription_ids(inscriptions);
let act_inscriptions = get_values_by_key(
act_utxo.seals,
MoveString::from_str(UTXO_SEAL_INSCRIPTION_PROTOCOL).unwrap(),
);
if inscriptions_obj_ids.is_empty() {
assert!(act_inscriptions.is_none());
} else {
let act_inscriptions = act_inscriptions.unwrap();
assert_eq!(act_inscriptions, inscriptions_obj_ids);
}
if addr_updates.is_some() {
let (addr_key, addr_state) = addr_updates.unwrap();
let act_address_state = resolver
Expand Down
15 changes: 15 additions & 0 deletions crates/rooch/src/commands/statedb/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,21 @@ impl OutpointInscriptionsMap {
}
}

pub(crate) fn get_values_by_key<Key, Value>(
map: SimpleMultiMap<Key, Value>,
key: Key,
) -> Option<Vec<Value>>
where
Key: PartialEq,
{
for element in map.data {
if element.key == key {
return Some(element.value);
}
}
None
}

#[cfg(test)]
mod tests {
use std::collections::HashSet;
Expand Down
6 changes: 3 additions & 3 deletions crates/rooch/src/commands/statedb/commands/utxo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ const SCRIPT_TYPE_NON_STANDARD: &str = "non-standard";

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UTXORawData {
txid: Txid,
vout: u32,
amount: u64,
pub(crate) txid: Txid,
pub(crate) vout: u32,
pub(crate) amount: u64,
script: String,
script_type: String,
address: String,
Expand Down

0 comments on commit 3553aa0

Please sign in to comment.