Skip to content

Commit bc63a9c

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 ece31e6 commit bc63a9c

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
@@ -1853,8 +1853,9 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
18531853
};
18541854

18551855
for htlc_source in failed_htlcs.drain(..) {
1856+
let reason = HTLCFailReason::from_failure_code(0x4000 | 8);
18561857
let receiver = HTLCDestination::NextHopChannel { node_id: Some(*counterparty_node_id), channel_id: *channel_id };
1857-
self.fail_htlc_backwards_internal(htlc_source.0, &htlc_source.1, HTLCFailReason::from_failure_code(0x4000 | 8), receiver);
1858+
self.fail_htlc_backwards_internal(&htlc_source.0, &htlc_source.1, &reason, receiver);
18581859
}
18591860

18601861
let _ = handle_error!(self, result, *counterparty_node_id);
@@ -1911,8 +1912,9 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
19111912
log_debug!(self.logger, "Finishing force-closure of channel with {} HTLCs to fail", failed_htlcs.len());
19121913
for htlc_source in failed_htlcs.drain(..) {
19131914
let (source, payment_hash, counterparty_node_id, channel_id) = htlc_source;
1915+
let reason = HTLCFailReason::from_failure_code(0x4000 | 8);
19141916
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id), channel_id };
1915-
self.fail_htlc_backwards_internal(source, &payment_hash, HTLCFailReason::from_failure_code(0x4000 | 8), receiver);
1917+
self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, receiver);
19161918
}
19171919
if let Some((funding_txo, monitor_update)) = monitor_update_option {
19181920
// There isn't anything we can do if we get an update failure - we're already
@@ -3463,7 +3465,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
34633465
}
34643466

34653467
for (htlc_source, payment_hash, failure_reason, destination) in failed_forwards.drain(..) {
3466-
self.fail_htlc_backwards_internal(htlc_source, &payment_hash, failure_reason, destination);
3468+
self.fail_htlc_backwards_internal(&htlc_source, &payment_hash, &failure_reason, destination);
34673469
}
34683470
self.forward_htlcs(&mut phantom_receives);
34693471

@@ -3726,8 +3728,10 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
37263728
});
37273729

37283730
for htlc_source in timed_out_mpp_htlcs.drain(..) {
3731+
let source = HTLCSource::PreviousHopData(htlc_source.0.clone());
3732+
let reason = HTLCFailReason::from_failure_code(23);
37293733
let receiver = HTLCDestination::FailedPayment { payment_hash: htlc_source.1 };
3730-
self.fail_htlc_backwards_internal(HTLCSource::PreviousHopData(htlc_source.0.clone()), &htlc_source.1, HTLCFailReason::from_failure_code(23), receiver );
3734+
self.fail_htlc_backwards_internal(&source, &htlc_source.1, &reason, receiver);
37313735
}
37323736

37333737
for (err, counterparty_node_id) in handle_errors.drain(..) {
@@ -3762,10 +3766,10 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
37623766
let mut htlc_msat_height_data = byte_utils::be64_to_array(htlc.value).to_vec();
37633767
htlc_msat_height_data.extend_from_slice(&byte_utils::be32_to_array(
37643768
self.best_block.read().unwrap().height()));
3765-
self.fail_htlc_backwards_internal(
3766-
HTLCSource::PreviousHopData(htlc.prev_hop), payment_hash,
3767-
HTLCFailReason::reason(0x4000 | 15, htlc_msat_height_data),
3768-
HTLCDestination::FailedPayment { payment_hash: *payment_hash });
3769+
let source = HTLCSource::PreviousHopData(htlc.prev_hop);
3770+
let reason = HTLCFailReason::reason(0x4000 | 15, htlc_msat_height_data);
3771+
let receiver = HTLCDestination::FailedPayment { payment_hash: *payment_hash };
3772+
self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, receiver);
37693773
}
37703774
}
37713775
}
@@ -3833,14 +3837,15 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
38333837
hash_map::Entry::Vacant(_) => (0x4000|10, Vec::new())
38343838
};
38353839

3840+
let reason = HTLCFailReason::reason(failure_code, onion_failure_data);
38363841
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id.clone()), channel_id };
3837-
self.fail_htlc_backwards_internal(htlc_src, &payment_hash, HTLCFailReason::reason(failure_code, onion_failure_data), receiver);
3842+
self.fail_htlc_backwards_internal(&htlc_src, &payment_hash, &reason, receiver);
38383843
}
38393844
}
38403845

38413846
/// Fails an HTLC backwards to the sender of it to us.
38423847
/// Note that we do not assume that channels corresponding to failed HTLCs are still available.
3843-
fn fail_htlc_backwards_internal(&self, source: HTLCSource, payment_hash: &PaymentHash, onion_error: HTLCFailReason,destination: HTLCDestination) {
3848+
fn fail_htlc_backwards_internal(&self, source: &HTLCSource, payment_hash: &PaymentHash, onion_error: &HTLCFailReason, destination: HTLCDestination) {
38443849
#[cfg(debug_assertions)]
38453850
{
38463851
// Ensure that the `channel_state` lock is not held when calling this function.
@@ -3859,13 +3864,13 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
38593864
// from block_connected which may run during initialization prior to the chain_monitor
38603865
// being fully configured. See the docs for `ChannelManagerReadArgs` for more.
38613866
match source {
3862-
HTLCSource::OutboundRoute { ref path, session_priv, payment_id, ref payment_params, .. } => {
3867+
HTLCSource::OutboundRoute { ref path, ref session_priv, ref payment_id, ref payment_params, .. } => {
38633868
let mut session_priv_bytes = [0; 32];
38643869
session_priv_bytes.copy_from_slice(&session_priv[..]);
38653870
let mut outbounds = self.pending_outbound_payments.lock().unwrap();
38663871
let mut all_paths_failed = false;
38673872
let mut full_failure_ev = None;
3868-
if let hash_map::Entry::Occupied(mut payment) = outbounds.entry(payment_id) {
3873+
if let hash_map::Entry::Occupied(mut payment) = outbounds.entry(*payment_id) {
38693874
if !payment.get_mut().remove(&session_priv_bytes, Some(&path)) {
38703875
log_trace!(self.logger, "Received duplicative fail for HTLC with payment_hash {}", log_bytes!(payment_hash.0));
38713876
return;
@@ -3878,7 +3883,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
38783883
all_paths_failed = true;
38793884
if payment.get().abandoned() {
38803885
full_failure_ev = Some(events::Event::PaymentFailed {
3881-
payment_id,
3886+
payment_id: *payment_id,
38823887
payment_hash: payment.get().payment_hash().expect("PendingOutboundPayments::RetriesExceeded always has a payment hash set"),
38833888
});
38843889
payment.remove();
@@ -3908,13 +3913,13 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
39083913
if self.payment_is_probe(payment_hash, &payment_id) {
39093914
if !payment_retryable {
39103915
events::Event::ProbeSuccessful {
3911-
payment_id,
3916+
payment_id: *payment_id,
39123917
payment_hash: payment_hash.clone(),
39133918
path: path.clone(),
39143919
}
39153920
} else {
39163921
events::Event::ProbeFailed {
3917-
payment_id,
3922+
payment_id: *payment_id,
39183923
payment_hash: payment_hash.clone(),
39193924
path: path.clone(),
39203925
short_channel_id,
@@ -3928,7 +3933,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
39283933
retry.as_mut().map(|r| r.payment_params.previously_failed_channels.push(scid));
39293934
}
39303935
events::Event::PaymentPathFailed {
3931-
payment_id: Some(payment_id),
3936+
payment_id: Some(*payment_id),
39323937
payment_hash: payment_hash.clone(),
39333938
payment_failed_permanently: !payment_retryable,
39343939
network_update,
@@ -3961,14 +3966,14 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
39613966

39623967
if self.payment_is_probe(payment_hash, &payment_id) {
39633968
events::Event::ProbeFailed {
3964-
payment_id,
3969+
payment_id: *payment_id,
39653970
payment_hash: payment_hash.clone(),
39663971
path: path.clone(),
39673972
short_channel_id: Some(scid),
39683973
}
39693974
} else {
39703975
events::Event::PaymentPathFailed {
3971-
payment_id: Some(payment_id),
3976+
payment_id: Some(*payment_id),
39723977
payment_hash: payment_hash.clone(),
39733978
payment_failed_permanently: false,
39743979
network_update: None,
@@ -3988,22 +3993,22 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
39883993
pending_events.push(path_failure);
39893994
if let Some(ev) = full_failure_ev { pending_events.push(ev); }
39903995
},
3991-
HTLCSource::PreviousHopData(HTLCPreviousHopData { short_channel_id, htlc_id, incoming_packet_shared_secret, phantom_shared_secret, outpoint }) => {
3996+
HTLCSource::PreviousHopData(HTLCPreviousHopData { ref short_channel_id, ref htlc_id, ref incoming_packet_shared_secret, ref phantom_shared_secret, ref outpoint }) => {
39923997
let err_packet = match onion_error {
3993-
HTLCFailReason::Reason { failure_code, data } => {
3998+
HTLCFailReason::Reason { ref failure_code, ref data } => {
39943999
log_trace!(self.logger, "Failing HTLC with payment_hash {} backwards from us with code {}", log_bytes!(payment_hash.0), failure_code);
39954000
if let Some(phantom_ss) = phantom_shared_secret {
3996-
let phantom_packet = onion_utils::build_failure_packet(&phantom_ss, failure_code, &data[..]).encode();
3997-
let encrypted_phantom_packet = onion_utils::encrypt_failure_packet(&phantom_ss, &phantom_packet);
3998-
onion_utils::encrypt_failure_packet(&incoming_packet_shared_secret, &encrypted_phantom_packet.data[..])
4001+
let phantom_packet = onion_utils::build_failure_packet(phantom_ss, *failure_code, &data[..]).encode();
4002+
let encrypted_phantom_packet = onion_utils::encrypt_failure_packet(phantom_ss, &phantom_packet);
4003+
onion_utils::encrypt_failure_packet(incoming_packet_shared_secret, &encrypted_phantom_packet.data[..])
39994004
} else {
4000-
let packet = onion_utils::build_failure_packet(&incoming_packet_shared_secret, failure_code, &data[..]).encode();
4001-
onion_utils::encrypt_failure_packet(&incoming_packet_shared_secret, &packet)
4005+
let packet = onion_utils::build_failure_packet(incoming_packet_shared_secret, *failure_code, &data[..]).encode();
4006+
onion_utils::encrypt_failure_packet(incoming_packet_shared_secret, &packet)
40024007
}
40034008
},
40044009
HTLCFailReason::LightningError { err } => {
40054010
log_trace!(self.logger, "Failing HTLC with payment_hash {} backwards with pre-built LightningError", log_bytes!(payment_hash.0));
4006-
onion_utils::encrypt_failure_packet(&incoming_packet_shared_secret, &err.data)
4011+
onion_utils::encrypt_failure_packet(incoming_packet_shared_secret, &err.data)
40074012
}
40084013
};
40094014

@@ -4012,12 +4017,12 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
40124017
if forward_htlcs.is_empty() {
40134018
forward_event = Some(Duration::from_millis(MIN_HTLC_RELAY_HOLDING_CELL_MILLIS));
40144019
}
4015-
match forward_htlcs.entry(short_channel_id) {
4020+
match forward_htlcs.entry(*short_channel_id) {
40164021
hash_map::Entry::Occupied(mut entry) => {
4017-
entry.get_mut().push(HTLCForwardInfo::FailHTLC { htlc_id, err_packet });
4022+
entry.get_mut().push(HTLCForwardInfo::FailHTLC { htlc_id: *htlc_id, err_packet });
40184023
},
40194024
hash_map::Entry::Vacant(entry) => {
4020-
entry.insert(vec!(HTLCForwardInfo::FailHTLC { htlc_id, err_packet }));
4025+
entry.insert(vec!(HTLCForwardInfo::FailHTLC { htlc_id: *htlc_id, err_packet }));
40214026
}
40224027
}
40234028
mem::drop(forward_htlcs);
@@ -4029,7 +4034,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
40294034
}
40304035
pending_events.push(events::Event::HTLCHandlingFailed {
40314036
prev_channel_id: outpoint.to_channel_id(),
4032-
failed_next_destination: destination
4037+
failed_next_destination: destination,
40334038
});
40344039
},
40354040
}
@@ -4151,10 +4156,10 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
41514156
let mut htlc_msat_height_data = byte_utils::be64_to_array(htlc.value).to_vec();
41524157
htlc_msat_height_data.extend_from_slice(&byte_utils::be32_to_array(
41534158
self.best_block.read().unwrap().height()));
4154-
self.fail_htlc_backwards_internal(
4155-
HTLCSource::PreviousHopData(htlc.prev_hop), &payment_hash,
4156-
HTLCFailReason::reason(0x4000 | 15, htlc_msat_height_data),
4157-
HTLCDestination::FailedPayment { payment_hash } );
4159+
let source = HTLCSource::PreviousHopData(htlc.prev_hop);
4160+
let reason = HTLCFailReason::reason(0x4000 | 15, htlc_msat_height_data);
4161+
let receiver = HTLCDestination::FailedPayment { payment_hash };
4162+
self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, receiver);
41584163
}
41594164
}
41604165

@@ -4482,7 +4487,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
44824487
self.finalize_claims(finalized_claims);
44834488
for failure in pending_failures.drain(..) {
44844489
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id), channel_id: funding_txo.to_channel_id() };
4485-
self.fail_htlc_backwards_internal(failure.0, &failure.1, failure.2, receiver);
4490+
self.fail_htlc_backwards_internal(&failure.0, &failure.1, &failure.2, receiver);
44864491
}
44874492
}
44884493

@@ -4850,7 +4855,8 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
48504855
};
48514856
for htlc_source in dropped_htlcs.drain(..) {
48524857
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id.clone()), channel_id: msg.channel_id };
4853-
self.fail_htlc_backwards_internal(htlc_source.0, &htlc_source.1, HTLCFailReason::from_failure_code(0x4000 | 8), receiver);
4858+
let reason = HTLCFailReason::from_failure_code(0x4000 | 8);
4859+
self.fail_htlc_backwards_internal(&htlc_source.0, &htlc_source.1, &reason, receiver);
48544860
}
48554861

48564862
let _ = handle_error!(self, result, *counterparty_node_id);
@@ -5141,7 +5147,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
51415147
{
51425148
for failure in pending_failures.drain(..) {
51435149
let receiver = HTLCDestination::NextHopChannel { node_id: Some(*counterparty_node_id), channel_id: channel_outpoint.to_channel_id() };
5144-
self.fail_htlc_backwards_internal(failure.0, &failure.1, failure.2, receiver);
5150+
self.fail_htlc_backwards_internal(&failure.0, &failure.1, &failure.2, receiver);
51455151
}
51465152
self.forward_htlcs(&mut [(short_channel_id, channel_outpoint, pending_forwards)]);
51475153
self.finalize_claims(finalized_claim_htlcs);
@@ -5301,7 +5307,8 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
53015307
} else {
53025308
log_trace!(self.logger, "Failing HTLC with hash {} from our monitor", log_bytes!(htlc_update.payment_hash.0));
53035309
let receiver = HTLCDestination::NextHopChannel { node_id: counterparty_node_id, channel_id: funding_outpoint.to_channel_id() };
5304-
self.fail_htlc_backwards_internal(htlc_update.source, &htlc_update.payment_hash, HTLCFailReason::from_failure_code(0x4000 | 8), receiver);
5310+
let reason = HTLCFailReason::from_failure_code(0x4000 | 8);
5311+
self.fail_htlc_backwards_internal(&htlc_update.source, &htlc_update.payment_hash, &reason, receiver);
53055312
}
53065313
},
53075314
MonitorEvent::CommitmentTxConfirmed(funding_outpoint) |
@@ -6071,7 +6078,7 @@ where
60716078
self.handle_init_event_channel_failures(failed_channels);
60726079

60736080
for (source, payment_hash, reason, destination) in timed_out_htlcs.drain(..) {
6074-
self.fail_htlc_backwards_internal(source, &payment_hash, reason, destination);
6081+
self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, destination);
60756082
}
60766083
}
60776084

@@ -7536,7 +7543,8 @@ impl<'a, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
75367543
for htlc_source in failed_htlcs.drain(..) {
75377544
let (source, payment_hash, counterparty_node_id, channel_id) = htlc_source;
75387545
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id), channel_id };
7539-
channel_manager.fail_htlc_backwards_internal(source, &payment_hash, HTLCFailReason::from_failure_code(0x4000 | 8), receiver);
7546+
let reason = HTLCFailReason::from_failure_code(0x4000 | 8);
7547+
channel_manager.fail_htlc_backwards_internal(&source, &payment_hash, &reason, receiver);
75407548
}
75417549

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

0 commit comments

Comments
 (0)