Skip to content

Commit fd328e7

Browse files
committed
Drop redundant code in fail_holding_cell_htlcs
`fail_holding_cell_htlcs` calls through to `fail_htlc_backwards_internal` for HTLCs that need to be failed-backwards but opts to generate its own payment failure events for `HTLCSource:;OutboundRoute` HTLCs. There is no reason for that as `fail_htlc_backwards_internal` will also happily generate (now-)equivalent events for `HTLCSource::OutboundRoute` HTLCs. Thus, we can drop the redundant code and always call `fail_htlc_backwards_internal` for each HTLC in `fail_holding_cell_htlcs`.
1 parent 5e65c49 commit fd328e7

File tree

2 files changed

+14
-63
lines changed

2 files changed

+14
-63
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 10 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3812,62 +3812,17 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
38123812
counterparty_node_id: &PublicKey
38133813
) {
38143814
for (htlc_src, payment_hash) in htlcs_to_fail.drain(..) {
3815-
match htlc_src {
3816-
HTLCSource::PreviousHopData(HTLCPreviousHopData { .. }) => {
3817-
let (failure_code, onion_failure_data) =
3818-
match self.channel_state.lock().unwrap().by_id.entry(channel_id) {
3819-
hash_map::Entry::Occupied(chan_entry) => {
3820-
self.get_htlc_inbound_temp_fail_err_and_data(0x1000|7, &chan_entry.get())
3821-
},
3822-
hash_map::Entry::Vacant(_) => (0x4000|10, Vec::new())
3823-
};
3824-
let channel_state = self.channel_state.lock().unwrap();
3815+
let mut channel_state = self.channel_state.lock().unwrap();
3816+
let (failure_code, onion_failure_data) =
3817+
match channel_state.by_id.entry(channel_id) {
3818+
hash_map::Entry::Occupied(chan_entry) => {
3819+
self.get_htlc_inbound_temp_fail_err_and_data(0x1000|7, &chan_entry.get())
3820+
},
3821+
hash_map::Entry::Vacant(_) => (0x4000|10, Vec::new())
3822+
};
38253823

3826-
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id.clone()), channel_id };
3827-
self.fail_htlc_backwards_internal(channel_state, htlc_src, &payment_hash, HTLCFailReason::Reason { failure_code, data: onion_failure_data }, receiver)
3828-
},
3829-
HTLCSource::OutboundRoute { session_priv, payment_id, path, payment_params, .. } => {
3830-
let mut session_priv_bytes = [0; 32];
3831-
session_priv_bytes.copy_from_slice(&session_priv[..]);
3832-
let mut outbounds = self.pending_outbound_payments.lock().unwrap();
3833-
if let hash_map::Entry::Occupied(mut payment) = outbounds.entry(payment_id) {
3834-
if payment.get_mut().remove(&session_priv_bytes, Some(&path)) && !payment.get().is_fulfilled() {
3835-
let retry = if let Some(payment_params_data) = payment_params {
3836-
let path_last_hop = path.last().expect("Outbound payments must have had a valid path");
3837-
Some(RouteParameters {
3838-
payment_params: payment_params_data,
3839-
final_value_msat: path_last_hop.fee_msat,
3840-
final_cltv_expiry_delta: path_last_hop.cltv_expiry_delta,
3841-
})
3842-
} else { None };
3843-
let mut pending_events = self.pending_events.lock().unwrap();
3844-
pending_events.push(events::Event::PaymentPathFailed {
3845-
payment_id: Some(payment_id),
3846-
payment_hash,
3847-
payment_failed_permanently: false,
3848-
network_update: None,
3849-
all_paths_failed: payment.get().remaining_parts() == 0,
3850-
path: path.clone(),
3851-
short_channel_id: None,
3852-
retry,
3853-
#[cfg(test)]
3854-
error_code: None,
3855-
#[cfg(test)]
3856-
error_data: None,
3857-
});
3858-
if payment.get().abandoned() && payment.get().remaining_parts() == 0 {
3859-
pending_events.push(events::Event::PaymentFailed {
3860-
payment_id,
3861-
payment_hash: payment.get().payment_hash().expect("PendingOutboundPayments::RetriesExceeded always has a payment hash set"),
3862-
});
3863-
payment.remove();
3864-
}
3865-
}
3866-
} else {
3867-
log_trace!(self.logger, "Received duplicative fail for HTLC with payment_hash {}", log_bytes!(payment_hash.0));
3868-
}
3869-
},
3870-
};
3824+
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id.clone()), channel_id };
3825+
self.fail_htlc_backwards_internal(channel_state, htlc_src, &payment_hash, HTLCFailReason::Reason { failure_code, data: onion_failure_data }, receiver);
38713826
}
38723827
}
38733828

lightning/src/ln/functional_tests.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6264,15 +6264,13 @@ fn test_fail_holding_cell_htlc_upon_free() {
62646264
let events = nodes[0].node.get_and_clear_pending_events();
62656265
assert_eq!(events.len(), 1);
62666266
match &events[0] {
6267-
&Event::PaymentPathFailed { ref payment_id, ref payment_hash, ref payment_failed_permanently, ref network_update, ref all_paths_failed, ref short_channel_id, ref error_code, ref error_data, .. } => {
6267+
&Event::PaymentPathFailed { ref payment_id, ref payment_hash, ref payment_failed_permanently, ref network_update, ref all_paths_failed, ref short_channel_id, .. } => {
62686268
assert_eq!(our_payment_id, *payment_id.as_ref().unwrap());
62696269
assert_eq!(our_payment_hash.clone(), *payment_hash);
62706270
assert_eq!(*payment_failed_permanently, false);
62716271
assert_eq!(*all_paths_failed, true);
62726272
assert_eq!(*network_update, None);
6273-
assert_eq!(*short_channel_id, None);
6274-
assert_eq!(*error_code, None);
6275-
assert_eq!(*error_data, None);
6273+
assert_eq!(*short_channel_id, Some(route.paths[0][0].short_channel_id));
62766274
},
62776275
_ => panic!("Unexpected event"),
62786276
}
@@ -6350,15 +6348,13 @@ fn test_free_and_fail_holding_cell_htlcs() {
63506348
let events = nodes[0].node.get_and_clear_pending_events();
63516349
assert_eq!(events.len(), 1);
63526350
match &events[0] {
6353-
&Event::PaymentPathFailed { ref payment_id, ref payment_hash, ref payment_failed_permanently, ref network_update, ref all_paths_failed, ref short_channel_id, ref error_code, ref error_data, .. } => {
6351+
&Event::PaymentPathFailed { ref payment_id, ref payment_hash, ref payment_failed_permanently, ref network_update, ref all_paths_failed, ref short_channel_id, .. } => {
63546352
assert_eq!(payment_id_2, *payment_id.as_ref().unwrap());
63556353
assert_eq!(payment_hash_2.clone(), *payment_hash);
63566354
assert_eq!(*payment_failed_permanently, false);
63576355
assert_eq!(*all_paths_failed, true);
63586356
assert_eq!(*network_update, None);
6359-
assert_eq!(*short_channel_id, None);
6360-
assert_eq!(*error_code, None);
6361-
assert_eq!(*error_data, None);
6357+
assert_eq!(*short_channel_id, Some(route_2.paths[0][0].short_channel_id));
63626358
},
63636359
_ => panic!("Unexpected event"),
63646360
}

0 commit comments

Comments
 (0)