bitcoin_block_parser::utxos

Struct UtxoParser

Source
pub struct UtxoParser { /* private fields */ }
Expand description

Multithreaded parser that returns a ParserIterator of UtxoBlock.

§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.

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.

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());

Implementations§

Source§

impl UtxoParser

Source

pub fn new(blocks_dir: &str) -> Result<Self>

Creates a new parser given the blocks directory where the *.blk files are located.

Source

pub fn new_with_opts(blocks_dir: &str, options: ParserOptions) -> Result<Self>

Creates a parser with custom ParserOptions.

Source

pub fn estimated_utxos(self, estimated_utxos: usize) -> Self

Set the estimated amount of UTXOs in the range of blocks you are parsing.

Used to lower the memory usage of shared state objects.

Source

pub fn parse<T: Send + 'static>( self, extract: impl Fn(UtxoBlock) -> T + Clone + Send + 'static, ) -> ParserIterator<T>

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.
Source

pub fn block_range_end(self, end: usize) -> Self

Set the height of the last block to parse.

Parsing always starts at the genesis block in order to track the transaction graph properly.

Source

pub fn load_or_create_filter(self, filter_file: &str) -> Result<Self>

Loads a filter_file or creates a new one if it doesn’t exist.

Source

pub fn load_filter(self, filter_file: &str) -> Result<Self>

Loads a filter_file or returns Err if it doesn’t exist.

Source

pub fn create_filter(self, filter_file: &str) -> Result<Self>

Creates a new filter_file.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V