relative locktime: copy comments and PartialOrd impl from absolute locktimes

This commit is contained in:
Andrew Poelstra 2024-03-07 15:21:33 +00:00
parent 2ff5085e70
commit f02b1dac5b
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 21 additions and 3 deletions

View File

@ -6,7 +6,7 @@
//! whether bit 22 of the `u32` consensus value is set. //! whether bit 22 of the `u32` consensus value is set.
//! //!
use core::fmt; use core::{cmp, fmt};
#[cfg(all(test, mutate))] #[cfg(all(test, mutate))]
use mutagen::mutate; use mutagen::mutate;
@ -20,8 +20,13 @@ pub use units::locktime::relative::{Height, Time, TimeOverflowError};
/// A relative lock time value, representing either a block height or time (512 second intervals). /// A relative lock time value, representing either a block height or time (512 second intervals).
/// ///
/// The `relative::LockTime` type does not have any constructors, this is by design, please use /// Used for sequence numbers (`nSequence` in Bitcoin Core and [`crate::Transaction::TxIn::sequence`]
/// `Sequence::to_relative_lock_time` to create a relative lock time. /// in this library) and also for the argument to opcode 'OP_CHECKSEQUENCEVERIFY`.
///
/// ### Note on ordering
///
/// Because locktimes may be height- or time-based, and these metrics are incommensurate, there
/// is no total ordering on locktimes. We therefore have implemented [`PartialOrd`] but not [`Ord`].
/// ///
/// ### Relevant BIPs /// ### Relevant BIPs
/// ///
@ -173,6 +178,19 @@ impl From<Time> for LockTime {
fn from(t: Time) -> Self { LockTime::Time(t) } fn from(t: Time) -> Self { LockTime::Time(t) }
} }
impl PartialOrd for LockTime {
#[inline]
fn partial_cmp(&self, other: &LockTime) -> Option<cmp::Ordering> {
use LockTime::*;
match (*self, *other) {
(Blocks(ref a), Blocks(ref b)) => a.partial_cmp(b),
(Time(ref a), Time(ref b)) => a.partial_cmp(b),
(_, _) => None,
}
}
}
impl fmt::Display for LockTime { impl fmt::Display for LockTime {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use LockTime::*; use LockTime::*;