Trait bitcoin_block_parser::blocks::BlockParser

source ·
pub trait BlockParser<B: Send + 'static, C: Send + 'static>:
    Clone
    + Send
    + 'static {
    // Required methods
    fn extract(&self, block: Block) -> Option<B>;
    fn batch(&self, items: Vec<B>) -> Vec<C>;

    // Provided methods
    fn options() -> Options { ... }
    fn parse(&self, headers: &[ParsedHeader]) -> Receiver<Result<C>> { ... }
    fn parse_ordered(&self, headers: &[ParsedHeader]) -> Receiver<Result<C>> { ... }
    fn parse_with_opts(
        &self,
        headers: &[ParsedHeader],
        opts: Options,
    ) -> Receiver<Result<C>> { ... }
    fn send_batch(&self, tx_c: &Sender<Result<C>>, batch: Result<Vec<B>>) { ... }
}
Expand description

Implement this trait to create a custom Block parser.

Required Methods§

source

fn extract(&self, block: Block) -> Option<B>

Extracts the data you need from the block.

If you can keep B small or return None you will gain memory/speed performance. Always runs on blocks out-of-order using multiple threads so put compute-heavy code in here.

source

fn batch(&self, items: Vec<B>) -> Vec<C>

Runs on batches of the BlockParser::extract to return the final results.

We batch to reduce contention in the case the parser needs to modify shared state by locking an Arc<Mutex<_>>. Use Options::batch_size if you need to tune the number of the items.

Provided Methods§

source

fn options() -> Options

The default Options that this parser will use.

Generally you will want to call BlockParser::options if you need to modify the default options, then pass them into BlockParser::parse_with_opts.

source

fn parse(&self, headers: &[ParsedHeader]) -> Receiver<Result<C>>

Parse all the blocks represented by the headers.

source

fn parse_ordered(&self, headers: &[ParsedHeader]) -> Receiver<Result<C>>

Parse all the blocks represented by the headers, ensuring the results C are returned in the same order the ParsedHeader were passed in.

Note that by ordering the results BlockParser::batch will run on a single thread instead of multiple which could affect performance.

source

fn parse_with_opts( &self, headers: &[ParsedHeader], opts: Options, ) -> Receiver<Result<C>>

Allows users to pass in custom Options in case they need to reduce memory usage or otherwise tune performance for their system.

source

fn send_batch(&self, tx_c: &Sender<Result<C>>, batch: Result<Vec<B>>)

Helper function for sending batch results in a channel

Object Safety§

This trait is not object safe.

Implementors§