Skip to content

Commit 6eb3711

Browse files
Support abandoning pending outbound async payments.
1 parent 99ade3d commit 6eb3711

File tree

1 file changed

+60
-14
lines changed

1 file changed

+60
-14
lines changed

lightning/src/ln/outbound_payment.rs

+60-14
Original file line numberDiff line numberDiff line change
@@ -214,20 +214,26 @@ impl PendingOutboundPayment {
214214
}
215215

216216
fn mark_abandoned(&mut self, reason: PaymentFailureReason) {
217-
if let PendingOutboundPayment::Retryable { session_privs, payment_hash, .. } = self {
218-
let mut our_session_privs = new_hash_set();
219-
core::mem::swap(&mut our_session_privs, session_privs);
220-
*self = PendingOutboundPayment::Abandoned {
221-
session_privs: our_session_privs,
222-
payment_hash: *payment_hash,
223-
reason: Some(reason)
224-
};
225-
} else if let PendingOutboundPayment::InvoiceReceived { payment_hash, .. } = self {
226-
*self = PendingOutboundPayment::Abandoned {
227-
session_privs: new_hash_set(),
228-
payment_hash: *payment_hash,
229-
reason: Some(reason)
230-
};
217+
let session_privs = match self {
218+
PendingOutboundPayment::Retryable { session_privs, .. } => {
219+
let mut our_session_privs = new_hash_set();
220+
core::mem::swap(&mut our_session_privs, session_privs);
221+
our_session_privs
222+
},
223+
_ => new_hash_set(),
224+
};
225+
match self {
226+
Self::Retryable { payment_hash, .. } |
227+
Self::InvoiceReceived { payment_hash, .. } |
228+
Self::StaticInvoiceReceived { payment_hash, .. } =>
229+
{
230+
*self = Self::Abandoned {
231+
session_privs,
232+
payment_hash: *payment_hash,
233+
reason: Some(reason),
234+
};
235+
},
236+
_ => {}
231237
}
232238
}
233239

@@ -2616,4 +2622,44 @@ mod tests {
26162622
reason: Some(PaymentFailureReason::PaymentExpired),
26172623
}, None));
26182624
}
2625+
2626+
#[test]
2627+
fn abandon_unreleased_async_payment() {
2628+
let pending_events = Mutex::new(VecDeque::new());
2629+
let outbound_payments = OutboundPayments::new();
2630+
let payment_id = PaymentId([0; 32]);
2631+
let absolute_expiry = 60;
2632+
2633+
let mut outbounds = outbound_payments.pending_outbound_payments.lock().unwrap();
2634+
let payment_params = PaymentParameters::from_node_id(test_utils::pubkey(42), 0)
2635+
.with_expiry_time(absolute_expiry);
2636+
let route_params = RouteParameters {
2637+
payment_params,
2638+
final_value_msat: 0,
2639+
max_total_routing_fee_msat: None,
2640+
};
2641+
let payment_hash = PaymentHash([0; 32]);
2642+
let outbound = PendingOutboundPayment::StaticInvoiceReceived {
2643+
payment_hash,
2644+
keysend_preimage: PaymentPreimage([0; 32]),
2645+
retry_strategy: Retry::Attempts(0),
2646+
payment_release_secret: [0; 32],
2647+
route_params,
2648+
};
2649+
outbounds.insert(payment_id, outbound);
2650+
core::mem::drop(outbounds);
2651+
2652+
outbound_payments.abandon_payment(
2653+
payment_id, PaymentFailureReason::UserAbandoned, &pending_events
2654+
);
2655+
let outbounds = outbound_payments.pending_outbound_payments.lock().unwrap();
2656+
assert_eq!(outbounds.len(), 0);
2657+
let events = pending_events.lock().unwrap();
2658+
assert_eq!(events.len(), 1);
2659+
assert_eq!(events[0], (Event::PaymentFailed {
2660+
payment_hash,
2661+
payment_id,
2662+
reason: Some(PaymentFailureReason::UserAbandoned),
2663+
}, None));
2664+
}
26192665
}

0 commit comments

Comments
 (0)