lsps-types: test the opening fee computation
What changed, and why it matters
This commit only adds unit tests and property-based tests for an existing fee-calculation function. It does not change the function's behavior, fix a bug, or alter any production code paths. There is no security issue introduced or resolved by this change.
No security action required. Treat as routine test-coverage improvement.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit adds test coverage for compute_opening_fee in lsps-types/src/lsps2/fee.rs. It introduces proptest as a dev-dependency and verifies rounding boundaries, the min_fee_msat floor, and u64 overflow behavior for both the multiplication and the +999_999 addition. The implementation of compute_opening_fee is unchanged; only #[cfg(test)] code is appended.
Changed components
lsps-types/src/lsps2/fee.rslsps-types/Cargo.tomlCargo.lockInspect captured patch +213 / −0
### Cargo.lock
@@ -269,6 +269,21 @@ version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32637268377fc7b10a8c6d51de3e7fba1ce5dd371a96e342b34e6078db558e7f"
+[[package]]
+name = "bit-set"
+version = "0.8.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "08807e080ed7f9d5433fa9b275196cfc35414f66a0c79d864dc51a0d825231a3"
+dependencies = [
+ "bit-vec",
+]
+
+[[package]]
+name = "bit-vec"
+version = "0.8.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5e764a1d40d510daf35e07be9eb06e75770908c27d411ee6c92109c9840eaaf7"
+
[[package]]
name = "bitcoin"
version = "0.32.102"
@@ -1757,6 +1772,7 @@ name = "lsps-types"
version = "0.7.0"
dependencies = [
"chrono",
+ "proptest",
"serde",
"serde_json",
]
@@ -2060,6 +2076,25 @@ dependencies = [
"unicode-ident",
]
+[[package]]
+name = "proptest"
+version = "1.11.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4b45fcc2344c680f5025fe57779faef368840d0bd1f42f216291f0dc4ace4744"
+dependencies = [
+ "bit-set",
+ "bit-vec",
+ "bitflags 2.13.1",
+ "num-traits",
+ "rand 0.9.5",
+ "rand_chacha",
+ "rand_xorshift",
+ "regex-syntax",
+ "rusty-fork",
+ "tempfile",
+ "unarray",
+]
+
[[package]]
name = "prost"
version = "0.14.4"
@@ -2197,6 +2232,12 @@ dependencies = [
"pulldown-cmark",
]
+[[package]]
+name = "quick-error"
+version = "1.2.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0"
+
[[package]]
name = "quick-xml"
version = "0.41.0"
@@ -2274,6 +2315,15 @@ version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69"
+[[package]]
+name = "rand_xorshift"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "513962919efc330f829edb2535844d1b912b0fbe2ca165d613e4e8788bb05a5a"
+dependencies = [
+ "rand_core 0.9.5",
+]
+
[[package]]
name = "rcgen"
version = "0.14.7"
@@ -2615,6 +2665,18 @@ version = "1.0.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f"
+[[package]]
+name = "rusty-fork"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cc6bf79ff24e648f6da1f8d1f011e9cac26491b619e6b9280f2b47f1774e6ee2"
+dependencies = [
+ "fnv",
+ "quick-error",
+ "tempfile",
+ "wait-timeout",
+]
+
[[package]]
name = "ryu"
version = "1.0.23"
@@ -3525,6 +3587,12 @@ version = "1.20.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20"
+[[package]]
+name = "unarray"
+version = "0.1.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "eaea85b334db583fe3274d12b4cd1880032beab409c0d774be044d4480ab9a94"
+
[[package]]
name = "unicase"
version = "2.9.0"
@@ -3640,6 +3708,15 @@ version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
+[[package]]
+name = "wait-timeout"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "09ac3b126d3914f9849036f826e054cbabdc8519970b8998ddaf3b5bd3c65f11"
+dependencies = [
+ "libc",
+]
+
[[package]]
name = "walkdir"
version = "2.5.0"
### lsps-types/Cargo.toml
@@ -13,3 +13,6 @@ rust-version.workspace = true
chrono = { version = "0.4.42", default-features = false, features = ["std", "serde"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
+
+[dev-dependencies]
+proptest = "1"
### lsps-types/src/lsps2/fee.rs
@@ -45,3 +45,136 @@ pub fn compute_opening_fee(
/ 1_000_000;
Some(fee.max(opening_fee_min_fee_msat))
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use proptest::prelude::*;
+
+ #[test]
+ fn rounds_up_to_the_next_msat() {
+ // The proportional part is above min_fee_msat (0) in all cases.
+ // product = payment_size * proportional
+ for (payment_size, proportional, fee) in [
+ (0, 1_000, 0), // product 0
+ (1, 1, 1), // product 1
+ (999_999, 1, 1), // product 10^6 - 1
+ (1_000_000, 1, 1), // product 10^6
+ (1_000_001, 1, 2), // product 10^6 + 1
+ (1_999_999, 1, 2), // product 2*10^6 - 1
+ (2_000_000, 1, 2), // product 2*10^6
+ (2_000_001, 1, 3), // product 2*10^6 + 1
+ (1_000, 1_000_000, 1_000), // product 10^9
+ (1_001, 999_999, 1_001), // product 10^9 - 1 + 1001 - 1000
+ (500_000_000, 2_000, 1_000_000), // product 10^12
+ (500_000_001, 2_000, 1_000_001), // product 10^12 + 2000
+ ] {
+ assert_eq!(
+ compute_opening_fee(payment_size, 0, proportional),
+ Some(fee),
+ "payment_size={payment_size} proportional={proportional}"
+ );
+ }
+ }
+
+ #[test]
+ fn min_fee_is_a_floor() {
+ // Proportional part: ceil(10_000_000 * 1_000 / 10^6) = 10_000.
+ assert_eq!(compute_opening_fee(10_000_000, 9_999, 1_000), Some(10_000));
+ assert_eq!(compute_opening_fee(10_000_000, 10_000, 1_000), Some(10_000));
+ assert_eq!(compute_opening_fee(10_000_000, 10_001, 1_000), Some(10_001));
+ // Rounding happens before the floor is applied.
+ assert_eq!(compute_opening_fee(10_000_001, 10_000, 1_000), Some(10_001));
+ assert_eq!(compute_opening_fee(0, 546_000, 1_200), Some(546_000));
+ assert_eq!(compute_opening_fee(0, u64::MAX, 0), Some(u64::MAX));
+ }
+
+ #[test]
+ fn multiplication_overflow_fails() {
+ assert_eq!(compute_opening_fee(u64::MAX, 0, 2), None);
+ assert_eq!(compute_opening_fee(2, 0, u64::MAX), None);
+ assert_eq!(compute_opening_fee(u64::MAX / 2 + 1, 0, 2), None);
+ assert_eq!(compute_opening_fee(u64::MAX, 0, u64::from(u32::MAX)), None);
+ // The product u64::MAX - 1 fits, but adding 999_999 does not.
+ assert_eq!(compute_opening_fee(u64::MAX / 2, 0, 2), None);
+ // Product u64::MAX - 10^6: multiplication and addition both fit.
+ assert_eq!(
+ compute_opening_fee((u64::MAX - 999_999) / 5, 0, 5),
+ Some(u64::MAX / 1_000_000)
+ );
+ }
+
+ #[test]
+ fn addition_overflow_fails() {
+ // The product fits, adding 999_999 does not.
+ assert_eq!(compute_opening_fee(u64::MAX - 999_998, 0, 1), None);
+ assert_eq!(compute_opening_fee(u64::MAX, 0, 1), None);
+ // u64::MAX / 10^6 * 10^6 fits, + 999_999 is above u64::MAX.
+ assert_eq!(
+ compute_opening_fee(u64::MAX / 1_000_000, 0, 1_000_000),
+ None
+ );
+ // The largest product that does not overflow.
+ assert_eq!(
+ compute_opening_fee(u64::MAX - 999_999, 0, 1),
+ Some(u64::MAX / 1_000_000)
+ );
+ // An overflow fails even if min_fee_msat would dominate.
+ assert_eq!(compute_opening_fee(u64::MAX, u64::MAX, 1), None);
+ }
+
+ #[test]
+ fn zero_proportional_is_min_fee() {
+ assert_eq!(compute_opening_fee(u64::MAX, 1_000, 0), Some(1_000));
+ }
+
+ /// Independent reference: exact arithmetic in u128, then the u64
+ /// overflow rule from the spec.
+ fn reference(payment_size: u64, min_fee: u64, proportional: u64) -> Option<u64> {
+ let sum = u128::from(payment_size) * u128::from(proportional) + 999_999;
+ if sum > u128::from(u64::MAX) {
+ return None;
+ }
+ let fee = u64::try_from(sum / 1_000_000).unwrap();
+ Some(fee.max(min_fee))
+ }
+
+ proptest! {
+ #[test]
+ fn matches_u128_reference(
+ payment_size in any::<u64>(),
+ min_fee in any::<u64>(),
+ proportional in any::<u64>(),
+ ) {
+ prop_assert_eq!(
+ compute_opening_fee(payment_size, min_fee, proportional),
+ reference(payment_size, min_fee, proportional)
+ );
+ }
+
+ #[test]
+ fn matches_u128_reference_realistic(
+ payment_size in 0u64..=21_000_000 * 100_000_000_000,
+ min_fee in 0u64..=100_000_000,
+ proportional in 0u64..=u64::from(u32::MAX),
+ ) {
+ prop_assert_eq!(
+ compute_opening_fee(payment_size, min_fee, proportional),
+ reference(payment_size, min_fee, proportional)
+ );
+ }
+
+ #[test]
+ fn is_ceiling_of_proportional_part(
+ // Small enough that payment_size * 10^6 + 999_999 fits in u64.
+ payment_size in 0u64..u64::MAX / 1_000_000,
+ proportional in 0u64..=1_000_000,
+ ) {
+ let fee = compute_opening_fee(payment_size, 0, proportional).unwrap();
+ let product = u128::from(payment_size) * u128::from(proportional);
+ // fee is the smallest integer with fee * 10^6 >= product.
+ prop_assert!(u128::from(fee) * 1_000_000 >= product);
+ prop_assert!(fee == 0 || (u128::from(fee) - 1) * 1_000_000 < product);
+ }
+ }
+}Why this scored 15/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.