]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Refactor initial route finding to separate method
authorJeffrey Czyz <jkczyz@gmail.com>
Thu, 11 Jul 2024 20:18:40 +0000 (15:18 -0500)
committerJeffrey Czyz <jkczyz@gmail.com>
Thu, 11 Jul 2024 23:05:10 +0000 (18:05 -0500)
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

index 42c100cde2813262b471d04bc9b70d56c4a8b8bf..2c929952e98a6b19d6df251d5efb5ebbc63a3670 100644 (file)
@@ -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<R: Deref, NS: Deref, ES: Deref, IH, SP, L: Deref>(
-               &self, payment_id: PaymentId, payment_hash: PaymentHash, recipient_onion: RecipientOnionFields,
-               keysend_preimage: Option<PaymentPreimage>, retry_strategy: Retry, mut route_params: RouteParameters,
-               router: &R, first_hops: Vec<ChannelDetails>, inflight_htlcs: IH, entropy_source: &ES,
-               node_signer: &NS, best_block_height: u32, logger: &L,
-               pending_events: &Mutex<VecDeque<(events::Event, Option<EventCompletionAction>)>>, send_payment_along_path: SP,
-       ) -> Result<(), RetryableSendFailure>
+       fn find_initial_route<R: Deref, NS: Deref, IH, L: Deref>(
+               &self, payment_id: PaymentId, payment_hash: PaymentHash,
+               recipient_onion: &RecipientOnionFields, keysend_preimage: Option<PaymentPreimage>,
+               route_params: &mut RouteParameters, router: &R, first_hops: &Vec<ChannelDetails>,
+               inflight_htlcs: &IH, node_signer: &NS, best_block_height: u32, logger: &L,
+       ) -> Result<Route, RetryableSendFailure>
        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::<Vec<_>>()), 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<R: Deref, NS: Deref, ES: Deref, IH, SP, L: Deref>(
+               &self, payment_id: PaymentId, payment_hash: PaymentHash, recipient_onion: RecipientOnionFields,
+               keysend_preimage: Option<PaymentPreimage>, retry_strategy: Retry, mut route_params: RouteParameters,
+               router: &R, first_hops: Vec<ChannelDetails>, inflight_htlcs: IH, entropy_source: &ES,
+               node_signer: &NS, best_block_height: u32, logger: &L,
+               pending_events: &Mutex<VecDeque<(events::Event, Option<EventCompletionAction>)>>, 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)