@@ -176,8 +176,7 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
176176 & descriptor, old_descriptor,
177177 "keychain already contains a different descriptor"
178178 ) ;
179- self . lookahead . insert ( keychain. clone ( ) , lookahead) ;
180- self . replenish_lookahead ( & keychain) ;
179+ self . set_lookahead ( & keychain, lookahead)
181180 }
182181
183182 /// Return the lookahead setting for each keychain.
@@ -207,9 +206,14 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
207206 ///
208207 /// # Panics
209208 ///
210- /// This will panic if the `keychain` does not exist.
209+ /// This will panic if the new `lookahead` value is smaller than the previous value.
210+ /// This will also panic if the `keychain` does not exist.
211211 pub fn set_lookahead ( & mut self , keychain : & K , lookahead : u32 ) {
212- self . lookahead . insert ( keychain. clone ( ) , lookahead) ;
212+ let old_lookahead = self
213+ . lookahead
214+ . insert ( keychain. clone ( ) , lookahead)
215+ . unwrap_or ( 0 ) ;
216+ assert ! ( old_lookahead <= lookahead, "lookahead must always increase" ) ;
213217 self . replenish_lookahead ( keychain) ;
214218 }
215219
@@ -228,9 +232,10 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
228232 pub fn lookahead_to_target ( & mut self , keychain : & K , target_index : u32 ) {
229233 let next_index = self . next_store_index ( keychain) ;
230234 if let Some ( temp_lookahead) = target_index. checked_sub ( next_index) . filter ( |& v| v > 0 ) {
235+ // We temporarily change the lookahead settings (so we can reuse the
236+ // `replenish_lookahead` logic).
231237 let old_lookahead = self . lookahead . insert ( keychain. clone ( ) , temp_lookahead) ;
232238 self . replenish_lookahead ( keychain) ;
233-
234239 // revert
235240 match old_lookahead {
236241 Some ( lookahead) => self . lookahead . insert ( keychain. clone ( ) , lookahead) ,
0 commit comments