X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Fpeer_handler.rs;h=9cbd02121e9e55231a58b7b4b3c935a9b0b07a23;hb=a8114a70cb793d85402ba94a2825899638023512;hp=8aff2fc5608917f6b63f4b21542c75f5c4401e2f;hpb=bff9982299921751edaaa208824890036aa12900;p=rust-lightning diff --git a/lightning/src/ln/peer_handler.rs b/lightning/src/ln/peer_handler.rs index 8aff2fc5..9cbd0212 100644 --- a/lightning/src/ln/peer_handler.rs +++ b/lightning/src/ln/peer_handler.rs @@ -133,13 +133,22 @@ impl Peer { /// announcements/updates for the given channel_id then we will send it when we get to that /// point and we shouldn't send it yet to avoid sending duplicate updates. If we've already /// sent the old versions, we should send the update, and so return true here. - fn should_forward_channel(&self, channel_id: u64)->bool{ + fn should_forward_channel_announcement(&self, channel_id: u64)->bool{ match self.sync_status { InitSyncTracker::NoSyncRequested => true, InitSyncTracker::ChannelsSyncing(i) => i < channel_id, InitSyncTracker::NodesSyncing(_) => true, } } + + /// Similar to the above, but for node announcements indexed by node_id. + fn should_forward_node_announcement(&self, node_id: PublicKey) -> bool { + match self.sync_status { + InitSyncTracker::NoSyncRequested => true, + InitSyncTracker::ChannelsSyncing(_) => false, + InitSyncTracker::NodesSyncing(pk) => pk < node_id, + } + } } struct PeerHolder { @@ -162,7 +171,7 @@ fn _check_usize_is_32_or_64() { /// lifetimes). Other times you can afford a reference, which is more efficient, in which case /// SimpleRefPeerManager is the more appropriate type. Defining these type aliases prevents /// issues such as overly long function definitions. -pub type SimpleArcPeerManager = Arc>>; +pub type SimpleArcPeerManager = Arc>>; /// SimpleRefPeerManager is a type alias for a PeerManager reference, and is the reference /// counterpart to the SimpleArcPeerManager type alias. Use this type by default when you don't @@ -170,7 +179,7 @@ pub type SimpleArcPeerManager = Arc = PeerManager>; +pub type SimpleRefPeerManager<'a, 'b, 'c, 'd, SD, M, T, F> = PeerManager>; /// A PeerManager manages a set of peers, described by their SocketDescriptor and marshalls socket /// events into messages which it passes on to its MessageHandlers. @@ -586,10 +595,6 @@ impl PeerManager where log_debug!(self, "Deserialization failed due to shortness of message"); return Err(PeerHandleError { no_connection_possible: false }); } - msgs::DecodeError::ExtraAddressesPerType => { - log_debug!(self, "Error decoding message, ignoring due to lnd spec incompatibility. See https://github.com/lightningnetwork/lnd/issues/1407"); - continue; - } msgs::DecodeError::BadLengthDescriptor => return Err(PeerHandleError { no_connection_possible: false }), msgs::DecodeError::Io(_) => return Err(PeerHandleError { no_connection_possible: false }), } @@ -755,10 +760,13 @@ impl PeerManager where // Unknown messages: wire::Message::Unknown(msg_type) if msg_type.is_even() => { + log_debug!(self, "Received unknown even message of type {}, disconnecting peer!", msg_type); // Fail the channel if message is an even, unknown type as per BOLT #1. return Err(PeerHandleError{ no_connection_possible: true }); }, - wire::Message::Unknown(_) => {}, + wire::Message::Unknown(msg_type) => { + log_trace!(self, "Received unknown odd message of type {}, ignoring", msg_type); + }, } } } @@ -955,7 +963,7 @@ impl PeerManager where for (ref descriptor, ref mut peer) in peers.peers.iter_mut() { if !peer.channel_encryptor.is_ready_for_encryption() || peer.their_features.is_none() || - !peer.should_forward_channel(msg.contents.short_channel_id) { + !peer.should_forward_channel_announcement(msg.contents.short_channel_id) { continue } match peer.their_node_id { @@ -972,6 +980,21 @@ impl PeerManager where } } }, + MessageSendEvent::BroadcastNodeAnnouncement { ref msg } => { + log_trace!(self, "Handling BroadcastNodeAnnouncement event in peer_handler"); + if self.message_handler.route_handler.handle_node_announcement(msg).is_ok() { + let encoded_msg = encode_msg!(msg); + + for (ref descriptor, ref mut peer) in peers.peers.iter_mut() { + if !peer.channel_encryptor.is_ready_for_encryption() || peer.their_features.is_none() || + !peer.should_forward_node_announcement(msg.contents.node_id) { + continue + } + peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encoded_msg[..])); + self.do_attempt_write_data(&mut (*descriptor).clone(), peer); + } + } + }, MessageSendEvent::BroadcastChannelUpdate { ref msg } => { log_trace!(self, "Handling BroadcastChannelUpdate event in peer_handler for short channel id {}", msg.contents.short_channel_id); if self.message_handler.route_handler.handle_channel_update(msg).is_ok() { @@ -979,7 +1002,7 @@ impl PeerManager where for (ref descriptor, ref mut peer) in peers.peers.iter_mut() { if !peer.channel_encryptor.is_ready_for_encryption() || peer.their_features.is_none() || - !peer.should_forward_channel(msg.contents.short_channel_id) { + !peer.should_forward_channel_announcement(msg.contents.short_channel_id) { continue } peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encoded_msg[..])); @@ -1086,10 +1109,15 @@ impl PeerManager where descriptors_needing_disconnect.push(descriptor.clone()); match peer.their_node_id { Some(node_id) => { + log_trace!(self, "Disconnecting peer with id {} due to ping timeout", node_id); node_id_to_descriptor.remove(&node_id); - self.message_handler.chan_handler.peer_disconnected(&node_id, true); + self.message_handler.chan_handler.peer_disconnected(&node_id, false); } - None => {} + None => { + // This can't actually happen as we should have hit + // is_ready_for_encryption() previously on this same peer. + unreachable!(); + }, } return false; }