Skip to content

Commit e140837

Browse files
committed
Fix wait_for_tx exponential backoff
Backoff wasn't actually working and polling would happen without any delay at all.
1 parent 61bed84 commit e140837

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
@@ -448,32 +448,31 @@ pub(crate) fn wait_for_block<E: ElectrumApi>(electrs: &E, min_height: usize) {
448448
}
449449

450450
pub(crate) fn wait_for_tx<E: ElectrumApi>(electrs: &E, txid: Txid) {
451-
let mut tx_res = electrs.transaction_get(&txid);
452-
loop {
453-
if tx_res.is_ok() {
454-
break;
455-
}
456-
tx_res = exponential_backoff_poll(|| {
457-
electrs.ping().unwrap();
458-
Some(electrs.transaction_get(&txid))
459-
});
451+
if electrs.transaction_get(&txid).is_ok() {
452+
return;
460453
}
454+
455+
exponential_backoff_poll(|| {
456+
electrs.ping().unwrap();
457+
electrs.transaction_get(&txid).ok()
458+
});
461459
}
462460

463461
pub(crate) fn wait_for_outpoint_spend<E: ElectrumApi>(electrs: &E, outpoint: OutPoint) {
464462
let tx = electrs.transaction_get(&outpoint.txid).unwrap();
465463
let txout_script = tx.output.get(outpoint.vout as usize).unwrap().clone().script_pubkey;
466-
let mut is_spent = !electrs.script_get_history(&txout_script).unwrap().is_empty();
467-
loop {
468-
if is_spent {
469-
break;
470-
}
471464

472-
is_spent = exponential_backoff_poll(|| {
473-
electrs.ping().unwrap();
474-
Some(!electrs.script_get_history(&txout_script).unwrap().is_empty())
475-
});
465+
let is_spent = !electrs.script_get_history(&txout_script).unwrap().is_empty();
466+
if is_spent {
467+
return;
476468
}
469+
470+
exponential_backoff_poll(|| {
471+
electrs.ping().unwrap();
472+
473+
let is_spent = !electrs.script_get_history(&txout_script).unwrap().is_empty();
474+
is_spent.then_some(())
475+
});
477476
}
478477

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

0 commit comments

Comments
 (0)