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
|
|
|
//! Electrum
|
|
|
|
//!
|
2020-09-09 18:17:49 +02:00
|
|
|
//! This module defines a [`Blockchain`] struct that wraps an [`electrum_client::Client`]
|
2020-09-03 11:36:07 +02:00
|
|
|
//! and implements the logic required to populate the wallet's [database](crate::database::Database) by
|
|
|
|
//! querying the inner client.
|
|
|
|
//!
|
|
|
|
//! ## Example
|
|
|
|
//!
|
|
|
|
//! ```no_run
|
2020-09-14 14:25:38 +02:00
|
|
|
//! # use bdk::blockchain::electrum::ElectrumBlockchain;
|
2020-11-24 12:16:49 +01:00
|
|
|
//! let client = electrum_client::Client::new("ssl://electrum.blockstream.info:50002")?;
|
2020-09-03 11:36:07 +02:00
|
|
|
//! let blockchain = ElectrumBlockchain::from(client);
|
2020-09-14 14:25:38 +02:00
|
|
|
//! # Ok::<(), bdk::Error>(())
|
2020-09-03 11:36:07 +02:00
|
|
|
//! ```
|
|
|
|
|
2020-05-07 15:14:05 +02:00
|
|
|
use std::collections::HashSet;
|
2020-05-03 16:15:11 +02:00
|
|
|
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use log::{debug, error, info, trace};
|
|
|
|
|
2020-11-16 12:18:34 +01:00
|
|
|
use bitcoin::{BlockHeader, Script, Transaction, Txid};
|
2020-05-03 16:15:11 +02:00
|
|
|
|
2020-11-24 12:16:49 +01:00
|
|
|
use electrum_client::{Client, ConfigBuilder, ElectrumApi, Socks5Config};
|
2020-05-03 16:15:11 +02:00
|
|
|
|
2021-03-30 16:33:07 +02:00
|
|
|
use self::utils::{ElectrumLikeSync, ElsGetHistoryRes};
|
2020-05-03 16:15:11 +02:00
|
|
|
use super::*;
|
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-09-03 11:36:07 +02:00
|
|
|
/// Wrapper over an Electrum Client that implements the required blockchain traits
|
|
|
|
///
|
|
|
|
/// ## Example
|
|
|
|
/// See the [`blockchain::electrum`](crate::blockchain::electrum) module for a usage example.
|
2021-07-15 10:55:49 -07:00
|
|
|
pub struct ElectrumBlockchain {
|
|
|
|
client: Client,
|
|
|
|
stop_gap: usize,
|
|
|
|
}
|
2020-05-03 16:15:11 +02:00
|
|
|
|
2020-07-15 18:49:24 +02:00
|
|
|
impl std::convert::From<Client> for ElectrumBlockchain {
|
|
|
|
fn from(client: Client) -> Self {
|
2021-07-15 10:55:49 -07:00
|
|
|
ElectrumBlockchain {
|
|
|
|
client,
|
|
|
|
stop_gap: 20,
|
|
|
|
}
|
2020-05-03 16:15:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-15 18:49:24 +02:00
|
|
|
impl Blockchain for ElectrumBlockchain {
|
|
|
|
fn get_capabilities(&self) -> HashSet<Capability> {
|
2020-08-25 16:07:26 +02:00
|
|
|
vec![
|
|
|
|
Capability::FullHistory,
|
|
|
|
Capability::GetAnyTx,
|
|
|
|
Capability::AccurateFees,
|
|
|
|
]
|
|
|
|
.into_iter()
|
|
|
|
.collect()
|
2020-05-03 16:15:11 +02:00
|
|
|
}
|
|
|
|
|
2020-08-31 10:49:44 +02:00
|
|
|
fn setup<D: BatchDatabase, P: Progress>(
|
2020-08-06 10:44:40 +02:00
|
|
|
&self,
|
2021-07-15 10:55:49 -07:00
|
|
|
_stop_gap: Option<usize>,
|
2020-05-03 16:15:11 +02:00
|
|
|
database: &mut D,
|
2020-05-07 15:14:05 +02:00
|
|
|
progress_update: P,
|
2020-05-03 16:15:11 +02:00
|
|
|
) -> Result<(), Error> {
|
2021-07-15 10:55:49 -07:00
|
|
|
self.client
|
|
|
|
.electrum_like_setup(Some(self.stop_gap), database, progress_update)
|
2020-05-03 16:15:11 +02:00
|
|
|
}
|
|
|
|
|
2020-08-06 10:44:40 +02:00
|
|
|
fn get_tx(&self, txid: &Txid) -> Result<Option<Transaction>, Error> {
|
2021-07-15 10:55:49 -07:00
|
|
|
Ok(self.client.transaction_get(txid).map(Option::Some)?)
|
2020-05-03 16:15:11 +02:00
|
|
|
}
|
|
|
|
|
2020-08-06 10:44:40 +02:00
|
|
|
fn broadcast(&self, tx: &Transaction) -> Result<(), Error> {
|
2021-07-15 10:55:49 -07:00
|
|
|
Ok(self.client.transaction_broadcast(tx).map(|_| ())?)
|
2020-05-03 16:15:11 +02:00
|
|
|
}
|
|
|
|
|
2020-08-08 12:06:40 +02:00
|
|
|
fn get_height(&self) -> Result<u32, Error> {
|
2020-05-06 17:17:14 +02:00
|
|
|
// TODO: unsubscribe when added to the client, or is there a better call to use here?
|
|
|
|
|
2020-05-03 16:15:11 +02:00
|
|
|
Ok(self
|
2021-07-15 10:55:49 -07:00
|
|
|
.client
|
2020-05-03 16:15:11 +02:00
|
|
|
.block_headers_subscribe()
|
2020-08-08 12:06:40 +02:00
|
|
|
.map(|data| data.height as u32)?)
|
2020-05-03 16:15:11 +02:00
|
|
|
}
|
2020-08-07 11:23:01 +02:00
|
|
|
|
|
|
|
fn estimate_fee(&self, target: usize) -> Result<FeeRate, Error> {
|
|
|
|
Ok(FeeRate::from_btc_per_kvb(
|
2021-07-15 10:55:49 -07:00
|
|
|
self.client.estimate_fee(target)? as f32
|
2020-08-07 11:23:01 +02:00
|
|
|
))
|
|
|
|
}
|
2020-05-03 16:15:11 +02:00
|
|
|
}
|
|
|
|
|
2020-07-15 18:49:24 +02:00
|
|
|
impl ElectrumLikeSync for Client {
|
2020-11-24 12:16:49 +01:00
|
|
|
fn els_batch_script_get_history<'s, I: IntoIterator<Item = &'s Script> + Clone>(
|
2020-08-06 10:44:40 +02:00
|
|
|
&self,
|
2020-05-07 15:14:05 +02:00
|
|
|
scripts: I,
|
2021-03-30 16:33:07 +02:00
|
|
|
) -> Result<Vec<Vec<ElsGetHistoryRes>>, Error> {
|
2020-05-07 15:14:05 +02:00
|
|
|
self.batch_script_get_history(scripts)
|
|
|
|
.map(|v| {
|
|
|
|
v.into_iter()
|
|
|
|
.map(|v| {
|
|
|
|
v.into_iter()
|
|
|
|
.map(
|
|
|
|
|electrum_client::GetHistoryRes {
|
|
|
|
height, tx_hash, ..
|
2021-03-30 16:33:07 +02:00
|
|
|
}| ElsGetHistoryRes {
|
2020-05-07 15:14:05 +02:00
|
|
|
height,
|
|
|
|
tx_hash,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.collect()
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
})
|
|
|
|
.map_err(Error::Electrum)
|
2020-05-03 16:15:11 +02:00
|
|
|
}
|
|
|
|
|
2020-11-24 12:16:49 +01:00
|
|
|
fn els_batch_transaction_get<'s, I: IntoIterator<Item = &'s Txid> + Clone>(
|
2020-08-06 10:44:40 +02:00
|
|
|
&self,
|
2020-11-16 12:18:34 +01:00
|
|
|
txids: I,
|
|
|
|
) -> Result<Vec<Transaction>, Error> {
|
|
|
|
self.batch_transaction_get(txids).map_err(Error::Electrum)
|
2020-05-07 15:14:05 +02:00
|
|
|
}
|
2020-05-03 16:15:11 +02:00
|
|
|
|
2020-11-24 12:16:49 +01:00
|
|
|
fn els_batch_block_header<I: IntoIterator<Item = u32> + Clone>(
|
2020-11-16 12:18:34 +01:00
|
|
|
&self,
|
|
|
|
heights: I,
|
|
|
|
) -> Result<Vec<BlockHeader>, Error> {
|
|
|
|
self.batch_block_header(heights).map_err(Error::Electrum)
|
2020-05-03 16:15:11 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-10 18:08:37 +02:00
|
|
|
|
|
|
|
/// Configuration for an [`ElectrumBlockchain`]
|
2021-03-25 16:28:38 +11:00
|
|
|
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone, PartialEq)]
|
2020-09-10 18:08:37 +02:00
|
|
|
pub struct ElectrumBlockchainConfig {
|
2020-11-27 10:25:12 +01:00
|
|
|
/// URL of the Electrum server (such as ElectrumX, Esplora, BWT) may start with `ssl://` or `tcp://` and include a port
|
|
|
|
///
|
|
|
|
/// eg. `ssl://electrum.blockstream.info:60002`
|
2020-09-10 18:08:37 +02:00
|
|
|
pub url: String,
|
2020-11-27 10:25:12 +01:00
|
|
|
/// URL of the socks5 proxy server or a Tor service
|
2020-09-10 18:08:37 +02:00
|
|
|
pub socks5: Option<String>,
|
2020-12-02 16:54:49 -08:00
|
|
|
/// Request retry count
|
2020-11-24 12:16:49 +01:00
|
|
|
pub retry: u8,
|
2020-12-02 16:54:49 -08:00
|
|
|
/// Request timeout (seconds)
|
2021-01-11 11:37:34 +01:00
|
|
|
pub timeout: Option<u8>,
|
2021-07-15 10:55:49 -07:00
|
|
|
/// Stop searching addresses for transactions after finding an unused gap of this length
|
|
|
|
pub stop_gap: usize,
|
2020-09-10 18:08:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ConfigurableBlockchain for ElectrumBlockchain {
|
|
|
|
type Config = ElectrumBlockchainConfig;
|
|
|
|
|
|
|
|
fn from_config(config: &Self::Config) -> Result<Self, Error> {
|
2020-11-24 12:16:49 +01:00
|
|
|
let socks5 = config.socks5.as_ref().map(Socks5Config::new);
|
|
|
|
let electrum_config = ConfigBuilder::new()
|
|
|
|
.retry(config.retry)
|
|
|
|
.timeout(config.timeout)?
|
2021-01-11 11:37:34 +01:00
|
|
|
.socks5(socks5)?
|
2020-11-24 12:16:49 +01:00
|
|
|
.build();
|
|
|
|
|
2021-07-15 10:55:49 -07:00
|
|
|
Ok(ElectrumBlockchain {
|
|
|
|
client: Client::from_config(config.url.as_str(), electrum_config)?,
|
|
|
|
stop_gap: config.stop_gap,
|
|
|
|
})
|
2020-09-10 18:08:37 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-16 15:07:55 +10:00
|
|
|
|
2021-06-17 13:48:46 +02:00
|
|
|
#[cfg(test)]
|
2021-06-28 15:04:17 +02:00
|
|
|
#[cfg(feature = "test-electrum")]
|
2021-05-19 13:04:32 +10:00
|
|
|
crate::bdk_blockchain_tests! {
|
2021-06-17 13:48:46 +02:00
|
|
|
fn test_instance(test_client: &TestClient) -> ElectrumBlockchain {
|
|
|
|
ElectrumBlockchain::from(Client::new(&test_client.electrsd.electrum_url).unwrap())
|
2021-05-16 15:07:55 +10:00
|
|
|
}
|
|
|
|
}
|