@@ -2848,26 +2848,35 @@ where
2848
2848
let mut should_persist = NotifyOption::SkipPersistNoEvents;
2849
2849
2850
2850
// Try to send the `BroadcastChannelUpdate` to the peer we just force-closed on, but if
2851
- // not try to broadcast it via whatever peer we have .
2851
+ // not try to broadcast it via whatever peer we are connected to .
2852
2852
let brodcast_message_evt = events::MessageSendEvent::BroadcastChannelUpdate {
2853
2853
msg: update.clone()
2854
2854
};
2855
2855
2856
2856
let per_peer_state = self.per_peer_state.read().unwrap();
2857
2857
2858
- // Attempt to get the a_peer_state_mutex for the peer we disconnected on.
2859
- let a_peer_state_mutex_opt = per_peer_state.get(peer_node_id).map(|v| v);
2860
-
2861
- // If the particular peer is not present. Select any random peer from the ones we are connected on.
2862
- let a_peer_state_mutex_opt = a_peer_state_mutex_opt.or_else(|| per_peer_state.iter().next().map(|(_, v)| v));
2858
+ // Attempt to get the a_peer_state_mutex for the peer we force-closed on.
2859
+ let a_peer_state_mutex_opt = per_peer_state
2860
+ .get(peer_node_id)
2861
+ .filter(|v| v.lock().unwrap().is_connected)
2862
+ .map(|v| v);
2863
+
2864
+ // If the particular peer is not present, select any random connected peer from the ones we are connected to.
2865
+ let a_peer_state_mutex_opt = a_peer_state_mutex_opt.or_else(|| {
2866
+ per_peer_state
2867
+ .iter()
2868
+ .find(|(_, v)| v.lock().unwrap().is_connected)
2869
+ .map(|(_, v)| v)
2870
+ });
2863
2871
2864
2872
match a_peer_state_mutex_opt {
2865
2873
Some(a_peer_state_mutex) => {
2874
+ // Handle the case where a connected peer is found.
2866
2875
let mut a_peer_state = a_peer_state_mutex.lock().unwrap();
2867
2876
a_peer_state.pending_msg_events.push(brodcast_message_evt);
2868
2877
}
2869
- // If we are connected to no peer.
2870
2878
None => {
2879
+ // Handle the case where no connected peer is found.
2871
2880
let mut pending_broadcast_messages = self.pending_broadcast_messages.lock().unwrap();
2872
2881
pending_broadcast_messages.push(brodcast_message_evt);
2873
2882
log_info!(self.logger, "Not able to broadcast channel_update of force-closed channel right now.
@@ -4944,23 +4953,6 @@ where
4944
4953
{
4945
4954
let per_peer_state = self.per_peer_state.read().unwrap();
4946
4955
4947
- {
4948
- // Get pending messages to be broadcasted.
4949
- let broadcast_evts = self.pending_broadcast_messages.lock().unwrap();
4950
-
4951
- // If we have some pending message to broadcast, and we are connected to peers.
4952
- if broadcast_evts.len() > 0 && per_peer_state.len() > 0 {
4953
- let a_peer_state_mutex = per_peer_state.values().next().unwrap();
4954
- let mut a_peer_state = a_peer_state_mutex.lock().unwrap();
4955
-
4956
- a_peer_state.pending_msg_events.extend(broadcast_evts.iter().cloned());
4957
-
4958
- self.pending_broadcast_messages.lock().unwrap().clear();
4959
-
4960
- should_persist = NotifyOption::DoPersist;
4961
- }
4962
- }
4963
-
4964
4956
for (counterparty_node_id, peer_state_mutex) in per_peer_state.iter() {
4965
4957
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
4966
4958
let peer_state = &mut *peer_state_lock;
@@ -8863,6 +8855,21 @@ where
8863
8855
msg: chan.get_channel_reestablish(&self.logger),
8864
8856
});
8865
8857
});
8858
+
8859
+ {
8860
+ // Get pending messages to be broadcasted.
8861
+ let broadcast_evts = self.pending_broadcast_messages.lock().unwrap();
8862
+
8863
+ // If we have some pending message to broadcast, we will
8864
+ // broadcast them to the peer we just connected with
8865
+ if !broadcast_evts.is_empty() {
8866
+
8867
+ pending_msg_events.extend(broadcast_evts.iter().cloned());
8868
+ self.pending_broadcast_messages.lock().unwrap().clear();
8869
+
8870
+ return NotifyOption::DoPersist;
8871
+ }
8872
+ }
8866
8873
}
8867
8874
8868
8875
return NotifyOption::SkipPersistHandleEvents;
0 commit comments