-
Notifications
You must be signed in to change notification settings - Fork 393
esplora: chain_update
errors if no point of connection
#1971
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
ValuedMammal
wants to merge
3
commits into
bitcoindevkit:master
Choose a base branch
from
ValuedMammal:fix/esplora-chain-update
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
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
|
@@ -205,12 +205,16 @@ async fn fetch_block<S: Sleeper>( | |
|
||
// We avoid fetching blocks higher than previously fetched `latest_blocks` as the local chain | ||
// tip is used to signal for the last-synced-up-to-height. | ||
let &tip_height = latest_blocks | ||
.keys() | ||
.last() | ||
.expect("must have atleast one entry"); | ||
if height > tip_height { | ||
return Ok(None); | ||
match latest_blocks.keys().last().copied() { | ||
None => { | ||
debug_assert!(false, "`latest_blocks` should not be empty"); | ||
return Ok(None); | ||
} | ||
Some(tip_height) => { | ||
if height > tip_height { | ||
return Ok(None); | ||
} | ||
} | ||
} | ||
|
||
Ok(Some(client.get_block_hash(height).await?)) | ||
|
@@ -227,27 +231,36 @@ async fn chain_update<S: Sleeper>( | |
anchors: &BTreeSet<(ConfirmationBlockTime, Txid)>, | ||
) -> Result<CheckPoint, Error> { | ||
let mut point_of_agreement = None; | ||
let mut local_cp_hash = local_tip.hash(); | ||
let mut conflicts = vec![]; | ||
|
||
for local_cp in local_tip.iter() { | ||
let remote_hash = match fetch_block(client, latest_blocks, local_cp.height()).await? { | ||
Some(hash) => hash, | ||
None => continue, | ||
}; | ||
if remote_hash == local_cp.hash() { | ||
point_of_agreement = Some(local_cp.clone()); | ||
point_of_agreement = Some(local_cp); | ||
break; | ||
} else { | ||
// it is not strictly necessary to include all the conflicted heights (we do need the | ||
// first one) but it seems prudent to make sure the updated chain's heights are a | ||
// superset of the existing chain after update. | ||
conflicts.push(BlockId { | ||
height: local_cp.height(), | ||
hash: remote_hash, | ||
}); | ||
} | ||
local_cp_hash = local_cp.hash(); | ||
// It is not strictly necessary to include all the conflicted heights (we do need the | ||
// first one) but it seems prudent to make sure the updated chain's heights are a | ||
// superset of the existing chain after update. | ||
conflicts.push(BlockId { | ||
height: local_cp.height(), | ||
hash: remote_hash, | ||
}); | ||
} | ||
|
||
let mut tip = point_of_agreement.expect("remote esplora should have same genesis block"); | ||
let mut tip = match point_of_agreement { | ||
Some(tip) => tip, | ||
None => { | ||
return Err(Box::new(esplora_client::Error::HeaderHashNotFound( | ||
local_cp_hash, | ||
))); | ||
} | ||
}; | ||
|
||
tip = tip | ||
.extend(conflicts.into_iter().rev()) | ||
|
@@ -545,7 +558,7 @@ mod test { | |
local_chain::LocalChain, | ||
BlockId, | ||
}; | ||
use bdk_core::ConfirmationBlockTime; | ||
use bdk_core::{bitcoin, ConfirmationBlockTime}; | ||
use bdk_testenv::{anyhow, bitcoincore_rpc::RpcApi, TestEnv}; | ||
use esplora_client::Builder; | ||
|
||
|
@@ -557,6 +570,41 @@ mod test { | |
}}; | ||
} | ||
|
||
// Test that `chain_update` fails due to wrong network. | ||
#[tokio::test] | ||
async fn test_chain_update_wrong_network_error() -> anyhow::Result<()> { | ||
let env = TestEnv::new()?; | ||
let base_url = format!("http://{}", &env.electrsd.esplora_url.clone().unwrap()); | ||
let client = Builder::new(base_url.as_str()).build_async()?; | ||
let initial_height = env.rpc_client().get_block_count()? as u32; | ||
|
||
let mine_to = 16; | ||
let _ = env.mine_blocks((mine_to - initial_height) as usize, None)?; | ||
while client.get_height().await? < mine_to { | ||
std::thread::sleep(Duration::from_millis(64)); | ||
} | ||
Comment on lines
+583
to
+585
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. nit: FWIW, you can use |
||
let latest_blocks = fetch_latest_blocks(&client).await?; | ||
assert!(!latest_blocks.is_empty()); | ||
assert_eq!(latest_blocks.keys().last(), Some(&mine_to)); | ||
|
||
let genesis_hash = | ||
bitcoin::constants::genesis_block(bitcoin::Network::Testnet4).block_hash(); | ||
let cp = bdk_chain::CheckPoint::new(BlockId { | ||
height: 0, | ||
hash: genesis_hash, | ||
}); | ||
|
||
let anchors = BTreeSet::new(); | ||
let res = chain_update(&client, &latest_blocks, &cp, &anchors).await; | ||
use esplora_client::Error; | ||
assert!( | ||
matches!(*res.unwrap_err(), Error::HeaderHashNotFound(hash) if hash == genesis_hash), | ||
"`chain_update` should error if it can't connect to the local CP", | ||
); | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Ensure that update does not remove heights (from original), and all anchor heights are | ||
/// included. | ||
#[tokio::test] | ||
|
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
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.
Uh oh!
There was an error while loading. Please reload this page.