Implement FeeRate checked_add

This commit is contained in:
yancy 2024-09-21 09:30:29 -05:00
parent c967eabd43
commit 212a751929
1 changed files with 14 additions and 0 deletions

View File

@ -96,6 +96,11 @@ impl FeeRate {
Some(Amount::from_sat(sats)) Some(Amount::from_sat(sats))
} }
/// Checked addition.
///
/// Computes `self + rhs` returning [`None`] if overflow occured.
pub fn checked_add(self, rhs: u64) -> Option<Self> { self.0.checked_add(rhs).map(Self) }
/// Calculates the fee by multiplying this fee rate by weight, in weight units, returning [`None`] /// Calculates the fee by multiplying this fee rate by weight, in weight units, returning [`None`]
/// if an overflow occurred. /// if an overflow occurred.
/// ///
@ -286,6 +291,15 @@ mod tests {
assert_eq!(f, FeeRate(1)); assert_eq!(f, FeeRate(1));
} }
#[test]
fn checked_add() {
let f = FeeRate(1).checked_add(2).unwrap();
assert_eq!(FeeRate(3), f);
let f = FeeRate(u64::MAX).checked_add(1);
assert!(f.is_none());
}
#[test] #[test]
fn fee_rate_const_test() { fn fee_rate_const_test() {
assert_eq!(0, FeeRate::ZERO.to_sat_per_kwu()); assert_eq!(0, FeeRate::ZERO.to_sat_per_kwu());