@@ -5,7 +5,6 @@ use crate::{
55 spk_iter:: BIP32_MAX_INDEX ,
66 SpkIterator , SpkTxOutIndex ,
77} ;
8- use alloc:: vec:: Vec ;
98use bitcoin:: { OutPoint , Script , TxOut } ;
109use core:: { fmt:: Debug , ops:: Deref } ;
1110
@@ -67,17 +66,12 @@ pub struct KeychainTxOutIndex<K> {
6766 // last revealed indexes
6867 last_revealed : BTreeMap < K , u32 > ,
6968 // lookahead settings for each keychain
70- lookahead : BTreeMap < K , u32 > ,
69+ lookahead : u32 ,
7170}
7271
7372impl < K > Default for KeychainTxOutIndex < K > {
7473 fn default ( ) -> Self {
75- Self {
76- inner : SpkTxOutIndex :: default ( ) ,
77- keychains : BTreeMap :: default ( ) ,
78- last_revealed : BTreeMap :: default ( ) ,
79- lookahead : BTreeMap :: default ( ) ,
80- }
74+ Self :: new ( DEFAULT_LOOKAHEAD )
8175 }
8276}
8377
@@ -120,6 +114,24 @@ impl<K: Clone + Ord + Debug> Indexer for KeychainTxOutIndex<K> {
120114 }
121115}
122116
117+ impl < K > KeychainTxOutIndex < K > {
118+ /// Construct a [`KeychainTxOutIndex`] with the given `lookahead`.
119+ ///
120+ /// The lookahead is the number of scripts to cache ahead of the last revealed script index.
121+ /// This is useful to find outputs you own when processing block data that lie beyond the last
122+ /// revealed index. In certain situations, such as when performing an initial scan of the
123+ /// blockchain during wallet import, it may be uncertain or unknown what the last revealed index
124+ /// is.
125+ pub fn new ( lookahead : u32 ) -> Self {
126+ Self {
127+ inner : SpkTxOutIndex :: default ( ) ,
128+ keychains : BTreeMap :: new ( ) ,
129+ last_revealed : BTreeMap :: new ( ) ,
130+ lookahead,
131+ }
132+ }
133+ }
134+
123135impl < K : Clone + Ord + Debug > KeychainTxOutIndex < K > {
124136 /// Return a reference to the internal [`SpkTxOutIndex`].
125137 pub fn inner ( & self ) -> & SpkTxOutIndex < ( K , u32 ) > {
@@ -136,38 +148,17 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
136148 & self . keychains
137149 }
138150
139- /// Add a keychain to the tracker's `txout_index` with a descriptor to derive addresses and a
140- /// default lookahead of `1_000`.
141- ///
142- /// See [`add_keychain_with_lookahead`] for details.
143- ///
144- /// # Panics
145- ///
146- /// This will panic if a different `descriptor` is introduced to the same `keychain`.
147- ///
148- /// [`add_keychain_with_lookahead`]: Self::add_keychain_with_lookahead
149- pub fn add_keychain ( & mut self , keychain : K , descriptor : Descriptor < DescriptorPublicKey > ) {
150- self . add_keychain_with_lookahead ( keychain, descriptor, DEFAULT_LOOKAHEAD )
151- }
152-
153151 /// Add a keychain to the tracker's `txout_index` with a descriptor to derive addresses.
154152 ///
155153 /// Adding a keychain means you will be able to derive new script pubkeys under that keychain
156154 /// and the txout index will discover transaction outputs with those script pubkeys.
157155 ///
158- /// Refer to [`set_lookahead`] for an explanation of the `lookahead` parameter.
159- ///
160156 /// # Panics
161157 ///
162158 /// This will panic if a different `descriptor` is introduced to the same `keychain`.
163159 ///
164- /// [`set_lookahead`]: Self::set_lookahead
165- pub fn add_keychain_with_lookahead (
166- & mut self ,
167- keychain : K ,
168- descriptor : Descriptor < DescriptorPublicKey > ,
169- lookahead : u32 ,
170- ) {
160+ /// [`add_keychain_with_lookahead`]: Self::add_keychain_with_lookahead
161+ pub fn add_keychain ( & mut self , keychain : K , descriptor : Descriptor < DescriptorPublicKey > ) {
171162 let old_descriptor = & * self
172163 . keychains
173164 . entry ( keychain. clone ( ) )
@@ -176,54 +167,12 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
176167 & descriptor, old_descriptor,
177168 "keychain already contains a different descriptor"
178169 ) ;
179- self . set_lookahead ( & keychain, lookahead)
180- }
181-
182- /// Return the lookahead setting for each keychain.
183- ///
184- /// Refer to [`set_lookahead`] for a deeper explanation of the `lookahead`.
185- ///
186- /// [`set_lookahead`]: Self::set_lookahead
187- pub fn lookaheads ( & self ) -> & BTreeMap < K , u32 > {
188- & self . lookahead
189- }
190-
191- /// Convenience method to call [`set_lookahead`] for all keychains.
192- ///
193- /// [`set_lookahead`]: Self::set_lookahead
194- pub fn set_lookahead_for_all ( & mut self , lookahead : u32 ) {
195- for keychain in & self . keychains . keys ( ) . cloned ( ) . collect :: < Vec < _ > > ( ) {
196- self . set_lookahead ( keychain, lookahead) ;
197- }
198- }
199-
200- /// Set the lookahead count for `keychain`.
201- ///
202- /// The lookahead is the number of scripts to cache ahead of the last revealed script index. This
203- /// is useful to find outputs you own when processing block data that lie beyond the last revealed
204- /// index. In certain situations, such as when performing an initial scan of the blockchain during
205- /// wallet import, it may be uncertain or unknown what the last revealed index is.
206- ///
207- /// # Panics
208- ///
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.
211- pub fn set_lookahead ( & mut self , keychain : & K , lookahead : u32 ) {
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" ) ;
217- self . replenish_lookahead ( keychain) ;
170+ self . replenish_lookahead ( & keychain, self . lookahead ) ;
218171 }
219172
220- /// Convenience method to call [`lookahead_to_target`] for multiple keychains.
221- ///
222- /// [`lookahead_to_target`]: Self::lookahead_to_target
223- pub fn lookahead_to_target_multi ( & mut self , target_indexes : BTreeMap < K , u32 > ) {
224- for ( keychain, target_index) in target_indexes {
225- self . lookahead_to_target ( & keychain, target_index)
226- }
173+ /// Return the lookahead setting.
174+ pub fn lookahead ( & self ) -> u32 {
175+ self . lookahead
227176 }
228177
229178 /// Store lookahead scripts until `target_index`.
@@ -232,23 +181,14 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
232181 pub fn lookahead_to_target ( & mut self , keychain : & K , target_index : u32 ) {
233182 let next_index = self . next_store_index ( keychain) ;
234183 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).
237- let old_lookahead = self . lookahead . insert ( keychain. clone ( ) , temp_lookahead) ;
238- self . replenish_lookahead ( keychain) ;
239- // revert
240- match old_lookahead {
241- Some ( lookahead) => self . lookahead . insert ( keychain. clone ( ) , lookahead) ,
242- None => self . lookahead . remove ( keychain) ,
243- } ;
184+ self . replenish_lookahead ( keychain, temp_lookahead) ;
244185 }
245186 }
246187
247- fn replenish_lookahead ( & mut self , keychain : & K ) {
188+ fn replenish_lookahead ( & mut self , keychain : & K , lookahead : u32 ) {
248189 let descriptor = self . keychains . get ( keychain) . expect ( "keychain must exist" ) ;
249190 let next_store_index = self . next_store_index ( keychain) ;
250191 let next_reveal_index = self . last_revealed . get ( keychain) . map_or ( 0 , |v| * v + 1 ) ;
251- let lookahead = self . lookahead . get ( keychain) . map_or ( 0 , |v| * v) ;
252192
253193 for ( new_index, new_spk) in
254194 SpkIterator :: new_with_range ( descriptor, next_store_index..next_reveal_index + lookahead)
@@ -420,22 +360,21 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
420360
421361 let target_index = if has_wildcard { target_index } else { 0 } ;
422362 let next_reveal_index = self . last_revealed . get ( keychain) . map_or ( 0 , |v| * v + 1 ) ;
423- let lookahead = self . lookahead . get ( keychain) . map_or ( 0 , |v| * v) ;
424363
425- debug_assert ! ( next_reveal_index + lookahead >= self . next_store_index( keychain) ) ;
364+ debug_assert ! ( next_reveal_index + self . lookahead >= self . next_store_index( keychain) ) ;
426365
427366 // if we need to reveal new indices, the latest revealed index goes here
428367 let mut reveal_to_index = None ;
429368
430369 // if the target is not yet revealed, but is already stored (due to lookahead), we need to
431370 // set the `reveal_to_index` as target here (as the `for` loop below only updates
432371 // `reveal_to_index` for indexes that are NOT stored)
433- if next_reveal_index <= target_index && target_index < next_reveal_index + lookahead {
372+ if next_reveal_index <= target_index && target_index < next_reveal_index + self . lookahead {
434373 reveal_to_index = Some ( target_index) ;
435374 }
436375
437376 // we range over indexes that are not stored
438- let range = next_reveal_index + lookahead..=target_index + lookahead;
377+ let range = next_reveal_index + self . lookahead ..=target_index + self . lookahead ;
439378 for ( new_index, new_spk) in SpkIterator :: new_with_range ( descriptor, range) {
440379 let _inserted = self
441380 . inner
0 commit comments