units: change type of MtpHeight::to_mtp to BlockMtp

We are going to delete MtpHeight in a couple commits, but to let us do
the transition with smaller diffs, we will first change it to be easily
convertible to a BlockHeight/BlockMtp pair.
This commit is contained in:
Andrew Poelstra 2025-05-06 15:39:40 +00:00
parent dcbdb7ca8a
commit d933c754f5
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 4 additions and 8 deletions

View File

@ -2,14 +2,14 @@
//! Median Time Past (MTP) and height - used for working lock times. //! Median Time Past (MTP) and height - used for working lock times.
use crate::{BlockHeight, BlockTime}; use crate::{BlockHeight, BlockMtp, BlockTime};
/// A structure containing both Median Time Past (MTP) and current /// A structure containing both Median Time Past (MTP) and current
/// absolute block height, used for validating relative locktimes. /// absolute block height, used for validating relative locktimes.
#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct MtpAndHeight { pub struct MtpAndHeight {
/// The Median Time Past (median of the last 11 blocks' timestamps) /// The Median Time Past (median of the last 11 blocks' timestamps)
mtp: BlockTime, mtp: BlockMtp,
/// The current block height, /// The current block height,
height: BlockHeight, height: BlockHeight,
} }
@ -26,15 +26,11 @@ impl MtpAndHeight {
/// - … /// - …
/// - `timestamps[10]` is the timestamp at height `height` /// - `timestamps[10]` is the timestamp at height `height`
pub fn new(height: BlockHeight, timestamps: [BlockTime; 11]) -> Self { pub fn new(height: BlockHeight, timestamps: [BlockTime; 11]) -> Self {
let mut mtp_timestamps = timestamps; MtpAndHeight { mtp: BlockMtp::new(timestamps), height }
mtp_timestamps.sort_unstable();
let mtp = mtp_timestamps[5];
MtpAndHeight { mtp, height }
} }
/// Returns the median-time-past component. /// Returns the median-time-past component.
pub fn to_mtp(self) -> BlockTime { self.mtp } pub fn to_mtp(self) -> BlockMtp { self.mtp }
/// Returns the block-height component. /// Returns the block-height component.
pub fn to_height(self) -> BlockHeight { self.height } pub fn to_height(self) -> BlockHeight { self.height }