From 315f687683a2e7813681d253938c75bc39579360 Mon Sep 17 00:00:00 2001 From: Wei Chen Date: Thu, 5 Sep 2024 11:42:59 +0800 Subject: [PATCH] refactor(core): `spk_client` types take generics --- crates/core/src/spk_client.rs | 100 ++++++++++++++++++---------------- 1 file changed, 52 insertions(+), 48 deletions(-) diff --git a/crates/core/src/spk_client.rs b/crates/core/src/spk_client.rs index 1aceae5bb..2c84fdf86 100644 --- a/crates/core/src/spk_client.rs +++ b/crates/core/src/spk_client.rs @@ -4,7 +4,7 @@ use crate::{ collections::BTreeMap, CheckPoint, ConfirmationBlockTime, Indexed, }; -use bitcoin::{OutPoint, Script, ScriptBuf, Txid}; +use bitcoin::{BlockHash, OutPoint, Script, ScriptBuf, Txid}; type InspectSync = dyn FnMut(SyncItem, SyncProgress) + Send + 'static; @@ -88,11 +88,11 @@ impl SyncProgress { /// Builds a [`SyncRequest`]. #[must_use] -pub struct SyncRequestBuilder { - inner: SyncRequest, +pub struct SyncRequestBuilder { + inner: SyncRequest, } -impl Default for SyncRequestBuilder { +impl Default for SyncRequestBuilder { fn default() -> Self { Self { inner: Default::default(), @@ -107,11 +107,11 @@ impl SyncRequestBuilder<()> { } } -impl SyncRequestBuilder { +impl SyncRequestBuilder { /// Set the initial chain tip for the sync request. /// /// This is used to update [`LocalChain`](../../bdk_chain/local_chain/struct.LocalChain.html). - pub fn chain_tip(mut self, cp: CheckPoint) -> Self { + pub fn chain_tip(mut self, cp: CheckPoint) -> Self { self.inner.chain_tip = Some(cp); self } @@ -124,6 +124,7 @@ impl SyncRequestBuilder { /// [`KeychainTxOutIndex`](../../bdk_chain/indexer/keychain_txout/struct.KeychainTxOutIndex.html). /// /// ```rust + /// # use bdk_chain::bitcoin::BlockHash; /// # use bdk_chain::spk_client::SyncRequest; /// # use bdk_chain::indexer::keychain_txout::KeychainTxOutIndex; /// # use bdk_chain::miniscript::{Descriptor, DescriptorPublicKey}; @@ -141,7 +142,7 @@ impl SyncRequestBuilder { /// let (newly_revealed_spks, _changeset) = indexer /// .reveal_to_target("descriptor_a", 21) /// .expect("keychain must exist"); - /// let _request = SyncRequest::builder() + /// let _request: SyncRequest = SyncRequest::builder() /// .spks_with_indexes(newly_revealed_spks) /// .build(); /// @@ -149,7 +150,7 @@ impl SyncRequestBuilder { /// // keychains. Each spk will be indexed with `(&'static str, u32)` where `&'static str` is /// // the keychain identifier and `u32` is the derivation index. /// let all_revealed_spks = indexer.revealed_spks(..); - /// let _request = SyncRequest::builder() + /// let _request: SyncRequest<(&str, u32), BlockHash> = SyncRequest::builder() /// .spks_with_indexes(all_revealed_spks) /// .build(); /// # Ok::<_, bdk_chain::keychain_txout::InsertDescriptorError<_>>(()) @@ -181,7 +182,7 @@ impl SyncRequestBuilder { } /// Build the [`SyncRequest`]. - pub fn build(self) -> SyncRequest { + pub fn build(self) -> SyncRequest { self.inner } } @@ -209,8 +210,8 @@ impl SyncRequestBuilder { /// .build(); /// ``` #[must_use] -pub struct SyncRequest { - chain_tip: Option, +pub struct SyncRequest { + chain_tip: Option>, spks: VecDeque<(I, ScriptBuf)>, spks_consumed: usize, txids: VecDeque, @@ -220,7 +221,7 @@ pub struct SyncRequest { inspect: Box>, } -impl Default for SyncRequest { +impl Default for SyncRequest { fn default() -> Self { Self { chain_tip: None, @@ -235,15 +236,15 @@ impl Default for SyncRequest { } } -impl From> for SyncRequest { - fn from(builder: SyncRequestBuilder) -> Self { +impl From> for SyncRequest { + fn from(builder: SyncRequestBuilder) -> Self { builder.inner } } -impl SyncRequest { +impl SyncRequest { /// Start building a [`SyncRequest`]. - pub fn builder() -> SyncRequestBuilder { + pub fn builder() -> SyncRequestBuilder { SyncRequestBuilder { inner: Default::default(), } @@ -262,7 +263,7 @@ impl SyncRequest { } /// Get the chain tip [`CheckPoint`] of this request (if any). - pub fn chain_tip(&self) -> Option { + pub fn chain_tip(&self) -> Option> { self.chain_tip.clone() } @@ -298,17 +299,17 @@ impl SyncRequest { /// Iterate over [`ScriptBuf`]s contained in this request. pub fn iter_spks(&mut self) -> impl ExactSizeIterator + '_ { - SyncIter::::new(self) + SyncIter::::new(self) } /// Iterate over [`Txid`]s contained in this request. pub fn iter_txids(&mut self) -> impl ExactSizeIterator + '_ { - SyncIter::::new(self) + SyncIter::::new(self) } /// Iterate over [`OutPoint`]s contained in this request. pub fn iter_outpoints(&mut self) -> impl ExactSizeIterator + '_ { - SyncIter::::new(self) + SyncIter::::new(self) } fn _call_inspect(&mut self, item: SyncItem) { @@ -322,14 +323,14 @@ impl SyncRequest { /// See also [`SyncRequest`]. #[must_use] #[derive(Debug)] -pub struct SyncResult { +pub struct SyncResult { /// Relevant transaction data discovered during the scan. pub tx_update: crate::TxUpdate, /// Changes to the chain discovered during the scan. - pub chain_update: Option, + pub chain_update: Option>, } -impl Default for SyncResult { +impl Default for SyncResult { fn default() -> Self { Self { tx_update: Default::default(), @@ -340,11 +341,11 @@ impl Default for SyncResult { /// Builds a [`FullScanRequest`]. #[must_use] -pub struct FullScanRequestBuilder { - inner: FullScanRequest, +pub struct FullScanRequestBuilder { + inner: FullScanRequest, } -impl Default for FullScanRequestBuilder { +impl Default for FullScanRequestBuilder { fn default() -> Self { Self { inner: Default::default(), @@ -352,11 +353,11 @@ impl Default for FullScanRequestBuilder { } } -impl FullScanRequestBuilder { +impl FullScanRequestBuilder { /// Set the initial chain tip for the full scan request. /// /// This is used to update [`LocalChain`](../../bdk_chain/local_chain/struct.LocalChain.html). - pub fn chain_tip(mut self, tip: CheckPoint) -> Self { + pub fn chain_tip(mut self, tip: CheckPoint) -> Self { self.inner.chain_tip = Some(tip); self } @@ -383,7 +384,7 @@ impl FullScanRequestBuilder { } /// Build the [`FullScanRequest`]. - pub fn build(self) -> FullScanRequest { + pub fn build(self) -> FullScanRequest { self.inner } } @@ -396,19 +397,19 @@ impl FullScanRequestBuilder { /// used scripts is not known. The full scan process also updates the chain from the given /// [`chain_tip`](FullScanRequestBuilder::chain_tip) (if provided). #[must_use] -pub struct FullScanRequest { - chain_tip: Option, +pub struct FullScanRequest { + chain_tip: Option>, spks_by_keychain: BTreeMap> + Send>>, inspect: Box>, } -impl From> for FullScanRequest { - fn from(builder: FullScanRequestBuilder) -> Self { +impl From> for FullScanRequest { + fn from(builder: FullScanRequestBuilder) -> Self { builder.inner } } -impl Default for FullScanRequest { +impl Default for FullScanRequest { fn default() -> Self { Self { chain_tip: None, @@ -418,16 +419,16 @@ impl Default for FullScanRequest { } } -impl FullScanRequest { +impl FullScanRequest { /// Start building a [`FullScanRequest`]. - pub fn builder() -> FullScanRequestBuilder { + pub fn builder() -> FullScanRequestBuilder { FullScanRequestBuilder { inner: Self::default(), } } /// Get the chain tip [`CheckPoint`] of this request (if any). - pub fn chain_tip(&self) -> Option { + pub fn chain_tip(&self) -> Option> { self.chain_tip.clone() } @@ -459,17 +460,17 @@ impl FullScanRequest { /// See also [`FullScanRequest`]. #[must_use] #[derive(Debug)] -pub struct FullScanResult { +pub struct FullScanResult { /// Relevant transaction data discovered during the scan. pub tx_update: crate::TxUpdate, /// Last active indices for the corresponding keychains (`K`). An index is active if it had a /// transaction associated with the script pubkey at that index. pub last_active_indices: BTreeMap, /// Changes to the chain discovered during the scan. - pub chain_update: Option, + pub chain_update: Option>, } -impl Default for FullScanResult { +impl Default for FullScanResult { fn default() -> Self { Self { tx_update: Default::default(), @@ -495,13 +496,13 @@ impl<'r, K: Ord + Clone> Iterator for KeychainSpkIter<'r, K> { } } -struct SyncIter<'r, I, Item> { - request: &'r mut SyncRequest, +struct SyncIter<'r, I, B, Item> { + request: &'r mut SyncRequest, marker: core::marker::PhantomData, } -impl<'r, I, Item> SyncIter<'r, I, Item> { - fn new(request: &'r mut SyncRequest) -> Self { +impl<'r, I, B, Item> SyncIter<'r, I, B, Item> { + fn new(request: &'r mut SyncRequest) -> Self { Self { request, marker: core::marker::PhantomData, @@ -509,9 +510,12 @@ impl<'r, I, Item> SyncIter<'r, I, Item> { } } -impl<'r, I, Item> ExactSizeIterator for SyncIter<'r, I, Item> where SyncIter<'r, I, Item>: Iterator {} +impl<'r, I, B, Item> ExactSizeIterator for SyncIter<'r, I, B, Item> where + SyncIter<'r, I, B, Item>: Iterator +{ +} -impl<'r, I> Iterator for SyncIter<'r, I, ScriptBuf> { +impl<'r, I, B> Iterator for SyncIter<'r, I, B, ScriptBuf> { type Item = ScriptBuf; fn next(&mut self) -> Option { @@ -524,7 +528,7 @@ impl<'r, I> Iterator for SyncIter<'r, I, ScriptBuf> { } } -impl<'r, I> Iterator for SyncIter<'r, I, Txid> { +impl<'r, I, B> Iterator for SyncIter<'r, I, B, Txid> { type Item = Txid; fn next(&mut self) -> Option { @@ -537,7 +541,7 @@ impl<'r, I> Iterator for SyncIter<'r, I, Txid> { } } -impl<'r, I> Iterator for SyncIter<'r, I, OutPoint> { +impl<'r, I, B> Iterator for SyncIter<'r, I, B, OutPoint> { type Item = OutPoint; fn next(&mut self) -> Option {