Write the docs for blockchain::*
This commit is contained in:
@@ -22,6 +22,22 @@
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
//! Blockchain backends
|
||||
//!
|
||||
//! This module provides the implementation of a few commonly-used backends like
|
||||
//! [Electrum](crate::blockchain::electrum), [Esplora](crate::blockchain::esplora) and
|
||||
//! [Compact Filters/Neutrino](crate::blockchain::compact_filters), along with two generalized
|
||||
//! traits [`Blockchain`] and [`OnlineBlockchain`] that can be implemented to build customized
|
||||
//! backends.
|
||||
//!
|
||||
//! Types that only implement the [`Blockchain`] trait can be used as backends for [`Wallet`](crate::wallet::Wallet)s, but any
|
||||
//! action that requires interacting with the blockchain won't be available ([`Wallet::sync`](crate::wallet::Wallet::sync) and
|
||||
//! [`Wallet::broadcast`](crate::wallet::Wallet::broadcast)). This allows the creation of physically air-gapped wallets, that have no
|
||||
//! ability to contact the outside world. An example of an offline-only client is [`OfflineBlockchain`].
|
||||
//!
|
||||
//! Types that also implement [`OnlineBlockchain`] will make the two aforementioned actions
|
||||
//! available.
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::ops::Deref;
|
||||
use std::sync::mpsc::{channel, Receiver, Sender};
|
||||
@@ -53,19 +69,40 @@ pub mod compact_filters;
|
||||
#[cfg(feature = "compact_filters")]
|
||||
pub use self::compact_filters::CompactFiltersBlockchain;
|
||||
|
||||
/// Capabilities that can be supported by an [`OnlineBlockchain`] backend
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum Capability {
|
||||
/// Can recover the full history of a wallet and not only the set of currently spendable UTXOs
|
||||
FullHistory,
|
||||
/// Can fetch any historical transaction given its txid
|
||||
GetAnyTx,
|
||||
/// Can compute accurate fees for the transactions found during sync
|
||||
AccurateFees,
|
||||
}
|
||||
|
||||
/// Base trait for a blockchain backend
|
||||
///
|
||||
/// This trait is always required, even for "air-gapped" backends that don't actually make any
|
||||
/// external call. Clients that have the ability to make external calls must also implement `OnlineBlockchain`.
|
||||
pub trait Blockchain {
|
||||
/// Return whether or not the client has the ability to fullfill requests
|
||||
///
|
||||
/// This should always be `false` for offline-only types, and can be true for types that also
|
||||
/// implement [`OnlineBlockchain`], if they have the ability to fullfill requests.
|
||||
fn is_online(&self) -> bool;
|
||||
|
||||
/// Create a new instance of the client that is offline-only
|
||||
///
|
||||
/// For types that also implement [`OnlineBlockchain`], this means creating an instance that
|
||||
/// returns [`Error::OfflineClient`](crate::error::Error::OfflineClient) if any of the "online"
|
||||
/// methods are called.
|
||||
///
|
||||
/// This is generally implemented by wrapping the client in an [`Option`] that has [`Option::None`] value
|
||||
/// when created with this method, and is [`Option::Some`] if properly instantiated.
|
||||
fn offline() -> Self;
|
||||
}
|
||||
|
||||
/// Type that only implements [`Blockchain`] and is always offline
|
||||
pub struct OfflineBlockchain;
|
||||
impl Blockchain for OfflineBlockchain {
|
||||
fn offline() -> Self {
|
||||
@@ -77,16 +114,47 @@ impl Blockchain for OfflineBlockchain {
|
||||
}
|
||||
}
|
||||
|
||||
/// Trait that defines the actions that must be supported by an online [`Blockchain`]
|
||||
#[maybe_async]
|
||||
pub trait OnlineBlockchain: Blockchain {
|
||||
/// Return the set of [`Capability`] supported by this backend
|
||||
fn get_capabilities(&self) -> HashSet<Capability>;
|
||||
|
||||
/// Setup the backend and populate the internal database for the first time
|
||||
///
|
||||
/// This method is the equivalent of [`OnlineBlockchain::sync`], but it's guaranteed to only be
|
||||
/// called once, at the first [`Wallet::sync`](crate::wallet::Wallet::sync).
|
||||
///
|
||||
/// The rationale behind the distinction between `sync` and `setup` is that some custom backends
|
||||
/// might need to perform specific actions only the first time they are synced.
|
||||
///
|
||||
/// For types that do not have that distinction, only this method can be implemented, since
|
||||
/// [`OnlineBlockchain::sync`] defaults to calling this internally if not overridden.
|
||||
fn setup<D: BatchDatabase, P: 'static + Progress>(
|
||||
&self,
|
||||
stop_gap: Option<usize>,
|
||||
database: &mut D,
|
||||
progress_update: P,
|
||||
) -> Result<(), Error>;
|
||||
/// Populate the internal database with transactions and UTXOs
|
||||
///
|
||||
/// If not overridden, it defaults to calling [`OnlineBlockchain::setup`] internally.
|
||||
///
|
||||
/// This method should implement the logic required to iterate over the list of the wallet's
|
||||
/// script_pubkeys using [`Database::iter_script_pubkeys`] and look for relevant transactions
|
||||
/// in the blockchain to populate the database with [`BatchOperations::set_tx`] and
|
||||
/// [`BatchOperations::set_utxo`].
|
||||
///
|
||||
/// This method should also take care of removing UTXOs that are seen as spent in the
|
||||
/// blockchain, using [`BatchOperations::del_utxo`].
|
||||
///
|
||||
/// The `progress_update` object can be used to give the caller updates about the progress by using
|
||||
/// [`Progress::update`].
|
||||
///
|
||||
/// [`Database::iter_script_pubkeys`]: crate::database::Database::iter_script_pubkeys
|
||||
/// [`BatchOperations::set_tx`]: crate::database::BatchOperations::set_tx
|
||||
/// [`BatchOperations::set_utxo`]: crate::database::BatchOperations::set_utxo
|
||||
/// [`BatchOperations::del_utxo`]: crate::database::BatchOperations::del_utxo
|
||||
fn sync<D: BatchDatabase, P: 'static + Progress>(
|
||||
&self,
|
||||
stop_gap: Option<usize>,
|
||||
@@ -96,19 +164,31 @@ pub trait OnlineBlockchain: Blockchain {
|
||||
maybe_await!(self.setup(stop_gap, database, progress_update))
|
||||
}
|
||||
|
||||
/// Fetch a transaction from the blockchain given its txid
|
||||
fn get_tx(&self, txid: &Txid) -> Result<Option<Transaction>, Error>;
|
||||
/// Broadcast a transaction
|
||||
fn broadcast(&self, tx: &Transaction) -> Result<(), Error>;
|
||||
|
||||
/// Return the current height
|
||||
fn get_height(&self) -> Result<u32, Error>;
|
||||
/// Estimate the fee rate required to confirm a transaction in a given `target` of blocks
|
||||
fn estimate_fee(&self, target: usize) -> Result<FeeRate, Error>;
|
||||
}
|
||||
|
||||
/// Data sent with a progress update over a [`channel`]
|
||||
pub type ProgressData = (f32, Option<String>);
|
||||
|
||||
/// Trait for types that can receive and process progress updates during [`OnlineBlockchain::sync`] and
|
||||
/// [`OnlineBlockchain::setup`]
|
||||
pub trait Progress: Send {
|
||||
/// Send a new progress update
|
||||
///
|
||||
/// The `progress` value should be in the range 0.0 - 100.0, and the `message` value is an
|
||||
/// optional text message that can be displayed to the user.
|
||||
fn update(&self, progress: f32, message: Option<String>) -> Result<(), Error>;
|
||||
}
|
||||
|
||||
/// Shortcut to create a [`channel`] (pair of [`Sender`] and [`Receiver`]) that can transport [`ProgressData`]
|
||||
pub fn progress() -> (Sender<ProgressData>, Receiver<ProgressData>) {
|
||||
channel()
|
||||
}
|
||||
@@ -124,9 +204,11 @@ impl Progress for Sender<ProgressData> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Type that implements [`Progress`] and drops every update received
|
||||
#[derive(Clone)]
|
||||
pub struct NoopProgress;
|
||||
|
||||
/// Create a new instance of [`NoopProgress`]
|
||||
pub fn noop_progress() -> NoopProgress {
|
||||
NoopProgress
|
||||
}
|
||||
@@ -137,9 +219,11 @@ impl Progress for NoopProgress {
|
||||
}
|
||||
}
|
||||
|
||||
/// Type that implements [`Progress`] and logs at level `INFO` every update received
|
||||
#[derive(Clone)]
|
||||
pub struct LogProgress;
|
||||
|
||||
/// Create a nwe instance of [`LogProgress`]
|
||||
pub fn log_progress() -> LogProgress {
|
||||
LogProgress
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user