Skip to content

Commit

Permalink
electrum: check for reorg during rescan
Browse files Browse the repository at this point in the history
  • Loading branch information
jp1ac4 committed Sep 6, 2024
1 parent b9aaea1 commit 1bbfd2e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
52 changes: 33 additions & 19 deletions src/bitcoin/electrum/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use bdk_electrum::bdk_chain::{
spk_client::{FullScanRequest, SyncRequest},
ChainPosition,
};
use utils::tip_from_block_id;

pub mod client;
mod utils;
Expand Down Expand Up @@ -136,7 +137,8 @@ impl Electrum {
const FETCH_PREV_TXOUTS: bool = false;
const STOP_GAP: usize = 50;

let (chain_update, mut graph_update, keychain_update) = if !self.is_rescanning() {
let is_rescanning = self.is_rescanning();
let (chain_update, mut graph_update, keychain_update) = if !is_rescanning {
log::info!("Performing sync.");
let mut request = SyncRequest::from_chain_tip(local_chain_tip.clone())
.cache_graph_txs(self.bdk_wallet.graph());
Expand Down Expand Up @@ -197,25 +199,37 @@ impl Electrum {
if let Some(keychain_update) = keychain_update {
self.bdk_wallet.apply_keychain_update(keychain_update);
}
let changeset = self.bdk_wallet.apply_connected_chain_update(chain_update);

let mut changes_iter = changeset.into_iter();
let reorg_common_ancestor = loop {
match changes_iter.next() {
Some((height, Some(_))) => {
// `BlockHash` being `Some(_)` means a checkpoint at this height was added to the chain.
// Since we iterate in ascending height order, we'll see the lowest block height first.
// If the lowest height it adds is higher than our height before syncing, we're good.
// Else if it's adding a block at height before syncing or lower, it's a reorg.
break if height > local_chain_tip.height() {
None
} else {
log::info!("Block chain reorganization detected.");
Some(self.bdk_wallet.find_block_at_or_before_height(height))
};

let reorg_common_ancestor = if !is_rescanning {
let changeset = self.bdk_wallet.apply_connected_chain_update(chain_update);
let mut changes_iter = changeset.into_iter();
loop {
match changes_iter.next() {
Some((height, Some(_))) => {
// `BlockHash` being `Some(_)` means a checkpoint at this height was added to the chain.
// Since we iterate in ascending height order, we'll see the lowest block height first.
// If the lowest height it adds is higher than our height before syncing, we're good.
// Else if it's adding a block at height before syncing or lower, it's a reorg.
break if height > local_chain_tip.height() {
None
} else {
log::info!("Block chain reorganization detected.");
Some(self.bdk_wallet.find_block_at_or_before_height(height))
};
}
Some((_, None)) => continue,
None => break None,
}
Some((_, None)) => continue,
None => break None,
}
} else {
// If there was a rescan and the new height is lower, we need to indicate the reorg.
// There might still have been a reorg in other cases, but they will be picked up
// by rescan logic.
self.bdk_wallet.replace_chain_from_tip(chain_update);
if self.local_chain().tip().height() < local_chain_tip.height() {
Some(tip_from_block_id(self.local_chain().tip().block_id()))
} else {
None
}
};

Expand Down
6 changes: 6 additions & 0 deletions src/bitcoin/electrum/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,12 @@ impl BdkWallet {
unreachable!("There must be at least the genesis block.")
}

/// Replace the local chain with a new one from the given tip.
/// Panics if tip does not contain genesis block.
pub fn replace_chain_from_tip(&mut self, tip: CheckPoint) {
self.local_chain = LocalChain::from_tip(tip).expect("must contain genesis block");
}

/// Apply an update to the local chain.
/// Panics if update does not connect to the local chain.
pub fn apply_connected_chain_update(&mut self, chain_update: CheckPoint) -> ChainChangeSet {
Expand Down

0 comments on commit 1bbfd2e

Please sign in to comment.