Merge bitcoindevkit/bdk#1145: fix(electrum): fixed chain sync issue

1010efd8d68e886cc46f0ac2f016630b670ea73c fix(electrum): fixed chain sync issue (Wei Chen)

Pull request description:

  ### Description

  This may or may not fix #1125.
  Fixed what appeared to be a logic error in `construct_update_tip` in `electrum_ext.rs` that caused the local chain tip to always be a block behind the newest confirmed block.

  ### Checklists

  #### All Submissions:

  * [x] I've signed all my commits
  * [x] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk/blob/master/CONTRIBUTING.md)
  * [x] I ran `cargo fmt` and `cargo clippy` before committing

  #### Bugfixes:

  * [ ] This pull request breaks the existing API
  * [x] I've added tests to reproduce the issue which are now passing
  * [x] I'm linking the issue being fixed by this PR

ACKs for top commit:
  danielabrozzoni:
    ACK 1010efd8d68e886cc46f0ac2f016630b670ea73c - although I've been able to reproduce the issue in #1125, I'm convinced that this PR fixes at least a bug, as demonstrated in #1171 (yet to be reviewed and merged).

Tree-SHA512: 92790e9072d17be74d2cd24bec3503e1ad5d97f728ee81490eeb09ac3f8d4a3a7e8d9628e943bc801246d5bfd345152c11d5dbe25246f5a57b3118727d3ae315
This commit is contained in:
Daniela Brozzoni 2023-11-13 09:35:45 +01:00
commit 855c61a6ab
No known key found for this signature in database
GPG Key ID: 7DE4F1FDCED0AB87

View File

@ -11,8 +11,8 @@ use std::{
str::FromStr, str::FromStr,
}; };
/// We assume that a block of this depth and deeper cannot be reorged. /// We include a chain suffix of a certain length for the purpose of robustness.
const ASSUME_FINAL_DEPTH: u32 = 8; const CHAIN_SUFFIX_LENGTH: u32 = 8;
/// Represents updates fetched from an Electrum server, but excludes full transactions. /// Represents updates fetched from an Electrum server, but excludes full transactions.
/// ///
@ -302,12 +302,12 @@ fn construct_update_tip(
} }
} }
// Atomically fetch the latest `ASSUME_FINAL_DEPTH` count of blocks from Electrum. We use this // Atomically fetch the latest `CHAIN_SUFFIX_LENGTH` count of blocks from Electrum. We use this
// to construct our checkpoint update. // to construct our checkpoint update.
let mut new_blocks = { let mut new_blocks = {
let start_height = new_tip_height.saturating_sub(ASSUME_FINAL_DEPTH); let start_height = new_tip_height.saturating_sub(CHAIN_SUFFIX_LENGTH - 1);
let hashes = client let hashes = client
.block_headers(start_height as _, ASSUME_FINAL_DEPTH as _)? .block_headers(start_height as _, CHAIN_SUFFIX_LENGTH as _)?
.headers .headers
.into_iter() .into_iter()
.map(|h| h.block_hash()); .map(|h| h.block_hash());