1 //! A lightweight client for keeping in sync with chain activity.
3 //! Defines a [`BlockSource`] trait, which is an asynchronous interface for retrieving block headers
6 //! Enabling feature `rest-client` or `rpc-client` allows configuring the client to fetch blocks
7 //! using Bitcoin Core's REST or RPC interface, respectively.
9 //! Both features support either blocking I/O using `std::net::TcpStream` or, with feature `tokio`,
10 //! non-blocking I/O using `tokio::net::TcpStream` from inside a Tokio runtime.
12 //! [`BlockSource`]: trait.BlockSource.html
14 #[cfg(any(feature = "rest-client", feature = "rpc-client"))]
17 #[cfg(feature = "rest-client")]
20 #[cfg(feature = "rpc-client")]
23 #[cfg(any(feature = "rest-client", feature = "rpc-client"))]
26 #[cfg(any(feature = "rest-client", feature = "rpc-client"))]
29 use bitcoin::blockdata::block::{Block, BlockHeader};
30 use bitcoin::hash_types::BlockHash;
31 use bitcoin::util::uint::Uint256;
33 use std::future::Future;
36 /// Abstract type for retrieving block headers and data.
37 pub trait BlockSource : Sync + Send {
38 /// Returns the header for a given hash. A height hint may be provided in case a block source
39 /// cannot easily find headers based on a hash. This is merely a hint and thus the returned
40 /// header must have the same hash as was requested. Otherwise, an error must be returned.
42 /// Implementations that cannot find headers based on the hash should return a `Transient` error
43 /// when `height_hint` is `None`.
44 fn get_header<'a>(&'a mut self, header_hash: &'a BlockHash, height_hint: Option<u32>) -> AsyncBlockSourceResult<'a, BlockHeaderData>;
46 /// Returns the block for a given hash. A headers-only block source should return a `Transient`
48 fn get_block<'a>(&'a mut self, header_hash: &'a BlockHash) -> AsyncBlockSourceResult<'a, Block>;
50 // TODO: Phrase in terms of `Poll` once added.
51 /// Returns the hash of the best block and, optionally, its height. When polling a block source,
52 /// the height is passed to `get_header` to allow for a more efficient lookup.
53 fn get_best_block<'a>(&'a mut self) -> AsyncBlockSourceResult<(BlockHash, Option<u32>)>;
56 /// Result type for `BlockSource` requests.
57 type BlockSourceResult<T> = Result<T, BlockSourceError>;
59 // TODO: Replace with BlockSourceResult once `async` trait functions are supported. For details,
60 // see: https://areweasyncyet.rs.
61 /// Result type for asynchronous `BlockSource` requests.
62 type AsyncBlockSourceResult<'a, T> = Pin<Box<dyn Future<Output = BlockSourceResult<T>> + 'a + Send>>;
64 /// Error type for `BlockSource` requests.
66 /// Transient errors may be resolved when re-polling, but no attempt will be made to re-poll on
67 /// persistent errors.
68 pub struct BlockSourceError {
69 kind: BlockSourceErrorKind,
70 error: Box<dyn std::error::Error + Send + Sync>,
73 /// The kind of `BlockSourceError`, either persistent or transient.
74 #[derive(Clone, Copy)]
75 pub enum BlockSourceErrorKind {
76 /// Indicates an error that won't resolve when retrying a request (e.g., invalid data).
79 /// Indicates an error that may resolve when retrying a request (e.g., unresponsive).
83 impl BlockSourceError {
84 /// Creates a new persistent error originated from the given error.
85 pub fn persistent<E>(error: E) -> Self
86 where E: Into<Box<dyn std::error::Error + Send + Sync>> {
88 kind: BlockSourceErrorKind::Persistent,
93 /// Creates a new transient error originated from the given error.
94 pub fn transient<E>(error: E) -> Self
95 where E: Into<Box<dyn std::error::Error + Send + Sync>> {
97 kind: BlockSourceErrorKind::Transient,
102 /// Returns the kind of error.
103 pub fn kind(&self) -> BlockSourceErrorKind {
107 /// Converts the error into the underlying error.
108 pub fn into_inner(self) -> Box<dyn std::error::Error + Send + Sync> {
113 /// A block header and some associated data. This information should be available from most block
114 /// sources (and, notably, is available in Bitcoin Core's RPC and REST interfaces).
115 #[derive(Clone, Copy, Debug, PartialEq)]
116 pub struct BlockHeaderData {
117 /// The block header itself.
118 pub header: BlockHeader,
120 /// The block height where the genesis block has height 0.
123 /// The total chain work in expected number of double-SHA256 hashes required to build a chain
124 /// of equivalent weight.
125 pub chainwork: Uint256,