pub struct MedianTimePast(/* private fields */);Expand description
The median timestamp of 11 consecutive blocks, representing “the timestamp” of the final block for locktime-checking purposes.
Time-based locktimes are not measured against the timestamps in individual block headers, since these are not monotone and may be subject to miner manipulation. Instead, locktimes use the “median-time-past” (MTP) of the most recent 11 blocks, a quantity which is required by consensus to be monotone and which is difficult for any individual miner to manipulate.
Implementations§
Source§impl MedianTimePast
impl MedianTimePast
Sourcepub const MIN: MedianTimePast
pub const MIN: MedianTimePast
The minimum MTP allowable in a locktime (Tue Nov 05 1985 00:53:20 GMT+0000).
Sourcepub const MAX: MedianTimePast
pub const MAX: MedianTimePast
The maximum MTP allowable in a locktime (Sun Feb 07 2106 06:28:15 GMT+0000).
Sourcepub fn new(
timestamps: [BlockTime; 11],
) -> Result<MedianTimePast, ConversionError>
pub fn new( timestamps: [BlockTime; 11], ) -> Result<MedianTimePast, ConversionError>
Constructs an MedianTimePast by computing the median-time-past from the last
11 block timestamps.
Because block timestamps are not monotonic, this function internally sorts them; it is therefore not important what order they appear in the array; use whatever is most convenient.
§Errors
If the median block timestamp is not in the allowable range of MTPs in a
locktime: [500_000_000, 2^32 - 1]. Because there is a consensus rule that MTP
be monotonically increasing, and the MTP of the first 11 blocks exceeds 500_000_000
for every real-life chain, this error typically cannot be hit in practice.
Sourcepub fn from_hex(s: &str) -> Result<MedianTimePast, ParseTimeError>
pub fn from_hex(s: &str) -> Result<MedianTimePast, ParseTimeError>
Constructs a new MedianTimePast from a big-endian hex-encoded u32.
The input string may or may not contain a typical hex prefix e.g., 0x.
§Errors
If the input string is not a valid hex representation of a block time.
Sourcepub const fn from_u32(n: u32) -> Result<MedianTimePast, ConversionError>
pub const fn from_u32(n: u32) -> Result<MedianTimePast, ConversionError>
Constructs a new MTP directly from a u32 value.
This function, with MedianTimePast::to_u32, is used to obtain a raw MTP value. It is
not used to convert to or from a block timestamp, which is not a MTP.
§Errors
If n is not in the allowable range of MTPs in a locktime: [500_000_000, 2^32 - 1].
§Examples
use bitcoin_units::locktime::absolute;
let t: u32 = 1653195600; // May 22nd, 5am UTC.
let time = absolute::MedianTimePast::from_u32(t)?;
assert_eq!(time.to_u32(), t);Sourcepub const fn to_u32(self) -> u32
pub const fn to_u32(self) -> u32
Converts this MedianTimePast to a raw u32 value.
§Examples
use bitcoin_units::locktime::absolute;
assert_eq!(absolute::MedianTimePast::MIN.to_u32(), 500_000_000);Sourcepub fn is_satisfied_by(self, time: MedianTimePast) -> bool
pub fn is_satisfied_by(self, time: MedianTimePast) -> bool
Returns true if a transaction with this locktime can be included in the next block.
self is the value of the LockTime and if time is the median time past of the block at
the chain tip then a transaction with this lock can be broadcast for inclusion in the next
block.
Trait Implementations§
Source§impl<'a> Arbitrary<'a> for MedianTimePast
Available on crate feature arbitrary only.
impl<'a> Arbitrary<'a> for MedianTimePast
arbitrary only.Source§fn arbitrary(u: &mut Unstructured<'a>) -> Result<MedianTimePast, Error>
fn arbitrary(u: &mut Unstructured<'a>) -> Result<MedianTimePast, Error>
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 moreSource§impl Binary for MedianTimePast
impl Binary for MedianTimePast
Source§impl Clone for MedianTimePast
impl Clone for MedianTimePast
Source§fn clone(&self) -> MedianTimePast
fn clone(&self) -> MedianTimePast
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for MedianTimePast
impl Debug for MedianTimePast
Source§impl Display for MedianTimePast
impl Display for MedianTimePast
Source§impl From<MedianTimePast> for BlockMtp
impl From<MedianTimePast> for BlockMtp
Source§fn from(h: MedianTimePast) -> BlockMtp
fn from(h: MedianTimePast) -> BlockMtp
Converts a [locktime::absolute::MedianTimePast] to a BlockMtp.
An absolute locktime MTP has a minimum value of absolute::LOCK_TIME_THRESHOLD,
while BlockMtp may take the full range of u32.
Source§impl From<MedianTimePast> for LockTime
impl From<MedianTimePast> for LockTime
Source§fn from(t: MedianTimePast) -> LockTime
fn from(t: MedianTimePast) -> LockTime
Source§impl FromStr for MedianTimePast
impl FromStr for MedianTimePast
Source§type Err = ParseTimeError
type Err = ParseTimeError
Source§fn from_str(s: &str) -> Result<MedianTimePast, <MedianTimePast as FromStr>::Err>
fn from_str(s: &str) -> Result<MedianTimePast, <MedianTimePast as FromStr>::Err>
s to return a value of this type. Read moreSource§impl Hash for MedianTimePast
impl Hash for MedianTimePast
Source§impl LowerHex for MedianTimePast
impl LowerHex for MedianTimePast
Source§impl Octal for MedianTimePast
impl Octal for MedianTimePast
Source§impl Ord for MedianTimePast
impl Ord for MedianTimePast
Source§fn cmp(&self, other: &MedianTimePast) -> Ordering
fn cmp(&self, other: &MedianTimePast) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq for MedianTimePast
impl PartialEq for MedianTimePast
Source§impl PartialOrd for MedianTimePast
impl PartialOrd for MedianTimePast
Source§impl TryFrom<&str> for MedianTimePast
impl TryFrom<&str> for MedianTimePast
Source§type Error = ParseTimeError
type Error = ParseTimeError
Source§fn try_from(
s: &str,
) -> Result<MedianTimePast, <MedianTimePast as TryFrom<&str>>::Error>
fn try_from( s: &str, ) -> Result<MedianTimePast, <MedianTimePast as TryFrom<&str>>::Error>
Source§impl TryFrom<BlockMtp> for MedianTimePast
impl TryFrom<BlockMtp> for MedianTimePast
Source§fn try_from(
h: BlockMtp,
) -> Result<MedianTimePast, <MedianTimePast as TryFrom<BlockMtp>>::Error>
fn try_from( h: BlockMtp, ) -> Result<MedianTimePast, <MedianTimePast as TryFrom<BlockMtp>>::Error>
Converts a BlockHeight to a [locktime::absolute::Height].
An absolute locktime MTP has a minimum value of absolute::LOCK_TIME_THRESHOLD,
while BlockMtp may take the full range of u32.