-
Notifications
You must be signed in to change notification settings - Fork 390
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
Open
evanlinjin
wants to merge
1
commit into
bitcoindevkit:master
Choose a base branch
from
evanlinjin:fix/wait_until_electrum_sees_block
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -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 |
||
&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)) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.