UtxoParser

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/", "filter.bin");
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(|(_, out)| out.value).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
}).unwrap();
println!("Maximum mining fee: {}", fees.max().unwrap());

Computing the largest UTXO requires knowing the OutputStatus to determine whether a TxOut was spent or unspent.

use std::cmp::max;
use bitcoin::Amount;
use bitcoin_block_parser::utxos::*;

let parser = UtxoParser::new("/home/user/.bitcoin/blocks/", "filter.bin");
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
}).unwrap();
println!("Maximum unspent output: {}", amounts.max().unwrap());

Implementations§

Source§

impl UtxoParser

Source

pub fn new(blocks_dir: &str, filter_file: &str) -> Self

Creates a new parser.

  • blocks_dir - directory where the *.blk files are located.
  • filter_file - file that will store the UTXO filter.

Returns an Err if unable to parse the blk files. You can specify the blocks directory when running bitcoind.

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 end_height(self, end_height: usize) -> Self

Sets the inclusive end of block heights to parse. Parsing always starts at the genesis block in order to track the transaction graph properly.

  • end_height - the height to end at, usize::MAX will stop at the last block available.
Source

pub fn with_opts(self, options: ParserOptions) -> Self

Creates a parser with custom ParserOptions.

Source

pub fn parse<T: Send + 'static>( self, extract: impl Fn(UtxoBlock) -> T + Clone + Send + 'static, ) -> Result<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 create_filter(&self) -> Result<Self>

Force the creation of a new filter_file.

Trait Implementations§

Source§

impl Clone for UtxoParser

Source§

fn clone(&self) -> UtxoParser

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for UtxoParser

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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