Skip to content

Commit

Permalink
audittx: make Eq and Ord agree
Browse files Browse the repository at this point in the history
- remove custom comparators
  • Loading branch information
ValuedMammal committed Feb 19, 2025
1 parent 0d03c7a commit 60bce0e
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 35 deletions.
8 changes: 4 additions & 4 deletions src/audittx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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")
}
}

Expand All @@ -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)
}
}

Expand All @@ -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")
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/blockmk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ impl BlockAssembler {
self.modified.remove(&ancestor.uid);
}
}
sorted.sort_unstable_by(compare_ancestor_count);
sorted.sort();
let package: Vec<usize> = sorted.into_iter().map(|(_, _, uid)| uid).collect();
package
};
Expand Down
4 changes: 1 addition & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
27 changes: 0 additions & 27 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -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`
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 60bce0e

Please sign in to comment.