Skip to content

Commit 1b70a1e

Browse files
Avoid unnecessary looping over all peers' channels
1 parent e2c5cb5 commit 1b70a1e

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6160,11 +6160,17 @@ where
61606160
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
61616161

61626162
if msg.channel_id == [0; 32] {
6163-
for chan in self.list_channels() {
6164-
if chan.counterparty.node_id == *counterparty_node_id {
6165-
// Untrusted messages from peer, we throw away the error if id points to a non-existent channel
6166-
let _ = self.force_close_channel_with_peer(&chan.channel_id, counterparty_node_id, Some(&msg.data), true);
6167-
}
6163+
let channel_ids: Vec<[u8; 32]> = {
6164+
let per_peer_state = self.per_peer_state.read().unwrap();
6165+
let peer_state_mutex_opt = per_peer_state.get(counterparty_node_id);
6166+
if let None = peer_state_mutex_opt { return; }
6167+
let mut peer_state_lock = peer_state_mutex_opt.unwrap().lock().unwrap();
6168+
let peer_state = &mut *peer_state_lock;
6169+
peer_state.channel_by_id.keys().cloned().collect()
6170+
};
6171+
for channel_id in channel_ids {
6172+
// Untrusted messages from peer, we throw away the error if id points to a non-existent channel
6173+
let _ = self.force_close_channel_with_peer(&channel_id, counterparty_node_id, Some(&msg.data), true);
61686174
}
61696175
} else {
61706176
{

0 commit comments

Comments
 (0)