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§
sourcefn extract(&self, block: Block) -> Option<B>
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.
sourcefn batch(&self, items: Vec<B>) -> Vec<C>
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§
sourcefn options() -> Options
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
.
sourcefn parse(&self, headers: &[ParsedHeader]) -> Receiver<Result<C>>
fn parse(&self, headers: &[ParsedHeader]) -> Receiver<Result<C>>
Parse all the blocks represented by the headers.
sourcefn parse_ordered(&self, headers: &[ParsedHeader]) -> Receiver<Result<C>>
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.
sourcefn parse_with_opts(
&self,
headers: &[ParsedHeader],
opts: Options,
) -> Receiver<Result<C>>
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.