Support option_upfront_shutdown_script for local peer
[rust-lightning] / src / ln / peer_handler.rs
index 87a2e6d1c242b64f6e9b15fb0e9de9ef7cb8f74e..f544ae237e26b0179ececeae63c4e509c89e4492 100644 (file)
@@ -113,7 +113,7 @@ struct Peer {
 }
 
 impl Peer {
-       /// Returns true if the the channel announcements/updates for the given channel should be
+       /// Returns true if the channel announcements/updates for the given channel should be
        /// forwarded to this peer.
        /// If we are sending our routing table to this peer and we have not yet sent channel
        /// announcements/updates for the given channel_id then we will send it when we get to that
@@ -234,7 +234,7 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
                if peers.peers.insert(descriptor, Peer {
                        channel_encryptor: peer_encryptor,
                        outbound: true,
-                       their_node_id: Some(their_node_id),
+                       their_node_id: None,
                        their_global_features: None,
                        their_local_features: None,
 
@@ -438,9 +438,6 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
 
                                                        macro_rules! try_potential_handleerror {
                                                                ($thing: expr) => {
-                                                                       try_potential_handleerror!($thing, false);
-                                                               };
-                                                               ($thing: expr, $pre_noise: expr) => {
                                                                        match $thing {
                                                                                Ok(x) => x,
                                                                                Err(e) => {
@@ -449,9 +446,6 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
                                                                                                        msgs::ErrorAction::DisconnectPeer { msg: _ } => {
                                                                                                                //TODO: Try to push msg
                                                                                                                log_trace!(self, "Got Err handling message, disconnecting peer because {}", e.err);
-                                                                                                               if $pre_noise {
-                                                                                                                       peer.their_node_id = None; // Unset so that we don't generate a peer_disconnected event
-                                                                                                               }
                                                                                                                return Err(PeerHandleError{ no_connection_possible: false });
                                                                                                        },
                                                                                                        msgs::ErrorAction::IgnoreError => {
@@ -481,11 +475,17 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
                                                                                        match e {
                                                                                                msgs::DecodeError::UnknownVersion => return Err(PeerHandleError{ no_connection_possible: false }),
                                                                                                msgs::DecodeError::UnknownRequiredFeature => {
-                                                                                                       log_debug!(self, "Got a channel/node announcement with an known required feature flag, you may want to udpate!");
+                                                                                                       log_debug!(self, "Got a channel/node announcement with an known required feature flag, you may want to update!");
                                                                                                        continue;
                                                                                                },
-                                                                                               msgs::DecodeError::InvalidValue => return Err(PeerHandleError{ no_connection_possible: false }),
-                                                                                               msgs::DecodeError::ShortRead => return Err(PeerHandleError{ no_connection_possible: false }),
+                                                                                               msgs::DecodeError::InvalidValue => {
+                                                                                                       log_debug!(self, "Got an invalid value while deserializing message");
+                                                                                                       return Err(PeerHandleError{ no_connection_possible: false });
+                                                                                               },
+                                                                                               msgs::DecodeError::ShortRead => {
+                                                                                                       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;
@@ -517,16 +517,17 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
                                                        let next_step = peer.channel_encryptor.get_noise_step();
                                                        match next_step {
                                                                NextNoiseStep::ActOne => {
-                                                                       let act_two = try_potential_handleerror!(peer.channel_encryptor.process_act_one_with_key(&peer.pending_read_buffer[..], &self.our_node_secret), true).to_vec();
+                                                                       let act_two = try_potential_handleerror!(peer.channel_encryptor.process_act_one_with_key(&peer.pending_read_buffer[..], &self.our_node_secret)).to_vec();
                                                                        peer.pending_outbound_buffer.push_back(act_two);
                                                                        peer.pending_read_buffer = [0; 66].to_vec(); // act three is 66 bytes long
                                                                },
                                                                NextNoiseStep::ActTwo => {
-                                                                       let act_three = try_potential_handleerror!(peer.channel_encryptor.process_act_two(&peer.pending_read_buffer[..], &self.our_node_secret), true).to_vec();
-                                                                       peer.pending_outbound_buffer.push_back(act_three);
+                                                                       let (act_three, their_node_id) = try_potential_handleerror!(peer.channel_encryptor.process_act_two(&peer.pending_read_buffer[..], &self.our_node_secret));
+                                                                       peer.pending_outbound_buffer.push_back(act_three.to_vec());
                                                                        peer.pending_read_buffer = [0; 18].to_vec(); // Message length header is 18 bytes
                                                                        peer.pending_read_is_header = true;
 
+                                                                       peer.their_node_id = Some(their_node_id);
                                                                        insert_node_id!();
                                                                        let mut local_features = msgs::LocalFeatures::new();
                                                                        if self.initial_syncs_sent.load(Ordering::Acquire) < INITIAL_SYNCS_TO_SEND {
@@ -539,7 +540,7 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
                                                                        }, 16);
                                                                },
                                                                NextNoiseStep::ActThree => {
-                                                                       let their_node_id = try_potential_handleerror!(peer.channel_encryptor.process_act_three(&peer.pending_read_buffer[..]), true);
+                                                                       let their_node_id = try_potential_handleerror!(peer.channel_encryptor.process_act_three(&peer.pending_read_buffer[..]));
                                                                        peer.pending_read_buffer = [0; 18].to_vec(); // Message length header is 18 bytes
                                                                        peer.pending_read_is_header = true;
                                                                        peer.their_node_id = Some(their_node_id);
@@ -586,10 +587,6 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
                                                                                                        log_info!(self, "Peer local features required data_loss_protect");
                                                                                                        return Err(PeerHandleError{ no_connection_possible: true });
                                                                                                }
-                                                                                               if msg.local_features.requires_upfront_shutdown_script() {
-                                                                                                       log_info!(self, "Peer local features required upfront_shutdown_script");
-                                                                                                       return Err(PeerHandleError{ no_connection_possible: true });
-                                                                                               }
                                                                                                if peer.their_global_features.is_some() {
                                                                                                        return Err(PeerHandleError{ no_connection_possible: false });
                                                                                                }
@@ -658,11 +655,11 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
                                                                                        // Channel control:
                                                                                        32 => {
                                                                                                let msg = try_potential_decodeerror!(msgs::OpenChannel::read(&mut reader));
-                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_open_channel(&peer.their_node_id.unwrap(), &msg));
+                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_open_channel(&peer.their_node_id.unwrap(), peer.their_local_features.clone().unwrap(), &msg));
                                                                                        },
                                                                                        33 => {
                                                                                                let msg = try_potential_decodeerror!(msgs::AcceptChannel::read(&mut reader));
-                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_accept_channel(&peer.their_node_id.unwrap(), &msg));
+                                                                                               try_potential_handleerror!(self.message_handler.chan_handler.handle_accept_channel(&peer.their_node_id.unwrap(), peer.their_local_features.clone().unwrap(), &msg));
                                                                                        },
 
                                                                                        34 => {
@@ -1096,7 +1093,6 @@ mod tests {
        }
 
        fn create_network(peer_count: usize) -> Vec<PeerManager<FileDescriptor>> {
-               let secp_ctx = Secp256k1::new();
                let mut peers = Vec::new();
                let mut rng = thread_rng();
                let logger : Arc<Logger> = Arc::new(test_utils::TestLogger::new());
@@ -1107,7 +1103,7 @@ mod tests {
                        let node_id = {
                                let mut key_slice = [0;32];
                                rng.fill_bytes(&mut key_slice);
-                               SecretKey::from_slice(&secp_ctx, &key_slice).unwrap()
+                               SecretKey::from_slice(&key_slice).unwrap()
                        };
                        let msg_handler = MessageHandler { chan_handler: Arc::new(chan_handler), route_handler: Arc::new(router) };
                        let peer = PeerManager::new(msg_handler, node_id, Arc::clone(&logger));
@@ -1128,7 +1124,7 @@ mod tests {
        #[test]
        fn test_disconnect_peer() {
                // Simple test which builds a network of PeerManager, connects and brings them to NoiseState::Finished and
-               // push an DisconnectPeer event to remove the node flagged by id
+               // push a DisconnectPeer event to remove the node flagged by id
                let mut peers = create_network(2);
                establish_connection(&peers[0], &peers[1]);
                assert_eq!(peers[0].peers.lock().unwrap().peers.len(), 1);