-
Notifications
You must be signed in to change notification settings - Fork 396
Refactor TestEnv::wait_until_electrum_sees_block
to be more concrete.
#1640
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -188,18 +188,43 @@ impl TestEnv { | |
Ok((bt.height as usize, block.block_hash())) | ||
} | ||
|
||
/// This method waits for the Electrum notification indicating that a new block has been mined. | ||
/// `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()?; | ||
/// Wait until Electrum is aware of a block of a given `block_height` (and optionally, matches | ||
/// the given `block_hash`). | ||
pub fn wait_until_electrum_sees_block( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is See: LagginTimes@1a055c7 for the tests that prompted this question. |
||
&self, | ||
block_height: usize, | ||
block_hash: Option<BlockHash>, | ||
timeout: Duration, | ||
) -> anyhow::Result<()> { | ||
self.electrsd.trigger()?; | ||
// NOTE: We use the subscribe endpoint because polling Electrs for a block header at | ||
// `block_height` and verifying the `block_hash` does not ensure that the spk histories | ||
// are current. In contrast, getting a notification for a new block tip ensures that the | ||
// confirmed spk histories are current, including the new notified tip. This is a result of | ||
// the internal workings of Electrs. | ||
self.electrum_client().block_headers_subscribe()?; | ||
|
||
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(()); | ||
self.electrum_client().ping()?; | ||
if let Some(header_notif) = self.electrum_client().block_headers_pop()? { | ||
if header_notif.height >= block_height { | ||
let header = if header_notif.height == block_height { | ||
header_notif.header | ||
} else { | ||
self.electrum_client().block_header(block_height)? | ||
}; | ||
match block_hash { | ||
None => return Ok(()), | ||
Some(exp_hash) => { | ||
if exp_hash == header.block_hash() { | ||
return Ok(()); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
std::thread::sleep(delay); | ||
|
@@ -210,6 +235,16 @@ impl TestEnv { | |
)) | ||
} | ||
|
||
/// Wait until Electrum is aware of bitcoind's chain tip. | ||
pub fn wait_until_electrum_tip_syncs_with_bitcoind( | ||
&self, | ||
timeout: Duration, | ||
) -> anyhow::Result<()> { | ||
let chain_height = self.rpc_client().get_block_count()?; | ||
let chain_hash = self.rpc_client().get_block_hash(chain_height)?; | ||
self.wait_until_electrum_sees_block(chain_height as _, Some(chain_hash), timeout) | ||
} | ||
|
||
/// 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( | ||
|
@@ -323,15 +358,15 @@ mod test { | |
|
||
// Mine some blocks. | ||
env.mine_blocks(101, None)?; | ||
env.wait_until_electrum_sees_block(Duration::from_secs(6))?; | ||
env.wait_until_electrum_tip_syncs_with_bitcoind(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(Duration::from_secs(6))?; | ||
env.wait_until_electrum_tip_syncs_with_bitcoind(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)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: This is now outdated since the test passes.