Skip to content

Commit

Permalink
feat(testenv): Added bdk_electrum wait for Txid method
Browse files Browse the repository at this point in the history
Added `wait_until_electrum_sees_txid` method to `TestEnv`. Both
`bdk_electrum` wait methods now have a `timeout` option.
  • Loading branch information
LagginTimes committed Aug 9, 2024
1 parent 88e9e76 commit 13ebe13
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions crates/testenv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,22 +168,42 @@ impl TestEnv {
}

/// This method waits for the Electrum notification indicating that a new block has been mined.
pub fn wait_until_electrum_sees_block(&self) -> anyhow::Result<()> {
/// `timeout` is the maximum [`Duration`] we want to wait for a response from Electrsd.
pub fn wait_until_electrum_sees_block(&self, timeout: Duration) -> anyhow::Result<()> {
self.electrsd.client.block_headers_subscribe()?;
let mut delay = Duration::from_millis(64);

loop {
let delay = Duration::from_millis(200);
let start = std::time::Instant::now();
while start.elapsed() < timeout {
self.electrsd.trigger()?;
self.electrsd.client.ping()?;
if self.electrsd.client.block_headers_pop()?.is_some() {
return Ok(());
}
std::thread::sleep(delay);
}
Err(anyhow::Error::msg(
"Timed out waiting for Electrsd to get block header",
))
}

if delay.as_millis() < 512 {
delay = delay.mul_f32(2.0);
/// This method waits for Electrsd to see a transaction with given `txid`. `timeout` is the
/// maximum [`Duration`] we want to wait for a response from Electrsd.
pub fn wait_until_electrum_sees_txid(
&self,
txid: Txid,
timeout: Duration,
) -> anyhow::Result<()> {
let delay = Duration::from_millis(200);
let start = std::time::Instant::now();
while start.elapsed() < timeout {
if self.electrsd.client.transaction_get(&txid).is_ok() {
return Ok(());
}
std::thread::sleep(delay);
}
Err(anyhow::Error::msg(
"Timed out waiting for Electrsd to get transaction",
))
}

/// Invalidate a number of blocks of a given size `count`.
Expand Down Expand Up @@ -266,6 +286,7 @@ impl TestEnv {
#[cfg(test)]
mod test {
use crate::TestEnv;
use core::time::Duration;
use electrsd::bitcoind::{anyhow::Result, bitcoincore_rpc::RpcApi};

/// This checks that reorgs initiated by `bitcoind` is detected by our `electrsd` instance.
Expand All @@ -275,15 +296,15 @@ mod test {

// Mine some blocks.
env.mine_blocks(101, None)?;
env.wait_until_electrum_sees_block()?;
env.wait_until_electrum_sees_block(Duration::from_secs(6))?;
let height = env.bitcoind.client.get_block_count()?;
let blocks = (0..=height)
.map(|i| env.bitcoind.client.get_block_hash(i))
.collect::<Result<Vec<_>, _>>()?;

// Perform reorg on six blocks.
env.reorg(6)?;
env.wait_until_electrum_sees_block()?;
env.wait_until_electrum_sees_block(Duration::from_secs(6))?;
let reorged_height = env.bitcoind.client.get_block_count()?;
let reorged_blocks = (0..=height)
.map(|i| env.bitcoind.client.get_block_hash(i))
Expand Down

0 comments on commit 13ebe13

Please sign in to comment.