Skip to content
This repository has been archived by the owner on Mar 14, 2023. It is now read-only.

Commit

Permalink
Simplify RPC example with KeychainTxOutIndex::last_active
Browse files Browse the repository at this point in the history
  • Loading branch information
evanlinjin committed Dec 15, 2022
1 parent 4cfa0ba commit 320ae89
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 30 deletions.
31 changes: 9 additions & 22 deletions bdk_keychain/src/keychain_txout_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,15 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
.collect()
}

pub fn last_active(&self, keychain: &K) -> Option<(u32, &Script)> {
self.inner
.script_pubkeys()
.range((keychain.clone(), u32::MIN)..=(keychain.clone(), u32::MAX))
.filter(|(index, _)| self.is_used(index))
.last()
.map(|(&(_, index), script)| (index, script))
}

/// Convenience method to call [`derive_spks_up_to`] on several keychains.
///
/// Returns whether any new script pubkeys were derived (or if they had already all been
Expand Down Expand Up @@ -266,28 +275,6 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
.unwrap()
}
}

pub fn trailing_unused_count(&self, keychain: &K) -> usize {
let last_index = self
.inner
.range_unused((keychain.clone(), u32::MIN)..=(keychain.clone(), u32::MAX))
.last()
.map(|(&(_, index), _)| index)
.unwrap_or(0);

(0..=last_index)
.rev()
.map(|index| (keychain.clone(), index))
.take_while(|index| !self.inner.is_used(index))
.count()
}

pub fn trailing_unused_counts(&self) -> BTreeMap<K, usize> {
self.keychains
.iter()
.map(|(k, _)| (k.clone(), self.trailing_unused_count(k)))
.collect()
}
}

fn descriptor_into_script_iter(
Expand Down
27 changes: 19 additions & 8 deletions bdk_rpc_example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ enum RpcCommands {
initial_height: u64,
/// The unused-script gap will be kept at this value
#[clap(long, default_value = "5")]
stop_gap: usize,
stop_gap: u32,
},
}

Expand Down Expand Up @@ -169,14 +169,25 @@ fn main() -> anyhow::Result<()> {
Ok(())
}

pub fn ensure_spk_cached<K>(txout_index: &mut KeychainTxOutIndex<K>, stop_gap: usize)
pub fn ensure_spk_cached<K>(txout_index: &mut KeychainTxOutIndex<K>, stop_gap: u32) -> bool
where
K: Ord + Clone + core::fmt::Debug,
{
txout_index.trailing_unused_counts().into_iter().for_each(
|(keychain, trailing_unused_count)| {
let missing_count = stop_gap.saturating_sub(trailing_unused_count);
txout_index.derive_new_count(&keychain, missing_count);
},
);
if stop_gap == 0 {
return false;
}

let up_to_per_keychain = txout_index
.keychains()
.iter()
.map(|(keychain, _)| {
let up_to = txout_index
.last_active(keychain)
.map(|(last_active_index, _)| last_active_index + stop_gap)
.unwrap_or(stop_gap.saturating_sub(1));
(keychain.clone(), up_to)
})
.collect::<BTreeMap<_, _>>();

txout_index.store_all_up_to(&up_to_per_keychain)
}

0 comments on commit 320ae89

Please sign in to comment.