Skip to content

Commit

Permalink
Implement ArbitraryOrd for relative::LockTime
Browse files Browse the repository at this point in the history
TL;DR As we do for `absolute::LockTime` and for the same reasons;
implement `ArbitraryOrd` for `relative::LockTime`.

locktimes do not have a semantic ordering if they differ (blocks, time)
so we do not derive `Ord` however it is useful for downstream to be able
to order structs that contain lock times. This is exactly what the
`ArbitraryOrd` trait is for.

Fix: rust-bitcoin#2566
  • Loading branch information
tcharding committed Mar 20, 2024
1 parent 6ff8505 commit 53c5d4f
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions bitcoin/src/blockdata/locktime/relative.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
//! whether bit 22 of the `u32` consensus value is set.
//!

#[cfg(feature = "ordered")]
use core::cmp::Ordering;
use core::fmt;

#[cfg(all(test, mutate))]
Expand Down Expand Up @@ -191,6 +193,20 @@ impl fmt::Display for LockTime {
}
}

#[cfg(feature = "ordered")]
impl ordered::ArbitraryOrd for LockTime {
fn arbitrary_cmp(&self, other: &Self) -> Ordering {
use LockTime::*;

match (self, other) {
(Blocks(_), Time(_)) => Ordering::Less,
(Time(_), Blocks(_)) => Ordering::Greater,
(Blocks(this), Blocks(that)) => this.cmp(that),
(Time(this), Time(that)) => this.cmp(that),
}
}
}

/// Tried to satisfy a lock-by-blocktime lock using a height value.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
Expand Down

0 comments on commit 53c5d4f

Please sign in to comment.