Skip to content

Commit 1dd3184

Browse files
committed
Do not lock while looping htlcs_to_fail
Currently we loop over `htlcs_to_fail` locking `channel_state` for each element only to call `get_htlc_inbound_temp_fail_err_and_data` with the same inputs on each iteration. This is unnecessary, we can refactor and call `get_htlc_inbound_temp_fail_err_and_data` outside of the loop.
1 parent c21378f commit 1dd3184

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3974,16 +3974,16 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
39743974
&self, mut htlcs_to_fail: Vec<(HTLCSource, PaymentHash)>, channel_id: [u8; 32],
39753975
counterparty_node_id: &PublicKey
39763976
) {
3977-
for (htlc_src, payment_hash) in htlcs_to_fail.drain(..) {
3978-
let (failure_code, onion_failure_data) =
3979-
match self.channel_state.lock().unwrap().by_id.entry(channel_id) {
3980-
hash_map::Entry::Occupied(chan_entry) => {
3981-
self.get_htlc_inbound_temp_fail_err_and_data(0x1000|7, &chan_entry.get())
3982-
},
3983-
hash_map::Entry::Vacant(_) => (0x4000|10, Vec::new())
3984-
};
3977+
let (failure_code, onion_failure_data) =
3978+
match self.channel_state.lock().unwrap().by_id.entry(channel_id) {
3979+
hash_map::Entry::Occupied(chan_entry) => {
3980+
self.get_htlc_inbound_temp_fail_err_and_data(0x1000|7, &chan_entry.get())
3981+
},
3982+
hash_map::Entry::Vacant(_) => (0x4000|10, Vec::new())
3983+
};
39853984

3986-
let reason = HTLCFailReason::reason(failure_code, onion_failure_data);
3985+
for (htlc_src, payment_hash) in htlcs_to_fail.drain(..) {
3986+
let reason = HTLCFailReason::reason(failure_code, onion_failure_data.clone());
39873987
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id.clone()), channel_id };
39883988
self.fail_htlc_backwards_internal(&htlc_src, &payment_hash, &reason, receiver);
39893989
}

0 commit comments

Comments
 (0)