2020-08-31 11:26:36 +02:00
|
|
|
// Magical Bitcoin Library
|
|
|
|
// Written in 2020 by
|
|
|
|
// Alekos Filini <alekos.filini@gmail.com>
|
|
|
|
//
|
|
|
|
// Copyright (c) 2020 Magical Bitcoin
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
// SOFTWARE.
|
|
|
|
|
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-09-03 11:36:07 +02:00
|
|
|
//! let client = electrum_client::Client::new("ssl://electrum.blockstream.info:50002", None)?;
|
|
|
|
//! 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-07-15 18:49:24 +02:00
|
|
|
use electrum_client::{Client, ElectrumApi};
|
2020-05-03 16:15:11 +02:00
|
|
|
|
2020-11-16 12:18:34 +01:00
|
|
|
use self::utils::{ELSGetHistoryRes, ElectrumLikeSync};
|
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.
|
2020-09-09 18:17:49 +02:00
|
|
|
pub struct ElectrumBlockchain(Client);
|
2020-05-03 16:15:11 +02:00
|
|
|
|
2020-08-10 10:49:34 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
#[cfg(feature = "test-electrum")]
|
2020-09-14 14:25:38 +02:00
|
|
|
#[bdk_blockchain_tests(crate)]
|
2020-08-10 10:49:34 +02:00
|
|
|
fn local_electrs() -> ElectrumBlockchain {
|
|
|
|
ElectrumBlockchain::from(Client::new(&testutils::get_electrum_url(), None).unwrap())
|
|
|
|
}
|
|
|
|
|
2020-07-15 18:49:24 +02:00
|
|
|
impl std::convert::From<Client> for ElectrumBlockchain {
|
|
|
|
fn from(client: Client) -> Self {
|
2020-09-09 18:17:49 +02:00
|
|
|
ElectrumBlockchain(client)
|
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,
|
2020-05-03 16:15:11 +02:00
|
|
|
stop_gap: Option<usize>,
|
|
|
|
database: &mut D,
|
2020-05-07 15:14:05 +02:00
|
|
|
progress_update: P,
|
2020-05-03 16:15:11 +02:00
|
|
|
) -> Result<(), Error> {
|
2020-05-07 15:14:05 +02:00
|
|
|
self.0
|
|
|
|
.electrum_like_setup(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> {
|
2020-09-09 18:17:49 +02:00
|
|
|
Ok(self.0.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> {
|
2020-09-09 18:17:49 +02:00
|
|
|
Ok(self.0.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
|
|
|
|
.0
|
|
|
|
.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(
|
2020-09-09 18:17:49 +02:00
|
|
|
self.0.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 {
|
|
|
|
fn els_batch_script_get_history<'s, I: IntoIterator<Item = &'s Script>>(
|
2020-08-06 10:44:40 +02:00
|
|
|
&self,
|
2020-05-07 15:14:05 +02:00
|
|
|
scripts: I,
|
|
|
|
) -> Result<Vec<Vec<ELSGetHistoryRes>>, Error> {
|
|
|
|
self.batch_script_get_history(scripts)
|
|
|
|
.map(|v| {
|
|
|
|
v.into_iter()
|
|
|
|
.map(|v| {
|
|
|
|
v.into_iter()
|
|
|
|
.map(
|
|
|
|
|electrum_client::GetHistoryRes {
|
|
|
|
height, tx_hash, ..
|
|
|
|
}| ELSGetHistoryRes {
|
|
|
|
height,
|
|
|
|
tx_hash,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.collect()
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
})
|
|
|
|
.map_err(Error::Electrum)
|
2020-05-03 16:15:11 +02:00
|
|
|
}
|
|
|
|
|
2020-11-16 12:18:34 +01:00
|
|
|
fn els_batch_transaction_get<'s, I: IntoIterator<Item = &'s Txid>>(
|
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-16 12:18:34 +01:00
|
|
|
fn els_batch_block_header<I: IntoIterator<Item = u32>>(
|
|
|
|
&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`]
|
|
|
|
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
|
|
|
pub struct ElectrumBlockchainConfig {
|
|
|
|
pub url: String,
|
|
|
|
pub socks5: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ConfigurableBlockchain for ElectrumBlockchain {
|
|
|
|
type Config = ElectrumBlockchainConfig;
|
|
|
|
|
|
|
|
fn from_config(config: &Self::Config) -> Result<Self, Error> {
|
|
|
|
Ok(ElectrumBlockchain(Client::new(
|
|
|
|
config.url.as_str(),
|
|
|
|
config.socks5.as_deref(),
|
|
|
|
)?))
|
|
|
|
}
|
|
|
|
}
|