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.
This commit is contained in:
Daniela Brozzoni 2024-01-08 14:47:28 +01:00
parent 21d8875826
commit 887e112e8f
No known key found for this signature in database
GPG Key ID: 7DE4F1FDCED0AB87

View File

@ -364,51 +364,41 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
let has_wildcard = descriptor.has_wildcard(); let has_wildcard = descriptor.has_wildcard();
let target_index = if has_wildcard { target_index } else { 0 }; let target_index = if has_wildcard { target_index } else { 0 };
let next_reveal_index = self.last_revealed.get(keychain).map_or(0, |v| *v + 1); let next_reveal_index = self
.last_revealed
.get(keychain)
.map_or(0, |index| *index + 1);
debug_assert!(next_reveal_index + self.lookahead >= self.next_store_index(keychain)); debug_assert!(next_reveal_index + self.lookahead >= self.next_store_index(keychain));
// if we need to reveal new indices, the latest revealed index goes here // If the target_index is already revealed, we are done
let mut reveal_to_index = None; if next_reveal_index > target_index {
return (
// if the target is not yet revealed, but is already stored (due to lookahead), we need to SpkIterator::new_with_range(
// set the `reveal_to_index` as target here (as the `for` loop below only updates descriptor.clone(),
// `reveal_to_index` for indexes that are NOT stored) next_reveal_index..next_reveal_index,
if next_reveal_index <= target_index && target_index < next_reveal_index + self.lookahead { ),
reveal_to_index = Some(target_index); super::ChangeSet::default(),
);
} }
// we range over indexes that are not stored // We range over the indexes that are not stored and insert their spks in the index.
// Indexes from next_reveal_index to next_reveal_index + lookahead are already stored (due
// to lookahead), so we only range from next_reveal_index + lookahead to target + lookahead
let range = next_reveal_index + self.lookahead..=target_index + self.lookahead; let range = next_reveal_index + self.lookahead..=target_index + self.lookahead;
for (new_index, new_spk) in SpkIterator::new_with_range(descriptor, range) { for (new_index, new_spk) in SpkIterator::new_with_range(descriptor, range) {
let _inserted = self let _inserted = self
.inner .inner
.insert_spk((keychain.clone(), new_index), new_spk); .insert_spk((keychain.clone(), new_index), new_spk);
debug_assert!(_inserted, "must not have existing spk",); debug_assert!(_inserted, "must not have existing spk",);
// everything after `target_index` is stored for lookahead only
if new_index <= target_index {
reveal_to_index = Some(new_index);
}
} }
match reveal_to_index { let _old_index = self.last_revealed.insert(keychain.clone(), target_index);
Some(index) => { debug_assert!(_old_index < Some(target_index));
let _old_index = self.last_revealed.insert(keychain.clone(), index); (
debug_assert!(_old_index < Some(index)); SpkIterator::new_with_range(descriptor.clone(), next_reveal_index..target_index + 1),
( super::ChangeSet(core::iter::once((keychain.clone(), target_index)).collect()),
SpkIterator::new_with_range(descriptor.clone(), next_reveal_index..index + 1), )
super::ChangeSet(core::iter::once((keychain.clone(), index)).collect()),
)
}
None => (
SpkIterator::new_with_range(
descriptor.clone(),
next_reveal_index..next_reveal_index,
),
super::ChangeSet::default(),
),
}
} }
/// Attempts to reveal the next script pubkey for `keychain`. /// Attempts to reveal the next script pubkey for `keychain`.