Skip to content

Commit f00c4af

Browse files
committed
Refactor OutboundPayments::retry_payment_internal
Consolidate the creation and insertion of onion_session_privs to the PendingOutboundPayment::Retryable arm. In an upcoming commit, this method will be reused for an initial BOLT 12 invoice payment. However, onion_session_privs are created using a different helper.
1 parent bdc21c9 commit f00c4af

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

lightning/src/ln/outbound_payment.rs

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -839,11 +839,6 @@ impl OutboundPayments {
839839
}
840840
}
841841

842-
let mut onion_session_privs = Vec::with_capacity(route.paths.len());
843-
for _ in 0..route.paths.len() {
844-
onion_session_privs.push(entropy_source.get_secure_random_bytes());
845-
}
846-
847842
macro_rules! abandon_with_entry {
848843
($payment: expr, $reason: expr) => {
849844
$payment.get_mut().mark_abandoned($reason);
@@ -859,11 +854,11 @@ impl OutboundPayments {
859854
}
860855
}
861856
}
862-
let (total_msat, recipient_onion, keysend_preimage) = {
857+
let (total_msat, recipient_onion, keysend_preimage, onion_session_privs) = {
863858
let mut outbounds = self.pending_outbound_payments.lock().unwrap();
864859
match outbounds.entry(payment_id) {
865860
hash_map::Entry::Occupied(mut payment) => {
866-
let res = match payment.get() {
861+
match payment.get() {
867862
PendingOutboundPayment::Retryable {
868863
total_msat, keysend_preimage, payment_secret, payment_metadata,
869864
custom_tlvs, pending_amt_msat, ..
@@ -875,11 +870,33 @@ impl OutboundPayments {
875870
abandon_with_entry!(payment, PaymentFailureReason::UnexpectedError);
876871
return
877872
}
878-
(*total_msat, RecipientOnionFields {
879-
payment_secret: *payment_secret,
880-
payment_metadata: payment_metadata.clone(),
881-
custom_tlvs: custom_tlvs.clone(),
882-
}, *keysend_preimage)
873+
874+
if !payment.get().is_retryable_now() {
875+
log_error!(logger, "Retries exhausted for payment id {}", &payment_id);
876+
abandon_with_entry!(payment, PaymentFailureReason::RetriesExhausted);
877+
return
878+
}
879+
880+
let total_msat = *total_msat;
881+
let recipient_onion = RecipientOnionFields {
882+
payment_secret: *payment_secret,
883+
payment_metadata: payment_metadata.clone(),
884+
custom_tlvs: custom_tlvs.clone(),
885+
};
886+
let keysend_preimage = *keysend_preimage;
887+
888+
let mut onion_session_privs = Vec::with_capacity(route.paths.len());
889+
for _ in 0..route.paths.len() {
890+
onion_session_privs.push(entropy_source.get_secure_random_bytes());
891+
}
892+
893+
for (path, session_priv_bytes) in route.paths.iter().zip(onion_session_privs.iter()) {
894+
assert!(payment.get_mut().insert(*session_priv_bytes, path));
895+
}
896+
897+
payment.get_mut().increment_attempts();
898+
899+
(total_msat, recipient_onion, keysend_preimage, onion_session_privs)
883900
},
884901
PendingOutboundPayment::Legacy { .. } => {
885902
log_error!(logger, "Unable to retry payments that were initially sent on LDK versions prior to 0.0.102");
@@ -899,17 +916,7 @@ impl OutboundPayments {
899916
log_error!(logger, "Payment already abandoned (with some HTLCs still pending)");
900917
return
901918
},
902-
};
903-
if !payment.get().is_retryable_now() {
904-
log_error!(logger, "Retries exhausted for payment id {}", &payment_id);
905-
abandon_with_entry!(payment, PaymentFailureReason::RetriesExhausted);
906-
return
907-
}
908-
payment.get_mut().increment_attempts();
909-
for (path, session_priv_bytes) in route.paths.iter().zip(onion_session_privs.iter()) {
910-
assert!(payment.get_mut().insert(*session_priv_bytes, path));
911919
}
912-
res
913920
},
914921
hash_map::Entry::Vacant(_) => {
915922
log_error!(logger, "Payment with ID {} not found", &payment_id);

0 commit comments

Comments
 (0)