Skip to content

Commit 84a5066

Browse files
Remove AllPathsFailed outbounds at send_payment_internal callsites instead
This makes it easier to retry payments if all paths fail on initial send, in in which case we'll want to hold off on removing the pending payment
1 parent 74c8fbb commit 84a5066

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

lightning/src/ln/outbound_payment.rs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,14 @@ impl OutboundPayments {
380380
u32, PaymentId, &Option<PaymentPreimage>, [u8; 32]) -> Result<(), APIError>
381381
{
382382
let onion_session_privs = self.add_new_pending_payment(payment_hash, *payment_secret, payment_id, route, Retry::Attempts(0), None, entropy_source, best_block_height)?;
383-
self.send_payment_internal(route, payment_hash, payment_secret, None, payment_id, None, onion_session_privs, node_signer, best_block_height, send_payment_along_path)
383+
self.send_payment_internal(route, payment_hash, payment_secret, None, payment_id, None,
384+
onion_session_privs, node_signer, best_block_height, send_payment_along_path)
385+
.map_err(|e| { // TODO: use inspect_err instead when it's within MSRV
386+
if let PaymentSendFailure::AllFailedResendSafe(_) = e {
387+
self.all_failed_remove_outbound(payment_id);
388+
}
389+
e
390+
})
384391
}
385392

386393
pub(super) fn send_spontaneous_payment<ES: Deref, NS: Deref, F>(
@@ -402,7 +409,12 @@ impl OutboundPayments {
402409

403410
match self.send_payment_internal(route, payment_hash, &None, Some(preimage), payment_id, None, onion_session_privs, node_signer, best_block_height, send_payment_along_path) {
404411
Ok(()) => Ok(payment_hash),
405-
Err(e) => Err(e)
412+
Err(e) => {
413+
if let PaymentSendFailure::AllFailedResendSafe(_) = e {
414+
self.all_failed_remove_outbound(payment_id);
415+
}
416+
Err(e)
417+
}
406418
}
407419
}
408420

@@ -501,7 +513,12 @@ impl OutboundPayments {
501513

502514
match self.send_payment_internal(&route, payment_hash, &None, None, payment_id, None, onion_session_privs, node_signer, best_block_height, send_payment_along_path) {
503515
Ok(()) => Ok((payment_hash, payment_id)),
504-
Err(e) => Err(e)
516+
Err(e) => {
517+
if let PaymentSendFailure::AllFailedResendSafe(_) = e {
518+
self.all_failed_remove_outbound(payment_id);
519+
}
520+
Err(e)
521+
}
505522
}
506523
}
507524

@@ -648,10 +665,6 @@ impl OutboundPayments {
648665
} else { None },
649666
})
650667
} else if has_err {
651-
// If we failed to send any paths, we should remove the new PaymentId from the
652-
// `pending_outbound_payments` map, as the user isn't expected to `abandon_payment`.
653-
let removed = self.pending_outbound_payments.lock().unwrap().remove(&payment_id).is_some();
654-
debug_assert!(removed, "We should always have a pending payment to remove here");
655668
Err(PaymentSendFailure::AllFailedResendSafe(results.drain(..).map(|r| r.unwrap_err()).collect()))
656669
} else {
657670
Ok(())
@@ -673,6 +686,19 @@ impl OutboundPayments {
673686
self.send_payment_internal(route, payment_hash, payment_secret, keysend_preimage, payment_id,
674687
recv_value_msat, onion_session_privs, node_signer, best_block_height,
675688
send_payment_along_path)
689+
.map_err(|e| { // TODO: use inspect_err instead when it's within MSRV
690+
if let PaymentSendFailure::AllFailedResendSafe(_) = e {
691+
self.all_failed_remove_outbound(payment_id);
692+
}
693+
e
694+
})
695+
}
696+
697+
// If we failed to send any paths, we should remove the new PaymentId from the
698+
// `pending_outbound_payments` map, as the user isn't expected to `abandon_payment`.
699+
fn all_failed_remove_outbound(&self, payment_id: PaymentId) {
700+
let removed = self.pending_outbound_payments.lock().unwrap().remove(&payment_id).is_some();
701+
debug_assert!(removed, "We should always have a pending payment to remove here");
676702
}
677703

678704
pub(super) fn claim_htlc<L: Deref>(

0 commit comments

Comments
 (0)