Skip to content

Commit 72a7da8

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 686ef08 commit 72a7da8

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

lightning/src/ln/outbound_payment.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,9 @@ 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| { self.remove_outbound_if_all_failed(payment_id, &e); e })
384386
}
385387

386388
pub(super) fn send_spontaneous_payment<ES: Deref, NS: Deref, F>(
@@ -402,7 +404,10 @@ impl OutboundPayments {
402404

403405
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) {
404406
Ok(()) => Ok(payment_hash),
405-
Err(e) => Err(e)
407+
Err(e) => {
408+
self.remove_outbound_if_all_failed(payment_id, &e);
409+
Err(e)
410+
}
406411
}
407412
}
408413

@@ -501,7 +506,10 @@ impl OutboundPayments {
501506

502507
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) {
503508
Ok(()) => Ok((payment_hash, payment_id)),
504-
Err(e) => Err(e)
509+
Err(e) => {
510+
self.remove_outbound_if_all_failed(payment_id, &e);
511+
Err(e)
512+
}
505513
}
506514
}
507515

@@ -648,10 +656,6 @@ impl OutboundPayments {
648656
} else { None },
649657
})
650658
} 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");
655659
Err(PaymentSendFailure::AllFailedResendSafe(results.drain(..).map(|r| r.unwrap_err()).collect()))
656660
} else {
657661
Ok(())
@@ -673,6 +677,16 @@ impl OutboundPayments {
673677
self.send_payment_internal(route, payment_hash, payment_secret, keysend_preimage, payment_id,
674678
recv_value_msat, onion_session_privs, node_signer, best_block_height,
675679
send_payment_along_path)
680+
.map_err(|e| { self.remove_outbound_if_all_failed(payment_id, &e); e })
681+
}
682+
683+
// If we failed to send any paths, we should remove the new PaymentId from the
684+
// `pending_outbound_payments` map, as the user isn't expected to `abandon_payment`.
685+
fn remove_outbound_if_all_failed(&self, payment_id: PaymentId, err: &PaymentSendFailure) {
686+
if let &PaymentSendFailure::AllFailedResendSafe(_) = err {
687+
let removed = self.pending_outbound_payments.lock().unwrap().remove(&payment_id).is_some();
688+
debug_assert!(removed, "We should always have a pending payment to remove here");
689+
}
676690
}
677691

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

0 commit comments

Comments
 (0)