Skip to content

Commit 557a404

Browse files
committed
Harden generated block waits
Wait for bitcoind to reach the generated height before polling electrs for the corresponding block header. Co-Authored-By: HAL 9000
1 parent 7c8f154 commit 557a404

1 file changed

Lines changed: 36 additions & 18 deletions

File tree

tests/common/mod.rs

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,9 @@ pub(crate) async fn generate_blocks_and_wait<E: ElectrumApi>(
636636
let address = bitcoind.new_address().expect("failed to get new address");
637637
// TODO: expect this Result once the WouldBlock issue is resolved upstream.
638638
let _block_hashes_res = bitcoind.generate_to_address(num, &address);
639-
wait_for_block(electrs, cur_height as usize + num).await;
639+
let min_height = cur_height as usize + num;
640+
wait_for_bitcoind_block(bitcoind, min_height).await;
641+
wait_for_block(electrs, min_height).await;
640642
print!(" Done!");
641643
println!("\n");
642644
}
@@ -656,26 +658,42 @@ pub(crate) fn invalidate_blocks(bitcoind: &BitcoindClient, num_blocks: usize) {
656658
assert!(new_cur_height + num_blocks == cur_height);
657659
}
658660

661+
async fn wait_for_bitcoind_block(bitcoind: &BitcoindClient, min_height: usize) {
662+
let mut delay = Duration::from_millis(64);
663+
let mut tries = 0;
664+
loop {
665+
let height =
666+
bitcoind.get_blockchain_info().expect("failed to get blockchain info").blocks as usize;
667+
if height >= min_height {
668+
return;
669+
}
670+
assert!(
671+
tries < 120,
672+
"bitcoind height did not reach {} within 60 seconds, current height {}",
673+
min_height,
674+
height
675+
);
676+
tries += 1;
677+
tokio::time::sleep(delay).await;
678+
if delay.as_millis() < 512 {
679+
delay = delay.mul_f32(2.0);
680+
}
681+
}
682+
}
683+
659684
pub(crate) async fn wait_for_block<E: ElectrumApi>(electrs: &E, min_height: usize) {
660-
let mut header = match electrs.block_headers_subscribe() {
661-
Ok(header) => header,
662-
Err(_) => {
663-
// While subscribing should succeed the first time around, we ran into some cases where
664-
// it didn't. Since we can't proceed without subscribing, we try again after a delay
665-
// and panic if it still fails.
666-
tokio::time::sleep(Duration::from_secs(3)).await;
667-
electrs.block_headers_subscribe().expect("failed to subscribe to block headers")
668-
},
669-
};
685+
let mut delay = Duration::from_millis(64);
686+
let mut tries = 0;
670687
loop {
671-
if header.height >= min_height {
672-
break;
688+
if electrs.block_header(min_height).is_ok() {
689+
return;
690+
}
691+
assert!(tries < 120, "electrs did not serve block header {} within 60 seconds", min_height);
692+
tries += 1;
693+
tokio::time::sleep(delay).await;
694+
if delay.as_millis() < 512 {
695+
delay = delay.mul_f32(2.0);
673696
}
674-
header = exponential_backoff_poll(|| {
675-
electrs.ping().expect("failed to ping electrs");
676-
electrs.block_headers_pop().expect("failed to pop block header")
677-
})
678-
.await;
679697
}
680698
}
681699

0 commit comments

Comments
 (0)