diff --git a/components/zcash_protocol/CHANGELOG.md b/components/zcash_protocol/CHANGELOG.md index 618e98341..e2e99084f 100644 --- a/components/zcash_protocol/CHANGELOG.md +++ b/components/zcash_protocol/CHANGELOG.md @@ -7,6 +7,16 @@ and this library adheres to Rust's notion of ## [Unreleased] +### Added +- `impl Sub for BlockHeight` unlike the implementation that was + removed in version `0.3.0`, a saturating subtraction for block heights having + a return type of `u32` makes sense for `BlockHeight`. Subtracting one block + height from another yields the delta between them. + +### Changed +- Adding a delta to a `BlockHeight` now uses saturating addition. +- Subtracting a delta to a `BlockHeight` now uses saturating subtraction. + ## [0.3.0] - 2024-08-26 ### Changed - Testnet activation height has been set for `consensus::BranchId::Nu6`. diff --git a/components/zcash_protocol/src/consensus.rs b/components/zcash_protocol/src/consensus.rs index 5f480714c..43a1b6f86 100644 --- a/components/zcash_protocol/src/consensus.rs +++ b/components/zcash_protocol/src/consensus.rs @@ -103,7 +103,7 @@ impl Add for BlockHeight { type Output = Self; fn add(self, other: u32) -> Self { - BlockHeight(self.0 + other) + BlockHeight(self.0.saturating_add(other)) } } @@ -111,11 +111,15 @@ impl Sub for BlockHeight { type Output = Self; fn sub(self, other: u32) -> Self { - if other > self.0 { - panic!("Subtraction resulted in negative block height."); - } + BlockHeight(self.0.saturating_sub(other)) + } +} - BlockHeight(self.0 - other) +impl Sub for BlockHeight { + type Output = u32; + + fn sub(self, other: BlockHeight) -> u32 { + self.0.saturating_sub(other.0) } }