Skip to content

Commit df323f3

Browse files
Support abandoning pending outbound async payments.
1 parent bcf71ba commit df323f3

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

@@ -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)