From d4244e315017dfe570220f34f75b34e60a7fa8dd Mon Sep 17 00:00:00 2001 From: Matthew Date: Mon, 29 Sep 2025 16:00:47 -0500 Subject: [PATCH] refactor(esplora): clear remaining panic paths --- crates/esplora/src/async_ext.rs | 24 ++++++++++++------------ crates/esplora/src/blocking_ext.rs | 17 +++++++---------- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/crates/esplora/src/async_ext.rs b/crates/esplora/src/async_ext.rs index c9cb17c1e..a450585c2 100644 --- a/crates/esplora/src/async_ext.rs +++ b/crates/esplora/src/async_ext.rs @@ -314,8 +314,9 @@ where type TxsOfSpkIndex = (u32, Vec, HashSet); let mut update = TxUpdate::::default(); - let mut last_index = Option::::None; let mut last_active_index = Option::::None; + let mut consecutive_unused = 0usize; + let gap_limit = stop_gap.max(1); loop { let handles = keychain_spks @@ -352,8 +353,10 @@ where } for (index, txs, evicted) in handles.try_collect::>().await? { - last_index = Some(index); - if !txs.is_empty() { + if txs.is_empty() { + consecutive_unused = consecutive_unused.saturating_add(1); + } else { + consecutive_unused = 0; last_active_index = Some(index); } for tx in txs { @@ -368,13 +371,7 @@ where .extend(evicted.into_iter().map(|txid| (txid, start_time))); } - let last_index = last_index.expect("Must be set since handles wasn't empty."); - let gap_limit_reached = if let Some(i) = last_active_index { - last_index >= i.saturating_add(stop_gap as u32) - } else { - last_index + 1 >= stop_gap as u32 - }; - if gap_limit_reached { + if consecutive_unused >= gap_limit { break; } } @@ -594,9 +591,12 @@ mod test { let anchors = BTreeSet::new(); let res = chain_update(&client, &latest_blocks, &cp, &anchors).await; - use esplora_client::Error; + use esplora_client::Error as ClientError; assert!( - matches!(*res.unwrap_err(), Error::HeaderHashNotFound(hash) if hash == genesis_hash), + matches!( + *res.unwrap_err(), + ClientError::HeaderHashNotFound(hash) if hash == genesis_hash + ), "`chain_update` should error if it can't connect to the local CP", ); diff --git a/crates/esplora/src/blocking_ext.rs b/crates/esplora/src/blocking_ext.rs index 5f8ab531c..2eeda0b25 100644 --- a/crates/esplora/src/blocking_ext.rs +++ b/crates/esplora/src/blocking_ext.rs @@ -282,8 +282,9 @@ fn fetch_txs_with_keychain_spks type TxsOfSpkIndex = (u32, Vec, HashSet); let mut update = TxUpdate::::default(); - let mut last_index = Option::::None; let mut last_active_index = Option::::None; + let mut consecutive_unused = 0usize; + let gap_limit = stop_gap.max(1); loop { let handles = keychain_spks @@ -321,8 +322,10 @@ fn fetch_txs_with_keychain_spks for handle in handles { let (index, txs, evicted) = handle.join().expect("thread must not panic")?; - last_index = Some(index); - if !txs.is_empty() { + if txs.is_empty() { + consecutive_unused = consecutive_unused.saturating_add(1); + } else { + consecutive_unused = 0; last_active_index = Some(index); } for tx in txs { @@ -337,13 +340,7 @@ fn fetch_txs_with_keychain_spks .extend(evicted.into_iter().map(|txid| (txid, start_time))); } - let last_index = last_index.expect("Must be set since handles wasn't empty."); - let gap_limit_reached = if let Some(i) = last_active_index { - last_index >= i.saturating_add(stop_gap as u32) - } else { - last_index + 1 >= stop_gap as u32 - }; - if gap_limit_reached { + if consecutive_unused >= gap_limit { break; } }