Skip to content

Commit

Permalink
feat(chain): panic when decreasing lookahead
Browse files Browse the repository at this point in the history
  • Loading branch information
evanlinjin committed Dec 21, 2023
1 parent ec7d716 commit 373d0f8
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions crates/chain/src/keychain/txout_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
&descriptor, old_descriptor,
"keychain already contains a different descriptor"
);
self.lookahead.insert(keychain.clone(), lookahead);
self.replenish_lookahead(&keychain);
self.set_lookahead(&keychain, lookahead)
}

/// Return the lookahead setting for each keychain.
Expand Down Expand Up @@ -207,9 +206,14 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
///
/// # Panics
///
/// This will panic if the `keychain` does not exist.
/// This will panic if the new `lookahead` value is smaller than the previous value.
/// This will also panic if the `keychain` does not exist.
pub fn set_lookahead(&mut self, keychain: &K, lookahead: u32) {
self.lookahead.insert(keychain.clone(), lookahead);
let old_lookahead = self
.lookahead
.insert(keychain.clone(), lookahead)
.unwrap_or(0);
assert!(old_lookahead <= lookahead, "lookahead must always increase");
self.replenish_lookahead(keychain);
}

Expand All @@ -228,9 +232,10 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
pub fn lookahead_to_target(&mut self, keychain: &K, target_index: u32) {
let next_index = self.next_store_index(keychain);
if let Some(temp_lookahead) = target_index.checked_sub(next_index).filter(|&v| v > 0) {
// We temporarily change the lookahead settings (so we can reuse the
// `replenish_lookahead` logic).
let old_lookahead = self.lookahead.insert(keychain.clone(), temp_lookahead);
self.replenish_lookahead(keychain);

// revert
match old_lookahead {
Some(lookahead) => self.lookahead.insert(keychain.clone(), lookahead),
Expand Down

0 comments on commit 373d0f8

Please sign in to comment.