Skip to content

Commit

Permalink
Set htlc_maximum_msat in BlindedPayInfo on route construction
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinewallace committed Aug 16, 2023
1 parent dc4f471 commit 9d481d1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
5 changes: 3 additions & 2 deletions lightning/src/blinded_path/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ impl BlindedPath {
/// * any unknown features are required in the provided [`BlindedPaymentTlvs`]
// TODO: make all payloads the same size with padding + add dummy hops
pub fn new_for_payment<ES: EntropySource, T: secp256k1::Signing + secp256k1::Verification>(
path: &[(PublicKey, BlindedPaymentTlvs)], entropy_source: &ES, secp_ctx: &Secp256k1<T>
path: &[(PublicKey, BlindedPaymentTlvs)], htlc_maximum_msat: u64, entropy_source: &ES,
secp_ctx: &Secp256k1<T>
) -> Result<(BlindedPayInfo, Self), ()> {
if path.len() < 1 { return Err(()) }
let mut found_recv_payload = false;
Expand All @@ -104,7 +105,7 @@ impl BlindedPath {
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(path)?;
let blinded_payinfo = payment::compute_payinfo(path, htlc_maximum_msat)?;
Ok((blinded_payinfo, BlindedPath {
introduction_node_id: path[0].0,
blinding_point: PublicKey::from_secret_key(secp_ctx, &blinding_secret),
Expand Down
20 changes: 11 additions & 9 deletions lightning/src/blinded_path/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
}

pub(super) fn compute_payinfo(
path: &[(PublicKey, BlindedPaymentTlvs)]
path: &[(PublicKey, BlindedPaymentTlvs)], htlc_maximum_msat: u64,
) -> Result<BlindedPayInfo, ()> {
let mut curr_base_fee: u32 = 0;
let mut curr_prop_mil: u32 = 0;
Expand All @@ -195,14 +195,15 @@ pub(super) fn compute_payinfo(
.map(|f| f / 1_000_000)
.ok_or(())?;
}
let htlc_minimum_msat = path.iter().map(|(_, tlvs)| tlvs.htlc_minimum_msat()).max().unwrap_or(0);
if htlc_maximum_msat < htlc_minimum_msat { return Err(()) }
Ok(BlindedPayInfo {
fee_base_msat: curr_base_fee,
fee_proportional_millionths: curr_prop_mil,
cltv_expiry_delta: path.iter().map(|(_, tlvs)| tlvs.cltv_expiry_delta())
.try_fold(0u16, |acc, delta| acc.checked_add(delta)).ok_or(())?,
htlc_minimum_msat: path.iter().map(|(_, tlvs)| tlvs.htlc_minimum_msat()).max().unwrap_or(0),
// TODO: this field isn't present in route blinding encrypted data
htlc_maximum_msat: 21_000_000 * 100_000_000 * 1_000, // Total bitcoin supply
htlc_minimum_msat,
htlc_maximum_msat,
features: BlindedHopFeatures::empty(),
})
}
Expand Down Expand Up @@ -264,11 +265,12 @@ mod tests {
htlc_minimum_msat: 1,
},
})];
let blinded_payinfo = super::compute_payinfo(&path[..]).unwrap();
let blinded_payinfo = super::compute_payinfo(&path[..], 4242).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, 1_000);
assert_eq!(blinded_payinfo.htlc_maximum_msat, 4242);
}

#[test]
Expand All @@ -281,7 +283,7 @@ mod tests {
htlc_minimum_msat: 1,
},
})];
let blinded_payinfo = super::compute_payinfo(&path[..]).unwrap();
let blinded_payinfo = super::compute_payinfo(&path[..], 42).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);
Expand Down Expand Up @@ -313,7 +315,7 @@ mod tests {
},
features: BlindedHopFeatures::empty(),
})];
assert!(BlindedPath::new_for_payment(&out_of_order_payloads_path[..], &keys_manager, &secp_ctx).is_err());
assert!(BlindedPath::new_for_payment(&out_of_order_payloads_path[..], 1_000_000, &keys_manager, &secp_ctx).is_err());

let multiple_recv_payloads_path = vec![(dummy_pk, BlindedPaymentTlvs::Receive {
payment_secret: PaymentSecret([0; 32]),
Expand All @@ -328,7 +330,7 @@ mod tests {
htlc_minimum_msat: 1,
},
})];
assert!(BlindedPath::new_for_payment(&multiple_recv_payloads_path[..], &keys_manager, &secp_ctx).is_err());
assert!(BlindedPath::new_for_payment(&multiple_recv_payloads_path[..], 1_000_000, &keys_manager, &secp_ctx).is_err());

let missing_recv_payload_path = vec![(dummy_pk, BlindedPaymentTlvs::Forward {
short_channel_id: 0,
Expand All @@ -343,6 +345,6 @@ mod tests {
},
features: BlindedHopFeatures::empty(),
})];
assert!(BlindedPath::new_for_payment(&missing_recv_payload_path[..], &keys_manager, &secp_ctx).is_err());
assert!(BlindedPath::new_for_payment(&missing_recv_payload_path[..], 1_000_000, &keys_manager, &secp_ctx).is_err());
}
}

0 comments on commit 9d481d1

Please sign in to comment.