Skip to content

Commit 3a6a399

Browse files
committed
Merge rust-bitcoin#4462: Do lock time renames
dca4266 units: Fix rustdoc column width (Tobin C. Harding) d557caf Run the formatter (Tobin C. Harding) 7c2115b Rename MtpInterval to NumberOf512Seconds (Tobin C. Harding) 3a97ea2 Rename HeightInterval to NumberOfBlocks (Tobin C. Harding) c3b7457 Rename Mtp to MedianTimePast (Tobin C. Harding) b38d225 Run the formatter (Tobin C. Harding) Pull request description: Naming things is hard, naming lock time things is _really_ hard. This is another attempt at improving the very low level types in the `units::locktime` modules. Formatting done separately so that the diffs are easier to tab through. ACKs for top commit: apoelstra: ACK dca4266; successfully ran local tests Tree-SHA512: 24d1b0cae3d7f926622177f00aedc50c53164e04f9d400f9c2c009157f36fd1f55ac2d1b70ceb10b0a165130ffe7eef25d64dc6ea5e6f3caec812c39ab0bbd66
2 parents dc499b0 + dca4266 commit 3a6a399

File tree

12 files changed

+255
-246
lines changed

12 files changed

+255
-246
lines changed

bitcoin/src/blockdata/block.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ use crate::transaction::{Transaction, TransactionExt as _, Wtxid};
3030
#[doc(inline)]
3131
pub use primitives::block::{Block, Checked, Unchecked, Validation, Version, BlockHash, Header, WitnessCommitment};
3232
#[doc(inline)]
33-
pub use units::block::{
34-
BlockHeight, BlockHeightInterval, TooBigForRelativeBlockHeightIntervalError,
35-
};
33+
pub use units::block::{BlockHeight, BlockHeightInterval, TooBigForRelativeHeightError};
3634

3735
#[deprecated(since = "TBD", note = "use `BlockHeightInterval` instead")]
3836
#[doc(hidden)]

bitcoin/src/blockdata/mod.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ pub mod locktime {
4141

4242
/// Re-export everything from the `primitives::locktime::absolute` module.
4343
#[rustfmt::skip] // Keep public re-exports separate.
44-
pub use primitives::locktime::absolute::{ConversionError, Height, LockTime, ParseHeightError, ParseTimeError, Mtp};
44+
pub use primitives::locktime::absolute::{ConversionError, Height, LockTime, ParseHeightError, ParseTimeError, MedianTimePast};
4545

46-
#[deprecated(since = "TBD", note = "use `Mtp` instead")]
46+
#[deprecated(since = "TBD", note = "use `MedianTimePast` instead")]
4747
#[doc(hidden)]
48-
pub type Time = Mtp;
48+
pub type Time = MedianTimePast;
4949

5050
impl Encodable for LockTime {
5151
#[inline]
@@ -71,17 +71,17 @@ pub mod locktime {
7171
7272
/// Re-export everything from the `primitives::locktime::relative` module.
7373
pub use primitives::locktime::relative::{
74-
DisabledLockTimeError, HeightInterval, IncompatibleHeightError, IncompatibleTimeError,
75-
LockTime, MtpInterval, TimeOverflowError,
74+
DisabledLockTimeError, IncompatibleHeightError, IncompatibleTimeError, LockTime,
75+
NumberOf512Seconds, NumberOfBlocks, TimeOverflowError,
7676
};
7777

78-
#[deprecated(since = "TBD", note = "use `Mtp` instead")]
78+
#[deprecated(since = "TBD", note = "use `NumberOfBlocks` instead")]
7979
#[doc(hidden)]
80-
pub type Height = HeightInterval;
80+
pub type Height = NumberOfBlocks;
8181

82-
#[deprecated(since = "TBD", note = "use `Mtp` instead")]
82+
#[deprecated(since = "TBD", note = "use `NumberOf512Seconds` instead")]
8383
#[doc(hidden)]
84-
pub type Time = MtpInterval;
84+
pub type Time = NumberOf512Seconds;
8585
}
8686
}
8787

bitcoin/src/blockdata/transaction.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use primitives::Sequence;
2020
use super::Weight;
2121
use crate::consensus::{self, encode, Decodable, Encodable};
2222
use crate::internal_macros::{impl_consensus_encoding, impl_hashencode};
23-
use crate::locktime::absolute::{self, Height, Mtp};
23+
use crate::locktime::absolute::{self, Height, MedianTimePast};
2424
use crate::prelude::{Borrow, Vec};
2525
use crate::script::{Script, ScriptBuf, ScriptExt as _, ScriptExtPriv as _};
2626
#[cfg(doc)]
@@ -295,7 +295,7 @@ pub trait TransactionExt: sealed::Sealed {
295295
/// By definition if the lock time is not enabled the transaction's absolute timelock is
296296
/// considered to be satisfied i.e., there are no timelock constraints restricting this
297297
/// transaction from being mined immediately.
298-
fn is_absolute_timelock_satisfied(&self, height: Height, time: Mtp) -> bool;
298+
fn is_absolute_timelock_satisfied(&self, height: Height, time: MedianTimePast) -> bool;
299299

300300
/// Returns `true` if this transactions nLockTime is enabled ([BIP-65]).
301301
///
@@ -393,7 +393,7 @@ impl TransactionExt for Transaction {
393393

394394
fn is_explicitly_rbf(&self) -> bool { self.input.iter().any(|input| input.sequence.is_rbf()) }
395395

396-
fn is_absolute_timelock_satisfied(&self, height: Height, time: Mtp) -> bool {
396+
fn is_absolute_timelock_satisfied(&self, height: Height, time: MedianTimePast) -> bool {
397397
if !self.is_lock_time_enabled() {
398398
return true;
399399
}

primitives/src/locktime/absolute.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ use crate::{absolute, Transaction};
1616

1717
#[rustfmt::skip] // Keep public re-exports separate.
1818
#[doc(inline)]
19-
pub use units::locktime::absolute::{ConversionError, Height, ParseHeightError, ParseTimeError, Mtp, LOCK_TIME_THRESHOLD};
19+
pub use units::locktime::absolute::{ConversionError, Height, ParseHeightError, ParseTimeError, MedianTimePast, LOCK_TIME_THRESHOLD};
2020

21-
#[deprecated(since = "TBD", note = "use `Mtp` instead")]
21+
#[deprecated(since = "TBD", note = "use `MedianTimePast` instead")]
2222
#[doc(hidden)]
23-
pub type Time = Mtp;
23+
pub type Time = MedianTimePast;
2424

2525
/// An absolute lock time value, representing either a block height or a UNIX timestamp (seconds
2626
/// since epoch).
@@ -83,7 +83,7 @@ pub enum LockTime {
8383
/// assert!(n.is_block_time());
8484
/// assert_eq!(n.to_consensus_u32(), seconds);
8585
/// ```
86-
Seconds(Mtp),
86+
Seconds(MedianTimePast),
8787
}
8888

8989
impl LockTime {
@@ -147,7 +147,7 @@ impl LockTime {
147147
if units::locktime::absolute::is_block_height(n) {
148148
Self::Blocks(Height::from_u32(n).expect("n is valid"))
149149
} else {
150-
Self::Seconds(Mtp::from_u32(n).expect("n is valid"))
150+
Self::Seconds(MedianTimePast::from_u32(n).expect("n is valid"))
151151
}
152152
}
153153

@@ -202,7 +202,7 @@ impl LockTime {
202202
/// ```
203203
#[inline]
204204
pub fn from_mtp(n: u32) -> Result<Self, ConversionError> {
205-
let time = Mtp::from_u32(n)?;
205+
let time = MedianTimePast::from_u32(n)?;
206206
Ok(LockTime::Seconds(time))
207207
}
208208

@@ -239,15 +239,15 @@ impl LockTime {
239239
/// # use bitcoin_primitives::absolute;
240240
/// // Can be implemented if block chain data is available.
241241
/// fn get_height() -> absolute::Height { todo!("return the current block height") }
242-
/// fn get_time() -> absolute::Mtp { todo!("return the current block time") }
242+
/// fn get_time() -> absolute::MedianTimePast { todo!("return the current block time") }
243243
///
244244
/// let n = absolute::LockTime::from_consensus(741521); // `n OP_CHEKCLOCKTIMEVERIFY`.
245245
/// if n.is_satisfied_by(get_height(), get_time()) {
246246
/// // Can create and mine a transaction that satisfies the OP_CLTV timelock constraint.
247247
/// }
248248
/// ````
249249
#[inline]
250-
pub fn is_satisfied_by(self, height: Height, time: Mtp) -> bool {
250+
pub fn is_satisfied_by(self, height: Height, time: MedianTimePast) -> bool {
251251
use LockTime as L;
252252

253253
match self {
@@ -333,9 +333,9 @@ impl From<Height> for LockTime {
333333
fn from(h: Height) -> Self { LockTime::Blocks(h) }
334334
}
335335

336-
impl From<Mtp> for LockTime {
336+
impl From<MedianTimePast> for LockTime {
337337
#[inline]
338-
fn from(t: Mtp) -> Self { LockTime::Seconds(t) }
338+
fn from(t: MedianTimePast) -> Self { LockTime::Seconds(t) }
339339
}
340340

341341
impl fmt::Debug for LockTime {
@@ -512,7 +512,7 @@ mod tests {
512512
let lock_by_height = LockTime::from(height);
513513

514514
let t: u32 = 1_653_195_600; // May 22nd, 5am UTC.
515-
let time = Mtp::from_u32(t).unwrap();
515+
let time = MedianTimePast::from_u32(t).unwrap();
516516

517517
assert!(!lock_by_height.is_satisfied_by(height_below, time));
518518
assert!(lock_by_height.is_satisfied_by(height, time));
@@ -521,9 +521,9 @@ mod tests {
521521

522522
#[test]
523523
fn satisfied_by_time() {
524-
let time_before = Mtp::from_u32(1_653_109_200).unwrap(); // "May 21th 2022, 5am UTC.
525-
let time = Mtp::from_u32(1_653_195_600).unwrap(); // "May 22nd 2022, 5am UTC.
526-
let time_after = Mtp::from_u32(1_653_282_000).unwrap(); // "May 23rd 2022, 5am UTC.
524+
let time_before = MedianTimePast::from_u32(1_653_109_200).unwrap(); // "May 21th 2022, 5am UTC.
525+
let time = MedianTimePast::from_u32(1_653_195_600).unwrap(); // "May 22nd 2022, 5am UTC.
526+
let time_after = MedianTimePast::from_u32(1_653_282_000).unwrap(); // "May 23rd 2022, 5am UTC.
527527

528528
let lock_by_time = LockTime::from(time);
529529

0 commit comments

Comments
 (0)