Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit e497873

Browse files
committed
Add test for compute_opening_fee
1 parent 175696c commit e497873

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/lsps2/utils.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,29 @@ pub fn compute_opening_fee(
5555
.and_then(|f| f.checked_div(1000000))
5656
.map(|f| core::cmp::max(f, opening_fee_min_fee_msat))
5757
}
58+
59+
#[cfg(test)]
60+
mod tests {
61+
use super::*;
62+
use proptest::prelude::*;
63+
64+
const MAX_VALUE_MSAT: u64 = 21_000_000_0000_0000_000;
65+
66+
fn arb_opening_fee_params() -> impl Strategy<Value = (u64, u64, u64)> {
67+
(0u64..MAX_VALUE_MSAT, 0u64..MAX_VALUE_MSAT, 0u64..MAX_VALUE_MSAT)
68+
}
69+
70+
proptest! {
71+
#[test]
72+
fn test_compute_opening_fee((payment_size_msat, opening_fee_min_fee_msat, opening_fee_proportional) in arb_opening_fee_params()) {
73+
if let Some(res) = compute_opening_fee(payment_size_msat, opening_fee_min_fee_msat, opening_fee_proportional) {
74+
assert!(res >= opening_fee_min_fee_msat);
75+
assert_eq!(res as f32, (payment_size_msat as f32 * opening_fee_proportional as f32));
76+
} else {
77+
// Check we actually overflowed.
78+
let max_value = u64::MAX as u128;
79+
assert!((payment_size_msat as u128 * opening_fee_proportional as u128) > max_value);
80+
}
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)