@@ -26,7 +26,7 @@ use std::rc::Rc;
2626use std:: sync:: LazyLock ;
2727
2828use bpstd:: psbt:: Utxo ;
29- use bpstd:: { DescrId , Keychain , NormalIndex , Outpoint , Sats , Terminal , XpubDerivable } ;
29+ use bpstd:: { DescrId , Idx , Keychain , NormalIndex , Outpoint , Sats , Terminal , XpubDerivable } ;
3030use native_db:: transaction:: { RTransaction , RwTransaction } ;
3131use native_db:: { Builder , Database , Models , db_type} ;
3232use rgbp:: descriptors:: RgbDescr ;
@@ -129,6 +129,15 @@ impl DbUtxos {
129129 } )
130130 }
131131
132+ fn descr ( & self ) -> DescrModel {
133+ self . with_reader ( |tx| {
134+ Ok ( tx
135+ . get ( )
136+ . primary :: < DescrModel > ( self . id . 0 ) ?
137+ . expect ( "descriptor not found" ) )
138+ } )
139+ }
140+
132141 pub fn utxos ( & self ) -> HashSet < Utxo > { self . all ( ) . collect ( ) }
133142}
134143
@@ -194,9 +203,26 @@ impl UtxoSet for DbUtxos {
194203
195204 fn outpoints ( & self ) -> impl Iterator < Item = Outpoint > { self . all ( ) . map ( |utxo| utxo. outpoint ) }
196205
197- fn next_index_noshift ( & self , keychain : impl Into < Keychain > ) -> NormalIndex { todo ! ( ) }
206+ fn next_index_noshift ( & self , keychain : impl Into < Keychain > ) -> NormalIndex {
207+ self . descr ( ) . next_index ( keychain)
208+ }
198209
199- fn next_index ( & mut self , keychain : impl Into < Keychain > , _shift : bool ) -> NormalIndex { todo ! ( ) }
210+ fn next_index ( & mut self , keychain : impl Into < Keychain > , shift : bool ) -> NormalIndex {
211+ let keychain = keychain. into ( ) ;
212+ let mut descr = self . descr ( ) ;
213+ let next_index = descr. next_index ( keychain) ;
214+ if shift {
215+ descr
216+ . next_index
217+ . entry ( keychain)
218+ . and_modify ( |i| * i = next_index. saturating_inc ( ) ) ;
219+ self . with_writer ( |tx| {
220+ tx. upsert ( descr) ?;
221+ Ok ( ( ) )
222+ } ) ;
223+ }
224+ next_index
225+ }
200226}
201227
202228// We send it only once on `WriterService` construction, and then use it from a single thread
0 commit comments