Skip to content

Commit 315f687

Browse files
committed
refactor(core): spk_client types take generics
1 parent c993106 commit 315f687

File tree

1 file changed

+52
-48
lines changed

1 file changed

+52
-48
lines changed

crates/core/src/spk_client.rs

Lines changed: 52 additions & 48 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

@@ -88,11 +88,11 @@ impl SyncProgress {
8888

8989
/// Builds a [`SyncRequest`].
9090
#[must_use]
91-
pub struct SyncRequestBuilder<I = ()> {
92-
inner: SyncRequest<I>,
91+
pub struct SyncRequestBuilder<I = (), B = BlockHash> {
92+
inner: SyncRequest<I, B>,
9393
}
9494

95-
impl<I> Default for SyncRequestBuilder<I> {
95+
impl<I, B> Default for SyncRequestBuilder<I, B> {
9696
fn default() -> Self {
9797
Self {
9898
inner: Default::default(),
@@ -107,11 +107,11 @@ impl SyncRequestBuilder<()> {
107107
}
108108
}
109109

110-
impl<I> SyncRequestBuilder<I> {
110+
impl<I, B> SyncRequestBuilder<I, B> {
111111
/// Set the initial chain tip for the sync request.
112112
///
113113
/// This is used to update [`LocalChain`](../../bdk_chain/local_chain/struct.LocalChain.html).
114-
pub fn chain_tip(mut self, cp: CheckPoint) -> Self {
114+
pub fn chain_tip(mut self, cp: CheckPoint<B>) -> Self {
115115
self.inner.chain_tip = Some(cp);
116116
self
117117
}
@@ -124,6 +124,7 @@ impl<I> SyncRequestBuilder<I> {
124124
/// [`KeychainTxOutIndex`](../../bdk_chain/indexer/keychain_txout/struct.KeychainTxOutIndex.html).
125125
///
126126
/// ```rust
127+
/// # use bdk_chain::bitcoin::BlockHash;
127128
/// # use bdk_chain::spk_client::SyncRequest;
128129
/// # use bdk_chain::indexer::keychain_txout::KeychainTxOutIndex;
129130
/// # use bdk_chain::miniscript::{Descriptor, DescriptorPublicKey};
@@ -141,15 +142,15 @@ impl<I> SyncRequestBuilder<I> {
141142
/// let (newly_revealed_spks, _changeset) = indexer
142143
/// .reveal_to_target("descriptor_a", 21)
143144
/// .expect("keychain must exist");
144-
/// let _request = SyncRequest::builder()
145+
/// let _request: SyncRequest<u32, BlockHash> = SyncRequest::builder()
145146
/// .spks_with_indexes(newly_revealed_spks)
146147
/// .build();
147148
///
148149
/// // Sync all revealed spks in the indexer. This time, spks may be derived from different
149150
/// // keychains. Each spk will be indexed with `(&'static str, u32)` where `&'static str` is
150151
/// // the keychain identifier and `u32` is the derivation index.
151152
/// let all_revealed_spks = indexer.revealed_spks(..);
152-
/// let _request = SyncRequest::builder()
153+
/// let _request: SyncRequest<(&str, u32), BlockHash> = SyncRequest::builder()
153154
/// .spks_with_indexes(all_revealed_spks)
154155
/// .build();
155156
/// # Ok::<_, bdk_chain::keychain_txout::InsertDescriptorError<_>>(())
@@ -181,7 +182,7 @@ impl<I> SyncRequestBuilder<I> {
181182
}
182183

183184
/// Build the [`SyncRequest`].
184-
pub fn build(self) -> SyncRequest<I> {
185+
pub fn build(self) -> SyncRequest<I, B> {
185186
self.inner
186187
}
187188
}
@@ -209,8 +210,8 @@ impl<I> SyncRequestBuilder<I> {
209210
/// .build();
210211
/// ```
211212
#[must_use]
212-
pub struct SyncRequest<I = ()> {
213-
chain_tip: Option<CheckPoint>,
213+
pub struct SyncRequest<I = (), B = BlockHash> {
214+
chain_tip: Option<CheckPoint<B>>,
214215
spks: VecDeque<(I, ScriptBuf)>,
215216
spks_consumed: usize,
216217
txids: VecDeque<Txid>,
@@ -220,7 +221,7 @@ pub struct SyncRequest<I = ()> {
220221
inspect: Box<InspectSync<I>>,
221222
}
222223

223-
impl<I> Default for SyncRequest<I> {
224+
impl<I, B> Default for SyncRequest<I, B> {
224225
fn default() -> Self {
225226
Self {
226227
chain_tip: None,
@@ -235,15 +236,15 @@ impl<I> Default for SyncRequest<I> {
235236
}
236237
}
237238

238-
impl<I> From<SyncRequestBuilder<I>> for SyncRequest<I> {
239-
fn from(builder: SyncRequestBuilder<I>) -> Self {
239+
impl<I, B> From<SyncRequestBuilder<I, B>> for SyncRequest<I, B> {
240+
fn from(builder: SyncRequestBuilder<I, B>) -> Self {
240241
builder.inner
241242
}
242243
}
243244

244-
impl<I> SyncRequest<I> {
245+
impl<I, B> SyncRequest<I, B> {
245246
/// Start building a [`SyncRequest`].
246-
pub fn builder() -> SyncRequestBuilder<I> {
247+
pub fn builder() -> SyncRequestBuilder<I, B> {
247248
SyncRequestBuilder {
248249
inner: Default::default(),
249250
}
@@ -262,7 +263,7 @@ impl<I> SyncRequest<I> {
262263
}
263264

264265
/// Get the chain tip [`CheckPoint`] of this request (if any).
265-
pub fn chain_tip(&self) -> Option<CheckPoint> {
266+
pub fn chain_tip(&self) -> Option<CheckPoint<B>> {
266267
self.chain_tip.clone()
267268
}
268269

@@ -298,17 +299,17 @@ impl<I> SyncRequest<I> {
298299

299300
/// Iterate over [`ScriptBuf`]s contained in this request.
300301
pub fn iter_spks(&mut self) -> impl ExactSizeIterator<Item = ScriptBuf> + '_ {
301-
SyncIter::<I, ScriptBuf>::new(self)
302+
SyncIter::<I, B, ScriptBuf>::new(self)
302303
}
303304

304305
/// Iterate over [`Txid`]s contained in this request.
305306
pub fn iter_txids(&mut self) -> impl ExactSizeIterator<Item = Txid> + '_ {
306-
SyncIter::<I, Txid>::new(self)
307+
SyncIter::<I, B, Txid>::new(self)
307308
}
308309

309310
/// Iterate over [`OutPoint`]s contained in this request.
310311
pub fn iter_outpoints(&mut self) -> impl ExactSizeIterator<Item = OutPoint> + '_ {
311-
SyncIter::<I, OutPoint>::new(self)
312+
SyncIter::<I, B, OutPoint>::new(self)
312313
}
313314

314315
fn _call_inspect(&mut self, item: SyncItem<I>) {
@@ -322,14 +323,14 @@ impl<I> SyncRequest<I> {
322323
/// See also [`SyncRequest`].
323324
#[must_use]
324325
#[derive(Debug)]
325-
pub struct SyncResult<A = ConfirmationBlockTime> {
326+
pub struct SyncResult<B = BlockHash, A = ConfirmationBlockTime> {
326327
/// Relevant transaction data discovered during the scan.
327328
pub tx_update: crate::TxUpdate<A>,
328329
/// Changes to the chain discovered during the scan.
329-
pub chain_update: Option<CheckPoint>,
330+
pub chain_update: Option<CheckPoint<B>>,
330331
}
331332

332-
impl<A> Default for SyncResult<A> {
333+
impl<B, A> Default for SyncResult<B, A> {
333334
fn default() -> Self {
334335
Self {
335336
tx_update: Default::default(),
@@ -340,23 +341,23 @@ impl<A> Default for SyncResult<A> {
340341

341342
/// Builds a [`FullScanRequest`].
342343
#[must_use]
343-
pub struct FullScanRequestBuilder<K> {
344-
inner: FullScanRequest<K>,
344+
pub struct FullScanRequestBuilder<K, B = BlockHash> {
345+
inner: FullScanRequest<K, B>,
345346
}
346347

347-
impl<K> Default for FullScanRequestBuilder<K> {
348+
impl<K, B> Default for FullScanRequestBuilder<K, B> {
348349
fn default() -> Self {
349350
Self {
350351
inner: Default::default(),
351352
}
352353
}
353354
}
354355

355-
impl<K: Ord> FullScanRequestBuilder<K> {
356+
impl<K: Ord, B> FullScanRequestBuilder<K, B> {
356357
/// Set the initial chain tip for the full scan request.
357358
///
358359
/// This is used to update [`LocalChain`](../../bdk_chain/local_chain/struct.LocalChain.html).
359-
pub fn chain_tip(mut self, tip: CheckPoint) -> Self {
360+
pub fn chain_tip(mut self, tip: CheckPoint<B>) -> Self {
360361
self.inner.chain_tip = Some(tip);
361362
self
362363
}
@@ -383,7 +384,7 @@ impl<K: Ord> FullScanRequestBuilder<K> {
383384
}
384385

385386
/// Build the [`FullScanRequest`].
386-
pub fn build(self) -> FullScanRequest<K> {
387+
pub fn build(self) -> FullScanRequest<K, B> {
387388
self.inner
388389
}
389390
}
@@ -396,19 +397,19 @@ impl<K: Ord> FullScanRequestBuilder<K> {
396397
/// used scripts is not known. The full scan process also updates the chain from the given
397398
/// [`chain_tip`](FullScanRequestBuilder::chain_tip) (if provided).
398399
#[must_use]
399-
pub struct FullScanRequest<K> {
400-
chain_tip: Option<CheckPoint>,
400+
pub struct FullScanRequest<K, B = BlockHash> {
401+
chain_tip: Option<CheckPoint<B>>,
401402
spks_by_keychain: BTreeMap<K, Box<dyn Iterator<Item = Indexed<ScriptBuf>> + Send>>,
402403
inspect: Box<InspectFullScan<K>>,
403404
}
404405

405-
impl<K> From<FullScanRequestBuilder<K>> for FullScanRequest<K> {
406-
fn from(builder: FullScanRequestBuilder<K>) -> Self {
406+
impl<K, B> From<FullScanRequestBuilder<K, B>> for FullScanRequest<K, B> {
407+
fn from(builder: FullScanRequestBuilder<K, B>) -> Self {
407408
builder.inner
408409
}
409410
}
410411

411-
impl<K> Default for FullScanRequest<K> {
412+
impl<K, B> Default for FullScanRequest<K, B> {
412413
fn default() -> Self {
413414
Self {
414415
chain_tip: None,
@@ -418,16 +419,16 @@ impl<K> Default for FullScanRequest<K> {
418419
}
419420
}
420421

421-
impl<K: Ord + Clone> FullScanRequest<K> {
422+
impl<K: Ord + Clone, B> FullScanRequest<K, B> {
422423
/// Start building a [`FullScanRequest`].
423-
pub fn builder() -> FullScanRequestBuilder<K> {
424+
pub fn builder() -> FullScanRequestBuilder<K, B> {
424425
FullScanRequestBuilder {
425426
inner: Self::default(),
426427
}
427428
}
428429

429430
/// Get the chain tip [`CheckPoint`] of this request (if any).
430-
pub fn chain_tip(&self) -> Option<CheckPoint> {
431+
pub fn chain_tip(&self) -> Option<CheckPoint<B>> {
431432
self.chain_tip.clone()
432433
}
433434

@@ -459,17 +460,17 @@ impl<K: Ord + Clone> FullScanRequest<K> {
459460
/// See also [`FullScanRequest`].
460461
#[must_use]
461462
#[derive(Debug)]
462-
pub struct FullScanResult<K, A = ConfirmationBlockTime> {
463+
pub struct FullScanResult<K, B = BlockHash, A = ConfirmationBlockTime> {
463464
/// Relevant transaction data discovered during the scan.
464465
pub tx_update: crate::TxUpdate<A>,
465466
/// Last active indices for the corresponding keychains (`K`). An index is active if it had a
466467
/// transaction associated with the script pubkey at that index.
467468
pub last_active_indices: BTreeMap<K, u32>,
468469
/// Changes to the chain discovered during the scan.
469-
pub chain_update: Option<CheckPoint>,
470+
pub chain_update: Option<CheckPoint<B>>,
470471
}
471472

472-
impl<K, A> Default for FullScanResult<K, A> {
473+
impl<K, B, A> Default for FullScanResult<K, B, A> {
473474
fn default() -> Self {
474475
Self {
475476
tx_update: Default::default(),
@@ -495,23 +496,26 @@ impl<'r, K: Ord + Clone> Iterator for KeychainSpkIter<'r, K> {
495496
}
496497
}
497498

498-
struct SyncIter<'r, I, Item> {
499-
request: &'r mut SyncRequest<I>,
499+
struct SyncIter<'r, I, B, Item> {
500+
request: &'r mut SyncRequest<I, B>,
500501
marker: core::marker::PhantomData<Item>,
501502
}
502503

503-
impl<'r, I, Item> SyncIter<'r, I, Item> {
504-
fn new(request: &'r mut SyncRequest<I>) -> Self {
504+
impl<'r, I, B, Item> SyncIter<'r, I, B, Item> {
505+
fn new(request: &'r mut SyncRequest<I, B>) -> Self {
505506
Self {
506507
request,
507508
marker: core::marker::PhantomData,
508509
}
509510
}
510511
}
511512

512-
impl<'r, I, Item> ExactSizeIterator for SyncIter<'r, I, Item> where SyncIter<'r, I, Item>: Iterator {}
513+
impl<'r, I, B, Item> ExactSizeIterator for SyncIter<'r, I, B, Item> where
514+
SyncIter<'r, I, B, Item>: Iterator
515+
{
516+
}
513517

514-
impl<'r, I> Iterator for SyncIter<'r, I, ScriptBuf> {
518+
impl<'r, I, B> Iterator for SyncIter<'r, I, B, ScriptBuf> {
515519
type Item = ScriptBuf;
516520

517521
fn next(&mut self) -> Option<Self::Item> {
@@ -524,7 +528,7 @@ impl<'r, I> Iterator for SyncIter<'r, I, ScriptBuf> {
524528
}
525529
}
526530

527-
impl<'r, I> Iterator for SyncIter<'r, I, Txid> {
531+
impl<'r, I, B> Iterator for SyncIter<'r, I, B, Txid> {
528532
type Item = Txid;
529533

530534
fn next(&mut self) -> Option<Self::Item> {
@@ -537,7 +541,7 @@ impl<'r, I> Iterator for SyncIter<'r, I, Txid> {
537541
}
538542
}
539543

540-
impl<'r, I> Iterator for SyncIter<'r, I, OutPoint> {
544+
impl<'r, I, B> Iterator for SyncIter<'r, I, B, OutPoint> {
541545
type Item = OutPoint;
542546

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

0 commit comments

Comments
 (0)