Skip to content

Commit

Permalink
Add check for is_connected to the broadcast logic when channel is for…
Browse files Browse the repository at this point in the history
…ce-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
  • Loading branch information
shaavan committed Nov 23, 2023
1 parent 00e9496 commit 40bbfeb
Showing 1 changed file with 31 additions and 24 deletions.
55 changes: 31 additions & 24 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2848,26 +2848,35 @@ where
let mut should_persist = NotifyOption::SkipPersistNoEvents;

// Try to send the `BroadcastChannelUpdate` to the peer we just force-closed on, but if
// not try to broadcast it via whatever peer we have.
// not try to broadcast it via whatever peer we are connected to.
let brodcast_message_evt = events::MessageSendEvent::BroadcastChannelUpdate {
msg: update.clone()
};

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

// Attempt to get the a_peer_state_mutex for the peer we disconnected on.
let a_peer_state_mutex_opt = per_peer_state.get(peer_node_id).map(|v| v);

// If the particular peer is not present. Select any random peer from the ones we are connected on.
let a_peer_state_mutex_opt = a_peer_state_mutex_opt.or_else(|| per_peer_state.iter().next().map(|(_, v)| v));
// Attempt to get the a_peer_state_mutex for the peer we force-closed on.
let a_peer_state_mutex_opt = per_peer_state
.get(peer_node_id)
.filter(|v| v.lock().unwrap().is_connected)
.map(|v| v);

// If the particular peer is not present, select any random connected peer from the ones we are connected to.
let a_peer_state_mutex_opt = a_peer_state_mutex_opt.or_else(|| {
per_peer_state
.iter()
.find(|(_, v)| v.lock().unwrap().is_connected)
.map(|(_, v)| v)
});

match a_peer_state_mutex_opt {
Some(a_peer_state_mutex) => {
// Handle the case where a connected peer is found.
let mut a_peer_state = a_peer_state_mutex.lock().unwrap();
a_peer_state.pending_msg_events.push(brodcast_message_evt);
}
// If we are connected to no peer.
None => {
// Handle the case where no connected peer is found.
let mut pending_broadcast_messages = self.pending_broadcast_messages.lock().unwrap();
pending_broadcast_messages.push(brodcast_message_evt);
log_info!(self.logger, "Not able to broadcast channel_update of force-closed channel right now.
Expand Down Expand Up @@ -4944,23 +4953,6 @@ where
{
let per_peer_state = self.per_peer_state.read().unwrap();

{
// Get pending messages to be broadcasted.
let broadcast_evts = self.pending_broadcast_messages.lock().unwrap();

// If we have some pending message to broadcast, and we are connected to peers.
if broadcast_evts.len() > 0 && per_peer_state.len() > 0 {
let a_peer_state_mutex = per_peer_state.values().next().unwrap();
let mut a_peer_state = a_peer_state_mutex.lock().unwrap();

a_peer_state.pending_msg_events.extend(broadcast_evts.iter().cloned());

self.pending_broadcast_messages.lock().unwrap().clear();

should_persist = NotifyOption::DoPersist;
}
}

for (counterparty_node_id, peer_state_mutex) in per_peer_state.iter() {
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
let peer_state = &mut *peer_state_lock;
Expand Down Expand Up @@ -8863,6 +8855,21 @@ where
msg: chan.get_channel_reestablish(&self.logger),
});
});

{
// Get pending messages to be broadcasted.
let broadcast_evts = self.pending_broadcast_messages.lock().unwrap();

// If we have some pending message to broadcast, we will
// broadcast them to the peer we just connected with
if !broadcast_evts.is_empty() {

pending_msg_events.extend(broadcast_evts.iter().cloned());
self.pending_broadcast_messages.lock().unwrap().clear();

return NotifyOption::DoPersist;
}
}
}

return NotifyOption::SkipPersistHandleEvents;
Expand Down

0 comments on commit 40bbfeb

Please sign in to comment.