Skip to content

Commit 264bb85

Browse files
committed
Merge #1261: Refactor reveal_to_target and next_store_index
761189a feat(chain): debug_assert non-wildcard desc. to only cache index 0 (志宇) 887e112 ref(chain): Refactor reveal_to_target (Daniela Brozzoni) 21d8875 ref(chain): Refactor next_store_index (Daniela Brozzoni) Pull request description: ### Description As a part of #1203, I found myself having to modify `reveal_to_target`, but had some troubles understanding exactly what the method was doing. This PR refactors `reveal_to_target` to be, in my opinion, a bit clearer. We now exist prematurely if `next_reveal_index` < `target_index`; this way, we don't need to keep track of `next_reveal_index`, as it would always be equal to `Some(target_index)`. As a part of trying to understand `reveal_to_target` I had to read through `next_store_index`, I slightly improved it for clarity reasons as well. ### Changelog notice ### Checklists #### All Submissions: * [x] I've signed all my commits * [x] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk/blob/master/CONTRIBUTING.md) * [x] I ran `cargo fmt` and `cargo clippy` before committing ACKs for top commit: evanlinjin: ACK 761189a Tree-SHA512: 17f70bccbfa1cccf718ece0eeb61f4944c9108776cf1d606ef823ae9ff58bf52114750927ac99561644ece598f8131ee7f7605071d42c91e5e88e05035e74836
2 parents f9dad51 + 761189a commit 264bb85

File tree

1 file changed

+30
-34
lines changed

1 file changed

+30
-34
lines changed

crates/chain/src/keychain/txout_index.rs

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,11 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
206206
fn next_store_index(&self, keychain: &K) -> u32 {
207207
self.inner()
208208
.all_spks()
209+
// This range is filtering out the spks with a keychain different than
210+
// `keychain`. We don't use filter here as range is more optimized.
209211
.range((keychain.clone(), u32::MIN)..(keychain.clone(), u32::MAX))
210212
.last()
211-
.map_or(0, |((_, v), _)| *v + 1)
213+
.map_or(0, |((_, index), _)| *index + 1)
212214
}
213215

214216
/// Generates script pubkey iterators for every `keychain`. The iterators iterate over all
@@ -362,51 +364,45 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
362364
let has_wildcard = descriptor.has_wildcard();
363365

364366
let target_index = if has_wildcard { target_index } else { 0 };
365-
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);
366371

367372
debug_assert!(next_reveal_index + self.lookahead >= self.next_store_index(keychain));
368373

369-
// if we need to reveal new indices, the latest revealed index goes here
370-
let mut reveal_to_index = None;
371-
372-
// if the target is not yet revealed, but is already stored (due to lookahead), we need to
373-
// set the `reveal_to_index` as target here (as the `for` loop below only updates
374-
// `reveal_to_index` for indexes that are NOT stored)
375-
if next_reveal_index <= target_index && target_index < next_reveal_index + self.lookahead {
376-
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+
);
377383
}
378384

379-
// 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
380388
let range = next_reveal_index + self.lookahead..=target_index + self.lookahead;
381389
for (new_index, new_spk) in SpkIterator::new_with_range(descriptor, range) {
382390
let _inserted = self
383391
.inner
384392
.insert_spk((keychain.clone(), new_index), new_spk);
385-
debug_assert!(_inserted, "must not have existing spk",);
386-
387-
// everything after `target_index` is stored for lookahead only
388-
if new_index <= target_index {
389-
reveal_to_index = Some(new_index);
390-
}
393+
debug_assert!(_inserted, "must not have existing spk");
394+
debug_assert!(
395+
has_wildcard || new_index == 0,
396+
"non-wildcard descriptors must not iterate past index 0"
397+
);
391398
}
392399

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

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

0 commit comments

Comments
 (0)