units: rename BlockInterval to BlockHeightInterval

Now that we have BlockMtpInterval we want to distinguish BlockInterval
from it.
This commit is contained in:
Andrew Poelstra 2025-05-05 22:42:35 +00:00
parent cb882c5ce1
commit 4e4601b3d5
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
7 changed files with 93 additions and 71 deletions

View File

@ -30,7 +30,13 @@ use crate::transaction::{Transaction, TransactionExt as _, Wtxid};
#[doc(inline)]
pub use primitives::block::{Block, Checked, Unchecked, Validation, Version, BlockHash, Header, WitnessCommitment};
#[doc(inline)]
pub use units::block::{BlockHeight, BlockInterval, TooBigForRelativeBlockHeightIntervalError};
pub use units::block::{
BlockHeight, BlockHeightInterval, TooBigForRelativeBlockHeightIntervalError,
};
#[deprecated(since = "TBD", note = "use `BlockHeightInterval` instead")]
#[doc(hidden)]
pub type BlockInterval = BlockHeightInterval;
impl_hashencode!(BlockHash);

View File

@ -134,12 +134,16 @@ pub use primitives::{
#[doc(inline)]
pub use units::{
amount::{Amount, Denomination, SignedAmount},
block::{BlockHeight, BlockInterval, BlockMtp},
block::{BlockHeight, BlockHeightInterval, BlockMtp},
fee_rate::FeeRate,
time::{self, BlockTime},
weight::Weight,
};
#[deprecated(since = "TBD", note = "use `BlockHeightInterval` instead")]
#[doc(hidden)]
pub type BlockInterval = BlockHeightInterval;
#[doc(inline)]
pub use crate::{
address::{Address, AddressType, KnownHrp},
@ -242,7 +246,7 @@ mod encode_impls {
//! Encodable/Decodable implementations.
// While we are deprecating, re-exporting, and generally moving things around just put these here.
use units::{BlockHeight, BlockInterval};
use units::{BlockHeight, BlockHeightInterval};
use crate::consensus::{encode, Decodable, Encodable};
use crate::io::{BufRead, Write};
@ -274,5 +278,5 @@ mod encode_impls {
}
impl_encodable_for_u32_wrapper!(BlockHeight);
impl_encodable_for_u32_wrapper!(BlockInterval);
impl_encodable_for_u32_wrapper!(BlockHeightInterval);
}

View File

@ -67,7 +67,7 @@
//! # }
//! ```
use units::{BlockHeight, BlockInterval};
use units::{BlockHeight, BlockHeightInterval};
use crate::network::Network;
#[cfg(doc)]
@ -92,9 +92,9 @@ pub struct Params {
/// Minimum blocks including miner confirmation of the total of 2016 blocks in a retargeting period,
/// (nPowTargetTimespan / nPowTargetSpacing) which is also used for BIP9 deployments.
/// Examples: 1916 for 95%, 1512 for testchains.
pub rule_change_activation_threshold: BlockInterval,
pub rule_change_activation_threshold: BlockHeightInterval,
/// Number of blocks with the same set of rules.
pub miner_confirmation_window: BlockInterval,
pub miner_confirmation_window: BlockHeightInterval,
/// Proof of work limit value. It contains the lowest possible difficulty.
#[deprecated(since = "0.32.0", note = "use `max_attainable_target` instead")]
pub pow_limit: Target,
@ -152,8 +152,8 @@ impl Params {
bip34_height: BlockHeight::from_u32(227931), // 000000000000024b89b42a942fe0d9fea3bb44ab7bd1b19115dd6a759c0808b8
bip65_height: BlockHeight::from_u32(388381), // 000000000000000004c2b624ed5d7756c508d90fd0da2c7c679febfa6c4735f0
bip66_height: BlockHeight::from_u32(363725), // 00000000000000000379eaa19dce8c9b722d46ae6a57c2f1a988119488b50931
rule_change_activation_threshold: BlockInterval::from_u32(1916), // 95%
miner_confirmation_window: BlockInterval::from_u32(2016),
rule_change_activation_threshold: BlockHeightInterval::from_u32(1916), // 95%
miner_confirmation_window: BlockHeightInterval::from_u32(2016),
pow_limit: Target::MAX_ATTAINABLE_MAINNET,
max_attainable_target: Target::MAX_ATTAINABLE_MAINNET,
pow_target_spacing: 10 * 60, // 10 minutes.
@ -170,8 +170,8 @@ impl Params {
bip34_height: BlockHeight::from_u32(21111), // 0000000023b3a96d3484e5abb3755c413e7d41500f8e2a5c3f0dd01299cd8ef8
bip65_height: BlockHeight::from_u32(581885), // 00000000007f6655f22f98e72ed80d8b06dc761d5da09df0fa1dc4be4f861eb6
bip66_height: BlockHeight::from_u32(330776), // 000000002104c8c45e99a8853285a3b592602a3ccde2b832481da85e9e4ba182
rule_change_activation_threshold: BlockInterval::from_u32(1512), // 75%
miner_confirmation_window: BlockInterval::from_u32(2016),
rule_change_activation_threshold: BlockHeightInterval::from_u32(1512), // 75%
miner_confirmation_window: BlockHeightInterval::from_u32(2016),
pow_limit: Target::MAX_ATTAINABLE_TESTNET,
max_attainable_target: Target::MAX_ATTAINABLE_TESTNET,
pow_target_spacing: 10 * 60, // 10 minutes.
@ -187,8 +187,8 @@ impl Params {
bip34_height: BlockHeight::from_u32(21111), // 0000000023b3a96d3484e5abb3755c413e7d41500f8e2a5c3f0dd01299cd8ef8
bip65_height: BlockHeight::from_u32(581885), // 00000000007f6655f22f98e72ed80d8b06dc761d5da09df0fa1dc4be4f861eb6
bip66_height: BlockHeight::from_u32(330776), // 000000002104c8c45e99a8853285a3b592602a3ccde2b832481da85e9e4ba182
rule_change_activation_threshold: BlockInterval::from_u32(1512), // 75%
miner_confirmation_window: BlockInterval::from_u32(2016),
rule_change_activation_threshold: BlockHeightInterval::from_u32(1512), // 75%
miner_confirmation_window: BlockHeightInterval::from_u32(2016),
pow_limit: Target::MAX_ATTAINABLE_TESTNET,
max_attainable_target: Target::MAX_ATTAINABLE_TESTNET,
pow_target_spacing: 10 * 60, // 10 minutes.
@ -204,8 +204,8 @@ impl Params {
bip34_height: BlockHeight::from_u32(1),
bip65_height: BlockHeight::from_u32(1),
bip66_height: BlockHeight::from_u32(1),
rule_change_activation_threshold: BlockInterval::from_u32(1512), // 75%
miner_confirmation_window: BlockInterval::from_u32(2016),
rule_change_activation_threshold: BlockHeightInterval::from_u32(1512), // 75%
miner_confirmation_window: BlockHeightInterval::from_u32(2016),
pow_limit: Target::MAX_ATTAINABLE_TESTNET,
max_attainable_target: Target::MAX_ATTAINABLE_TESTNET,
pow_target_spacing: 10 * 60, // 10 minutes.
@ -221,8 +221,8 @@ impl Params {
bip34_height: BlockHeight::from_u32(1),
bip65_height: BlockHeight::from_u32(1),
bip66_height: BlockHeight::from_u32(1),
rule_change_activation_threshold: BlockInterval::from_u32(1916), // 95%
miner_confirmation_window: BlockInterval::from_u32(2016),
rule_change_activation_threshold: BlockHeightInterval::from_u32(1916), // 95%
miner_confirmation_window: BlockHeightInterval::from_u32(2016),
pow_limit: Target::MAX_ATTAINABLE_SIGNET,
max_attainable_target: Target::MAX_ATTAINABLE_SIGNET,
pow_target_spacing: 10 * 60, // 10 minutes.
@ -238,8 +238,8 @@ impl Params {
bip34_height: BlockHeight::from_u32(100000000), // not activated on regtest
bip65_height: BlockHeight::from_u32(1351),
bip66_height: BlockHeight::from_u32(1251), // used only in rpc tests
rule_change_activation_threshold: BlockInterval::from_u32(108), // 75%
miner_confirmation_window: BlockInterval::from_u32(144),
rule_change_activation_threshold: BlockHeightInterval::from_u32(108), // 75%
miner_confirmation_window: BlockHeightInterval::from_u32(144),
pow_limit: Target::MAX_ATTAINABLE_REGTEST,
max_attainable_target: Target::MAX_ATTAINABLE_REGTEST,
pow_target_spacing: 10 * 60, // 10 minutes.

View File

@ -50,12 +50,16 @@ pub mod witness;
#[doc(inline)]
pub use units::{
amount::{self, Amount, SignedAmount},
block::{BlockHeight, BlockInterval, BlockMtp},
block::{BlockHeight, BlockHeightInterval, BlockMtp, BlockMtpInterval},
fee_rate::{self, FeeRate},
time::{self, BlockTime},
weight::{self, Weight},
};
#[deprecated(since = "TBD", note = "use `BlockHeightInterval` instead")]
#[doc(hidden)]
pub type BlockInterval = BlockHeightInterval;
#[doc(inline)]
#[cfg(feature = "alloc")]
pub use self::{

View File

@ -119,25 +119,25 @@ impl_u32_wrapper! {
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
// Public to try and make it really clear that there are no invariants.
pub struct BlockInterval(pub u32);
pub struct BlockHeightInterval(pub u32);
}
impl From<relative::HeightInterval> for BlockInterval {
/// Converts a [`locktime::relative::HeightInterval`] to a [`BlockInterval`].
impl From<relative::HeightInterval> for BlockHeightInterval {
/// Converts a [`locktime::relative::HeightInterval`] to a [`BlockHeightInterval`].
///
/// 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.
/// [`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()) }
}
impl TryFrom<BlockInterval> for relative::HeightInterval {
impl TryFrom<BlockHeightInterval> for relative::HeightInterval {
type Error = TooBigForRelativeBlockHeightIntervalError;
/// Converts a [`BlockInterval`] to a [`locktime::relative::HeightInterval`].
/// Converts a [`BlockHeightInterval`] to a [`locktime::relative::HeightInterval`].
///
/// 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 try_from(h: BlockInterval) -> Result<Self, Self::Error> {
/// [`BlockHeightInterval`] is a thin wrapper around a `u32`, the two types are not interchangeable.
fn try_from(h: BlockHeightInterval) -> Result<Self, Self::Error> {
let h = h.to_u32();
if h > u32::from(u16::MAX) {
@ -267,51 +267,51 @@ impl std::error::Error for TooBigForRelativeBlockHeightIntervalError {}
crate::internal_macros::impl_op_for_references! {
// height - height = interval
impl ops::Sub<BlockHeight> for BlockHeight {
type Output = BlockInterval;
type Output = BlockHeightInterval;
fn sub(self, rhs: BlockHeight) -> Self::Output {
let interval = self.to_u32() - rhs.to_u32();
BlockInterval::from_u32(interval)
BlockHeightInterval::from_u32(interval)
}
}
// height + interval = height
impl ops::Add<BlockInterval> for BlockHeight {
impl ops::Add<BlockHeightInterval> for BlockHeight {
type Output = BlockHeight;
fn add(self, rhs: BlockInterval) -> Self::Output {
fn add(self, rhs: BlockHeightInterval) -> Self::Output {
let height = self.to_u32() + rhs.to_u32();
BlockHeight::from_u32(height)
}
}
// height - interval = height
impl ops::Sub<BlockInterval> for BlockHeight {
impl ops::Sub<BlockHeightInterval> for BlockHeight {
type Output = BlockHeight;
fn sub(self, rhs: BlockInterval) -> Self::Output {
fn sub(self, rhs: BlockHeightInterval) -> Self::Output {
let height = self.to_u32() - rhs.to_u32();
BlockHeight::from_u32(height)
}
}
// interval + interval = interval
impl ops::Add<BlockInterval> for BlockInterval {
type Output = BlockInterval;
impl ops::Add<BlockHeightInterval> for BlockHeightInterval {
type Output = BlockHeightInterval;
fn add(self, rhs: BlockInterval) -> Self::Output {
fn add(self, rhs: BlockHeightInterval) -> Self::Output {
let height = self.to_u32() + rhs.to_u32();
BlockInterval::from_u32(height)
BlockHeightInterval::from_u32(height)
}
}
// interval - interval = interval
impl ops::Sub<BlockInterval> for BlockInterval {
type Output = BlockInterval;
impl ops::Sub<BlockHeightInterval> for BlockHeightInterval {
type Output = BlockHeightInterval;
fn sub(self, rhs: BlockInterval) -> Self::Output {
fn sub(self, rhs: BlockHeightInterval) -> Self::Output {
let height = self.to_u32() - rhs.to_u32();
BlockInterval::from_u32(height)
BlockHeightInterval::from_u32(height)
}
}
@ -366,25 +366,25 @@ crate::internal_macros::impl_op_for_references! {
}
}
crate::internal_macros::impl_add_assign!(BlockInterval);
crate::internal_macros::impl_sub_assign!(BlockInterval);
crate::internal_macros::impl_add_assign!(BlockHeightInterval);
crate::internal_macros::impl_sub_assign!(BlockHeightInterval);
crate::internal_macros::impl_add_assign!(BlockMtpInterval);
crate::internal_macros::impl_sub_assign!(BlockMtpInterval);
impl core::iter::Sum for BlockInterval {
impl core::iter::Sum for BlockHeightInterval {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
let sum = iter.map(|interval| interval.0).sum();
BlockInterval::from_u32(sum)
BlockHeightInterval::from_u32(sum)
}
}
impl<'a> core::iter::Sum<&'a BlockInterval> for BlockInterval {
impl<'a> core::iter::Sum<&'a BlockHeightInterval> for BlockHeightInterval {
fn sum<I>(iter: I) -> Self
where
I: Iterator<Item = &'a BlockInterval>,
I: Iterator<Item = &'a BlockHeightInterval>,
{
let sum = iter.map(|interval| interval.0).sum();
BlockInterval::from_u32(sum)
BlockHeightInterval::from_u32(sum)
}
}
@ -414,17 +414,19 @@ mod tests {
let height: u32 = BlockHeight(100).into();
assert_eq!(height, 100);
let interval: u32 = BlockInterval(100).into();
let interval: u32 = BlockHeightInterval(100).into();
assert_eq!(interval, 100);
let interval_from_height: BlockInterval = relative::HeightInterval::from(10u16).into();
let interval_from_height: BlockHeightInterval =
relative::HeightInterval::from(10u16).into();
assert_eq!(interval_from_height.to_u32(), 10u32);
let invalid_height_greater =
relative::HeightInterval::try_from(BlockInterval(u32::from(u16::MAX) + 1));
relative::HeightInterval::try_from(BlockHeightInterval(u32::from(u16::MAX) + 1));
assert!(invalid_height_greater.is_err());
let valid_height = relative::HeightInterval::try_from(BlockInterval(u32::from(u16::MAX)));
let valid_height =
relative::HeightInterval::try_from(BlockHeightInterval(u32::from(u16::MAX)));
assert!(valid_height.is_ok());
}
@ -432,39 +434,41 @@ mod tests {
#[test]
fn all_available_ops() {
// height - height = interval
assert!(BlockHeight(10) - BlockHeight(7) == BlockInterval(3));
assert!(BlockHeight(10) - BlockHeight(7) == BlockHeightInterval(3));
// height + interval = height
assert!(BlockHeight(100) + BlockInterval(1) == BlockHeight(101));
assert!(BlockHeight(100) + BlockHeightInterval(1) == BlockHeight(101));
// height - interval == height
assert!(BlockHeight(100) - BlockInterval(1) == BlockHeight(99));
assert!(BlockHeight(100) - BlockHeightInterval(1) == BlockHeight(99));
// interval + interval = interval
assert!(BlockInterval(1) + BlockInterval(2) == BlockInterval(3));
assert!(BlockHeightInterval(1) + BlockHeightInterval(2) == BlockHeightInterval(3));
// interval - interval = interval
assert!(BlockInterval(10) - BlockInterval(7) == BlockInterval(3));
assert!(BlockHeightInterval(10) - BlockHeightInterval(7) == BlockHeightInterval(3));
assert!(
[BlockInterval(1), BlockInterval(2), BlockInterval(3)].iter().sum::<BlockInterval>()
== BlockInterval(6)
[BlockHeightInterval(1), BlockHeightInterval(2), BlockHeightInterval(3)]
.iter()
.sum::<BlockHeightInterval>()
== BlockHeightInterval(6)
);
assert!(
[BlockInterval(4), BlockInterval(5), BlockInterval(6)]
[BlockHeightInterval(4), BlockHeightInterval(5), BlockHeightInterval(6)]
.into_iter()
.sum::<BlockInterval>()
== BlockInterval(15)
.sum::<BlockHeightInterval>()
== BlockHeightInterval(15)
);
// interval += interval
let mut int = BlockInterval(1);
int += BlockInterval(2);
assert_eq!(int, BlockInterval(3));
let mut int = BlockHeightInterval(1);
int += BlockHeightInterval(2);
assert_eq!(int, BlockHeightInterval(3));
// interval -= interval
let mut int = BlockInterval(10);
int -= BlockInterval(7);
assert_eq!(int, BlockInterval(3));
let mut int = BlockHeightInterval(10);
int -= BlockHeightInterval(7);
assert_eq!(int, BlockHeightInterval(3));
}
}

View File

@ -57,10 +57,14 @@ pub mod weight;
#[rustfmt::skip]
pub use self::{
amount::{Amount, SignedAmount},
block::{BlockHeight, BlockInterval, BlockMtp, BlockMtpInterval},
block::{BlockHeight, BlockHeightInterval, BlockMtp, BlockMtpInterval},
fee_rate::FeeRate,
result::{NumOpError, NumOpResult, MathOp},
time::BlockTime,
weight::Weight
};
pub(crate) use self::result::OptionExt;
#[deprecated(since = "TBD", note = "use `BlockHeightInterval` instead")]
#[doc(hidden)]
pub type BlockInterval = BlockHeightInterval;

View File

@ -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, BlockInterval, TooBigForRelativeBlockHeightIntervalError,
BlockHeight, BlockHeightInterval, TooBigForRelativeBlockHeightIntervalError,
};
}