Merge pull request #513 from ariard/2020-02-fix-zero-msat-htlc
[rust-lightning] / lightning / src / ln / peer_handler.rs
index ca6b33a778e0c5a046e6ba47a3987981e4b67111..12cf937bc0f07785fbb58d93db58355dbbf0dce9 100644 (file)
@@ -47,14 +47,15 @@ pub struct MessageHandler<CM: Deref> where CM::Target: msgs::ChannelMessageHandl
 /// For efficiency, Clone should be relatively cheap for this type.
 ///
 /// You probably want to just extend an int and put a file descriptor in a struct and implement
-/// send_data. Note that if you are using a higher-level net library that may close() itself, be
-/// careful to ensure you don't have races whereby you might register a new connection with an fd
-/// the same as a yet-to-be-socket_disconnected()-ed.
+/// send_data. Note that if you are using a higher-level net library that may call close() itself,
+/// be careful to ensure you don't have races whereby you might register a new connection with an
+/// fd which is the same as a previous one which has yet to be removed via
+/// PeerManager::socket_disconnected().
 pub trait SocketDescriptor : cmp::Eq + hash::Hash + Clone {
        /// Attempts to send some data from the given slice to the peer.
        ///
        /// Returns the amount of data which was sent, possibly 0 if the socket has since disconnected.
-       /// Note that in the disconnected case, socket_disconnected must still fire and further write
+       /// Note that in the disconnected case, socket_disconnected must still fire and further write
        /// attempts may occur until that time.
        ///
        /// If the returned size is smaller than data.len(), a write_available event must
@@ -69,16 +70,16 @@ pub trait SocketDescriptor : cmp::Eq + hash::Hash + Clone {
        /// Disconnect the socket pointed to by this SocketDescriptor. Once this function returns, no
        /// more calls to write_buffer_space_avail, read_event or socket_disconnected may be made with
        /// this descriptor. No socket_disconnected call should be generated as a result of this call,
-       /// though obviously races may occur whereby disconnect_socket is called after a call to
-       /// socket_disconnected but prior to that event completing.
+       /// though races may occur whereby disconnect_socket is called after a call to
+       /// socket_disconnected but prior to socket_disconnected returning.
        fn disconnect_socket(&mut self);
 }
 
 /// Error for PeerManager errors. If you get one of these, you must disconnect the socket and
 /// generate no further read_event/write_buffer_space_avail calls for the descriptor, only
 /// triggering a single socket_disconnected call (unless it was provided in response to a
-/// new_*_connection event, in which case no such socket_disconnected() must be generated and the
-/// socket be silently disconencted).
+/// new_*_connection event, in which case no such socket_disconnected() must be called and the
+/// socket silently disconencted).
 pub struct PeerHandleError {
        /// Used to indicate that we probably can't make any future connections to this peer, implying
        /// we should go ahead and force-close any channels we have with it.
@@ -132,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<Descriptor: SocketDescriptor> {
@@ -161,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<SD, M> = Arc<PeerManager<SD, SimpleArcChannelManager<M>>>;
+pub type SimpleArcPeerManager<SD, M, T, F> = Arc<PeerManager<SD, SimpleArcChannelManager<M, T, F>>>;
 
 /// 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
@@ -169,7 +179,7 @@ pub type SimpleArcPeerManager<SD, M> = Arc<PeerManager<SD, SimpleArcChannelManag
 /// usage of lightning-net-tokio (since tokio::spawn requires parameters with static lifetimes).
 /// But if this is not necessary, using a reference is more efficient. Defining these type aliases
 /// helps with issues such as long function definitions.
-pub type SimpleRefPeerManager<'a, SD, M> = PeerManager<SD, SimpleRefChannelManager<'a, M>>;
+pub type SimpleRefPeerManager<'a, 'b, 'c, 'd, SD, M, T, F> = PeerManager<SD, SimpleRefChannelManager<'a, 'b, 'c, 'd, M, T, F>>;
 
 /// A PeerManager manages a set of peers, described by their SocketDescriptor and marshalls socket
 /// events into messages which it passes on to its MessageHandlers.
@@ -435,7 +445,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
        /// on this file descriptor has resume_read set (preventing DoS issues in the send buffer).
        ///
        /// Panics if the descriptor was not previously registered in a new_*_connection event.
-       pub fn read_event(&self, peer_descriptor: &mut Descriptor, data: Vec<u8>) -> Result<bool, PeerHandleError> {
+       pub fn read_event(&self, peer_descriptor: &mut Descriptor, data: &[u8]) -> Result<bool, PeerHandleError> {
                match self.do_read_event(peer_descriptor, data) {
                        Ok(res) => Ok(res),
                        Err(e) => {
@@ -445,7 +455,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
                }
        }
 
-       fn do_read_event(&self, peer_descriptor: &mut Descriptor, data: Vec<u8>) -> Result<bool, PeerHandleError> {
+       fn do_read_event(&self, peer_descriptor: &mut Descriptor, data: &[u8]) -> Result<bool, PeerHandleError> {
                let pause_read = {
                        let mut peers_lock = self.peers.lock().unwrap();
                        let peers = &mut *peers_lock;
@@ -585,10 +595,6 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> 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 }),
                                                                                                }
@@ -754,10 +760,13 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> 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);
+                                                                                       },
                                                                                }
                                                                        }
                                                                }
@@ -954,7 +963,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> 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 {
@@ -971,6 +980,21 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> 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() {
@@ -978,7 +1002,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> 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[..]));
@@ -1085,10 +1109,15 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> 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;
                                }
@@ -1199,9 +1228,9 @@ mod tests {
                let mut fd_b = FileDescriptor { fd: 1, outbound_data: Arc::new(Mutex::new(Vec::new())) };
                let initial_data = peer_b.new_outbound_connection(a_id, fd_b.clone()).unwrap();
                peer_a.new_inbound_connection(fd_a.clone()).unwrap();
-               assert_eq!(peer_a.read_event(&mut fd_a, initial_data).unwrap(), false);
-               assert_eq!(peer_b.read_event(&mut fd_b, fd_a.outbound_data.lock().unwrap().split_off(0)).unwrap(), false);
-               assert_eq!(peer_a.read_event(&mut fd_a, fd_b.outbound_data.lock().unwrap().split_off(0)).unwrap(), false);
+               assert_eq!(peer_a.read_event(&mut fd_a, &initial_data).unwrap(), false);
+               assert_eq!(peer_b.read_event(&mut fd_b, &fd_a.outbound_data.lock().unwrap().split_off(0)).unwrap(), false);
+               assert_eq!(peer_a.read_event(&mut fd_a, &fd_b.outbound_data.lock().unwrap().split_off(0)).unwrap(), false);
        }
 
        #[test]