Skip to content

Commit fd8322a

Browse files
committed
1 parent ad865bb commit fd8322a

File tree

1 file changed

+35
-32
lines changed

1 file changed

+35
-32
lines changed

crates/chain/src/keychain/txout_index.rs

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,15 @@ impl<K: Clone + Ord + Debug> Indexer for KeychainTxOutIndex<K> {
142142

143143
fn index_txout(&mut self, outpoint: OutPoint, txout: &TxOut) -> Self::ChangeSet {
144144
let mut changeset = ChangeSet::default();
145-
if let Some((keychain, index)) = self.inner.scan_txout(outpoint, txout) {
145+
if let Some((keychain, index)) = self.inner.scan_txout(outpoint, txout).cloned() {
146146
let did = self
147147
.keychains_to_descriptor_ids
148-
.get(keychain)
148+
.get(&keychain)
149149
.expect("invariant");
150-
if self.last_revealed.get(did) < Some(index) {
151-
self.last_revealed.insert(*did, *index);
152-
changeset.last_revealed.insert(*did, *index);
153-
self.replenish_lookahead_did(*did);
150+
if self.last_revealed.get(did) < Some(&index) {
151+
self.last_revealed.insert(*did, index);
152+
changeset.last_revealed.insert(*did, index);
153+
self.replenish_lookahead(*did, &keychain, self.lookahead);
154154
}
155155
}
156156
changeset
@@ -376,7 +376,7 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
376376
.insert(keychain.clone(), desc_id);
377377
self.descriptor_ids_to_keychains
378378
.insert(desc_id, keychain.clone());
379-
self.replenish_lookahead(&keychain, self.lookahead);
379+
self.replenish_lookahead_keychain(&keychain, self.lookahead);
380380
changeset
381381
.keychains_added
382382
.insert(keychain.clone(), descriptor);
@@ -439,39 +439,42 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
439439
.filter(|&index| index > 0);
440440

441441
if let Some(temp_lookahead) = temp_lookahead {
442-
self.replenish_lookahead(keychain, temp_lookahead);
442+
self.replenish_lookahead_keychain(keychain, temp_lookahead);
443443
}
444444
}
445445
}
446446

447-
fn replenish_lookahead_did(&mut self, did: DescriptorId) {
447+
fn replenish_lookahead_did(&mut self, did: DescriptorId, lookahead: u32) {
448448
if let Some(keychain) = self.descriptor_ids_to_keychains.get(&did).cloned() {
449-
self.replenish_lookahead(&keychain, self.lookahead);
449+
self.replenish_lookahead(did, &keychain, lookahead);
450450
}
451451
}
452452

453-
fn replenish_lookahead(&mut self, keychain: &K, lookahead: u32) {
453+
fn replenish_lookahead_keychain(&mut self, keychain: &K, lookahead: u32) {
454454
if let Some(did) = self.keychains_to_descriptor_ids.get(keychain) {
455-
let descriptor = self
456-
.descriptor_ids_to_descriptors
457-
.get(did)
458-
.expect("invariant");
459-
let next_store_index = self
455+
self.replenish_lookahead(*did, keychain, lookahead);
456+
}
457+
}
458+
459+
fn replenish_lookahead(&mut self, did: DescriptorId, keychain: &K, lookahead: u32) {
460+
let descriptor = self
461+
.descriptor_ids_to_descriptors
462+
.get(&did)
463+
.expect("invariant");
464+
let next_store_index = self
465+
.inner
466+
.all_spks()
467+
.range(&(keychain.clone(), u32::MIN)..=&(keychain.clone(), u32::MAX))
468+
.last()
469+
.map_or(0, |((_, index), _)| *index + 1);
470+
let next_reveal_index = self.last_revealed.get(&did).map_or(0, |v| *v + 1);
471+
for (new_index, new_spk) in
472+
SpkIterator::new_with_range(descriptor, next_store_index..next_reveal_index + lookahead)
473+
{
474+
let _inserted = self
460475
.inner
461-
.all_spks()
462-
.range(&(keychain.clone(), u32::MIN)..=&(keychain.clone(), u32::MAX))
463-
.last()
464-
.map_or(0, |((_, index), _)| *index + 1);
465-
let next_reveal_index = self.last_revealed.get(did).map_or(0, |v| *v + 1);
466-
for (new_index, new_spk) in SpkIterator::new_with_range(
467-
descriptor,
468-
next_store_index..next_reveal_index + lookahead,
469-
) {
470-
let _inserted = self
471-
.inner
472-
.insert_spk((keychain.clone(), new_index), new_spk);
473-
debug_assert!(_inserted, "replenish lookahead: must not have existing spk: keychain={:?}, lookahead={}, next_store_index={}, next_reveal_index={}", keychain, lookahead, next_store_index, next_reveal_index);
474-
}
476+
.insert_spk((keychain.clone(), new_index), new_spk);
477+
debug_assert!(_inserted, "replenish lookahead: must not have existing spk: keychain={:?}, lookahead={}, next_store_index={}, next_reveal_index={}", keychain, lookahead, next_store_index, next_reveal_index);
475478
}
476479
}
477480

@@ -719,7 +722,7 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
719722
let _ = self.inner.insert_spk((keychain.clone(), next_index), spk);
720723
self.last_revealed.insert(*did, next_index);
721724
changeset.last_revealed.insert(*did, next_index);
722-
self.replenish_lookahead(keychain, self.lookahead);
725+
self.replenish_lookahead_keychain(keychain, self.lookahead);
723726
}
724727
let script = self
725728
.inner
@@ -823,7 +826,7 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
823826
}
824827

825828
for did in last_revealed.keys() {
826-
self.replenish_lookahead_did(*did);
829+
self.replenish_lookahead_did(*did, self.lookahead);
827830
}
828831
}
829832
}

0 commit comments

Comments
 (0)