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.
This commit is contained in:
Andrew Poelstra 2024-03-07 18:14:05 +00:00
parent c2f87c7ab3
commit 878b865f85
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
2 changed files with 20 additions and 3 deletions

View File

@ -176,7 +176,7 @@ impl LockTime {
/// Returns true if this lock time value is a block height. /// Returns true if this lock time value is a block height.
#[inline] #[inline]
pub fn is_block_height(&self) -> bool { pub const fn is_block_height(&self) -> bool {
match *self { match *self {
LockTime::Blocks(_) => true, LockTime::Blocks(_) => true,
LockTime::Seconds(_) => false, LockTime::Seconds(_) => false,
@ -185,7 +185,7 @@ impl LockTime {
/// Returns true if this lock time value is a block time (UNIX timestamp). /// Returns true if this lock time value is a block time (UNIX timestamp).
#[inline] #[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`. /// Returns true if this timelock constraint is satisfied by the respective `height`/`time`.
/// ///

View File

@ -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). /// 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`. /// in this library) and also for the argument to opcode 'OP_CHECKSEQUENCEVERIFY`.
/// ///
/// ### Note on ordering /// ### 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. /// Returns true if this [`relative::LockTime`] is satisfied by either height or time.
/// ///
/// # Examples /// # Examples