[blockchain] Use async I/O in the various blockchain impls
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
use std::collections::HashSet;
|
||||
use std::io::{Read, Write};
|
||||
|
||||
#[allow(unused_imports)]
|
||||
use log::{debug, error, info, trace};
|
||||
|
||||
use bitcoin::{Script, Transaction, Txid};
|
||||
|
||||
use electrum_client::tokio::io::{AsyncRead, AsyncWrite};
|
||||
use electrum_client::Client;
|
||||
|
||||
use self::utils::{ELSGetHistoryRes, ELSListUnspentRes, ElectrumLikeSync};
|
||||
@@ -13,15 +13,15 @@ use super::*;
|
||||
use crate::database::{BatchDatabase, DatabaseUtils};
|
||||
use crate::error::Error;
|
||||
|
||||
pub struct ElectrumBlockchain<T: Read + Write>(Option<Client<T>>);
|
||||
pub struct ElectrumBlockchain<T: AsyncRead + AsyncWrite + Send>(Option<Client<T>>);
|
||||
|
||||
impl<T: Read + Write> std::convert::From<Client<T>> for ElectrumBlockchain<T> {
|
||||
impl<T: AsyncRead + AsyncWrite + Send> std::convert::From<Client<T>> for ElectrumBlockchain<T> {
|
||||
fn from(client: Client<T>) -> Self {
|
||||
ElectrumBlockchain(Some(client))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Read + Write> Blockchain for ElectrumBlockchain<T> {
|
||||
impl<T: AsyncRead + AsyncWrite + Send> Blockchain for ElectrumBlockchain<T> {
|
||||
fn offline() -> Self {
|
||||
ElectrumBlockchain(None)
|
||||
}
|
||||
@@ -31,14 +31,15 @@ impl<T: Read + Write> Blockchain for ElectrumBlockchain<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Read + Write> OnlineBlockchain for ElectrumBlockchain<T> {
|
||||
fn get_capabilities(&self) -> HashSet<Capability> {
|
||||
#[async_trait(?Send)]
|
||||
impl<T: AsyncRead + AsyncWrite + Send> OnlineBlockchain for ElectrumBlockchain<T> {
|
||||
async fn get_capabilities(&self) -> HashSet<Capability> {
|
||||
vec![Capability::FullHistory, Capability::GetAnyTx]
|
||||
.into_iter()
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn setup<D: BatchDatabase + DatabaseUtils, P: Progress>(
|
||||
async fn setup<D: BatchDatabase + DatabaseUtils, P: Progress>(
|
||||
&mut self,
|
||||
stop_gap: Option<usize>,
|
||||
database: &mut D,
|
||||
@@ -48,27 +49,30 @@ impl<T: Read + Write> OnlineBlockchain for ElectrumBlockchain<T> {
|
||||
.as_mut()
|
||||
.ok_or(Error::OfflineClient)?
|
||||
.electrum_like_setup(stop_gap, database, progress_update)
|
||||
.await
|
||||
}
|
||||
|
||||
fn get_tx(&mut self, txid: &Txid) -> Result<Option<Transaction>, Error> {
|
||||
async 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)?)
|
||||
}
|
||||
|
||||
fn broadcast(&mut self, tx: &Transaction) -> Result<(), Error> {
|
||||
async fn broadcast(&mut self, tx: &Transaction) -> Result<(), Error> {
|
||||
Ok(self
|
||||
.0
|
||||
.as_mut()
|
||||
.ok_or(Error::OfflineClient)?
|
||||
.transaction_broadcast(tx)
|
||||
.await
|
||||
.map(|_| ())?)
|
||||
}
|
||||
|
||||
fn get_height(&mut self) -> Result<usize, Error> {
|
||||
async 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
|
||||
@@ -76,16 +80,19 @@ impl<T: Read + Write> OnlineBlockchain for ElectrumBlockchain<T> {
|
||||
.as_mut()
|
||||
.ok_or(Error::OfflineClient)?
|
||||
.block_headers_subscribe()
|
||||
.await
|
||||
.map(|data| data.height)?)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Read + Write> ElectrumLikeSync for Client<T> {
|
||||
fn els_batch_script_get_history<'s, I: IntoIterator<Item = &'s Script>>(
|
||||
#[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>>(
|
||||
&mut self,
|
||||
scripts: I,
|
||||
) -> Result<Vec<Vec<ELSGetHistoryRes>>, Error> {
|
||||
self.batch_script_get_history(scripts)
|
||||
.await
|
||||
.map(|v| {
|
||||
v.into_iter()
|
||||
.map(|v| {
|
||||
@@ -105,11 +112,12 @@ impl<T: Read + Write> ElectrumLikeSync for Client<T> {
|
||||
.map_err(Error::Electrum)
|
||||
}
|
||||
|
||||
fn els_batch_script_list_unspent<'s, I: IntoIterator<Item = &'s Script>>(
|
||||
async 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| {
|
||||
@@ -133,7 +141,7 @@ impl<T: Read + Write> ElectrumLikeSync for Client<T> {
|
||||
.map_err(Error::Electrum)
|
||||
}
|
||||
|
||||
fn els_transaction_get(&mut self, txid: &Txid) -> Result<Transaction, Error> {
|
||||
self.transaction_get(txid).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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user