Skip to content

Commit 4dd6142

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.
1 parent bd70d56 commit 4dd6142

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
@@ -436,6 +436,7 @@ impl Peer {
436436
/// point and we shouldn't send it yet to avoid sending duplicate updates. If we've already
437437
/// sent the old versions, we should send the update, and so return true here.
438438
fn should_forward_channel_announcement(&self, channel_id: u64) -> bool {
439+
if self.their_features.is_none() { return false; }
439440
if self.their_features.as_ref().unwrap().supports_gossip_queries() &&
440441
!self.sent_gossip_timestamp_filter {
441442
return false;
@@ -449,6 +450,7 @@ impl Peer {
449450

450451
/// Similar to the above, but for node announcements indexed by node_id.
451452
fn should_forward_node_announcement(&self, node_id: NodeId) -> bool {
453+
if self.their_features.is_none() { return false; }
452454
if self.their_features.as_ref().unwrap().supports_gossip_queries() &&
453455
!self.sent_gossip_timestamp_filter {
454456
return false;
@@ -475,19 +477,20 @@ impl Peer {
475477
fn should_buffer_gossip_backfill(&self) -> bool {
476478
self.pending_outbound_buffer.is_empty() && self.gossip_broadcast_buffer.is_empty()
477479
&& self.msgs_sent_since_pong < BUFFER_DRAIN_MSGS_PER_TICK
480+
&& self.their_features.is_some()
478481
}
479482

480483
/// Determines if we should push an onion message onto a peer's outbound buffer. This is checked
481484
/// every time the peer's buffer may have been drained.
482485
fn should_buffer_onion_message(&self) -> bool {
483-
self.pending_outbound_buffer.is_empty()
486+
self.pending_outbound_buffer.is_empty() && self.their_features.is_some()
484487
&& self.msgs_sent_since_pong < BUFFER_DRAIN_MSGS_PER_TICK
485488
}
486489

487490
/// Determines if we should push additional gossip broadcast messages onto a peer's outbound
488491
/// buffer. This is checked every time the peer's buffer may have been drained.
489492
fn should_buffer_gossip_broadcast(&self) -> bool {
490-
self.pending_outbound_buffer.is_empty()
493+
self.pending_outbound_buffer.is_empty() && self.their_features.is_some()
491494
&& self.msgs_sent_since_pong < BUFFER_DRAIN_MSGS_PER_TICK
492495
}
493496

@@ -1529,10 +1532,12 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
15291532

15301533
for (_, peer_mutex) in peers.iter() {
15311534
let mut peer = peer_mutex.lock().unwrap();
1532-
if !peer.channel_encryptor.is_ready_for_encryption() || peer.their_features.is_none() ||
1535+
if peer.their_features.is_none() ||
15331536
!peer.should_forward_channel_announcement(msg.contents.short_channel_id) {
15341537
continue
15351538
}
1539+
debug_assert!(peer.their_node_id.is_some());
1540+
debug_assert!(peer.channel_encryptor.is_ready_for_encryption());
15361541
if peer.buffer_full_drop_gossip_broadcast() {
15371542
log_gossip!(self.logger, "Skipping broadcast message to {:?} as its outbound buffer is full", peer.their_node_id);
15381543
continue;
@@ -1554,10 +1559,12 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
15541559

15551560
for (_, peer_mutex) in peers.iter() {
15561561
let mut peer = peer_mutex.lock().unwrap();
1557-
if !peer.channel_encryptor.is_ready_for_encryption() || peer.their_features.is_none() ||
1562+
if peer.their_features.is_none() ||
15581563
!peer.should_forward_node_announcement(msg.contents.node_id) {
15591564
continue
15601565
}
1566+
debug_assert!(peer.their_node_id.is_some());
1567+
debug_assert!(peer.channel_encryptor.is_ready_for_encryption());
15611568
if peer.buffer_full_drop_gossip_broadcast() {
15621569
log_gossip!(self.logger, "Skipping broadcast message to {:?} as its outbound buffer is full", peer.their_node_id);
15631570
continue;
@@ -1579,10 +1586,12 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
15791586

15801587
for (_, peer_mutex) in peers.iter() {
15811588
let mut peer = peer_mutex.lock().unwrap();
1582-
if !peer.channel_encryptor.is_ready_for_encryption() || peer.their_features.is_none() ||
1589+
if peer.their_features.is_none() ||
15831590
!peer.should_forward_channel_announcement(msg.contents.short_channel_id) {
15841591
continue
15851592
}
1593+
debug_assert!(peer.their_node_id.is_some());
1594+
debug_assert!(peer.channel_encryptor.is_ready_for_encryption());
15861595
if peer.buffer_full_drop_gossip_broadcast() {
15871596
log_gossip!(self.logger, "Skipping broadcast message to {:?} as its outbound buffer is full", peer.their_node_id);
15881597
continue;
@@ -2016,7 +2025,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
20162025
let mut peer = peer_mutex.lock().unwrap();
20172026
if flush_read_disabled { peer.received_channel_announce_since_backlogged = false; }
20182027

2019-
if !peer.channel_encryptor.is_ready_for_encryption() || peer.their_node_id.is_none() {
2028+
if peer.their_features.is_none() {
20202029
// The peer needs to complete its handshake before we can exchange messages. We
20212030
// give peers one timer tick to complete handshake, reusing
20222031
// `awaiting_pong_timer_tick_intervals` to track number of timer ticks taken
@@ -2028,6 +2037,8 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
20282037
}
20292038
continue;
20302039
}
2040+
debug_assert!(peer.channel_encryptor.is_ready_for_encryption());
2041+
debug_assert!(peer.their_node_id.is_some());
20312042

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

0 commit comments

Comments
 (0)