Skip to content

Commit 4e24f02

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 4b4e428 commit 4e24f02

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 which 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
@@ -1728,7 +1728,7 @@ pub fn fail_payment_along_route<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expe
17281728
for path in expected_paths.iter() {
17291729
assert_eq!(path.last().unwrap().node.get_our_node_id(), expected_paths[0].last().unwrap().node.get_our_node_id());
17301730
}
1731-
assert!(expected_paths[0].last().unwrap().node.fail_htlc_backwards(&our_payment_hash));
1731+
expected_paths[0].last().unwrap().node.fail_htlc_backwards(&our_payment_hash);
17321732
expect_pending_htlcs_forwardable!(expected_paths[0].last().unwrap());
17331733

17341734
pass_failed_payment_back(origin_node, expected_paths, skip_last, our_payment_hash);
@@ -1834,7 +1834,7 @@ pub fn pass_failed_payment_back<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expe
18341834
}
18351835

18361836
// Ensure that fail_htlc_backwards is idempotent.
1837-
assert!(!expected_paths[0].last().unwrap().node.fail_htlc_backwards(&our_payment_hash));
1837+
expected_paths[0].last().unwrap().node.fail_htlc_backwards(&our_payment_hash);
18381838
assert!(expected_paths[0].last().unwrap().node.get_and_clear_pending_events().is_empty());
18391839
assert!(expected_paths[0].last().unwrap().node.get_and_clear_pending_msg_events().is_empty());
18401840
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
@@ -3065,7 +3065,7 @@ fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool, use
30653065
let (_, second_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], value);
30663066
let (_, third_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], value);
30673067

3068-
assert!(nodes[2].node.fail_htlc_backwards(&first_payment_hash));
3068+
nodes[2].node.fail_htlc_backwards(&first_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());
@@ -3078,7 +3078,7 @@ fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool, use
30783078
let bs_raa = commitment_signed_dance!(nodes[1], nodes[2], updates.commitment_signed, false, true, false, true);
30793079
// Drop the last RAA from 3 -> 2
30803080

3081-
assert!(nodes[2].node.fail_htlc_backwards(&second_payment_hash));
3081+
nodes[2].node.fail_htlc_backwards(&second_payment_hash);
30823082
expect_pending_htlcs_forwardable!(nodes[2]);
30833083
check_added_monitors!(nodes[2], 1);
30843084
let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
@@ -3095,7 +3095,7 @@ fn do_test_commitment_revoked_fail_backward_exhaustive(deliver_bs_raa: bool, use
30953095
nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &as_raa);
30963096
check_added_monitors!(nodes[2], 1);
30973097

3098-
assert!(nodes[2].node.fail_htlc_backwards(&third_payment_hash));
3098+
nodes[2].node.fail_htlc_backwards(&third_payment_hash);
30993099
expect_pending_htlcs_forwardable!(nodes[2]);
31003100
check_added_monitors!(nodes[2], 1);
31013101
let updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
@@ -5452,10 +5452,10 @@ fn do_test_fail_backwards_unrevoked_remote_announce(deliver_last_raa: bool, anno
54525452

54535453
// Now fail back three of the over-dust-limit and three of the under-dust-limit payments in one go.
54545454
// Fail 0th below-dust, 4th above-dust, 8th above-dust, 10th below-dust HTLCs
5455-
assert!(nodes[4].node.fail_htlc_backwards(&payment_hash_1));
5456-
assert!(nodes[4].node.fail_htlc_backwards(&payment_hash_3));
5457-
assert!(nodes[4].node.fail_htlc_backwards(&payment_hash_5));
5458-
assert!(nodes[4].node.fail_htlc_backwards(&payment_hash_6));
5455+
nodes[4].node.fail_htlc_backwards(&payment_hash_1);
5456+
nodes[4].node.fail_htlc_backwards(&payment_hash_3);
5457+
nodes[4].node.fail_htlc_backwards(&payment_hash_5);
5458+
nodes[4].node.fail_htlc_backwards(&payment_hash_6);
54595459
check_added_monitors!(nodes[4], 0);
54605460
expect_pending_htlcs_forwardable!(nodes[4]);
54615461
check_added_monitors!(nodes[4], 1);
@@ -5468,8 +5468,8 @@ fn do_test_fail_backwards_unrevoked_remote_announce(deliver_last_raa: bool, anno
54685468
commitment_signed_dance!(nodes[3], nodes[4], four_removes.commitment_signed, false);
54695469

54705470
// Fail 3rd below-dust and 7th above-dust HTLCs
5471-
assert!(nodes[5].node.fail_htlc_backwards(&payment_hash_2));
5472-
assert!(nodes[5].node.fail_htlc_backwards(&payment_hash_4));
5471+
nodes[5].node.fail_htlc_backwards(&payment_hash_2);
5472+
nodes[5].node.fail_htlc_backwards(&payment_hash_4);
54735473
check_added_monitors!(nodes[5], 0);
54745474
expect_pending_htlcs_forwardable!(nodes[5]);
54755475
check_added_monitors!(nodes[5], 1);
@@ -5894,7 +5894,7 @@ fn do_htlc_claim_previous_remote_commitment_only(use_dust: bool, check_revoke_no
58945894
// actually revoked.
58955895
let htlc_value = if use_dust { 50000 } else { 3000000 };
58965896
let (_, our_payment_hash, _) = route_payment(&nodes[0], &[&nodes[1]], htlc_value);
5897-
assert!(nodes[1].node.fail_htlc_backwards(&our_payment_hash));
5897+
nodes[1].node.fail_htlc_backwards(&our_payment_hash);
58985898
expect_pending_htlcs_forwardable!(nodes[1]);
58995899
check_added_monitors!(nodes[1], 1);
59005900

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

70727072
// Fail one HTLC to prune it in the will-be-latest-local commitment tx
7073-
assert!(nodes[1].node.fail_htlc_backwards(&payment_hash_2));
7073+
nodes[1].node.fail_htlc_backwards(&payment_hash_2);
70747074
check_added_monitors!(nodes[1], 0);
70757075
expect_pending_htlcs_forwardable!(nodes[1]);
70767076
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)