bdk_chain::bitcoin::blockdata::locktime::relative

Enum LockTime

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§

§

Blocks(Height)

A block height lock time value.

§

Time(Time)

A 512 second time interval value.

Implementations§

§

impl LockTime

pub const ZERO: LockTime

A relative locktime of 0 is always valid, and is assumed valid for inputs that are not yet confirmed.

pub const SIZE: usize = 4usize

The number of bytes that the locktime contributes to the size of a transaction.

pub fn from_consensus(n: u32) -> Result<LockTime, 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);

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.

pub fn from_sequence(n: Sequence) -> Result<LockTime, 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.

pub fn to_sequence(&self) -> Sequence

Encodes the locktime as a sequence number.

pub const fn from_height(n: u16) -> LockTime

Constructs a LockTime from n, expecting n to be a 16-bit count of blocks.

pub const fn from_512_second_intervals(intervals: u16) -> LockTime

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.

pub const fn from_seconds_floor( seconds: u32, ) -> Result<LockTime, TimeOverflowError>

Create a LockTime from seconds, converting the seconds into 512 second interval with truncating division.

§Errors

Will return an error if the input cannot be encoded in 16 bits.

pub const fn from_seconds_ceil( seconds: u32, ) -> Result<LockTime, TimeOverflowError>

Create a LockTime from seconds, converting the seconds into 512 second interval with ceiling division.

§Errors

Will return an error if the input cannot be encoded in 16 bits.

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.

pub const fn is_block_height(&self) -> bool

Returns true if this lock time value is in units of block height.

pub const fn is_block_time(&self) -> bool

Returns true if this lock time value is in units of time.

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()));

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);

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.

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"));

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§

§

impl Clone for LockTime

§

fn clone(&self) -> LockTime

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl Debug for LockTime

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl<'de> Deserialize<'de> for LockTime

§

fn deserialize<__D>( __deserializer: __D, ) -> Result<LockTime, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
§

impl Display for LockTime

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl From<Height> for LockTime

§

fn from(h: Height) -> LockTime

Converts to this type from the input type.
§

impl From<LockTime> for Sequence

§

fn from(lt: LockTime) -> Sequence

Converts to this type from the input type.
§

impl From<RelLockTime> for LockTime

§

fn from(lock_time: RelLockTime) -> LockTime

Converts to this type from the input type.
§

impl From<Time> for LockTime

§

fn from(t: Time) -> LockTime

Converts to this type from the input type.
§

impl Hash for LockTime

§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
§

impl PartialEq for LockTime

§

fn eq(&self, other: &LockTime) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
§

impl PartialOrd for LockTime

§

fn partial_cmp(&self, other: &LockTime) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
§

impl<Pk> Satisfier<Pk> for LockTime
where Pk: MiniscriptKey + ToPublicKey,

§

fn check_older(&self, n: LockTime) -> bool

Assert whether an relative locktime is satisfied Read more
§

fn lookup_ecdsa_sig(&self, _: &Pk) -> Option<Signature>

Given a public key, look up an ECDSA signature with that key
§

fn lookup_tap_key_spend_sig(&self) -> Option<Signature>

Lookup the tap key spend sig
§

fn lookup_tap_leaf_script_sig( &self, _: &Pk, _: &TapLeafHash, ) -> Option<Signature>

Given a public key and a associated leaf hash, look up an schnorr signature with that key
§

fn lookup_tap_control_block_map( &self, ) -> Option<&BTreeMap<ControlBlock, (ScriptBuf, LeafVersion)>>

Obtain a reference to the control block for a ver and script
§

fn lookup_raw_pkh_pk(&self, _: &Hash) -> Option<PublicKey>

Given a raw Pkh, lookup corresponding bitcoin::PublicKey
§

fn lookup_raw_pkh_x_only_pk(&self, _: &Hash) -> Option<XOnlyPublicKey>

Given a raw Pkh, lookup corresponding bitcoin::secp256k1::XOnlyPublicKey
§

fn lookup_raw_pkh_ecdsa_sig(&self, _: &Hash) -> Option<(PublicKey, Signature)>

Given a keyhash, look up the EC signature and the associated key Even if signatures for public key Hashes are not available, the users can use this map to provide pkh -> pk mapping which can be useful for dissatisfying pkh.
§

fn lookup_raw_pkh_tap_leaf_script_sig( &self, _: &(Hash, TapLeafHash), ) -> Option<(XOnlyPublicKey, Signature)>

Given a keyhash, look up the schnorr signature and the associated key Even if signatures for public key Hashes are not available, the users can use this map to provide pkh -> pk mapping which can be useful for dissatisfying pkh.
§

fn lookup_sha256(&self, _: &<Pk as MiniscriptKey>::Sha256) -> Option<[u8; 32]>

Given a SHA256 hash, look up its preimage
§

fn lookup_hash256(&self, _: &<Pk as MiniscriptKey>::Hash256) -> Option<[u8; 32]>

Given a HASH256 hash, look up its preimage
§

fn lookup_ripemd160( &self, _: &<Pk as MiniscriptKey>::Ripemd160, ) -> Option<[u8; 32]>

Given a RIPEMD160 hash, look up its preimage
§

fn lookup_hash160(&self, _: &<Pk as MiniscriptKey>::Hash160) -> Option<[u8; 32]>

Given a HASH160 hash, look up its preimage
§

fn check_after(&self, _: LockTime) -> bool

Assert whether a absolute locktime is satisfied Read more
§

impl Serialize for LockTime

§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
§

impl TryFrom<Sequence> for LockTime

§

type Error = DisabledLockTimeError

The type returned in the event of a conversion error.
§

fn try_from(seq: Sequence) -> Result<LockTime, DisabledLockTimeError>

Performs the conversion.
§

impl Copy for LockTime

§

impl Eq for LockTime

§

impl StructuralPartialEq for LockTime

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T, Pk> AssetProvider<Pk> for T
where T: Satisfier<Pk>, Pk: MiniscriptKey + ToPublicKey,

§

fn provider_lookup_ecdsa_sig(&self, pk: &Pk) -> bool

Given a public key, look up an ECDSA signature with that key, return whether we found it
§

fn provider_lookup_tap_key_spend_sig(&self, _: &Pk) -> Option<usize>

Lookup the tap key spend sig and return its size
§

fn provider_lookup_tap_leaf_script_sig( &self, pk: &Pk, leaf_hash: &TapLeafHash, ) -> Option<usize>

Given a public key and a associated leaf hash, look up a schnorr signature with that key and return its size
§

fn provider_lookup_tap_control_block_map( &self, ) -> Option<&BTreeMap<ControlBlock, (ScriptBuf, LeafVersion)>>

Obtain a reference to the control block for a ver and script
§

fn provider_lookup_raw_pkh_pk(&self, hash: &Hash) -> Option<PublicKey>

Given a raw Pkh, lookup corresponding bitcoin::PublicKey
§

fn provider_lookup_raw_pkh_x_only_pk( &self, hash: &Hash, ) -> Option<XOnlyPublicKey>

Given a raw Pkh, lookup corresponding bitcoin::secp256k1::XOnlyPublicKey
§

fn provider_lookup_raw_pkh_ecdsa_sig(&self, hash: &Hash) -> Option<PublicKey>

Given a keyhash, look up the EC signature and the associated key. Returns the key if a signature is found. Even if signatures for public key Hashes are not available, the users can use this map to provide pkh -> pk mapping which can be useful for dissatisfying pkh.
§

fn provider_lookup_raw_pkh_tap_leaf_script_sig( &self, hash: &(Hash, TapLeafHash), ) -> Option<(XOnlyPublicKey, usize)>

Given a keyhash, look up the schnorr signature and the associated key. Returns the key and sig len if a signature is found. Even if signatures for public key Hashes are not available, the users can use this map to provide pkh -> pk mapping which can be useful for dissatisfying pkh.
§

fn provider_lookup_sha256(&self, hash: &<Pk as MiniscriptKey>::Sha256) -> bool

Given a SHA256 hash, look up its preimage, return whether we found it
§

fn provider_lookup_hash256(&self, hash: &<Pk as MiniscriptKey>::Hash256) -> bool

Given a HASH256 hash, look up its preimage, return whether we found it
§

fn provider_lookup_ripemd160( &self, hash: &<Pk as MiniscriptKey>::Ripemd160, ) -> bool

Given a RIPEMD160 hash, look up its preimage, return whether we found it
§

fn provider_lookup_hash160(&self, hash: &<Pk as MiniscriptKey>::Hash160) -> bool

Given a HASH160 hash, look up its preimage, return whether we found it
§

fn check_older(&self, s: LockTime) -> bool

Assert whether a relative locktime is satisfied
§

fn check_after(&self, l: LockTime) -> bool

Assert whether an absolute locktime is satisfied
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,