diff --git a/crates/esplora/src/async_ext.rs b/crates/esplora/src/async_ext.rs index 8e697a2a9a..91f419b40e 100644 --- a/crates/esplora/src/async_ext.rs +++ b/crates/esplora/src/async_ext.rs @@ -1,7 +1,8 @@ use async_trait::async_trait; use bdk_chain::collections::btree_map; +use bdk_chain::spk_client::{FullScanRequest, SyncRequest}; use bdk_chain::{ - bitcoin::{BlockHash, OutPoint, ScriptBuf, Txid}, + bitcoin::{BlockHash, ScriptBuf, Txid}, collections::BTreeMap, local_chain::{self, CheckPoint}, BlockId, ConfirmationTimeHeightAnchor, TxGraph, @@ -43,9 +44,7 @@ pub trait EsploraAsyncExt { ) -> Result; /// Full scan the keychain scripts specified with the blockchain (via an Esplora client) and - /// returns a [`TxGraph`] and a map of last active indices. - /// - /// * `keychain_spks`: keychains that we want to scan transactions for + /// returns a [`TxGraph`] and a map of keychain last active indices. /// /// The full scan for each keychain stops after a gap of `stop_gap` script pubkeys with no associated /// transactions. `parallel_requests` specifies the max number of HTTP requests to make in @@ -53,7 +52,7 @@ pub trait EsploraAsyncExt { #[allow(clippy::result_large_err)] async fn full_scan( &self, - keychain_spks: BTreeMap< + request: FullScanRequest< K, impl IntoIterator + Send> + Send, >, @@ -76,9 +75,7 @@ pub trait EsploraAsyncExt { #[allow(clippy::result_large_err)] async fn sync( &self, - misc_spks: impl IntoIterator + Send> + Send, - txids: impl IntoIterator + Send> + Send, - outpoints: impl IntoIterator + Send> + Send, + request: SyncRequest, parallel_requests: usize, ) -> Result, Error>; } @@ -151,7 +148,7 @@ impl EsploraAsyncExt for esplora_client::AsyncClient { async fn full_scan( &self, - keychain_spks: BTreeMap< + request: FullScanRequest< K, impl IntoIterator + Send> + Send, >, @@ -161,13 +158,12 @@ impl EsploraAsyncExt for esplora_client::AsyncClient { type TxsOfSpkIndex = (u32, Vec); let parallel_requests = Ord::max(parallel_requests, 1); let mut graph = TxGraph::::default(); - let mut last_active_indexes = BTreeMap::::new(); + let mut last_active_indices = BTreeMap::::new(); - for (keychain, spks) in keychain_spks { + for (keychain, spks) in request.spks_by_keychain { let mut spks = spks.into_iter(); let mut last_index = Option::::None; let mut last_active_index = Option::::None; - loop { let handles = spks .by_ref() @@ -219,37 +215,36 @@ impl EsploraAsyncExt for esplora_client::AsyncClient { } if let Some(last_active_index) = last_active_index { - last_active_indexes.insert(keychain, last_active_index); + last_active_indices.insert(keychain, last_active_index); } } - Ok((graph, last_active_indexes)) + Ok((graph, last_active_indices)) } async fn sync( &self, - misc_spks: impl IntoIterator + Send> + Send, - txids: impl IntoIterator + Send> + Send, - outpoints: impl IntoIterator + Send> + Send, + request: SyncRequest, parallel_requests: usize, ) -> Result, Error> { + let full_scan_request = FullScanRequest { + spks_by_keychain: [( + 0, + request + .spks + .into_iter() + .enumerate() + .map(|(i, spk)| (i as u32, spk)), + )] + .into(), + checkpoint: request.checkpoint, + }; let mut graph = self - .full_scan( - [( - (), - misc_spks - .into_iter() - .enumerate() - .map(|(i, spk)| (i as u32, spk)), - )] - .into(), - usize::MAX, - parallel_requests, - ) + .full_scan(full_scan_request, usize::MAX, parallel_requests) .await .map(|(g, _)| g)?; - let mut txids = txids.into_iter(); + let mut txids = request.txids.into_iter(); loop { let handles = txids .by_ref() @@ -257,7 +252,9 @@ impl EsploraAsyncExt for esplora_client::AsyncClient { .filter(|&txid| graph.get_tx(txid).is_none()) .map(|txid| { let client = self.clone(); - async move { client.get_tx_status(&txid).await.map(|s| (txid, s)) } + async move { + client.get_tx_status(&txid).await.map(|s| (txid, s)) + } }) .collect::>(); @@ -272,7 +269,7 @@ impl EsploraAsyncExt for esplora_client::AsyncClient { } } - for op in outpoints.into_iter() { + for op in request.outpoints.into_iter() { if graph.get_tx(op.txid).is_none() { if let Some(tx) = self.get_tx(&op.txid).await? { let _ = graph.insert_tx(tx);