Skip to content

Commit f79ad2e

Browse files
Allow failing back intercepted HTLCs
Co-authored-by: John Cantrell <johncantrell97@gmail.com> Co-authored-by: Valentine Wallace <vwallace@protonmail.com>
1 parent c1f1b78 commit f79ad2e

File tree

3 files changed

+108
-46
lines changed

3 files changed

+108
-46
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3059,7 +3059,8 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
30593059
///
30603060
/// To make use of intercepted HTLCs, use [`ChannelManager::get_intercept_scid`] to generate short
30613061
/// channel id(s) to put in the receiver's invoice route hints. These route hints will signal to
3062-
/// LDK to generate an [`HTLCIntercepted`] event when it receives the forwarded HTLC.
3062+
/// LDK to generate an [`HTLCIntercepted`] event when it receives the forwarded HTLC, and this
3063+
/// method or [`ChannelManager::fail_intercepted_htlc`] MUST be called in response to the event.
30633064
///
30643065
/// Note that LDK does not enforce fee requirements in `amt_to_forward_msat`, and will not stop
30653066
/// you from forwarding more than you received.
@@ -3102,6 +3103,35 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
31023103
Ok(())
31033104
}
31043105

3106+
/// Fails the intercepted HTLC indicated by intercept_id. Should only be called in response to
3107+
/// an [`HTLCIntercepted`] event. See [`ChannelManager::forward_intercepted_htlc`].
3108+
///
3109+
/// [`HTLCIntercepted`]: events::Event::HTLCIntercepted
3110+
pub fn fail_intercepted_htlc(&self, intercept_id: InterceptId) -> Result<(), APIError> {
3111+
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
3112+
3113+
let payment = self.pending_intercepted_htlcs.lock().unwrap().remove(&intercept_id)
3114+
.ok_or_else(|| APIError::APIMisuseError {
3115+
err: format!("Payment with InterceptId {:?} not found", intercept_id)
3116+
})?;
3117+
3118+
if let PendingHTLCRouting::Forward { short_channel_id, .. } = payment.forward_info.routing {
3119+
let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
3120+
short_channel_id: payment.prev_short_channel_id,
3121+
outpoint: payment.prev_funding_outpoint,
3122+
htlc_id: payment.prev_htlc_id,
3123+
incoming_packet_shared_secret: payment.forward_info.incoming_shared_secret,
3124+
phantom_shared_secret: None,
3125+
});
3126+
3127+
let failure_reason = HTLCFailReason::Reason { failure_code: 0x4000 | 10, data: Vec::new() };
3128+
let destination = HTLCDestination::UnknownNextHop { requested_forward_scid: short_channel_id };
3129+
self.fail_htlc_backwards_internal(htlc_source, &payment.forward_info.payment_hash, failure_reason, destination);
3130+
} else { unreachable!() } // Only `PendingHTLCRouting::Forward`s are intercepted
3131+
3132+
Ok(())
3133+
}
3134+
31053135
/// Processes HTLCs which are pending waiting on random forward delay.
31063136
///
31073137
/// Should only really ever be called in response to a PendingHTLCsForwardable event.

lightning/src/ln/payment_tests.rs

Lines changed: 71 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,9 +1374,15 @@ fn test_holding_cell_inflight_htlcs() {
13741374
}
13751375

13761376
#[test]
1377-
fn forward_intercepted_payment() {
1377+
fn intercepted_payment() {
13781378
// Test that detecting an intercept scid on payment forward will signal LDK to generate an
1379-
// intercept event, which the LSP can then use to open a JIT channel to forward the payment.
1379+
// intercept event, which the LSP can then use to either (a) open a JIT channel to forward the
1380+
// payment or (b) fail the payment.
1381+
do_test_intercepted_payment(false);
1382+
do_test_intercepted_payment(true);
1383+
}
1384+
1385+
fn do_test_intercepted_payment(fail_intercept: bool) {
13801386
let chanmon_cfgs = create_chanmon_cfgs(3);
13811387
let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
13821388
let mut chan_config = test_default_channel_config();
@@ -1449,49 +1455,69 @@ fn forward_intercepted_payment() {
14491455
let unknown_chan_id_err = nodes[1].node.forward_intercepted_htlc(intercept_id, &[42; 32], nodes[2].node.get_our_node_id(), expected_outbound_amount_msat).unwrap_err();
14501456
assert_eq!(unknown_chan_id_err , APIError::APIMisuseError { err: format!("Channel with id {:?} not found", [42; 32]) });
14511457

1452-
// Open the just-in-time channel so the payment can then be forwarded.
1453-
let (_, channel_id) = open_zero_conf_channel(&nodes[1], &nodes[2], None);
1454-
1455-
// Check for unknown intercept id error.
1456-
let unknown_intercept_id = InterceptId([42; 32]);
1457-
let unknown_intercept_id_err = nodes[1].node.forward_intercepted_htlc(unknown_intercept_id, &channel_id, nodes[2].node.get_our_node_id(), expected_outbound_amount_msat).unwrap_err();
1458-
assert_eq!(unknown_intercept_id_err , APIError::APIMisuseError { err: format!("Payment with intercept id {:?} not found", unknown_intercept_id.0) });
1459-
1460-
// Finally, forward the intercepted payment through and claim it.
1461-
nodes[1].node.forward_intercepted_htlc(intercept_id, &channel_id, nodes[2].node.get_our_node_id(), expected_outbound_amount_msat).unwrap();
1462-
expect_pending_htlcs_forwardable!(nodes[1]);
1463-
1464-
let payment_event = {
1465-
{
1466-
let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
1467-
assert_eq!(added_monitors.len(), 1);
1468-
added_monitors.clear();
1458+
if fail_intercept {
1459+
// Ensure we can fail the intercepted payment back.
1460+
nodes[1].node.fail_intercepted_htlc(intercept_id).unwrap();
1461+
expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(nodes[1], vec![HTLCDestination::UnknownNextHop { requested_forward_scid: intercept_scid }]);
1462+
nodes[1].node.process_pending_htlc_forwards();
1463+
let update_fail = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
1464+
check_added_monitors!(&nodes[1], 1);
1465+
assert!(update_fail.update_fail_htlcs.len() == 1);
1466+
let fail_msg = update_fail.update_fail_htlcs[0].clone();
1467+
nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &fail_msg);
1468+
commitment_signed_dance!(nodes[0], nodes[1], update_fail.commitment_signed, false);
1469+
1470+
// Ensure the payment fails with the expected error.
1471+
let fail_conditions = PaymentFailedConditions::new()
1472+
.blamed_scid(intercept_scid)
1473+
.blamed_chan_closed(true)
1474+
.expected_htlc_error_data(0x4000 | 10, &[]);
1475+
expect_payment_failed_conditions(&nodes[0], payment_hash, false, fail_conditions);
1476+
} else {
1477+
// Open the just-in-time channel so the payment can then be forwarded.
1478+
let (_, channel_id) = open_zero_conf_channel(&nodes[1], &nodes[2], None);
1479+
1480+
// Check for unknown intercept id error.
1481+
let unknown_intercept_id = InterceptId([42; 32]);
1482+
let unknown_intercept_id_err = nodes[1].node.forward_intercepted_htlc(unknown_intercept_id, &channel_id, nodes[2].node.get_our_node_id(), expected_outbound_amount_msat).unwrap_err();
1483+
assert_eq!(unknown_intercept_id_err , APIError::APIMisuseError { err: format!("Payment with intercept id {:?} not found", unknown_intercept_id.0) });
1484+
1485+
// Finally, forward the intercepted payment through and claim it.
1486+
nodes[1].node.forward_intercepted_htlc(intercept_id, &channel_id, nodes[2].node.get_our_node_id(), expected_outbound_amount_msat).unwrap();
1487+
expect_pending_htlcs_forwardable!(nodes[1]);
1488+
1489+
let payment_event = {
1490+
{
1491+
let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
1492+
assert_eq!(added_monitors.len(), 1);
1493+
added_monitors.clear();
1494+
}
1495+
let mut events = nodes[1].node.get_and_clear_pending_msg_events();
1496+
assert_eq!(events.len(), 1);
1497+
SendEvent::from_event(events.remove(0))
1498+
};
1499+
nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
1500+
commitment_signed_dance!(nodes[2], nodes[1], &payment_event.commitment_msg, false, true);
1501+
expect_pending_htlcs_forwardable!(nodes[2]);
1502+
1503+
let payment_preimage = nodes[2].node.get_payment_preimage(payment_hash, payment_secret).unwrap();
1504+
expect_payment_received!(&nodes[2], payment_hash, payment_secret, amt_msat, Some(payment_preimage), nodes[2].node.get_our_node_id());
1505+
do_claim_payment_along_route(&nodes[0], &vec!(&vec!(&nodes[1], &nodes[2])[..]), false, payment_preimage);
1506+
let events = nodes[0].node.get_and_clear_pending_events();
1507+
assert_eq!(events.len(), 2);
1508+
match events[0] {
1509+
Event::PaymentSent { payment_preimage: ref ev_preimage, payment_hash: ref ev_hash, ref fee_paid_msat, .. } => {
1510+
assert_eq!(payment_preimage, *ev_preimage);
1511+
assert_eq!(payment_hash, *ev_hash);
1512+
assert_eq!(fee_paid_msat, &Some(1000));
1513+
},
1514+
_ => panic!("Unexpected event")
1515+
}
1516+
match events[1] {
1517+
Event::PaymentPathSuccessful { payment_hash: hash, .. } => {
1518+
assert_eq!(hash, Some(payment_hash));
1519+
},
1520+
_ => panic!("Unexpected event")
14691521
}
1470-
let mut events = nodes[1].node.get_and_clear_pending_msg_events();
1471-
assert_eq!(events.len(), 1);
1472-
SendEvent::from_event(events.remove(0))
1473-
};
1474-
nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
1475-
commitment_signed_dance!(nodes[2], nodes[1], &payment_event.commitment_msg, false, true);
1476-
expect_pending_htlcs_forwardable!(nodes[2]);
1477-
1478-
let payment_preimage = nodes[2].node.get_payment_preimage(payment_hash, payment_secret).unwrap();
1479-
expect_payment_received!(&nodes[2], payment_hash, payment_secret, amt_msat, Some(payment_preimage), nodes[2].node.get_our_node_id());
1480-
do_claim_payment_along_route(&nodes[0], &vec!(&vec!(&nodes[1], &nodes[2])[..]), false, payment_preimage);
1481-
let events = nodes[0].node.get_and_clear_pending_events();
1482-
assert_eq!(events.len(), 2);
1483-
match events[0] {
1484-
Event::PaymentSent { payment_preimage: ref ev_preimage, payment_hash: ref ev_hash, ref fee_paid_msat, .. } => {
1485-
assert_eq!(payment_preimage, *ev_preimage);
1486-
assert_eq!(payment_hash, *ev_hash);
1487-
assert_eq!(fee_paid_msat, &Some(1000));
1488-
},
1489-
_ => panic!("Unexpected event")
1490-
}
1491-
match events[1] {
1492-
Event::PaymentPathSuccessful { payment_hash: hash, .. } => {
1493-
assert_eq!(hash, Some(payment_hash));
1494-
},
1495-
_ => panic!("Unexpected event")
14961522
}
14971523
}

lightning/src/util/events.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,13 @@ pub enum Event {
614614
/// you've encoded an intercept scid in the receiver's invoice route hints using
615615
/// [`ChannelManager::get_intercept_scid`].
616616
///
617+
/// [`ChannelManager::forward_intercepted_htlc`] or
618+
/// [`ChannelManager::fail_intercepted_htlc`] MUST be called in response to this event. See
619+
/// their docs for more information.
620+
///
617621
/// [`ChannelManager::get_intercept_scid`]: crate::ln::channelmanager::ChannelManager::get_intercept_scid
622+
/// [`ChannelManager::forward_intercepted_htlc`]: crate::ln::channelmanager::ChannelManager::forward_intercepted_htlc
623+
/// [`ChannelManager::fail_intercepted_htlc`]: crate::ln::channelmanager::ChannelManager::fail_intercepted_htlc
618624
HTLCIntercepted {
619625
/// An id to help LDK identify which HTLC is being forwarded or failed.
620626
intercept_id: InterceptId,

0 commit comments

Comments
 (0)