pub struct BlockParser { /* private fields */ }
Expand description
Multithreaded parser for bitcoin::Block
.
§Examples
Call parse()
to run a Fn(Block) -> T
that returns a ParserIterator<T>
. The Fn
closure
runs on multiple threads.
use bitcoin_block_parser::blocks::*;
let parser = BlockParser::new("/home/user/.bitcoin/blocks/").unwrap();
let iterator = parser.parse(|block| block.total_size() as u64);
println!("Total blockchain size: {}", iterator.sum::<u64>());
You can call start_height()
to constrain the block range and ordered()
to ensure the
iterator returns blocks in height order:
use bitcoin_block_parser::blocks::*;
let parser = BlockParser::new("/home/user/.bitcoin/blocks/").unwrap();
let iterator = parser
.start_height(100_000)
.end_height(100_010)
.parse(|block| block.block_hash())
.ordered();
println!("In-order block hashes from 100,000 to 100,010:");
for block_hash in iterator {
println!("{}", block_hash);
}
Implementations§
Source§impl BlockParser
impl BlockParser
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 start_height(self, start_height: usize) -> Self
pub fn start_height(self, start_height: usize) -> Self
Sets the inclusive start of block heights to parse.
start_height
- must be less than the total number of blocks,0
will start at the genesis block.
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.
end_height
- the height to end at,usize::MAX
will stop at the last block available.
Sourcepub fn parse<T: Send + 'static>(
&self,
extract: impl Fn(Block) -> T + Clone + Send + 'static,
) -> ParserIterator<T> ⓘ
pub fn parse<T: Send + 'static>( &self, extract: impl Fn(Block) -> T + Clone + Send + 'static, ) -> ParserIterator<T> ⓘ
Parse all bitcoin::Block
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.
Trait Implementations§
Source§impl Clone for BlockParser
impl Clone for BlockParser
Source§fn clone(&self) -> BlockParser
fn clone(&self) -> BlockParser
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreAuto Trait Implementations§
impl Freeze for BlockParser
impl RefUnwindSafe for BlockParser
impl Send for BlockParser
impl Sync for BlockParser
impl Unpin for BlockParser
impl UnwindSafe for BlockParser
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more