Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 17 additions & 18 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,32 +437,31 @@ pub(crate) fn wait_for_block<E: ElectrumApi>(electrs: &E, min_height: usize) {
}

pub(crate) fn wait_for_tx<E: ElectrumApi>(electrs: &E, txid: Txid) {
let mut tx_res = electrs.transaction_get(&txid);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's worth to keep the pattern to avoid the unnecessary ping if the result is immediately available.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. What is the ping for actually?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See https://docs.rs/electrum-client/latest/electrum_client/trait.ElectrumApi.html#tymethod.ping

ping might trigger electrs to process new data before we actually attempt the lookup.

loop {
if tx_res.is_ok() {
break;
}
tx_res = exponential_backoff_poll(|| {
electrs.ping().unwrap();
Some(electrs.transaction_get(&txid))
});
if electrs.transaction_get(&txid).is_ok() {
return;
}

exponential_backoff_poll(|| {
electrs.ping().unwrap();
electrs.transaction_get(&txid).ok()
});
}

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

is_spent = exponential_backoff_poll(|| {
electrs.ping().unwrap();
Some(!electrs.script_get_history(&txout_script).unwrap().is_empty())
});
let is_spent = !electrs.script_get_history(&txout_script).unwrap().is_empty();
if is_spent {
return;
}

exponential_backoff_poll(|| {
electrs.ping().unwrap();

let is_spent = !electrs.script_get_history(&txout_script).unwrap().is_empty();
is_spent.then_some(())
});
}

pub(crate) fn exponential_backoff_poll<T, F>(mut poll: F) -> T
Expand Down
Loading