From 87593befd4a85accbab7e7417b322ea74393b69d Mon Sep 17 00:00:00 2001 From: Michael Mallan Date: Tue, 10 Sep 2024 16:37:27 +0100 Subject: [PATCH] electrum: don't roll the tip forward upon reorg --- src/bitcoin/poller/looper.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/bitcoin/poller/looper.rs b/src/bitcoin/poller/looper.rs index 20cc1d1b..34f44181 100644 --- a/src/bitcoin/poller/looper.rs +++ b/src/bitcoin/poller/looper.rs @@ -275,8 +275,20 @@ fn updates( Ok(Some(reorg_common_ancestor)) => { // The block chain was reorganized. Rollback our state down to the common ancestor // between our former chain and the new one, then restart fresh. - db_conn.rollback_tip(&reorg_common_ancestor); - log::info!("Tip was rolled back to '{}'.", &reorg_common_ancestor); + // Make sure the common ancestor is not higher than the current DB tip, which could + // happen if a rescan has been detected and the DB tip rolled back accordingly. + if reorg_common_ancestor.height <= current_tip.height + // check hash in case height is the same + && reorg_common_ancestor.hash != current_tip.hash + { + db_conn.rollback_tip(&reorg_common_ancestor); + log::info!("Tip was rolled back to '{}'.", &reorg_common_ancestor); + } else { + log::info!( + "Tip was already earlier than common ancestor '{}'.", + &reorg_common_ancestor + ); + } return updates(db_conn, bit, descs, secp); } Err(e) => {