Skip to content

Commit ab0315d

Browse files
committed
feat!: move spk_clients to bdk_core
Also introduced extension trait for builder methods that take in a `KeychainTxOutIndex`. `Indexed<T>` and `KeychainIndexed<T>` are also moved to `bdk_core`.
1 parent bdea871 commit ab0315d

File tree

8 files changed

+68
-52
lines changed

8 files changed

+68
-52
lines changed

crates/chain/src/indexer/keychain_txout.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use crate::{
55
collections::*,
66
miniscript::{Descriptor, DescriptorPublicKey},
7+
spk_client::{FullScanRequestBuilder, SyncRequestBuilder},
78
spk_iter::BIP32_MAX_INDEX,
89
spk_txout::SpkTxOutIndex,
910
DescriptorExt, DescriptorId, Indexed, Indexer, KeychainIndexed, SpkIterator,
@@ -875,3 +876,43 @@ impl Merge for ChangeSet {
875876
self.last_revealed.is_empty()
876877
}
877878
}
879+
880+
/// Trait to extend [`SyncRequestBuilder`].
881+
pub trait SyncRequestBuilderExt<K> {
882+
/// Add [`Script`](bitcoin::Script)s that are revealed by the `indexer` of the given `spk_range`
883+
/// that will be synced against.
884+
fn revealed_spks_from_indexer<R>(self, indexer: &KeychainTxOutIndex<K>, spk_range: R) -> Self
885+
where
886+
R: core::ops::RangeBounds<K>;
887+
888+
/// Add [`Script`](bitcoin::Script)s that are revealed by the `indexer` but currently unused.
889+
fn unused_spks_from_indexer(self, indexer: &KeychainTxOutIndex<K>) -> Self;
890+
}
891+
892+
impl<K: Clone + Ord + core::fmt::Debug> SyncRequestBuilderExt<K> for SyncRequestBuilder<(K, u32)> {
893+
fn revealed_spks_from_indexer<R>(self, indexer: &KeychainTxOutIndex<K>, spk_range: R) -> Self
894+
where
895+
R: core::ops::RangeBounds<K>,
896+
{
897+
self.spks_with_indexes(indexer.revealed_spks(spk_range))
898+
}
899+
900+
fn unused_spks_from_indexer(self, indexer: &KeychainTxOutIndex<K>) -> Self {
901+
self.spks_with_indexes(indexer.unused_spks())
902+
}
903+
}
904+
905+
/// Trait to extend [`FullScanRequestBuilder`].
906+
pub trait FullScanRequestBuilderExt<K> {
907+
/// Add spk iterators for each keychain tracked in `indexer`.
908+
fn spks_from_indexer(self, indexer: &KeychainTxOutIndex<K>) -> Self;
909+
}
910+
911+
impl<K: Clone + Ord + core::fmt::Debug> FullScanRequestBuilderExt<K> for FullScanRequestBuilder<K> {
912+
fn spks_from_indexer(mut self, indexer: &KeychainTxOutIndex<K>) -> Self {
913+
for (keychain, spks) in indexer.all_unbounded_spk_iters() {
914+
self = self.spks_for_keychain(keychain, spks);
915+
}
916+
self
917+
}
918+
}

crates/chain/src/lib.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ pub use indexer::keychain_txout;
6161
pub use spk_iter::*;
6262
#[cfg(feature = "rusqlite")]
6363
pub mod rusqlite_impl;
64-
pub mod spk_client;
6564

6665
pub extern crate bdk_core;
6766
pub use bdk_core::*;
@@ -81,11 +80,6 @@ extern crate std;
8180
/// How many confirmations are needed f or a coinbase output to be spent.
8281
pub const COINBASE_MATURITY: u32 = 100;
8382

84-
/// A tuple of keychain index and `T` representing the indexed value.
85-
pub type Indexed<T> = (u32, T);
86-
/// A tuple of keychain `K`, derivation index (`u32`) and a `T` associated with them.
87-
pub type KeychainIndexed<K, T> = ((K, u32), T);
88-
8983
/// A wrapper that we use to impl remote traits for types in our crate or dependency crates.
9084
pub struct Impl<T>(pub T);
9185

crates/core/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ hashbrown = { version = "0.9.1", optional = true }
1919
default = ["std"]
2020
std = ["bitcoin/std"]
2121
serde = ["dep:serde", "bitcoin/serde", "hashbrown?/serde"]
22+
23+
[dev-dependencies]
24+
bdk_chain = { version = "0.17.0", path = "../chain" }

crates/core/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,19 @@ pub mod collections {
5454
pub use hashbrown::hash_map;
5555
}
5656

57+
/// A tuple of keychain index and `T` representing the indexed value.
58+
pub type Indexed<T> = (u32, T);
59+
/// A tuple of keychain `K`, derivation index (`u32`) and a `T` associated with them.
60+
pub type KeychainIndexed<K, T> = ((K, u32), T);
61+
5762
mod chain_data;
5863
pub use chain_data::*;
5964

6065
mod checkpoint;
6166
pub use checkpoint::*;
6267

68+
pub mod spk_client;
69+
6370
/// Core structures for [`TxGraph`].
6471
///
6572
/// [`TxGraph`]: https://docs.rs/bdk_chain/latest/bdk_chain/tx_graph/struct.TxGraph.html

crates/chain/src/spk_client.rs renamed to crates/core/src/spk_client.rs

Lines changed: 12 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
use crate::{
33
alloc::{boxed::Box, collections::VecDeque, vec::Vec},
44
collections::BTreeMap,
5-
local_chain::CheckPoint,
6-
ConfirmationBlockTime, Indexed,
5+
CheckPoint, ConfirmationBlockTime, Indexed,
76
};
87
use bitcoin::{OutPoint, Script, ScriptBuf, Txid};
98

@@ -101,27 +100,6 @@ impl<I> Default for SyncRequestBuilder<I> {
101100
}
102101
}
103102

104-
#[cfg(feature = "miniscript")]
105-
impl<K: Clone + Ord + core::fmt::Debug + Send + Sync> SyncRequestBuilder<(K, u32)> {
106-
/// Add [`Script`]s that are revealed by the `indexer` of the given `spk_range` that will be
107-
/// synced against.
108-
pub fn revealed_spks_from_indexer(
109-
self,
110-
indexer: &crate::indexer::keychain_txout::KeychainTxOutIndex<K>,
111-
spk_range: impl core::ops::RangeBounds<K>,
112-
) -> Self {
113-
self.spks_with_indexes(indexer.revealed_spks(spk_range))
114-
}
115-
116-
/// Add [`Script`]s that are revealed by the `indexer` but currently unused.
117-
pub fn unused_spks_from_indexer(
118-
self,
119-
indexer: &crate::indexer::keychain_txout::KeychainTxOutIndex<K>,
120-
) -> Self {
121-
self.spks_with_indexes(indexer.unused_spks())
122-
}
123-
}
124-
125103
impl SyncRequestBuilder<()> {
126104
/// Add [`Script`]s that will be synced against.
127105
pub fn spks(self, spks: impl IntoIterator<Item = ScriptBuf>) -> Self {
@@ -132,7 +110,7 @@ impl SyncRequestBuilder<()> {
132110
impl<I> SyncRequestBuilder<I> {
133111
/// Set the initial chain tip for the sync request.
134112
///
135-
/// This is used to update [`LocalChain`](crate::local_chain::LocalChain).
113+
/// This is used to update [`LocalChain`](../../bdk_chain/local_chain/struct.LocalChain.html).
136114
pub fn chain_tip(mut self, cp: CheckPoint) -> Self {
137115
self.inner.chain_tip = Some(cp);
138116
self
@@ -143,7 +121,7 @@ impl<I> SyncRequestBuilder<I> {
143121
/// # Example
144122
///
145123
/// Sync revealed script pubkeys obtained from a
146-
/// [`KeychainTxOutIndex`](crate::keychain_txout::KeychainTxOutIndex).
124+
/// [`KeychainTxOutIndex`](../../bdk_chain/indexer/keychain_txout/struct.KeychainTxOutIndex.html).
147125
///
148126
/// ```rust
149127
/// # use bdk_chain::spk_client::SyncRequest;
@@ -216,9 +194,9 @@ impl<I> SyncRequestBuilder<I> {
216194
///
217195
/// ```rust
218196
/// # use bdk_chain::{bitcoin::{hashes::Hash, ScriptBuf}, local_chain::LocalChain};
197+
/// # use bdk_chain::spk_client::SyncRequest;
219198
/// # let (local_chain, _) = LocalChain::from_genesis_hash(Hash::all_zeros());
220199
/// # let scripts = [ScriptBuf::default(), ScriptBuf::default()];
221-
/// # use bdk_chain::spk_client::SyncRequest;
222200
/// // Construct a sync request.
223201
/// let sync_request = SyncRequest::builder()
224202
/// // Provide chain tip of the local wallet.
@@ -345,9 +323,11 @@ impl<I> SyncRequest<I> {
345323
#[must_use]
346324
#[derive(Debug)]
347325
pub struct SyncResult<A = ConfirmationBlockTime> {
348-
/// The update to apply to the receiving [`TxGraph`](crate::tx_graph::TxGraph).
326+
/// The update to apply to the receiving
327+
/// [`TxGraph`](../../bdk_chain/tx_graph/struct.TxGraph.html).
349328
pub graph_update: crate::tx_graph::Update<A>,
350-
/// The update to apply to the receiving [`LocalChain`](crate::local_chain::LocalChain).
329+
/// The update to apply to the receiving
330+
/// [`LocalChain`](../../bdk_chain/local_chain/struct.LocalChain.html).
351331
pub chain_update: Option<CheckPoint>,
352332
}
353333

@@ -374,24 +354,10 @@ impl<K> Default for FullScanRequestBuilder<K> {
374354
}
375355
}
376356

377-
#[cfg(feature = "miniscript")]
378-
impl<K: Ord + Clone + core::fmt::Debug> FullScanRequestBuilder<K> {
379-
/// Add spk iterators for each keychain tracked in `indexer`.
380-
pub fn spks_from_indexer(
381-
mut self,
382-
indexer: &crate::indexer::keychain_txout::KeychainTxOutIndex<K>,
383-
) -> Self {
384-
for (keychain, spks) in indexer.all_unbounded_spk_iters() {
385-
self = self.spks_for_keychain(keychain, spks);
386-
}
387-
self
388-
}
389-
}
390-
391357
impl<K: Ord> FullScanRequestBuilder<K> {
392358
/// Set the initial chain tip for the full scan request.
393359
///
394-
/// This is used to update [`LocalChain`](crate::local_chain::LocalChain).
360+
/// This is used to update [`LocalChain`](../../bdk_chain/local_chain/struct.LocalChain.html).
395361
pub fn chain_tip(mut self, tip: CheckPoint) -> Self {
396362
self.inner.chain_tip = Some(tip);
397363
self
@@ -496,9 +462,10 @@ impl<K: Ord + Clone> FullScanRequest<K> {
496462
#[must_use]
497463
#[derive(Debug)]
498464
pub struct FullScanResult<K, A = ConfirmationBlockTime> {
499-
/// The update to apply to the receiving [`LocalChain`](crate::local_chain::LocalChain).
465+
/// The update to apply to the receiving
466+
/// [`LocalChain`](../../bdk_chain/local_chain/struct.LocalChain.html).
500467
pub graph_update: crate::tx_graph::Update<A>,
501-
/// The update to apply to the receiving [`TxGraph`](crate::tx_graph::TxGraph).
468+
/// The update to apply to the receiving [`TxGraph`](../../bdk_chain/tx_graph/struct.TxGraph.html).
502469
pub chain_update: Option<CheckPoint>,
503470
/// Last active indices for the corresponding keychains (`K`).
504471
pub last_active_indices: BTreeMap<K, u32>,

crates/testenv/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use bdk_chain::{
44
secp256k1::rand::random, transaction, Address, Amount, Block, BlockHash, CompactTarget,
55
ScriptBuf, ScriptHash, Transaction, TxIn, TxOut, Txid,
66
},
7-
BlockId, local_chain::CheckPoint,
7+
local_chain::CheckPoint,
8+
BlockId,
89
};
910
use bitcoincore_rpc::{
1011
bitcoincore_rpc_json::{GetBlockTemplateModes, GetBlockTemplateRules},

crates/wallet/src/wallet/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2472,6 +2472,7 @@ impl Wallet {
24722472
/// [`SyncRequest`] collects all revealed script pubkeys from the wallet keychain needed to
24732473
/// start a blockchain sync with a spk based blockchain client.
24742474
pub fn start_sync_with_revealed_spks(&self) -> SyncRequestBuilder<(KeychainKind, u32)> {
2475+
use bdk_chain::keychain_txout::SyncRequestBuilderExt;
24752476
SyncRequest::builder()
24762477
.chain_tip(self.chain.tip())
24772478
.revealed_spks_from_indexer(&self.indexed_graph.index, ..)
@@ -2486,6 +2487,7 @@ impl Wallet {
24862487
/// This operation is generally only used when importing or restoring a previously used wallet
24872488
/// in which the list of used scripts is not known.
24882489
pub fn start_full_scan(&self) -> FullScanRequestBuilder<KeychainKind> {
2490+
use bdk_chain::keychain_txout::FullScanRequestBuilderExt;
24892491
FullScanRequest::builder()
24902492
.chain_tip(self.chain.tip())
24912493
.spks_from_indexer(&self.indexed_graph.index)

example-crates/example_esplora/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::{
66

77
use bdk_chain::{
88
bitcoin::Network,
9+
keychain_txout::FullScanRequestBuilderExt,
910
spk_client::{FullScanRequest, SyncRequest},
1011
Merge,
1112
};

0 commit comments

Comments
 (0)