From f7f1a0be8c698af46e07a058e6ab737469ec68ae Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 8 Apr 2025 14:14:09 +1000 Subject: [PATCH] Add privacy boundary to BlockTime As per policy in #4090 add a privacy boundary to the `BlockTime` type. Use the module name `encapsulate` as is done in `amount` - its private so the name can easily be changed later if needed. Explicitly do not run the formatter or update rustdoc column width so that review is easier. --- units/src/time.rs | 53 ++++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/units/src/time.rs b/units/src/time.rs index bd0cdb556..2534763b9 100644 --- a/units/src/time.rs +++ b/units/src/time.rs @@ -9,34 +9,39 @@ #[cfg(feature = "arbitrary")] use arbitrary::{Arbitrary, Unstructured}; -#[cfg(feature = "serde")] -use serde::{Deserialize, Serialize}; -/// A Bitcoin block timestamp. -/// -/// > Each block contains a Unix time timestamp. In addition to serving as a source of variation for -/// > the block hash, they also make it more difficult for an adversary to manipulate the block chain. -/// > -/// > A timestamp is accepted as valid if it is greater than the median timestamp of previous 11 -/// > blocks, and less than the network-adjusted time + 2 hours. "Network-adjusted time" is the -/// > median of the timestamps returned by all nodes connected to you. As a result block timestamps -/// > are not exactly accurate, and they do not need to be. Block times are accurate only to within -/// > an hour or two. -/// -/// ref: -#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct BlockTime(u32); +mod encapsulate { + #[cfg(feature = "serde")] + use serde::{Deserialize, Serialize}; -impl BlockTime { - /// Constructs a new [`BlockTime`] from an unsigned 32 bit integer value. - #[inline] - pub const fn from_u32(t: u32) -> Self { BlockTime(t) } + /// A Bitcoin block timestamp. + /// + /// > Each block contains a Unix time timestamp. In addition to serving as a source of variation for + /// > the block hash, they also make it more difficult for an adversary to manipulate the block chain. + /// > + /// > A timestamp is accepted as valid if it is greater than the median timestamp of previous 11 + /// > blocks, and less than the network-adjusted time + 2 hours. "Network-adjusted time" is the + /// > median of the timestamps returned by all nodes connected to you. As a result block timestamps + /// > are not exactly accurate, and they do not need to be. Block times are accurate only to within + /// > an hour or two. + /// + /// ref: + #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] + #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] + pub struct BlockTime(u32); - /// Returns the inner `u32` value. - #[inline] - pub const fn to_u32(self) -> u32 { self.0 } + impl BlockTime { + /// Constructs a new [`BlockTime`] from an unsigned 32 bit integer value. + #[inline] + pub const fn from_u32(t: u32) -> Self { BlockTime(t) } + + /// Returns the inner `u32` value. + #[inline] + pub const fn to_u32(self) -> u32 { self.0 } + } } +#[doc(inline)] +pub use encapsulate::BlockTime; impl From for BlockTime { #[inline]