Skip to content

Commit 35538e2

Browse files
committed
refactor based on mammal comment
1 parent 481f92d commit 35538e2

2 files changed

Lines changed: 20 additions & 24 deletions

File tree

crates/esplora/src/async_ext.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,10 @@ 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 mut processed_any = false;
320+
let gap_limit = stop_gap.max(1);
319321

320322
loop {
321323
let handles = keychain_spks
@@ -348,15 +350,18 @@ where
348350
.collect::<FuturesOrdered<_>>();
349351

350352
if handles.is_empty() {
351-
if last_index.is_none() {
353+
if !processed_any {
352354
return Err(Box::new(esplora_client::Error::InvalidResponse));
353355
}
354356
break;
355357
}
356358

357359
for (index, txs, evicted) in handles.try_collect::<Vec<TxsOfSpkIndex>>().await? {
358-
last_index = Some(index);
359-
if !txs.is_empty() {
360+
processed_any = true;
361+
if txs.is_empty() {
362+
consecutive_unused = consecutive_unused.saturating_add(1);
363+
} else {
364+
consecutive_unused = 0;
360365
last_active_index = Some(index);
361366
}
362367
for tx in txs {
@@ -371,14 +376,7 @@ where
371376
.extend(evicted.into_iter().map(|txid| (txid, start_time)));
372377
}
373378

374-
let last_index =
375-
last_index.ok_or_else(|| Box::new(esplora_client::Error::InvalidResponse))?;
376-
let gap_limit_reached = if let Some(i) = last_active_index {
377-
last_index >= i.saturating_add(stop_gap as u32)
378-
} else {
379-
last_index + 1 >= stop_gap as u32
380-
};
381-
if gap_limit_reached {
379+
if consecutive_unused >= gap_limit {
382380
break;
383381
}
384382
}

crates/esplora/src/blocking_ext.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,10 @@ 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 mut processed_any = false;
288+
let gap_limit = stop_gap.max(1);
287289

288290
loop {
289291
let handles = keychain_spks
@@ -316,7 +318,7 @@ fn fetch_txs_with_keychain_spks<I: Iterator<Item = Indexed<SpkWithExpectedTxids>
316318
.collect::<Vec<JoinHandle<Result<TxsOfSpkIndex, Error>>>>();
317319

318320
if handles.is_empty() {
319-
if last_index.is_none() {
321+
if !processed_any {
320322
return Err(Box::new(esplora_client::Error::InvalidResponse));
321323
}
322324
break;
@@ -327,8 +329,11 @@ fn fetch_txs_with_keychain_spks<I: Iterator<Item = Indexed<SpkWithExpectedTxids>
327329
.join()
328330
.map_err(|_| Box::new(esplora_client::Error::InvalidResponse))?;
329331
let (index, txs, evicted) = handle_result?;
330-
last_index = Some(index);
331-
if !txs.is_empty() {
332+
processed_any = true;
333+
if txs.is_empty() {
334+
consecutive_unused = consecutive_unused.saturating_add(1);
335+
} else {
336+
consecutive_unused = 0;
332337
last_active_index = Some(index);
333338
}
334339
for tx in txs {
@@ -343,14 +348,7 @@ fn fetch_txs_with_keychain_spks<I: Iterator<Item = Indexed<SpkWithExpectedTxids>
343348
.extend(evicted.into_iter().map(|txid| (txid, start_time)));
344349
}
345350

346-
let last_index =
347-
last_index.ok_or_else(|| Box::new(esplora_client::Error::InvalidResponse))?;
348-
let gap_limit_reached = if let Some(i) = last_active_index {
349-
last_index >= i.saturating_add(stop_gap as u32)
350-
} else {
351-
last_index + 1 >= stop_gap as u32
352-
};
353-
if gap_limit_reached {
351+
if consecutive_unused >= gap_limit {
354352
break;
355353
}
356354
}

0 commit comments

Comments
 (0)