pub struct UtxoParser { /* private fields */ }
Expand description
Multithreaded parser that returns a ParserIterator
of UtxoBlock
- Tracks the
TxOut
of everyTxIn
- Tracks the
OutputStatus
for everyTxOut
§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
impl UtxoParser
Sourcepub fn new(blocks_dir: &str, filter_file: &str) -> Self
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
.
Sourcepub fn estimated_utxos(self, estimated_utxos: usize) -> Self
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.
Sourcepub fn end_height(self, end_height: usize) -> Self
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.
Sourcepub fn with_opts(self, options: ParserOptions) -> Self
pub fn with_opts(self, options: ParserOptions) -> Self
Creates a parser with custom ParserOptions
.
Sourcepub fn parse<T: Send + 'static>(
self,
extract: impl Fn(UtxoBlock) -> T + Clone + Send + 'static,
) -> Result<ParserIterator<T>>
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.
Sourcepub fn create_filter(&self) -> Result<Self>
pub fn create_filter(&self) -> Result<Self>
Force the creation of a new filter_file
.
Trait Implementations§
Source§impl Clone for UtxoParser
impl Clone for UtxoParser
Source§fn clone(&self) -> UtxoParser
fn clone(&self) -> UtxoParser
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more