Skip to content

Commit

Permalink
fix: sort chaintips before comparing them
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
0xB10C committed Jan 24, 2025
1 parent f7ca6ec commit cfa09b3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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<HeaderInfo>, Vec<BlockHash>) =
match node
Expand Down
4 changes: 2 additions & 2 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit cfa09b3

Please sign in to comment.