Skip to content

Commit 51302e8

Browse files
Support abandoning pending outbound async payments.
1 parent 24789f4 commit 51302e8

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
@@ -202,20 +202,26 @@ impl PendingOutboundPayment {
202202
}
203203

204204
fn mark_abandoned(&mut self, reason: PaymentFailureReason) {
205-
if let PendingOutboundPayment::Retryable { session_privs, payment_hash, .. } = self {
206-
let mut our_session_privs = new_hash_set();
207-
core::mem::swap(&mut our_session_privs, session_privs);
208-
*self = PendingOutboundPayment::Abandoned {
209-
session_privs: our_session_privs,
210-
payment_hash: *payment_hash,
211-
reason: Some(reason)
212-
};
213-
} else if let PendingOutboundPayment::InvoiceReceived { payment_hash, .. } = self {
214-
*self = PendingOutboundPayment::Abandoned {
215-
session_privs: new_hash_set(),
216-
payment_hash: *payment_hash,
217-
reason: Some(reason)
218-
};
205+
let session_privs = match self {
206+
PendingOutboundPayment::Retryable { session_privs, .. } => {
207+
let mut our_session_privs = new_hash_set();
208+
core::mem::swap(&mut our_session_privs, session_privs);
209+
our_session_privs
210+
},
211+
_ => new_hash_set(),
212+
};
213+
match self {
214+
Self::Retryable { payment_hash, .. } |
215+
Self::InvoiceReceived { payment_hash, .. } |
216+
Self::StaticInvoiceReceived { payment_hash, .. } =>
217+
{
218+
*self = Self::Abandoned {
219+
session_privs,
220+
payment_hash: *payment_hash,
221+
reason: Some(reason),
222+
};
223+
},
224+
_ => {}
219225
}
220226
}
221227

@@ -2628,4 +2634,44 @@ mod tests {
26282634
reason: Some(PaymentFailureReason::PaymentExpired),
26292635
}, None));
26302636
}
2637+
2638+
#[test]
2639+
fn abandon_unreleased_async_payment() {
2640+
let pending_events = Mutex::new(VecDeque::new());
2641+
let outbound_payments = OutboundPayments::new();
2642+
let payment_id = PaymentId([0; 32]);
2643+
let absolute_expiry = 60;
2644+
2645+
let mut outbounds = outbound_payments.pending_outbound_payments.lock().unwrap();
2646+
let payment_params = PaymentParameters::from_node_id(test_utils::pubkey(42), 0)
2647+
.with_expiry_time(absolute_expiry);
2648+
let route_params = RouteParameters {
2649+
payment_params,
2650+
final_value_msat: 0,
2651+
max_total_routing_fee_msat: None,
2652+
};
2653+
let payment_hash = PaymentHash([0; 32]);
2654+
let outbound = PendingOutboundPayment::StaticInvoiceReceived {
2655+
payment_hash,
2656+
keysend_preimage: PaymentPreimage([0; 32]),
2657+
retry_strategy: Retry::Attempts(0),
2658+
payment_release_secret: [0; 32],
2659+
route_params,
2660+
};
2661+
outbounds.insert(payment_id, outbound);
2662+
core::mem::drop(outbounds);
2663+
2664+
outbound_payments.abandon_payment(
2665+
payment_id, PaymentFailureReason::UserAbandoned, &pending_events
2666+
);
2667+
let outbounds = outbound_payments.pending_outbound_payments.lock().unwrap();
2668+
assert_eq!(outbounds.len(), 0);
2669+
let events = pending_events.lock().unwrap();
2670+
assert_eq!(events.len(), 1);
2671+
assert_eq!(events[0], (Event::PaymentFailed {
2672+
payment_hash,
2673+
payment_id,
2674+
reason: Some(PaymentFailureReason::UserAbandoned),
2675+
}, None));
2676+
}
26312677
}

0 commit comments

Comments
 (0)