|
| 1 | +mod bitcoin_primitives; |
| 2 | +mod bitcoin_serialization; |
| 3 | +mod utils; |
| 4 | + |
| 5 | +mod cli; |
| 6 | + |
| 7 | +use anyhow::{Context, Result}; |
| 8 | +use bitcoin::hashes::Hash; |
| 9 | +use clap::Parser; |
| 10 | +use rocksdb::{DB, IteratorMode, Options}; |
| 11 | + |
| 12 | +use bitcoin::hashes::sha256; |
| 13 | + |
| 14 | +use crate::{ |
| 15 | + bitcoin_primitives::{CoinKey, CoinValue}, |
| 16 | + bitcoin_serialization::deobfuscate, |
| 17 | + utils::{P2PKH_UTXO_SIZE, load_utxos, save_utxos}, |
| 18 | +}; |
| 19 | + |
| 20 | +const OBFUSCATION_KEY_DB_KEY: &[u8] = b"\x0e\x00obfuscate_key"; |
| 21 | + |
| 22 | +fn main() -> Result<()> { |
| 23 | + let cli = cli::Cli::parse(); |
| 24 | + |
| 25 | + match &cli.command { |
| 26 | + cli::Commands::IndexChainstate { |
| 27 | + chainstate_path, |
| 28 | + output_path, |
| 29 | + } => run_index_chainstate(chainstate_path.as_str(), output_path.as_str()), |
| 30 | + cli::Commands::BuildMerkleRoot { utxo_index_path } => { |
| 31 | + run_build_merkle_root(utxo_index_path.as_str()) |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +fn run_index_chainstate(chainstate_path: &str, output_path: &str) -> Result<()> { |
| 37 | + let mut opts = Options::default(); |
| 38 | + opts.set_compression_type(rocksdb::DBCompressionType::Snappy); |
| 39 | + opts.create_if_missing(false); |
| 40 | + |
| 41 | + let db = DB::open_for_read_only(&opts, chainstate_path, false)?; |
| 42 | + let obfuscation_key_entry = db |
| 43 | + .get(OBFUSCATION_KEY_DB_KEY) |
| 44 | + .context("DB is not reachable")? |
| 45 | + .context("obfuscation key is not present in DB")?; |
| 46 | + |
| 47 | + println!( |
| 48 | + "Opened chainstate at {:?}. Using obfuscation key: {}", |
| 49 | + chainstate_path, |
| 50 | + hex::encode(&obfuscation_key_entry[1..]) |
| 51 | + ); |
| 52 | + println!("Parsing UTXO entries..."); |
| 53 | + |
| 54 | + let iter = db.iterator(IteratorMode::Start); |
| 55 | + |
| 56 | + let mut p2pkh_count = 0; |
| 57 | + |
| 58 | + let mut utxos: Vec<[u8; P2PKH_UTXO_SIZE]> = Vec::with_capacity(100_000_000); // theoretical max amount of P2PKH UTXO in near future |
| 59 | + |
| 60 | + for (i, item) in iter.enumerate() { |
| 61 | + if i % 500_000 == 0 { |
| 62 | + println!( |
| 63 | + "Processed {} entries. Current P2PKH count: {}", |
| 64 | + i, p2pkh_count |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + let (key, value) = item.context("DB is not reachable and iterable")?; |
| 69 | + |
| 70 | + let _coin_key = match CoinKey::deserialize(&key) { |
| 71 | + Some(ck) => ck, |
| 72 | + None => continue, |
| 73 | + }; |
| 74 | + |
| 75 | + let deobfuscated_value = deobfuscate(&value, &obfuscation_key_entry[1..]); |
| 76 | + |
| 77 | + let coin_value = match CoinValue::deserialize(&deobfuscated_value) { |
| 78 | + Some(cv) => cv, |
| 79 | + None => continue, |
| 80 | + }; |
| 81 | + |
| 82 | + p2pkh_count += 1; |
| 83 | + |
| 84 | + let mut utxo = Vec::with_capacity(P2PKH_UTXO_SIZE); |
| 85 | + utxo.extend_from_slice(&coin_value.amount.to_le_bytes()); |
| 86 | + utxo.extend_from_slice(&coin_value.script_pubkey); |
| 87 | + |
| 88 | + utxos.push(utxo.try_into().expect("UTXO size should match")); |
| 89 | + } |
| 90 | + |
| 91 | + println!("Total P2PKH UTXO entries: {}", p2pkh_count); |
| 92 | + |
| 93 | + save_utxos(&utxos, output_path)?; |
| 94 | + |
| 95 | + println!("UTXO index saved to {}", output_path); |
| 96 | + |
| 97 | + Ok(()) |
| 98 | +} |
| 99 | + |
| 100 | +fn run_build_merkle_root(utxo_index_path: &str) -> Result<()> { |
| 101 | + println!("Loading UTXO index from {}", utxo_index_path); |
| 102 | + |
| 103 | + let utxos = load_utxos(utxo_index_path)?; |
| 104 | + |
| 105 | + println!("Calculating Merkle root for {} UTXOs", utxos.len()); |
| 106 | + |
| 107 | + let mut merkle_tree_leaf_hashes: Vec<sha256::Hash> = Vec::with_capacity(utxos.len()); |
| 108 | + for utxo in utxos { |
| 109 | + let leaf_hash = sha256::Hash::hash(&utxo); |
| 110 | + merkle_tree_leaf_hashes.push(leaf_hash); |
| 111 | + } |
| 112 | + |
| 113 | + let root = bitcoin::merkle_tree::calculate_root(merkle_tree_leaf_hashes.into_iter()) |
| 114 | + .expect("UTXO set should not be empty"); |
| 115 | + |
| 116 | + println!("Merkle root: {}", hex::encode(root.as_byte_array())); |
| 117 | + |
| 118 | + Ok(()) |
| 119 | +} |
0 commit comments