From c8fd681d4f269f0fd3f683de8c5b1f9842a6600a Mon Sep 17 00:00:00 2001 From: Jeffrey Czyz Date: Thu, 11 Jul 2024 15:18:40 -0500 Subject: [PATCH] Refactor initial route finding to separate method This will be used later in send_payment_for_bolt12_invoice instead of find_route_and_send_payment as it will allow for returning RetryableSendFailure when path finding fails. --- lightning/src/ln/outbound_payment.rs | 54 +++++++++++++++++++--------- 1 file changed, 37 insertions(+), 17 deletions(-) diff --git a/lightning/src/ln/outbound_payment.rs b/lightning/src/ln/outbound_payment.rs index 42c100cde..2c929952e 100644 --- a/lightning/src/ln/outbound_payment.rs +++ b/lightning/src/ln/outbound_payment.rs @@ -925,25 +925,17 @@ impl OutboundPayments { !pmt.is_awaiting_invoice()) } - /// Errors immediately on [`RetryableSendFailure`] error conditions. Otherwise, further errors may - /// be surfaced asynchronously via [`Event::PaymentPathFailed`] and [`Event::PaymentFailed`]. - /// - /// [`Event::PaymentPathFailed`]: crate::events::Event::PaymentPathFailed - /// [`Event::PaymentFailed`]: crate::events::Event::PaymentFailed - fn send_payment_internal( - &self, payment_id: PaymentId, payment_hash: PaymentHash, recipient_onion: RecipientOnionFields, - keysend_preimage: Option, retry_strategy: Retry, mut route_params: RouteParameters, - router: &R, first_hops: Vec, inflight_htlcs: IH, entropy_source: &ES, - node_signer: &NS, best_block_height: u32, logger: &L, - pending_events: &Mutex)>>, send_payment_along_path: SP, - ) -> Result<(), RetryableSendFailure> + fn find_initial_route( + &self, payment_id: PaymentId, payment_hash: PaymentHash, + recipient_onion: &RecipientOnionFields, keysend_preimage: Option, + route_params: &mut RouteParameters, router: &R, first_hops: &Vec, + inflight_htlcs: &IH, node_signer: &NS, best_block_height: u32, logger: &L, + ) -> Result where R::Target: Router, - ES::Target: EntropySource, NS::Target: NodeSigner, L::Target: Logger, IH: Fn() -> InFlightHtlcs, - SP: Fn(SendAlongPathArgs) -> Result<(), APIError>, { #[cfg(feature = "std")] { if has_expired(&route_params) { @@ -954,7 +946,7 @@ impl OutboundPayments { } onion_utils::set_max_path_length( - &mut route_params, &recipient_onion, keysend_preimage, best_block_height + route_params, recipient_onion, keysend_preimage, best_block_height ) .map_err(|()| { log_error!(logger, "Can't construct an onion packet without exceeding 1300-byte onion \ @@ -963,7 +955,7 @@ impl OutboundPayments { })?; let mut route = router.find_route_with_id( - &node_signer.get_node_id(Recipient::Node).unwrap(), &route_params, + &node_signer.get_node_id(Recipient::Node).unwrap(), route_params, Some(&first_hops.iter().collect::>()), inflight_htlcs(), payment_hash, payment_id, ).map_err(|_| { @@ -972,12 +964,40 @@ impl OutboundPayments { RetryableSendFailure::RouteNotFound })?; - if route.route_params.as_ref() != Some(&route_params) { + if route.route_params.as_ref() != Some(route_params) { debug_assert!(false, "Routers are expected to return a Route which includes the requested RouteParameters"); route.route_params = Some(route_params.clone()); } + Ok(route) + } + + /// Errors immediately on [`RetryableSendFailure`] error conditions. Otherwise, further errors may + /// be surfaced asynchronously via [`Event::PaymentPathFailed`] and [`Event::PaymentFailed`]. + /// + /// [`Event::PaymentPathFailed`]: crate::events::Event::PaymentPathFailed + /// [`Event::PaymentFailed`]: crate::events::Event::PaymentFailed + fn send_payment_internal( + &self, payment_id: PaymentId, payment_hash: PaymentHash, recipient_onion: RecipientOnionFields, + keysend_preimage: Option, retry_strategy: Retry, mut route_params: RouteParameters, + router: &R, first_hops: Vec, inflight_htlcs: IH, entropy_source: &ES, + node_signer: &NS, best_block_height: u32, logger: &L, + pending_events: &Mutex)>>, send_payment_along_path: SP, + ) -> Result<(), RetryableSendFailure> + where + R::Target: Router, + ES::Target: EntropySource, + NS::Target: NodeSigner, + L::Target: Logger, + IH: Fn() -> InFlightHtlcs, + SP: Fn(SendAlongPathArgs) -> Result<(), APIError>, + { + let route = self.find_initial_route( + payment_id, payment_hash, &recipient_onion, keysend_preimage, &mut route_params, router, + &first_hops, &inflight_htlcs, node_signer, best_block_height, logger, + )?; + let onion_session_privs = self.add_new_pending_payment(payment_hash, recipient_onion.clone(), payment_id, keysend_preimage, &route, Some(retry_strategy), Some(route_params.payment_params.clone()), entropy_source, best_block_height) -- 2.39.5