Skip to content

Commit

Permalink
relative locktime: introduce is_* methods to check units
Browse files Browse the repository at this point in the history
Copy these from absolute::LockTime. While we are at it, make the
functions in absolute::LockTime const.
  • Loading branch information
apoelstra committed Mar 7, 2024
1 parent c5e3c1f commit cbf749c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
4 changes: 2 additions & 2 deletions bitcoin/src/blockdata/locktime/absolute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,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,
Expand All @@ -196,7 +196,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`.
///
Expand Down
23 changes: 21 additions & 2 deletions bitcoin/src/blockdata/locktime/relative.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! whether bit 22 of the `u32` consensus value is set.
//!

use core::{cmp, convert, fmt};
use core::{cmp, convert, fmt, mem};

#[cfg(all(test, mutate))]
use mutagen::mutate;
Expand All @@ -18,7 +18,7 @@ use crate::Sequence;

/// 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
Expand Down Expand Up @@ -138,6 +138,25 @@ impl LockTime {
Time::from_seconds_ceil(seconds).map(LockTime::Time)
}

/// Returns true if both lock times use the same unit i.e., both height based or both time based.
#[inline]
pub fn is_same_unit(&self, other: LockTime) -> bool {
mem::discriminant(self) == mem::discriminant(&other)
}

/// Returns true if this lock time value is in units of block height.
#[inline]
pub const fn is_block_height(&self) -> bool {
match *self {
LockTime::Blocks(_) => true,
LockTime::Time(_) => false,
}
}

/// 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
Expand Down

0 comments on commit cbf749c

Please sign in to comment.