Skip to content

Commit 3aef3b4

Browse files
committed
fix: remove panic from stop gap scan loop
1 parent be356d5 commit 3aef3b4

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

crates/esplora/src/async_ext.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,11 @@ 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+
// Use consecutive_unused so unused count drives stop gap.
319+
let mut consecutive_unused = 0usize;
320+
// Treat stop_gap = 0 as 1 while preserving original semantics for other values.
321+
let gap_limit = stop_gap.max(1);
319322

320323
loop {
321324
let handles = keychain_spks
@@ -352,8 +355,10 @@ where
352355
}
353356

354357
for (index, txs, evicted) in handles.try_collect::<Vec<TxsOfSpkIndex>>().await? {
355-
last_index = Some(index);
356-
if !txs.is_empty() {
358+
if txs.is_empty() {
359+
consecutive_unused = consecutive_unused.saturating_add(1);
360+
} else {
361+
consecutive_unused = 0;
357362
last_active_index = Some(index);
358363
}
359364
for tx in txs {
@@ -368,13 +373,7 @@ where
368373
.extend(evicted.into_iter().map(|txid| (txid, start_time)));
369374
}
370375

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 {
376+
if consecutive_unused >= gap_limit {
378377
break;
379378
}
380379
}
@@ -596,7 +595,10 @@ mod test {
596595
let res = chain_update(&client, &latest_blocks, &cp, &anchors).await;
597596
use esplora_client::Error;
598597
assert!(
599-
matches!(*res.unwrap_err(), Error::HeaderHashNotFound(hash) if hash == genesis_hash),
598+
matches!(
599+
*res.unwrap_err(),
600+
Error::HeaderHashNotFound(hash) if hash == genesis_hash
601+
),
600602
"`chain_update` should error if it can't connect to the local CP",
601603
);
602604

crates/esplora/src/blocking_ext.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,11 @@ 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+
// Use consecutive_unused so unused count drives stop gap.
287+
let mut consecutive_unused = 0usize;
288+
// Treat stop_gap = 0 as 1 while preserving original semantics for other values.
289+
let gap_limit = stop_gap.max(1);
287290

288291
loop {
289292
let handles = keychain_spks
@@ -321,8 +324,10 @@ fn fetch_txs_with_keychain_spks<I: Iterator<Item = Indexed<SpkWithExpectedTxids>
321324

322325
for handle in handles {
323326
let (index, txs, evicted) = handle.join().expect("thread must not panic")?;
324-
last_index = Some(index);
325-
if !txs.is_empty() {
327+
if txs.is_empty() {
328+
consecutive_unused = consecutive_unused.saturating_add(1);
329+
} else {
330+
consecutive_unused = 0;
326331
last_active_index = Some(index);
327332
}
328333
for tx in txs {
@@ -337,13 +342,7 @@ fn fetch_txs_with_keychain_spks<I: Iterator<Item = Indexed<SpkWithExpectedTxids>
337342
.extend(evicted.into_iter().map(|txid| (txid, start_time)));
338343
}
339344

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 {
345+
if consecutive_unused >= gap_limit {
347346
break;
348347
}
349348
}

0 commit comments

Comments
 (0)