Skip to content

Commit c8fd681

Browse files
committed
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.
1 parent aa55366 commit c8fd681

File tree

1 file changed

+37
-17
lines changed

1 file changed

+37
-17
lines changed

lightning/src/ln/outbound_payment.rs

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -925,25 +925,17 @@ impl OutboundPayments {
925925
!pmt.is_awaiting_invoice())
926926
}
927927

928-
/// Errors immediately on [`RetryableSendFailure`] error conditions. Otherwise, further errors may
929-
/// be surfaced asynchronously via [`Event::PaymentPathFailed`] and [`Event::PaymentFailed`].
930-
///
931-
/// [`Event::PaymentPathFailed`]: crate::events::Event::PaymentPathFailed
932-
/// [`Event::PaymentFailed`]: crate::events::Event::PaymentFailed
933-
fn send_payment_internal<R: Deref, NS: Deref, ES: Deref, IH, SP, L: Deref>(
934-
&self, payment_id: PaymentId, payment_hash: PaymentHash, recipient_onion: RecipientOnionFields,
935-
keysend_preimage: Option<PaymentPreimage>, retry_strategy: Retry, mut route_params: RouteParameters,
936-
router: &R, first_hops: Vec<ChannelDetails>, inflight_htlcs: IH, entropy_source: &ES,
937-
node_signer: &NS, best_block_height: u32, logger: &L,
938-
pending_events: &Mutex<VecDeque<(events::Event, Option<EventCompletionAction>)>>, send_payment_along_path: SP,
939-
) -> Result<(), RetryableSendFailure>
928+
fn find_initial_route<R: Deref, NS: Deref, IH, L: Deref>(
929+
&self, payment_id: PaymentId, payment_hash: PaymentHash,
930+
recipient_onion: &RecipientOnionFields, keysend_preimage: Option<PaymentPreimage>,
931+
route_params: &mut RouteParameters, router: &R, first_hops: &Vec<ChannelDetails>,
932+
inflight_htlcs: &IH, node_signer: &NS, best_block_height: u32, logger: &L,
933+
) -> Result<Route, RetryableSendFailure>
940934
where
941935
R::Target: Router,
942-
ES::Target: EntropySource,
943936
NS::Target: NodeSigner,
944937
L::Target: Logger,
945938
IH: Fn() -> InFlightHtlcs,
946-
SP: Fn(SendAlongPathArgs) -> Result<(), APIError>,
947939
{
948940
#[cfg(feature = "std")] {
949941
if has_expired(&route_params) {
@@ -954,7 +946,7 @@ impl OutboundPayments {
954946
}
955947

956948
onion_utils::set_max_path_length(
957-
&mut route_params, &recipient_onion, keysend_preimage, best_block_height
949+
route_params, recipient_onion, keysend_preimage, best_block_height
958950
)
959951
.map_err(|()| {
960952
log_error!(logger, "Can't construct an onion packet without exceeding 1300-byte onion \
@@ -963,7 +955,7 @@ impl OutboundPayments {
963955
})?;
964956

965957
let mut route = router.find_route_with_id(
966-
&node_signer.get_node_id(Recipient::Node).unwrap(), &route_params,
958+
&node_signer.get_node_id(Recipient::Node).unwrap(), route_params,
967959
Some(&first_hops.iter().collect::<Vec<_>>()), inflight_htlcs(),
968960
payment_hash, payment_id,
969961
).map_err(|_| {
@@ -972,12 +964,40 @@ impl OutboundPayments {
972964
RetryableSendFailure::RouteNotFound
973965
})?;
974966

975-
if route.route_params.as_ref() != Some(&route_params) {
967+
if route.route_params.as_ref() != Some(route_params) {
976968
debug_assert!(false,
977969
"Routers are expected to return a Route which includes the requested RouteParameters");
978970
route.route_params = Some(route_params.clone());
979971
}
980972

973+
Ok(route)
974+
}
975+
976+
/// Errors immediately on [`RetryableSendFailure`] error conditions. Otherwise, further errors may
977+
/// be surfaced asynchronously via [`Event::PaymentPathFailed`] and [`Event::PaymentFailed`].
978+
///
979+
/// [`Event::PaymentPathFailed`]: crate::events::Event::PaymentPathFailed
980+
/// [`Event::PaymentFailed`]: crate::events::Event::PaymentFailed
981+
fn send_payment_internal<R: Deref, NS: Deref, ES: Deref, IH, SP, L: Deref>(
982+
&self, payment_id: PaymentId, payment_hash: PaymentHash, recipient_onion: RecipientOnionFields,
983+
keysend_preimage: Option<PaymentPreimage>, retry_strategy: Retry, mut route_params: RouteParameters,
984+
router: &R, first_hops: Vec<ChannelDetails>, inflight_htlcs: IH, entropy_source: &ES,
985+
node_signer: &NS, best_block_height: u32, logger: &L,
986+
pending_events: &Mutex<VecDeque<(events::Event, Option<EventCompletionAction>)>>, send_payment_along_path: SP,
987+
) -> Result<(), RetryableSendFailure>
988+
where
989+
R::Target: Router,
990+
ES::Target: EntropySource,
991+
NS::Target: NodeSigner,
992+
L::Target: Logger,
993+
IH: Fn() -> InFlightHtlcs,
994+
SP: Fn(SendAlongPathArgs) -> Result<(), APIError>,
995+
{
996+
let route = self.find_initial_route(
997+
payment_id, payment_hash, &recipient_onion, keysend_preimage, &mut route_params, router,
998+
&first_hops, &inflight_htlcs, node_signer, best_block_height, logger,
999+
)?;
1000+
9811001
let onion_session_privs = self.add_new_pending_payment(payment_hash,
9821002
recipient_onion.clone(), payment_id, keysend_preimage, &route, Some(retry_strategy),
9831003
Some(route_params.payment_params.clone()), entropy_source, best_block_height)

0 commit comments

Comments
 (0)