+ let res = self.pay_route_internal(&route, payment_hash, recipient_onion, keysend_preimage,
+ payment_id, Some(total_msat), onion_session_privs, node_signer, best_block_height,
+ &send_payment_along_path);
+ log_info!(logger, "Result retrying payment id {}: {:?}", log_bytes!(payment_id.0), res);
+ if let Err(e) = res {
+ self.handle_pay_route_err(e, payment_id, payment_hash, route, route_params, router, first_hops, inflight_htlcs, entropy_source, node_signer, best_block_height, logger, pending_events, send_payment_along_path);
+ }
+ }
+
+ fn handle_pay_route_err<R: Deref, NS: Deref, ES: Deref, IH, SP, L: Deref>(
+ &self, err: PaymentSendFailure, payment_id: PaymentId, payment_hash: PaymentHash, route: Route,
+ 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<Vec<events::Event>>, send_payment_along_path: &SP,
+ )
+ where
+ R::Target: Router,
+ ES::Target: EntropySource,
+ NS::Target: NodeSigner,
+ L::Target: Logger,
+ IH: Fn() -> InFlightHtlcs,
+ SP: Fn(&Path, &PaymentHash, RecipientOnionFields, u64, u32, PaymentId,
+ &Option<PaymentPreimage>, [u8; 32]) -> Result<(), APIError>
+ {
+ match err {
+ PaymentSendFailure::AllFailedResendSafe(errs) => {
+ Self::push_path_failed_evs_and_scids(payment_id, payment_hash, &mut route_params, route.paths, errs.into_iter().map(|e| Err(e)), logger, pending_events);
+ self.retry_payment_internal(payment_hash, payment_id, route_params, router, first_hops, inflight_htlcs, entropy_source, node_signer, best_block_height, logger, pending_events, send_payment_along_path);
+ },
+ PaymentSendFailure::PartialFailure { failed_paths_retry: Some(mut retry), results, .. } => {
+ Self::push_path_failed_evs_and_scids(payment_id, payment_hash, &mut retry, route.paths, results.into_iter(), logger, pending_events);
+ // Some paths were sent, even if we failed to send the full MPP value our recipient may
+ // misbehave and claim the funds, at which point we have to consider the payment sent, so
+ // return `Ok()` here, ignoring any retry errors.
+ self.retry_payment_internal(payment_hash, payment_id, retry, router, first_hops, inflight_htlcs, entropy_source, node_signer, best_block_height, logger, pending_events, send_payment_along_path);
+ },
+ PaymentSendFailure::PartialFailure { failed_paths_retry: None, .. } => {
+ // This may happen if we send a payment and some paths fail, but only due to a temporary
+ // monitor failure or the like, implying they're really in-flight, but we haven't sent the
+ // initial HTLC-Add messages yet.
+ },
+ PaymentSendFailure::PathParameterError(results) => {
+ log_error!(logger, "Failed to send to route due to parameter error in a single path. Your router is buggy");
+ Self::push_path_failed_evs_and_scids(payment_id, payment_hash, &mut route_params, route.paths, results.into_iter(), logger, pending_events);
+ self.abandon_payment(payment_id, PaymentFailureReason::UnexpectedError, pending_events);
+ },
+ PaymentSendFailure::ParameterError(e) => {
+ log_error!(logger, "Failed to send to route due to parameter error: {:?}. Your router is buggy", e);
+ self.abandon_payment(payment_id, PaymentFailureReason::UnexpectedError, pending_events);
+ },
+ PaymentSendFailure::DuplicatePayment => debug_assert!(false), // unreachable
+ }
+ }
+
+ fn push_path_failed_evs_and_scids<I: ExactSizeIterator + Iterator<Item = Result<(), APIError>>, L: Deref>(
+ payment_id: PaymentId, payment_hash: PaymentHash, route_params: &mut RouteParameters,
+ paths: Vec<Path>, path_results: I, logger: &L, pending_events: &Mutex<Vec<events::Event>>
+ ) where L::Target: Logger {
+ let mut events = pending_events.lock().unwrap();
+ debug_assert_eq!(paths.len(), path_results.len());
+ for (path, path_res) in paths.into_iter().zip(path_results) {
+ if let Err(e) = path_res {
+ if let APIError::MonitorUpdateInProgress = e { continue }
+ log_error!(logger, "Failed to send along path due to error: {:?}", e);
+ let mut failed_scid = None;
+ if let APIError::ChannelUnavailable { .. } = e {
+ let scid = path.hops[0].short_channel_id;
+ failed_scid = Some(scid);
+ route_params.payment_params.previously_failed_channels.push(scid);
+ }
+ events.push(events::Event::PaymentPathFailed {
+ payment_id: Some(payment_id),
+ payment_hash,
+ payment_failed_permanently: false,
+ failure: events::PathFailure::InitialSend { err: e },
+ path,
+ short_channel_id: failed_scid,
+ #[cfg(test)]
+ error_code: None,
+ #[cfg(test)]
+ error_data: None,
+ });
+ }
+ }