@@ -120,18 +120,10 @@ pub const DEFAULT_LOOKAHEAD: u32 = 25;
120120#[ derive( Clone , Debug ) ]
121121pub struct KeychainTxOutIndex < K > {
122122 inner : SpkTxOutIndex < ( K , u32 ) > ,
123- /// keychain -> (descriptor id) map
124- keychains_to_descriptor_ids : BTreeMap < K , DescriptorId > ,
125- /// descriptor id -> keychain map
126- descriptor_ids_to_keychains : HashMap < DescriptorId , K > ,
127- /// descriptor_id -> descriptor map
128- /// This is a "monotone" map, meaning that its size keeps growing, i.e., we never delete
129- /// descriptors from it. This is useful for revealing spks for descriptors that don't have
130- /// keychains associated.
131- descriptor_ids_to_descriptors : HashMap < DescriptorId , Descriptor < DescriptorPublicKey > > ,
132- /// last revealed indices for each descriptor.
123+ keychain_to_descriptor_id : BTreeMap < K , DescriptorId > ,
124+ descriptor_id_to_keychain : HashMap < DescriptorId , K > ,
125+ descriptors : HashMap < DescriptorId , Descriptor < DescriptorPublicKey > > ,
133126 last_revealed : HashMap < DescriptorId , u32 > ,
134- /// lookahead setting
135127 lookahead : u32 ,
136128}
137129
@@ -148,7 +140,7 @@ impl<K: Clone + Ord + Debug> Indexer for KeychainTxOutIndex<K> {
148140 let mut changeset = ChangeSet :: default ( ) ;
149141 if let Some ( ( keychain, index) ) = self . inner . scan_txout ( outpoint, txout) . cloned ( ) {
150142 let did = self
151- . keychains_to_descriptor_ids
143+ . keychain_to_descriptor_id
152144 . get ( & keychain)
153145 . expect ( "invariant" ) ;
154146 if self . last_revealed . get ( did) < Some ( & index) {
@@ -201,9 +193,9 @@ impl<K> KeychainTxOutIndex<K> {
201193 pub fn new ( lookahead : u32 ) -> Self {
202194 Self {
203195 inner : SpkTxOutIndex :: default ( ) ,
204- keychains_to_descriptor_ids : Default :: default ( ) ,
205- descriptor_ids_to_descriptors : Default :: default ( ) ,
206- descriptor_ids_to_keychains : Default :: default ( ) ,
196+ keychain_to_descriptor_id : Default :: default ( ) ,
197+ descriptors : Default :: default ( ) ,
198+ descriptor_id_to_keychain : Default :: default ( ) ,
207199 last_revealed : Default :: default ( ) ,
208200 lookahead,
209201 }
@@ -346,14 +338,9 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
346338 & self ,
347339 ) -> impl DoubleEndedIterator < Item = ( & K , & Descriptor < DescriptorPublicKey > ) > + ExactSizeIterator + ' _
348340 {
349- self . keychains_to_descriptor_ids . iter ( ) . map ( |( k, did) | {
350- (
351- k,
352- self . descriptor_ids_to_descriptors
353- . get ( did)
354- . expect ( "invariant" ) ,
355- )
356- } )
341+ self . keychain_to_descriptor_id
342+ . iter ( )
343+ . map ( |( k, did) | ( k, self . descriptors . get ( did) . expect ( "invariant" ) ) )
357344 }
358345
359346 /// Insert a descriptor with a keychain associated to it.
@@ -371,25 +358,19 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
371358 ) -> Result < ChangeSet < K > , InsertDescriptorError < K > > {
372359 let mut changeset = ChangeSet :: < K > :: default ( ) ;
373360 let did = descriptor. descriptor_id ( ) ;
374- if !self . keychains_to_descriptor_ids . contains_key ( & keychain)
375- && !self . descriptor_ids_to_keychains . contains_key ( & did)
361+ if !self . keychain_to_descriptor_id . contains_key ( & keychain)
362+ && !self . descriptor_id_to_keychain . contains_key ( & did)
376363 {
377- self . descriptor_ids_to_descriptors
378- . insert ( did, descriptor. clone ( ) ) ;
379- self . keychains_to_descriptor_ids
380- . insert ( keychain. clone ( ) , did) ;
381- self . descriptor_ids_to_keychains
382- . insert ( did, keychain. clone ( ) ) ;
364+ self . descriptors . insert ( did, descriptor. clone ( ) ) ;
365+ self . keychain_to_descriptor_id . insert ( keychain. clone ( ) , did) ;
366+ self . descriptor_id_to_keychain . insert ( did, keychain. clone ( ) ) ;
383367 self . replenish_inner_index ( did, & keychain, self . lookahead ) ;
384368 changeset
385369 . keychains_added
386370 . insert ( keychain. clone ( ) , descriptor) ;
387371 } else {
388- if let Some ( existing_desc_id) = self . keychains_to_descriptor_ids . get ( & keychain) {
389- let descriptor = self
390- . descriptor_ids_to_descriptors
391- . get ( existing_desc_id)
392- . expect ( "invariant" ) ;
372+ if let Some ( existing_desc_id) = self . keychain_to_descriptor_id . get ( & keychain) {
373+ let descriptor = self . descriptors . get ( existing_desc_id) . expect ( "invariant" ) ;
393374 if * existing_desc_id != did {
394375 return Err ( InsertDescriptorError :: KeychainAlreadyAssigned {
395376 existing_assignment : descriptor. clone ( ) ,
@@ -398,12 +379,8 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
398379 }
399380 }
400381
401- if let Some ( existing_keychain) = self . descriptor_ids_to_keychains . get ( & did) {
402- let descriptor = self
403- . descriptor_ids_to_descriptors
404- . get ( & did)
405- . expect ( "invariant" )
406- . clone ( ) ;
382+ if let Some ( existing_keychain) = self . descriptor_id_to_keychain . get ( & did) {
383+ let descriptor = self . descriptors . get ( & did) . expect ( "invariant" ) . clone ( ) ;
407384
408385 if * existing_keychain != keychain {
409386 return Err ( InsertDescriptorError :: DescriptorAlreadyAssigned {
@@ -420,8 +397,8 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
420397 /// Gets the descriptor associated with the keychain. Returns `None` if the keychain doesn't
421398 /// have a descriptor associated with it.
422399 pub fn get_descriptor ( & self , keychain : & K ) -> Option < & Descriptor < DescriptorPublicKey > > {
423- let did = self . keychains_to_descriptor_ids . get ( keychain) ?;
424- self . descriptor_ids_to_descriptors . get ( did)
400+ let did = self . keychain_to_descriptor_id . get ( keychain) ?;
401+ self . descriptors . get ( did)
425402 }
426403
427404 /// Get the lookahead setting.
@@ -449,23 +426,20 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
449426 }
450427
451428 fn replenish_inner_index_did ( & mut self , did : DescriptorId , lookahead : u32 ) {
452- if let Some ( keychain) = self . descriptor_ids_to_keychains . get ( & did) . cloned ( ) {
429+ if let Some ( keychain) = self . descriptor_id_to_keychain . get ( & did) . cloned ( ) {
453430 self . replenish_inner_index ( did, & keychain, lookahead) ;
454431 }
455432 }
456433
457434 fn replenish_inner_index_keychain ( & mut self , keychain : & K , lookahead : u32 ) {
458- if let Some ( did) = self . keychains_to_descriptor_ids . get ( keychain) {
435+ if let Some ( did) = self . keychain_to_descriptor_id . get ( keychain) {
459436 self . replenish_inner_index ( * did, keychain, lookahead) ;
460437 }
461438 }
462439
463440 /// Syncs the state of the inner spk index after changes to a keychain
464441 fn replenish_inner_index ( & mut self , did : DescriptorId , keychain : & K , lookahead : u32 ) {
465- let descriptor = self
466- . descriptor_ids_to_descriptors
467- . get ( & did)
468- . expect ( "invariant" ) ;
442+ let descriptor = self . descriptors . get ( & did) . expect ( "invariant" ) ;
469443 let next_store_index = self
470444 . inner
471445 . all_spks ( )
@@ -497,17 +471,12 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
497471 pub fn all_unbounded_spk_iters (
498472 & self ,
499473 ) -> BTreeMap < K , SpkIterator < Descriptor < DescriptorPublicKey > > > {
500- self . keychains_to_descriptor_ids
474+ self . keychain_to_descriptor_id
501475 . iter ( )
502476 . map ( |( k, did) | {
503477 (
504478 k. clone ( ) ,
505- SpkIterator :: new (
506- self . descriptor_ids_to_descriptors
507- . get ( did)
508- . expect ( "invariant" )
509- . clone ( ) ,
510- ) ,
479+ SpkIterator :: new ( self . descriptors . get ( did) . expect ( "invariant" ) . clone ( ) ) ,
511480 )
512481 } )
513482 . collect ( )
@@ -521,7 +490,7 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
521490 let start = range. start_bound ( ) ;
522491 let end = range. end_bound ( ) ;
523492 let mut iter_last_revealed = self
524- . keychains_to_descriptor_ids
493+ . keychain_to_descriptor_id
525494 . range ( ( start, end) )
526495 . map ( |( k, did) | ( k, self . last_revealed . get ( did) . cloned ( ) ) ) ;
527496 let mut iter_spks = self
@@ -571,12 +540,10 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
571540 pub fn unused_spks (
572541 & self ,
573542 ) -> impl DoubleEndedIterator < Item = KeychainIndexed < K , & Script > > + Clone {
574- self . keychains_to_descriptor_ids
575- . keys ( )
576- . flat_map ( |keychain| {
577- self . unused_keychain_spks ( keychain)
578- . map ( |( i, spk) | ( ( keychain. clone ( ) , i) , spk) )
579- } )
543+ self . keychain_to_descriptor_id . keys ( ) . flat_map ( |keychain| {
544+ self . unused_keychain_spks ( keychain)
545+ . map ( |( i, spk) | ( ( keychain. clone ( ) , i) , spk) )
546+ } )
580547 }
581548
582549 /// Iterate over revealed, but unused, spks of the given `keychain`.
@@ -585,7 +552,7 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
585552 & self ,
586553 keychain : & K ,
587554 ) -> impl DoubleEndedIterator < Item = Indexed < & Script > > + Clone {
588- let end = match self . keychains_to_descriptor_ids . get ( keychain) {
555+ let end = match self . keychain_to_descriptor_id . get ( keychain) {
589556 Some ( did) => self . last_revealed . get ( did) . map ( |v| * v + 1 ) . unwrap_or ( 0 ) ,
590557 None => 0 ,
591558 } ;
@@ -608,12 +575,9 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
608575 ///
609576 /// Returns None if the provided `keychain` doesn't exist.
610577 pub fn next_index ( & self , keychain : & K ) -> Option < ( u32 , bool ) > {
611- let did = self . keychains_to_descriptor_ids . get ( keychain) ?;
578+ let did = self . keychain_to_descriptor_id . get ( keychain) ?;
612579 let last_index = self . last_revealed . get ( did) . cloned ( ) ;
613- let descriptor = self
614- . descriptor_ids_to_descriptors
615- . get ( did)
616- . expect ( "invariant" ) ;
580+ let descriptor = self . descriptors . get ( did) . expect ( "invariant" ) ;
617581
618582 // we can only get the next index if the wildcard exists.
619583 let has_wildcard = descriptor. has_wildcard ( ) ;
@@ -640,7 +604,7 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
640604 self . last_revealed
641605 . iter ( )
642606 . filter_map ( |( desc_id, index) | {
643- let keychain = self . descriptor_ids_to_keychains . get ( desc_id) ?;
607+ let keychain = self . descriptor_id_to_keychain . get ( desc_id) ?;
644608 Some ( ( keychain. clone ( ) , * index) )
645609 } )
646610 . collect ( )
@@ -649,7 +613,7 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
649613 /// Get the last derivation index revealed for `keychain`. Returns None if the keychain doesn't
650614 /// exist, or if the keychain doesn't have any revealed scripts.
651615 pub fn last_revealed_index ( & self , keychain : & K ) -> Option < u32 > {
652- let descriptor_id = self . keychains_to_descriptor_ids . get ( keychain) ?;
616+ let descriptor_id = self . keychain_to_descriptor_id . get ( keychain) ?;
653617 self . last_revealed . get ( descriptor_id) . cloned ( )
654618 }
655619
@@ -719,7 +683,7 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
719683 let mut changeset = ChangeSet :: default ( ) ;
720684
721685 if new {
722- let did = self . keychains_to_descriptor_ids . get ( keychain) ?;
686+ let did = self . keychain_to_descriptor_id . get ( keychain) ?;
723687 self . last_revealed . insert ( * did, next_index) ;
724688 changeset. last_revealed . insert ( * did, next_index) ;
725689 self . replenish_inner_index ( * did, keychain, self . lookahead ) ;
@@ -797,7 +761,7 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
797761 /// Returns the highest derivation index of each keychain that [`KeychainTxOutIndex`] has found
798762 /// a [`TxOut`] with it's script pubkey.
799763 pub fn last_used_indices ( & self ) -> BTreeMap < K , u32 > {
800- self . keychains_to_descriptor_ids
764+ self . keychain_to_descriptor_id
801765 . iter ( )
802766 . filter_map ( |( keychain, _) | {
803767 self . last_used_index ( keychain)
0 commit comments