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-05-07 15:14:05 +02:00
|
|
|
use bitcoin::{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-05-07 15:14:05 +02:00
|
|
|
use self::utils::{ELSGetHistoryRes, ELSListUnspentRes, ElectrumLikeSync};
|
2020-05-03 16:15:11 +02:00
|
|
|
use super::*;
|
2020-05-07 15:14:05 +02:00
|
|
|
use crate::database::{BatchDatabase, DatabaseUtils};
|
2020-05-03 16:15:11 +02:00
|
|
|
use crate::error::Error;
|
|
|
|
|
2020-07-15 18:49:24 +02:00
|
|
|
pub struct ElectrumBlockchain(Option<Client>);
|
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 {
|
2020-05-03 16:15:11 +02:00
|
|
|
ElectrumBlockchain(Some(client))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-15 18:49:24 +02:00
|
|
|
impl Blockchain for ElectrumBlockchain {
|
2020-05-03 16:15:11 +02:00
|
|
|
fn offline() -> Self {
|
|
|
|
ElectrumBlockchain(None)
|
|
|
|
}
|
2020-05-06 17:17:14 +02:00
|
|
|
|
|
|
|
fn is_online(&self) -> bool {
|
|
|
|
self.0.is_some()
|
|
|
|
}
|
2020-05-03 16:15:11 +02:00
|
|
|
}
|
|
|
|
|
2020-07-15 18:49:24 +02:00
|
|
|
impl OnlineBlockchain for ElectrumBlockchain {
|
|
|
|
fn get_capabilities(&self) -> HashSet<Capability> {
|
2020-05-03 16:15:11 +02:00
|
|
|
vec![Capability::FullHistory, Capability::GetAnyTx]
|
|
|
|
.into_iter()
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2020-07-15 18:49:24 +02:00
|
|
|
fn setup<D: BatchDatabase + DatabaseUtils, 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
|
2020-08-06 10:44:40 +02:00
|
|
|
.as_ref()
|
2020-05-07 15:14:05 +02:00
|
|
|
.ok_or(Error::OfflineClient)?
|
|
|
|
.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-05-03 16:15:11 +02:00
|
|
|
Ok(self
|
|
|
|
.0
|
2020-08-06 10:44:40 +02:00
|
|
|
.as_ref()
|
2020-05-06 16:50:03 +02:00
|
|
|
.ok_or(Error::OfflineClient)?
|
2020-05-03 16:15:11 +02:00
|
|
|
.transaction_get(txid)
|
|
|
|
.map(Option::Some)?)
|
|
|
|
}
|
|
|
|
|
2020-08-06 10:44:40 +02:00
|
|
|
fn broadcast(&self, tx: &Transaction) -> Result<(), Error> {
|
2020-05-03 16:15:11 +02:00
|
|
|
Ok(self
|
|
|
|
.0
|
2020-08-06 10:44:40 +02:00
|
|
|
.as_ref()
|
2020-05-06 16:50:03 +02:00
|
|
|
.ok_or(Error::OfflineClient)?
|
2020-05-03 16:15:11 +02:00
|
|
|
.transaction_broadcast(tx)
|
|
|
|
.map(|_| ())?)
|
|
|
|
}
|
|
|
|
|
2020-08-06 10:44:40 +02:00
|
|
|
fn get_height(&self) -> Result<usize, 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
|
2020-08-06 10:44:40 +02:00
|
|
|
.as_ref()
|
2020-05-06 16:50:03 +02:00
|
|
|
.ok_or(Error::OfflineClient)?
|
2020-05-03 16:15:11 +02:00
|
|
|
.block_headers_subscribe()
|
|
|
|
.map(|data| data.height)?)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07-15 18:49:24 +02:00
|
|
|
fn els_batch_script_list_unspent<'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<ELSListUnspentRes>>, Error> {
|
|
|
|
self.batch_script_list_unspent(scripts)
|
|
|
|
.map(|v| {
|
|
|
|
v.into_iter()
|
|
|
|
.map(|v| {
|
|
|
|
v.into_iter()
|
|
|
|
.map(
|
|
|
|
|electrum_client::ListUnspentRes {
|
|
|
|
height,
|
|
|
|
tx_hash,
|
|
|
|
tx_pos,
|
|
|
|
..
|
|
|
|
}| ELSListUnspentRes {
|
|
|
|
height,
|
|
|
|
tx_hash,
|
|
|
|
tx_pos,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.collect()
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
})
|
|
|
|
.map_err(Error::Electrum)
|
|
|
|
}
|
2020-05-03 16:15:11 +02:00
|
|
|
|
2020-08-06 10:44:40 +02:00
|
|
|
fn els_transaction_get(&self, txid: &Txid) -> Result<Transaction, Error> {
|
2020-07-15 18:49:24 +02:00
|
|
|
self.transaction_get(txid).map_err(Error::Electrum)
|
2020-05-03 16:15:11 +02:00
|
|
|
}
|
|
|
|
}
|