Skip to content

Commit ede57ff

Browse files
committed
Drop return value from fail_htlc_backwards, clarify docs
`ChannelManager::fail_htlc_backwards`' bool return value is quite confusing - just because it returns false doesn't mean the payment wasn't (already) failed. Worse, in some race cases around shutdown where a payment was claimed before an unclean shutdown and then retried on startup, `fail_htlc_backwards` could return true even though (a duplicate copy of the same payment) was claimed, but the claim event has not been seen by the user yet. While its possible to use it correctly, its somewhat confusing to have a return value at all, and definitely lends itself to misuse. Instead, we should push users towards a model where they don't care if `fail_htlc_backwards` succeeds - either they've locally marked the payment as failed (prior to seeing any `PaymentReceived` events) and will fail any attempts to pay it, or they have not and the payment is still receivable until its timeout time is reached. We can revisit this decision based on user feedback, but will need to very carefully document the potential failure modes here if we do.
1 parent 1882c58 commit ede57ff

6 files changed

+30
-23
lines changed

fuzz/src/chanmon_consistency.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out) {
840840
events::Event::PaymentReceived { payment_hash, .. } => {
841841
if claim_set.insert(payment_hash.0) {
842842
if $fail {
843-
assert!(nodes[$node].fail_htlc_backwards(&payment_hash));
843+
nodes[$node].fail_htlc_backwards(&payment_hash);
844844
} else {
845845
nodes[$node].claim_funds(PaymentPreimage(payment_hash.0));
846846
}

lightning/src/ln/chanmon_update_fail_tests.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ fn do_test_monitor_update_fail_raa(test_ignore_second_cs: bool) {
831831
let (_, payment_hash_1, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1000000);
832832

833833
// Fail the payment backwards, failing the monitor update on nodes[1]'s receipt of the RAA
834-
assert!(nodes[2].node.fail_htlc_backwards(&payment_hash_1));
834+
nodes[2].node.fail_htlc_backwards(&payment_hash_1);
835835
expect_pending_htlcs_forwardable!(nodes[2]);
836836
check_added_monitors!(nodes[2], 1);
837837

@@ -1696,7 +1696,7 @@ fn test_monitor_update_on_pending_forwards() {
16961696
send_payment(&nodes[0], &[&nodes[1], &nodes[2]], 5000000);
16971697

16981698
let (_, payment_hash_1, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1000000);
1699-
assert!(nodes[2].node.fail_htlc_backwards(&payment_hash_1));
1699+
nodes[2].node.fail_htlc_backwards(&payment_hash_1);
17001700
expect_pending_htlcs_forwardable!(nodes[2]);
17011701
check_added_monitors!(nodes[2], 1);
17021702

@@ -2468,7 +2468,7 @@ fn do_test_reconnect_dup_htlc_claims(htlc_status: HTLCStatusAtDupClaim, second_f
24682468
payment_preimage,
24692469
};
24702470
if second_fails {
2471-
assert!(nodes[2].node.fail_htlc_backwards(&payment_hash));
2471+
nodes[2].node.fail_htlc_backwards(&payment_hash);
24722472
expect_pending_htlcs_forwardable!(nodes[2]);
24732473
check_added_monitors!(nodes[2], 1);
24742474
get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());

lightning/src/ln/channelmanager.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -3500,9 +3500,17 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
35003500
/// Indicates that the preimage for payment_hash is unknown or the received amount is incorrect
35013501
/// after a PaymentReceived event, failing the HTLC back to its origin and freeing resources
35023502
/// along the path (including in our own channel on which we received it).
3503-
/// Returns false if no payment was found to fail backwards, true if the process of failing the
3504-
/// HTLC backwards has been started.
3505-
pub fn fail_htlc_backwards(&self, payment_hash: &PaymentHash) -> bool {
3503+
///
3504+
/// Note that in some cases around unclean shutdown, it is possible the payment may have
3505+
/// already been claimed by you via [`ChannelManager::claim_funds`] prior to you seeing (a
3506+
/// second copy of) the [`events::Event::PaymentReceived`] event. Alternatively, the payment
3507+
/// may have already been failed automatically by LDK if it was nearing its expiration time.
3508+
///
3509+
/// While LDK will never claim a payment automatically on your behalf (i.e. without you calling
3510+
/// [`ChannelManager::claim_funds`]), you should still monitor for
3511+
/// [`events::Event::PaymentClaimed`] events even for payments you intend to fail, especially on
3512+
/// startup during which time claims that were in-progress at shutdown may be replayed.
3513+
pub fn fail_htlc_backwards(&self, payment_hash: &PaymentHash) {
35063514
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
35073515

35083516
let mut channel_state = Some(self.channel_state.lock().unwrap());
@@ -3517,8 +3525,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
35173525
HTLCSource::PreviousHopData(htlc.prev_hop), payment_hash,
35183526
HTLCFailReason::Reason { failure_code: 0x4000 | 15, data: htlc_msat_height_data });
35193527
}
3520-
true
3521-
} else { false }
3528+
}
35223529
}
35233530

35243531
/// Gets an HTLC onion failure code and error data for an `UPDATE` error, given the error code

lightning/src/ln/functional_test_utils.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1739,7 +1739,7 @@ pub fn fail_payment_along_route<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expe
17391739
for path in expected_paths.iter() {
17401740
assert_eq!(path.last().unwrap().node.get_our_node_id(), expected_paths[0].last().unwrap().node.get_our_node_id());
17411741
}
1742-
assert!(expected_paths[0].last().unwrap().node.fail_htlc_backwards(&our_payment_hash));
1742+
expected_paths[0].last().unwrap().node.fail_htlc_backwards(&our_payment_hash);
17431743
expect_pending_htlcs_forwardable!(expected_paths[0].last().unwrap());
17441744

17451745
pass_failed_payment_back(origin_node, expected_paths, skip_last, our_payment_hash);
@@ -1845,7 +1845,7 @@ pub fn pass_failed_payment_back<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expe
18451845
}
18461846

18471847
// Ensure that fail_htlc_backwards is idempotent.
1848-
assert!(!expected_paths[0].last().unwrap().node.fail_htlc_backwards(&our_payment_hash));
1848+
expected_paths[0].last().unwrap().node.fail_htlc_backwards(&our_payment_hash);
18491849
assert!(expected_paths[0].last().unwrap().node.get_and_clear_pending_events().is_empty());
18501850
assert!(expected_paths[0].last().unwrap().node.get_and_clear_pending_msg_events().is_empty());
18511851
check_added_monitors!(expected_paths[0].last().unwrap(), 0);

lightning/src/ln/functional_tests.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -3089,7 +3089,7 @@ fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool, use
30893089
let (_, second_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], value);
30903090
let (_, third_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], value);
30913091

3092-
assert!(nodes[2].node.fail_htlc_backwards(&first_payment_hash));
3092+
nodes[2].node.fail_htlc_backwards(&first_payment_hash);
30933093
expect_pending_htlcs_forwardable!(nodes[2]);
30943094
check_added_monitors!(nodes[2], 1);
30953095
let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
@@ -3102,7 +3102,7 @@ fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool, use
31023102
let bs_raa = commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false, true, false, true);
31033103
// Drop the last RAA from 3 -> 2
31043104

3105-
assert!(nodes[2].node.fail_htlc_backwards(&second_payment_hash));
3105+
nodes[2].node.fail_htlc_backwards(&second_payment_hash);
31063106
expect_pending_htlcs_forwardable!(nodes[2]);
31073107
check_added_monitors!(nodes[2], 1);
31083108
let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
@@ -3119,7 +3119,7 @@ fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool, use
31193119
nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_raa);
31203120
check_added_monitors!(nodes[2], 1);
31213121

3122-
assert!(nodes[2].node.fail_htlc_backwards(&third_payment_hash));
3122+
nodes[2].node.fail_htlc_backwards(&third_payment_hash);
31233123
expect_pending_htlcs_forwardable!(nodes[2]);
31243124
check_added_monitors!(nodes[2], 1);
31253125
let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
@@ -5518,10 +5518,10 @@ fn do_test_fail_backwards_unrevoked_remote_announce(deliver_last_raa: bool, anno
55185518

55195519
// Now fail back three of the over-dust-limit and three of the under-dust-limit payments in one go.
55205520
// Fail 0th below-dust, 4th above-dust, 8th above-dust, 10th below-dust HTLCs
5521-
assert!(nodes[4].node.fail_htlc_backwards(&payment_hash_1));
5522-
assert!(nodes[4].node.fail_htlc_backwards(&payment_hash_3));
5523-
assert!(nodes[4].node.fail_htlc_backwards(&payment_hash_5));
5524-
assert!(nodes[4].node.fail_htlc_backwards(&payment_hash_6));
5521+
nodes[4].node.fail_htlc_backwards(&payment_hash_1);
5522+
nodes[4].node.fail_htlc_backwards(&payment_hash_3);
5523+
nodes[4].node.fail_htlc_backwards(&payment_hash_5);
5524+
nodes[4].node.fail_htlc_backwards(&payment_hash_6);
55255525
check_added_monitors!(nodes[4], 0);
55265526
expect_pending_htlcs_forwardable!(nodes[4]);
55275527
check_added_monitors!(nodes[4], 1);
@@ -5534,8 +5534,8 @@ fn do_test_fail_backwards_unrevoked_remote_announce(deliver_last_raa: bool, anno
55345534
commitment_signed_dance!(nodes[3], nodes[4], four_removes.commitment_signed, false);
55355535

55365536
// Fail 3rd below-dust and 7th above-dust HTLCs
5537-
assert!(nodes[5].node.fail_htlc_backwards(&payment_hash_2));
5538-
assert!(nodes[5].node.fail_htlc_backwards(&payment_hash_4));
5537+
nodes[5].node.fail_htlc_backwards(&payment_hash_2);
5538+
nodes[5].node.fail_htlc_backwards(&payment_hash_4);
55395539
check_added_monitors!(nodes[5], 0);
55405540
expect_pending_htlcs_forwardable!(nodes[5]);
55415541
check_added_monitors!(nodes[5], 1);
@@ -5960,7 +5960,7 @@ fn do_htlc_claim_previous_remote_commitment_only(use_dust: bool, check_revoke_no
59605960
// actually revoked.
59615961
let htlc_value = if use_dust { 50000 } else { 3000000 };
59625962
let (_, our_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], htlc_value);
5963-
assert!(nodes[1].node.fail_htlc_backwards(&our_payment_hash));
5963+
nodes[1].node.fail_htlc_backwards(&our_payment_hash);
59645964
expect_pending_htlcs_forwardable!(nodes[1]);
59655965
check_added_monitors!(nodes[1], 1);
59665966

@@ -7136,7 +7136,7 @@ fn do_test_failure_delay_dust_htlc_local_commitment(announce_latest: bool) {
71367136
let as_prev_commitment_tx = get_local_commitment_txn!(nodes[0], chan.2);
71377137

71387138
// Fail one HTLC to prune it in the will-be-latest-local commitment tx
7139-
assert!(nodes[1].node.fail_htlc_backwards(&payment_hash_2));
7139+
nodes[1].node.fail_htlc_backwards(&payment_hash_2);
71407140
check_added_monitors!(nodes[1], 0);
71417141
expect_pending_htlcs_forwardable!(nodes[1]);
71427142
check_added_monitors!(nodes[1], 1);

lightning/src/ln/onion_route_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1155,7 +1155,7 @@ fn test_phantom_failure_reject_payment() {
11551155
expect_pending_htlcs_forwardable_ignore!(nodes[1]);
11561156
nodes[1].node.process_pending_htlc_forwards();
11571157
expect_payment_received!(nodes[1], payment_hash, payment_secret, recv_amt_msat);
1158-
assert!(nodes[1].node.fail_htlc_backwards(&payment_hash));
1158+
nodes[1].node.fail_htlc_backwards(&payment_hash);
11591159
expect_pending_htlcs_forwardable_ignore!(nodes[1]);
11601160
nodes[1].node.process_pending_htlc_forwards();
11611161

0 commit comments

Comments
 (0)