diff --git a/primitives/src/locktime/absolute.rs b/primitives/src/locktime/absolute.rs index 242536a1d..9ce1b64e0 100644 --- a/primitives/src/locktime/absolute.rs +++ b/primitives/src/locktime/absolute.rs @@ -145,7 +145,7 @@ impl LockTime { #[allow(clippy::missing_panics_doc)] pub fn from_consensus(n: u32) -> Self { if units::locktime::absolute::is_block_height(n) { - Self::Blocks(Height::from_consensus(n).expect("n is valid")) + Self::Blocks(Height::from_u32(n).expect("n is valid")) } else { Self::Seconds(Mtp::from_u32(n).expect("n is valid")) } @@ -170,7 +170,7 @@ impl LockTime { /// ``` #[inline] pub fn from_height(n: u32) -> Result { - let height = Height::from_consensus(n)?; + let height = Height::from_u32(n)?; Ok(LockTime::Blocks(height)) } @@ -314,7 +314,7 @@ impl LockTime { #[inline] pub fn to_consensus_u32(self) -> u32 { match self { - LockTime::Blocks(ref h) => h.to_consensus_u32(), + LockTime::Blocks(ref h) => h.to_u32(), LockTime::Seconds(ref t) => t.to_u32(), } } @@ -499,9 +499,9 @@ mod tests { #[test] fn satisfied_by_height() { - let height_below = Height::from_consensus(700_000).unwrap(); - let height = Height::from_consensus(750_000).unwrap(); - let height_above = Height::from_consensus(800_000).unwrap(); + let height_below = Height::from_u32(700_000).unwrap(); + let height = Height::from_u32(750_000).unwrap(); + let height_above = Height::from_u32(800_000).unwrap(); let lock_by_height = LockTime::from(height); @@ -521,7 +521,7 @@ mod tests { let lock_by_time = LockTime::from(time); - let height = Height::from_consensus(800_000).unwrap(); + let height = Height::from_u32(800_000).unwrap(); assert!(!lock_by_time.is_satisfied_by(height, time_before)); assert!(lock_by_time.is_satisfied_by(height, time)); diff --git a/units/src/block.rs b/units/src/block.rs index 5d0dc90b8..b28c475e1 100644 --- a/units/src/block.rs +++ b/units/src/block.rs @@ -70,7 +70,7 @@ impl From for BlockHeight { /// An absolute locktime block height has a maximum value of [`absolute::LOCK_TIME_THRESHOLD`] /// (500,000,000) where as a [`BlockHeight`] is a thin wrapper around a `u32`, the two types are /// not interchangeable. - fn from(h: absolute::Height) -> Self { Self::from_u32(h.to_consensus_u32()) } + fn from(h: absolute::Height) -> Self { Self::from_u32(h.to_u32()) } } impl TryFrom for absolute::Height { @@ -82,7 +82,7 @@ impl TryFrom for absolute::Height { /// (500,000,000) where as a [`BlockHeight`] is a thin wrapper around a `u32`, the two types are /// not interchangeable. fn try_from(h: BlockHeight) -> Result { - absolute::Height::from_consensus(h.to_u32()) + absolute::Height::from_u32(h.to_u32()) } } diff --git a/units/src/locktime/absolute.rs b/units/src/locktime/absolute.rs index 81e090e87..1df4b04cf 100644 --- a/units/src/locktime/absolute.rs +++ b/units/src/locktime/absolute.rs @@ -45,15 +45,22 @@ impl Height { /// # Errors /// /// If the input string is not a valid hex representation of a block height. - pub fn from_hex(s: &str) -> Result { - parse_hex(s, Self::from_consensus) - } + pub fn from_hex(s: &str) -> Result { parse_hex(s, Self::from_u32) } - /// Constructs a new block height. + #[deprecated(since = "TBD", note = "use `from_u32` instead")] + #[doc(hidden)] + pub const fn from_consensus(n: u32) -> Result { Self::from_u32(n) } + + #[deprecated(since = "TBD", note = "use `to_u32` instead")] + #[doc(hidden)] + pub const fn to_consensus_u32(self) -> u32 { self.to_u32() } + + /// Constructs a new block height directly from a `u32` value. /// /// # Errors /// - /// If `n` does not represent a valid block height value. + /// If `n` does not represent a block height within the valid range for a locktime: + /// `[0, 499_999_999]`. /// /// # Examples /// @@ -61,11 +68,11 @@ impl Height { /// use bitcoin_units::locktime::absolute::Height; /// /// let h: u32 = 741521; - /// let height = Height::from_consensus(h).expect("invalid height value"); + /// let height = Height::from_u32(h).expect("invalid height value"); /// assert_eq!(height.to_consensus_u32(), h); /// ``` #[inline] - pub const fn from_consensus(n: u32) -> Result { + pub const fn from_u32(n: u32) -> Result { if is_block_height(n) { Ok(Self(n)) } else { @@ -73,16 +80,16 @@ impl Height { } } - /// Converts this [`Height`] to its inner `u32` value. + /// Converts this [`Height`] to a raw `u32` value. #[inline] - pub const fn to_consensus_u32(self) -> u32 { self.0 } + pub const fn to_u32(self) -> u32 { self.0 } } impl fmt::Display for Height { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(&self.0, f) } } -crate::impl_parse_str!(Height, ParseHeightError, parser(Height::from_consensus)); +crate::impl_parse_str!(Height, ParseHeightError, parser(Height::from_u32)); /// Error returned when parsing block height fails. #[derive(Debug, Clone, Eq, PartialEq)] @@ -111,7 +118,7 @@ impl<'de> serde::Deserialize<'de> for Height { D: serde::Deserializer<'de>, { let u = u32::deserialize(deserializer)?; - Height::from_consensus(u).map_err(serde::de::Error::custom) + Height::from_u32(u).map_err(serde::de::Error::custom) } } @@ -121,7 +128,7 @@ impl serde::Serialize for Height { where S: serde::Serializer, { - self.to_consensus_u32().serialize(serializer) + self.to_u32().serialize(serializer) } } @@ -158,7 +165,7 @@ impl Mtp { #[deprecated(since = "TBD", note = "use `from_u32` instead")] #[doc(hidden)] - pub const fn from_consensus(n: u32) -> Result { Self::from_u32(n) } + pub const fn from_consensus(n: u32) -> Result { Self::from_u32(n) } #[deprecated(since = "TBD", note = "use `to_u32` instead")] #[doc(hidden)] @@ -183,7 +190,7 @@ impl Mtp { /// assert_eq!(time.to_consensus_u32(), t); /// ``` #[inline] - pub const fn from_u32(n: u32) -> Result { + pub const fn from_u32(n: u32) -> Result { if is_block_time(n) { Ok(Self(n)) } else { @@ -425,10 +432,10 @@ impl<'a> Arbitrary<'a> for Height { 0 => Ok(Height::MIN), 1 => Ok(Height::MAX), _ => { - let min = Height::MIN.to_consensus_u32(); - let max = Height::MAX.to_consensus_u32(); + let min = Height::MIN.to_u32(); + let max = Height::MAX.to_u32(); let h = u.int_in_range(min..=max)?; - Ok(Height::from_consensus(h).unwrap()) + Ok(Height::from_u32(h).unwrap()) } } }