Skip to content

Commit deed803

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 4bbe381 commit deed803

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

lightning/src/ln/peer_handler.rs

Lines changed: 19 additions & 9 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.handshake_complete() { 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.handshake_complete() { 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.handshake_complete()
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.handshake_complete()
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.handshake_complete()
499502
&& self.msgs_sent_since_pong < BUFFER_DRAIN_MSGS_PER_TICK
500503
}
501504

@@ -777,8 +780,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
777780
let peers = self.peers.read().unwrap();
778781
peers.values().filter_map(|peer_mutex| {
779782
let p = peer_mutex.lock().unwrap();
780-
if !p.channel_encryptor.is_ready_for_encryption() || p.their_features.is_none() ||
781-
p.their_node_id.is_none() {
783+
if !p.handshake_complete() {
782784
return None;
783785
}
784786
Some((p.their_node_id.unwrap().0, p.their_net_address.clone()))
@@ -1537,10 +1539,12 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
15371539

15381540
for (_, peer_mutex) in peers.iter() {
15391541
let mut peer = peer_mutex.lock().unwrap();
1540-
if !peer.channel_encryptor.is_ready_for_encryption() || peer.their_features.is_none() ||
1542+
if !peer.handshake_complete() ||
15411543
!peer.should_forward_channel_announcement(msg.contents.short_channel_id) {
15421544
continue
15431545
}
1546+
debug_assert!(peer.their_node_id.is_some());
1547+
debug_assert!(peer.channel_encryptor.is_ready_for_encryption());
15441548
if peer.buffer_full_drop_gossip_broadcast() {
15451549
log_gossip!(self.logger, "Skipping broadcast message to {:?} as its outbound buffer is full", peer.their_node_id);
15461550
continue;
@@ -1562,10 +1566,12 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
15621566

15631567
for (_, peer_mutex) in peers.iter() {
15641568
let mut peer = peer_mutex.lock().unwrap();
1565-
if !peer.channel_encryptor.is_ready_for_encryption() || peer.their_features.is_none() ||
1569+
if !peer.handshake_complete() ||
15661570
!peer.should_forward_node_announcement(msg.contents.node_id) {
15671571
continue
15681572
}
1573+
debug_assert!(peer.their_node_id.is_some());
1574+
debug_assert!(peer.channel_encryptor.is_ready_for_encryption());
15691575
if peer.buffer_full_drop_gossip_broadcast() {
15701576
log_gossip!(self.logger, "Skipping broadcast message to {:?} as its outbound buffer is full", peer.their_node_id);
15711577
continue;
@@ -1587,10 +1593,12 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
15871593

15881594
for (_, peer_mutex) in peers.iter() {
15891595
let mut peer = peer_mutex.lock().unwrap();
1590-
if !peer.channel_encryptor.is_ready_for_encryption() || peer.their_features.is_none() ||
1596+
if !peer.handshake_complete() ||
15911597
!peer.should_forward_channel_announcement(msg.contents.short_channel_id) {
15921598
continue
15931599
}
1600+
debug_assert!(peer.their_node_id.is_some());
1601+
debug_assert!(peer.channel_encryptor.is_ready_for_encryption());
15941602
if peer.buffer_full_drop_gossip_broadcast() {
15951603
log_gossip!(self.logger, "Skipping broadcast message to {:?} as its outbound buffer is full", peer.their_node_id);
15961604
continue;
@@ -1670,7 +1678,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
16701678
Some(descriptor) => match peers.get(&descriptor) {
16711679
Some(peer_mutex) => {
16721680
let peer_lock = peer_mutex.lock().unwrap();
1673-
if peer_lock.their_features.is_none() {
1681+
if !peer_lock.handshake_complete() {
16741682
continue;
16751683
}
16761684
peer_lock
@@ -2024,7 +2032,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
20242032
let mut peer = peer_mutex.lock().unwrap();
20252033
if flush_read_disabled { peer.received_channel_announce_since_backlogged = false; }
20262034

2027-
if !peer.channel_encryptor.is_ready_for_encryption() || peer.their_node_id.is_none() {
2035+
if !peer.handshake_complete() {
20282036
// The peer needs to complete its handshake before we can exchange messages. We
20292037
// give peers one timer tick to complete handshake, reusing
20302038
// `awaiting_pong_timer_tick_intervals` to track number of timer ticks taken
@@ -2036,6 +2044,8 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
20362044
}
20372045
continue;
20382046
}
2047+
debug_assert!(peer.channel_encryptor.is_ready_for_encryption());
2048+
debug_assert!(peer.their_node_id.is_some());
20392049

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

0 commit comments

Comments
 (0)