From 826acb827326875f407ba0b7159894f0d8f90d2f Mon Sep 17 00:00:00 2001 From: Andrew Poelstra Date: Fri, 2 May 2025 17:52:38 +0000 Subject: [PATCH] units: rename relative::HeightInterval constructors Rename `value` to `to_height` to be symmetric with `from_height`; deprecate `to_consensus_u32` which had no symmetric `from_consensus_u32` and was only used to implement the corresponding methods in primitives and bitcoin. --- primitives/src/locktime/relative.rs | 4 ++-- primitives/src/sequence.rs | 4 ++-- units/src/block.rs | 2 +- units/src/locktime/relative.rs | 14 ++++++++++++-- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/primitives/src/locktime/relative.rs b/primitives/src/locktime/relative.rs index 386c7c2ed..053e883cf 100644 --- a/primitives/src/locktime/relative.rs +++ b/primitives/src/locktime/relative.rs @@ -132,7 +132,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) => u32::from(h.to_height()), LockTime::Time(ref t) => Sequence::LOCK_TYPE_MASK | u32::from(t.to_512_second_intervals()), } @@ -353,7 +353,7 @@ impl LockTime { use LockTime as L; match self { - L::Blocks(ref required_height) => Ok(required_height.value() <= height.value()), + L::Blocks(required_height) => Ok(required_height <= height), L::Time(time) => Err(IncompatibleHeightError { height, time }), } } diff --git a/primitives/src/sequence.rs b/primitives/src/sequence.rs index f24381575..b659c98e6 100644 --- a/primitives/src/sequence.rs +++ b/primitives/src/sequence.rs @@ -261,8 +261,8 @@ impl<'a> Arbitrary<'a> for Sequence { 1 => Ok(Sequence::ZERO), 2 => Ok(Sequence::MIN_NO_RBF), 3 => Ok(Sequence::ENABLE_LOCKTIME_AND_RBF), - 4 => Ok(Sequence::from_consensus(relative::HeightInterval::MIN.to_consensus_u32())), - 5 => Ok(Sequence::from_consensus(relative::HeightInterval::MAX.to_consensus_u32())), + 4 => Ok(Sequence::from_consensus(u32::from(relative::HeightInterval::MIN.to_height()))), + 5 => Ok(Sequence::from_consensus(u32::from(relative::HeightInterval::MAX.to_height()))), 6 => Ok(Sequence::from_consensus( Sequence::LOCK_TYPE_MASK | u32::from(relative::MtpInterval::MIN.to_512_second_intervals()), diff --git a/units/src/block.rs b/units/src/block.rs index 8c60e0d58..000f0995b 100644 --- a/units/src/block.rs +++ b/units/src/block.rs @@ -134,7 +134,7 @@ impl From for BlockInterval { /// /// A relative locktime block height has a maximum value of `u16::MAX` where as a /// [`BlockInterval`] is a thin wrapper around a `u32`, the two types are not interchangeable. - fn from(h: relative::HeightInterval) -> Self { Self::from_u32(h.value().into()) } + fn from(h: relative::HeightInterval) -> Self { Self::from_u32(h.to_height().into()) } } impl TryFrom for relative::HeightInterval { diff --git a/units/src/locktime/relative.rs b/units/src/locktime/relative.rs index 96dfc4fa6..47dde70aa 100644 --- a/units/src/locktime/relative.rs +++ b/units/src/locktime/relative.rs @@ -34,14 +34,24 @@ impl HeightInterval { #[inline] pub const fn from_height(blocks: u16) -> Self { Self(blocks) } + /// Express the [`Height`] as a count of blocks. + #[inline] + #[must_use] + pub const fn to_height(self) -> u16 { self.0 } + /// Returns the inner `u16` value. #[inline] #[must_use] + #[deprecated(since = "TBD", note = "use `to_height` instead")] + #[doc(hidden)] pub const fn value(self) -> u16 { self.0 } /// Returns the `u32` value used to encode this locktime in an nSequence field or /// argument to `OP_CHECKSEQUENCEVERIFY`. - #[inline] + #[deprecated( + since = "TBD", + note = "use `LockTime::from` followed by `to_consensus_u32` instead" + )] pub const fn to_consensus_u32(self) -> u32 { self.0 as u32 // cast safety: u32 is wider than u16 on all architectures } @@ -60,7 +70,7 @@ impl HeightInterval { pub fn is_satisfied_by(self, chain_tip: MtpAndHeight, utxo_mined_at: MtpAndHeight) -> bool { // let chain_tip_height = BlockHeight::from(chain_tip); // let utxo_mined_at_height = BlockHeight::from(utxo_mined_at); - match self.to_consensus_u32().checked_add(utxo_mined_at.to_height().to_u32()) { + match u32::from(self.to_height()).checked_add(utxo_mined_at.to_height().to_u32()) { Some(target_height) => chain_tip.to_height().to_u32() >= target_height, None => false, }