From cfa09b32101933f56fed602d47b816b391456919 Mon Sep 17 00:00:00 2001 From: 0xb10c Date: Fri, 24 Jan 2025 11:21:52 +0100 Subject: [PATCH] fix: sort chaintips before comparing them btcd would return the same chaintips in a different order, which caused us to think they aren't the same as the previous, Sort them to be sure they can be compared. --- src/main.rs | 7 ++++++- src/types.rs | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 36611c8..0a0004a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -189,7 +189,7 @@ async fn main() -> Result<(), MainError> { // are using 'continue' on errors. If we would wait at the end, // we might skip the waiting. interval.tick().await; - let tips = match node.tips().await { + let mut tips = match node.tips().await { Ok(tips) => { if !is_node_reachable(&caches_clone, network.id, node.info().id).await { update_cache( @@ -229,6 +229,11 @@ async fn main() -> Result<(), MainError> { } }; + // For example, btcd doesn't gurantee the order of the chain + // tips returned. This means, while they are equal, the order + // can differ and we will treat them as unequal. + tips.sort(); + if last_tips != tips { let (new_headers, miners_needed): (Vec, Vec) = match node diff --git a/src/types.rs b/src/types.rs index 33a4795..59b1a41 100644 --- a/src/types.rs +++ b/src/types.rs @@ -207,7 +207,7 @@ pub struct DataChanged { pub network_id: u32, } -#[derive(Deserialize, Clone, Debug, PartialEq, Eq)] +#[derive(Deserialize, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] pub enum ChainTipStatus { #[serde(rename = "active")] Active, @@ -260,7 +260,7 @@ impl fmt::Display for ChainTipStatus { } } -#[derive(Deserialize, Debug, Clone, PartialEq, Eq)] +#[derive(Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] pub struct ChainTip { pub height: u64, pub hash: String,