Skip to content

Commit beae304

Browse files
Allow failing back intercepted payments
Co-authored-by: John Cantrell <[email protected]> Co-authored-by: Valentine Wallace <[email protected]>
1 parent afb81fd commit beae304

File tree

2 files changed

+94
-40
lines changed

2 files changed

+94
-40
lines changed

lightning/src/ln/channelmanager.rs

+28
Original file line numberDiff line numberDiff line change
@@ -3185,6 +3185,34 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
31853185
Ok(())
31863186
}
31873187

3188+
/// Fails the intercepted payment indicated by intercept_id. Should only be called in response to
3189+
/// a PaymentIntercepted event.
3190+
// TODO expand docs
3191+
pub fn fail_intercepted_payment(&self, intercept_id: InterceptId) -> Result<(), APIError> {
3192+
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
3193+
3194+
let payment = self.pending_intercepted_payments.lock().unwrap().remove(&intercept_id)
3195+
.ok_or_else(|| APIError::APIMisuseError {
3196+
err: format!("Payment with InterceptId {:?} not found", intercept_id)
3197+
})?;
3198+
3199+
if let PendingHTLCRouting::Forward { short_channel_id, .. } = payment.forward_info.routing {
3200+
let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
3201+
short_channel_id: payment.prev_short_channel_id,
3202+
outpoint: payment.prev_funding_outpoint,
3203+
htlc_id: payment.prev_htlc_id,
3204+
incoming_packet_shared_secret: payment.forward_info.incoming_shared_secret,
3205+
phantom_shared_secret: None,
3206+
});
3207+
3208+
let failure_reason = HTLCFailReason::Reason { failure_code: 0x4000 | 10, data: Vec::new() };
3209+
let destination = HTLCDestination::UnknownNextHop { requested_forward_scid: short_channel_id };
3210+
self.fail_htlc_backwards_internal(htlc_source, &payment.forward_info.payment_hash, failure_reason, destination);
3211+
} else { unreachable!() }
3212+
3213+
Ok(())
3214+
}
3215+
31883216
/// Processes HTLCs which are pending waiting on random forward delay.
31893217
///
31903218
/// Should only really ever be called in response to a PendingHTLCsForwardable event.

lightning/src/ln/payment_tests.rs

+66-40
Original file line numberDiff line numberDiff line change
@@ -1388,9 +1388,15 @@ fn abandoned_send_payment_idempotent() {
13881388
}
13891389

13901390
#[test]
1391-
fn forward_intercepted_payment() {
1391+
fn intercepted_payment() {
13921392
// Test that detecting an intercept scid on payment forward will signal LDK to generate an
1393-
// intercept event, which the LSP can then use to open a JIT channel to forward the payment.
1393+
// intercept event, which the LSP can then use to either (a) open a JIT channel to forward the
1394+
// payment or (b) fail the payment.
1395+
do_test_intercepted_payment(false);
1396+
do_test_intercepted_payment(true);
1397+
}
1398+
1399+
fn do_test_intercepted_payment(fail_intercept: bool) {
13941400
let chanmon_cfgs = create_chanmon_cfgs(3);
13951401
let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
13961402
let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
@@ -1457,44 +1463,64 @@ fn forward_intercepted_payment() {
14571463
_ => panic!()
14581464
};
14591465

1460-
// Open the just-in-time channel so the payment can then be forwarded.
1461-
let scid = create_announced_chan_between_nodes(&nodes, 1, 2, channelmanager::provided_init_features(), channelmanager::provided_init_features()).0.contents.short_channel_id;
1462-
1463-
// Finally, forward the intercepted payment through and claim it.
1464-
nodes[1].node.forward_intercepted_payment(intercept_id, scid, expected_outbound_amount_msat).unwrap();
1465-
expect_pending_htlcs_forwardable!(nodes[1]);
1466-
1467-
let payment_event = {
1468-
{
1469-
let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
1470-
assert_eq!(added_monitors.len(), 1);
1471-
added_monitors.clear();
1466+
if fail_intercept {
1467+
// Ensure we can fail the intercepted payment back.
1468+
nodes[1].node.fail_intercepted_payment(intercept_id).unwrap();
1469+
expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(nodes[1], vec![HTLCDestination::UnknownNextHop { requested_forward_scid: intercept_scid }]);
1470+
nodes[1].node.process_pending_htlc_forwards();
1471+
let update_fail = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1472+
check_added_monitors!(&nodes[1], 1);
1473+
assert!(update_fail.update_fail_htlcs.len() == 1);
1474+
let fail_msg = update_fail.update_fail_htlcs[0].clone();
1475+
nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_msg);
1476+
commitment_signed_dance!(nodes[0], nodes[1], update_fail.commitment_signed, false);
1477+
1478+
// Ensure the payment fails with the expected error.
1479+
let mut fail_conditions = PaymentFailedConditions::new()
1480+
.blamed_scid(intercept_scid)
1481+
.blamed_chan_closed(true)
1482+
.expected_htlc_error_data(0x4000 | 10, &[]);
1483+
expect_payment_failed_conditions(&nodes[0], payment_hash, false, fail_conditions);
1484+
} else {
1485+
// Open the just-in-time channel so the payment can then be forwarded.
1486+
let scid = create_announced_chan_between_nodes(&nodes, 1, 2, channelmanager::provided_init_features(), channelmanager::provided_init_features()).0.contents.short_channel_id;
1487+
1488+
// Finally, forward the intercepted payment through and claim it.
1489+
nodes[1].node.forward_intercepted_payment(intercept_id, scid, expected_outbound_amount_msat).unwrap();
1490+
expect_pending_htlcs_forwardable!(nodes[1]);
1491+
1492+
let payment_event = {
1493+
{
1494+
let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
1495+
assert_eq!(added_monitors.len(), 1);
1496+
added_monitors.clear();
1497+
}
1498+
let mut events = nodes[1].node.get_and_clear_pending_msg_events();
1499+
assert_eq!(events.len(), 1);
1500+
SendEvent::from_event(events.remove(0))
1501+
};
1502+
nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
1503+
commitment_signed_dance!(nodes[2], nodes[1], &payment_event.commitment_msg, false, true);
1504+
expect_pending_htlcs_forwardable!(nodes[2]);
1505+
1506+
let payment_preimage = nodes[2].node.get_payment_preimage(payment_hash, payment_secret).unwrap();
1507+
expect_payment_received!(&nodes[2], payment_hash, payment_secret, amt_msat, Some(payment_preimage));
1508+
do_claim_payment_along_route(&nodes[0], &vec!(&vec!(&nodes[1], &nodes[2])[..]), false, payment_preimage);
1509+
let events = nodes[0].node.get_and_clear_pending_events();
1510+
assert_eq!(events.len(), 2);
1511+
match events[0] {
1512+
Event::PaymentSent { payment_preimage: ref ev_preimage, payment_hash: ref ev_hash, ref fee_paid_msat, .. } => {
1513+
assert_eq!(payment_preimage, *ev_preimage);
1514+
assert_eq!(payment_hash, *ev_hash);
1515+
assert_eq!(fee_paid_msat, &Some(1000));
1516+
},
1517+
_ => panic!("Unexpected event")
1518+
}
1519+
match events[1] {
1520+
Event::PaymentPathSuccessful { payment_hash: hash, .. } => {
1521+
assert_eq!(hash, Some(payment_hash));
1522+
},
1523+
_ => panic!("Unexpected event")
14721524
}
1473-
let mut events = nodes[1].node.get_and_clear_pending_msg_events();
1474-
assert_eq!(events.len(), 1);
1475-
SendEvent::from_event(events.remove(0))
1476-
};
1477-
nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
1478-
commitment_signed_dance!(nodes[2], nodes[1], &payment_event.commitment_msg, false, true);
1479-
expect_pending_htlcs_forwardable!(nodes[2]);
1480-
1481-
let payment_preimage = nodes[2].node.get_payment_preimage(payment_hash, payment_secret).unwrap();
1482-
expect_payment_received!(&nodes[2], payment_hash, payment_secret, amt_msat, Some(payment_preimage));
1483-
do_claim_payment_along_route(&nodes[0], &vec!(&vec!(&nodes[1], &nodes[2])[..]), false, payment_preimage);
1484-
let events = nodes[0].node.get_and_clear_pending_events();
1485-
assert_eq!(events.len(), 2);
1486-
match events[0] {
1487-
Event::PaymentSent { payment_preimage: ref ev_preimage, payment_hash: ref ev_hash, ref fee_paid_msat, .. } => {
1488-
assert_eq!(payment_preimage, *ev_preimage);
1489-
assert_eq!(payment_hash, *ev_hash);
1490-
assert_eq!(fee_paid_msat, &Some(1000));
1491-
},
1492-
_ => panic!("Unexpected event")
1493-
}
1494-
match events[1] {
1495-
Event::PaymentPathSuccessful { payment_hash: hash, .. } => {
1496-
assert_eq!(hash, Some(payment_hash));
1497-
},
1498-
_ => panic!("Unexpected event")
14991525
}
15001526
}

0 commit comments

Comments
 (0)