bitcoin_block_parser/utxos.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
//! Contains [`UtxoParser`] for tracking input amounts and output statuses in [`UtxoBlock`].
use crate::blocks::{BlockParser, ParserIterator, ParserOptions, Pipeline};
use anyhow::{bail, Result};
use bitcoin::block::Header;
use bitcoin::hashes::Hash;
use bitcoin::{Amount, Block, OutPoint, Transaction, TxIn, TxOut, Txid};
use dashmap::DashMap;
use log::info;
use rand::prelude::SmallRng;
use rand::{Error, RngCore, SeedableRng};
use scalable_cuckoo_filter::{DefaultHasher, ScalableCuckooFilter, ScalableCuckooFilterBuilder};
use std::fs;
use std::fs::File;
use std::io::{BufReader, BufWriter};
use std::iter::Zip;
use std::slice::Iter;
use std::sync::{Arc, Mutex};
/// A block that has been parsed tracking input amounts and output status
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct UtxoBlock {
/// The block header
pub header: Header,
/// List of transactions contained in the block
pub txdata: Vec<UtxoTransaction>,
}
impl UtxoBlock {
/// Construct from a bitcoin [`Block`].
fn new(block: Block) -> Self {
Self {
header: block.header,
txdata: block.txdata.into_iter().map(UtxoTransaction::new).collect(),
}
}
/// Convert back into a [`bitcoin::Block`].
pub fn to_block(self) -> Block {
Block {
header: self.header,
txdata: self.txdata.into_iter().map(|tx| tx.transaction).collect(),
}
}
}
/// A transaction that has been parsed tracking input amounts and output status
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct UtxoTransaction {
/// Underlying bitcoin transaction [`Transaction`]
pub transaction: Transaction,
/// Precomputed [`Txid`]
pub txid: Txid,
/// Tracks the input amounts in-order of inputs
inputs: Vec<Amount>,
/// Tracks the output statuses in-order of outputs
outputs: Vec<OutputStatus>,
}
impl UtxoTransaction {
/// Construct from a bitcoin [`Transaction`].
fn new(transaction: Transaction) -> UtxoTransaction {
Self {
txid: transaction.compute_txid(),
transaction,
inputs: vec![],
outputs: vec![],
}
}
/// Returns the [`TxIn`] of the transaction zipped with the input amounts.
pub fn input(&self) -> Zip<Iter<'_, TxIn>, Iter<'_, Amount>> {
self.transaction.input.iter().zip(self.inputs.iter())
}
/// Returns the [`TxOut`] of the transaction zipped with the output [`OutputStatus`].
pub fn output(&self) -> Zip<Iter<'_, TxOut>, Iter<'_, OutputStatus>> {
self.transaction.output.iter().zip(self.outputs.iter())
}
}
/// Status of the [`TxOut`] within the transaction graph.
#[derive(Clone, Debug, Eq, PartialEq, Copy)]
pub enum OutputStatus {
/// The output was spent in a later block.
Spent,
/// The output was never spent in any later block (it is a UTXO).
Unspent,
/// The status of the output is unknown (only if [`UtxoParser::load_filter`] was not called).
Unknown,
}
type ShortOutPoints = (Vec<ShortOutPoint>, Vec<ShortOutPoint>);
type ShortOutPointFilter = ScalableCuckooFilter<ShortOutPoint, DefaultHasher, FastRng>;
/// Multithreaded parser that returns a [`ParserIterator`] of [`UtxoBlock`].
/// * Tracks the [`Amount`] for every [`TxIn`].
/// * Tracks the [`OutputStatus`] for every [`TxOut`] if [`UtxoParser::load_filter`] is called.
///
/// # Examples
/// Computing the largest mining fee requires knowing the input amounts of every transaction.
/// Call [`UtxoParser::parse`] to get a [`UtxoBlock`] that tracks input amounts.
/// ```no_run
/// use std::cmp::max;
/// use bitcoin::Amount;
/// use bitcoin_block_parser::utxos::*;
///
/// let parser = UtxoParser::new("/home/user/.bitcoin/blocks/").unwrap();
/// let fees = parser.parse(|block| {
/// let mut max_mining_fee = Amount::ZERO;
/// for tx in block.txdata.into_iter() {
/// // For every transaction sum up the input and output amounts
/// let inputs: Amount = tx.input().map(|(_, amount)| *amount).sum();
/// let outputs: Amount = tx.output().map(|(out, _)| out.value).sum();
/// if !tx.transaction.is_coinbase() {
/// // Subtract outputs amount from inputs amount to get the fee
/// max_mining_fee = max(inputs - outputs, max_mining_fee);
/// }
/// }
/// max_mining_fee
/// });
/// println!("Maximum mining fee: {}", fees.max().unwrap());
/// ```
///
/// Computing the largest UTXO requires knowing the [`OutputStatus`] to determine whether a
/// [`TxOut`] was spent or unspent. Call [`UtxoParser::load_or_create_filter`] to track the output
/// status.
///
/// Although this takes longer to run the first time it also lowers the memory usage.
/// ```no_run
/// use std::cmp::max;
/// use bitcoin::Amount;
/// use bitcoin_block_parser::utxos::*;
///
/// let parser = UtxoParser::new("/home/user/.bitcoin/blocks/").unwrap();
/// let parser = parser.load_or_create_filter("filter.bin").unwrap();
/// let amounts = parser.parse(|block| {
/// let mut max_unspent_tx = Amount::ZERO;
/// for tx in block.txdata.into_iter() {
/// for (output, status) in tx.output() {
/// if status == &OutputStatus::Unspent {
/// max_unspent_tx = max(output.value, max_unspent_tx);
/// }
/// }
/// }
/// max_unspent_tx
/// });
/// println!("Maximum unspent output: {}", amounts.max().unwrap());
/// ```
pub struct UtxoParser {
/// Filter that contains all unspent transaction outpoints.
filter: Option<ShortOutPointFilter>,
/// Underlying parser for parsing the blocks.
parser: BlockParser,
/// Used to allocate the initial capacity of shared state.
estimated_utxos: usize,
}
impl UtxoParser {
/// Creates a new parser given the `blocks` directory where the `*.blk` files are located.
///
/// - Returns an `Err` if unable to parse the `blk` files.
/// - You can [specify the blocks directory](https://en.bitcoin.it/wiki/Data_directory) when
/// running `bitcoind`.
pub fn new(blocks_dir: &str) -> Result<Self> {
Self::new_with_opts(blocks_dir, ParserOptions::default())
}
/// Creates a parser with custom [`ParserOptions`].
pub fn new_with_opts(blocks_dir: &str, options: ParserOptions) -> Result<Self> {
Ok(Self {
filter: None,
parser: BlockParser::new_with_opts(blocks_dir, options)?,
estimated_utxos: 300_000_000,
})
}
/// Set the estimated amount of UTXOs in the range of blocks you are parsing.
///
/// Used to lower the memory usage of shared state objects.
pub fn estimated_utxos(mut self, estimated_utxos: usize) -> Self {
self.estimated_utxos = estimated_utxos;
self
}
/// Parse all [`UtxoBlock`] into type `T` and return a [`ParserIterator<T>`]. Results will
/// be in random order due to multithreading.
///
/// * `extract` - a closure that runs on multiple threads. For best performance perform as much
/// computation and data reduction here as possible.
pub fn parse<T: Send + 'static>(
self,
extract: impl Fn(UtxoBlock) -> T + Clone + Send + 'static,
) -> ParserIterator<T> {
// if using a filter we can save memory by reducing the initial hashmap capacity
let hashmap_capacity = if self.filter.is_some() {
self.estimated_utxos / 10
} else {
self.estimated_utxos
};
let pipeline = UtxoPipeline::new(self.filter, hashmap_capacity, extract);
self.parser
.parse(UtxoBlock::new)
.ordered()
.pipeline(&pipeline)
}
/// Set the height of the last block to parse.
///
/// Parsing always starts at the genesis block in order to track the transaction graph properly.
pub fn block_range_end(mut self, end: usize) -> Self {
self.parser = self.parser.block_range(0, end);
self
}
/// Loads a `filter_file` or creates a new one if it doesn't exist.
pub fn load_or_create_filter(self, filter_file: &str) -> Result<Self> {
if !fs::exists(filter_file)? {
self.create_filter(filter_file)?.load_filter(filter_file)
} else {
self.load_filter(filter_file)
}
}
/// Loads a `filter_file` or returns `Err` if it doesn't exist.
pub fn load_filter(mut self, filter_file: &str) -> Result<Self> {
if !fs::exists(filter_file)? {
bail!("Filter file '{}' doesn't exist", filter_file);
}
let reader = BufReader::new(File::open(filter_file)?);
let filter = bincode::deserialize_from(reader)?;
self.filter = Some(filter);
Ok(self)
}
/// Creates a new `filter_file`.
pub fn create_filter(self, filter_file: &str) -> Result<Self> {
info!("Creating '{}'", filter_file);
let filter = UtxoFilter::new(self.estimated_utxos);
self.parser
.parse(UtxoFilter::outpoints)
.ordered()
.map(&|outpoints| filter.update(outpoints))
.for_each(|_| {});
let filter = Arc::try_unwrap(filter.filter).expect("Arc still referenced");
let mut filter = Mutex::into_inner(filter)?;
filter.shrink_to_fit();
let writer = BufWriter::new(File::create(filter_file)?);
bincode::serialize_into(writer, &filter)?;
Ok(self)
}
}
/// Contains the filter data that tracks all unspent outputs in a memory-efficient manner.
#[derive(Clone)]
struct UtxoFilter {
filter: Arc<Mutex<ShortOutPointFilter>>,
}
impl UtxoFilter {
/// Construct with an initial `filter_capacity`.
fn new(filter_capacity: usize) -> UtxoFilter {
Self {
filter: Arc::new(Mutex::new(
ScalableCuckooFilterBuilder::default()
.initial_capacity(filter_capacity)
.false_positive_probability(0.000_000_000_001)
.rng(FastRng::default())
.finish(),
)),
}
}
/// Returns [`ShortOutPoint`] for all inputs and outputs.
fn outpoints(block: Block) -> ShortOutPoints {
let mut inputs = vec![];
let mut outputs = vec![];
for tx in block.txdata.iter() {
let txid = tx.compute_txid();
for input in &tx.input {
inputs.push(ShortOutPoint::from_outpoint(&input.previous_output));
}
for (index, _) in tx.output.iter().enumerate() {
outputs.push(ShortOutPoint::new(index, &txid));
}
}
(inputs, outputs)
}
/// Given the results of `outpoints()` update the filter.
pub fn update(&self, outpoints: ShortOutPoints) {
let mut filter = self.filter.lock().expect("Lock poisoned");
let (inputs, outputs) = outpoints;
for outpoint in outputs {
// insert outpoints for every output
filter.insert(&outpoint);
}
for input in inputs {
// remove outpoints that are spent in a subsequent transaction
filter.remove(&input);
}
}
}
/// Pipeline for multithreaded tracking of the input amounts and output statuses.
#[derive(Clone, Default)]
struct UtxoPipeline<F> {
/// Optional filter containing all unspent outpoints.
filter: Option<Arc<ShortOutPointFilter>>,
/// Tracks the amounts for every input.
amounts: Arc<DashMap<ShortOutPoint, Amount>>,
/// Extract function that maps the [`UtxoBlock`] to a new type
extract: F,
}
impl<F> UtxoPipeline<F> {
/// Construct a new pipeline with an optional `filter` and initial `hashmap_capacity`.
fn new(filter: Option<ShortOutPointFilter>, hashmap_capacity: usize, extract: F) -> Self {
Self {
filter: filter.map(Arc::new),
amounts: Arc::new(DashMap::with_capacity(hashmap_capacity)),
extract,
}
}
/// Returns the [`OutputStatus`] of an outpoint, returning [`OutputStatus::Unknown`] if running
/// without a filter.
fn status(&self, outpoint: &ShortOutPoint) -> OutputStatus {
match &self.filter {
None => OutputStatus::Unknown,
Some(filter) if filter.contains(outpoint) => OutputStatus::Unspent,
_ => OutputStatus::Spent,
}
}
}
impl<F, T> Pipeline<UtxoBlock, UtxoBlock, T> for UtxoPipeline<F>
where
F: Fn(UtxoBlock) -> T + Clone + Send + 'static,
{
fn first(&self, mut block: UtxoBlock) -> UtxoBlock {
for tx in &mut block.txdata {
for (index, output) in tx.transaction.output.iter().enumerate() {
let outpoint = ShortOutPoint::new(index, &tx.txid);
let status = self.status(&outpoint);
// if an outpoint is unspent we don't need to track it (saving memory)
if status != OutputStatus::Unspent {
self.amounts.insert(outpoint, output.value);
}
tx.outputs.push(status);
}
}
block
}
fn second(&self, mut block: UtxoBlock) -> T {
for tx in &mut block.txdata {
for input in tx.transaction.input.iter() {
if tx.transaction.is_coinbase() {
// coinbase transactions will not have a previous input
tx.inputs.push(Amount::ZERO);
} else {
let outpoint = ShortOutPoint::from_outpoint(&input.previous_output);
let (_, value) = self.amounts.remove(&outpoint).expect("Missing outpoint");
tx.inputs.push(value);
}
}
}
(self.extract)(block)
}
}
/// Shortened [`OutPoint`] to save memory (14 bytes instead of 36 bytes)
///
/// - 2 bytes represent far more than the maximum tx outputs (2^16)
/// - 12 byte subset of the txid is unlikely to generate collisions even with 1 billion txs (~6.3e-12)
#[derive(Eq, PartialEq, Hash, Debug, Clone)]
struct ShortOutPoint(pub Vec<u8>);
impl ShortOutPoint {
/// Shorten an existing [`OutPoint`].
fn from_outpoint(outpoint: &OutPoint) -> ShortOutPoint {
Self::new(outpoint.vout as usize, &outpoint.txid)
}
/// Create a new [`ShortOutPoint`] given its transaction id and index.
fn new(vout: usize, txid: &Txid) -> ShortOutPoint {
let mut bytes = vec![];
bytes.extend_from_slice(&vout.to_le_bytes()[0..2]);
bytes.extend_from_slice(&txid.as_byte_array()[0..12]);
ShortOutPoint(bytes)
}
}
/// Wrapper for [`SmallRng`] since it doesn't implement [`Default`] required to deserialize.
#[derive(Debug)]
struct FastRng(SmallRng);
impl Default for FastRng {
fn default() -> Self {
Self(SmallRng::seed_from_u64(0x2c76c58e13b3a812))
}
}
impl RngCore for FastRng {
fn next_u32(&mut self) -> u32 {
self.0.next_u32()
}
fn next_u64(&mut self) -> u64 {
self.0.next_u64()
}
fn fill_bytes(&mut self, dest: &mut [u8]) {
self.0.fill_bytes(dest)
}
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> std::result::Result<(), Error> {
self.0.try_fill_bytes(dest)
}
}