Skip to content

Commit feb7420

Browse files
committed
Cache and Broadcast channel update later
- Cache the force-close channel update message, and broadcast them later. - Update get_and_clear_pending_msg_events to broadcast the pending_broadcast_events along with other messages.
1 parent 40574d0 commit feb7420

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

lightning/src/ln/channelmanager.rs

+16-11
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,10 @@ where
13811381

13821382
pending_offers_messages: Mutex<Vec<PendingOnionMessage<OffersMessage>>>,
13831383

1384+
/// Tracks the channel_update message that were not broadcasted because
1385+
/// we were not connected to any peers.
1386+
pending_broadcast_messages: Mutex<Vec<MessageSendEvent>>,
1387+
13841388
entropy_source: ES,
13851389
node_signer: NS,
13861390
signer_provider: SP,
@@ -2455,6 +2459,7 @@ where
24552459
funding_batch_states: Mutex::new(BTreeMap::new()),
24562460

24572461
pending_offers_messages: Mutex::new(Vec::new()),
2462+
pending_broadcast_messages: Mutex::new(Vec::new()),
24582463

24592464
entropy_source,
24602465
node_signer,
@@ -2946,17 +2951,12 @@ where
29462951
}
29472952
};
29482953
if let Some(update) = update_opt {
2949-
// Try to send the `BroadcastChannelUpdate` to the peer we just force-closed on, but if
2950-
// not try to broadcast it via whatever peer we have.
2951-
let per_peer_state = self.per_peer_state.read().unwrap();
2952-
let a_peer_state_opt = per_peer_state.get(peer_node_id)
2953-
.ok_or(per_peer_state.values().next());
2954-
if let Ok(a_peer_state_mutex) = a_peer_state_opt {
2955-
let mut a_peer_state = a_peer_state_mutex.lock().unwrap();
2956-
a_peer_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
2957-
msg: update
2958-
});
2959-
}
2954+
// If we have some Channel Update to broadcast, we cache it and broadcast it later.
2955+
let mut pending_broadcast_messages = self.pending_broadcast_messages.lock().unwrap();
2956+
pending_broadcast_messages.push(events::MessageSendEvent::BroadcastChannelUpdate {
2957+
msg: update
2958+
});
2959+
log_info!(self.logger, "Caching the channel_updates of force-closed channel. We will them broadcast later.");
29602960
}
29612961

29622962
Ok(counterparty_node_id)
@@ -4915,6 +4915,7 @@ where
49154915

49164916
{
49174917
let per_peer_state = self.per_peer_state.read().unwrap();
4918+
49184919
for (counterparty_node_id, peer_state_mutex) in per_peer_state.iter() {
49194920
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
49204921
let peer_state = &mut *peer_state_lock;
@@ -8203,6 +8204,8 @@ where
82038204
pending_events.append(&mut peer_state.pending_msg_events);
82048205
}
82058206
}
8207+
let mut broadcast_msgs = self.pending_broadcast_messages.lock().unwrap();
8208+
pending_events.append(&mut broadcast_msgs);
82068209

82078210
if !pending_events.is_empty() {
82088211
events.replace(pending_events);
@@ -11099,6 +11102,8 @@ where
1109911102

1110011103
pending_offers_messages: Mutex::new(Vec::new()),
1110111104

11105+
pending_broadcast_messages: Mutex::new(Vec::new()),
11106+
1110211107
entropy_source: args.entropy_source,
1110311108
node_signer: args.node_signer,
1110411109
signer_provider: args.signer_provider,

0 commit comments

Comments
 (0)