Allow to set concurrency in Esplora config and optionally pass it in repl
This commit is contained in:
parent
4c59809f8e
commit
c9079a7292
@ -95,6 +95,9 @@ fn main() {
|
|||||||
let config = match matches.value_of("esplora") {
|
let config = match matches.value_of("esplora") {
|
||||||
Some(base_url) => AnyBlockchainConfig::Esplora(EsploraBlockchainConfig {
|
Some(base_url) => AnyBlockchainConfig::Esplora(EsploraBlockchainConfig {
|
||||||
base_url: base_url.to_string(),
|
base_url: base_url.to_string(),
|
||||||
|
concurrency: matches
|
||||||
|
.value_of("esplora_concurrency")
|
||||||
|
.and_then(|v| v.parse::<u8>().ok()),
|
||||||
}),
|
}),
|
||||||
None => AnyBlockchainConfig::Electrum(ElectrumBlockchainConfig {
|
None => AnyBlockchainConfig::Electrum(ElectrumBlockchainConfig {
|
||||||
url: matches.value_of("server").unwrap().to_string(),
|
url: matches.value_of("server").unwrap().to_string(),
|
||||||
|
@ -52,7 +52,7 @@
|
|||||||
//!
|
//!
|
||||||
//! # #[cfg(feature = "esplora")]
|
//! # #[cfg(feature = "esplora")]
|
||||||
//! # {
|
//! # {
|
||||||
//! let esplora_blockchain = EsploraBlockchain::new("...");
|
//! let esplora_blockchain = EsploraBlockchain::new("...", None);
|
||||||
//! let wallet_esplora: Wallet<AnyBlockchain, _> = Wallet::new(
|
//! let wallet_esplora: Wallet<AnyBlockchain, _> = Wallet::new(
|
||||||
//! "...",
|
//! "...",
|
||||||
//! None,
|
//! None,
|
||||||
|
@ -31,7 +31,7 @@
|
|||||||
//!
|
//!
|
||||||
//! ```no_run
|
//! ```no_run
|
||||||
//! # use bdk::blockchain::esplora::EsploraBlockchain;
|
//! # use bdk::blockchain::esplora::EsploraBlockchain;
|
||||||
//! let blockchain = EsploraBlockchain::new("https://blockstream.info/testnet/api");
|
//! let blockchain = EsploraBlockchain::new("https://blockstream.info/testnet/api", None);
|
||||||
//! # Ok::<(), bdk::Error>(())
|
//! # Ok::<(), bdk::Error>(())
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
@ -59,7 +59,7 @@ use crate::error::Error;
|
|||||||
use crate::wallet::utils::ChunksIterator;
|
use crate::wallet::utils::ChunksIterator;
|
||||||
use crate::FeeRate;
|
use crate::FeeRate;
|
||||||
|
|
||||||
const CONCURRENT: usize = 4;
|
const DEFAULT_CONCURRENT_REQUESTS: u8 = 4;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct UrlClient {
|
struct UrlClient {
|
||||||
@ -67,6 +67,7 @@ struct UrlClient {
|
|||||||
// We use the async client instead of the blocking one because it automatically uses `fetch`
|
// We use the async client instead of the blocking one because it automatically uses `fetch`
|
||||||
// when the target platform is wasm32.
|
// when the target platform is wasm32.
|
||||||
client: Client,
|
client: Client,
|
||||||
|
concurrency: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Structure that implements the logic to sync with Esplora
|
/// Structure that implements the logic to sync with Esplora
|
||||||
@ -84,10 +85,11 @@ impl std::convert::From<UrlClient> for EsploraBlockchain {
|
|||||||
|
|
||||||
impl EsploraBlockchain {
|
impl EsploraBlockchain {
|
||||||
/// Create a new instance of the client from a base URL
|
/// Create a new instance of the client from a base URL
|
||||||
pub fn new(base_url: &str) -> Self {
|
pub fn new(base_url: &str, concurrency: Option<u8>) -> Self {
|
||||||
EsploraBlockchain(UrlClient {
|
EsploraBlockchain(UrlClient {
|
||||||
url: base_url.to_string(),
|
url: base_url.to_string(),
|
||||||
client: Client::new(),
|
client: Client::new(),
|
||||||
|
concurrency: concurrency.unwrap_or(DEFAULT_CONCURRENT_REQUESTS),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -305,7 +307,7 @@ impl ElectrumLikeSync for UrlClient {
|
|||||||
) -> Result<Vec<Vec<ELSGetHistoryRes>>, Error> {
|
) -> Result<Vec<Vec<ELSGetHistoryRes>>, Error> {
|
||||||
let future = async {
|
let future = async {
|
||||||
let mut results = vec![];
|
let mut results = vec![];
|
||||||
for chunk in ChunksIterator::new(scripts.into_iter(), CONCURRENT) {
|
for chunk in ChunksIterator::new(scripts.into_iter(), self.concurrency as usize) {
|
||||||
let mut futs = FuturesOrdered::new();
|
let mut futs = FuturesOrdered::new();
|
||||||
for script in chunk {
|
for script in chunk {
|
||||||
futs.push(self._script_get_history(&script));
|
futs.push(self._script_get_history(&script));
|
||||||
@ -325,7 +327,7 @@ impl ElectrumLikeSync for UrlClient {
|
|||||||
) -> Result<Vec<Transaction>, Error> {
|
) -> Result<Vec<Transaction>, Error> {
|
||||||
let future = async {
|
let future = async {
|
||||||
let mut results = vec![];
|
let mut results = vec![];
|
||||||
for chunk in ChunksIterator::new(txids.into_iter(), CONCURRENT) {
|
for chunk in ChunksIterator::new(txids.into_iter(), self.concurrency as usize) {
|
||||||
let mut futs = FuturesOrdered::new();
|
let mut futs = FuturesOrdered::new();
|
||||||
for txid in chunk {
|
for txid in chunk {
|
||||||
futs.push(self._get_tx_no_opt(&txid));
|
futs.push(self._get_tx_no_opt(&txid));
|
||||||
@ -345,7 +347,7 @@ impl ElectrumLikeSync for UrlClient {
|
|||||||
) -> Result<Vec<BlockHeader>, Error> {
|
) -> Result<Vec<BlockHeader>, Error> {
|
||||||
let future = async {
|
let future = async {
|
||||||
let mut results = vec![];
|
let mut results = vec![];
|
||||||
for chunk in ChunksIterator::new(heights.into_iter(), CONCURRENT) {
|
for chunk in ChunksIterator::new(heights.into_iter(), self.concurrency as usize) {
|
||||||
let mut futs = FuturesOrdered::new();
|
let mut futs = FuturesOrdered::new();
|
||||||
for height in chunk {
|
for height in chunk {
|
||||||
futs.push(self._get_header(height));
|
futs.push(self._get_header(height));
|
||||||
@ -404,13 +406,17 @@ impl Into<BlockHeader> for EsploraHeader {
|
|||||||
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
||||||
pub struct EsploraBlockchainConfig {
|
pub struct EsploraBlockchainConfig {
|
||||||
pub base_url: String,
|
pub base_url: String,
|
||||||
|
pub concurrency: Option<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ConfigurableBlockchain for EsploraBlockchain {
|
impl ConfigurableBlockchain for EsploraBlockchain {
|
||||||
type Config = EsploraBlockchainConfig;
|
type Config = EsploraBlockchainConfig;
|
||||||
|
|
||||||
fn from_config(config: &Self::Config) -> Result<Self, Error> {
|
fn from_config(config: &Self::Config) -> Result<Self, Error> {
|
||||||
Ok(EsploraBlockchain::new(config.base_url.as_str()))
|
Ok(EsploraBlockchain::new(
|
||||||
|
config.base_url.as_str(),
|
||||||
|
config.concurrency,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
13
src/cli.rs
13
src/cli.rs
@ -362,14 +362,23 @@ pub fn add_global_flags<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if cfg!(feature = "esplora") {
|
if cfg!(feature = "esplora") {
|
||||||
app = app.arg(
|
app = app
|
||||||
|
.arg(
|
||||||
Arg::with_name("esplora")
|
Arg::with_name("esplora")
|
||||||
.short("e")
|
.short("e")
|
||||||
.long("esplora")
|
.long("esplora")
|
||||||
.value_name("ESPLORA")
|
.value_name("ESPLORA")
|
||||||
.help("Use the esplora server if given as parameter")
|
.help("Use the esplora server if given as parameter")
|
||||||
.takes_value(true),
|
.takes_value(true),
|
||||||
);
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("esplora_concurrency")
|
||||||
|
.long("esplora_concurrency")
|
||||||
|
.value_name("ESPLORA_CONCURRENCY")
|
||||||
|
.help("Concurrency of requests made to the esplora server")
|
||||||
|
.default_value("4")
|
||||||
|
.takes_value(true),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg!(feature = "electrum") {
|
if cfg!(feature = "electrum") {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user