From 878b865f855e3c67ab9a211f33a229d897b4451a Mon Sep 17 00:00:00 2001 From: Andrew Poelstra Date: Thu, 7 Mar 2024 18:14:05 +0000 Subject: [PATCH] relative locktime: introduce is_* methods to check units Copy these from absolute::LockTime. While we are at it, make the functions in absolute::LockTime const. --- bitcoin/src/blockdata/locktime/absolute.rs | 4 ++-- bitcoin/src/blockdata/locktime/relative.rs | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/bitcoin/src/blockdata/locktime/absolute.rs b/bitcoin/src/blockdata/locktime/absolute.rs index 92867051..839317e5 100644 --- a/bitcoin/src/blockdata/locktime/absolute.rs +++ b/bitcoin/src/blockdata/locktime/absolute.rs @@ -176,7 +176,7 @@ impl LockTime { /// Returns true if this lock time value is a block height. #[inline] - pub fn is_block_height(&self) -> bool { + pub const fn is_block_height(&self) -> bool { match *self { LockTime::Blocks(_) => true, LockTime::Seconds(_) => false, @@ -185,7 +185,7 @@ impl LockTime { /// Returns true if this lock time value is a block time (UNIX timestamp). #[inline] - pub fn is_block_time(&self) -> bool { !self.is_block_height() } + pub const fn is_block_time(&self) -> bool { !self.is_block_height() } /// Returns true if this timelock constraint is satisfied by the respective `height`/`time`. /// diff --git a/bitcoin/src/blockdata/locktime/relative.rs b/bitcoin/src/blockdata/locktime/relative.rs index 98ffef7f..f574f7fb 100644 --- a/bitcoin/src/blockdata/locktime/relative.rs +++ b/bitcoin/src/blockdata/locktime/relative.rs @@ -21,7 +21,7 @@ pub use units::locktime::relative::{Height, Time, TimeOverflowError}; /// A relative lock time value, representing either a block height or time (512 second intervals). /// -/// Used for sequence numbers (`nSequence` in Bitcoin Core and [`crate::Transaction::TxIn::sequence`] +/// Used for sequence numbers (`nSequence` in Bitcoin Core and [`crate::TxIn::sequence`] /// in this library) and also for the argument to opcode 'OP_CHECKSEQUENCEVERIFY`. /// /// ### Note on ordering @@ -141,6 +141,23 @@ impl LockTime { } } + /// Returns true if both lock times use the same unit i.e., both height based or both time based. + #[inline] + pub const fn is_same_unit(&self, other: LockTime) -> bool { + matches!( + (self, other), + (LockTime::Blocks(_), LockTime::Blocks(_)) | (LockTime::Time(_), LockTime::Time(_)) + ) + } + + /// Returns true if this lock time value is in units of block height. + #[inline] + pub const fn is_block_height(&self) -> bool { matches!(*self, LockTime::Blocks(_)) } + + /// Returns true if this lock time value is in units of time. + #[inline] + pub const fn is_block_time(&self) -> bool { !self.is_block_height() } + /// Returns true if this [`relative::LockTime`] is satisfied by either height or time. /// /// # Examples