Skip to content

Commit

Permalink
refactor(core): spk_client types take generics
Browse files Browse the repository at this point in the history
  • Loading branch information
LagginTimes committed Sep 5, 2024
1 parent 5d77eaf commit 6300d7c
Showing 1 changed file with 52 additions and 48 deletions.
100 changes: 52 additions & 48 deletions crates/core/src/spk_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<I> = dyn FnMut(SyncItem<I>, SyncProgress) + Send + 'static;

Expand Down Expand Up @@ -88,11 +88,11 @@ impl SyncProgress {

/// Builds a [`SyncRequest`].
#[must_use]
pub struct SyncRequestBuilder<I = ()> {
inner: SyncRequest<I>,
pub struct SyncRequestBuilder<I = (), B = BlockHash> {
inner: SyncRequest<I, B>,
}

impl<I> Default for SyncRequestBuilder<I> {
impl<I, B> Default for SyncRequestBuilder<I, B> {
fn default() -> Self {
Self {
inner: Default::default(),
Expand All @@ -107,11 +107,11 @@ impl SyncRequestBuilder<()> {
}
}

impl<I> SyncRequestBuilder<I> {
impl<I, B> SyncRequestBuilder<I, B> {
/// 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<B>) -> Self {
self.inner.chain_tip = Some(cp);
self
}
Expand All @@ -124,6 +124,7 @@ impl<I> SyncRequestBuilder<I> {
/// [`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};
Expand All @@ -141,15 +142,15 @@ impl<I> SyncRequestBuilder<I> {
/// let (newly_revealed_spks, _changeset) = indexer
/// .reveal_to_target("descriptor_a", 21)
/// .expect("keychain must exist");
/// let _request = SyncRequest::builder()
/// let _request: SyncRequest<u32, BlockHash> = SyncRequest::builder()
/// .spks_with_indexes(newly_revealed_spks)
/// .build();
///
/// // Sync all revealed spks in the indexer. This time, spks may be derived from different
/// // 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<_>>(())
Expand Down Expand Up @@ -181,7 +182,7 @@ impl<I> SyncRequestBuilder<I> {
}

/// Build the [`SyncRequest`].
pub fn build(self) -> SyncRequest<I> {
pub fn build(self) -> SyncRequest<I, B> {
self.inner
}
}
Expand Down Expand Up @@ -209,8 +210,8 @@ impl<I> SyncRequestBuilder<I> {
/// .build();
/// ```
#[must_use]
pub struct SyncRequest<I = ()> {
chain_tip: Option<CheckPoint>,
pub struct SyncRequest<I = (), B = BlockHash> {
chain_tip: Option<CheckPoint<B>>,
spks: VecDeque<(I, ScriptBuf)>,
spks_consumed: usize,
txids: VecDeque<Txid>,
Expand All @@ -220,7 +221,7 @@ pub struct SyncRequest<I = ()> {
inspect: Box<InspectSync<I>>,
}

impl<I> Default for SyncRequest<I> {
impl<I, B> Default for SyncRequest<I, B> {
fn default() -> Self {
Self {
chain_tip: None,
Expand All @@ -235,15 +236,15 @@ impl<I> Default for SyncRequest<I> {
}
}

impl<I> From<SyncRequestBuilder<I>> for SyncRequest<I> {
fn from(builder: SyncRequestBuilder<I>) -> Self {
impl<I, B> From<SyncRequestBuilder<I, B>> for SyncRequest<I, B> {
fn from(builder: SyncRequestBuilder<I, B>) -> Self {
builder.inner
}
}

impl<I> SyncRequest<I> {
impl<I, B> SyncRequest<I, B> {
/// Start building a [`SyncRequest`].
pub fn builder() -> SyncRequestBuilder<I> {
pub fn builder() -> SyncRequestBuilder<I, B> {
SyncRequestBuilder {
inner: Default::default(),
}
Expand All @@ -262,7 +263,7 @@ impl<I> SyncRequest<I> {
}

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

Expand Down Expand Up @@ -298,17 +299,17 @@ impl<I> SyncRequest<I> {

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

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

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

fn _call_inspect(&mut self, item: SyncItem<I>) {
Expand All @@ -322,14 +323,14 @@ impl<I> SyncRequest<I> {
/// See also [`SyncRequest`].
#[must_use]
#[derive(Debug)]
pub struct SyncResult<A = ConfirmationBlockTime> {
pub struct SyncResult<B = BlockHash, A = ConfirmationBlockTime> {
/// Relevant transaction data discovered during the scan.
pub tx_update: crate::TxUpdate<A>,
/// Changes to the chain discovered during the scan.
pub chain_update: Option<CheckPoint>,
pub chain_update: Option<CheckPoint<B>>,
}

impl<A> Default for SyncResult<A> {
impl<B, A> Default for SyncResult<B, A> {
fn default() -> Self {
Self {
tx_update: Default::default(),
Expand All @@ -340,23 +341,23 @@ impl<A> Default for SyncResult<A> {

/// Builds a [`FullScanRequest`].
#[must_use]
pub struct FullScanRequestBuilder<K> {
inner: FullScanRequest<K>,
pub struct FullScanRequestBuilder<K, B = BlockHash> {
inner: FullScanRequest<K, B>,
}

impl<K> Default for FullScanRequestBuilder<K> {
impl<K, B> Default for FullScanRequestBuilder<K, B> {
fn default() -> Self {
Self {
inner: Default::default(),
}
}
}

impl<K: Ord> FullScanRequestBuilder<K> {
impl<K: Ord, B> FullScanRequestBuilder<K, B> {
/// 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<B>) -> Self {
self.inner.chain_tip = Some(tip);
self
}
Expand All @@ -383,7 +384,7 @@ impl<K: Ord> FullScanRequestBuilder<K> {
}

/// Build the [`FullScanRequest`].
pub fn build(self) -> FullScanRequest<K> {
pub fn build(self) -> FullScanRequest<K, B> {
self.inner
}
}
Expand All @@ -396,19 +397,19 @@ impl<K: Ord> FullScanRequestBuilder<K> {
/// 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<K> {
chain_tip: Option<CheckPoint>,
pub struct FullScanRequest<K, B = BlockHash> {
chain_tip: Option<CheckPoint<B>>,
spks_by_keychain: BTreeMap<K, Box<dyn Iterator<Item = Indexed<ScriptBuf>> + Send>>,
inspect: Box<InspectFullScan<K>>,
}

impl<K> From<FullScanRequestBuilder<K>> for FullScanRequest<K> {
fn from(builder: FullScanRequestBuilder<K>) -> Self {
impl<K, B> From<FullScanRequestBuilder<K, B>> for FullScanRequest<K, B> {
fn from(builder: FullScanRequestBuilder<K, B>) -> Self {
builder.inner
}
}

impl<K> Default for FullScanRequest<K> {
impl<K, B> Default for FullScanRequest<K, B> {
fn default() -> Self {
Self {
chain_tip: None,
Expand All @@ -418,16 +419,16 @@ impl<K> Default for FullScanRequest<K> {
}
}

impl<K: Ord + Clone> FullScanRequest<K> {
impl<K: Ord + Clone, B> FullScanRequest<K, B> {
/// Start building a [`FullScanRequest`].
pub fn builder() -> FullScanRequestBuilder<K> {
pub fn builder() -> FullScanRequestBuilder<K, B> {
FullScanRequestBuilder {
inner: Self::default(),
}
}

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

Expand Down Expand Up @@ -459,17 +460,17 @@ impl<K: Ord + Clone> FullScanRequest<K> {
/// See also [`FullScanRequest`].
#[must_use]
#[derive(Debug)]
pub struct FullScanResult<K, A = ConfirmationBlockTime> {
pub struct FullScanResult<K, B = BlockHash, A = ConfirmationBlockTime> {
/// Relevant transaction data discovered during the scan.
pub tx_update: crate::TxUpdate<A>,
/// 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<K, u32>,
/// Changes to the chain discovered during the scan.
pub chain_update: Option<CheckPoint>,
pub chain_update: Option<CheckPoint<B>>,
}

impl<K, A> Default for FullScanResult<K, A> {
impl<K, B, A> Default for FullScanResult<K, B, A> {
fn default() -> Self {
Self {
tx_update: Default::default(),
Expand All @@ -495,23 +496,26 @@ impl<'r, K: Ord + Clone> Iterator for KeychainSpkIter<'r, K> {
}
}

struct SyncIter<'r, I, Item> {
request: &'r mut SyncRequest<I>,
struct SyncIter<'r, I, B, Item> {
request: &'r mut SyncRequest<I, B>,
marker: core::marker::PhantomData<Item>,
}

impl<'r, I, Item> SyncIter<'r, I, Item> {
fn new(request: &'r mut SyncRequest<I>) -> Self {
impl<'r, I, B, Item> SyncIter<'r, I, B, Item> {
fn new(request: &'r mut SyncRequest<I, B>) -> Self {
Self {
request,
marker: core::marker::PhantomData,
}
}
}

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<Self::Item> {
Expand All @@ -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<Self::Item> {
Expand All @@ -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<Self::Item> {
Expand Down

0 comments on commit 6300d7c

Please sign in to comment.