Merge rust-bitcoin/rust-bitcoin#7005: feat(units): add saturating arithmetic methods to FeeRate
What changed, and why it matters
This commit adds new safe arithmetic helpers to the FeeRate type in the rust-bitcoin library. These methods multiply, add, or subtract fee rates while capping the result at the maximum or minimum allowed value, preventing accidental numeric overflow. It is a routine feature addition with no security fix or vulnerability indicator.
No security action needed. Treat as a normal feature merge.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change introduces saturating_mul, saturating_add, and saturating_sub on FeeRate in units/src/fee_rate/mod.rs. They delegate to u64 saturating operations and are marked const, inline, and must_use. Unit tests cover normal cases, boundary values, and saturation behavior. No existing behavior is modified, no unsafe code is added, and no security issue is described.
Changed components
units/src/fee_rate/mod.rsInspect captured patch +49 / −0
### units/src/fee_rate/mod.rs
@@ -212,6 +212,33 @@ impl FeeRate {
Some(Self::from_sat_per_mvb(sat_mvb))
}
+ /// Saturating integer multiplication.
+ ///
+ /// Computes `self * rhs`, saturating at [`FeeRate::MAX`] instead of overflowing.
+ #[inline]
+ #[must_use]
+ pub const fn saturating_mul(self, rhs: u64) -> Self {
+ Self::from_sat_per_mvb(self.to_sat_per_mvb().saturating_mul(rhs))
+ }
+
+ /// Saturating integer addition.
+ ///
+ /// Computes `self + rhs`, saturating at [`FeeRate::MAX`] instead of overflowing.
+ #[inline]
+ #[must_use]
+ pub const fn saturating_add(self, rhs: Self) -> Self {
+ Self::from_sat_per_mvb(self.to_sat_per_mvb().saturating_add(rhs.to_sat_per_mvb()))
+ }
+
+ /// Saturating integer subtraction.
+ ///
+ /// Computes `self - rhs`, saturating at [`FeeRate::MIN`] instead of overflowing.
+ #[inline]
+ #[must_use]
+ pub const fn saturating_sub(self, rhs: Self) -> Self {
+ Self::from_sat_per_mvb(self.to_sat_per_mvb().saturating_sub(rhs.to_sat_per_mvb()))
+ }
+
/// Calculates the fee by multiplying this fee rate by weight.
///
/// Computes the absolute fee amount for a given [`Weight`] at this fee rate. When the resulting
@@ -509,6 +536,28 @@ mod tests {
assert!(fee_rate.is_none());
}
+ #[test]
+ fn saturating_mul() {
+ assert_eq!(TEN.saturating_mul(10), ONE_HUNDRED);
+ assert_eq!(TEN.saturating_mul(0), FeeRate::ZERO);
+ assert_eq!(FeeRate::MAX.saturating_mul(2), FeeRate::MAX);
+ assert_eq!(TEN.saturating_mul(u64::MAX), FeeRate::MAX);
+ }
+
+ #[test]
+ fn saturating_add() {
+ assert_eq!(ONE.saturating_add(TWO), THREE);
+ assert_eq!(FeeRate::MAX.saturating_add(ONE), FeeRate::MAX);
+ assert_eq!(FeeRate::ZERO.saturating_add(FeeRate::ZERO), FeeRate::ZERO);
+ }
+
+ #[test]
+ fn saturating_sub() {
+ assert_eq!(THREE.saturating_sub(TWO), ONE);
+ assert_eq!(FeeRate::ZERO.saturating_sub(ONE), FeeRate::ZERO);
+ assert_eq!(ONE.saturating_sub(THREE), FeeRate::ZERO);
+ }
+
#[test]
fn mvb() {
let fee_rate = FeeRate::from_sat_per_mvb(1_234_567);Why this scored 18/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.