Skip to content

Commit 171df89

Browse files
committed
refactor(core): spk_client types take generics
1 parent c55744d commit 171df89

File tree

1 file changed

+50
-46
lines changed

1 file changed

+50
-46
lines changed

crates/core/src/spk_client.rs

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
collections::BTreeMap,
55
CheckPoint, ConfirmationBlockTime, Indexed,
66
};
7-
use bitcoin::{OutPoint, Script, ScriptBuf, Txid};
7+
use bitcoin::{BlockHash, OutPoint, Script, ScriptBuf, Txid};
88

99
type InspectSync<I> = dyn FnMut(SyncItem<I>, SyncProgress) + Send + 'static;
1010

@@ -90,8 +90,8 @@ impl SyncProgress {
9090
///
9191
/// Construct with [`SyncRequest::builder`].
9292
#[must_use]
93-
pub struct SyncRequestBuilder<I = ()> {
94-
inner: SyncRequest<I>,
93+
pub struct SyncRequestBuilder<I = (), B = BlockHash> {
94+
inner: SyncRequest<I, B>,
9595
}
9696

9797
impl SyncRequestBuilder<()> {
@@ -101,11 +101,11 @@ impl SyncRequestBuilder<()> {
101101
}
102102
}
103103

104-
impl<I> SyncRequestBuilder<I> {
104+
impl<I, B> SyncRequestBuilder<I, B> {
105105
/// Set the initial chain tip for the sync request.
106106
///
107107
/// This is used to update [`LocalChain`](../../bdk_chain/local_chain/struct.LocalChain.html).
108-
pub fn chain_tip(mut self, cp: CheckPoint) -> Self {
108+
pub fn chain_tip(mut self, cp: CheckPoint<B>) -> Self {
109109
self.inner.chain_tip = Some(cp);
110110
self
111111
}
@@ -118,6 +118,7 @@ impl<I> SyncRequestBuilder<I> {
118118
/// [`KeychainTxOutIndex`](../../bdk_chain/indexer/keychain_txout/struct.KeychainTxOutIndex.html).
119119
///
120120
/// ```rust
121+
/// # use bdk_chain::bitcoin::BlockHash;
121122
/// # use bdk_chain::spk_client::SyncRequest;
122123
/// # use bdk_chain::indexer::keychain_txout::KeychainTxOutIndex;
123124
/// # use bdk_chain::miniscript::{Descriptor, DescriptorPublicKey};
@@ -135,15 +136,15 @@ impl<I> SyncRequestBuilder<I> {
135136
/// let (newly_revealed_spks, _changeset) = indexer
136137
/// .reveal_to_target("descriptor_a", 21)
137138
/// .expect("keychain must exist");
138-
/// let _request = SyncRequest::builder()
139+
/// let _request: SyncRequest<u32, BlockHash> = SyncRequest::builder()
139140
/// .spks_with_indexes(newly_revealed_spks)
140141
/// .build();
141142
///
142143
/// // Sync all revealed spks in the indexer. This time, spks may be derived from different
143144
/// // keychains. Each spk will be indexed with `(&'static str, u32)` where `&'static str` is
144145
/// // the keychain identifier and `u32` is the derivation index.
145146
/// let all_revealed_spks = indexer.revealed_spks(..);
146-
/// let _request = SyncRequest::builder()
147+
/// let _request: SyncRequest<(&str, u32), BlockHash> = SyncRequest::builder()
147148
/// .spks_with_indexes(all_revealed_spks)
148149
/// .build();
149150
/// # Ok::<_, bdk_chain::keychain_txout::InsertDescriptorError<_>>(())
@@ -175,7 +176,7 @@ impl<I> SyncRequestBuilder<I> {
175176
}
176177

177178
/// Build the [`SyncRequest`].
178-
pub fn build(self) -> SyncRequest<I> {
179+
pub fn build(self) -> SyncRequest<I, B> {
179180
self.inner
180181
}
181182
}
@@ -203,9 +204,9 @@ impl<I> SyncRequestBuilder<I> {
203204
/// .build();
204205
/// ```
205206
#[must_use]
206-
pub struct SyncRequest<I = ()> {
207+
pub struct SyncRequest<I = (), B = BlockHash> {
207208
start_time: u64,
208-
chain_tip: Option<CheckPoint>,
209+
chain_tip: Option<CheckPoint<B>>,
209210
spks: VecDeque<(I, ScriptBuf)>,
210211
spks_consumed: usize,
211212
txids: VecDeque<Txid>,
@@ -215,13 +216,13 @@ pub struct SyncRequest<I = ()> {
215216
inspect: Box<InspectSync<I>>,
216217
}
217218

218-
impl<I> From<SyncRequestBuilder<I>> for SyncRequest<I> {
219-
fn from(builder: SyncRequestBuilder<I>) -> Self {
219+
impl<I, B> From<SyncRequestBuilder<I, B>> for SyncRequest<I, B> {
220+
fn from(builder: SyncRequestBuilder<I, B>) -> Self {
220221
builder.inner
221222
}
222223
}
223224

224-
impl<I> SyncRequest<I> {
225+
impl<I, B> SyncRequest<I, B> {
225226
/// Start building [`SyncRequest`] with a given `start_time`.
226227
///
227228
/// `start_time` specifies the start time of sync. Chain sources can use this value to set
@@ -230,7 +231,7 @@ impl<I> SyncRequest<I> {
230231
///
231232
/// Use [`SyncRequest::builder`] to use the current timestamp as `start_time` (this requires
232233
/// `feature = "std"`).
233-
pub fn builder_at(start_time: u64) -> SyncRequestBuilder<I> {
234+
pub fn builder_at(start_time: u64) -> SyncRequestBuilder<I, B> {
234235
SyncRequestBuilder {
235236
inner: Self {
236237
start_time,
@@ -252,7 +253,7 @@ impl<I> SyncRequest<I> {
252253
/// is not available.
253254
#[cfg(feature = "std")]
254255
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
255-
pub fn builder() -> SyncRequestBuilder<I> {
256+
pub fn builder() -> SyncRequestBuilder<I, B> {
256257
let start_time = std::time::UNIX_EPOCH
257258
.elapsed()
258259
.expect("failed to get current timestamp")
@@ -278,7 +279,7 @@ impl<I> SyncRequest<I> {
278279
}
279280

280281
/// Get the chain tip [`CheckPoint`] of this request (if any).
281-
pub fn chain_tip(&self) -> Option<CheckPoint> {
282+
pub fn chain_tip(&self) -> Option<CheckPoint<B>> {
282283
self.chain_tip.clone()
283284
}
284285

@@ -314,17 +315,17 @@ impl<I> SyncRequest<I> {
314315

315316
/// Iterate over [`ScriptBuf`]s contained in this request.
316317
pub fn iter_spks(&mut self) -> impl ExactSizeIterator<Item = ScriptBuf> + '_ {
317-
SyncIter::<I, ScriptBuf>::new(self)
318+
SyncIter::<I, B, ScriptBuf>::new(self)
318319
}
319320

320321
/// Iterate over [`Txid`]s contained in this request.
321322
pub fn iter_txids(&mut self) -> impl ExactSizeIterator<Item = Txid> + '_ {
322-
SyncIter::<I, Txid>::new(self)
323+
SyncIter::<I, B, Txid>::new(self)
323324
}
324325

325326
/// Iterate over [`OutPoint`]s contained in this request.
326327
pub fn iter_outpoints(&mut self) -> impl ExactSizeIterator<Item = OutPoint> + '_ {
327-
SyncIter::<I, OutPoint>::new(self)
328+
SyncIter::<I, B, OutPoint>::new(self)
328329
}
329330

330331
fn _call_inspect(&mut self, item: SyncItem<I>) {
@@ -338,14 +339,14 @@ impl<I> SyncRequest<I> {
338339
/// See also [`SyncRequest`].
339340
#[must_use]
340341
#[derive(Debug)]
341-
pub struct SyncResponse<A = ConfirmationBlockTime> {
342+
pub struct SyncResponse<B = BlockHash, A = ConfirmationBlockTime> {
342343
/// Relevant transaction data discovered during the scan.
343344
pub tx_update: crate::TxUpdate<A>,
344345
/// Changes to the chain discovered during the scan.
345-
pub chain_update: Option<CheckPoint>,
346+
pub chain_update: Option<CheckPoint<B>>,
346347
}
347348

348-
impl<A> Default for SyncResponse<A> {
349+
impl<B, A> Default for SyncResponse<B, A> {
349350
fn default() -> Self {
350351
Self {
351352
tx_update: Default::default(),
@@ -358,15 +359,15 @@ impl<A> Default for SyncResponse<A> {
358359
///
359360
/// Construct with [`FullScanRequest::builder`].
360361
#[must_use]
361-
pub struct FullScanRequestBuilder<K> {
362-
inner: FullScanRequest<K>,
362+
pub struct FullScanRequestBuilder<K, B = BlockHash> {
363+
inner: FullScanRequest<K, B>,
363364
}
364365

365-
impl<K: Ord> FullScanRequestBuilder<K> {
366+
impl<K: Ord, B> FullScanRequestBuilder<K, B> {
366367
/// Set the initial chain tip for the full scan request.
367368
///
368369
/// This is used to update [`LocalChain`](../../bdk_chain/local_chain/struct.LocalChain.html).
369-
pub fn chain_tip(mut self, tip: CheckPoint) -> Self {
370+
pub fn chain_tip(mut self, tip: CheckPoint<B>) -> Self {
370371
self.inner.chain_tip = Some(tip);
371372
self
372373
}
@@ -393,7 +394,7 @@ impl<K: Ord> FullScanRequestBuilder<K> {
393394
}
394395

395396
/// Build the [`FullScanRequest`].
396-
pub fn build(self) -> FullScanRequest<K> {
397+
pub fn build(self) -> FullScanRequest<K, B> {
397398
self.inner
398399
}
399400
}
@@ -406,20 +407,20 @@ impl<K: Ord> FullScanRequestBuilder<K> {
406407
/// used scripts is not known. The full scan process also updates the chain from the given
407408
/// [`chain_tip`](FullScanRequestBuilder::chain_tip) (if provided).
408409
#[must_use]
409-
pub struct FullScanRequest<K> {
410+
pub struct FullScanRequest<K, B = BlockHash> {
410411
start_time: u64,
411-
chain_tip: Option<CheckPoint>,
412+
chain_tip: Option<CheckPoint<B>>,
412413
spks_by_keychain: BTreeMap<K, Box<dyn Iterator<Item = Indexed<ScriptBuf>> + Send>>,
413414
inspect: Box<InspectFullScan<K>>,
414415
}
415416

416-
impl<K> From<FullScanRequestBuilder<K>> for FullScanRequest<K> {
417-
fn from(builder: FullScanRequestBuilder<K>) -> Self {
417+
impl<K, B> From<FullScanRequestBuilder<K, B>> for FullScanRequest<K, B> {
418+
fn from(builder: FullScanRequestBuilder<K, B>) -> Self {
418419
builder.inner
419420
}
420421
}
421422

422-
impl<K: Ord + Clone> FullScanRequest<K> {
423+
impl<K: Ord + Clone, B> FullScanRequest<K, B> {
423424
/// Start building a [`FullScanRequest`] with a given `start_time`.
424425
///
425426
/// `start_time` specifies the start time of sync. Chain sources can use this value to set
@@ -428,7 +429,7 @@ impl<K: Ord + Clone> FullScanRequest<K> {
428429
///
429430
/// Use [`FullScanRequest::builder`] to use the current timestamp as `start_time` (this
430431
/// requires `feature = "std`).
431-
pub fn builder_at(start_time: u64) -> FullScanRequestBuilder<K> {
432+
pub fn builder_at(start_time: u64) -> FullScanRequestBuilder<K, B> {
432433
FullScanRequestBuilder {
433434
inner: Self {
434435
start_time,
@@ -445,7 +446,7 @@ impl<K: Ord + Clone> FullScanRequest<K> {
445446
/// "std"` is not available.
446447
#[cfg(feature = "std")]
447448
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
448-
pub fn builder() -> FullScanRequestBuilder<K> {
449+
pub fn builder() -> FullScanRequestBuilder<K, B> {
449450
let start_time = std::time::UNIX_EPOCH
450451
.elapsed()
451452
.expect("failed to get current timestamp")
@@ -459,7 +460,7 @@ impl<K: Ord + Clone> FullScanRequest<K> {
459460
}
460461

461462
/// Get the chain tip [`CheckPoint`] of this request (if any).
462-
pub fn chain_tip(&self) -> Option<CheckPoint> {
463+
pub fn chain_tip(&self) -> Option<CheckPoint<B>> {
463464
self.chain_tip.clone()
464465
}
465466

@@ -491,17 +492,17 @@ impl<K: Ord + Clone> FullScanRequest<K> {
491492
/// See also [`FullScanRequest`].
492493
#[must_use]
493494
#[derive(Debug)]
494-
pub struct FullScanResponse<K, A = ConfirmationBlockTime> {
495+
pub struct FullScanResponse<K, B = BlockHash, A = ConfirmationBlockTime> {
495496
/// Relevant transaction data discovered during the scan.
496497
pub tx_update: crate::TxUpdate<A>,
497498
/// Last active indices for the corresponding keychains (`K`). An index is active if it had a
498499
/// transaction associated with the script pubkey at that index.
499500
pub last_active_indices: BTreeMap<K, u32>,
500501
/// Changes to the chain discovered during the scan.
501-
pub chain_update: Option<CheckPoint>,
502+
pub chain_update: Option<CheckPoint<B>>,
502503
}
503504

504-
impl<K, A> Default for FullScanResponse<K, A> {
505+
impl<K, B, A> Default for FullScanResponse<K, B, A> {
505506
fn default() -> Self {
506507
Self {
507508
tx_update: Default::default(),
@@ -527,23 +528,26 @@ impl<K: Ord + Clone> Iterator for KeychainSpkIter<'_, K> {
527528
}
528529
}
529530

530-
struct SyncIter<'r, I, Item> {
531-
request: &'r mut SyncRequest<I>,
531+
struct SyncIter<'r, I, B, Item> {
532+
request: &'r mut SyncRequest<I, B>,
532533
marker: core::marker::PhantomData<Item>,
533534
}
534535

535-
impl<'r, I, Item> SyncIter<'r, I, Item> {
536-
fn new(request: &'r mut SyncRequest<I>) -> Self {
536+
impl<'r, I, B, Item> SyncIter<'r, I, B, Item> {
537+
fn new(request: &'r mut SyncRequest<I, B>) -> Self {
537538
Self {
538539
request,
539540
marker: core::marker::PhantomData,
540541
}
541542
}
542543
}
543544

544-
impl<'r, I, Item> ExactSizeIterator for SyncIter<'r, I, Item> where SyncIter<'r, I, Item>: Iterator {}
545+
impl<'r, I, B, Item> ExactSizeIterator for SyncIter<'r, I, B, Item> where
546+
SyncIter<'r, I, B, Item>: Iterator
547+
{
548+
}
545549

546-
impl<I> Iterator for SyncIter<'_, I, ScriptBuf> {
550+
impl<I, B> Iterator for SyncIter<'_, I, B, ScriptBuf> {
547551
type Item = ScriptBuf;
548552

549553
fn next(&mut self) -> Option<Self::Item> {
@@ -556,7 +560,7 @@ impl<I> Iterator for SyncIter<'_, I, ScriptBuf> {
556560
}
557561
}
558562

559-
impl<I> Iterator for SyncIter<'_, I, Txid> {
563+
impl<I, B> Iterator for SyncIter<'_, I, B, Txid> {
560564
type Item = Txid;
561565

562566
fn next(&mut self) -> Option<Self::Item> {
@@ -569,7 +573,7 @@ impl<I> Iterator for SyncIter<'_, I, Txid> {
569573
}
570574
}
571575

572-
impl<I> Iterator for SyncIter<'_, I, OutPoint> {
576+
impl<I, B> Iterator for SyncIter<'_, I, B, OutPoint> {
573577
type Item = OutPoint;
574578

575579
fn next(&mut self) -> Option<Self::Item> {

0 commit comments

Comments
 (0)