Skip to content

Commit 5639b8d

Browse files
committed
Make fail_htlc_backwards_internal borrow parameters
Currently `fail_htlc_backwards_internal` takes ownership of its source and reason parameters however they are not consumed so we can borrow them. Includes refactoring to use local variables before the function call.
1 parent 8c7d23d commit 5639b8d

File tree

1 file changed

+48
-40
lines changed

1 file changed

+48
-40
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,8 +1860,9 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
18601860
};
18611861

18621862
for htlc_source in failed_htlcs.drain(..) {
1863+
let reason = HTLCFailReason::from_failure_code(0x4000 | 8);
18631864
let receiver = HTLCDestination::NextHopChannel { node_id: Some(*counterparty_node_id), channel_id: *channel_id };
1864-
self.fail_htlc_backwards_internal(htlc_source.0, &htlc_source.1, HTLCFailReason::from_failure_code(0x4000 | 8), receiver);
1865+
self.fail_htlc_backwards_internal(&htlc_source.0, &htlc_source.1, &reason, receiver);
18651866
}
18661867

18671868
let _ = handle_error!(self, result, *counterparty_node_id);
@@ -1918,8 +1919,9 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
19181919
log_debug!(self.logger, "Finishing force-closure of channel with {} HTLCs to fail", failed_htlcs.len());
19191920
for htlc_source in failed_htlcs.drain(..) {
19201921
let (source, payment_hash, counterparty_node_id, channel_id) = htlc_source;
1922+
let reason = HTLCFailReason::from_failure_code(0x4000 | 8);
19211923
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id), channel_id };
1922-
self.fail_htlc_backwards_internal(source, &payment_hash, HTLCFailReason::from_failure_code(0x4000 | 8), receiver);
1924+
self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, receiver);
19231925
}
19241926
if let Some((funding_txo, monitor_update)) = monitor_update_option {
19251927
// There isn't anything we can do if we get an update failure - we're already
@@ -3484,7 +3486,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
34843486
}
34853487

34863488
for (htlc_source, payment_hash, failure_reason, destination) in failed_forwards.drain(..) {
3487-
self.fail_htlc_backwards_internal(htlc_source, &payment_hash, failure_reason, destination);
3489+
self.fail_htlc_backwards_internal(&htlc_source, &payment_hash, &failure_reason, destination);
34883490
}
34893491
self.forward_htlcs(&mut phantom_receives);
34903492

@@ -3747,8 +3749,10 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
37473749
});
37483750

37493751
for htlc_source in timed_out_mpp_htlcs.drain(..) {
3752+
let source = HTLCSource::PreviousHopData(htlc_source.0.clone());
3753+
let reason = HTLCFailReason::from_failure_code(23);
37503754
let receiver = HTLCDestination::FailedPayment { payment_hash: htlc_source.1 };
3751-
self.fail_htlc_backwards_internal(HTLCSource::PreviousHopData(htlc_source.0.clone()), &htlc_source.1, HTLCFailReason::from_failure_code(23), receiver );
3755+
self.fail_htlc_backwards_internal(&source, &htlc_source.1, &reason, receiver);
37523756
}
37533757

37543758
for (err, counterparty_node_id) in handle_errors.drain(..) {
@@ -3783,10 +3787,10 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
37833787
let mut htlc_msat_height_data = byte_utils::be64_to_array(htlc.value).to_vec();
37843788
htlc_msat_height_data.extend_from_slice(&byte_utils::be32_to_array(
37853789
self.best_block.read().unwrap().height()));
3786-
self.fail_htlc_backwards_internal(
3787-
HTLCSource::PreviousHopData(htlc.prev_hop), payment_hash,
3788-
HTLCFailReason::reason(0x4000 | 15, htlc_msat_height_data),
3789-
HTLCDestination::FailedPayment { payment_hash: *payment_hash });
3790+
let source = HTLCSource::PreviousHopData(htlc.prev_hop);
3791+
let reason = HTLCFailReason::reason(0x4000 | 15, htlc_msat_height_data);
3792+
let receiver = HTLCDestination::FailedPayment { payment_hash: *payment_hash };
3793+
self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, receiver);
37903794
}
37913795
}
37923796
}
@@ -3854,14 +3858,15 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
38543858
hash_map::Entry::Vacant(_) => (0x4000|10, Vec::new())
38553859
};
38563860

3861+
let reason = HTLCFailReason::reason(failure_code, onion_failure_data);
38573862
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id.clone()), channel_id };
3858-
self.fail_htlc_backwards_internal(htlc_src, &payment_hash, HTLCFailReason::reason(failure_code, onion_failure_data), receiver);
3863+
self.fail_htlc_backwards_internal(&htlc_src, &payment_hash, &reason, receiver);
38593864
}
38603865
}
38613866

38623867
/// Fails an HTLC backwards to the sender of it to us.
38633868
/// Note that we do not assume that channels corresponding to failed HTLCs are still available.
3864-
fn fail_htlc_backwards_internal(&self, source: HTLCSource, payment_hash: &PaymentHash, onion_error: HTLCFailReason,destination: HTLCDestination) {
3869+
fn fail_htlc_backwards_internal(&self, source: &HTLCSource, payment_hash: &PaymentHash, onion_error: &HTLCFailReason, destination: HTLCDestination) {
38653870
#[cfg(debug_assertions)]
38663871
{
38673872
// Ensure that the `channel_state` lock is not held when calling this function.
@@ -3880,13 +3885,13 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
38803885
// from block_connected which may run during initialization prior to the chain_monitor
38813886
// being fully configured. See the docs for `ChannelManagerReadArgs` for more.
38823887
match source {
3883-
HTLCSource::OutboundRoute { ref path, session_priv, payment_id, ref payment_params, .. } => {
3888+
HTLCSource::OutboundRoute { ref path, ref session_priv, ref payment_id, ref payment_params, .. } => {
38843889
let mut session_priv_bytes = [0; 32];
38853890
session_priv_bytes.copy_from_slice(&session_priv[..]);
38863891
let mut outbounds = self.pending_outbound_payments.lock().unwrap();
38873892
let mut all_paths_failed = false;
38883893
let mut full_failure_ev = None;
3889-
if let hash_map::Entry::Occupied(mut payment) = outbounds.entry(payment_id) {
3894+
if let hash_map::Entry::Occupied(mut payment) = outbounds.entry(*payment_id) {
38903895
if !payment.get_mut().remove(&session_priv_bytes, Some(&path)) {
38913896
log_trace!(self.logger, "Received duplicative fail for HTLC with payment_hash {}", log_bytes!(payment_hash.0));
38923897
return;
@@ -3899,7 +3904,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
38993904
all_paths_failed = true;
39003905
if payment.get().abandoned() {
39013906
full_failure_ev = Some(events::Event::PaymentFailed {
3902-
payment_id,
3907+
payment_id: *payment_id,
39033908
payment_hash: payment.get().payment_hash().expect("PendingOutboundPayments::RetriesExceeded always has a payment hash set"),
39043909
});
39053910
payment.remove();
@@ -3929,13 +3934,13 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
39293934
if self.payment_is_probe(payment_hash, &payment_id) {
39303935
if !payment_retryable {
39313936
events::Event::ProbeSuccessful {
3932-
payment_id,
3937+
payment_id: *payment_id,
39333938
payment_hash: payment_hash.clone(),
39343939
path: path.clone(),
39353940
}
39363941
} else {
39373942
events::Event::ProbeFailed {
3938-
payment_id,
3943+
payment_id: *payment_id,
39393944
payment_hash: payment_hash.clone(),
39403945
path: path.clone(),
39413946
short_channel_id,
@@ -3949,7 +3954,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
39493954
retry.as_mut().map(|r| r.payment_params.previously_failed_channels.push(scid));
39503955
}
39513956
events::Event::PaymentPathFailed {
3952-
payment_id: Some(payment_id),
3957+
payment_id: Some(*payment_id),
39533958
payment_hash: payment_hash.clone(),
39543959
payment_failed_permanently: !payment_retryable,
39553960
network_update,
@@ -3982,14 +3987,14 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
39823987

39833988
if self.payment_is_probe(payment_hash, &payment_id) {
39843989
events::Event::ProbeFailed {
3985-
payment_id,
3990+
payment_id: *payment_id,
39863991
payment_hash: payment_hash.clone(),
39873992
path: path.clone(),
39883993
short_channel_id: Some(scid),
39893994
}
39903995
} else {
39913996
events::Event::PaymentPathFailed {
3992-
payment_id: Some(payment_id),
3997+
payment_id: Some(*payment_id),
39933998
payment_hash: payment_hash.clone(),
39943999
payment_failed_permanently: false,
39954000
network_update: None,
@@ -4009,22 +4014,22 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
40094014
pending_events.push(path_failure);
40104015
if let Some(ev) = full_failure_ev { pending_events.push(ev); }
40114016
},
4012-
HTLCSource::PreviousHopData(HTLCPreviousHopData { short_channel_id, htlc_id, incoming_packet_shared_secret, phantom_shared_secret, outpoint }) => {
4017+
HTLCSource::PreviousHopData(HTLCPreviousHopData { ref short_channel_id, ref htlc_id, ref incoming_packet_shared_secret, ref phantom_shared_secret, ref outpoint }) => {
40134018
let err_packet = match onion_error {
4014-
HTLCFailReason::Reason { failure_code, data } => {
4019+
HTLCFailReason::Reason { ref failure_code, ref data } => {
40154020
log_trace!(self.logger, "Failing HTLC with payment_hash {} backwards from us with code {}", log_bytes!(payment_hash.0), failure_code);
40164021
if let Some(phantom_ss) = phantom_shared_secret {
4017-
let phantom_packet = onion_utils::build_failure_packet(&phantom_ss, failure_code, &data[..]).encode();
4018-
let encrypted_phantom_packet = onion_utils::encrypt_failure_packet(&phantom_ss, &phantom_packet);
4019-
onion_utils::encrypt_failure_packet(&incoming_packet_shared_secret, &encrypted_phantom_packet.data[..])
4022+
let phantom_packet = onion_utils::build_failure_packet(phantom_ss, *failure_code, &data[..]).encode();
4023+
let encrypted_phantom_packet = onion_utils::encrypt_failure_packet(phantom_ss, &phantom_packet);
4024+
onion_utils::encrypt_failure_packet(incoming_packet_shared_secret, &encrypted_phantom_packet.data[..])
40204025
} else {
4021-
let packet = onion_utils::build_failure_packet(&incoming_packet_shared_secret, failure_code, &data[..]).encode();
4022-
onion_utils::encrypt_failure_packet(&incoming_packet_shared_secret, &packet)
4026+
let packet = onion_utils::build_failure_packet(incoming_packet_shared_secret, *failure_code, &data[..]).encode();
4027+
onion_utils::encrypt_failure_packet(incoming_packet_shared_secret, &packet)
40234028
}
40244029
},
40254030
HTLCFailReason::LightningError { err } => {
40264031
log_trace!(self.logger, "Failing HTLC with payment_hash {} backwards with pre-built LightningError", log_bytes!(payment_hash.0));
4027-
onion_utils::encrypt_failure_packet(&incoming_packet_shared_secret, &err.data)
4032+
onion_utils::encrypt_failure_packet(incoming_packet_shared_secret, &err.data)
40284033
}
40294034
};
40304035

@@ -4033,12 +4038,12 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
40334038
if forward_htlcs.is_empty() {
40344039
forward_event = Some(Duration::from_millis(MIN_HTLC_RELAY_HOLDING_CELL_MILLIS));
40354040
}
4036-
match forward_htlcs.entry(short_channel_id) {
4041+
match forward_htlcs.entry(*short_channel_id) {
40374042
hash_map::Entry::Occupied(mut entry) => {
4038-
entry.get_mut().push(HTLCForwardInfo::FailHTLC { htlc_id, err_packet });
4043+
entry.get_mut().push(HTLCForwardInfo::FailHTLC { htlc_id: *htlc_id, err_packet });
40394044
},
40404045
hash_map::Entry::Vacant(entry) => {
4041-
entry.insert(vec!(HTLCForwardInfo::FailHTLC { htlc_id, err_packet }));
4046+
entry.insert(vec!(HTLCForwardInfo::FailHTLC { htlc_id: *htlc_id, err_packet }));
40424047
}
40434048
}
40444049
mem::drop(forward_htlcs);
@@ -4050,7 +4055,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
40504055
}
40514056
pending_events.push(events::Event::HTLCHandlingFailed {
40524057
prev_channel_id: outpoint.to_channel_id(),
4053-
failed_next_destination: destination
4058+
failed_next_destination: destination,
40544059
});
40554060
},
40564061
}
@@ -4179,10 +4184,10 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
41794184
let mut htlc_msat_height_data = byte_utils::be64_to_array(htlc.value).to_vec();
41804185
htlc_msat_height_data.extend_from_slice(&byte_utils::be32_to_array(
41814186
self.best_block.read().unwrap().height()));
4182-
self.fail_htlc_backwards_internal(
4183-
HTLCSource::PreviousHopData(htlc.prev_hop), &payment_hash,
4184-
HTLCFailReason::reason(0x4000 | 15, htlc_msat_height_data),
4185-
HTLCDestination::FailedPayment { payment_hash } );
4187+
let source = HTLCSource::PreviousHopData(htlc.prev_hop);
4188+
let reason = HTLCFailReason::reason(0x4000 | 15, htlc_msat_height_data);
4189+
let receiver = HTLCDestination::FailedPayment { payment_hash };
4190+
self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, receiver);
41864191
}
41874192
}
41884193

@@ -4506,7 +4511,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
45064511
self.finalize_claims(finalized_claims);
45074512
for failure in pending_failures.drain(..) {
45084513
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id), channel_id: funding_txo.to_channel_id() };
4509-
self.fail_htlc_backwards_internal(failure.0, &failure.1, failure.2, receiver);
4514+
self.fail_htlc_backwards_internal(&failure.0, &failure.1, &failure.2, receiver);
45104515
}
45114516
}
45124517

@@ -4874,7 +4879,8 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
48744879
};
48754880
for htlc_source in dropped_htlcs.drain(..) {
48764881
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id.clone()), channel_id: msg.channel_id };
4877-
self.fail_htlc_backwards_internal(htlc_source.0, &htlc_source.1, HTLCFailReason::from_failure_code(0x4000 | 8), receiver);
4882+
let reason = HTLCFailReason::from_failure_code(0x4000 | 8);
4883+
self.fail_htlc_backwards_internal(&htlc_source.0, &htlc_source.1, &reason, receiver);
48784884
}
48794885

48804886
let _ = handle_error!(self, result, *counterparty_node_id);
@@ -5166,7 +5172,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
51665172
{
51675173
for failure in pending_failures.drain(..) {
51685174
let receiver = HTLCDestination::NextHopChannel { node_id: Some(*counterparty_node_id), channel_id: channel_outpoint.to_channel_id() };
5169-
self.fail_htlc_backwards_internal(failure.0, &failure.1, failure.2, receiver);
5175+
self.fail_htlc_backwards_internal(&failure.0, &failure.1, &failure.2, receiver);
51705176
}
51715177
self.forward_htlcs(&mut [(short_channel_id, channel_outpoint, user_channel_id, pending_forwards)]);
51725178
self.finalize_claims(finalized_claim_htlcs);
@@ -5326,7 +5332,8 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
53265332
} else {
53275333
log_trace!(self.logger, "Failing HTLC with hash {} from our monitor", log_bytes!(htlc_update.payment_hash.0));
53285334
let receiver = HTLCDestination::NextHopChannel { node_id: counterparty_node_id, channel_id: funding_outpoint.to_channel_id() };
5329-
self.fail_htlc_backwards_internal(htlc_update.source, &htlc_update.payment_hash, HTLCFailReason::from_failure_code(0x4000 | 8), receiver);
5335+
let reason = HTLCFailReason::from_failure_code(0x4000 | 8);
5336+
self.fail_htlc_backwards_internal(&htlc_update.source, &htlc_update.payment_hash, &reason, receiver);
53305337
}
53315338
},
53325339
MonitorEvent::CommitmentTxConfirmed(funding_outpoint) |
@@ -6096,7 +6103,7 @@ where
60966103
self.handle_init_event_channel_failures(failed_channels);
60976104

60986105
for (source, payment_hash, reason, destination) in timed_out_htlcs.drain(..) {
6099-
self.fail_htlc_backwards_internal(source, &payment_hash, reason, destination);
6106+
self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, destination);
61006107
}
61016108
}
61026109

@@ -7573,7 +7580,8 @@ impl<'a, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
75737580
for htlc_source in failed_htlcs.drain(..) {
75747581
let (source, payment_hash, counterparty_node_id, channel_id) = htlc_source;
75757582
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id), channel_id };
7576-
channel_manager.fail_htlc_backwards_internal(source, &payment_hash, HTLCFailReason::from_failure_code(0x4000 | 8), receiver);
7583+
let reason = HTLCFailReason::from_failure_code(0x4000 | 8);
7584+
channel_manager.fail_htlc_backwards_internal(&source, &payment_hash, &reason, receiver);
75777585
}
75787586

75797587
//TODO: Broadcast channel update for closed channels, but only after we've made a

0 commit comments

Comments
 (0)