|
| 1 | +//! A lightweight client for keeping in sync with chain activity. |
| 2 | +//! |
| 3 | +//! Defines a [`BlockSource`] trait, which is an asynchronous interface for retrieving block headers |
| 4 | +//! and data. |
| 5 | +//! |
| 6 | +//! [`BlockSource`]: trait.BlockSource.html |
| 7 | +
|
| 8 | +use bitcoin::blockdata::block::{Block, BlockHeader}; |
| 9 | +use bitcoin::hash_types::BlockHash; |
| 10 | +use bitcoin::util::uint::Uint256; |
| 11 | + |
| 12 | +use std::future::Future; |
| 13 | +use std::pin::Pin; |
| 14 | + |
| 15 | +/// Abstract type for retrieving block headers and data. |
| 16 | +pub trait BlockSource : Sync + Send { |
| 17 | + /// Returns the header for a given hash. A height hint may be provided in case a source cannot |
| 18 | + /// easily find headers based on a hash. This is merely a hint and thus the returned header must |
| 19 | + /// have the same hash as was requested. Otherwise, an error must be returned. |
| 20 | + /// |
| 21 | + /// Implementations that cannot find headers based on the hash should return a `Transient` error |
| 22 | + /// when `height_hint` is `None`. In such a case, `get_best_block` should never return `None` |
| 23 | + /// for the height. Otherwise, the source could not be used independently for an initial sync. |
| 24 | + fn get_header<'a>(&'a mut self, header_hash: &'a BlockHash, height_hint: Option<u32>) -> AsyncBlockSourceResult<'a, BlockHeaderData>; |
| 25 | + |
| 26 | + /// Returns the block for a given hash. A headers-only block source should return a `Transient` |
| 27 | + /// error. |
| 28 | + fn get_block<'a>(&'a mut self, header_hash: &'a BlockHash) -> AsyncBlockSourceResult<'a, Block>; |
| 29 | + |
| 30 | + /// Returns the hash of the best block and, optionally, its height. The height is passed to |
| 31 | + /// `get_header` to allow a more efficient lookup on some block sources. |
| 32 | + fn get_best_block<'a>(&'a mut self) -> AsyncBlockSourceResult<(BlockHash, Option<u32>)>; |
| 33 | +} |
| 34 | + |
| 35 | +/// Result type for `BlockSource` requests. |
| 36 | +type BlockSourceResult<T> = Result<T, BlockSourceError>; |
| 37 | + |
| 38 | +/// Result type for asynchronous `BlockSource` requests. |
| 39 | +/// |
| 40 | +/// TODO: Replace with BlockSourceResult once `async` trait functions are supported. For details, |
| 41 | +/// see: https://areweasyncyet.rs. |
| 42 | +type AsyncBlockSourceResult<'a, T> = Pin<Box<dyn Future<Output = BlockSourceResult<T>> + 'a + Send>>; |
| 43 | + |
| 44 | +/// Error type for `BlockSource` requests. |
| 45 | +/// |
| 46 | +/// Transient errors may be resolved when re-polling, but no attempt will be made to re-poll on |
| 47 | +/// persistent errors. |
| 48 | +#[derive(Clone, Copy, Debug, PartialEq)] |
| 49 | +pub enum BlockSourceError { |
| 50 | + /// Indicates an error that won't resolve when retrying a request (e.g., invalid data). |
| 51 | + Persistent, |
| 52 | + |
| 53 | + /// Indicates an error that may resolve when retrying a request (e.g., unresponsive). |
| 54 | + Transient, |
| 55 | +} |
| 56 | + |
| 57 | +/// A block header and some associated data. This information should be available from most block |
| 58 | +/// sources (and, notably, is available in Bitcoin Core's RPC and REST interfaces). |
| 59 | +#[derive(Clone, Copy, Debug, PartialEq)] |
| 60 | +pub struct BlockHeaderData { |
| 61 | + /// The block header itself. |
| 62 | + pub header: BlockHeader, |
| 63 | + |
| 64 | + /// The block height where the genesis block has height 0. |
| 65 | + pub height: u32, |
| 66 | + |
| 67 | + /// The total chain work in expected number of double-SHA256 hashes required to build a chain |
| 68 | + /// of equivalent weight. |
| 69 | + pub chainwork: Uint256, |
| 70 | +} |
0 commit comments