pub enum LockTime {
Blocks(NumberOfBlocks),
Time(NumberOf512Seconds),
}Expand description
A relative lock time value, representing either a count of blocks or time (512 second intervals).
Used for sequence numbers (nSequence in Bitcoin Core and TxIn::sequence
in rust-bitcoin) and also for the argument to opcode OP_CHECKSEQUENCEVERIFY.
§Note on ordering
Locktimes may be block- or time-based, and these metrics are incommensurate; there is no total
ordering on locktimes. In order to compare locktimes, instead of using < or > we provide the
LockTime::is_satisfied_by API.
§Relevant BIPs
Variants§
Blocks(NumberOfBlocks)
A lock time measured in number of blocks.
Time(NumberOf512Seconds)
A 512 second time interval value.
Implementations§
Source§impl LockTime
impl LockTime
Sourcepub const ZERO: Self
pub const ZERO: Self
A relative locktime of 0 is always valid, and is assumed valid for inputs that are not yet confirmed.
Sourcepub const SIZE: usize = 4
pub const SIZE: usize = 4
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 new 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.
§Errors
If n, interpreted as a Sequence number does not encode a relative lock time.
§Examples
// Values with bit 22 set to 0 will be interpreted as block-based lock times.
let blocks: u32 = 144; // 144 blocks, approx 24h.
let lock_time = relative::LockTime::from_consensus(blocks)?;
assert!(lock_time.is_block_count());
assert_eq!(lock_time.to_consensus_u32(), blocks);
// Values with bit 22 set to 1 will be interpreted as time-based lock times.
let time: u32 = 168 | (1 << 22) ; // Bit 22 is 1 with time approx 24h.
let lock_time = relative::LockTime::from_consensus(time)?;
assert!(lock_time.is_block_time());
assert_eq!(lock_time.to_consensus_u32(), 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 new 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.
§Errors
If n does not encode a relative lock time.
§Examples
// Interpret a sequence number from a Bitcoin transaction input as a relative lock time
let sequence_number = Sequence::from_consensus(144); // 144 blocks, approx 24h.
let lock_time = relative::LockTime::from_sequence(sequence_number)?;
assert!(lock_time.is_block_count());
Sourcepub fn to_sequence(self) -> Sequence
pub fn to_sequence(self) -> Sequence
Encodes the locktime as a sequence number.
Sourcepub const fn from_block_count(n: u16) -> Self
pub const fn from_block_count(n: u16) -> Self
Constructs a new 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 new 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: Self) -> bool
pub const fn is_same_unit(self, other: Self) -> 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_count(self) -> bool
pub const fn is_block_count(self) -> bool
Returns true if this lock time value is in units of blocks.
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,
chain_tip_height: BlockHeight,
chain_tip_mtp: BlockMtp,
utxo_mined_at_height: Option<BlockHeight>,
utxo_mined_at_mtp: Option<BlockMtp>,
) -> Result<bool, IsSatisfiedByError>
pub fn is_satisfied_by( self, chain_tip_height: BlockHeight, chain_tip_mtp: BlockMtp, utxo_mined_at_height: Option<BlockHeight>, utxo_mined_at_mtp: Option<BlockMtp>, ) -> Result<bool, IsSatisfiedByError>
Returns true if this relative::LockTime is satisfied by the given chain state.
If this function returns true then an output with this locktime can be spent in the next block.
§Arguments
chain_tip_height: The current height of the blockchain.chain_tip_mtp: The current MTP (i.e. median of the 11 latest blocks).utxo_mined_at_height:Someheight if mined,Noneif UTXO may go in the next block.utxo_mined_at_mtp:SomeMTP if mined,Noneif UTXO may go in the next block.
§Errors
If chain_tip is not after utxo_mined_at i.e., if you get the args mixed up.
Sourcepub fn is_satisfied_by_height(
self,
chain_tip: BlockHeight,
utxo_mined_at: Option<BlockHeight>,
) -> Result<bool, IsSatisfiedByHeightError>
pub fn is_satisfied_by_height( self, chain_tip: BlockHeight, utxo_mined_at: Option<BlockHeight>, ) -> Result<bool, IsSatisfiedByHeightError>
Sourcepub fn is_satisfied_by_time(
self,
chain_tip: BlockMtp,
utxo_mined_at: Option<BlockMtp>,
) -> Result<bool, IsSatisfiedByTimeError>
pub fn is_satisfied_by_time( self, chain_tip: BlockMtp, utxo_mined_at: Option<BlockMtp>, ) -> Result<bool, IsSatisfiedByTimeError>
Sourcepub fn is_implied_by(self, other: Self) -> bool
pub fn is_implied_by(self, other: Self) -> 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.
§Examples
let sequence = Sequence::from_consensus(1 << 22 | 168); // Bit 22 is 1 with time approx 24h.
let lock_time = relative::LockTime::from_sequence(sequence)?;
let input_sequence = Sequence::from_consensus(1 << 22 | 336); // Approx 48h.
assert!(lock_time.is_block_time());
assert!(lock_time.is_implied_by_sequence(input_sequence));
Trait Implementations§
Source§impl<'a> Arbitrary<'a> for LockTime
Available on crate feature arbitrary only.
impl<'a> Arbitrary<'a> for LockTime
arbitrary only.Source§fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
Self from the given unstructured data. Read moreSource§fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
Self from the entirety of the given
unstructured data. Read moreSource§fn size_hint(depth: usize) -> (usize, Option<usize>)
fn size_hint(depth: usize) -> (usize, Option<usize>)
Unstructured this type
needs to construct itself. Read moreSource§fn try_size_hint(
depth: usize,
) -> Result<(usize, Option<usize>), MaxRecursionReached>
fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>
Unstructured this type
needs to construct itself. Read moreimpl Copy for LockTime
Source§impl<'de> Deserialize<'de> for LockTime
Available on crate feature serde only.
impl<'de> Deserialize<'de> for LockTime
serde only.