@@ -9,10 +9,7 @@ use bdk_core::{
99use esplora_client:: { OutputStatus , Tx } ;
1010use std:: thread:: JoinHandle ;
1111
12- use crate :: { insert_anchor_or_seen_at_from_status, insert_prevouts} ;
13-
14- /// [`esplora_client::Error`]
15- pub type Error = Box < esplora_client:: Error > ;
12+ use crate :: { insert_anchor_or_seen_at_from_status, insert_prevouts, Error } ;
1613
1714/// Trait to extend the functionality of [`esplora_client::BlockingClient`].
1815///
@@ -241,15 +238,13 @@ fn chain_update(
241238 let mut tip = match point_of_agreement {
242239 Some ( tip) => tip,
243240 None => {
244- return Err ( Box :: new ( esplora_client:: Error :: HeaderHashNotFound (
245- local_cp_hash,
246- ) ) ) ;
241+ return Err ( esplora_client:: Error :: HeaderHashNotFound ( local_cp_hash) . into ( ) ) ;
247242 }
248243 } ;
249244
250245 tip = tip
251246 . extend ( conflicts. into_iter ( ) . rev ( ) . map ( |b| ( b. height , b. hash ) ) )
252- . expect ( "evicted are in order" ) ;
247+ . map_err ( Error :: Checkpoint ) ? ;
253248
254249 for ( anchor, _) in anchors {
255250 let height = anchor. block_id . height ;
@@ -282,8 +277,9 @@ fn fetch_txs_with_keychain_spks<I: Iterator<Item = Indexed<SpkWithExpectedTxids>
282277 type TxsOfSpkIndex = ( u32 , Vec < esplora_client:: Tx > , HashSet < Txid > ) ;
283278
284279 let mut update = TxUpdate :: < ConfirmationBlockTime > :: default ( ) ;
285- let mut last_index = Option :: < u32 > :: None ;
286280 let mut last_active_index = Option :: < u32 > :: None ;
281+ let mut consecutive_unused = 0usize ;
282+ let gap_limit = stop_gap. max ( 1 ) ;
287283
288284 loop {
289285 let handles = keychain_spks
@@ -321,8 +317,10 @@ fn fetch_txs_with_keychain_spks<I: Iterator<Item = Indexed<SpkWithExpectedTxids>
321317
322318 for handle in handles {
323319 let ( index, txs, evicted) = handle. join ( ) . expect ( "thread must not panic" ) ?;
324- last_index = Some ( index) ;
325- if !txs. is_empty ( ) {
320+ if txs. is_empty ( ) {
321+ consecutive_unused = consecutive_unused. saturating_add ( 1 ) ;
322+ } else {
323+ consecutive_unused = 0 ;
326324 last_active_index = Some ( index) ;
327325 }
328326 for tx in txs {
@@ -337,13 +335,7 @@ fn fetch_txs_with_keychain_spks<I: Iterator<Item = Indexed<SpkWithExpectedTxids>
337335 . extend ( evicted. into_iter ( ) . map ( |txid| ( txid, start_time) ) ) ;
338336 }
339337
340- let last_index = last_index. expect ( "Must be set since handles wasn't empty." ) ;
341- let gap_limit_reached = if let Some ( i) = last_active_index {
342- last_index >= i. saturating_add ( stop_gap as u32 )
343- } else {
344- last_index + 1 >= stop_gap as u32
345- } ;
346- if gap_limit_reached {
338+ if consecutive_unused >= gap_limit {
347339 break ;
348340 }
349341 }
@@ -406,7 +398,7 @@ fn fetch_txs_with_txids<I: IntoIterator<Item = Txid>>(
406398 std:: thread:: spawn ( move || {
407399 client
408400 . get_tx_info ( & txid)
409- . map_err ( Box :: new )
401+ . map_err ( Error :: Client )
410402 . map ( |t| ( txid, t) )
411403 } )
412404 } )
@@ -468,7 +460,7 @@ fn fetch_txs_with_outpoints<I: IntoIterator<Item = OutPoint>>(
468460 std:: thread:: spawn ( move || {
469461 client
470462 . get_output_status ( & op. txid , op. vout as _ )
471- . map_err ( Box :: new )
463+ . map_err ( Error :: Client )
472464 } )
473465 } )
474466 . collect :: < Vec < JoinHandle < Result < Option < OutputStatus > , Error > > > > ( ) ;
@@ -512,6 +504,7 @@ fn fetch_txs_with_outpoints<I: IntoIterator<Item = OutPoint>>(
512504#[ cfg_attr( coverage_nightly, coverage( off) ) ]
513505mod test {
514506 use crate :: blocking_ext:: { chain_update, fetch_latest_blocks} ;
507+ use crate :: Error as EsploraError ;
515508 use bdk_chain:: bitcoin;
516509 use bdk_chain:: bitcoin:: hashes:: Hash ;
517510 use bdk_chain:: bitcoin:: Txid ;
@@ -562,7 +555,7 @@ mod test {
562555 let res = chain_update ( & client, & latest_blocks, & cp, & anchors) ;
563556 use esplora_client:: Error ;
564557 assert ! (
565- matches!( * res. unwrap_err( ) , Error :: HeaderHashNotFound ( hash) if hash == genesis_hash) ,
558+ matches!( res. unwrap_err( ) , EsploraError :: Client ( Error :: HeaderHashNotFound ( hash) ) if hash == genesis_hash) ,
566559 "`chain_update` should error if it can't connect to the local CP" ,
567560 ) ;
568561
0 commit comments