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"))]
19 #[cfg(feature = "rest-client")]
22 #[cfg(feature = "rpc-client")]
25 #[cfg(any(feature = "rest-client", feature = "rpc-client"))]
28 #[cfg(any(feature = "rest-client", feature = "rpc-client"))]
31 use bitcoin::blockdata::block::{Block, BlockHeader};
32 use bitcoin::hash_types::BlockHash;
33 use bitcoin::util::uint::Uint256;
35 use std::future::Future;
38 /// Abstract type for retrieving block headers and data.
39 pub trait BlockSource : Sync + Send {
40 /// Returns the header for a given hash. A height hint may be provided in case a block source
41 /// cannot easily find headers based on a hash. This is merely a hint and thus the returned
42 /// header must have the same hash as was requested. Otherwise, an error must be returned.
44 /// Implementations that cannot find headers based on the hash should return a `Transient` error
45 /// when `height_hint` is `None`.
46 fn get_header<'a>(&'a mut self, header_hash: &'a BlockHash, height_hint: Option<u32>) -> AsyncBlockSourceResult<'a, BlockHeaderData>;
48 /// Returns the block for a given hash. A headers-only block source should return a `Transient`
50 fn get_block<'a>(&'a mut self, header_hash: &'a BlockHash) -> AsyncBlockSourceResult<'a, Block>;
52 // TODO: Phrase in terms of `Poll` once added.
53 /// Returns the hash of the best block and, optionally, its height. When polling a block source,
54 /// the height is passed to `get_header` to allow for a more efficient lookup.
55 fn get_best_block<'a>(&'a mut self) -> AsyncBlockSourceResult<(BlockHash, Option<u32>)>;
58 /// Result type for `BlockSource` requests.
59 type BlockSourceResult<T> = Result<T, BlockSourceError>;
61 // TODO: Replace with BlockSourceResult once `async` trait functions are supported. For details,
62 // see: https://areweasyncyet.rs.
63 /// Result type for asynchronous `BlockSource` requests.
64 type AsyncBlockSourceResult<'a, T> = Pin<Box<dyn Future<Output = BlockSourceResult<T>> + 'a + Send>>;
66 /// Error type for `BlockSource` requests.
68 /// Transient errors may be resolved when re-polling, but no attempt will be made to re-poll on
69 /// persistent errors.
70 pub struct BlockSourceError {
71 kind: BlockSourceErrorKind,
72 error: Box<dyn std::error::Error + Send + Sync>,
75 /// The kind of `BlockSourceError`, either persistent or transient.
76 #[derive(Clone, Copy)]
77 pub enum BlockSourceErrorKind {
78 /// Indicates an error that won't resolve when retrying a request (e.g., invalid data).
81 /// Indicates an error that may resolve when retrying a request (e.g., unresponsive).
85 impl BlockSourceError {
86 /// Creates a new persistent error originated from the given error.
87 pub fn persistent<E>(error: E) -> Self
88 where E: Into<Box<dyn std::error::Error + Send + Sync>> {
90 kind: BlockSourceErrorKind::Persistent,
95 /// Creates a new transient error originated from the given error.
96 pub fn transient<E>(error: E) -> Self
97 where E: Into<Box<dyn std::error::Error + Send + Sync>> {
99 kind: BlockSourceErrorKind::Transient,
104 /// Returns the kind of error.
105 pub fn kind(&self) -> BlockSourceErrorKind {
109 /// Converts the error into the underlying error.
110 pub fn into_inner(self) -> Box<dyn std::error::Error + Send + Sync> {
115 /// A block header and some associated data. This information should be available from most block
116 /// sources (and, notably, is available in Bitcoin Core's RPC and REST interfaces).
117 #[derive(Clone, Copy, Debug, PartialEq)]
118 pub struct BlockHeaderData {
119 /// The block header itself.
120 pub header: BlockHeader,
122 /// The block height where the genesis block has height 0.
125 /// The total chain work in expected number of double-SHA256 hashes required to build a chain
126 /// of equivalent weight.
127 pub chainwork: Uint256,