utxo_scanner/
lib.rs

1#![allow(warnings)]
2//
3// Usage:
4//
5// ! ```rust,ignore
6//
7// use utxo_scanner
8//
9// //Scan for all UTXOs
10// let stats = utxo_scanner::scan("/path/to/.bitcoin", true, Some("/path/to/desired.csv"));
11//
12// println!("Total Txs: {}", stats.count);
13// println!("Total Amount: {}", stats.amount);
14// println!("Total Secs: {}", stats.total_secs);
15//
16// Remove RocksDB and start fresh
17// utxo_scanner::reset_rocksdb
18//
19#![crate_type = "lib"]
20
21pub mod database;
22pub mod keys;
23pub mod scanner;
24pub mod csv_loader;
25pub mod utils;
26pub mod utxo;
27#[allow(missing_docs)]
28
29use lazy_static::lazy_static; // 1.4.0
30use std::fs;
31use std::path::Path;
32use std::sync::Mutex;
33use crate::scanner::Stats;
34use std::collections::BTreeMap;
35
36lazy_static! {
37    static ref BITCOIN_DATADIR: Mutex<String> = Mutex::new("".to_string());
38}
39/// Scan Bitcoin Core chainstate LevelDB and extract all UTXOs to
40//// RocksDB, a CSV file or both.
41///
42/// Returns a struct that contains the total number of transactions, amount and seconds the process took.
43pub fn scan(bitcoin_datadir: &str, create_rocksdb: bool, csv_file: Option<&str>, testnet: bool) -> Stats {
44    *BITCOIN_DATADIR.lock().unwrap() = bitcoin_datadir
45        .to_string()
46        .trim_end_matches("/")
47        .to_string();
48
49    let stats = scanner::scan(create_rocksdb, csv_file, testnet);
50    stats
51}
52
53/// Reset the RocksDB and start fresh when scanning.
54pub fn reset_rocksdb(bitcoin_datadir: &str) {
55    // Get directory
56    let dirname = format!("{}/rocksdb", bitcoin_datadir.trim_end_matches("/"));
57    if !Path::new(&dirname).exists() {
58        return;
59    }
60
61    // Remove
62    match fs::remove_dir_all(&dirname) {
63        Ok(_dir) => {}
64        Err(e) => panic!("Unable to remove directory at {}, error: {}.", dirname, e),
65    };
66}
67
68
69/// Load all UTXOs from a previously generated CSV file into a BTreeMap<String, Vec<u64>> 
70/// where the string is the payment address and the vector is the line number(s) that match that address.
71pub fn load_from_csv(csv_file: &str) -> BTreeMap<String, Vec<u64>> {
72    let map = csv_loader::load(csv_file);
73    map
74}
75
76