Skip to content

Commit 313e824

Browse files
committed
If needed, rebroadcast channel update of a force-closed channel
- This commit adds the ability to rebroadcast the channel update message for a force closed channel if we fail to broadcast it to any peer at the time of closing of channel. - Also it adds persistance for those message, saving them at the time of their addition or clearance.
1 parent 6120eb9 commit 313e824

File tree

1 file changed

+43
-8
lines changed

1 file changed

+43
-8
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,6 +1363,10 @@ where
13631363

13641364
pending_offers_messages: Mutex<Vec<PendingOnionMessage<OffersMessage>>>,
13651365

1366+
/// Tracks the channel_update message that were not broadcasted because
1367+
/// we were not connected to any peers.
1368+
pending_broadcast_messages: Mutex<Vec<MessageSendEvent>>,
1369+
13661370
entropy_source: ES,
13671371
node_signer: NS,
13681372
signer_provider: SP,
@@ -2441,6 +2445,7 @@ where
24412445
funding_batch_states: Mutex::new(BTreeMap::new()),
24422446

24432447
pending_offers_messages: Mutex::new(Vec::new()),
2448+
pending_broadcast_messages: Mutex::new(Vec::new()),
24442449

24452450
entropy_source,
24462451
node_signer,
@@ -2936,15 +2941,30 @@ where
29362941
};
29372942
if let Some(update) = update_opt {
29382943
// Try to send the `BroadcastChannelUpdate` to the peer we just force-closed on, but if
2939-
// not try to broadcast it via whatever peer we have.
2944+
// not try to broadcast it via whatever peer we are connected to.
2945+
let brodcast_message_evt = events::MessageSendEvent::BroadcastChannelUpdate {
2946+
msg: update
2947+
};
2948+
29402949
let per_peer_state = self.per_peer_state.read().unwrap();
2941-
let a_peer_state_opt = per_peer_state.get(peer_node_id)
2942-
.ok_or(per_peer_state.values().next());
2943-
if let Ok(a_peer_state_mutex) = a_peer_state_opt {
2944-
let mut a_peer_state = a_peer_state_mutex.lock().unwrap();
2945-
a_peer_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
2946-
msg: update
2947-
});
2950+
2951+
// Attempt to get the peer_state_mutex for the peer we force-closed on (counterparty).
2952+
let peer_state_mutex_opt = per_peer_state.get(peer_node_id);
2953+
2954+
match peer_state_mutex_opt {
2955+
Some(peer_state_mutex) => {
2956+
let mut peer_state = peer_state_mutex.lock().unwrap();
2957+
peer_state.pending_msg_events.push(brodcast_message_evt);
2958+
}
2959+
None => {
2960+
// If we could not find the couterparty in our per_peer_state, we poll
2961+
// the messages together in pending_broadcast_messages, and broadcast
2962+
// them later.
2963+
let mut pending_broadcast_messages = self.pending_broadcast_messages.lock().unwrap();
2964+
pending_broadcast_messages.push(brodcast_message_evt);
2965+
log_info!(self.logger, "Not able to broadcast channel_update of force-closed channel right now.
2966+
Will try rebroadcasting later.");
2967+
}
29482968
}
29492969
}
29502970

@@ -4859,6 +4879,7 @@ where
48594879

48604880
{
48614881
let per_peer_state = self.per_peer_state.read().unwrap();
4882+
48624883
for (counterparty_node_id, peer_state_mutex) in per_peer_state.iter() {
48634884
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
48644885
let peer_state = &mut *peer_state_lock;
@@ -8056,6 +8077,8 @@ where
80568077
pending_events.append(&mut peer_state.pending_msg_events);
80578078
}
80588079
}
8080+
let mut broadcast_msgs = self.pending_broadcast_messages.lock().unwrap();
8081+
pending_events.append(&mut broadcast_msgs);
80598082

80608083
if !pending_events.is_empty() {
80618084
events.replace(pending_events);
@@ -8881,6 +8904,16 @@ where
88818904
msg: chan.get_channel_reestablish(&&logger),
88828905
});
88838906
});
8907+
8908+
{
8909+
// Get pending messages to be broadcasted.
8910+
let mut broadcast_msgs = self.pending_broadcast_messages.lock().unwrap();
8911+
8912+
// If we have some pending message to broadcast, and we are connected to peers.
8913+
if broadcast_msgs.len() > 0 {
8914+
pending_msg_events.append(&mut broadcast_msgs);
8915+
}
8916+
}
88848917
}
88858918

88868919
return NotifyOption::SkipPersistHandleEvents;
@@ -10894,6 +10927,8 @@ where
1089410927

1089510928
pending_offers_messages: Mutex::new(Vec::new()),
1089610929

10930+
pending_broadcast_messages: Mutex::new(Vec::new()),
10931+
1089710932
entropy_source: args.entropy_source,
1089810933
node_signer: args.node_signer,
1089910934
signer_provider: args.signer_provider,

0 commit comments

Comments
 (0)