From 60bce0e0a673df29b4aabc8ee8399b0a521dc5c7 Mon Sep 17 00:00:00 2001 From: valued mammal Date: Wed, 19 Feb 2025 10:58:06 -0500 Subject: [PATCH] audittx: make Eq and Ord agree - remove custom comparators --- src/audittx.rs | 8 ++++---- src/blockmk.rs | 2 +- src/lib.rs | 4 +--- src/util.rs | 27 --------------------------- 4 files changed, 6 insertions(+), 35 deletions(-) diff --git a/src/audittx.rs b/src/audittx.rs index 09440ea..5c39682 100644 --- a/src/audittx.rs +++ b/src/audittx.rs @@ -62,7 +62,7 @@ impl AuditTx { impl PartialEq for AuditTx { fn eq(&self, other: &Self) -> bool { - self.uid == other.uid + self.score.eq(&other.score) && self.order.eq(&other.order) && self.uid.eq(&other.uid) } } @@ -72,7 +72,7 @@ impl Ord for AuditTx { fn cmp(&self, other: &Self) -> Ordering { let a = (self.score, self.order, self.uid); let b = (other.score, other.order, other.uid); - compare_audit_tx(a, b) + a.partial_cmp(&b).expect("must have ordering") } } @@ -92,7 +92,7 @@ pub struct TxPriority { impl PartialEq for TxPriority { fn eq(&self, other: &Self) -> bool { - self.uid == other.uid + self.score.eq(&other.score) && self.order.eq(&other.order) && self.uid.eq(&other.uid) } } @@ -102,7 +102,7 @@ impl Ord for TxPriority { fn cmp(&self, other: &Self) -> Ordering { let a = (self.score, self.order, self.uid); let b = (other.score, other.order, other.uid); - compare_audit_tx(a, b) + a.partial_cmp(&b).expect("must have ordering") } } diff --git a/src/blockmk.rs b/src/blockmk.rs index fd862b5..fa5bbd2 100644 --- a/src/blockmk.rs +++ b/src/blockmk.rs @@ -237,7 +237,7 @@ impl BlockAssembler { self.modified.remove(&ancestor.uid); } } - sorted.sort_unstable_by(compare_ancestor_count); + sorted.sort(); let package: Vec = sorted.into_iter().map(|(_, _, uid)| uid).collect(); package }; diff --git a/src/lib.rs b/src/lib.rs index 26a1676..61aad15 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,9 +9,7 @@ pub use { bitcoincore_rpc::{self, bitcoincore_rpc_json, Client, RpcApi}, error::Result, std::collections::{HashMap, HashSet}, - util::{ - compare_ancestor_count, compare_audit_tx, key_index, median_from_sorted, try_from_value, - }, + util::{key_index, median_from_sorted, try_from_value}, }; pub mod audittx; diff --git a/src/util.rs b/src/util.rs index eeee83d..1c01715 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,5 +1,4 @@ use super::*; -use std::cmp::Ordering; use std::hash::Hash; /// Creates a "reverse" index by mapping keys of the given `map` @@ -53,29 +52,3 @@ pub fn median_from_sorted(seq: &[f64]) -> f64 { truncate!(feerate) } } - -/// `a` and `b` as (score, order, uid) -pub fn compare_audit_tx(a: (f64, u32, usize), b: (f64, u32, usize)) -> Ordering { - if (a.0 - b.0).abs() > f64::EPSILON { - // a != b - a.0.total_cmp(&b.0) - } else if a.1 != b.1 { - a.1.cmp(&b.1) - } else { - a.2.cmp(&b.2) - } -} - -/// `a` and `b` as (ancestor count, order, uid) -pub fn compare_ancestor_count(a: &(usize, u32, usize), b: &(usize, u32, usize)) -> Ordering { - if a.0 != b.0 { - // compare ancestor count - a.2.cmp(&b.2) - } else if a.1 != b.1 { - // compare txid - a.1.cmp(&b.1) - } else { - // fallback to uid - a.2.cmp(&b.2) - } -}