Skip to content

Commit 1d05ee7

Browse files
committed
fix(esplora): error if keychain iteration yields no indices
1 parent acbd864 commit 1d05ee7

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

crates/esplora/src/async_ext.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,9 @@ where
348348
.collect::<FuturesOrdered<_>>();
349349

350350
if handles.is_empty() {
351+
if last_index.is_none() {
352+
return Err(Box::new(esplora_client::Error::InvalidResponse));
353+
}
351354
break;
352355
}
353356

@@ -368,7 +371,8 @@ 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.");
374+
let last_index =
375+
last_index.ok_or_else(|| Box::new(esplora_client::Error::InvalidResponse))?;
372376
let gap_limit_reached = if let Some(i) = last_active_index {
373377
last_index >= i.saturating_add(stop_gap as u32)
374378
} else {
@@ -571,6 +575,15 @@ mod test {
571575
}};
572576
}
573577

578+
#[test]
579+
fn ensure_last_index_none_returns_error() {
580+
let last_index: Option<u32> = None;
581+
let err = last_index
582+
.ok_or_else(|| Box::new(esplora_client::Error::InvalidResponse))
583+
.unwrap_err();
584+
assert!(matches!(*err, esplora_client::Error::InvalidResponse));
585+
}
586+
574587
// Test that `chain_update` fails due to wrong network.
575588
#[tokio::test]
576589
async fn test_chain_update_wrong_network_error() -> anyhow::Result<()> {

crates/esplora/src/blocking_ext.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,8 @@ fn fetch_txs_with_keychain_spks<I: Iterator<Item = Indexed<SpkWithExpectedTxids>
343343
.extend(evicted.into_iter().map(|txid| (txid, start_time)));
344344
}
345345

346-
let last_index = last_index.expect("Must be set since handles wasn't empty.");
346+
let last_index =
347+
last_index.ok_or_else(|| Box::new(esplora_client::Error::InvalidResponse))?;
347348
let gap_limit_reached = if let Some(i) = last_active_index {
348349
last_index >= i.saturating_add(stop_gap as u32)
349350
} else {
@@ -561,6 +562,15 @@ mod test {
561562
));
562563
}
563564

565+
#[test]
566+
fn ensure_last_index_none_returns_error() {
567+
let last_index: Option<u32> = None;
568+
let err = last_index
569+
.ok_or_else(|| Box::new(esplora_client::Error::InvalidResponse))
570+
.unwrap_err();
571+
assert!(matches!(*err, esplora_client::Error::InvalidResponse));
572+
}
573+
564574
macro_rules! local_chain {
565575
[ $(($height:expr, $block_hash:expr)), * ] => {{
566576
#[allow(unused_mut)]

0 commit comments

Comments
 (0)