From 8ffcd2cf30d3418ae69244f75f948d476b087125 Mon Sep 17 00:00:00 2001 From: Andrew Poelstra Date: Thu, 1 May 2025 17:20:54 +0000 Subject: [PATCH] units: rename absolute::Time to absolute::Mtp This is not a generic UNIX timestamp, but rather a MTP restricted to have values between 500 million and u32::MAX. Most importantly, it is *not* a blocktime, which is what is implied by its name and constructors. --- bitcoin/src/blockdata/mod.rs | 6 +- bitcoin/src/blockdata/transaction.rs | 6 +- primitives/src/locktime/absolute.rs | 28 +++++---- units/src/locktime/absolute.rs | 85 +++++++++++++++------------- units/tests/api.rs | 10 ++-- units/tests/serde.rs | 4 +- 6 files changed, 77 insertions(+), 62 deletions(-) diff --git a/bitcoin/src/blockdata/mod.rs b/bitcoin/src/blockdata/mod.rs index ab3b67463..36b4ce410 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] 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 d6434ed83..6ea4d3c08 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 { @@ -143,7 +147,7 @@ impl LockTime { if units::locktime::absolute::is_block_height(n) { Self::Blocks(Height::from_consensus(n).expect("n is valid")) } else { - Self::Seconds(Time::from_consensus(n).expect("n is valid")) + Self::Seconds(Mtp::from_consensus(n).expect("n is valid")) } } @@ -192,7 +196,7 @@ impl LockTime { /// ``` #[inline] pub fn from_time(n: u32) -> Result { - let time = Time::from_consensus(n)?; + let time = Mtp::from_consensus(n)?; Ok(LockTime::Seconds(time)) } @@ -229,7 +233,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 +241,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 { @@ -317,9 +321,9 @@ impl From for LockTime { fn from(h: Height) -> Self { LockTime::Blocks(h) } } -impl From