Skip to content

Commit

Permalink
fix: Remove collect, use DoubleEndedIterator
Browse files Browse the repository at this point in the history
  • Loading branch information
danielabrozzoni committed May 8, 2024
1 parent f3c463a commit bb0a951
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
4 changes: 3 additions & 1 deletion crates/bdk/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,9 @@ impl Wallet {
/// script pubkeys the wallet is storing internally).
pub fn all_unbounded_spk_iters(
&self,
) -> BTreeMap<KeychainKind, impl Iterator<Item = (u32, ScriptBuf)> + Clone> {
) -> impl DoubleEndedIterator<
Item = (KeychainKind, impl Iterator<Item = (u32, ScriptBuf)> + Clone),
> + '_ {
self.indexed_graph.index.all_unbounded_spk_iters()
}

Expand Down
7 changes: 5 additions & 2 deletions crates/bdk/tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,11 @@ fn load_recovers_wallet() {
wallet_spk_index.keychains().collect::<Vec<_>>()
);
assert_eq!(
wallet.spk_index().last_revealed_indices(),
wallet_spk_index.last_revealed_indices()
wallet
.spk_index()
.last_revealed_indices()
.collect::<Vec<_>>(),
wallet_spk_index.last_revealed_indices().collect::<Vec<_>>()
);
let secp = Secp256k1::new();
assert_eq!(
Expand Down
20 changes: 8 additions & 12 deletions crates/chain/src/keychain/txout_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,11 +575,11 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
/// Get unbounded spk iterators for all keychains.
pub fn all_unbounded_spk_iters(
&self,
) -> BTreeMap<K, SpkIterator<Descriptor<DescriptorPublicKey>>> {
) -> impl DoubleEndedIterator<Item = (K, SpkIterator<Descriptor<DescriptorPublicKey>>)> + Clone + '_
{
self.keychains_to_descriptors
.iter()
.map(|(k, (_, descriptor))| (k.clone(), SpkIterator::new(descriptor.clone())))
.collect()
}

/// Iterate over revealed spks of keychains in `range`
Expand Down Expand Up @@ -684,14 +684,11 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
/// Get the last derivation index that is revealed for each keychain.
///
/// Keychains with no revealed indices will not be included in the returned [`BTreeMap`].
pub fn last_revealed_indices(&self) -> BTreeMap<K, u32> {
self.last_revealed
.iter()
.filter_map(|(desc_id, index)| {
let keychain = self.keychain_of_desc_id(desc_id)?;
Some((keychain.clone(), *index))
})
.collect()
pub fn last_revealed_indices(&self) -> impl DoubleEndedIterator<Item = (K, u32)> + '_ {
self.last_revealed.iter().filter_map(|(desc_id, index)| {
let keychain = self.keychain_of_desc_id(desc_id)?;
Some((keychain.clone(), *index))
})
}

/// Get the last derivation index revealed for `keychain`. Returns None if the keychain doesn't
Expand Down Expand Up @@ -923,14 +920,13 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {

/// Returns the highest derivation index of each keychain that [`KeychainTxOutIndex`] has found
/// a [`TxOut`] with it's script pubkey.
pub fn last_used_indices(&self) -> BTreeMap<K, u32> {
pub fn last_used_indices(&self) -> impl DoubleEndedIterator<Item = (K, u32)> + '_ {
self.keychains_to_descriptors
.iter()
.filter_map(|(keychain, _)| {
self.last_used_index(keychain)
.map(|index| (keychain.clone(), index))
})
.collect()
}

/// Applies the derivation changeset to the [`KeychainTxOutIndex`], as specified in the
Expand Down
11 changes: 8 additions & 3 deletions crates/chain/tests/test_keychain_txout_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,12 @@ fn test_set_all_derivation_indices() {
last_revealed: last_revealed.clone()
}
);
assert_eq!(txout_index.last_revealed_indices(), derive_to);
assert_eq!(
txout_index
.last_revealed_indices()
.collect::<BTreeMap<_, _>>(),
derive_to
);
assert_eq!(
txout_index.reveal_to_target_multi(&derive_to).1,
keychain::ChangeSet::default(),
Expand Down Expand Up @@ -738,8 +743,8 @@ fn applying_changesets_one_by_one_vs_aggregate_must_have_same_result() {
indexer_b.spk_at_index(TestKeychain::Internal, 0)
);
assert_eq!(
indexer_a.last_revealed_indices(),
indexer_b.last_revealed_indices()
indexer_a.last_revealed_indices().collect::<Vec<_>>(),
indexer_b.last_revealed_indices().collect::<Vec<_>>(),
);
}

Expand Down

0 comments on commit bb0a951

Please sign in to comment.