Skip to content

Commit 65945f8

Browse files
authored
Merge pull request #641 from joostjager/backoff-fix
Fix wait_for_tx/outpoint exponential backoff
2 parents 569e910 + 3df1477 commit 65945f8

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

tests/common/mod.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -437,32 +437,31 @@ pub(crate) fn wait_for_block<E: ElectrumApi>(electrs: &E, min_height: usize) {
437437
}
438438

439439
pub(crate) fn wait_for_tx<E: ElectrumApi>(electrs: &E, txid: Txid) {
440-
let mut tx_res = electrs.transaction_get(&txid);
441-
loop {
442-
if tx_res.is_ok() {
443-
break;
444-
}
445-
tx_res = exponential_backoff_poll(|| {
446-
electrs.ping().unwrap();
447-
Some(electrs.transaction_get(&txid))
448-
});
440+
if electrs.transaction_get(&txid).is_ok() {
441+
return;
449442
}
443+
444+
exponential_backoff_poll(|| {
445+
electrs.ping().unwrap();
446+
electrs.transaction_get(&txid).ok()
447+
});
450448
}
451449

452450
pub(crate) fn wait_for_outpoint_spend<E: ElectrumApi>(electrs: &E, outpoint: OutPoint) {
453451
let tx = electrs.transaction_get(&outpoint.txid).unwrap();
454452
let txout_script = tx.output.get(outpoint.vout as usize).unwrap().clone().script_pubkey;
455-
let mut is_spent = !electrs.script_get_history(&txout_script).unwrap().is_empty();
456-
loop {
457-
if is_spent {
458-
break;
459-
}
460453

461-
is_spent = exponential_backoff_poll(|| {
462-
electrs.ping().unwrap();
463-
Some(!electrs.script_get_history(&txout_script).unwrap().is_empty())
464-
});
454+
let is_spent = !electrs.script_get_history(&txout_script).unwrap().is_empty();
455+
if is_spent {
456+
return;
465457
}
458+
459+
exponential_backoff_poll(|| {
460+
electrs.ping().unwrap();
461+
462+
let is_spent = !electrs.script_get_history(&txout_script).unwrap().is_empty();
463+
is_spent.then_some(())
464+
});
466465
}
467466

468467
pub(crate) fn exponential_backoff_poll<T, F>(mut poll: F) -> T

0 commit comments

Comments
 (0)