diff --git a/src/bitcoin/electrum/mod.rs b/src/bitcoin/electrum/mod.rs index 8b9745c76..4cfe8441f 100644 --- a/src/bitcoin/electrum/mod.rs +++ b/src/bitcoin/electrum/mod.rs @@ -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; @@ -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()); @@ -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 } }; diff --git a/src/bitcoin/electrum/wallet.rs b/src/bitcoin/electrum/wallet.rs index 2e467f214..cdb51719b 100644 --- a/src/bitcoin/electrum/wallet.rs +++ b/src/bitcoin/electrum/wallet.rs @@ -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 {