Skip to content

Commit 132f91a

Browse files
ref(chain): Refactor reveal_to_target
Simplify the `reveal_to_target` algorithm by exiting prematurely if the `target_index` is already revealed. Since the `reveal_to_index` variable was different from `Some(target_index)` only if the target was already revealed, we can getrid of the variable altogether.
1 parent 0ea1fb5 commit 132f91a

File tree

1 file changed

+22
-32
lines changed

1 file changed

+22
-32
lines changed

crates/chain/src/keychain/txout_index.rs

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -363,51 +363,41 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
363363
let has_wildcard = descriptor.has_wildcard();
364364

365365
let target_index = if has_wildcard { target_index } else { 0 };
366-
let next_reveal_index = self.last_revealed.get(keychain).map_or(0, |v| *v + 1);
366+
let next_reveal_index = self
367+
.last_revealed
368+
.get(keychain)
369+
.map_or(0, |index| *index + 1);
367370

368371
debug_assert!(next_reveal_index + self.lookahead >= self.next_store_index(keychain));
369372

370-
// if we need to reveal new indices, the latest revealed index goes here
371-
let mut reveal_to_index = None;
372-
373-
// if the target is not yet revealed, but is already stored (due to lookahead), we need to
374-
// set the `reveal_to_index` as target here (as the `for` loop below only updates
375-
// `reveal_to_index` for indexes that are NOT stored)
376-
if next_reveal_index <= target_index && target_index < next_reveal_index + self.lookahead {
377-
reveal_to_index = Some(target_index);
373+
// If the target_index is already revealed, we are done
374+
if next_reveal_index > target_index {
375+
return (
376+
SpkIterator::new_with_range(
377+
descriptor.clone(),
378+
next_reveal_index..next_reveal_index,
379+
),
380+
super::ChangeSet::default(),
381+
);
378382
}
379383

380-
// we range over indexes that are not stored
384+
// We range over the indexes that are not stored and insert their spks in the index.
385+
// Indexes from next_reveal_index to next_reveal_index + lookahead are already stored (due
386+
// to lookahead), so we only range from next_reveal_index + lookahead to target + lookahead
381387
let range = next_reveal_index + self.lookahead..=target_index + self.lookahead;
382388
for (new_index, new_spk) in SpkIterator::new_with_range(descriptor, range) {
383389
let _inserted = self
384390
.inner
385391
.insert_spk((keychain.clone(), new_index), new_spk);
386392
debug_assert!(_inserted, "must not have existing spk",);
387-
388-
// everything after `target_index` is stored for lookahead only
389-
if new_index <= target_index {
390-
reveal_to_index = Some(new_index);
391-
}
392393
}
393394

394-
match reveal_to_index {
395-
Some(index) => {
396-
let _old_index = self.last_revealed.insert(keychain.clone(), index);
397-
debug_assert!(_old_index < Some(index));
398-
(
399-
SpkIterator::new_with_range(descriptor.clone(), next_reveal_index..index + 1),
400-
super::ChangeSet(core::iter::once((keychain.clone(), index)).collect()),
401-
)
402-
}
403-
None => (
404-
SpkIterator::new_with_range(
405-
descriptor.clone(),
406-
next_reveal_index..next_reveal_index,
407-
),
408-
super::ChangeSet::default(),
409-
),
410-
}
395+
let _old_index = self.last_revealed.insert(keychain.clone(), target_index);
396+
debug_assert!(_old_index < Some(target_index));
397+
(
398+
SpkIterator::new_with_range(descriptor.clone(), next_reveal_index..target_index + 1),
399+
super::ChangeSet(core::iter::once((keychain.clone(), target_index)).collect()),
400+
)
411401
}
412402

413403
/// Attempts to reveal the next script pubkey for `keychain`.

0 commit comments

Comments
 (0)