Skip to content

Commit e0cc997

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

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
@@ -3828,16 +3828,16 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
38283828
&self, mut htlcs_to_fail: Vec<(HTLCSource, PaymentHash)>, channel_id: [u8; 32],
38293829
counterparty_node_id: &PublicKey
38303830
) {
3831-
for (htlc_src, payment_hash) in htlcs_to_fail.drain(..) {
3832-
let (failure_code, onion_failure_data) =
3833-
match self.channel_state.lock().unwrap().by_id.entry(channel_id) {
3834-
hash_map::Entry::Occupied(chan_entry) => {
3835-
self.get_htlc_inbound_temp_fail_err_and_data(0x1000|7, &chan_entry.get())
3836-
},
3837-
hash_map::Entry::Vacant(_) => (0x4000|10, Vec::new())
3838-
};
3831+
let (failure_code, onion_failure_data) =
3832+
match self.channel_state.lock().unwrap().by_id.entry(channel_id) {
3833+
hash_map::Entry::Occupied(chan_entry) => {
3834+
self.get_htlc_inbound_temp_fail_err_and_data(0x1000|7, &chan_entry.get())
3835+
},
3836+
hash_map::Entry::Vacant(_) => (0x4000|10, Vec::new())
3837+
};
38393838

3840-
let reason = HTLCFailReason::reason(failure_code, onion_failure_data);
3839+
for (htlc_src, payment_hash) in htlcs_to_fail.drain(..) {
3840+
let reason = HTLCFailReason::reason(failure_code, onion_failure_data.clone());
38413841
let receiver = HTLCDestination::NextHopChannel { node_id: Some(counterparty_node_id.clone()), channel_id };
38423842
self.fail_htlc_backwards_internal(&htlc_src, &payment_hash, &reason, receiver);
38433843
}

0 commit comments

Comments
 (0)