Remove async, upgrade electrum-client

This commit is contained in:
Alekos Filini
2020-07-15 18:49:24 +02:00
parent c3923b66f8
commit 123984e99d
9 changed files with 144 additions and 149 deletions

View File

@@ -5,23 +5,22 @@ use log::{debug, error, info, trace};
use bitcoin::{Script, Transaction, Txid};
use electrum_client::tokio::io::{AsyncRead, AsyncWrite};
use electrum_client::Client;
use electrum_client::{Client, ElectrumApi};
use self::utils::{ELSGetHistoryRes, ELSListUnspentRes, ElectrumLikeSync};
use super::*;
use crate::database::{BatchDatabase, DatabaseUtils};
use crate::error::Error;
pub struct ElectrumBlockchain<T: AsyncRead + AsyncWrite + Send>(Option<Client<T>>);
pub struct ElectrumBlockchain(Option<Client>);
impl<T: AsyncRead + AsyncWrite + Send> std::convert::From<Client<T>> for ElectrumBlockchain<T> {
fn from(client: Client<T>) -> Self {
impl std::convert::From<Client> for ElectrumBlockchain {
fn from(client: Client) -> Self {
ElectrumBlockchain(Some(client))
}
}
impl<T: AsyncRead + AsyncWrite + Send> Blockchain for ElectrumBlockchain<T> {
impl Blockchain for ElectrumBlockchain {
fn offline() -> Self {
ElectrumBlockchain(None)
}
@@ -31,15 +30,14 @@ impl<T: AsyncRead + AsyncWrite + Send> Blockchain for ElectrumBlockchain<T> {
}
}
#[async_trait(?Send)]
impl<T: AsyncRead + AsyncWrite + Send> OnlineBlockchain for ElectrumBlockchain<T> {
async fn get_capabilities(&self) -> HashSet<Capability> {
impl OnlineBlockchain for ElectrumBlockchain {
fn get_capabilities(&self) -> HashSet<Capability> {
vec![Capability::FullHistory, Capability::GetAnyTx]
.into_iter()
.collect()
}
async fn setup<D: BatchDatabase + DatabaseUtils, P: Progress>(
fn setup<D: BatchDatabase + DatabaseUtils, P: Progress>(
&mut self,
stop_gap: Option<usize>,
database: &mut D,
@@ -49,30 +47,27 @@ impl<T: AsyncRead + AsyncWrite + Send> OnlineBlockchain for ElectrumBlockchain<T
.as_mut()
.ok_or(Error::OfflineClient)?
.electrum_like_setup(stop_gap, database, progress_update)
.await
}
async fn get_tx(&mut self, txid: &Txid) -> Result<Option<Transaction>, Error> {
fn get_tx(&mut self, txid: &Txid) -> Result<Option<Transaction>, Error> {
Ok(self
.0
.as_mut()
.ok_or(Error::OfflineClient)?
.transaction_get(txid)
.await
.map(Option::Some)?)
}
async fn broadcast(&mut self, tx: &Transaction) -> Result<(), Error> {
fn broadcast(&mut self, tx: &Transaction) -> Result<(), Error> {
Ok(self
.0
.as_mut()
.ok_or(Error::OfflineClient)?
.transaction_broadcast(tx)
.await
.map(|_| ())?)
}
async fn get_height(&mut self) -> Result<usize, Error> {
fn get_height(&mut self) -> Result<usize, Error> {
// TODO: unsubscribe when added to the client, or is there a better call to use here?
Ok(self
@@ -80,19 +75,16 @@ impl<T: AsyncRead + AsyncWrite + Send> OnlineBlockchain for ElectrumBlockchain<T
.as_mut()
.ok_or(Error::OfflineClient)?
.block_headers_subscribe()
.await
.map(|data| data.height)?)
}
}
#[async_trait(?Send)]
impl<T: AsyncRead + AsyncWrite + Send> ElectrumLikeSync for Client<T> {
async fn els_batch_script_get_history<'s, I: IntoIterator<Item = &'s Script>>(
impl ElectrumLikeSync for Client {
fn els_batch_script_get_history<'s, I: IntoIterator<Item = &'s Script>>(
&mut self,
scripts: I,
) -> Result<Vec<Vec<ELSGetHistoryRes>>, Error> {
self.batch_script_get_history(scripts)
.await
.map(|v| {
v.into_iter()
.map(|v| {
@@ -112,12 +104,11 @@ impl<T: AsyncRead + AsyncWrite + Send> ElectrumLikeSync for Client<T> {
.map_err(Error::Electrum)
}
async fn els_batch_script_list_unspent<'s, I: IntoIterator<Item = &'s Script>>(
fn els_batch_script_list_unspent<'s, I: IntoIterator<Item = &'s Script>>(
&mut self,
scripts: I,
) -> Result<Vec<Vec<ELSListUnspentRes>>, Error> {
self.batch_script_list_unspent(scripts)
.await
.map(|v| {
v.into_iter()
.map(|v| {
@@ -141,7 +132,7 @@ impl<T: AsyncRead + AsyncWrite + Send> ElectrumLikeSync for Client<T> {
.map_err(Error::Electrum)
}
async fn els_transaction_get(&mut self, txid: &Txid) -> Result<Transaction, Error> {
self.transaction_get(txid).await.map_err(Error::Electrum)
fn els_transaction_get(&mut self, txid: &Txid) -> Result<Transaction, Error> {
self.transaction_get(txid).map_err(Error::Electrum)
}
}

View File

@@ -1,14 +1,16 @@
use std::collections::HashSet;
use std::sync::Mutex;
use futures::stream::{self, StreamExt, TryStreamExt};
use tokio::runtime::Runtime;
#[allow(unused_imports)]
use log::{debug, error, info, trace};
use serde::Deserialize;
use reqwest::Client;
use reqwest::StatusCode;
use reqwest::{Client, StatusCode};
use bitcoin::consensus::{deserialize, serialize};
use bitcoin::hashes::hex::ToHex;
@@ -23,7 +25,12 @@ use crate::error::Error;
#[derive(Debug)]
pub struct UrlClient {
url: String,
// We use the async client instead of the blocking one because it automatically uses `fetch`
// when the target platform is wasm32. For some reason the blocking client doesn't, so we are
// stuck with this
client: Client,
runtime: Mutex<Runtime>,
}
#[derive(Debug)]
@@ -40,6 +47,8 @@ impl EsploraBlockchain {
EsploraBlockchain(Some(UrlClient {
url: base_url.to_string(),
client: Client::new(),
runtime: Mutex::new(Runtime::new().unwrap()),
}))
}
}
@@ -54,15 +63,14 @@ impl Blockchain for EsploraBlockchain {
}
}
#[async_trait(?Send)]
impl OnlineBlockchain for EsploraBlockchain {
async fn get_capabilities(&self) -> HashSet<Capability> {
fn get_capabilities(&self) -> HashSet<Capability> {
vec![Capability::FullHistory, Capability::GetAnyTx]
.into_iter()
.collect()
}
async fn setup<D: BatchDatabase + DatabaseUtils, P: Progress>(
fn setup<D: BatchDatabase + DatabaseUtils, P: Progress>(
&mut self,
stop_gap: Option<usize>,
database: &mut D,
@@ -72,34 +80,22 @@ impl OnlineBlockchain for EsploraBlockchain {
.as_mut()
.ok_or(Error::OfflineClient)?
.electrum_like_setup(stop_gap, database, progress_update)
.await
}
async fn get_tx(&mut self, txid: &Txid) -> Result<Option<Transaction>, Error> {
fn get_tx(&mut self, txid: &Txid) -> Result<Option<Transaction>, Error> {
Ok(self.0.as_mut().ok_or(Error::OfflineClient)?._get_tx(txid)?)
}
fn broadcast(&mut self, tx: &Transaction) -> Result<(), Error> {
Ok(self
.0
.as_mut()
.ok_or(Error::OfflineClient)?
._get_tx(txid)
.await?)
._broadcast(tx)?)
}
async fn broadcast(&mut self, tx: &Transaction) -> Result<(), Error> {
Ok(self
.0
.as_mut()
.ok_or(Error::OfflineClient)?
._broadcast(tx)
.await?)
}
async fn get_height(&mut self) -> Result<usize, Error> {
Ok(self
.0
.as_mut()
.ok_or(Error::OfflineClient)?
._get_height()
.await?)
fn get_height(&mut self) -> Result<usize, Error> {
Ok(self.0.as_mut().ok_or(Error::OfflineClient)?._get_height()?)
}
}
@@ -108,40 +104,53 @@ impl UrlClient {
sha256::Hash::hash(script.as_bytes()).into_inner().to_hex()
}
async fn _get_tx(&self, txid: &Txid) -> Result<Option<Transaction>, EsploraError> {
let resp = self
.client
.get(&format!("{}/api/tx/{}/raw", self.url, txid))
.send()
.await?;
fn _get_tx(&self, txid: &Txid) -> Result<Option<Transaction>, EsploraError> {
let resp = self.runtime.lock().unwrap().block_on(
self.client
.get(&format!("{}/api/tx/{}/raw", self.url, txid))
.send(),
)?;
if let StatusCode::NOT_FOUND = resp.status() {
return Ok(None);
}
Ok(Some(deserialize(&resp.error_for_status()?.bytes().await?)?))
Ok(Some(deserialize(
&self
.runtime
.lock()
.unwrap()
.block_on(resp.error_for_status()?.bytes())?,
)?))
}
async fn _broadcast(&self, transaction: &Transaction) -> Result<(), EsploraError> {
self.client
.post(&format!("{}/api/tx", self.url))
.body(serialize(transaction).to_hex())
.send()
.await?
fn _broadcast(&self, transaction: &Transaction) -> Result<(), EsploraError> {
self.runtime
.lock()
.unwrap()
.block_on(
self.client
.post(&format!("{}/api/tx", self.url))
.body(serialize(transaction).to_hex())
.send(),
)?
.error_for_status()?;
Ok(())
}
async fn _get_height(&self) -> Result<usize, EsploraError> {
fn _get_height(&self) -> Result<usize, EsploraError> {
let req = self.runtime.lock().unwrap().block_on(
self.client
.get(&format!("{}/api/blocks/tip/height", self.url))
.send(),
)?;
Ok(self
.client
.get(&format!("{}/api/blocks/tip/height", self.url))
.send()
.await?
.error_for_status()?
.text()
.await?
.runtime
.lock()
.unwrap()
.block_on(req.error_for_status()?.text())?
.parse()?)
}
@@ -238,32 +247,34 @@ impl UrlClient {
}
}
#[async_trait(?Send)]
impl ElectrumLikeSync for UrlClient {
async fn els_batch_script_get_history<'s, I: IntoIterator<Item = &'s Script>>(
fn els_batch_script_get_history<'s, I: IntoIterator<Item = &'s Script>>(
&mut self,
scripts: I,
) -> Result<Vec<Vec<ELSGetHistoryRes>>, Error> {
Ok(stream::iter(scripts)
.then(|script| self._script_get_history(&script))
.try_collect()
.await?)
self.runtime.lock().unwrap().block_on(async {
Ok(stream::iter(scripts)
.then(|script| self._script_get_history(&script))
.try_collect()
.await?)
})
}
async fn els_batch_script_list_unspent<'s, I: IntoIterator<Item = &'s Script>>(
fn els_batch_script_list_unspent<'s, I: IntoIterator<Item = &'s Script>>(
&mut self,
scripts: I,
) -> Result<Vec<Vec<ELSListUnspentRes>>, Error> {
Ok(stream::iter(scripts)
.then(|script| self._script_list_unspent(&script))
.try_collect()
.await?)
self.runtime.lock().unwrap().block_on(async {
Ok(stream::iter(scripts)
.then(|script| self._script_list_unspent(&script))
.try_collect()
.await?)
})
}
async fn els_transaction_get(&mut self, txid: &Txid) -> Result<Transaction, Error> {
fn els_transaction_get(&mut self, txid: &Txid) -> Result<Transaction, Error> {
Ok(self
._get_tx(txid)
.await?
._get_tx(txid)?
.ok_or_else(|| EsploraError::TransactionNotFound(*txid))?)
}
}

View File

@@ -41,29 +41,28 @@ impl Blockchain for OfflineBlockchain {
}
}
#[async_trait(?Send)]
pub trait OnlineBlockchain: Blockchain {
async fn get_capabilities(&self) -> HashSet<Capability>;
fn get_capabilities(&self) -> HashSet<Capability>;
async fn setup<D: BatchDatabase + DatabaseUtils, P: Progress>(
fn setup<D: BatchDatabase + DatabaseUtils, P: Progress>(
&mut self,
stop_gap: Option<usize>,
database: &mut D,
progress_update: P,
) -> Result<(), Error>;
async fn sync<D: BatchDatabase + DatabaseUtils, P: Progress>(
fn sync<D: BatchDatabase + DatabaseUtils, P: Progress>(
&mut self,
stop_gap: Option<usize>,
database: &mut D,
progress_update: P,
) -> Result<(), Error> {
self.setup(stop_gap, database, progress_update).await
self.setup(stop_gap, database, progress_update)
}
async fn get_tx(&mut self, txid: &Txid) -> Result<Option<Transaction>, Error>;
async fn broadcast(&mut self, tx: &Transaction) -> Result<(), Error>;
fn get_tx(&mut self, txid: &Txid) -> Result<Option<Transaction>, Error>;
fn broadcast(&mut self, tx: &Transaction) -> Result<(), Error>;
async fn get_height(&mut self) -> Result<usize, Error>;
fn get_height(&mut self) -> Result<usize, Error>;
}
pub type ProgressData = (f32, Option<String>);

View File

@@ -27,23 +27,22 @@ pub struct ELSListUnspentRes {
}
/// Implements the synchronization logic for an Electrum-like client.
#[async_trait(?Send)]
pub trait ElectrumLikeSync {
async fn els_batch_script_get_history<'s, I: IntoIterator<Item = &'s Script>>(
fn els_batch_script_get_history<'s, I: IntoIterator<Item = &'s Script>>(
&mut self,
scripts: I,
) -> Result<Vec<Vec<ELSGetHistoryRes>>, Error>;
async fn els_batch_script_list_unspent<'s, I: IntoIterator<Item = &'s Script>>(
fn els_batch_script_list_unspent<'s, I: IntoIterator<Item = &'s Script>>(
&mut self,
scripts: I,
) -> Result<Vec<Vec<ELSListUnspentRes>>, Error>;
async fn els_transaction_get(&mut self, txid: &Txid) -> Result<Transaction, Error>;
fn els_transaction_get(&mut self, txid: &Txid) -> Result<Transaction, Error>;
// Provided methods down here...
async fn electrum_like_setup<D: BatchDatabase + DatabaseUtils, P: Progress>(
fn electrum_like_setup<D: BatchDatabase + DatabaseUtils, P: Progress>(
&mut self,
stop_gap: Option<usize>,
database: &mut D,
@@ -86,7 +85,7 @@ pub trait ElectrumLikeSync {
let until = cmp::min(to_check_later.len(), batch_query_size);
let chunk: Vec<Script> = to_check_later.drain(..until).collect();
let call_result = self.els_batch_script_get_history(chunk.iter()).await?;
let call_result = self.els_batch_script_get_history(chunk.iter())?;
for (script, history) in chunk.into_iter().zip(call_result.into_iter()) {
trace!("received history for {:?}, size {}", script, history.len());
@@ -95,8 +94,7 @@ pub trait ElectrumLikeSync {
last_found = index;
let mut check_later_scripts = self
.check_history(database, script, history, &mut change_max_deriv)
.await?
.check_history(database, script, history, &mut change_max_deriv)?
.into_iter()
.filter(|x| already_checked.insert(x.clone()))
.collect();
@@ -126,7 +124,7 @@ pub trait ElectrumLikeSync {
let mut batch = database.begin_batch();
for chunk in ChunksIterator::new(database.iter_utxos()?.into_iter(), batch_query_size) {
let scripts: Vec<_> = chunk.iter().map(|u| &u.txout.script_pubkey).collect();
let call_result = self.els_batch_script_list_unspent(scripts).await?;
let call_result = self.els_batch_script_list_unspent(scripts)?;
// check which utxos are actually still unspent
for (utxo, list_unspent) in chunk.into_iter().zip(call_result.iter()) {
@@ -169,7 +167,7 @@ pub trait ElectrumLikeSync {
Ok(())
}
async fn check_tx_and_descendant<D: DatabaseUtils + BatchDatabase>(
fn check_tx_and_descendant<D: DatabaseUtils + BatchDatabase>(
&mut self,
database: &mut D,
txid: &Txid,
@@ -201,7 +199,7 @@ pub trait ElectrumLikeSync {
// went wrong
saved_tx.transaction.unwrap()
}
None => self.els_transaction_get(&txid).await?,
None => self.els_transaction_get(&txid)?,
};
let mut incoming: u64 = 0;
@@ -264,7 +262,7 @@ pub trait ElectrumLikeSync {
Ok(to_check_later)
}
async fn check_history<D: DatabaseUtils + BatchDatabase>(
fn check_history<D: DatabaseUtils + BatchDatabase>(
&mut self,
database: &mut D,
script_pubkey: Script,
@@ -286,17 +284,13 @@ pub trait ElectrumLikeSync {
x => u32::try_from(x).ok(),
};
to_check_later.extend_from_slice(
&self
.check_tx_and_descendant(
database,
&tx.tx_hash,
height,
&script_pubkey,
change_max_deriv,
)
.await?,
);
to_check_later.extend_from_slice(&self.check_tx_and_descendant(
database,
&tx.tx_hash,
height,
&script_pubkey,
change_max_deriv,
)?);
}
Ok(to_check_later)