Merge pull request #490 from jkczyz/2020-02-initial-routing-sync
[rust-lightning] / lightning / src / ln / peer_handler.rs
index 30e208155c5699596ee92988b37790f10ff904a2..6576da806afb48c938791f47c0fee55880b5452a 100644 (file)
@@ -189,7 +189,6 @@ pub struct PeerManager<Descriptor: SocketDescriptor, CM: Deref> where CM::Target
        peer_counter_low: AtomicUsize,
        peer_counter_high: AtomicUsize,
 
-       initial_syncs_sent: AtomicUsize,
        logger: Arc<Logger>,
 }
 
@@ -212,9 +211,6 @@ macro_rules! encode_msg {
        }}
 }
 
-//TODO: Really should do something smarter for this
-const INITIAL_SYNCS_TO_SEND: usize = 5;
-
 /// Manages and reacts to connection events. You probably want to use file descriptors as PeerIds.
 /// PeerIds may repeat, but only after disconnect_event() has been called.
 impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where CM::Target: msgs::ChannelMessageHandler {
@@ -236,7 +232,6 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
                        ephemeral_key_midstate,
                        peer_counter_low: AtomicUsize::new(0),
                        peer_counter_high: AtomicUsize::new(0),
-                       initial_syncs_sent: AtomicUsize::new(0),
                        logger,
                }
        }
@@ -517,37 +512,6 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
                                                                }
                                                        }
 
-                                                       macro_rules! try_potential_decodeerror {
-                                                               ($thing: expr) => {
-                                                                       match $thing {
-                                                                               Ok(x) => x,
-                                                                               Err(e) => {
-                                                                                       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 update!");
-                                                                                                       continue;
-                                                                                               },
-                                                                                               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;
-                                                                                               },
-                                                                                               msgs::DecodeError::BadLengthDescriptor => return Err(PeerHandleError{ no_connection_possible: false }),
-                                                                                               msgs::DecodeError::Io(_) => return Err(PeerHandleError{ no_connection_possible: false }),
-                                                                                       }
-                                                                               }
-                                                                       };
-                                                               }
-                                                       }
-
                                                        macro_rules! insert_node_id {
                                                                () => {
                                                                        match peers.node_id_to_descriptor.entry(peer.their_node_id.unwrap()) {
@@ -580,8 +544,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
                                                                        peer.their_node_id = Some(their_node_id);
                                                                        insert_node_id!();
                                                                        let mut features = InitFeatures::supported();
-                                                                       if self.initial_syncs_sent.load(Ordering::Acquire) < INITIAL_SYNCS_TO_SEND {
-                                                                               self.initial_syncs_sent.fetch_add(1, Ordering::AcqRel);
+                                                                       if self.message_handler.route_handler.should_request_full_sync(&peer.their_node_id.unwrap()) {
                                                                                features.set_initial_routing_sync();
                                                                        }
 
@@ -613,7 +576,34 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
                                                                                peer.pending_read_is_header = true;
 
                                                                                let mut reader = ::std::io::Cursor::new(&msg_data[..]);
-                                                                               let message = try_potential_decodeerror!(wire::read(&mut reader));
+                                                                               let message_result = wire::read(&mut reader);
+                                                                               let message = match message_result {
+                                                                                       Ok(x) => x,
+                                                                                       Err(e) => {
+                                                                                               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 update!");
+                                                                                                               continue;
+                                                                                                       }
+                                                                                                       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;
+                                                                                                       }
+                                                                                                       msgs::DecodeError::BadLengthDescriptor => return Err(PeerHandleError { no_connection_possible: false }),
+                                                                                                       msgs::DecodeError::Io(_) => return Err(PeerHandleError { no_connection_possible: false }),
+                                                                                               }
+                                                                                       }
+                                                                               };
+
                                                                                log_trace!(self, "Received message of type {} from {}", message.type_id(), log_pubkey!(peer.their_node_id.unwrap()));
 
                                                                                // Need an Init as first message
@@ -652,8 +642,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
 
                                                                                                if !peer.outbound {
                                                                                                        let mut features = InitFeatures::supported();
-                                                                                                       if self.initial_syncs_sent.load(Ordering::Acquire) < INITIAL_SYNCS_TO_SEND {
-                                                                                                               self.initial_syncs_sent.fetch_add(1, Ordering::AcqRel);
+                                                                                                       if self.message_handler.route_handler.should_request_full_sync(&peer.their_node_id.unwrap()) {
                                                                                                                features.set_initial_routing_sync();
                                                                                                        }
 
@@ -773,12 +762,11 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
                                                                                        },
 
                                                                                        // Unknown messages:
-                                                                                       wire::Message::Unknown(msg_type) => {
+                                                                                       wire::Message::Unknown(msg_type) if msg_type.is_even() => {
                                                                                                // Fail the channel if message is an even, unknown type as per BOLT #1.
-                                                                                               if (msg_type & 1) == 0 {
-                                                                                                       return Err(PeerHandleError{ no_connection_possible: true });
-                                                                                               }
+                                                                                               return Err(PeerHandleError{ no_connection_possible: true });
                                                                                        },
+                                                                                       wire::Message::Unknown(_) => {},
                                                                                }
                                                                        }
                                                                }