From: 志宇 Date: Fri, 23 May 2025 00:47:26 +0000 (+1000) Subject: fix(chain)!: API and logical issues in `KeychainTxOutIndex` X-Git-Tag: core-0.6.0~1^2~6 X-Git-Url: http://internal-gitweb-vhost/script/%22https:/database/scripts/trait.GetTx.html?a=commitdiff_plain;h=76875e7a4ea36ed5ae4b5ae211068111d3f28813;p=bdk fix(chain)!: API and logical issues in `KeychainTxOutIndex` Thanks to @valuedmammal for the suggestions --- diff --git a/crates/chain/src/indexer/keychain_txout.rs b/crates/chain/src/indexer/keychain_txout.rs index 0fa11ad8..37f229dd 100644 --- a/crates/chain/src/indexer/keychain_txout.rs +++ b/crates/chain/src/indexer/keychain_txout.rs @@ -448,21 +448,26 @@ impl KeychainTxOutIndex { /// Store lookahead scripts until `target_index` (inclusive). /// /// This does not change the global `lookahead` setting. - pub fn lookahead_to_target( - &mut self, - keychain: K, - target_index: u32, - derived_spks: &mut impl Extend>, - ) { + pub fn lookahead_to_target(&mut self, keychain: K, target_index: u32) -> ChangeSet { + let mut changeset = ChangeSet::default(); if let Some((next_index, _)) = self.next_index(keychain.clone()) { let temp_lookahead = (target_index + 1) .checked_sub(next_index) .filter(|&index| index > 0); if let Some(temp_lookahead) = temp_lookahead { - self.replenish_inner_index_keychain(keychain, temp_lookahead, derived_spks); + let did = self + .keychain_to_descriptor_id + .get(&keychain) + .expect("invariant"); + self.replenish_inner_index_keychain( + keychain, + temp_lookahead, + changeset.spk_cache.entry(*did).or_default(), + ); } } + changeset } fn replenish_inner_index_did( @@ -524,7 +529,7 @@ impl KeychainTxOutIndex { let spk_i = *_i; *_i = spk_i.saturating_add(1); - if let Some(spk) = spk_cache.get(_i) { + if let Some(spk) = spk_cache.get(&spk_i) { return Some((spk_i, spk.clone())); } let spk = _desc diff --git a/crates/chain/tests/test_keychain_txout_index.rs b/crates/chain/tests/test_keychain_txout_index.rs index 27baeacc..6619d5c2 100644 --- a/crates/chain/tests/test_keychain_txout_index.rs +++ b/crates/chain/tests/test_keychain_txout_index.rs @@ -607,7 +607,7 @@ fn lookahead_to_target() { } None => target, }; - index.lookahead_to_target(keychain.clone(), target, &mut Vec::new()); + let _ = index.lookahead_to_target(keychain.clone(), target); let keys: Vec<_> = (0..) .take_while(|&i| index.spk_at_index(keychain.clone(), i).is_some()) .collect();