diff --git a/lightning/src/blinded_path/mod.rs b/lightning/src/blinded_path/mod.rs index 7a5088f7475..760a4932b6c 100644 --- a/lightning/src/blinded_path/mod.rs +++ b/lightning/src/blinded_path/mod.rs @@ -87,12 +87,13 @@ impl BlindedPath { // TODO: make all payloads the same size with padding + add dummy hops pub fn new_for_payment( intermediate_nodes: &[(PublicKey, payment::ForwardTlvs)], payee_node_id: PublicKey, - payee_tlvs: payment::ReceiveTlvs, entropy_source: &ES, secp_ctx: &Secp256k1 + payee_tlvs: payment::ReceiveTlvs, htlc_maximum_msat: u64, entropy_source: &ES, + secp_ctx: &Secp256k1 ) -> Result<(BlindedPayInfo, Self), ()> { let blinding_secret_bytes = entropy_source.get_secure_random_bytes(); let blinding_secret = SecretKey::from_slice(&blinding_secret_bytes[..]).expect("RNG is busted"); - let blinded_payinfo = payment::compute_payinfo(intermediate_nodes, &payee_tlvs)?; + let blinded_payinfo = payment::compute_payinfo(intermediate_nodes, &payee_tlvs, htlc_maximum_msat)?; Ok((blinded_payinfo, BlindedPath { introduction_node_id: intermediate_nodes.first().map_or(payee_node_id, |n| n.0), blinding_point: PublicKey::from_secret_key(secp_ctx, &blinding_secret), diff --git a/lightning/src/blinded_path/payment.rs b/lightning/src/blinded_path/payment.rs index 9c93014dd2c..76eb78eb19a 100644 --- a/lightning/src/blinded_path/payment.rs +++ b/lightning/src/blinded_path/payment.rs @@ -154,7 +154,7 @@ pub(super) fn blinded_hops( } pub(super) fn compute_payinfo( - intermediate_nodes: &[(PublicKey, ForwardTlvs)], payee_tlvs: &ReceiveTlvs + intermediate_nodes: &[(PublicKey, ForwardTlvs)], payee_tlvs: &ReceiveTlvs, htlc_maximum_msat: u64 ) -> Result { let mut curr_base_fee: u128 = 0; let mut curr_prop_mil: u128 = 0; @@ -211,12 +211,13 @@ pub(super) fn compute_payinfo( htlc_minimum_msat = core::cmp::max(payee_tlvs.payment_constraints.htlc_minimum_msat as u128, htlc_minimum_msat); + if (htlc_maximum_msat as u128) < htlc_minimum_msat { return Err(()) } Ok(BlindedPayInfo { fee_base_msat: u32::try_from(curr_base_fee).map_err(|_| ())?, fee_proportional_millionths: u32::try_from(curr_prop_mil).map_err(|_| ())?, cltv_expiry_delta, htlc_minimum_msat: u64::try_from(htlc_minimum_msat).map_err(|_| ())?, - htlc_maximum_msat: 21_000_000 * 100_000_000 * 1_000, // TODO + htlc_maximum_msat, features: BlindedHopFeatures::empty(), }) } @@ -276,11 +277,13 @@ mod tests { htlc_minimum_msat: 1, }, }; - let blinded_payinfo = super::compute_payinfo(&intermediate_nodes[..], &recv_tlvs).unwrap(); + let htlc_maximum_msat = 100_000; + let blinded_payinfo = super::compute_payinfo(&intermediate_nodes[..], &recv_tlvs, htlc_maximum_msat).unwrap(); assert_eq!(blinded_payinfo.fee_base_msat, 201); assert_eq!(blinded_payinfo.fee_proportional_millionths, 1001); assert_eq!(blinded_payinfo.cltv_expiry_delta, 288); assert_eq!(blinded_payinfo.htlc_minimum_msat, 900); + assert_eq!(blinded_payinfo.htlc_maximum_msat, htlc_maximum_msat); } #[test] @@ -292,11 +295,12 @@ mod tests { htlc_minimum_msat: 1, }, }; - let blinded_payinfo = super::compute_payinfo(&[], &recv_tlvs).unwrap(); + let blinded_payinfo = super::compute_payinfo(&[], &recv_tlvs, 4242).unwrap(); assert_eq!(blinded_payinfo.fee_base_msat, 0); assert_eq!(blinded_payinfo.fee_proportional_millionths, 0); assert_eq!(blinded_payinfo.cltv_expiry_delta, 0); assert_eq!(blinded_payinfo.htlc_minimum_msat, 1); + assert_eq!(blinded_payinfo.htlc_maximum_msat, 4242); } #[test] @@ -336,7 +340,8 @@ mod tests { htlc_minimum_msat: 3, }, }; - let blinded_payinfo = super::compute_payinfo(&intermediate_nodes[..], &recv_tlvs).unwrap(); + let htlc_maximum_msat = 100_000; + let blinded_payinfo = super::compute_payinfo(&intermediate_nodes[..], &recv_tlvs, htlc_maximum_msat).unwrap(); assert_eq!(blinded_payinfo.htlc_minimum_msat, 2_000); } @@ -377,8 +382,12 @@ mod tests { htlc_minimum_msat: 1, }, }; + let htlc_minimum_msat = 3797; + assert!(super::compute_payinfo(&intermediate_nodes[..], &recv_tlvs, htlc_minimum_msat - 1).is_err()); - let blinded_payinfo = super::compute_payinfo(&intermediate_nodes[..], &recv_tlvs).unwrap(); - assert_eq!(blinded_payinfo.htlc_minimum_msat, 3797); + let htlc_maximum_msat = htlc_minimum_msat + 1; + let blinded_payinfo = super::compute_payinfo(&intermediate_nodes[..], &recv_tlvs, htlc_maximum_msat).unwrap(); + assert_eq!(blinded_payinfo.htlc_minimum_msat, htlc_minimum_msat); + assert_eq!(blinded_payinfo.htlc_maximum_msat, htlc_maximum_msat); } }