Skip to content

Commit

Permalink
refactor(chain)!: Rename spks_with_labels to spks_with_indices
Browse files Browse the repository at this point in the history
and use consistent generic parameter names across `SyncRequest` and
`SyncRequestBuilder`.
  • Loading branch information
evanlinjin committed Aug 13, 2024
1 parent 62be7ad commit ac4e189
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
25 changes: 11 additions & 14 deletions crates/chain/src/spk_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,26 +110,26 @@ impl<K: Clone + Ord + core::fmt::Debug + Send + Sync> SyncRequestBuilder<(K, u32
indexer: &crate::indexer::keychain_txout::KeychainTxOutIndex<K>,
spk_range: impl core::ops::RangeBounds<K>,
) -> Self {
self.spks_with_labels(indexer.revealed_spks(spk_range))
self.spks_with_indexes(indexer.revealed_spks(spk_range))
}

/// Add [`Script`]s that are revealed by the `indexer` but currently unused.
pub fn unused_spks_from_indexer(
self,
indexer: &crate::indexer::keychain_txout::KeychainTxOutIndex<K>,
) -> Self {
self.spks_with_labels(indexer.unused_spks())
self.spks_with_indexes(indexer.unused_spks())
}
}

impl SyncRequestBuilder<()> {
/// Add [`Script`]s that will be synced against.
pub fn spks(self, spks: impl IntoIterator<Item = ScriptBuf>) -> Self {
self.spks_with_labels(spks.into_iter().map(|spk| ((), spk)))
self.spks_with_indexes(spks.into_iter().map(|spk| ((), spk)))
}
}

impl<SpkLabel> SyncRequestBuilder<SpkLabel> {
impl<I> SyncRequestBuilder<I> {
/// Set the initial chain tip for the sync request.
///
/// This is used to update [`LocalChain`](crate::local_chain::LocalChain).
Expand Down Expand Up @@ -158,28 +158,25 @@ impl<SpkLabel> SyncRequestBuilder<SpkLabel> {
///
/// /* Assume that the caller does more mutations to the `indexer` here... */
///
/// // Reveal spks for "descriptor_a", then build a sync request. Each spk will be labelled with
/// // Reveal spks for "descriptor_a", then build a sync request. Each spk will be indexed with
/// // `u32`, which represents the derivation index of the associated spk from "descriptor_a".
/// let (newly_revealed_spks, _changeset) = indexer
/// .reveal_to_target("descriptor_a", 21)
/// .expect("keychain must exist");
/// let _request = SyncRequest::builder()
/// .spks_with_labels(newly_revealed_spks)
/// .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 labelled with `(&'static str, u32)` where `&'static str` is
/// // 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()
/// .spks_with_labels(all_revealed_spks)
/// .spks_with_indexes(all_revealed_spks)
/// .build();
/// # Ok::<_, bdk_chain::keychain_txout::InsertDescriptorError<_>>(())
/// ```
pub fn spks_with_labels(
mut self,
spks: impl IntoIterator<Item = (SpkLabel, ScriptBuf)>,
) -> Self {
pub fn spks_with_indexes(mut self, spks: impl IntoIterator<Item = (I, ScriptBuf)>) -> Self {
self.inner.spks.extend(spks);
self
}
Expand All @@ -199,14 +196,14 @@ impl<SpkLabel> SyncRequestBuilder<SpkLabel> {
/// Set the closure that will inspect every sync item visited.
pub fn inspect<F>(mut self, inspect: F) -> Self
where
F: FnMut(SyncItem<SpkLabel>, SyncProgress) + Send + 'static,
F: FnMut(SyncItem<I>, SyncProgress) + Send + 'static,
{
self.inner.inspect = Box::new(inspect);
self
}

/// Build the [`SyncRequest`].
pub fn build(self) -> SyncRequest<SpkLabel> {
pub fn build(self) -> SyncRequest<I> {
self.inner
}
}
Expand Down
4 changes: 2 additions & 2 deletions example-crates/example_electrum/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,10 @@ fn main() -> anyhow::Result<()> {
});

if all_spks {
request = request.spks_with_labels(graph.index.revealed_spks(..));
request = request.spks_with_indexes(graph.index.revealed_spks(..));
}
if unused_spks {
request = request.spks_with_labels(graph.index.unused_spks());
request = request.spks_with_indexes(graph.index.unused_spks());
}
if utxos {
let init_outpoints = graph.index.outpoints();
Expand Down
4 changes: 2 additions & 2 deletions example-crates/example_esplora/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,10 @@ fn main() -> anyhow::Result<()> {
let chain = chain.lock().unwrap();

if *all_spks {
request = request.spks_with_labels(graph.index.revealed_spks(..));
request = request.spks_with_indexes(graph.index.revealed_spks(..));
}
if unused_spks {
request = request.spks_with_labels(graph.index.unused_spks());
request = request.spks_with_indexes(graph.index.unused_spks());
}
if utxos {
// We want to search for whether the UTXO is spent, and spent by which
Expand Down

0 comments on commit ac4e189

Please sign in to comment.