Remove async, upgrade electrum-client
This commit is contained in:
parent
c3923b66f8
commit
123984e99d
@ -11,12 +11,12 @@ miniscript = { version = "0.12" }
|
|||||||
serde = { version = "^1.0", features = ["derive"] }
|
serde = { version = "^1.0", features = ["derive"] }
|
||||||
serde_json = { version = "^1.0" }
|
serde_json = { version = "^1.0" }
|
||||||
base64 = "^0.11"
|
base64 = "^0.11"
|
||||||
async-trait = "0.1"
|
|
||||||
|
|
||||||
# Optional dependencies
|
# Optional dependencies
|
||||||
sled = { version = "0.31.0", optional = true }
|
sled = { version = "0.31.0", optional = true }
|
||||||
electrum-client = { git = "https://github.com/MagicalBitcoin/rust-electrum-client.git", optional = true }
|
electrum-client = { version = "0.2.0-beta.1", optional = true }
|
||||||
reqwest = { version = "0.10", optional = true, features = ["json"] }
|
reqwest = { version = "0.10", optional = true, features = ["json"] }
|
||||||
|
tokio = { version = "0.2", optional = true, features = ["rt-core"] }
|
||||||
futures = { version = "0.3", optional = true }
|
futures = { version = "0.3", optional = true }
|
||||||
clap = { version = "2.33", optional = true }
|
clap = { version = "2.33", optional = true }
|
||||||
|
|
||||||
@ -25,12 +25,11 @@ minimal = []
|
|||||||
compiler = ["miniscript/compiler"]
|
compiler = ["miniscript/compiler"]
|
||||||
default = ["key-value-db", "electrum"]
|
default = ["key-value-db", "electrum"]
|
||||||
electrum = ["electrum-client"]
|
electrum = ["electrum-client"]
|
||||||
esplora = ["reqwest", "futures"]
|
esplora = ["reqwest", "futures", "tokio"]
|
||||||
key-value-db = ["sled"]
|
key-value-db = ["sled"]
|
||||||
cli-utils = ["clap"]
|
cli-utils = ["clap"]
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tokio = { version = "0.2", features = ["macros"] }
|
|
||||||
lazy_static = "1.4"
|
lazy_static = "1.4"
|
||||||
rustyline = "6.0"
|
rustyline = "6.0"
|
||||||
dirs = "2.0"
|
dirs = "2.0"
|
||||||
|
@ -32,8 +32,7 @@ fn prepare_home_dir() -> PathBuf {
|
|||||||
dir
|
dir
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
fn main() {
|
||||||
async fn main() {
|
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
||||||
let app = cli::make_cli_subcommands();
|
let app = cli::make_cli_subcommands();
|
||||||
@ -65,9 +64,11 @@ async fn main() {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
debug!("database opened successfully");
|
debug!("database opened successfully");
|
||||||
|
|
||||||
let client = Client::new(matches.value_of("server").unwrap())
|
let client = Client::new(
|
||||||
.await
|
matches.value_of("server").unwrap(),
|
||||||
.unwrap();
|
matches.value_of("proxy"),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
let wallet = Wallet::new(
|
let wallet = Wallet::new(
|
||||||
descriptor,
|
descriptor,
|
||||||
change_descriptor,
|
change_descriptor,
|
||||||
@ -75,7 +76,6 @@ async fn main() {
|
|||||||
tree,
|
tree,
|
||||||
ElectrumBlockchain::from(client),
|
ElectrumBlockchain::from(client),
|
||||||
)
|
)
|
||||||
.await
|
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let wallet = Arc::new(wallet);
|
let wallet = Arc::new(wallet);
|
||||||
|
|
||||||
@ -101,9 +101,8 @@ async fn main() {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(s) = cli::handle_matches(&Arc::clone(&wallet), matches.unwrap())
|
if let Some(s) =
|
||||||
.await
|
cli::handle_matches(&Arc::clone(&wallet), matches.unwrap()).unwrap()
|
||||||
.unwrap()
|
|
||||||
{
|
{
|
||||||
println!("{}", s);
|
println!("{}", s);
|
||||||
}
|
}
|
||||||
@ -119,7 +118,7 @@ async fn main() {
|
|||||||
|
|
||||||
// rl.save_history("history.txt").unwrap();
|
// rl.save_history("history.txt").unwrap();
|
||||||
} else {
|
} else {
|
||||||
if let Some(s) = cli::handle_matches(&wallet, matches).await.unwrap() {
|
if let Some(s) = cli::handle_matches(&wallet, matches).unwrap() {
|
||||||
println!("{}", s);
|
println!("{}", s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,23 +5,22 @@ use log::{debug, error, info, trace};
|
|||||||
|
|
||||||
use bitcoin::{Script, Transaction, Txid};
|
use bitcoin::{Script, Transaction, Txid};
|
||||||
|
|
||||||
use electrum_client::tokio::io::{AsyncRead, AsyncWrite};
|
use electrum_client::{Client, ElectrumApi};
|
||||||
use electrum_client::Client;
|
|
||||||
|
|
||||||
use self::utils::{ELSGetHistoryRes, ELSListUnspentRes, ElectrumLikeSync};
|
use self::utils::{ELSGetHistoryRes, ELSListUnspentRes, ElectrumLikeSync};
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::database::{BatchDatabase, DatabaseUtils};
|
use crate::database::{BatchDatabase, DatabaseUtils};
|
||||||
use crate::error::Error;
|
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> {
|
impl std::convert::From<Client> for ElectrumBlockchain {
|
||||||
fn from(client: Client<T>) -> Self {
|
fn from(client: Client) -> Self {
|
||||||
ElectrumBlockchain(Some(client))
|
ElectrumBlockchain(Some(client))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: AsyncRead + AsyncWrite + Send> Blockchain for ElectrumBlockchain<T> {
|
impl Blockchain for ElectrumBlockchain {
|
||||||
fn offline() -> Self {
|
fn offline() -> Self {
|
||||||
ElectrumBlockchain(None)
|
ElectrumBlockchain(None)
|
||||||
}
|
}
|
||||||
@ -31,15 +30,14 @@ impl<T: AsyncRead + AsyncWrite + Send> Blockchain for ElectrumBlockchain<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait(?Send)]
|
impl OnlineBlockchain for ElectrumBlockchain {
|
||||||
impl<T: AsyncRead + AsyncWrite + Send> OnlineBlockchain for ElectrumBlockchain<T> {
|
fn get_capabilities(&self) -> HashSet<Capability> {
|
||||||
async fn get_capabilities(&self) -> HashSet<Capability> {
|
|
||||||
vec![Capability::FullHistory, Capability::GetAnyTx]
|
vec![Capability::FullHistory, Capability::GetAnyTx]
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn setup<D: BatchDatabase + DatabaseUtils, P: Progress>(
|
fn setup<D: BatchDatabase + DatabaseUtils, P: Progress>(
|
||||||
&mut self,
|
&mut self,
|
||||||
stop_gap: Option<usize>,
|
stop_gap: Option<usize>,
|
||||||
database: &mut D,
|
database: &mut D,
|
||||||
@ -49,30 +47,27 @@ impl<T: AsyncRead + AsyncWrite + Send> OnlineBlockchain for ElectrumBlockchain<T
|
|||||||
.as_mut()
|
.as_mut()
|
||||||
.ok_or(Error::OfflineClient)?
|
.ok_or(Error::OfflineClient)?
|
||||||
.electrum_like_setup(stop_gap, database, progress_update)
|
.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
|
Ok(self
|
||||||
.0
|
.0
|
||||||
.as_mut()
|
.as_mut()
|
||||||
.ok_or(Error::OfflineClient)?
|
.ok_or(Error::OfflineClient)?
|
||||||
.transaction_get(txid)
|
.transaction_get(txid)
|
||||||
.await
|
|
||||||
.map(Option::Some)?)
|
.map(Option::Some)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn broadcast(&mut self, tx: &Transaction) -> Result<(), Error> {
|
fn broadcast(&mut self, tx: &Transaction) -> Result<(), Error> {
|
||||||
Ok(self
|
Ok(self
|
||||||
.0
|
.0
|
||||||
.as_mut()
|
.as_mut()
|
||||||
.ok_or(Error::OfflineClient)?
|
.ok_or(Error::OfflineClient)?
|
||||||
.transaction_broadcast(tx)
|
.transaction_broadcast(tx)
|
||||||
.await
|
|
||||||
.map(|_| ())?)
|
.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?
|
// TODO: unsubscribe when added to the client, or is there a better call to use here?
|
||||||
|
|
||||||
Ok(self
|
Ok(self
|
||||||
@ -80,19 +75,16 @@ impl<T: AsyncRead + AsyncWrite + Send> OnlineBlockchain for ElectrumBlockchain<T
|
|||||||
.as_mut()
|
.as_mut()
|
||||||
.ok_or(Error::OfflineClient)?
|
.ok_or(Error::OfflineClient)?
|
||||||
.block_headers_subscribe()
|
.block_headers_subscribe()
|
||||||
.await
|
|
||||||
.map(|data| data.height)?)
|
.map(|data| data.height)?)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait(?Send)]
|
impl ElectrumLikeSync for Client {
|
||||||
impl<T: AsyncRead + AsyncWrite + Send> ElectrumLikeSync for Client<T> {
|
fn els_batch_script_get_history<'s, I: IntoIterator<Item = &'s Script>>(
|
||||||
async fn els_batch_script_get_history<'s, I: IntoIterator<Item = &'s Script>>(
|
|
||||||
&mut self,
|
&mut self,
|
||||||
scripts: I,
|
scripts: I,
|
||||||
) -> Result<Vec<Vec<ELSGetHistoryRes>>, Error> {
|
) -> Result<Vec<Vec<ELSGetHistoryRes>>, Error> {
|
||||||
self.batch_script_get_history(scripts)
|
self.batch_script_get_history(scripts)
|
||||||
.await
|
|
||||||
.map(|v| {
|
.map(|v| {
|
||||||
v.into_iter()
|
v.into_iter()
|
||||||
.map(|v| {
|
.map(|v| {
|
||||||
@ -112,12 +104,11 @@ impl<T: AsyncRead + AsyncWrite + Send> ElectrumLikeSync for Client<T> {
|
|||||||
.map_err(Error::Electrum)
|
.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,
|
&mut self,
|
||||||
scripts: I,
|
scripts: I,
|
||||||
) -> Result<Vec<Vec<ELSListUnspentRes>>, Error> {
|
) -> Result<Vec<Vec<ELSListUnspentRes>>, Error> {
|
||||||
self.batch_script_list_unspent(scripts)
|
self.batch_script_list_unspent(scripts)
|
||||||
.await
|
|
||||||
.map(|v| {
|
.map(|v| {
|
||||||
v.into_iter()
|
v.into_iter()
|
||||||
.map(|v| {
|
.map(|v| {
|
||||||
@ -141,7 +132,7 @@ impl<T: AsyncRead + AsyncWrite + Send> ElectrumLikeSync for Client<T> {
|
|||||||
.map_err(Error::Electrum)
|
.map_err(Error::Electrum)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn els_transaction_get(&mut self, txid: &Txid) -> Result<Transaction, Error> {
|
fn els_transaction_get(&mut self, txid: &Txid) -> Result<Transaction, Error> {
|
||||||
self.transaction_get(txid).await.map_err(Error::Electrum)
|
self.transaction_get(txid).map_err(Error::Electrum)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,16 @@
|
|||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
use std::sync::Mutex;
|
||||||
|
|
||||||
use futures::stream::{self, StreamExt, TryStreamExt};
|
use futures::stream::{self, StreamExt, TryStreamExt};
|
||||||
|
|
||||||
|
use tokio::runtime::Runtime;
|
||||||
|
|
||||||
#[allow(unused_imports)]
|
#[allow(unused_imports)]
|
||||||
use log::{debug, error, info, trace};
|
use log::{debug, error, info, trace};
|
||||||
|
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use reqwest::Client;
|
use reqwest::{Client, StatusCode};
|
||||||
use reqwest::StatusCode;
|
|
||||||
|
|
||||||
use bitcoin::consensus::{deserialize, serialize};
|
use bitcoin::consensus::{deserialize, serialize};
|
||||||
use bitcoin::hashes::hex::ToHex;
|
use bitcoin::hashes::hex::ToHex;
|
||||||
@ -23,7 +25,12 @@ use crate::error::Error;
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct UrlClient {
|
pub struct UrlClient {
|
||||||
url: String,
|
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,
|
client: Client,
|
||||||
|
|
||||||
|
runtime: Mutex<Runtime>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@ -40,6 +47,8 @@ impl EsploraBlockchain {
|
|||||||
EsploraBlockchain(Some(UrlClient {
|
EsploraBlockchain(Some(UrlClient {
|
||||||
url: base_url.to_string(),
|
url: base_url.to_string(),
|
||||||
client: Client::new(),
|
client: Client::new(),
|
||||||
|
|
||||||
|
runtime: Mutex::new(Runtime::new().unwrap()),
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -54,15 +63,14 @@ impl Blockchain for EsploraBlockchain {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait(?Send)]
|
|
||||||
impl OnlineBlockchain for EsploraBlockchain {
|
impl OnlineBlockchain for EsploraBlockchain {
|
||||||
async fn get_capabilities(&self) -> HashSet<Capability> {
|
fn get_capabilities(&self) -> HashSet<Capability> {
|
||||||
vec![Capability::FullHistory, Capability::GetAnyTx]
|
vec![Capability::FullHistory, Capability::GetAnyTx]
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn setup<D: BatchDatabase + DatabaseUtils, P: Progress>(
|
fn setup<D: BatchDatabase + DatabaseUtils, P: Progress>(
|
||||||
&mut self,
|
&mut self,
|
||||||
stop_gap: Option<usize>,
|
stop_gap: Option<usize>,
|
||||||
database: &mut D,
|
database: &mut D,
|
||||||
@ -72,34 +80,22 @@ impl OnlineBlockchain for EsploraBlockchain {
|
|||||||
.as_mut()
|
.as_mut()
|
||||||
.ok_or(Error::OfflineClient)?
|
.ok_or(Error::OfflineClient)?
|
||||||
.electrum_like_setup(stop_gap, database, progress_update)
|
.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
|
Ok(self
|
||||||
.0
|
.0
|
||||||
.as_mut()
|
.as_mut()
|
||||||
.ok_or(Error::OfflineClient)?
|
.ok_or(Error::OfflineClient)?
|
||||||
._get_tx(txid)
|
._broadcast(tx)?)
|
||||||
.await?)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn broadcast(&mut self, tx: &Transaction) -> Result<(), Error> {
|
fn get_height(&mut self) -> Result<usize, Error> {
|
||||||
Ok(self
|
Ok(self.0.as_mut().ok_or(Error::OfflineClient)?._get_height()?)
|
||||||
.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?)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,40 +104,53 @@ impl UrlClient {
|
|||||||
sha256::Hash::hash(script.as_bytes()).into_inner().to_hex()
|
sha256::Hash::hash(script.as_bytes()).into_inner().to_hex()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn _get_tx(&self, txid: &Txid) -> Result<Option<Transaction>, EsploraError> {
|
fn _get_tx(&self, txid: &Txid) -> Result<Option<Transaction>, EsploraError> {
|
||||||
let resp = self
|
let resp = self.runtime.lock().unwrap().block_on(
|
||||||
.client
|
self.client
|
||||||
.get(&format!("{}/api/tx/{}/raw", self.url, txid))
|
.get(&format!("{}/api/tx/{}/raw", self.url, txid))
|
||||||
.send()
|
.send(),
|
||||||
.await?;
|
)?;
|
||||||
|
|
||||||
if let StatusCode::NOT_FOUND = resp.status() {
|
if let StatusCode::NOT_FOUND = resp.status() {
|
||||||
return Ok(None);
|
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> {
|
fn _broadcast(&self, transaction: &Transaction) -> Result<(), EsploraError> {
|
||||||
self.client
|
self.runtime
|
||||||
.post(&format!("{}/api/tx", self.url))
|
.lock()
|
||||||
.body(serialize(transaction).to_hex())
|
.unwrap()
|
||||||
.send()
|
.block_on(
|
||||||
.await?
|
self.client
|
||||||
|
.post(&format!("{}/api/tx", self.url))
|
||||||
|
.body(serialize(transaction).to_hex())
|
||||||
|
.send(),
|
||||||
|
)?
|
||||||
.error_for_status()?;
|
.error_for_status()?;
|
||||||
|
|
||||||
Ok(())
|
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
|
Ok(self
|
||||||
.client
|
.runtime
|
||||||
.get(&format!("{}/api/blocks/tip/height", self.url))
|
.lock()
|
||||||
.send()
|
.unwrap()
|
||||||
.await?
|
.block_on(req.error_for_status()?.text())?
|
||||||
.error_for_status()?
|
|
||||||
.text()
|
|
||||||
.await?
|
|
||||||
.parse()?)
|
.parse()?)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -238,32 +247,34 @@ impl UrlClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait(?Send)]
|
|
||||||
impl ElectrumLikeSync for UrlClient {
|
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,
|
&mut self,
|
||||||
scripts: I,
|
scripts: I,
|
||||||
) -> Result<Vec<Vec<ELSGetHistoryRes>>, Error> {
|
) -> Result<Vec<Vec<ELSGetHistoryRes>>, Error> {
|
||||||
Ok(stream::iter(scripts)
|
self.runtime.lock().unwrap().block_on(async {
|
||||||
.then(|script| self._script_get_history(&script))
|
Ok(stream::iter(scripts)
|
||||||
.try_collect()
|
.then(|script| self._script_get_history(&script))
|
||||||
.await?)
|
.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,
|
&mut self,
|
||||||
scripts: I,
|
scripts: I,
|
||||||
) -> Result<Vec<Vec<ELSListUnspentRes>>, Error> {
|
) -> Result<Vec<Vec<ELSListUnspentRes>>, Error> {
|
||||||
Ok(stream::iter(scripts)
|
self.runtime.lock().unwrap().block_on(async {
|
||||||
.then(|script| self._script_list_unspent(&script))
|
Ok(stream::iter(scripts)
|
||||||
.try_collect()
|
.then(|script| self._script_list_unspent(&script))
|
||||||
.await?)
|
.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
|
Ok(self
|
||||||
._get_tx(txid)
|
._get_tx(txid)?
|
||||||
.await?
|
|
||||||
.ok_or_else(|| EsploraError::TransactionNotFound(*txid))?)
|
.ok_or_else(|| EsploraError::TransactionNotFound(*txid))?)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -41,29 +41,28 @@ impl Blockchain for OfflineBlockchain {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait(?Send)]
|
|
||||||
pub trait OnlineBlockchain: Blockchain {
|
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,
|
&mut self,
|
||||||
stop_gap: Option<usize>,
|
stop_gap: Option<usize>,
|
||||||
database: &mut D,
|
database: &mut D,
|
||||||
progress_update: P,
|
progress_update: P,
|
||||||
) -> Result<(), Error>;
|
) -> Result<(), Error>;
|
||||||
async fn sync<D: BatchDatabase + DatabaseUtils, P: Progress>(
|
fn sync<D: BatchDatabase + DatabaseUtils, P: Progress>(
|
||||||
&mut self,
|
&mut self,
|
||||||
stop_gap: Option<usize>,
|
stop_gap: Option<usize>,
|
||||||
database: &mut D,
|
database: &mut D,
|
||||||
progress_update: P,
|
progress_update: P,
|
||||||
) -> Result<(), Error> {
|
) -> 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>;
|
fn get_tx(&mut self, txid: &Txid) -> Result<Option<Transaction>, Error>;
|
||||||
async fn broadcast(&mut self, tx: &Transaction) -> Result<(), 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>);
|
pub type ProgressData = (f32, Option<String>);
|
||||||
|
@ -27,23 +27,22 @@ pub struct ELSListUnspentRes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Implements the synchronization logic for an Electrum-like client.
|
/// Implements the synchronization logic for an Electrum-like client.
|
||||||
#[async_trait(?Send)]
|
|
||||||
pub trait ElectrumLikeSync {
|
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,
|
&mut self,
|
||||||
scripts: I,
|
scripts: I,
|
||||||
) -> Result<Vec<Vec<ELSGetHistoryRes>>, Error>;
|
) -> 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,
|
&mut self,
|
||||||
scripts: I,
|
scripts: I,
|
||||||
) -> Result<Vec<Vec<ELSListUnspentRes>>, Error>;
|
) -> 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...
|
// 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,
|
&mut self,
|
||||||
stop_gap: Option<usize>,
|
stop_gap: Option<usize>,
|
||||||
database: &mut D,
|
database: &mut D,
|
||||||
@ -86,7 +85,7 @@ pub trait ElectrumLikeSync {
|
|||||||
|
|
||||||
let until = cmp::min(to_check_later.len(), batch_query_size);
|
let until = cmp::min(to_check_later.len(), batch_query_size);
|
||||||
let chunk: Vec<Script> = to_check_later.drain(..until).collect();
|
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()) {
|
for (script, history) in chunk.into_iter().zip(call_result.into_iter()) {
|
||||||
trace!("received history for {:?}, size {}", script, history.len());
|
trace!("received history for {:?}, size {}", script, history.len());
|
||||||
@ -95,8 +94,7 @@ pub trait ElectrumLikeSync {
|
|||||||
last_found = index;
|
last_found = index;
|
||||||
|
|
||||||
let mut check_later_scripts = self
|
let mut check_later_scripts = self
|
||||||
.check_history(database, script, history, &mut change_max_deriv)
|
.check_history(database, script, history, &mut change_max_deriv)?
|
||||||
.await?
|
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|x| already_checked.insert(x.clone()))
|
.filter(|x| already_checked.insert(x.clone()))
|
||||||
.collect();
|
.collect();
|
||||||
@ -126,7 +124,7 @@ pub trait ElectrumLikeSync {
|
|||||||
let mut batch = database.begin_batch();
|
let mut batch = database.begin_batch();
|
||||||
for chunk in ChunksIterator::new(database.iter_utxos()?.into_iter(), batch_query_size) {
|
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 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
|
// check which utxos are actually still unspent
|
||||||
for (utxo, list_unspent) in chunk.into_iter().zip(call_result.iter()) {
|
for (utxo, list_unspent) in chunk.into_iter().zip(call_result.iter()) {
|
||||||
@ -169,7 +167,7 @@ pub trait ElectrumLikeSync {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn check_tx_and_descendant<D: DatabaseUtils + BatchDatabase>(
|
fn check_tx_and_descendant<D: DatabaseUtils + BatchDatabase>(
|
||||||
&mut self,
|
&mut self,
|
||||||
database: &mut D,
|
database: &mut D,
|
||||||
txid: &Txid,
|
txid: &Txid,
|
||||||
@ -201,7 +199,7 @@ pub trait ElectrumLikeSync {
|
|||||||
// went wrong
|
// went wrong
|
||||||
saved_tx.transaction.unwrap()
|
saved_tx.transaction.unwrap()
|
||||||
}
|
}
|
||||||
None => self.els_transaction_get(&txid).await?,
|
None => self.els_transaction_get(&txid)?,
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut incoming: u64 = 0;
|
let mut incoming: u64 = 0;
|
||||||
@ -264,7 +262,7 @@ pub trait ElectrumLikeSync {
|
|||||||
Ok(to_check_later)
|
Ok(to_check_later)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn check_history<D: DatabaseUtils + BatchDatabase>(
|
fn check_history<D: DatabaseUtils + BatchDatabase>(
|
||||||
&mut self,
|
&mut self,
|
||||||
database: &mut D,
|
database: &mut D,
|
||||||
script_pubkey: Script,
|
script_pubkey: Script,
|
||||||
@ -286,17 +284,13 @@ pub trait ElectrumLikeSync {
|
|||||||
x => u32::try_from(x).ok(),
|
x => u32::try_from(x).ok(),
|
||||||
};
|
};
|
||||||
|
|
||||||
to_check_later.extend_from_slice(
|
to_check_later.extend_from_slice(&self.check_tx_and_descendant(
|
||||||
&self
|
database,
|
||||||
.check_tx_and_descendant(
|
&tx.tx_hash,
|
||||||
database,
|
height,
|
||||||
&tx.tx_hash,
|
&script_pubkey,
|
||||||
height,
|
change_max_deriv,
|
||||||
&script_pubkey,
|
)?);
|
||||||
change_max_deriv,
|
|
||||||
)
|
|
||||||
.await?,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(to_check_later)
|
Ok(to_check_later)
|
||||||
|
14
src/cli.rs
14
src/cli.rs
@ -242,6 +242,14 @@ pub fn add_global_flags<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> {
|
|||||||
.takes_value(true)
|
.takes_value(true)
|
||||||
.default_value("tn.not.fyi:55001"),
|
.default_value("tn.not.fyi:55001"),
|
||||||
)
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("proxy")
|
||||||
|
.short("p")
|
||||||
|
.long("proxy")
|
||||||
|
.value_name("SERVER:PORT")
|
||||||
|
.help("Sets the SOCKS5 proxy for the Electrum client")
|
||||||
|
.takes_value(true),
|
||||||
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::with_name("descriptor")
|
Arg::with_name("descriptor")
|
||||||
.short("d")
|
.short("d")
|
||||||
@ -268,7 +276,7 @@ pub fn add_global_flags<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> {
|
|||||||
.subcommand(SubCommand::with_name("repl").about("Opens an interactive shell"))
|
.subcommand(SubCommand::with_name("repl").about("Opens an interactive shell"))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn handle_matches<C, D>(
|
pub fn handle_matches<C, D>(
|
||||||
wallet: &Wallet<C, D>,
|
wallet: &Wallet<C, D>,
|
||||||
matches: ArgMatches<'_>,
|
matches: ArgMatches<'_>,
|
||||||
) -> Result<Option<String>, Error>
|
) -> Result<Option<String>, Error>
|
||||||
@ -279,7 +287,7 @@ where
|
|||||||
if let Some(_sub_matches) = matches.subcommand_matches("get_new_address") {
|
if let Some(_sub_matches) = matches.subcommand_matches("get_new_address") {
|
||||||
Ok(Some(format!("{}", wallet.get_new_address()?)))
|
Ok(Some(format!("{}", wallet.get_new_address()?)))
|
||||||
} else if let Some(_sub_matches) = matches.subcommand_matches("sync") {
|
} else if let Some(_sub_matches) = matches.subcommand_matches("sync") {
|
||||||
wallet.sync(None, None).await?;
|
wallet.sync(None, None)?;
|
||||||
Ok(None)
|
Ok(None)
|
||||||
} else if let Some(_sub_matches) = matches.subcommand_matches("list_unspent") {
|
} else if let Some(_sub_matches) = matches.subcommand_matches("list_unspent") {
|
||||||
let mut res = String::new();
|
let mut res = String::new();
|
||||||
@ -374,7 +382,7 @@ where
|
|||||||
panic!("Missing `psbt` and `tx` option");
|
panic!("Missing `psbt` and `tx` option");
|
||||||
};
|
};
|
||||||
|
|
||||||
let txid = wallet.broadcast(tx).await?;
|
let txid = wallet.broadcast(tx)?;
|
||||||
|
|
||||||
Ok(Some(format!("TXID: {}", txid)))
|
Ok(Some(format!("TXID: {}", txid)))
|
||||||
} else if let Some(sub_matches) = matches.subcommand_matches("extract_psbt") {
|
} else if let Some(sub_matches) = matches.subcommand_matches("extract_psbt") {
|
||||||
|
@ -9,9 +9,6 @@ extern crate serde_json;
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate lazy_static;
|
extern crate lazy_static;
|
||||||
|
|
||||||
#[macro_use]
|
|
||||||
extern crate async_trait;
|
|
||||||
|
|
||||||
#[cfg(feature = "electrum")]
|
#[cfg(feature = "electrum")]
|
||||||
pub extern crate electrum_client;
|
pub extern crate electrum_client;
|
||||||
#[cfg(feature = "electrum")]
|
#[cfg(feature = "electrum")]
|
||||||
|
@ -709,7 +709,7 @@ where
|
|||||||
B: OnlineBlockchain,
|
B: OnlineBlockchain,
|
||||||
D: BatchDatabase,
|
D: BatchDatabase,
|
||||||
{
|
{
|
||||||
pub async fn new(
|
pub fn new(
|
||||||
descriptor: &str,
|
descriptor: &str,
|
||||||
change_descriptor: Option<&str>,
|
change_descriptor: Option<&str>,
|
||||||
network: Network,
|
network: Network,
|
||||||
@ -738,7 +738,7 @@ where
|
|||||||
None => None,
|
None => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let current_height = Some(client.get_height().await? as u32);
|
let current_height = Some(client.get_height()? as u32);
|
||||||
|
|
||||||
Ok(Wallet {
|
Ok(Wallet {
|
||||||
descriptor,
|
descriptor,
|
||||||
@ -752,7 +752,7 @@ where
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn sync(
|
pub fn sync(
|
||||||
&self,
|
&self,
|
||||||
max_address: Option<u32>,
|
max_address: Option<u32>,
|
||||||
_batch_query_size: Option<usize>,
|
_batch_query_size: Option<usize>,
|
||||||
@ -811,18 +811,15 @@ where
|
|||||||
self.database.borrow_mut().commit_batch(address_batch)?;
|
self.database.borrow_mut().commit_batch(address_batch)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.client
|
self.client.borrow_mut().sync(
|
||||||
.borrow_mut()
|
None,
|
||||||
.sync(
|
self.database.borrow_mut().deref_mut(),
|
||||||
None,
|
noop_progress(),
|
||||||
self.database.borrow_mut().deref_mut(),
|
)
|
||||||
noop_progress(),
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn broadcast(&self, tx: Transaction) -> Result<Txid, Error> {
|
pub fn broadcast(&self, tx: Transaction) -> Result<Txid, Error> {
|
||||||
self.client.borrow_mut().broadcast(&tx).await?;
|
self.client.borrow_mut().broadcast(&tx)?;
|
||||||
|
|
||||||
Ok(tx.txid())
|
Ok(tx.txid())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user