@@ -282,8 +282,10 @@ fn fetch_txs_with_keychain_spks<I: Iterator<Item = Indexed<SpkWithExpectedTxids>
282282 type TxsOfSpkIndex = ( u32 , Vec < esplora_client:: Tx > , HashSet < Txid > ) ;
283283
284284 let mut update = TxUpdate :: < ConfirmationBlockTime > :: default ( ) ;
285- let mut last_index = Option :: < u32 > :: None ;
286285 let mut last_active_index = Option :: < u32 > :: None ;
286+ let mut consecutive_unused = 0usize ;
287+ let mut processed_any = false ;
288+ let gap_limit = stop_gap. max ( 1 ) ;
287289
288290 loop {
289291 let handles = keychain_spks
@@ -316,7 +318,7 @@ fn fetch_txs_with_keychain_spks<I: Iterator<Item = Indexed<SpkWithExpectedTxids>
316318 . collect :: < Vec < JoinHandle < Result < TxsOfSpkIndex , Error > > > > ( ) ;
317319
318320 if handles. is_empty ( ) {
319- if last_index . is_none ( ) {
321+ if !processed_any {
320322 return Err ( Box :: new ( esplora_client:: Error :: InvalidResponse ) ) ;
321323 }
322324 break ;
@@ -327,8 +329,11 @@ fn fetch_txs_with_keychain_spks<I: Iterator<Item = Indexed<SpkWithExpectedTxids>
327329 . join ( )
328330 . map_err ( |_| Box :: new ( esplora_client:: Error :: InvalidResponse ) ) ?;
329331 let ( index, txs, evicted) = handle_result?;
330- last_index = Some ( index) ;
331- if !txs. is_empty ( ) {
332+ processed_any = true ;
333+ if txs. is_empty ( ) {
334+ consecutive_unused = consecutive_unused. saturating_add ( 1 ) ;
335+ } else {
336+ consecutive_unused = 0 ;
332337 last_active_index = Some ( index) ;
333338 }
334339 for tx in txs {
@@ -343,14 +348,7 @@ fn fetch_txs_with_keychain_spks<I: Iterator<Item = Indexed<SpkWithExpectedTxids>
343348 . extend ( evicted. into_iter ( ) . map ( |txid| ( txid, start_time) ) ) ;
344349 }
345350
346- let last_index =
347- last_index. ok_or_else ( || Box :: new ( esplora_client:: Error :: InvalidResponse ) ) ?;
348- let gap_limit_reached = if let Some ( i) = last_active_index {
349- last_index >= i. saturating_add ( stop_gap as u32 )
350- } else {
351- last_index + 1 >= stop_gap as u32
352- } ;
353- if gap_limit_reached {
351+ if consecutive_unused >= gap_limit {
354352 break ;
355353 }
356354 }
0 commit comments