2021-03-03 13:22:05 -08:00
|
|
|
// Bitcoin Dev Kit
|
|
|
|
// Written in 2020 by Alekos Filini <alekos.filini@gmail.com>
|
2020-09-10 18:11:24 +02:00
|
|
|
//
|
2021-03-03 13:22:05 -08:00
|
|
|
// Copyright (c) 2020-2021 Bitcoin Dev Kit Developers
|
2020-09-10 18:11:24 +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-09-10 18:11:24 +02:00
|
|
|
|
|
|
|
//! Runtime-checked blockchain types
|
|
|
|
//!
|
|
|
|
//! This module provides the implementation of [`AnyBlockchain`] which allows switching the
|
|
|
|
//! inner [`Blockchain`] type at runtime.
|
2020-09-15 15:19:57 +02:00
|
|
|
//!
|
|
|
|
//! ## Example
|
|
|
|
//!
|
2022-02-24 20:59:21 +11:00
|
|
|
//! When paired with the use of [`ConfigurableBlockchain`], it allows creating any
|
2020-09-15 15:19:57 +02:00
|
|
|
//! blockchain type supported using a single line of code:
|
|
|
|
//!
|
|
|
|
//! ```no_run
|
|
|
|
//! # use bitcoin::Network;
|
|
|
|
//! # use bdk::blockchain::*;
|
2021-06-01 16:36:09 +10:00
|
|
|
//! # #[cfg(all(feature = "esplora", feature = "ureq"))]
|
|
|
|
//! # {
|
2020-09-15 15:19:57 +02:00
|
|
|
//! let config = serde_json::from_str("...")?;
|
|
|
|
//! let blockchain = AnyBlockchain::from_config(&config)?;
|
2022-01-26 15:17:48 +11:00
|
|
|
//! let height = blockchain.get_height();
|
2021-06-01 16:36:09 +10:00
|
|
|
//! # }
|
2020-09-15 15:19:57 +02:00
|
|
|
//! # Ok::<(), bdk::Error>(())
|
|
|
|
//! ```
|
2020-09-10 18:11:24 +02:00
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
macro_rules! impl_from {
|
2022-05-03 12:41:22 +02:00
|
|
|
( boxed $from:ty, $to:ty, $variant:ident, $( $cfg:tt )* ) => {
|
|
|
|
$( $cfg )*
|
|
|
|
impl From<$from> for $to {
|
|
|
|
fn from(inner: $from) -> Self {
|
|
|
|
<$to>::$variant(Box::new(inner))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-09-10 18:11:24 +02:00
|
|
|
( $from:ty, $to:ty, $variant:ident, $( $cfg:tt )* ) => {
|
|
|
|
$( $cfg )*
|
|
|
|
impl From<$from> for $to {
|
|
|
|
fn from(inner: $from) -> Self {
|
|
|
|
<$to>::$variant(inner)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! impl_inner_method {
|
|
|
|
( $self:expr, $name:ident $(, $args:expr)* ) => {
|
|
|
|
match $self {
|
|
|
|
#[cfg(feature = "electrum")]
|
|
|
|
AnyBlockchain::Electrum(inner) => inner.$name( $($args, )* ),
|
|
|
|
#[cfg(feature = "esplora")]
|
|
|
|
AnyBlockchain::Esplora(inner) => inner.$name( $($args, )* ),
|
|
|
|
#[cfg(feature = "compact_filters")]
|
|
|
|
AnyBlockchain::CompactFilters(inner) => inner.$name( $($args, )* ),
|
2021-06-01 14:09:00 +02:00
|
|
|
#[cfg(feature = "rpc")]
|
|
|
|
AnyBlockchain::Rpc(inner) => inner.$name( $($args, )* ),
|
2020-09-10 18:11:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Type that can contain any of the [`Blockchain`] types defined by the library
|
|
|
|
///
|
|
|
|
/// It allows switching backend at runtime
|
2020-09-15 15:19:57 +02:00
|
|
|
///
|
|
|
|
/// See [this module](crate::blockchain::any)'s documentation for a usage example.
|
2020-09-10 18:11:24 +02:00
|
|
|
pub enum AnyBlockchain {
|
|
|
|
#[cfg(feature = "electrum")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "electrum")))]
|
2020-12-16 13:57:01 -08:00
|
|
|
/// Electrum client
|
2022-05-03 12:41:22 +02:00
|
|
|
Electrum(Box<electrum::ElectrumBlockchain>),
|
2020-09-10 18:11:24 +02:00
|
|
|
#[cfg(feature = "esplora")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "esplora")))]
|
2020-12-16 13:57:01 -08:00
|
|
|
/// Esplora client
|
2022-05-03 12:41:22 +02:00
|
|
|
Esplora(Box<esplora::EsploraBlockchain>),
|
2020-09-10 18:11:24 +02:00
|
|
|
#[cfg(feature = "compact_filters")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "compact_filters")))]
|
2020-12-16 13:57:01 -08:00
|
|
|
/// Compact filters client
|
2022-05-03 12:41:22 +02:00
|
|
|
CompactFilters(Box<compact_filters::CompactFiltersBlockchain>),
|
2021-06-01 14:09:00 +02:00
|
|
|
#[cfg(feature = "rpc")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "rpc")))]
|
|
|
|
/// RPC client
|
2022-05-03 12:41:22 +02:00
|
|
|
Rpc(Box<rpc::RpcBlockchain>),
|
2020-09-10 18:11:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[maybe_async]
|
|
|
|
impl Blockchain for AnyBlockchain {
|
|
|
|
fn get_capabilities(&self) -> HashSet<Capability> {
|
|
|
|
maybe_await!(impl_inner_method!(self, get_capabilities))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn broadcast(&self, tx: &Transaction) -> Result<(), Error> {
|
|
|
|
maybe_await!(impl_inner_method!(self, broadcast, tx))
|
|
|
|
}
|
|
|
|
|
2022-01-26 15:17:48 +11:00
|
|
|
fn estimate_fee(&self, target: usize) -> Result<FeeRate, Error> {
|
|
|
|
maybe_await!(impl_inner_method!(self, estimate_fee, target))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[maybe_async]
|
|
|
|
impl GetHeight for AnyBlockchain {
|
2020-09-10 18:11:24 +02:00
|
|
|
fn get_height(&self) -> Result<u32, Error> {
|
|
|
|
maybe_await!(impl_inner_method!(self, get_height))
|
|
|
|
}
|
2022-01-26 15:17:48 +11:00
|
|
|
}
|
|
|
|
|
2022-02-23 10:38:35 +11:00
|
|
|
#[maybe_async]
|
|
|
|
impl GetTx for AnyBlockchain {
|
|
|
|
fn get_tx(&self, txid: &Txid) -> Result<Option<Transaction>, Error> {
|
|
|
|
maybe_await!(impl_inner_method!(self, get_tx, txid))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-16 20:42:02 +01:00
|
|
|
#[maybe_async]
|
|
|
|
impl GetBlockHash for AnyBlockchain {
|
|
|
|
fn get_block_hash(&self, height: u64) -> Result<BlockHash, Error> {
|
|
|
|
maybe_await!(impl_inner_method!(self, get_block_hash, height))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 15:17:48 +11:00
|
|
|
#[maybe_async]
|
|
|
|
impl WalletSync for AnyBlockchain {
|
2022-01-27 16:52:53 +11:00
|
|
|
fn wallet_sync<D: BatchDatabase>(
|
2022-01-26 15:17:48 +11:00
|
|
|
&self,
|
|
|
|
database: &mut D,
|
2022-01-27 16:52:53 +11:00
|
|
|
progress_update: Box<dyn Progress>,
|
2022-01-26 15:17:48 +11:00
|
|
|
) -> Result<(), Error> {
|
|
|
|
maybe_await!(impl_inner_method!(
|
|
|
|
self,
|
|
|
|
wallet_sync,
|
|
|
|
database,
|
|
|
|
progress_update
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2022-01-27 16:52:53 +11:00
|
|
|
fn wallet_setup<D: BatchDatabase>(
|
2022-01-26 15:17:48 +11:00
|
|
|
&self,
|
|
|
|
database: &mut D,
|
2022-01-27 16:52:53 +11:00
|
|
|
progress_update: Box<dyn Progress>,
|
2022-01-26 15:17:48 +11:00
|
|
|
) -> Result<(), Error> {
|
|
|
|
maybe_await!(impl_inner_method!(
|
|
|
|
self,
|
|
|
|
wallet_setup,
|
|
|
|
database,
|
|
|
|
progress_update
|
|
|
|
))
|
2020-09-10 18:11:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-03 12:41:22 +02:00
|
|
|
impl_from!(boxed electrum::ElectrumBlockchain, AnyBlockchain, Electrum, #[cfg(feature = "electrum")]);
|
|
|
|
impl_from!(boxed esplora::EsploraBlockchain, AnyBlockchain, Esplora, #[cfg(feature = "esplora")]);
|
|
|
|
impl_from!(boxed compact_filters::CompactFiltersBlockchain, AnyBlockchain, CompactFilters, #[cfg(feature = "compact_filters")]);
|
|
|
|
impl_from!(boxed rpc::RpcBlockchain, AnyBlockchain, Rpc, #[cfg(feature = "rpc")]);
|
2020-09-10 18:11:24 +02:00
|
|
|
|
|
|
|
/// Type that can contain any of the blockchain configurations defined by the library
|
|
|
|
///
|
|
|
|
/// This allows storing a single configuration that can be loaded into an [`AnyBlockchain`]
|
|
|
|
/// instance. Wallets that plan to offer users the ability to switch blockchain backend at runtime
|
|
|
|
/// will find this particularly useful.
|
2021-03-25 16:28:38 +11:00
|
|
|
///
|
|
|
|
/// This type can be serialized from a JSON object like:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # #[cfg(feature = "electrum")]
|
|
|
|
/// # {
|
|
|
|
/// use bdk::blockchain::{electrum::ElectrumBlockchainConfig, AnyBlockchainConfig};
|
|
|
|
/// let config: AnyBlockchainConfig = serde_json::from_str(
|
|
|
|
/// r#"{
|
|
|
|
/// "type" : "electrum",
|
|
|
|
/// "url" : "ssl://electrum.blockstream.info:50002",
|
2021-07-15 10:55:49 -07:00
|
|
|
/// "retry": 2,
|
|
|
|
/// "stop_gap": 20
|
2021-03-25 16:28:38 +11:00
|
|
|
/// }"#,
|
|
|
|
/// )
|
|
|
|
/// .unwrap();
|
|
|
|
/// assert_eq!(
|
|
|
|
/// config,
|
|
|
|
/// AnyBlockchainConfig::Electrum(ElectrumBlockchainConfig {
|
|
|
|
/// url: "ssl://electrum.blockstream.info:50002".into(),
|
|
|
|
/// retry: 2,
|
|
|
|
/// socks5: None,
|
2021-07-15 10:55:49 -07:00
|
|
|
/// timeout: None,
|
|
|
|
/// stop_gap: 20,
|
2021-03-25 16:28:38 +11:00
|
|
|
/// })
|
|
|
|
/// );
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq)]
|
|
|
|
#[serde(tag = "type", rename_all = "snake_case")]
|
2020-09-10 18:11:24 +02:00
|
|
|
pub enum AnyBlockchainConfig {
|
|
|
|
#[cfg(feature = "electrum")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "electrum")))]
|
2020-12-16 13:57:01 -08:00
|
|
|
/// Electrum client
|
2020-09-10 18:11:24 +02:00
|
|
|
Electrum(electrum::ElectrumBlockchainConfig),
|
|
|
|
#[cfg(feature = "esplora")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "esplora")))]
|
2020-12-16 13:57:01 -08:00
|
|
|
/// Esplora client
|
2020-09-10 18:11:24 +02:00
|
|
|
Esplora(esplora::EsploraBlockchainConfig),
|
|
|
|
#[cfg(feature = "compact_filters")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "compact_filters")))]
|
2020-12-16 13:57:01 -08:00
|
|
|
/// Compact filters client
|
2020-09-10 18:11:24 +02:00
|
|
|
CompactFilters(compact_filters::CompactFiltersBlockchainConfig),
|
2021-06-01 14:09:00 +02:00
|
|
|
#[cfg(feature = "rpc")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "rpc")))]
|
|
|
|
/// RPC client configuration
|
|
|
|
Rpc(rpc::RpcConfig),
|
2020-09-10 18:11:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ConfigurableBlockchain for AnyBlockchain {
|
|
|
|
type Config = AnyBlockchainConfig;
|
|
|
|
|
|
|
|
fn from_config(config: &Self::Config) -> Result<Self, Error> {
|
|
|
|
Ok(match config {
|
|
|
|
#[cfg(feature = "electrum")]
|
|
|
|
AnyBlockchainConfig::Electrum(inner) => {
|
2022-05-03 12:41:22 +02:00
|
|
|
AnyBlockchain::Electrum(Box::new(electrum::ElectrumBlockchain::from_config(inner)?))
|
2020-09-10 18:11:24 +02:00
|
|
|
}
|
|
|
|
#[cfg(feature = "esplora")]
|
|
|
|
AnyBlockchainConfig::Esplora(inner) => {
|
2022-05-03 12:41:22 +02:00
|
|
|
AnyBlockchain::Esplora(Box::new(esplora::EsploraBlockchain::from_config(inner)?))
|
2020-09-10 18:11:24 +02:00
|
|
|
}
|
|
|
|
#[cfg(feature = "compact_filters")]
|
2022-05-03 12:41:22 +02:00
|
|
|
AnyBlockchainConfig::CompactFilters(inner) => AnyBlockchain::CompactFilters(Box::new(
|
2020-09-10 18:11:24 +02:00
|
|
|
compact_filters::CompactFiltersBlockchain::from_config(inner)?,
|
2022-05-03 12:41:22 +02:00
|
|
|
)),
|
2021-06-01 14:09:00 +02:00
|
|
|
#[cfg(feature = "rpc")]
|
|
|
|
AnyBlockchainConfig::Rpc(inner) => {
|
2022-05-03 12:41:22 +02:00
|
|
|
AnyBlockchain::Rpc(Box::new(rpc::RpcBlockchain::from_config(inner)?))
|
2021-06-01 14:09:00 +02:00
|
|
|
}
|
2020-09-10 18:11:24 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_from!(electrum::ElectrumBlockchainConfig, AnyBlockchainConfig, Electrum, #[cfg(feature = "electrum")]);
|
|
|
|
impl_from!(esplora::EsploraBlockchainConfig, AnyBlockchainConfig, Esplora, #[cfg(feature = "esplora")]);
|
|
|
|
impl_from!(compact_filters::CompactFiltersBlockchainConfig, AnyBlockchainConfig, CompactFilters, #[cfg(feature = "compact_filters")]);
|
2021-06-01 14:09:00 +02:00
|
|
|
impl_from!(rpc::RpcConfig, AnyBlockchainConfig, Rpc, #[cfg(feature = "rpc")]);
|