Skip to content

Commit aaa2820

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 ee4b2b3 commit aaa2820

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
@@ -3836,16 +3836,16 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
38363836
&self, mut htlcs_to_fail: Vec<(HTLCSource, PaymentHash)>, channel_id: [u8; 32],
38373837
counterparty_node_id: &PublicKey
38383838
) {
3839-
for (htlc_src, payment_hash) in htlcs_to_fail.drain(..) {
3840-
let (failure_code, onion_failure_data) =
3841-
match self.channel_state.lock().unwrap().by_id.entry(channel_id) {
3842-
hash_map::Entry::Occupied(chan_entry) => {
3843-
self.get_htlc_inbound_temp_fail_err_and_data(0x1000|7, &chan_entry.get())
3844-
},
3845-
hash_map::Entry::Vacant(_) => (0x4000|10, Vec::new())
3846-
};
3839+
let (failure_code, onion_failure_data) =
3840+
match self.channel_state.lock().unwrap().by_id.entry(channel_id) {
3841+
hash_map::Entry::Occupied(chan_entry) => {
3842+
self.get_htlc_inbound_temp_fail_err_and_data(0x1000|7, &chan_entry.get())
3843+
},
3844+
hash_map::Entry::Vacant(_) => (0x4000|10, Vec::new())
3845+
};
38473846

3848-
let reason = HTLCFailReason::reason(failure_code, onion_failure_data);
3847+
for (htlc_src, payment_hash) in htlcs_to_fail.drain(..) {
3848+
let reason = HTLCFailReason::reason(failure_code, onion_failure_data.clone());
38493849
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id.clone()), channel_id };
38503850
self.fail_htlc_backwards_internal(&htlc_src, &payment_hash, &reason, receiver);
38513851
}

0 commit comments

Comments
 (0)