Skip to content

Commit 0db23b9

Browse files
committed
Correct the "is peer live" checks in PeerManager
In general, we should be checking if a `Peer` has `their_features` set as the "is this peer connected and have they finished the handshake" flag as it indicates an `Init` message was received. While none of these appear to be reachable bugs, there were a number of places where we checked other flags for this purpose, which may lead to sending messages before `Init` in the future. Here we clean these cases up to always use the correct check (via the new util method).
1 parent b59379e commit 0db23b9

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

lightning/src/ln/peer_handler.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ impl Peer {
444444
/// point and we shouldn't send it yet to avoid sending duplicate updates. If we've already
445445
/// sent the old versions, we should send the update, and so return true here.
446446
fn should_forward_channel_announcement(&self, channel_id: u64) -> bool {
447+
if self.their_features.is_none() { return false; }
447448
if self.their_features.as_ref().unwrap().supports_gossip_queries() &&
448449
!self.sent_gossip_timestamp_filter {
449450
return false;
@@ -457,6 +458,7 @@ impl Peer {
457458

458459
/// Similar to the above, but for node announcements indexed by node_id.
459460
fn should_forward_node_announcement(&self, node_id: NodeId) -> bool {
461+
if self.their_features.is_none() { return false; }
460462
if self.their_features.as_ref().unwrap().supports_gossip_queries() &&
461463
!self.sent_gossip_timestamp_filter {
462464
return false;
@@ -483,19 +485,20 @@ impl Peer {
483485
fn should_buffer_gossip_backfill(&self) -> bool {
484486
self.pending_outbound_buffer.is_empty() && self.gossip_broadcast_buffer.is_empty()
485487
&& self.msgs_sent_since_pong < BUFFER_DRAIN_MSGS_PER_TICK
488+
&& self.their_features.is_some()
486489
}
487490

488491
/// Determines if we should push an onion message onto a peer's outbound buffer. This is checked
489492
/// every time the peer's buffer may have been drained.
490493
fn should_buffer_onion_message(&self) -> bool {
491-
self.pending_outbound_buffer.is_empty()
494+
self.pending_outbound_buffer.is_empty() && self.their_features.is_some()
492495
&& self.msgs_sent_since_pong < BUFFER_DRAIN_MSGS_PER_TICK
493496
}
494497

495498
/// Determines if we should push additional gossip broadcast messages onto a peer's outbound
496499
/// buffer. This is checked every time the peer's buffer may have been drained.
497500
fn should_buffer_gossip_broadcast(&self) -> bool {
498-
self.pending_outbound_buffer.is_empty()
501+
self.pending_outbound_buffer.is_empty() && self.their_features.is_some()
499502
&& self.msgs_sent_since_pong < BUFFER_DRAIN_MSGS_PER_TICK
500503
}
501504

@@ -1537,10 +1540,12 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
15371540

15381541
for (_, peer_mutex) in peers.iter() {
15391542
let mut peer = peer_mutex.lock().unwrap();
1540-
if !peer.channel_encryptor.is_ready_for_encryption() || peer.their_features.is_none() ||
1543+
if peer.their_features.is_none() ||
15411544
!peer.should_forward_channel_announcement(msg.contents.short_channel_id) {
15421545
continue
15431546
}
1547+
debug_assert!(peer.their_node_id.is_some());
1548+
debug_assert!(peer.channel_encryptor.is_ready_for_encryption());
15441549
if peer.buffer_full_drop_gossip_broadcast() {
15451550
log_gossip!(self.logger, "Skipping broadcast message to {:?} as its outbound buffer is full", peer.their_node_id);
15461551
continue;
@@ -1562,10 +1567,12 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
15621567

15631568
for (_, peer_mutex) in peers.iter() {
15641569
let mut peer = peer_mutex.lock().unwrap();
1565-
if !peer.channel_encryptor.is_ready_for_encryption() || peer.their_features.is_none() ||
1570+
if peer.their_features.is_none() ||
15661571
!peer.should_forward_node_announcement(msg.contents.node_id) {
15671572
continue
15681573
}
1574+
debug_assert!(peer.their_node_id.is_some());
1575+
debug_assert!(peer.channel_encryptor.is_ready_for_encryption());
15691576
if peer.buffer_full_drop_gossip_broadcast() {
15701577
log_gossip!(self.logger, "Skipping broadcast message to {:?} as its outbound buffer is full", peer.their_node_id);
15711578
continue;
@@ -1587,10 +1594,12 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
15871594

15881595
for (_, peer_mutex) in peers.iter() {
15891596
let mut peer = peer_mutex.lock().unwrap();
1590-
if !peer.channel_encryptor.is_ready_for_encryption() || peer.their_features.is_none() ||
1597+
if peer.their_features.is_none() ||
15911598
!peer.should_forward_channel_announcement(msg.contents.short_channel_id) {
15921599
continue
15931600
}
1601+
debug_assert!(peer.their_node_id.is_some());
1602+
debug_assert!(peer.channel_encryptor.is_ready_for_encryption());
15941603
if peer.buffer_full_drop_gossip_broadcast() {
15951604
log_gossip!(self.logger, "Skipping broadcast message to {:?} as its outbound buffer is full", peer.their_node_id);
15961605
continue;
@@ -2024,7 +2033,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
20242033
let mut peer = peer_mutex.lock().unwrap();
20252034
if flush_read_disabled { peer.received_channel_announce_since_backlogged = false; }
20262035

2027-
if !peer.channel_encryptor.is_ready_for_encryption() || peer.their_node_id.is_none() {
2036+
if peer.their_features.is_none() {
20282037
// The peer needs to complete its handshake before we can exchange messages. We
20292038
// give peers one timer tick to complete handshake, reusing
20302039
// `awaiting_pong_timer_tick_intervals` to track number of timer ticks taken
@@ -2036,6 +2045,8 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
20362045
}
20372046
continue;
20382047
}
2048+
debug_assert!(peer.channel_encryptor.is_ready_for_encryption());
2049+
debug_assert!(peer.their_node_id.is_some());
20392050

20402051
loop { // Used as a `goto` to skip writing a Ping message.
20412052
if peer.awaiting_pong_timer_tick_intervals == -1 {

0 commit comments

Comments
 (0)