electrum: common ancestor is before first changed block

This commit is contained in:
Michael Mallan 2024-09-08 17:12:36 +01:00
parent 51fef98b55
commit 86965c188e
No known key found for this signature in database
GPG Key ID: 5177CDCEDB0EABEB
2 changed files with 15 additions and 7 deletions

View File

@ -212,7 +212,12 @@ impl Electrum {
None
} else {
log::info!("Block chain reorganization detected.");
Some(self.bdk_wallet.find_block_at_or_before_height(height))
// We can assume height is positive as genesis block will not have changed.
Some(
self.bdk_wallet
.find_block_before_height(height)
.expect("height of first change is greater than 0"),
)
};
}
Some((_, None)) => continue,

View File

@ -295,17 +295,20 @@ impl BdkWallet {
})
}
/// Find the first block in the local chain whose height is less than or equal to this.
pub fn find_block_at_or_before_height(&self, height: u32) -> BlockChainTip {
/// Find the highest block in the local chain whose height is below `height`.
///
/// As the local chain will always contain the genesis block, this returns
/// `None` only if `height` is 0.
pub fn find_block_before_height(&self, height: u32) -> Option<BlockChainTip> {
for cp in self.local_chain.iter_checkpoints() {
if cp.height() <= height {
return BlockChainTip {
if cp.height() < height {
return Some(BlockChainTip {
height: height_i32_from_u32(cp.height()),
hash: cp.hash(),
};
});
}
}
unreachable!("There must be at least the genesis block.")
None
}
/// Apply an update to the local chain.