Skip to content

Commit

Permalink
Utilize ManualRoutingParameters to create new RouteParams
Browse files Browse the repository at this point in the history
In addition to using PaymentParameters from the invoice, this commit
enables the creation of RouteParameters using the values optionally
set in ManualRoutingParameters.
  • Loading branch information
shaavan committed Oct 13, 2024
1 parent ab0cd6b commit 0fc35e1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
16 changes: 6 additions & 10 deletions lightning/src/ln/outbound_payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,21 +844,19 @@ impl OutboundPayments {
SP: Fn(SendAlongPathArgs) -> Result<(), APIError>,
{
let payment_hash = invoice.payment_hash();
let max_total_routing_fee_msat;
let routing_params;
let retry_strategy;
match self.pending_outbound_payments.lock().unwrap().entry(payment_id) {
hash_map::Entry::Occupied(entry) => match entry.get() {
PendingOutboundPayment::AwaitingInvoice {
retry_strategy: retry, manual_routing_params, ..
} => {
retry_strategy = *retry;
max_total_routing_fee_msat = manual_routing_params.map(
|params| params.max_total_routing_fee_msat
).flatten();
routing_params = manual_routing_params.unwrap_or_else(|| ManualRoutingParameters::new());
*entry.into_mut() = PendingOutboundPayment::InvoiceReceived {
payment_hash,
retry_strategy: *retry,
max_total_routing_fee_msat,
max_total_routing_fee_msat: routing_params.max_total_routing_fee_msat,
};
},
_ => return Err(Bolt12PaymentError::DuplicateInvoice),
Expand All @@ -873,12 +871,10 @@ impl OutboundPayments {
return Err(Bolt12PaymentError::UnknownRequiredFeatures);
}

let mut route_params = RouteParameters::from_payment_params_and_value(
PaymentParameters::from_bolt12_invoice(&invoice), invoice.amount_msats()
let route_params = RouteParameters::from_payment_and_manual_params(
PaymentParameters::from_bolt12_invoice(&invoice), invoice.amount_msats(), routing_params
);
if let Some(max_fee_msat) = max_total_routing_fee_msat {
route_params.max_total_routing_fee_msat = Some(max_fee_msat);
}

self.send_payment_for_bolt12_invoice_internal(
payment_id, payment_hash, None, route_params, retry_strategy, router, first_hops,
inflight_htlcs, entropy_source, node_signer, node_id_lookup, secp_ctx, best_block_height,
Expand Down
18 changes: 18 additions & 0 deletions lightning/src/routing/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,24 @@ impl RouteParameters {
Self { payment_params, final_value_msat, max_total_routing_fee_msat: Some(final_value_msat / 100 + 50_000) }
}

/// Constructs [`RouteParameters`] from the given [`PaymentParameters`], a payment amount,
/// and from the provided [`ManualRoutingParameters`].
///
/// [`Self::max_total_routing_fee_msat`] defaults to 1% of the payment amount + 50 sats
pub fn from_payment_and_manual_params(mut payment_params: PaymentParameters, final_value_msat: u64, manual_routing_params: ManualRoutingParameters) -> Self {
manual_routing_params.max_total_cltv_expiry_delta.map(|v| payment_params.max_total_cltv_expiry_delta = v);
manual_routing_params.max_path_count.map(|v| payment_params.max_path_count = v);
manual_routing_params.max_channel_saturation_power_of_half.map(|v| payment_params.max_channel_saturation_power_of_half = v);

let mut route_params = RouteParameters::from_payment_params_and_value(payment_params, final_value_msat);

manual_routing_params.max_total_routing_fee_msat.map(
|fee_msat| route_params.max_total_routing_fee_msat = Some(fee_msat)
);

route_params
}

/// Sets the maximum number of hops that can be included in a payment path, based on the provided
/// [`RecipientOnionFields`] and blinded paths.
pub fn set_max_path_length(
Expand Down

0 comments on commit 0fc35e1

Please sign in to comment.