Skip to content

Commit 721c415

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 2893ef5 commit 721c415

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
@@ -3479,9 +3479,17 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
34793479
/// Indicates that the preimage for payment_hash is unknown or the received amount is incorrect
34803480
/// after a PaymentReceived event, failing the HTLC back to its origin and freeing resources
34813481
/// along the path (including in our own channel on which we received it).
3482-
/// Returns false if no payment was found to fail backwards, true if the process of failing the
3483-
/// HTLC backwards has been started.
3484-
pub fn fail_htlc_backwards(&self, payment_hash: &PaymentHash) -> bool {
3482+
///
3483+
/// Note that in some cases around unclean shutdown, it is possible the payment may have
3484+
/// already been claimed by you via [`ChannelManager::claim_funds`] prior to you seeing (a
3485+
/// second copy of) the [`events::Event::PaymentReceived`] event. Alternatively, the payment
3486+
/// may have already been failed automatically by LDK if it was nearing its expiration time.
3487+
///
3488+
/// While LDK will never claim a payment automatically on your behalf (i.e. without you calling
3489+
/// [`ChannelManager::claim_funds`]), you should still monitor for
3490+
/// [`events::Event::PaymentClaimed`] events even for payments you intend to fail, especially on
3491+
/// startup during which time claims which were in-progress at shutdown may be replayed.
3492+
pub fn fail_htlc_backwards(&self, payment_hash: &PaymentHash) {
34853493
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
34863494

34873495
let mut channel_state = Some(self.channel_state.lock().unwrap());
@@ -3496,8 +3504,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
34963504
HTLCSource::PreviousHopData(htlc.prev_hop), payment_hash,
34973505
HTLCFailReason::Reason { failure_code: 0x4000 | 15, data: htlc_msat_height_data });
34983506
}
3499-
true
3500-
} else { false }
3507+
}
35013508
}
35023509

35033510
/// 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
@@ -1717,7 +1717,7 @@ pub fn fail_payment_along_route<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expe
17171717
for path in expected_paths.iter() {
17181718
assert_eq!(path.last().unwrap().node.get_our_node_id(), expected_paths[0].last().unwrap().node.get_our_node_id());
17191719
}
1720-
assert!(expected_paths[0].last().unwrap().node.fail_htlc_backwards(&our_payment_hash));
1720+
expected_paths[0].last().unwrap().node.fail_htlc_backwards(&our_payment_hash);
17211721
expect_pending_htlcs_forwardable!(expected_paths[0].last().unwrap());
17221722

17231723
pass_failed_payment_back(origin_node, expected_paths, skip_last, our_payment_hash);
@@ -1823,7 +1823,7 @@ pub fn pass_failed_payment_back<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expe
18231823
}
18241824

18251825
// Ensure that fail_htlc_backwards is idempotent.
1826-
assert!(!expected_paths[0].last().unwrap().node.fail_htlc_backwards(&our_payment_hash));
1826+
expected_paths[0].last().unwrap().node.fail_htlc_backwards(&our_payment_hash);
18271827
assert!(expected_paths[0].last().unwrap().node.get_and_clear_pending_events().is_empty());
18281828
assert!(expected_paths[0].last().unwrap().node.get_and_clear_pending_msg_events().is_empty());
18291829
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
@@ -3052,7 +3052,7 @@ fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool, use
30523052
let (_, second_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], value);
30533053
let (_, third_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], value);
30543054

3055-
assert!(nodes[2].node.fail_htlc_backwards(&first_payment_hash));
3055+
nodes[2].node.fail_htlc_backwards(&first_payment_hash);
30563056
expect_pending_htlcs_forwardable!(nodes[2]);
30573057
check_added_monitors!(nodes[2], 1);
30583058
let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
@@ -3065,7 +3065,7 @@ fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool, use
30653065
let bs_raa = commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false, true, false, true);
30663066
// Drop the last RAA from 3 -> 2
30673067

3068-
assert!(nodes[2].node.fail_htlc_backwards(&second_payment_hash));
3068+
nodes[2].node.fail_htlc_backwards(&second_payment_hash);
30693069
expect_pending_htlcs_forwardable!(nodes[2]);
30703070
check_added_monitors!(nodes[2], 1);
30713071
let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
@@ -3082,7 +3082,7 @@ fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool, use
30823082
nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_raa);
30833083
check_added_monitors!(nodes[2], 1);
30843084

3085-
assert!(nodes[2].node.fail_htlc_backwards(&third_payment_hash));
3085+
nodes[2].node.fail_htlc_backwards(&third_payment_hash);
30863086
expect_pending_htlcs_forwardable!(nodes[2]);
30873087
check_added_monitors!(nodes[2], 1);
30883088
let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
@@ -5438,10 +5438,10 @@ fn do_test_fail_backwards_unrevoked_remote_announce(deliver_last_raa: bool, anno
54385438

54395439
// Now fail back three of the over-dust-limit and three of the under-dust-limit payments in one go.
54405440
// Fail 0th below-dust, 4th above-dust, 8th above-dust, 10th below-dust HTLCs
5441-
assert!(nodes[4].node.fail_htlc_backwards(&payment_hash_1));
5442-
assert!(nodes[4].node.fail_htlc_backwards(&payment_hash_3));
5443-
assert!(nodes[4].node.fail_htlc_backwards(&payment_hash_5));
5444-
assert!(nodes[4].node.fail_htlc_backwards(&payment_hash_6));
5441+
nodes[4].node.fail_htlc_backwards(&payment_hash_1);
5442+
nodes[4].node.fail_htlc_backwards(&payment_hash_3);
5443+
nodes[4].node.fail_htlc_backwards(&payment_hash_5);
5444+
nodes[4].node.fail_htlc_backwards(&payment_hash_6);
54455445
check_added_monitors!(nodes[4], 0);
54465446
expect_pending_htlcs_forwardable!(nodes[4]);
54475447
check_added_monitors!(nodes[4], 1);
@@ -5454,8 +5454,8 @@ fn do_test_fail_backwards_unrevoked_remote_announce(deliver_last_raa: bool, anno
54545454
commitment_signed_dance!(nodes[3], nodes[4], four_removes.commitment_signed, false);
54555455

54565456
// Fail 3rd below-dust and 7th above-dust HTLCs
5457-
assert!(nodes[5].node.fail_htlc_backwards(&payment_hash_2));
5458-
assert!(nodes[5].node.fail_htlc_backwards(&payment_hash_4));
5457+
nodes[5].node.fail_htlc_backwards(&payment_hash_2);
5458+
nodes[5].node.fail_htlc_backwards(&payment_hash_4);
54595459
check_added_monitors!(nodes[5], 0);
54605460
expect_pending_htlcs_forwardable!(nodes[5]);
54615461
check_added_monitors!(nodes[5], 1);
@@ -5880,7 +5880,7 @@ fn do_htlc_claim_previous_remote_commitment_only(use_dust: bool, check_revoke_no
58805880
// actually revoked.
58815881
let htlc_value = if use_dust { 50000 } else { 3000000 };
58825882
let (_, our_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], htlc_value);
5883-
assert!(nodes[1].node.fail_htlc_backwards(&our_payment_hash));
5883+
nodes[1].node.fail_htlc_backwards(&our_payment_hash);
58845884
expect_pending_htlcs_forwardable!(nodes[1]);
58855885
check_added_monitors!(nodes[1], 1);
58865886

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

70587058
// Fail one HTLC to prune it in the will-be-latest-local commitment tx
7059-
assert!(nodes[1].node.fail_htlc_backwards(&payment_hash_2));
7059+
nodes[1].node.fail_htlc_backwards(&payment_hash_2);
70607060
check_added_monitors!(nodes[1], 0);
70617061
expect_pending_htlcs_forwardable!(nodes[1]);
70627062
check_added_monitors!(nodes[1], 1);

lightning/src/ln/onion_route_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1134,7 +1134,7 @@ fn test_phantom_failure_reject_payment() {
11341134
expect_pending_htlcs_forwardable_ignore!(nodes[1]);
11351135
nodes[1].node.process_pending_htlc_forwards();
11361136
expect_payment_received!(nodes[1], payment_hash, payment_secret, recv_amt_msat);
1137-
assert!(nodes[1].node.fail_htlc_backwards(&payment_hash));
1137+
nodes[1].node.fail_htlc_backwards(&payment_hash);
11381138
expect_pending_htlcs_forwardable_ignore!(nodes[1]);
11391139
nodes[1].node.process_pending_htlc_forwards();
11401140

0 commit comments

Comments
 (0)