diff --git a/bitcoin/src/blockdata/block.rs b/bitcoin/src/blockdata/block.rs index b88f2a197..5fd59aa31 100644 --- a/bitcoin/src/blockdata/block.rs +++ b/bitcoin/src/blockdata/block.rs @@ -30,7 +30,7 @@ use crate::transaction::{Transaction, TransactionExt as _, Wtxid}; #[doc(inline)] pub use primitives::block::{Block, Checked, Unchecked, Validation, Version, BlockHash, Header, WitnessCommitment}; #[doc(inline)] -pub use units::block::{BlockHeight, BlockInterval, TooBigForRelativeBlockHeightError}; +pub use units::block::{BlockHeight, BlockInterval, TooBigForRelativeBlockHeightIntervalError}; impl_hashencode!(BlockHash); diff --git a/bitcoin/src/blockdata/mod.rs b/bitcoin/src/blockdata/mod.rs index ab3b67463..8255e3cc5 100644 --- a/bitcoin/src/blockdata/mod.rs +++ b/bitcoin/src/blockdata/mod.rs @@ -41,7 +41,11 @@ pub mod locktime { /// Re-export everything from the `primitives::locktime::absolute` module. #[rustfmt::skip] // Keep public re-exports separate. - pub use primitives::locktime::absolute::{ConversionError, Height, LockTime, ParseHeightError, ParseTimeError, Time}; + pub use primitives::locktime::absolute::{ConversionError, Height, LockTime, ParseHeightError, ParseTimeError, Mtp}; + + #[deprecated(since = "TBD", note = "use `Mtp` instead")] + #[doc(hidden)] + pub type Time = Mtp; impl Encodable for LockTime { #[inline] @@ -67,9 +71,17 @@ pub mod locktime { /// Re-export everything from the `primitives::locktime::relative` module. pub use primitives::locktime::relative::{ - DisabledLockTimeError, Height, IncompatibleHeightError, IncompatibleTimeError, - LockTime, Time, TimeOverflowError, + DisabledLockTimeError, HeightInterval, IncompatibleHeightError, IncompatibleTimeError, + LockTime, MtpInterval, TimeOverflowError, }; + + #[deprecated(since = "TBD", note = "use `Mtp` instead")] + #[doc(hidden)] + pub type Height = HeightInterval; + + #[deprecated(since = "TBD", note = "use `Mtp` instead")] + #[doc(hidden)] + pub type Time = MtpInterval; } } diff --git a/bitcoin/src/blockdata/transaction.rs b/bitcoin/src/blockdata/transaction.rs index 258597247..ff2d7d19a 100644 --- a/bitcoin/src/blockdata/transaction.rs +++ b/bitcoin/src/blockdata/transaction.rs @@ -20,7 +20,7 @@ use primitives::Sequence; use super::Weight; use crate::consensus::{self, encode, Decodable, Encodable}; use crate::internal_macros::{impl_consensus_encoding, impl_hashencode}; -use crate::locktime::absolute::{self, Height, Time}; +use crate::locktime::absolute::{self, Height, Mtp}; use crate::prelude::{Borrow, Vec}; use crate::script::{Script, ScriptBuf, ScriptExt as _, ScriptExtPriv as _}; #[cfg(doc)] @@ -295,7 +295,7 @@ pub trait TransactionExt: sealed::Sealed { /// By definition if the lock time is not enabled the transaction's absolute timelock is /// considered to be satisfied i.e., there are no timelock constraints restricting this /// transaction from being mined immediately. - fn is_absolute_timelock_satisfied(&self, height: Height, time: Time) -> bool; + fn is_absolute_timelock_satisfied(&self, height: Height, time: Mtp) -> bool; /// Returns `true` if this transactions nLockTime is enabled ([BIP-65]). /// @@ -393,7 +393,7 @@ impl TransactionExt for Transaction { fn is_explicitly_rbf(&self) -> bool { self.input.iter().any(|input| input.sequence.is_rbf()) } - fn is_absolute_timelock_satisfied(&self, height: Height, time: Time) -> bool { + fn is_absolute_timelock_satisfied(&self, height: Height, time: Mtp) -> bool { if !self.is_lock_time_enabled() { return true; } diff --git a/primitives/src/locktime/absolute.rs b/primitives/src/locktime/absolute.rs index 3d5205ac8..fff858a19 100644 --- a/primitives/src/locktime/absolute.rs +++ b/primitives/src/locktime/absolute.rs @@ -16,7 +16,11 @@ use crate::{absolute, Transaction}; #[rustfmt::skip] // Keep public re-exports separate. #[doc(inline)] -pub use units::locktime::absolute::{ConversionError, Height, ParseHeightError, ParseTimeError, Time, LOCK_TIME_THRESHOLD}; +pub use units::locktime::absolute::{ConversionError, Height, ParseHeightError, ParseTimeError, Mtp, LOCK_TIME_THRESHOLD}; + +#[deprecated(since = "TBD", note = "use `Mtp` instead")] +#[doc(hidden)] +pub type Time = Mtp; /// An absolute lock time value, representing either a block height or a UNIX timestamp (seconds /// since epoch). @@ -79,7 +83,7 @@ pub enum LockTime { /// assert!(n.is_block_time()); /// assert_eq!(n.to_consensus_u32(), seconds); /// ``` - Seconds(Time), + Seconds(Mtp), } impl LockTime { @@ -141,9 +145,9 @@ impl LockTime { #[allow(clippy::missing_panics_doc)] pub fn from_consensus(n: u32) -> Self { if units::locktime::absolute::is_block_height(n) { - Self::Blocks(Height::from_consensus(n).expect("n is valid")) + Self::Blocks(Height::from_u32(n).expect("n is valid")) } else { - Self::Seconds(Time::from_consensus(n).expect("n is valid")) + Self::Seconds(Mtp::from_u32(n).expect("n is valid")) } } @@ -166,17 +170,23 @@ impl LockTime { /// ``` #[inline] pub fn from_height(n: u32) -> Result { - let height = Height::from_consensus(n)?; + let height = Height::from_u32(n)?; Ok(LockTime::Blocks(height)) } - /// Constructs a new `LockTime` from `n`, expecting `n` to be a valid block time. + #[inline] + #[deprecated(since = "TBD", note = "use `from_mtp` instead")] + #[doc(hidden)] + pub fn from_time(n: u32) -> Result { Self::from_mtp(n) } + + /// Constructs a new `LockTime` from `n`, expecting `n` to be a median-time-past (MTP) + /// which is in range for a locktime. /// /// # Note /// - /// If the locktime is set to a timestamp `T`, - /// the transaction can be included in a block only if the median time past (MTP) of the - /// last 11 blocks is greater than `T`. + /// If the locktime is set to an MTP `T`, the transaction can be included in a block only if + /// the MTP of last recent 11 blocks is greater than `T`. + /// /// It is possible to broadcast the transaction once the MTP is greater than `T`.[see BIP-113] /// /// [BIP-113 Median time-past as endpoint for lock-time calculations](https://github.com/bitcoin/bips/blob/master/bip-0113.mediawiki) @@ -191,8 +201,8 @@ impl LockTime { /// assert!(absolute::LockTime::from_time(741521).is_err()); /// ``` #[inline] - pub fn from_time(n: u32) -> Result { - let time = Time::from_consensus(n)?; + pub fn from_mtp(n: u32) -> Result { + let time = Mtp::from_u32(n)?; Ok(LockTime::Seconds(time)) } @@ -229,7 +239,7 @@ impl LockTime { /// # use bitcoin_primitives::absolute; /// // Can be implemented if block chain data is available. /// fn get_height() -> absolute::Height { todo!("return the current block height") } - /// fn get_time() -> absolute::Time { todo!("return the current block time") } + /// fn get_time() -> absolute::Mtp { todo!("return the current block time") } /// /// let n = absolute::LockTime::from_consensus(741521); // `n OP_CHEKCLOCKTIMEVERIFY`. /// if n.is_satisfied_by(get_height(), get_time()) { @@ -237,7 +247,7 @@ impl LockTime { /// } /// ```` #[inline] - pub fn is_satisfied_by(self, height: Height, time: Time) -> bool { + pub fn is_satisfied_by(self, height: Height, time: Mtp) -> bool { use LockTime as L; match self { @@ -310,8 +320,8 @@ impl LockTime { #[inline] pub fn to_consensus_u32(self) -> u32 { match self { - LockTime::Blocks(ref h) => h.to_consensus_u32(), - LockTime::Seconds(ref t) => t.to_consensus_u32(), + LockTime::Blocks(ref h) => h.to_u32(), + LockTime::Seconds(ref t) => t.to_u32(), } } } @@ -323,9 +333,9 @@ impl From for LockTime { fn from(h: Height) -> Self { LockTime::Blocks(h) } } -impl From