]> Untitled Git - bdk/commitdiff
fix(chain)!: API and logical issues in `KeychainTxOutIndex`
author志宇 <hello@evanlinjin.me>
Fri, 23 May 2025 00:47:26 +0000 (10:47 +1000)
committer志宇 <hello@evanlinjin.me>
Fri, 23 May 2025 00:50:31 +0000 (10:50 +1000)
Thanks to @valuedmammal for the suggestions

crates/chain/src/indexer/keychain_txout.rs
crates/chain/tests/test_keychain_txout_index.rs

index 0fa11ad8e05191c60711b749b103709ec7d55ad4..37f229dd346e7b1bfbab270823b90f5f5c0c3e07 100644 (file)
@@ -448,21 +448,26 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
     /// 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<Indexed<ScriptBuf>>,
-    ) {
+    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<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
                 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
index 27baeaccfef44e56eb6845aea40743fc95f892b7..6619d5c22bed47daf60cd5ccc11141d03895c35f 100644 (file)
@@ -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();