-
Notifications
You must be signed in to change notification settings - Fork 329
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added
electrum
and esplora
support to TestEnv
`TestEnv` now supports both `electrum` and `esplora`. `esplora` tests have also been updated to use `TestEnv`.
- Loading branch information
1 parent
0be5338
commit c45a8f5
Showing
9 changed files
with
221 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use anyhow::Result; | ||
use electrsd::bitcoind::bitcoincore_rpc::RpcApi; | ||
use electrum_client::ElectrumApi; | ||
use std::time::Duration; | ||
use testenv::TestEnv; | ||
|
||
fn wait_for_block(env: &TestEnv, client: &electrum_client::Client, min_height: usize) { | ||
let mut header = client.block_headers_subscribe().unwrap(); | ||
loop { | ||
if header.height >= min_height { | ||
break; | ||
} | ||
header = exponential_backoff_poll(|| { | ||
env.electrsd.trigger().unwrap(); | ||
client.ping().unwrap(); | ||
client.block_headers_pop().unwrap() | ||
}); | ||
} | ||
} | ||
|
||
fn exponential_backoff_poll<T, F>(mut poll: F) -> T | ||
where | ||
F: FnMut() -> Option<T>, | ||
{ | ||
let mut delay = Duration::from_millis(64); | ||
loop { | ||
match poll() { | ||
Some(data) => break data, | ||
None if delay.as_millis() < 512 => delay = delay.mul_f32(2.0), | ||
None => {} | ||
} | ||
|
||
std::thread::sleep(delay); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_reorg_is_detected_in_electrsd() -> Result<()> { | ||
let env = TestEnv::new()?; | ||
let client = electrum_client::Client::new(env.electrsd.electrum_url.as_str())?; | ||
|
||
// Mine some blocks. | ||
env.mine_blocks(101, None)?; | ||
let height = env.bitcoind.client.get_block_count()?; | ||
wait_for_block(&env, &client, height as usize); | ||
let blocks = (0..=height) | ||
.map(|i| env.bitcoind.client.get_block_hash(i)) | ||
.collect::<Result<Vec<_>, _>>()?; | ||
|
||
// Perform reorg on six blocks. | ||
env.reorg(6)?; | ||
let reorged_height = env.bitcoind.client.get_block_count()?; | ||
wait_for_block(&env, &client, reorged_height as usize); | ||
let reorged_blocks = (0..=height) | ||
.map(|i| env.bitcoind.client.get_block_hash(i)) | ||
.collect::<Result<Vec<_>, _>>()?; | ||
|
||
assert_eq!(height, reorged_height); | ||
|
||
// Block hashes should not be equal on reorged blocks only. | ||
for (i, (block, reorged_block)) in blocks.iter().zip(reorged_blocks.iter()).enumerate() { | ||
match i <= height as usize - 6 { | ||
true => assert_eq!(block, reorged_block), | ||
false => assert_ne!(block, reorged_block), | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.