Skip to content

Commit 0f94f24

Browse files
committed
feat(esplora)!: update to use new sync/full-scan structures
1 parent 4c52f3e commit 0f94f24

File tree

8 files changed

+244
-261
lines changed

8 files changed

+244
-261
lines changed

crates/esplora/src/async_ext.rs

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::collections::BTreeSet;
22

33
use async_trait::async_trait;
4+
use bdk_chain::spk_client::{FullScanRequest, FullScanResult, SyncRequest, SyncResult};
45
use bdk_chain::Anchor;
56
use bdk_chain::{
67
bitcoin::{BlockHash, OutPoint, ScriptBuf, TxOut, Txid},
@@ -11,7 +12,7 @@ use bdk_chain::{
1112
use esplora_client::{Amount, TxStatus};
1213
use futures::{stream::FuturesOrdered, TryStreamExt};
1314

14-
use crate::{anchor_from_status, FullScanUpdate, SyncUpdate};
15+
use crate::anchor_from_status;
1516

1617
/// [`esplora_client::Error`]
1718
type Error = Box<esplora_client::Error>;
@@ -50,14 +51,10 @@ pub trait EsploraAsyncExt {
5051
/// [`LocalChain::tip`]: bdk_chain::local_chain::LocalChain::tip
5152
async fn full_scan<K: Ord + Clone + Send>(
5253
&self,
53-
local_tip: CheckPoint,
54-
keychain_spks: BTreeMap<
55-
K,
56-
impl IntoIterator<IntoIter = impl Iterator<Item = (u32, ScriptBuf)> + Send> + Send,
57-
>,
54+
request: FullScanRequest<K>,
5855
stop_gap: usize,
5956
parallel_requests: usize,
60-
) -> Result<FullScanUpdate<K>, Error>;
57+
) -> Result<FullScanResult<K>, Error>;
6158

6259
/// Sync a set of scripts with the blockchain (via an Esplora client) for the data
6360
/// specified and return a [`TxGraph`].
@@ -75,55 +72,66 @@ pub trait EsploraAsyncExt {
7572
/// [`full_scan`]: EsploraAsyncExt::full_scan
7673
async fn sync(
7774
&self,
78-
local_tip: CheckPoint,
79-
misc_spks: impl IntoIterator<IntoIter = impl Iterator<Item = ScriptBuf> + Send> + Send,
80-
txids: impl IntoIterator<IntoIter = impl Iterator<Item = Txid> + Send> + Send,
81-
outpoints: impl IntoIterator<IntoIter = impl Iterator<Item = OutPoint> + Send> + Send,
75+
request: SyncRequest,
8276
parallel_requests: usize,
83-
) -> Result<SyncUpdate, Error>;
77+
) -> Result<SyncResult, Error>;
8478
}
8579

8680
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
8781
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
8882
impl EsploraAsyncExt for esplora_client::AsyncClient {
8983
async fn full_scan<K: Ord + Clone + Send>(
9084
&self,
91-
local_tip: CheckPoint,
92-
keychain_spks: BTreeMap<
93-
K,
94-
impl IntoIterator<IntoIter = impl Iterator<Item = (u32, ScriptBuf)> + Send> + Send,
95-
>,
85+
request: FullScanRequest<K>,
9686
stop_gap: usize,
9787
parallel_requests: usize,
98-
) -> Result<FullScanUpdate<K>, Error> {
88+
) -> Result<FullScanResult<K>, Error> {
9989
let latest_blocks = fetch_latest_blocks(self).await?;
100-
let (tx_graph, last_active_indices) =
101-
full_scan_for_index_and_graph(self, keychain_spks, stop_gap, parallel_requests).await?;
102-
let local_chain =
103-
chain_update(self, &latest_blocks, &local_tip, tx_graph.all_anchors()).await?;
104-
Ok(FullScanUpdate {
105-
local_chain,
106-
tx_graph,
90+
let (graph_update, last_active_indices) = full_scan_for_index_and_graph(
91+
self,
92+
request.spks_by_keychain,
93+
stop_gap,
94+
parallel_requests,
95+
)
96+
.await?;
97+
let chain_update = chain_update(
98+
self,
99+
&latest_blocks,
100+
&request.chain_tip,
101+
graph_update.all_anchors(),
102+
)
103+
.await?;
104+
Ok(FullScanResult {
105+
chain_update,
106+
graph_update,
107107
last_active_indices,
108108
})
109109
}
110110

111111
async fn sync(
112112
&self,
113-
local_tip: CheckPoint,
114-
misc_spks: impl IntoIterator<IntoIter = impl Iterator<Item = ScriptBuf> + Send> + Send,
115-
txids: impl IntoIterator<IntoIter = impl Iterator<Item = Txid> + Send> + Send,
116-
outpoints: impl IntoIterator<IntoIter = impl Iterator<Item = OutPoint> + Send> + Send,
113+
request: SyncRequest,
117114
parallel_requests: usize,
118-
) -> Result<SyncUpdate, Error> {
115+
) -> Result<SyncResult, Error> {
119116
let latest_blocks = fetch_latest_blocks(self).await?;
120-
let tx_graph =
121-
sync_for_index_and_graph(self, misc_spks, txids, outpoints, parallel_requests).await?;
122-
let local_chain =
123-
chain_update(self, &latest_blocks, &local_tip, tx_graph.all_anchors()).await?;
124-
Ok(SyncUpdate {
125-
tx_graph,
126-
local_chain,
117+
let graph_update = sync_for_index_and_graph(
118+
self,
119+
request.spks,
120+
request.txids,
121+
request.outpoints,
122+
parallel_requests,
123+
)
124+
.await?;
125+
let chain_update = chain_update(
126+
self,
127+
&latest_blocks,
128+
&request.chain_tip,
129+
graph_update.all_anchors(),
130+
)
131+
.await?;
132+
Ok(SyncResult {
133+
chain_update,
134+
graph_update,
127135
})
128136
}
129137
}

crates/esplora/src/blocking_ext.rs

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::thread::JoinHandle;
33
use std::usize;
44

55
use bdk_chain::collections::BTreeMap;
6+
use bdk_chain::spk_client::{FullScanRequest, FullScanResult, SyncRequest, SyncResult};
67
use bdk_chain::Anchor;
78
use bdk_chain::{
89
bitcoin::{Amount, BlockHash, OutPoint, ScriptBuf, TxOut, Txid},
@@ -12,8 +13,6 @@ use bdk_chain::{
1213
use esplora_client::TxStatus;
1314

1415
use crate::anchor_from_status;
15-
use crate::FullScanUpdate;
16-
use crate::SyncUpdate;
1716

1817
/// [`esplora_client::Error`]
1918
pub type Error = Box<esplora_client::Error>;
@@ -50,11 +49,10 @@ pub trait EsploraExt {
5049
/// [`LocalChain::tip`]: bdk_chain::local_chain::LocalChain::tip
5150
fn full_scan<K: Ord + Clone>(
5251
&self,
53-
local_tip: CheckPoint,
54-
keychain_spks: BTreeMap<K, impl IntoIterator<Item = (u32, ScriptBuf)>>,
52+
request: FullScanRequest<K>,
5553
stop_gap: usize,
5654
parallel_requests: usize,
57-
) -> Result<FullScanUpdate<K>, Error>;
55+
) -> Result<FullScanResult<K>, Error>;
5856

5957
/// Sync a set of scripts with the blockchain (via an Esplora client) for the data
6058
/// specified and return a [`TxGraph`].
@@ -70,59 +68,54 @@ pub trait EsploraExt {
7068
///
7169
/// [`LocalChain::tip`]: bdk_chain::local_chain::LocalChain::tip
7270
/// [`full_scan`]: EsploraExt::full_scan
73-
fn sync(
74-
&self,
75-
local_tip: CheckPoint,
76-
misc_spks: impl IntoIterator<Item = ScriptBuf>,
77-
txids: impl IntoIterator<Item = Txid>,
78-
outpoints: impl IntoIterator<Item = OutPoint>,
79-
parallel_requests: usize,
80-
) -> Result<SyncUpdate, Error>;
71+
fn sync(&self, request: SyncRequest, parallel_requests: usize) -> Result<SyncResult, Error>;
8172
}
8273

8374
impl EsploraExt for esplora_client::BlockingClient {
8475
fn full_scan<K: Ord + Clone>(
8576
&self,
86-
local_tip: CheckPoint,
87-
keychain_spks: BTreeMap<K, impl IntoIterator<Item = (u32, ScriptBuf)>>,
77+
request: FullScanRequest<K>,
8878
stop_gap: usize,
8979
parallel_requests: usize,
90-
) -> Result<FullScanUpdate<K>, Error> {
80+
) -> Result<FullScanResult<K>, Error> {
9181
let latest_blocks = fetch_latest_blocks(self)?;
92-
let (tx_graph, last_active_indices) = full_scan_for_index_and_graph_blocking(
82+
let (graph_update, last_active_indices) = full_scan_for_index_and_graph_blocking(
9383
self,
94-
keychain_spks,
84+
request.spks_by_keychain,
9585
stop_gap,
9686
parallel_requests,
9787
)?;
98-
let local_chain = chain_update(self, &latest_blocks, &local_tip, tx_graph.all_anchors())?;
99-
Ok(FullScanUpdate {
100-
local_chain,
101-
tx_graph,
88+
let chain_update = chain_update(
89+
self,
90+
&latest_blocks,
91+
&request.chain_tip,
92+
graph_update.all_anchors(),
93+
)?;
94+
Ok(FullScanResult {
95+
chain_update,
96+
graph_update,
10297
last_active_indices,
10398
})
10499
}
105100

106-
fn sync(
107-
&self,
108-
local_tip: CheckPoint,
109-
misc_spks: impl IntoIterator<Item = ScriptBuf>,
110-
txids: impl IntoIterator<Item = Txid>,
111-
outpoints: impl IntoIterator<Item = OutPoint>,
112-
parallel_requests: usize,
113-
) -> Result<SyncUpdate, Error> {
101+
fn sync(&self, request: SyncRequest, parallel_requests: usize) -> Result<SyncResult, Error> {
114102
let latest_blocks = fetch_latest_blocks(self)?;
115-
let tx_graph = sync_for_index_and_graph_blocking(
103+
let graph_update = sync_for_index_and_graph_blocking(
116104
self,
117-
misc_spks,
118-
txids,
119-
outpoints,
105+
request.spks,
106+
request.txids,
107+
request.outpoints,
120108
parallel_requests,
121109
)?;
122-
let local_chain = chain_update(self, &latest_blocks, &local_tip, tx_graph.all_anchors())?;
123-
Ok(SyncUpdate {
124-
local_chain,
125-
tx_graph,
110+
let chain_update = chain_update(
111+
self,
112+
&latest_blocks,
113+
&request.chain_tip,
114+
graph_update.all_anchors(),
115+
)?;
116+
Ok(SyncResult {
117+
chain_update,
118+
graph_update,
126119
})
127120
}
128121
}

crates/esplora/src/lib.rs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
//! [`TxGraph`]: bdk_chain::tx_graph::TxGraph
1717
//! [`example_esplora`]: https://github.com/bitcoindevkit/bdk/tree/master/example-crates/example_esplora
1818
19-
use std::collections::BTreeMap;
20-
21-
use bdk_chain::{local_chain::CheckPoint, BlockId, ConfirmationTimeHeightAnchor, TxGraph};
19+
use bdk_chain::{BlockId, ConfirmationTimeHeightAnchor};
2220
use esplora_client::TxStatus;
2321

2422
pub use esplora_client;
@@ -50,21 +48,3 @@ fn anchor_from_status(status: &TxStatus) -> Option<ConfirmationTimeHeightAnchor>
5048
None
5149
}
5250
}
53-
54-
/// Update returns from a full scan.
55-
pub struct FullScanUpdate<K> {
56-
/// The update to apply to the receiving [`LocalChain`](bdk_chain::local_chain::LocalChain).
57-
pub local_chain: CheckPoint,
58-
/// The update to apply to the receiving [`TxGraph`].
59-
pub tx_graph: TxGraph<ConfirmationTimeHeightAnchor>,
60-
/// Last active indices for the corresponding keychains (`K`).
61-
pub last_active_indices: BTreeMap<K, u32>,
62-
}
63-
64-
/// Update returned from a sync.
65-
pub struct SyncUpdate {
66-
/// The update to apply to the receiving [`LocalChain`](bdk_chain::local_chain::LocalChain).
67-
pub local_chain: CheckPoint,
68-
/// The update to apply to the receiving [`TxGraph`].
69-
pub tx_graph: TxGraph<ConfirmationTimeHeightAnchor>,
70-
}

0 commit comments

Comments
 (0)