pub enum LockTime {
Blocks(Height),
Time(Time),
}
Expand description
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::TxIn::sequence
in this library) and also for the argument to opcode ’OP_CHECKSEQUENCEVERIFY`.
§Note on ordering
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
. We also
implement ordered::ArbitraryOrd
if the “ordered” feature is enabled.
§Relevant BIPs
Variants§
Implementations§
Source§impl LockTime
impl LockTime
Sourcepub const ZERO: LockTime = _
pub const ZERO: LockTime = _
A relative locktime of 0 is always valid, and is assumed valid for inputs that are not yet confirmed.
Sourcepub const SIZE: usize = 4usize
pub const SIZE: usize = 4usize
The number of bytes that the locktime contributes to the size of a transaction.
Sourcepub fn from_consensus(n: u32) -> Result<Self, DisabledLockTimeError>
pub fn from_consensus(n: u32) -> Result<Self, DisabledLockTimeError>
Constructs a LockTime
from an nSequence value or the argument to OP_CHECKSEQUENCEVERIFY.
This method will not round-trip with Self::to_consensus_u32
, because relative
locktimes only use some bits of the underlying u32
value and discard the rest. If
you want to preserve the full value, you should use the Sequence
type instead.
§Examples
// `from_consensus` roundtrips with `to_consensus_u32` for small values.
let n_lock_time: u32 = 7000;
let lock_time = LockTime::from_consensus(n_lock_time).unwrap();
assert_eq!(lock_time.to_consensus_u32(), n_lock_time);
Sourcepub fn to_consensus_u32(&self) -> u32
pub fn to_consensus_u32(&self) -> u32
Returns the u32
value used to encode this locktime in an nSequence field or
argument to OP_CHECKSEQUENCEVERIFY
.
§Warning
Locktimes are not ordered by the natural ordering on u32
. If you want to
compare locktimes, use Self::is_implied_by
or similar methods.
Sourcepub fn from_sequence(n: Sequence) -> Result<Self, DisabledLockTimeError>
pub fn from_sequence(n: Sequence) -> Result<Self, DisabledLockTimeError>
Constructs a LockTime
from the sequence number of a Bitcoin input.
This method will not round-trip with Self::to_sequence
. See the
docs for Self::from_consensus
for more information.
Sourcepub fn to_sequence(&self) -> Sequence
pub fn to_sequence(&self) -> Sequence
Encodes the locktime as a sequence number.
Sourcepub const fn from_height(n: u16) -> Self
pub const fn from_height(n: u16) -> Self
Constructs a LockTime
from n
, expecting n
to be a 16-bit count of blocks.
Sourcepub const fn from_512_second_intervals(intervals: u16) -> Self
pub const fn from_512_second_intervals(intervals: u16) -> Self
Constructs a LockTime
from n
, expecting n
to be a count of 512-second intervals.
This function is a little awkward to use, and users may wish to instead use
Self::from_seconds_floor
or Self::from_seconds_ceil
.
Sourcepub const fn from_seconds_floor(seconds: u32) -> Result<Self, TimeOverflowError>
pub const fn from_seconds_floor(seconds: u32) -> Result<Self, TimeOverflowError>
Sourcepub const fn from_seconds_ceil(seconds: u32) -> Result<Self, TimeOverflowError>
pub const fn from_seconds_ceil(seconds: u32) -> Result<Self, TimeOverflowError>
Sourcepub const fn is_same_unit(&self, other: LockTime) -> bool
pub const fn is_same_unit(&self, other: LockTime) -> bool
Returns true if both lock times use the same unit i.e., both height based or both time based.
Sourcepub const fn is_block_height(&self) -> bool
pub const fn is_block_height(&self) -> bool
Returns true if this lock time value is in units of block height.
Sourcepub const fn is_block_time(&self) -> bool
pub const fn is_block_time(&self) -> bool
Returns true if this lock time value is in units of time.
Sourcepub fn is_satisfied_by(&self, h: Height, t: Time) -> bool
pub fn is_satisfied_by(&self, h: Height, t: Time) -> bool
Returns true if this relative::LockTime
is satisfied by either height or time.
§Examples
// Users that have chain data can get the current height and time to check against a lock.
let height_and_time = (current_time(), current_height()); // tuple order does not matter.
assert!(lock.is_satisfied_by(current_height(), current_time()));
Sourcepub fn is_implied_by(&self, other: LockTime) -> bool
pub fn is_implied_by(&self, other: LockTime) -> bool
Returns true if satisfaction of other
lock time implies satisfaction of this
relative::LockTime
.
A lock time can only be satisfied by n blocks being mined or n seconds passing. If you have two lock times (same unit) then the larger lock time being satisfied implies (in a mathematical sense) the smaller one being satisfied.
This function is useful when checking sequence values against a lock, first one checks the
sequence represents a relative lock time by converting to LockTime
then use this function
to see if satisfaction of the newly created lock time would imply satisfaction of self
.
Can also be used to remove the smaller value of two OP_CHECKSEQUENCEVERIFY
operations
within one branch of the script.
§Examples
let satisfied = match test_sequence.to_relative_lock_time() {
None => false, // Handle non-lock-time case.
Some(test_lock) => lock.is_implied_by(test_lock),
};
assert!(satisfied);
Sourcepub fn is_implied_by_sequence(&self, other: Sequence) -> bool
pub fn is_implied_by_sequence(&self, other: Sequence) -> bool
Returns true if satisfaction of the sequence number implies satisfaction of this lock time.
When deciding whether an instance of <n> CHECKSEQUENCEVERIFY
will pass, this
method can be used by parsing n
as a LockTime
and calling this method
with the sequence number of the input which spends the script.
Sourcepub fn is_satisfied_by_height(
&self,
height: Height,
) -> Result<bool, IncompatibleHeightError>
pub fn is_satisfied_by_height( &self, height: Height, ) -> Result<bool, IncompatibleHeightError>
Returns true if this relative::LockTime
is satisfied by Height
.
§Errors
Returns an error if this lock is not lock-by-height.
§Examples
let height: u16 = 100;
let lock = Sequence::from_height(height).to_relative_lock_time().expect("valid height");
assert!(lock.is_satisfied_by_height(Height::from(height+1)).expect("a height"));
Sourcepub fn is_satisfied_by_time(
&self,
time: Time,
) -> Result<bool, IncompatibleTimeError>
pub fn is_satisfied_by_time( &self, time: Time, ) -> Result<bool, IncompatibleTimeError>
Returns true if this relative::LockTime
is satisfied by Time
.
§Errors
Returns an error if this lock is not lock-by-time.
§Examples
let intervals: u16 = 70; // approx 10 hours;
let lock = Sequence::from_512_second_intervals(intervals).to_relative_lock_time().expect("valid time");
assert!(lock.is_satisfied_by_time(Time::from_512_second_intervals(intervals + 10)).expect("a time"));
Trait Implementations§
Source§impl ArbitraryOrd for LockTime
Available on crate feature ordered
only.
impl ArbitraryOrd for LockTime
ordered
only.Source§fn arbitrary_cmp(&self, other: &Self) -> Ordering
fn arbitrary_cmp(&self, other: &Self) -> Ordering
Source§impl<'de> Deserialize<'de> for LockTime
impl<'de> Deserialize<'de> for LockTime
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl PartialOrd for LockTime
impl PartialOrd for LockTime
impl Copy for LockTime
impl Eq for LockTime
impl StructuralPartialEq for LockTime
Auto Trait Implementations§
impl Freeze for LockTime
impl RefUnwindSafe for LockTime
impl Send for LockTime
impl Sync for LockTime
impl Unpin for LockTime
impl UnwindSafe for LockTime
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)