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.
This commit is contained in:
Andrew Poelstra 2025-05-02 17:52:38 +00:00
parent 39b4f7670d
commit 826acb8273
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
4 changed files with 17 additions and 7 deletions

View File

@ -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 }),
}
}

View File

@ -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()),

View File

@ -134,7 +134,7 @@ impl From<relative::HeightInterval> 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<BlockInterval> for relative::HeightInterval {

View File

@ -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,
}