Skip to content

Commit

Permalink
Set htlc_maximum_msat in BlindedPayInfo on blinded path construction
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinewallace committed Aug 24, 2023
1 parent 7174491 commit b159729
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 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 @@ -87,12 +87,13 @@ impl BlindedPath {
// TODO: make all payloads the same size with padding + add dummy hops
pub fn new_for_payment<ES: EntropySource, T: secp256k1::Signing + secp256k1::Verification>(
intermediate_nodes: &[(PublicKey, payment::ForwardTlvs)], payee_node_id: PublicKey,
payee_tlvs: payment::ReceiveTlvs, entropy_source: &ES, secp_ctx: &Secp256k1<T>
payee_tlvs: payment::ReceiveTlvs, htlc_maximum_msat: u64, entropy_source: &ES,
secp_ctx: &Secp256k1<T>
) -> 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),
Expand Down
23 changes: 16 additions & 7 deletions lightning/src/blinded_path/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
}

pub(super) fn compute_payinfo(
intermediate_nodes: &[(PublicKey, ForwardTlvs)], payee_tlvs: &ReceiveTlvs
intermediate_nodes: &[(PublicKey, ForwardTlvs)], payee_tlvs: &ReceiveTlvs, htlc_maximum_msat: u64
) -> Result<BlindedPayInfo, ()> {
let mut curr_base_fee: u64 = 0;
let mut curr_prop_mil: u64 = 0;
Expand Down Expand Up @@ -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(),
})
}
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}
}

0 comments on commit b159729

Please sign in to comment.