Skip to content

Commit 40bbfeb

Browse files
committed
Add check for is_connected to the broadcast logic when channel is force-closed
- Also moved the logic of clearing pending_broadcast_message from timer_tick_occurred to peer_connected, to transfer the pending broadcast message as soon as we connect to a peer, instead of waiting for timer tick to occur
1 parent 00e9496 commit 40bbfeb

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2848,26 +2848,35 @@ where
28482848
let mut should_persist = NotifyOption::SkipPersistNoEvents;
28492849

28502850
// 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.
28522852
let brodcast_message_evt = events::MessageSendEvent::BroadcastChannelUpdate {
28532853
msg: update.clone()
28542854
};
28552855

28562856
let per_peer_state = self.per_peer_state.read().unwrap();
28572857

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+
});
28632871

28642872
match a_peer_state_mutex_opt {
28652873
Some(a_peer_state_mutex) => {
2874+
// Handle the case where a connected peer is found.
28662875
let mut a_peer_state = a_peer_state_mutex.lock().unwrap();
28672876
a_peer_state.pending_msg_events.push(brodcast_message_evt);
28682877
}
2869-
// If we are connected to no peer.
28702878
None => {
2879+
// Handle the case where no connected peer is found.
28712880
let mut pending_broadcast_messages = self.pending_broadcast_messages.lock().unwrap();
28722881
pending_broadcast_messages.push(brodcast_message_evt);
28732882
log_info!(self.logger, "Not able to broadcast channel_update of force-closed channel right now.
@@ -4944,23 +4953,6 @@ where
49444953
{
49454954
let per_peer_state = self.per_peer_state.read().unwrap();
49464955

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-
49644956
for (counterparty_node_id, peer_state_mutex) in per_peer_state.iter() {
49654957
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
49664958
let peer_state = &mut *peer_state_lock;
@@ -8863,6 +8855,21 @@ where
88638855
msg: chan.get_channel_reestablish(&self.logger),
88648856
});
88658857
});
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+
}
88668873
}
88678874

88688875
return NotifyOption::SkipPersistHandleEvents;

0 commit comments

Comments
 (0)