pub struct UtxoParser { /* private fields */ }
Expand description
Multithreaded parser that returns a ParserIterator
of UtxoBlock
.
- Tracks the
Amount
for everyTxIn
. - Tracks the
OutputStatus
for everyTxOut
ifUtxoParser::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.
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().map_parallel(|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 blocks = parser.load_or_create_filter("filter.bin").unwrap().parse();
let amounts = blocks.map_parallel(|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
impl UtxoParser
Sourcepub fn new(blocks_dir: &str) -> Result<Self>
pub fn new(blocks_dir: &str) -> Result<Self>
Creates a new parser given the blocks
directory where the *.blk
files are located.
- Returns an
Err
if unable to parse theblk
files. - You can specify the blocks directory when
running
bitcoind
.
Sourcepub fn new_with_opts(blocks_dir: &str, options: ParserOptions) -> Result<Self>
pub fn new_with_opts(blocks_dir: &str, options: ParserOptions) -> Result<Self>
Creates a parser with custom ParserOptions
.
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 parse(self) -> ParserIterator<UtxoBlock> ⓘ
pub fn parse(self) -> ParserIterator<UtxoBlock> ⓘ
Parse the blocks into an iterator of UtxoBlock
.
Sourcepub fn block_range_end(self, end: usize) -> Self
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.
Sourcepub fn load_or_create_filter(self, filter_file: &str) -> Result<Self>
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.
Sourcepub fn load_filter(self, filter_file: &str) -> Result<Self>
pub fn load_filter(self, filter_file: &str) -> Result<Self>
Loads a filter_file
or returns Err
if it doesn’t exist.
Sourcepub fn create_filter(self, filter_file: &str) -> Result<Self>
pub fn create_filter(self, filter_file: &str) -> Result<Self>
Creates a new filter_file
.