Skip to content

Commit 76875e7

Browse files
committed
fix(chain)!: API and logical issues in KeychainTxOutIndex
Thanks to @ValuedMammal for the suggestions
1 parent d299dae commit 76875e7

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

crates/chain/src/indexer/keychain_txout.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -448,21 +448,26 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
448448
/// Store lookahead scripts until `target_index` (inclusive).
449449
///
450450
/// This does not change the global `lookahead` setting.
451-
pub fn lookahead_to_target(
452-
&mut self,
453-
keychain: K,
454-
target_index: u32,
455-
derived_spks: &mut impl Extend<Indexed<ScriptBuf>>,
456-
) {
451+
pub fn lookahead_to_target(&mut self, keychain: K, target_index: u32) -> ChangeSet {
452+
let mut changeset = ChangeSet::default();
457453
if let Some((next_index, _)) = self.next_index(keychain.clone()) {
458454
let temp_lookahead = (target_index + 1)
459455
.checked_sub(next_index)
460456
.filter(|&index| index > 0);
461457

462458
if let Some(temp_lookahead) = temp_lookahead {
463-
self.replenish_inner_index_keychain(keychain, temp_lookahead, derived_spks);
459+
let did = self
460+
.keychain_to_descriptor_id
461+
.get(&keychain)
462+
.expect("invariant");
463+
self.replenish_inner_index_keychain(
464+
keychain,
465+
temp_lookahead,
466+
changeset.spk_cache.entry(*did).or_default(),
467+
);
464468
}
465469
}
470+
changeset
466471
}
467472

468473
fn replenish_inner_index_did(
@@ -524,7 +529,7 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
524529
let spk_i = *_i;
525530
*_i = spk_i.saturating_add(1);
526531

527-
if let Some(spk) = spk_cache.get(_i) {
532+
if let Some(spk) = spk_cache.get(&spk_i) {
528533
return Some((spk_i, spk.clone()));
529534
}
530535
let spk = _desc

crates/chain/tests/test_keychain_txout_index.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ fn lookahead_to_target() {
607607
}
608608
None => target,
609609
};
610-
index.lookahead_to_target(keychain.clone(), target, &mut Vec::new());
610+
let _ = index.lookahead_to_target(keychain.clone(), target);
611611
let keys: Vec<_> = (0..)
612612
.take_while(|&i| index.spk_at_index(keychain.clone(), i).is_some())
613613
.collect();

0 commit comments

Comments
 (0)