Skip to content

Commit a5bf97f

Browse files
committed
refactor(esplora): clear remaining panic paths
1 parent faf520d commit a5bf97f

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

crates/esplora/src/async_ext.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,9 @@ where
314314
type TxsOfSpkIndex = (u32, Vec<esplora_client::Tx>, HashSet<Txid>);
315315

316316
let mut update = TxUpdate::<ConfirmationBlockTime>::default();
317-
let mut last_index = Option::<u32>::None;
318317
let mut last_active_index = Option::<u32>::None;
318+
let mut consecutive_unused = 0usize;
319+
let gap_limit = stop_gap.max(1);
319320

320321
loop {
321322
let handles = keychain_spks
@@ -352,8 +353,10 @@ where
352353
}
353354

354355
for (index, txs, evicted) in handles.try_collect::<Vec<TxsOfSpkIndex>>().await? {
355-
last_index = Some(index);
356-
if !txs.is_empty() {
356+
if txs.is_empty() {
357+
consecutive_unused = consecutive_unused.saturating_add(1);
358+
} else {
359+
consecutive_unused = 0;
357360
last_active_index = Some(index);
358361
}
359362
for tx in txs {
@@ -368,13 +371,7 @@ where
368371
.extend(evicted.into_iter().map(|txid| (txid, start_time)));
369372
}
370373

371-
let last_index = last_index.expect("Must be set since handles wasn't empty.");
372-
let gap_limit_reached = if let Some(i) = last_active_index {
373-
last_index >= i.saturating_add(stop_gap as u32)
374-
} else {
375-
last_index + 1 >= stop_gap as u32
376-
};
377-
if gap_limit_reached {
374+
if consecutive_unused >= gap_limit {
378375
break;
379376
}
380377
}
@@ -594,9 +591,12 @@ mod test {
594591

595592
let anchors = BTreeSet::new();
596593
let res = chain_update(&client, &latest_blocks, &cp, &anchors).await;
597-
use esplora_client::Error;
594+
use esplora_client::Error as ClientError;
598595
assert!(
599-
matches!(*res.unwrap_err(), Error::HeaderHashNotFound(hash) if hash == genesis_hash),
596+
matches!(
597+
*res.unwrap_err(),
598+
ClientError::HeaderHashNotFound(hash) if hash == genesis_hash
599+
),
600600
"`chain_update` should error if it can't connect to the local CP",
601601
);
602602

crates/esplora/src/blocking_ext.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,9 @@ 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 gap_limit = stop_gap.max(1);
287288

288289
loop {
289290
let handles = keychain_spks
@@ -321,8 +322,10 @@ fn fetch_txs_with_keychain_spks<I: Iterator<Item = Indexed<SpkWithExpectedTxids>
321322

322323
for handle in handles {
323324
let (index, txs, evicted) = handle.join().expect("thread must not panic")?;
324-
last_index = Some(index);
325-
if !txs.is_empty() {
325+
if txs.is_empty() {
326+
consecutive_unused = consecutive_unused.saturating_add(1);
327+
} else {
328+
consecutive_unused = 0;
326329
last_active_index = Some(index);
327330
}
328331
for tx in txs {
@@ -337,13 +340,7 @@ fn fetch_txs_with_keychain_spks<I: Iterator<Item = Indexed<SpkWithExpectedTxids>
337340
.extend(evicted.into_iter().map(|txid| (txid, start_time)));
338341
}
339342

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 {
343+
if consecutive_unused >= gap_limit {
347344
break;
348345
}
349346
}
@@ -562,7 +559,10 @@ mod test {
562559
let res = chain_update(&client, &latest_blocks, &cp, &anchors);
563560
use esplora_client::Error;
564561
assert!(
565-
matches!(*res.unwrap_err(), Error::HeaderHashNotFound(hash) if hash == genesis_hash),
562+
matches!(
563+
*res.unwrap_err(),
564+
Error::HeaderHashNotFound(hash) if hash == genesis_hash
565+
),
566566
"`chain_update` should error if it can't connect to the local CP",
567567
);
568568

0 commit comments

Comments
 (0)