2021-03-03 13:22:05 -08:00
|
|
|
// Bitcoin Dev Kit
|
|
|
|
// Written in 2020 by Alekos Filini <alekos.filini@gmail.com>
|
2020-08-31 11:26:36 +02:00
|
|
|
//
|
2021-03-03 13:22:05 -08:00
|
|
|
// Copyright (c) 2020-2021 Bitcoin Dev Kit Developers
|
2020-08-31 11:26:36 +02:00
|
|
|
//
|
2021-03-03 13:22:05 -08:00
|
|
|
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
|
|
|
|
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
|
|
|
|
// You may not use this file except in accordance with one or both of these
|
|
|
|
// licenses.
|
2020-08-31 11:26:36 +02:00
|
|
|
|
2020-09-03 11:36:07 +02:00
|
|
|
//! Blockchain backends
|
|
|
|
//!
|
|
|
|
//! This module provides the implementation of a few commonly-used backends like
|
|
|
|
//! [Electrum](crate::blockchain::electrum), [Esplora](crate::blockchain::esplora) and
|
2020-09-09 18:17:49 +02:00
|
|
|
//! [Compact Filters/Neutrino](crate::blockchain::compact_filters), along with a generalized trait
|
|
|
|
//! [`Blockchain`] that can be implemented to build customized backends.
|
2020-09-03 11:36:07 +02:00
|
|
|
|
2020-05-03 16:15:11 +02:00
|
|
|
use std::collections::HashSet;
|
2020-08-17 10:53:38 +02:00
|
|
|
use std::ops::Deref;
|
2020-05-03 16:15:11 +02:00
|
|
|
use std::sync::mpsc::{channel, Receiver, Sender};
|
2020-08-17 10:53:38 +02:00
|
|
|
use std::sync::Arc;
|
2020-05-03 16:15:11 +02:00
|
|
|
|
|
|
|
use bitcoin::{Transaction, Txid};
|
|
|
|
|
2020-08-31 10:49:44 +02:00
|
|
|
use crate::database::BatchDatabase;
|
2020-05-03 16:15:11 +02:00
|
|
|
use crate::error::Error;
|
2020-08-07 11:23:01 +02:00
|
|
|
use crate::FeeRate;
|
2020-05-03 16:15:11 +02:00
|
|
|
|
2020-11-16 12:18:34 +01:00
|
|
|
#[cfg(any(feature = "electrum", feature = "esplora"))]
|
2020-08-31 10:49:44 +02:00
|
|
|
pub(crate) mod utils;
|
2020-05-07 15:14:05 +02:00
|
|
|
|
2020-09-10 18:11:24 +02:00
|
|
|
#[cfg(any(feature = "electrum", feature = "esplora", feature = "compact_filters"))]
|
|
|
|
pub mod any;
|
|
|
|
#[cfg(any(feature = "electrum", feature = "esplora", feature = "compact_filters"))]
|
|
|
|
pub use any::{AnyBlockchain, AnyBlockchainConfig};
|
|
|
|
|
2020-05-03 16:15:11 +02:00
|
|
|
#[cfg(feature = "electrum")]
|
2020-08-31 10:49:44 +02:00
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "electrum")))]
|
2020-05-03 16:15:11 +02:00
|
|
|
pub mod electrum;
|
|
|
|
#[cfg(feature = "electrum")]
|
|
|
|
pub use self::electrum::ElectrumBlockchain;
|
2020-10-28 21:34:04 -07:00
|
|
|
#[cfg(feature = "electrum")]
|
|
|
|
pub use self::electrum::ElectrumBlockchainConfig;
|
2020-05-03 16:15:11 +02:00
|
|
|
|
2021-05-17 17:20:32 +02:00
|
|
|
#[cfg(feature = "rpc")]
|
2021-07-09 09:11:02 +02:00
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "rpc")))]
|
2021-05-17 17:20:32 +02:00
|
|
|
pub mod rpc;
|
|
|
|
#[cfg(feature = "rpc")]
|
|
|
|
pub use self::rpc::RpcBlockchain;
|
|
|
|
#[cfg(feature = "rpc")]
|
|
|
|
pub use self::rpc::RpcConfig;
|
|
|
|
|
2020-05-07 15:14:05 +02:00
|
|
|
#[cfg(feature = "esplora")]
|
2020-08-31 10:49:44 +02:00
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "esplora")))]
|
2020-05-07 15:14:05 +02:00
|
|
|
pub mod esplora;
|
|
|
|
#[cfg(feature = "esplora")]
|
|
|
|
pub use self::esplora::EsploraBlockchain;
|
|
|
|
|
2020-08-25 16:07:26 +02:00
|
|
|
#[cfg(feature = "compact_filters")]
|
2020-08-31 10:49:44 +02:00
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "compact_filters")))]
|
2020-08-25 16:07:26 +02:00
|
|
|
pub mod compact_filters;
|
2021-05-17 17:20:32 +02:00
|
|
|
|
2020-08-31 10:49:44 +02:00
|
|
|
#[cfg(feature = "compact_filters")]
|
|
|
|
pub use self::compact_filters::CompactFiltersBlockchain;
|
2020-08-25 16:07:26 +02:00
|
|
|
|
2020-09-09 18:17:49 +02:00
|
|
|
/// Capabilities that can be supported by a [`Blockchain`] backend
|
2020-05-03 16:15:11 +02:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
|
|
pub enum Capability {
|
2020-09-03 11:36:07 +02:00
|
|
|
/// Can recover the full history of a wallet and not only the set of currently spendable UTXOs
|
2020-05-03 16:15:11 +02:00
|
|
|
FullHistory,
|
2020-09-03 11:36:07 +02:00
|
|
|
/// Can fetch any historical transaction given its txid
|
2020-05-03 16:15:11 +02:00
|
|
|
GetAnyTx,
|
2020-09-03 11:36:07 +02:00
|
|
|
/// Can compute accurate fees for the transactions found during sync
|
2020-08-25 16:07:26 +02:00
|
|
|
AccurateFees,
|
2020-05-03 16:15:11 +02:00
|
|
|
}
|
|
|
|
|
2020-09-09 18:17:49 +02:00
|
|
|
/// Trait that defines the actions that must be supported by a blockchain backend
|
2020-07-20 15:51:57 +02:00
|
|
|
#[maybe_async]
|
2020-12-23 13:48:17 +11:00
|
|
|
pub trait Blockchain {
|
2020-09-03 11:36:07 +02:00
|
|
|
/// Return the set of [`Capability`] supported by this backend
|
2020-07-15 18:49:24 +02:00
|
|
|
fn get_capabilities(&self) -> HashSet<Capability>;
|
2020-05-03 16:15:11 +02:00
|
|
|
|
2020-09-03 11:36:07 +02:00
|
|
|
/// Setup the backend and populate the internal database for the first time
|
|
|
|
///
|
2020-09-09 18:17:49 +02:00
|
|
|
/// This method is the equivalent of [`Blockchain::sync`], but it's guaranteed to only be
|
2020-09-03 11:36:07 +02:00
|
|
|
/// 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
|
2020-09-09 18:17:49 +02:00
|
|
|
/// [`Blockchain::sync`] defaults to calling this internally if not overridden.
|
2020-08-31 10:49:44 +02:00
|
|
|
fn setup<D: BatchDatabase, P: 'static + Progress>(
|
2020-08-06 10:44:40 +02:00
|
|
|
&self,
|
2020-05-03 16:15:11 +02:00
|
|
|
database: &mut D,
|
|
|
|
progress_update: P,
|
|
|
|
) -> Result<(), Error>;
|
2020-09-03 11:36:07 +02:00
|
|
|
/// Populate the internal database with transactions and UTXOs
|
|
|
|
///
|
2020-09-09 18:17:49 +02:00
|
|
|
/// If not overridden, it defaults to calling [`Blockchain::setup`] internally.
|
2020-09-03 11:36:07 +02:00
|
|
|
///
|
|
|
|
/// 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
|
2020-08-31 10:49:44 +02:00
|
|
|
fn sync<D: BatchDatabase, P: 'static + Progress>(
|
2020-08-06 10:44:40 +02:00
|
|
|
&self,
|
2020-05-03 16:15:11 +02:00
|
|
|
database: &mut D,
|
|
|
|
progress_update: P,
|
|
|
|
) -> Result<(), Error> {
|
2021-07-15 12:04:03 -07:00
|
|
|
maybe_await!(self.setup(database, progress_update))
|
2020-05-03 16:15:11 +02:00
|
|
|
}
|
|
|
|
|
2020-09-03 11:36:07 +02:00
|
|
|
/// Fetch a transaction from the blockchain given its txid
|
2020-08-06 10:44:40 +02:00
|
|
|
fn get_tx(&self, txid: &Txid) -> Result<Option<Transaction>, Error>;
|
2020-09-03 11:36:07 +02:00
|
|
|
/// Broadcast a transaction
|
2020-08-06 10:44:40 +02:00
|
|
|
fn broadcast(&self, tx: &Transaction) -> Result<(), Error>;
|
2020-05-03 16:15:11 +02:00
|
|
|
|
2020-09-03 11:36:07 +02:00
|
|
|
/// Return the current height
|
2020-08-08 12:06:40 +02:00
|
|
|
fn get_height(&self) -> Result<u32, Error>;
|
2020-09-03 11:36:07 +02:00
|
|
|
/// Estimate the fee rate required to confirm a transaction in a given `target` of blocks
|
2020-08-07 11:23:01 +02:00
|
|
|
fn estimate_fee(&self, target: usize) -> Result<FeeRate, Error>;
|
2020-05-03 16:15:11 +02:00
|
|
|
}
|
|
|
|
|
2020-09-10 18:08:37 +02:00
|
|
|
/// Trait for [`Blockchain`] types that can be created given a configuration
|
|
|
|
pub trait ConfigurableBlockchain: Blockchain + Sized {
|
|
|
|
/// Type that contains the configuration
|
|
|
|
type Config: std::fmt::Debug;
|
|
|
|
|
|
|
|
/// Create a new instance given a configuration
|
|
|
|
fn from_config(config: &Self::Config) -> Result<Self, Error>;
|
|
|
|
}
|
|
|
|
|
2020-09-03 11:36:07 +02:00
|
|
|
/// Data sent with a progress update over a [`channel`]
|
2020-05-03 16:15:11 +02:00
|
|
|
pub type ProgressData = (f32, Option<String>);
|
|
|
|
|
2020-09-09 18:17:49 +02:00
|
|
|
/// Trait for types that can receive and process progress updates during [`Blockchain::sync`] and
|
|
|
|
/// [`Blockchain::setup`]
|
2020-08-25 16:07:26 +02:00
|
|
|
pub trait Progress: Send {
|
2020-09-03 11:36:07 +02:00
|
|
|
/// 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.
|
2020-05-03 16:15:11 +02:00
|
|
|
fn update(&self, progress: f32, message: Option<String>) -> Result<(), Error>;
|
|
|
|
}
|
|
|
|
|
2020-09-03 11:36:07 +02:00
|
|
|
/// Shortcut to create a [`channel`] (pair of [`Sender`] and [`Receiver`]) that can transport [`ProgressData`]
|
2020-05-03 16:15:11 +02:00
|
|
|
pub fn progress() -> (Sender<ProgressData>, Receiver<ProgressData>) {
|
|
|
|
channel()
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Progress for Sender<ProgressData> {
|
|
|
|
fn update(&self, progress: f32, message: Option<String>) -> Result<(), Error> {
|
2021-01-18 19:28:18 +11:00
|
|
|
if !(0.0..=100.0).contains(&progress) {
|
2020-05-03 16:15:11 +02:00
|
|
|
return Err(Error::InvalidProgressValue(progress));
|
|
|
|
}
|
|
|
|
|
|
|
|
self.send((progress, message))
|
|
|
|
.map_err(|_| Error::ProgressUpdateError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-03 11:36:07 +02:00
|
|
|
/// Type that implements [`Progress`] and drops every update received
|
2021-05-30 08:50:02 -07:00
|
|
|
#[derive(Clone, Copy)]
|
2020-05-03 16:15:11 +02:00
|
|
|
pub struct NoopProgress;
|
|
|
|
|
2020-09-03 11:36:07 +02:00
|
|
|
/// Create a new instance of [`NoopProgress`]
|
2020-05-03 16:15:11 +02:00
|
|
|
pub fn noop_progress() -> NoopProgress {
|
|
|
|
NoopProgress
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Progress for NoopProgress {
|
|
|
|
fn update(&self, _progress: f32, _message: Option<String>) -> Result<(), Error> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2020-08-25 16:07:26 +02:00
|
|
|
|
2020-09-03 11:36:07 +02:00
|
|
|
/// Type that implements [`Progress`] and logs at level `INFO` every update received
|
2021-05-30 08:50:02 -07:00
|
|
|
#[derive(Clone, Copy)]
|
2020-08-25 16:07:26 +02:00
|
|
|
pub struct LogProgress;
|
|
|
|
|
2020-09-03 11:36:07 +02:00
|
|
|
/// Create a nwe instance of [`LogProgress`]
|
2020-08-25 16:07:26 +02:00
|
|
|
pub fn log_progress() -> LogProgress {
|
|
|
|
LogProgress
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Progress for LogProgress {
|
|
|
|
fn update(&self, progress: f32, message: Option<String>) -> Result<(), Error> {
|
2020-10-07 14:18:50 -07:00
|
|
|
log::info!(
|
|
|
|
"Sync {:.3}%: `{}`",
|
|
|
|
progress,
|
|
|
|
message.unwrap_or_else(|| "".into())
|
|
|
|
);
|
2020-08-25 16:07:26 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2020-08-17 10:53:38 +02:00
|
|
|
|
|
|
|
#[maybe_async]
|
2020-09-09 18:17:49 +02:00
|
|
|
impl<T: Blockchain> Blockchain for Arc<T> {
|
2020-08-17 10:53:38 +02:00
|
|
|
fn get_capabilities(&self) -> HashSet<Capability> {
|
|
|
|
maybe_await!(self.deref().get_capabilities())
|
|
|
|
}
|
|
|
|
|
2020-08-31 10:49:44 +02:00
|
|
|
fn setup<D: BatchDatabase, P: 'static + Progress>(
|
2020-08-17 10:53:38 +02:00
|
|
|
&self,
|
|
|
|
database: &mut D,
|
|
|
|
progress_update: P,
|
|
|
|
) -> Result<(), Error> {
|
2021-07-15 12:04:03 -07:00
|
|
|
maybe_await!(self.deref().setup(database, progress_update))
|
2020-08-17 10:53:38 +02:00
|
|
|
}
|
|
|
|
|
2020-08-31 10:49:44 +02:00
|
|
|
fn sync<D: BatchDatabase, P: 'static + Progress>(
|
2020-08-17 10:53:38 +02:00
|
|
|
&self,
|
|
|
|
database: &mut D,
|
|
|
|
progress_update: P,
|
|
|
|
) -> Result<(), Error> {
|
2021-07-15 12:04:03 -07:00
|
|
|
maybe_await!(self.deref().sync(database, progress_update))
|
2020-08-17 10:53:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_tx(&self, txid: &Txid) -> Result<Option<Transaction>, Error> {
|
|
|
|
maybe_await!(self.deref().get_tx(txid))
|
|
|
|
}
|
|
|
|
fn broadcast(&self, tx: &Transaction) -> Result<(), Error> {
|
|
|
|
maybe_await!(self.deref().broadcast(tx))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_height(&self) -> Result<u32, Error> {
|
|
|
|
maybe_await!(self.deref().get_height())
|
|
|
|
}
|
|
|
|
fn estimate_fee(&self, target: usize) -> Result<FeeRate, Error> {
|
|
|
|
maybe_await!(self.deref().estimate_fee(target))
|
|
|
|
}
|
|
|
|
}
|