Skip to content

Commit 887e112

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 21d8875 commit 887e112

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
@@ -364,51 +364,41 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
364364
let has_wildcard = descriptor.has_wildcard();
365365

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

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

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

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

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

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

0 commit comments

Comments
 (0)