diff --git a/units/src/lib.rs b/units/src/lib.rs index e5f5dda25..4625a7cb6 100644 --- a/units/src/lib.rs +++ b/units/src/lib.rs @@ -48,7 +48,6 @@ pub mod amount; pub mod block; pub mod fee_rate; pub mod locktime; -pub mod mtp_height; pub mod parse; pub mod time; pub mod weight; diff --git a/units/src/mtp_height.rs b/units/src/mtp_height.rs deleted file mode 100644 index 297001083..000000000 --- a/units/src/mtp_height.rs +++ /dev/null @@ -1,64 +0,0 @@ -// SPDX-License-Identifier: CC0-1.0 - -//! Median Time Past (MTP) and height - used for working lock times. - -use crate::{BlockHeight, BlockMtp, BlockTime}; - -/// A structure containing both Median Time Past (MTP) and current -/// absolute block height, used for validating relative locktimes. -#[derive(Copy, Clone, Debug, PartialEq, Eq)] -pub struct MtpAndHeight { - /// The Median Time Past (median of the last 11 blocks' timestamps) - mtp: BlockMtp, - /// The current block height, - height: BlockHeight, -} - -impl MtpAndHeight { - /// Constructs an [`MtpAndHeight`] by computing the median‐time‐past from the last 11 block timestamps - /// - /// # Parameters - /// - /// * `height` - The absolute height of the chain tip - /// * `timestamps` - An array of timestamps from the most recent 11 blocks, where - /// - `timestamps[0]` is the timestamp at height `height - 10` - /// - `timestamps[1]` is the timestamp at height `height - 9` - /// - … - /// - `timestamps[10]` is the timestamp at height `height` - pub fn new(height: BlockHeight, timestamps: [BlockTime; 11]) -> Self { - MtpAndHeight { mtp: BlockMtp::new(timestamps), height } - } - - /// Returns the median-time-past component. - pub fn to_mtp(self) -> BlockMtp { self.mtp } - - /// Returns the block-height component. - pub fn to_height(self) -> BlockHeight { self.height } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn valid_chain_computes_mtp() { - let height = BlockHeight::from_u32(100); - let timestamps = [ - BlockTime::from_u32(10), - BlockTime::from_u32(3), - BlockTime::from_u32(5), - BlockTime::from_u32(8), - BlockTime::from_u32(1), - BlockTime::from_u32(4), - BlockTime::from_u32(6), - BlockTime::from_u32(9), - BlockTime::from_u32(2), - BlockTime::from_u32(7), - BlockTime::from_u32(0), - ]; - - let result = MtpAndHeight::new(height, timestamps); - assert_eq!(result.height, height); - assert_eq!(result.mtp.to_u32(), 5); - } -}