From 0fdbe9d170941218dddd396c7204bfb8de035d43 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=E5=BF=97=E5=AE=87?= Date: Thu, 10 Jul 2025 03:04:24 +0000 Subject: [PATCH] fix(electrum): Return error on incorrect network --- crates/electrum/src/bdk_electrum_client.rs | 24 ++++++++-------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/crates/electrum/src/bdk_electrum_client.rs b/crates/electrum/src/bdk_electrum_client.rs index 6bbe2a65..672cf93b 100644 --- a/crates/electrum/src/bdk_electrum_client.rs +++ b/crates/electrum/src/bdk_electrum_client.rs @@ -647,25 +647,19 @@ fn fetch_tip_and_latest_blocks( } } agreement_cp + .ok_or_else(|| Error::Message("cannot find agreement block with server".to_string()))? }; - let agreement_height = agreement_cp.as_ref().map(CheckPoint::height); - - let new_tip = new_blocks + let extension = new_blocks .iter() - // Prune `new_blocks` to only include blocks that are actually new. - .filter(|(height, _)| Some(*<&u32>::clone(height)) > agreement_height) - .map(|(height, hash)| BlockId { - height: *height, - hash: *hash, - }) - .fold(agreement_cp, |prev_cp, block| { - Some(match prev_cp { - Some(cp) => cp.push(block).ok()?, - None => CheckPoint::new(block), - }) + .filter({ + let agreement_height = agreement_cp.height(); + move |(height, _)| **height > agreement_height }) - .ok_or_else(|| Error::Message("failed to construct new checkpoint tip".to_string()))?; + .map(|(&height, &hash)| BlockId { height, hash }); + let new_tip = agreement_cp + .extend(extension) + .expect("extension heights already checked to be greater than agreement height"); Ok((new_tip, new_blocks)) } -- 2.49.0