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:
parent
39b4f7670d
commit
826acb8273
|
@ -132,7 +132,7 @@ impl LockTime {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn to_consensus_u32(self) -> u32 {
|
pub fn to_consensus_u32(self) -> u32 {
|
||||||
match self {
|
match self {
|
||||||
LockTime::Blocks(ref h) => h.to_consensus_u32(),
|
LockTime::Blocks(ref h) => u32::from(h.to_height()),
|
||||||
LockTime::Time(ref t) =>
|
LockTime::Time(ref t) =>
|
||||||
Sequence::LOCK_TYPE_MASK | u32::from(t.to_512_second_intervals()),
|
Sequence::LOCK_TYPE_MASK | u32::from(t.to_512_second_intervals()),
|
||||||
}
|
}
|
||||||
|
@ -353,7 +353,7 @@ impl LockTime {
|
||||||
use LockTime as L;
|
use LockTime as L;
|
||||||
|
|
||||||
match self {
|
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 }),
|
L::Time(time) => Err(IncompatibleHeightError { height, time }),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -261,8 +261,8 @@ impl<'a> Arbitrary<'a> for Sequence {
|
||||||
1 => Ok(Sequence::ZERO),
|
1 => Ok(Sequence::ZERO),
|
||||||
2 => Ok(Sequence::MIN_NO_RBF),
|
2 => Ok(Sequence::MIN_NO_RBF),
|
||||||
3 => Ok(Sequence::ENABLE_LOCKTIME_AND_RBF),
|
3 => Ok(Sequence::ENABLE_LOCKTIME_AND_RBF),
|
||||||
4 => Ok(Sequence::from_consensus(relative::HeightInterval::MIN.to_consensus_u32())),
|
4 => Ok(Sequence::from_consensus(u32::from(relative::HeightInterval::MIN.to_height()))),
|
||||||
5 => Ok(Sequence::from_consensus(relative::HeightInterval::MAX.to_consensus_u32())),
|
5 => Ok(Sequence::from_consensus(u32::from(relative::HeightInterval::MAX.to_height()))),
|
||||||
6 => Ok(Sequence::from_consensus(
|
6 => Ok(Sequence::from_consensus(
|
||||||
Sequence::LOCK_TYPE_MASK
|
Sequence::LOCK_TYPE_MASK
|
||||||
| u32::from(relative::MtpInterval::MIN.to_512_second_intervals()),
|
| u32::from(relative::MtpInterval::MIN.to_512_second_intervals()),
|
||||||
|
|
|
@ -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
|
/// 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.
|
/// [`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 {
|
impl TryFrom<BlockInterval> for relative::HeightInterval {
|
||||||
|
|
|
@ -34,14 +34,24 @@ impl HeightInterval {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub const fn from_height(blocks: u16) -> Self { Self(blocks) }
|
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.
|
/// Returns the inner `u16` value.
|
||||||
#[inline]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
|
#[deprecated(since = "TBD", note = "use `to_height` instead")]
|
||||||
|
#[doc(hidden)]
|
||||||
pub const fn value(self) -> u16 { self.0 }
|
pub const fn value(self) -> u16 { self.0 }
|
||||||
|
|
||||||
/// Returns the `u32` value used to encode this locktime in an nSequence field or
|
/// Returns the `u32` value used to encode this locktime in an nSequence field or
|
||||||
/// argument to `OP_CHECKSEQUENCEVERIFY`.
|
/// 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 {
|
pub const fn to_consensus_u32(self) -> u32 {
|
||||||
self.0 as u32 // cast safety: u32 is wider than u16 on all architectures
|
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 {
|
pub fn is_satisfied_by(self, chain_tip: MtpAndHeight, utxo_mined_at: MtpAndHeight) -> bool {
|
||||||
// let chain_tip_height = BlockHeight::from(chain_tip);
|
// let chain_tip_height = BlockHeight::from(chain_tip);
|
||||||
// let utxo_mined_at_height = BlockHeight::from(utxo_mined_at);
|
// 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,
|
Some(target_height) => chain_tip.to_height().to_u32() >= target_height,
|
||||||
None => false,
|
None => false,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue