pub enum ElementsLockTime {
Blocks(Height),
Seconds(Time),
}Expand description
A lock time value, representing either a block height or a UNIX timestamp (seconds since epoch).
Used for transaction lock time (nLockTime in Bitcoin Core and crate::Transaction::lock_time
in this library) and also for the argument to opcode ’OP_CHECKLOCKTIMEVERIFY`.
§Relevant BIPs
§Examples
// To compare lock times there are various `is_satisfied_*` methods, you may also use:
let is_satisfied = match (n, lock_time) {
(Blocks(n), Blocks(lock_time)) => n <= lock_time,
(Seconds(n), Seconds(lock_time)) => n <= lock_time,
_ => panic!("handle invalid comparison error"),
};Variants§
Blocks(Height)
A block height lock time value.
§Examples
use elements::LockTime;
let block: u32 = 741521;
let n = LockTime::from_height(block).expect("valid height");
assert!(n.is_block_height());
assert_eq!(n.to_consensus_u32(), block);Seconds(Time)
A UNIX timestamp lock time value.
§Examples
use elements::LockTime;
let seconds: u32 = 1653195600; // May 22nd, 5am UTC.
let n = LockTime::from_time(seconds).expect("valid time");
assert!(n.is_block_time());
assert_eq!(n.to_consensus_u32(), seconds);Implementations§
Source§impl LockTime
impl LockTime
Sourcepub const ZERO: LockTime
pub const ZERO: LockTime
If crate::Transaction::lock_time is set to zero it is ignored, in other words a
transaction with nLocktime==0 is able to be included immediately in any block.
Sourcepub fn from_consensus(n: u32) -> LockTime
pub fn from_consensus(n: u32) -> LockTime
Constructs a LockTime from an nLockTime value or the argument to OP_CHEKCLOCKTIMEVERIFY.
§Examples
use elements::LockTime;
let n = LockTime::from_consensus(741521); // n OP_CHECKLOCKTIMEVERIFY
// `from_consensus` roundtrips as expected with `to_consensus_u32`.
let n_lock_time: u32 = 741521;
let lock_time = LockTime::from_consensus(n_lock_time);
assert_eq!(lock_time.to_consensus_u32(), n_lock_time);Sourcepub fn from_height(n: u32) -> Result<LockTime, Error>
pub fn from_height(n: u32) -> Result<LockTime, Error>
Constructs a LockTime from n, expecting n to be a valid block height.
See LOCK_TIME_THRESHOLD for definition of a valid height value.
§Examples
use elements::LockTime;
assert!(LockTime::from_height(741521).is_ok());
assert!(LockTime::from_height(1653195600).is_err());Sourcepub fn from_time(n: u32) -> Result<LockTime, Error>
pub fn from_time(n: u32) -> Result<LockTime, Error>
Constructs a LockTime from n, expecting n to be a valid block time.
See LOCK_TIME_THRESHOLD for definition of a valid time value.
§Examples
use elements::LockTime;
assert!(LockTime::from_time(1653195600).is_ok());
assert!(LockTime::from_time(741521).is_err());Sourcepub fn is_same_unit(&self, other: LockTime) -> bool
pub 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 fn is_block_height(&self) -> bool
pub fn is_block_height(&self) -> bool
Returns true if this lock time value is a block height.
Sourcepub fn is_block_time(&self) -> bool
pub fn is_block_time(&self) -> bool
Returns true if this lock time value is a block time (UNIX timestamp).
Sourcepub fn is_satisfied_by(&self, height: Height, time: Time) -> bool
pub fn is_satisfied_by(&self, height: Height, time: Time) -> bool
Returns true if this timelock constraint is satisfied by the respective height/time.
If self is a blockheight based lock then it is checked against height and if self is a
blocktime based lock it is checked against time.
A ‘timelock constraint’ refers to the n from n OP_CHEKCLOCKTIMEVERIFY, this constraint
is satisfied if a transaction with nLockTime (crate::Transaction::lock_time) set to
height/time is valid.
§Examples
// Can be implemented if block chain data is available.
fn get_height() -> Height { todo!("return the current block height") }
fn get_time() -> Time { todo!("return the current block time") }
let n = LockTime::from_consensus(741521); // `n OP_CHEKCLOCKTIMEVERIFY`.
if n.is_satisfied_by(get_height(), get_time()) {
// Can create and mine a transaction that satisfies the OP_CLTV timelock constraint.
}Sourcepub fn to_consensus_u32(self) -> u32
pub fn to_consensus_u32(self) -> u32
Returns the inner u32 value. This is the value used when creating this LockTime
i.e., n OP_CHECKLOCKTIMEVERIFY or nLockTime.
§Warning
Do not compare values return by this method. The whole point of the LockTime type is to
assist in doing correct comparisons. Either use is_satisfied_by or use the pattern below:
§Examples
let is_satisfied = match (n, lock_time) {
(Blocks(n), Blocks(lock_time)) => n <= lock_time,
(Seconds(n), Seconds(lock_time)) => n <= lock_time,
_ => panic!("invalid comparison"),
};
// Or, if you have Rust 1.53 or greater
// let is_satisfied = n.partial_cmp(&lock_time).expect("invalid comparison").is_le();Trait Implementations§
Source§impl<'de> Deserialize<'de> for LockTime
impl<'de> Deserialize<'de> for LockTime
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<LockTime, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<LockTime, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Source§impl Deserialize for LockTime
impl Deserialize for LockTime
Source§impl PartialOrd for LockTime
impl PartialOrd for LockTime
Source§impl Serialize for LockTime
impl Serialize for LockTime
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
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§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
Source§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.