use electurm-client updated

This commit is contained in:
Riccardo Casatta 2020-11-24 12:16:49 +01:00
parent fc3b6ad0b9
commit 6d1d5d5f57
No known key found for this signature in database
GPG Key ID: FD986A969E450397
7 changed files with 26 additions and 15 deletions

View File

@ -15,7 +15,7 @@ rand = "^0.7"
# Optional dependencies # Optional dependencies
sled = { version = "0.34", optional = true } sled = { version = "0.34", optional = true }
electrum-client = { version = "0.3.0-beta.1", optional = true } electrum-client = { version = "0.4.0-beta.1", optional = true }
reqwest = { version = "0.10", optional = true, features = ["json"] } reqwest = { version = "0.10", optional = true, features = ["json"] }
futures = { version = "0.3", optional = true } futures = { version = "0.3", optional = true }
clap = { version = "2.33", optional = true } clap = { version = "2.33", optional = true }

View File

@ -102,6 +102,8 @@ fn main() {
None => AnyBlockchainConfig::Electrum(ElectrumBlockchainConfig { None => AnyBlockchainConfig::Electrum(ElectrumBlockchainConfig {
url: matches.value_of("server").unwrap().to_string(), url: matches.value_of("server").unwrap().to_string(),
socks5: matches.value_of("proxy").map(ToString::to_string), socks5: matches.value_of("proxy").map(ToString::to_string),
retry: 10,
timeout: 10,
}), }),
}; };
let wallet = Arc::new( let wallet = Arc::new(

View File

@ -40,7 +40,7 @@
//! # use bdk::Wallet; //! # use bdk::Wallet;
//! # #[cfg(feature = "electrum")] //! # #[cfg(feature = "electrum")]
//! # { //! # {
//! let electrum_blockchain = ElectrumBlockchain::from(electrum_client::Client::new("...", None)?); //! let electrum_blockchain = ElectrumBlockchain::from(electrum_client::Client::new("...")?);
//! let wallet_electrum: Wallet<AnyBlockchain, _> = Wallet::new( //! let wallet_electrum: Wallet<AnyBlockchain, _> = Wallet::new(
//! "...", //! "...",
//! None, //! None,

View File

@ -32,7 +32,7 @@
//! //!
//! ```no_run //! ```no_run
//! # use bdk::blockchain::electrum::ElectrumBlockchain; //! # use bdk::blockchain::electrum::ElectrumBlockchain;
//! let client = electrum_client::Client::new("ssl://electrum.blockstream.info:50002", None)?; //! let client = electrum_client::Client::new("ssl://electrum.blockstream.info:50002")?;
//! let blockchain = ElectrumBlockchain::from(client); //! let blockchain = ElectrumBlockchain::from(client);
//! # Ok::<(), bdk::Error>(()) //! # Ok::<(), bdk::Error>(())
//! ``` //! ```
@ -44,7 +44,7 @@ use log::{debug, error, info, trace};
use bitcoin::{BlockHeader, Script, Transaction, Txid}; use bitcoin::{BlockHeader, Script, Transaction, Txid};
use electrum_client::{Client, ElectrumApi}; use electrum_client::{Client, ConfigBuilder, ElectrumApi, Socks5Config};
use self::utils::{ELSGetHistoryRes, ElectrumLikeSync}; use self::utils::{ELSGetHistoryRes, ElectrumLikeSync};
use super::*; use super::*;
@ -62,7 +62,7 @@ pub struct ElectrumBlockchain(Client);
#[cfg(feature = "test-electrum")] #[cfg(feature = "test-electrum")]
#[bdk_blockchain_tests(crate)] #[bdk_blockchain_tests(crate)]
fn local_electrs() -> ElectrumBlockchain { fn local_electrs() -> ElectrumBlockchain {
ElectrumBlockchain::from(Client::new(&testutils::get_electrum_url(), None).unwrap()) ElectrumBlockchain::from(Client::new(&testutils::get_electrum_url()).unwrap())
} }
impl std::convert::From<Client> for ElectrumBlockchain { impl std::convert::From<Client> for ElectrumBlockchain {
@ -117,7 +117,7 @@ impl Blockchain for ElectrumBlockchain {
} }
impl ElectrumLikeSync for Client { impl ElectrumLikeSync for Client {
fn els_batch_script_get_history<'s, I: IntoIterator<Item = &'s Script>>( fn els_batch_script_get_history<'s, I: IntoIterator<Item = &'s Script> + Clone>(
&self, &self,
scripts: I, scripts: I,
) -> Result<Vec<Vec<ELSGetHistoryRes>>, Error> { ) -> Result<Vec<Vec<ELSGetHistoryRes>>, Error> {
@ -141,14 +141,14 @@ impl ElectrumLikeSync for Client {
.map_err(Error::Electrum) .map_err(Error::Electrum)
} }
fn els_batch_transaction_get<'s, I: IntoIterator<Item = &'s Txid>>( fn els_batch_transaction_get<'s, I: IntoIterator<Item = &'s Txid> + Clone>(
&self, &self,
txids: I, txids: I,
) -> Result<Vec<Transaction>, Error> { ) -> Result<Vec<Transaction>, Error> {
self.batch_transaction_get(txids).map_err(Error::Electrum) self.batch_transaction_get(txids).map_err(Error::Electrum)
} }
fn els_batch_block_header<I: IntoIterator<Item = u32>>( fn els_batch_block_header<I: IntoIterator<Item = u32> + Clone>(
&self, &self,
heights: I, heights: I,
) -> Result<Vec<BlockHeader>, Error> { ) -> Result<Vec<BlockHeader>, Error> {
@ -161,15 +161,24 @@ impl ElectrumLikeSync for Client {
pub struct ElectrumBlockchainConfig { pub struct ElectrumBlockchainConfig {
pub url: String, pub url: String,
pub socks5: Option<String>, pub socks5: Option<String>,
pub retry: u8,
pub timeout: u8,
} }
impl ConfigurableBlockchain for ElectrumBlockchain { impl ConfigurableBlockchain for ElectrumBlockchain {
type Config = ElectrumBlockchainConfig; type Config = ElectrumBlockchainConfig;
fn from_config(config: &Self::Config) -> Result<Self, Error> { fn from_config(config: &Self::Config) -> Result<Self, Error> {
Ok(ElectrumBlockchain(Client::new( let socks5 = config.socks5.as_ref().map(Socks5Config::new);
let electrum_config = ConfigBuilder::new()
.retry(config.retry)
.socks5(socks5)?
.timeout(config.timeout)?
.build();
Ok(ElectrumBlockchain(Client::from_config(
config.url.as_str(), config.url.as_str(),
config.socks5.as_deref(), electrum_config,
)?)) )?))
} }
} }

View File

@ -47,17 +47,17 @@ pub struct ELSGetHistoryRes {
/// Implements the synchronization logic for an Electrum-like client. /// Implements the synchronization logic for an Electrum-like client.
#[maybe_async] #[maybe_async]
pub trait ElectrumLikeSync { pub trait ElectrumLikeSync {
fn els_batch_script_get_history<'s, I: IntoIterator<Item = &'s Script>>( fn els_batch_script_get_history<'s, I: IntoIterator<Item = &'s Script> + Clone>(
&self, &self,
scripts: I, scripts: I,
) -> Result<Vec<Vec<ELSGetHistoryRes>>, Error>; ) -> Result<Vec<Vec<ELSGetHistoryRes>>, Error>;
fn els_batch_transaction_get<'s, I: IntoIterator<Item = &'s Txid>>( fn els_batch_transaction_get<'s, I: IntoIterator<Item = &'s Txid> + Clone>(
&self, &self,
txids: I, txids: I,
) -> Result<Vec<Transaction>, Error>; ) -> Result<Vec<Transaction>, Error>;
fn els_batch_block_header<I: IntoIterator<Item = u32>>( fn els_batch_block_header<I: IntoIterator<Item = u32> + Clone>(
&self, &self,
heights: I, heights: I,
) -> Result<Vec<BlockHeader>, Error>; ) -> Result<Vec<BlockHeader>, Error>;

View File

@ -16,4 +16,4 @@ serde_json = "1.0"
serial_test = "0.4" serial_test = "0.4"
bitcoin = "0.25" bitcoin = "0.25"
bitcoincore-rpc = "0.12" bitcoincore-rpc = "0.12"
electrum-client = "0.3.0-beta.1" electrum-client = "0.4.0-beta.1"

View File

@ -270,7 +270,7 @@ impl TestClient {
pub fn new() -> Self { pub fn new() -> Self {
let url = env::var("MAGICAL_RPC_URL").unwrap_or("127.0.0.1:18443".to_string()); let url = env::var("MAGICAL_RPC_URL").unwrap_or("127.0.0.1:18443".to_string());
let client = RpcClient::new(format!("http://{}", url), get_auth()).unwrap(); let client = RpcClient::new(format!("http://{}", url), get_auth()).unwrap();
let electrum = ElectrumClient::new(&get_electrum_url(), None).unwrap(); let electrum = ElectrumClient::new(&get_electrum_url()).unwrap();
TestClient { client, electrum } TestClient { client, electrum }
} }