From 3a97ea2259761f9cc208f4ef953ae322b7f5b10b Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 8 May 2025 04:48:52 +1000 Subject: [PATCH] Rename HeightInterval to NumberOfBlocks Name this type exactly what it is. Note for the error we just use 'height' even though this is a bit stale but the general concept is ok in the error type because the name is long already. --- bitcoin/src/blockdata/block.rs | 2 +- bitcoin/src/blockdata/mod.rs | 6 ++-- primitives/src/locktime/relative.rs | 56 ++++++++++++++--------------- primitives/src/sequence.rs | 8 ++--- units/src/block.rs | 30 ++++++++-------- units/src/locktime/relative.rs | 40 ++++++++++----------- units/tests/api.rs | 4 +-- 7 files changed, 73 insertions(+), 73 deletions(-) diff --git a/bitcoin/src/blockdata/block.rs b/bitcoin/src/blockdata/block.rs index 3f2b86fa7..57a456b07 100644 --- a/bitcoin/src/blockdata/block.rs +++ b/bitcoin/src/blockdata/block.rs @@ -31,7 +31,7 @@ use crate::transaction::{Transaction, TransactionExt as _, Wtxid}; pub use primitives::block::{Block, Checked, Unchecked, Validation, Version, BlockHash, Header, WitnessCommitment}; #[doc(inline)] pub use units::block::{ - BlockHeight, BlockHeightInterval, TooBigForRelativeBlockHeightIntervalError, + BlockHeight, BlockHeightInterval, TooBigForRelativeHeightError, }; #[deprecated(since = "TBD", note = "use `BlockHeightInterval` instead")] diff --git a/bitcoin/src/blockdata/mod.rs b/bitcoin/src/blockdata/mod.rs index fa6f1502e..8e8f8757d 100644 --- a/bitcoin/src/blockdata/mod.rs +++ b/bitcoin/src/blockdata/mod.rs @@ -71,13 +71,13 @@ pub mod locktime { /// Re-export everything from the `primitives::locktime::relative` module. pub use primitives::locktime::relative::{ - DisabledLockTimeError, HeightInterval, IncompatibleHeightError, IncompatibleTimeError, + DisabledLockTimeError, NumberOfBlocks, IncompatibleHeightError, IncompatibleTimeError, LockTime, MtpInterval, TimeOverflowError, }; - #[deprecated(since = "TBD", note = "use `Mtp` instead")] + #[deprecated(since = "TBD", note = "use `NumberOfBlocks` instead")] #[doc(hidden)] - pub type Height = HeightInterval; + pub type Height = NumberOfBlocks; #[deprecated(since = "TBD", note = "use `Mtp` instead")] #[doc(hidden)] diff --git a/primitives/src/locktime/relative.rs b/primitives/src/locktime/relative.rs index b38e2e16c..d1ffd806e 100644 --- a/primitives/src/locktime/relative.rs +++ b/primitives/src/locktime/relative.rs @@ -13,12 +13,12 @@ use crate::{relative, TxIn}; #[rustfmt::skip] // Keep public re-exports separate. #[doc(inline)] -pub use units::locktime::relative::{HeightInterval, MtpInterval, TimeOverflowError}; +pub use units::locktime::relative::{NumberOfBlocks, MtpInterval, TimeOverflowError}; use units::{BlockHeight, BlockMtp}; -#[deprecated(since = "TBD", note = "use `Mtp` instead")] +#[deprecated(since = "TBD", note = "use `NumberOfBlocks` instead")] #[doc(hidden)] -pub type Height = HeightInterval; +pub type Height = NumberOfBlocks; #[deprecated(since = "TBD", note = "use `Mtp` instead")] #[doc(hidden)] @@ -77,7 +77,7 @@ pub type Time = MtpInterval; #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum LockTime { /// A block height lock time value. - Blocks(HeightInterval), + Blocks(NumberOfBlocks), /// A 512 second time interval value. Time(MtpInterval), } @@ -85,7 +85,7 @@ pub enum LockTime { impl LockTime { /// A relative locktime of 0 is always valid, and is assumed valid for inputs that /// are not yet confirmed. - pub const ZERO: LockTime = LockTime::Blocks(HeightInterval::ZERO); + pub const ZERO: LockTime = LockTime::Blocks(NumberOfBlocks::ZERO); /// The number of bytes that the locktime contributes to the size of a transaction. pub const SIZE: usize = 4; // Serialized length of a u32. @@ -164,7 +164,7 @@ impl LockTime { /// Constructs a new `LockTime` from `n`, expecting `n` to be a 16-bit count of blocks. #[inline] - pub const fn from_height(n: u16) -> Self { LockTime::Blocks(HeightInterval::from_height(n)) } + pub const fn from_height(n: u16) -> Self { LockTime::Blocks(NumberOfBlocks::from_height(n)) } /// Constructs a new `LockTime` from `n`, expecting `n` to be a count of 512-second intervals. /// @@ -332,7 +332,7 @@ impl LockTime { } } - /// Returns true if this [`relative::LockTime`] is satisfied by [`HeightInterval`]. + /// Returns true if this [`relative::LockTime`] is satisfied by [`NumberOfBlocks`]. /// /// # Errors /// @@ -346,12 +346,12 @@ impl LockTime { /// /// let required_height: u16 = 100; /// let lock = Sequence::from_height(required_height).to_relative_lock_time().expect("valid height"); - /// assert!(lock.is_satisfied_by_height(relative::HeightInterval::from(required_height + 1)).expect("a height")); + /// assert!(lock.is_satisfied_by_height(relative::NumberOfBlocks::from(required_height + 1)).expect("a height")); /// ``` #[inline] pub fn is_satisfied_by_height( self, - height: HeightInterval, + height: NumberOfBlocks, ) -> Result { use LockTime as L; @@ -388,9 +388,9 @@ impl LockTime { } } -impl From for LockTime { +impl From for LockTime { #[inline] - fn from(h: HeightInterval) -> Self { LockTime::Blocks(h) } + fn from(h: NumberOfBlocks) -> Self { LockTime::Blocks(h) } } impl From for LockTime { @@ -455,14 +455,14 @@ impl std::error::Error for DisabledLockTimeError {} #[derive(Debug, Clone, PartialEq, Eq)] pub struct IncompatibleHeightError { /// Attempted to satisfy a lock-by-blocktime lock with this height. - height: HeightInterval, + height: NumberOfBlocks, /// The inner time value of the lock-by-blocktime lock. time: MtpInterval, } impl IncompatibleHeightError { /// Returns the height that was erroneously used to try and satisfy a lock-by-blocktime lock. - pub fn incompatible(&self) -> HeightInterval { self.height } + pub fn incompatible(&self) -> NumberOfBlocks { self.height } /// Returns the time value of the lock-by-blocktime lock. pub fn expected(&self) -> MtpInterval { self.time } @@ -488,7 +488,7 @@ pub struct IncompatibleTimeError { /// Attempted to satisfy a lock-by-blockheight lock with this time. time: MtpInterval, /// The inner height value of the lock-by-blockheight lock. - height: HeightInterval, + height: NumberOfBlocks, } impl IncompatibleTimeError { @@ -496,7 +496,7 @@ impl IncompatibleTimeError { pub fn incompatible(&self) -> MtpInterval { self.time } /// Returns the height value of the lock-by-blockheight lock. - pub fn expected(&self) -> HeightInterval { self.height } + pub fn expected(&self) -> NumberOfBlocks { self.height } } impl fmt::Display for IncompatibleTimeError { @@ -551,8 +551,8 @@ mod tests { #[test] fn parses_correctly_to_height_or_time() { - let height1 = HeightInterval::from(10); - let height2 = HeightInterval::from(11); + let height1 = NumberOfBlocks::from(10); + let height2 = NumberOfBlocks::from(11); let time1 = MtpInterval::from_512_second_intervals(70); let time2 = MtpInterval::from_512_second_intervals(71); @@ -576,12 +576,12 @@ mod tests { #[test] fn height_correctly_implies() { - let height = HeightInterval::from(10); + let height = NumberOfBlocks::from(10); let lock_by_height = LockTime::from(height); - assert!(!lock_by_height.is_implied_by(LockTime::from(HeightInterval::from(9)))); - assert!(lock_by_height.is_implied_by(LockTime::from(HeightInterval::from(10)))); - assert!(lock_by_height.is_implied_by(LockTime::from(HeightInterval::from(11)))); + assert!(!lock_by_height.is_implied_by(LockTime::from(NumberOfBlocks::from(9)))); + assert!(lock_by_height.is_implied_by(LockTime::from(NumberOfBlocks::from(10)))); + assert!(lock_by_height.is_implied_by(LockTime::from(NumberOfBlocks::from(11)))); } #[test] @@ -602,7 +602,7 @@ mod tests { #[test] fn sequence_correctly_implies() { - let height = HeightInterval::from(10); + let height = NumberOfBlocks::from(10); let time = MtpInterval::from_512_second_intervals(70); let lock_by_height = LockTime::from(height); @@ -625,7 +625,7 @@ mod tests { #[test] fn incorrect_units_do_not_imply() { let time = MtpInterval::from_512_second_intervals(70); - let height = HeightInterval::from(10); + let height = NumberOfBlocks::from(10); let lock_by_time = LockTime::from(time); assert!(!lock_by_time.is_implied_by(LockTime::from(height))); @@ -664,7 +664,7 @@ mod tests { #[test] fn incompatible_height_error() { - let height = HeightInterval::from(10); + let height = NumberOfBlocks::from(10); let time = MtpInterval::from_512_second_intervals(70); let lock_by_time = LockTime::from(time); let err = lock_by_time.is_satisfied_by_height(height).unwrap_err(); @@ -676,7 +676,7 @@ mod tests { #[test] fn incompatible_time_error() { - let height = HeightInterval::from(10); + let height = NumberOfBlocks::from(10); let time = MtpInterval::from_512_second_intervals(70); let lock_by_height = LockTime::from(height); let err = lock_by_height.is_satisfied_by_time(time).unwrap_err(); @@ -704,10 +704,10 @@ mod tests { let utxo_height = BlockHeight::from_u32(80); let utxo_mtp = BlockMtp::new(utxo_timestamps); - let lock1 = LockTime::Blocks(HeightInterval::from(10)); + let lock1 = LockTime::Blocks(NumberOfBlocks::from(10)); assert!(lock1.is_satisfied_by(chain_height, chain_mtp, utxo_height, utxo_mtp)); - let lock2 = LockTime::Blocks(HeightInterval::from(21)); + let lock2 = LockTime::Blocks(NumberOfBlocks::from(21)); assert!(!lock2.is_satisfied_by(chain_height, chain_mtp, utxo_height, utxo_mtp)); let lock3 = LockTime::Time(MtpInterval::from_512_second_intervals(10)); @@ -727,7 +727,7 @@ mod tests { let lock6 = LockTime::from_seconds_floor(5000).unwrap(); assert!(lock6.is_satisfied_by(chain_height, chain_mtp, utxo_height, utxo_mtp)); - let max_height_lock = LockTime::Blocks(HeightInterval::MAX); + let max_height_lock = LockTime::Blocks(NumberOfBlocks::MAX); assert!(!max_height_lock.is_satisfied_by(chain_height, chain_mtp, utxo_height, utxo_mtp)); let max_time_lock = LockTime::Time(MtpInterval::MAX); diff --git a/primitives/src/sequence.rs b/primitives/src/sequence.rs index b659c98e6..62fd67355 100644 --- a/primitives/src/sequence.rs +++ b/primitives/src/sequence.rs @@ -187,7 +187,7 @@ impl Sequence { /// Constructs a new [`relative::LockTime`] from this [`Sequence`] number. #[inline] pub fn to_relative_lock_time(self) -> Option { - use crate::locktime::relative::{HeightInterval, LockTime, MtpInterval}; + use crate::locktime::relative::{NumberOfBlocks, LockTime, MtpInterval}; if !self.is_relative_lock_time() { return None; @@ -198,7 +198,7 @@ impl Sequence { if self.is_time_locked() { Some(LockTime::from(MtpInterval::from_512_second_intervals(lock_value))) } else { - Some(LockTime::from(HeightInterval::from(lock_value))) + Some(LockTime::from(NumberOfBlocks::from(lock_value))) } } @@ -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(u32::from(relative::HeightInterval::MIN.to_height()))), - 5 => Ok(Sequence::from_consensus(u32::from(relative::HeightInterval::MAX.to_height()))), + 4 => Ok(Sequence::from_consensus(u32::from(relative::NumberOfBlocks::MIN.to_height()))), + 5 => Ok(Sequence::from_consensus(u32::from(relative::NumberOfBlocks::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 351fbe63f..8882880ee 100644 --- a/units/src/block.rs +++ b/units/src/block.rs @@ -142,18 +142,18 @@ impl BlockHeightInterval { pub fn checked_add(self, other: Self) -> Option { self.0.checked_add(other.0).map(Self) } } -impl From for BlockHeightInterval { - /// Converts a [`locktime::relative::HeightInterval`] to a [`BlockHeightInterval`]. +impl From for BlockHeightInterval { + /// Converts a [`locktime::relative::NumberOfBlocks`] to a [`BlockHeightInterval`]. /// /// A relative locktime block height has a maximum value of `u16::MAX` where as a /// [`BlockHeightInterval`] is a thin wrapper around a `u32`, the two types are not interchangeable. - fn from(h: relative::HeightInterval) -> Self { Self::from_u32(h.to_height().into()) } + fn from(h: relative::NumberOfBlocks) -> Self { Self::from_u32(h.to_height().into()) } } -impl TryFrom for relative::HeightInterval { - type Error = TooBigForRelativeBlockHeightIntervalError; +impl TryFrom for relative::NumberOfBlocks { + type Error = TooBigForRelativeHeightError; - /// Converts a [`BlockHeightInterval`] to a [`locktime::relative::HeightInterval`]. + /// Converts a [`BlockHeightInterval`] to a [`locktime::relative::NumberOfBlocks`]. /// /// A relative locktime block height has a maximum value of `u16::MAX` where as a /// [`BlockHeightInterval`] is a thin wrapper around a `u32`, the two types are not interchangeable. @@ -161,9 +161,9 @@ impl TryFrom for relative::HeightInterval { let h = h.to_u32(); if h > u32::from(u16::MAX) { - return Err(TooBigForRelativeBlockHeightIntervalError(h)); + return Err(TooBigForRelativeHeightError(h)); } - Ok(relative::HeightInterval::from(h as u16)) // Cast ok, value checked above. + Ok(relative::NumberOfBlocks::from(h as u16)) // Cast ok, value checked above. } } @@ -284,21 +284,21 @@ impl From for BlockMtpInterval { /// Error returned when the block interval is too big to be used as a relative lock time. #[derive(Debug, Clone, PartialEq, Eq)] -pub struct TooBigForRelativeBlockHeightIntervalError(u32); +pub struct TooBigForRelativeHeightError(u32); -impl fmt::Display for TooBigForRelativeBlockHeightIntervalError { +impl fmt::Display for TooBigForRelativeHeightError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!( f, "block interval is too big to be used as a relative lock time: {} (max: {})", self.0, - relative::HeightInterval::MAX + relative::NumberOfBlocks::MAX ) } } #[cfg(feature = "std")] -impl std::error::Error for TooBigForRelativeBlockHeightIntervalError {} +impl std::error::Error for TooBigForRelativeHeightError {} crate::internal_macros::impl_op_for_references! { // height - height = interval @@ -454,15 +454,15 @@ mod tests { assert_eq!(interval, 100); let interval_from_height: BlockHeightInterval = - relative::HeightInterval::from(10u16).into(); + relative::NumberOfBlocks::from(10u16).into(); assert_eq!(interval_from_height.to_u32(), 10u32); let invalid_height_greater = - relative::HeightInterval::try_from(BlockHeightInterval(u32::from(u16::MAX) + 1)); + relative::NumberOfBlocks::try_from(BlockHeightInterval(u32::from(u16::MAX) + 1)); assert!(invalid_height_greater.is_err()); let valid_height = - relative::HeightInterval::try_from(BlockHeightInterval(u32::from(u16::MAX))); + relative::NumberOfBlocks::try_from(BlockHeightInterval(u32::from(u16::MAX))); assert!(valid_height.is_ok()); } diff --git a/units/src/locktime/relative.rs b/units/src/locktime/relative.rs index af3136d09..fdb4a6b5f 100644 --- a/units/src/locktime/relative.rs +++ b/units/src/locktime/relative.rs @@ -1,6 +1,6 @@ // SPDX-License-Identifier: CC0-1.0 -//! Provides [`Height`] and [`MtpInterval`] types used by the `rust-bitcoin` `relative::LockTime` type. +//! Provides [`NumberOfBlocks`] and [`MtpInterval`] types used by the `rust-bitcoin` `relative::LockTime` type. use core::fmt; @@ -9,16 +9,16 @@ use arbitrary::{Arbitrary, Unstructured}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; -#[deprecated(since = "TBD", note = "use `HeightIterval` instead")] +#[deprecated(since = "TBD", note = "use `NumberOfBlocks` instead")] #[doc(hidden)] -pub type Height = HeightInterval; +pub type Height = NumberOfBlocks; /// A relative lock time lock-by-blockheight value. #[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct HeightInterval(u16); +pub struct NumberOfBlocks(u16); -impl HeightInterval { +impl NumberOfBlocks { /// Relative block height 0, can be included in any block. pub const ZERO: Self = Self(0); @@ -28,7 +28,7 @@ impl HeightInterval { /// The maximum relative block height. pub const MAX: Self = Self(u16::MAX); - /// Constructs a new [`HeightInterval`] using a count of blocks. + /// Constructs a new [`NumberOfBlocks`] using a count of blocks. #[inline] pub const fn from_height(blocks: u16) -> Self { Self(blocks) } @@ -80,14 +80,14 @@ impl HeightInterval { } } -impl From for HeightInterval { +impl From for NumberOfBlocks { #[inline] - fn from(value: u16) -> Self { HeightInterval(value) } + fn from(value: u16) -> Self { NumberOfBlocks(value) } } -crate::impl_parse_str_from_int_infallible!(HeightInterval, u16, from); +crate::impl_parse_str_from_int_infallible!(NumberOfBlocks, u16, from); -impl fmt::Display for HeightInterval { +impl fmt::Display for NumberOfBlocks { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(&self.0, f) } } @@ -246,14 +246,14 @@ impl fmt::Display for TimeOverflowError { impl std::error::Error for TimeOverflowError {} #[cfg(feature = "arbitrary")] -impl<'a> Arbitrary<'a> for HeightInterval { +impl<'a> Arbitrary<'a> for NumberOfBlocks { fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result { let choice = u.int_in_range(0..=2)?; match choice { - 0 => Ok(HeightInterval::MIN), - 1 => Ok(HeightInterval::MAX), - _ => Ok(HeightInterval::from_height(u16::arbitrary(u)?)), + 0 => Ok(NumberOfBlocks::MIN), + 1 => Ok(NumberOfBlocks::MAX), + _ => Ok(NumberOfBlocks::from_height(u16::arbitrary(u)?)), } } } @@ -284,7 +284,7 @@ mod tests { #[test] #[allow(deprecated_in_future)] fn sanity_check() { - assert_eq!(HeightInterval::MAX.to_consensus_u32(), u32::from(u16::MAX)); + assert_eq!(NumberOfBlocks::MAX.to_consensus_u32(), u32::from(u16::MAX)); assert_eq!(MtpInterval::from_512_second_intervals(100).value(), 100u16); assert_eq!(MtpInterval::from_512_second_intervals(100).to_consensus_u32(), 4_194_404u32); // 0x400064 } @@ -339,9 +339,9 @@ mod tests { #[test] #[cfg(feature = "serde")] pub fn encode_decode_height() { - serde_round_trip!(HeightInterval::ZERO); - serde_round_trip!(HeightInterval::MIN); - serde_round_trip!(HeightInterval::MAX); + serde_round_trip!(NumberOfBlocks::ZERO); + serde_round_trip!(NumberOfBlocks::MIN); + serde_round_trip!(NumberOfBlocks::MAX); } #[test] @@ -402,7 +402,7 @@ mod tests { fn test_height_chain_state() { use crate::BlockHeight; - let height_lock = HeightInterval(10); + let height_lock = NumberOfBlocks(10); // Test case 1: Satisfaction (current_height >= utxo_height + required) let chain_state1 = BlockHeight::from_u32(100); @@ -415,7 +415,7 @@ mod tests { assert!(!height_lock.is_satisfied_by(chain_state2, utxo_state2)); // Test case 3: Overflow handling - tests that is_satisfied_by handles overflow gracefully - let max_height_lock = HeightInterval::MAX; + let max_height_lock = NumberOfBlocks::MAX; let chain_state3 = BlockHeight::from_u32(1000); let utxo_state3 = BlockHeight::from_u32(80); assert!(!max_height_lock.is_satisfied_by(chain_state3, utxo_state3)); diff --git a/units/tests/api.rs b/units/tests/api.rs index 5ce217b75..aa0a8ceaa 100644 --- a/units/tests/api.rs +++ b/units/tests/api.rs @@ -135,7 +135,7 @@ struct Errors { t: amount::PossiblyConfusingDenominationError, u: amount::TooPreciseError, v: amount::UnknownDenominationError, - w: block::TooBigForRelativeBlockHeightIntervalError, + w: block::TooBigForRelativeHeightError, x: locktime::absolute::ConversionError, y: locktime::absolute::Height, z: locktime::absolute::ParseHeightError, @@ -172,7 +172,7 @@ fn api_can_use_all_types_from_module_amount() { #[test] fn api_can_use_all_types_from_module_block() { use bitcoin_units::block::{ - BlockHeight, BlockHeightInterval, TooBigForRelativeBlockHeightIntervalError, + BlockHeight, BlockHeightInterval, TooBigForRelativeHeightError, }; }