Pass Init message to sync_routing_table method
[rust-lightning] / lightning / src / routing / network_graph.rs
index c1258ce37a2b1a6406dc53b5dea00b55401aff23..3a7cd4eb985ad3713dd468277cedb59131f9e738 100644 (file)
@@ -23,7 +23,7 @@ use bitcoin::hash_types::BlockHash;
 use chain;
 use chain::Access;
 use ln::features::{ChannelFeatures, NodeFeatures};
-use ln::msgs::{DecodeError, ErrorAction, LightningError, RoutingMessageHandler, NetAddress, MAX_VALUE_MSAT};
+use ln::msgs::{DecodeError, ErrorAction, Init, LightningError, RoutingMessageHandler, NetAddress, MAX_VALUE_MSAT};
 use ln::msgs::{ChannelAnnouncement, ChannelUpdate, NodeAnnouncement, OptionalField};
 use ln::msgs::{QueryChannelRange, ReplyChannelRange, QueryShortChannelIds, ReplyShortChannelIdsEnd};
 use ln::msgs;
@@ -226,7 +226,10 @@ impl<C: Deref + Sync + Send, L: Deref + Sync + Send> RoutingMessageHandler for N
        /// gossip messages for each channel. The sync is considered complete when
        /// the final reply_scids_end message is received, though we are not
        /// tracking this directly.
-       fn sync_routing_table(&self, their_node_id: &PublicKey) {
+       fn sync_routing_table(&self, their_node_id: &PublicKey, init_msg: &Init) {
+               if !init_msg.features.supports_gossip_queries() {
+                       return ();
+               }
                let first_blocknum = 0;
                let number_of_blocks = 0xffffffff;
                log_debug!(self.logger, "Sending query_channel_range peer={}, first_blocknum={}, number_of_blocks={}", log_pubkey!(their_node_id), first_blocknum, number_of_blocks);
@@ -246,10 +249,8 @@ impl<C: Deref + Sync + Send, L: Deref + Sync + Send> RoutingMessageHandler for N
        /// stateless, it does not validate the sequencing of replies for multi-
        /// reply ranges. It does not validate whether the reply(ies) cover the
        /// queried range. It also does not filter SCIDs to only those in the
-       /// original query range. In the event of a failure, we may have received
-       /// some channel information. Before trying with another peer, the
-       /// caller should update its set of SCIDs that need to be queried.
-       fn handle_reply_channel_range(&self, their_node_id: &PublicKey, msg: &ReplyChannelRange) -> Result<(), LightningError> {
+       /// original query range.
+       fn handle_reply_channel_range(&self, their_node_id: &PublicKey, msg: ReplyChannelRange) -> Result<(), LightningError> {
                log_debug!(self.logger, "Handling reply_channel_range peer={}, first_blocknum={}, number_of_blocks={}, full_information={}, scids={}", log_pubkey!(their_node_id), msg.first_blocknum, msg.number_of_blocks, msg.full_information, msg.short_channel_ids.len(),);
 
                // Validate that the remote node maintains up-to-date channel
@@ -263,20 +264,13 @@ impl<C: Deref + Sync + Send, L: Deref + Sync + Send> RoutingMessageHandler for N
                        });
                }
 
-               // Copy the SCIDs into a new vector to be sent in the SCID query
-               let scid_size = msg.short_channel_ids.len();
-               let mut short_channel_ids: Vec<u64> = Vec::with_capacity(scid_size);
-               for scid in msg.short_channel_ids.iter() {
-                       short_channel_ids.push(scid.clone());
-               }
-
-               log_debug!(self.logger, "Sending query_short_channel_ids peer={}, batch_size={}", log_pubkey!(their_node_id), scid_size);
+               log_debug!(self.logger, "Sending query_short_channel_ids peer={}, batch_size={}", log_pubkey!(their_node_id), msg.short_channel_ids.len());
                let mut pending_events = self.pending_events.lock().unwrap();
                pending_events.push(events::MessageSendEvent::SendShortIdsQuery {
                        node_id: their_node_id.clone(),
                        msg: QueryShortChannelIds {
-                               chain_hash: msg.chain_hash.clone(),
-                               short_channel_ids,
+                               chain_hash: msg.chain_hash,
+                               short_channel_ids: msg.short_channel_ids,
                        }
                });
 
@@ -287,7 +281,7 @@ impl<C: Deref + Sync + Send, L: Deref + Sync + Send> RoutingMessageHandler for N
        /// gossip messages. In the event of a failure, we may have received
        /// some channel information. Before trying with another peer, the
        /// caller should update its set of SCIDs that need to be queried.
-       fn handle_reply_short_channel_ids_end(&self, their_node_id: &PublicKey, msg: &ReplyShortChannelIdsEnd) -> Result<(), LightningError> {
+       fn handle_reply_short_channel_ids_end(&self, their_node_id: &PublicKey, msg: ReplyShortChannelIdsEnd) -> Result<(), LightningError> {
                log_debug!(self.logger, "Handling reply_short_channel_ids_end peer={}, full_information={}", log_pubkey!(their_node_id), msg.full_information);
 
                // If the remote node does not have up-to-date information for the
@@ -303,10 +297,7 @@ impl<C: Deref + Sync + Send, L: Deref + Sync + Send> RoutingMessageHandler for N
                Ok(())
        }
 
-       /// There are potential DoS vectors when handling inbound queries.
-       /// Handling requests with first_blocknum very far away may trigger repeated
-       /// disk I/O if the NetworkGraph is not fully in-memory.
-       fn handle_query_channel_range(&self, _their_node_id: &PublicKey, _msg: &QueryChannelRange) -> Result<(), LightningError> {
+       fn handle_query_channel_range(&self, _their_node_id: &PublicKey, _msg: QueryChannelRange) -> Result<(), LightningError> {
                // TODO
                Err(LightningError {
                        err: String::from("Not implemented"),
@@ -314,10 +305,7 @@ impl<C: Deref + Sync + Send, L: Deref + Sync + Send> RoutingMessageHandler for N
                })
        }
 
-       /// There are potential DoS vectors when handling inbound queries.
-       /// Handling requests with first_blocknum very far away may trigger repeated
-       /// disk I/O if the NetworkGraph is not fully in-memory.
-       fn handle_query_short_channel_ids(&self, _their_node_id: &PublicKey, _msg: &QueryShortChannelIds) -> Result<(), LightningError> {
+       fn handle_query_short_channel_ids(&self, _their_node_id: &PublicKey, _msg: QueryShortChannelIds) -> Result<(), LightningError> {
                // TODO
                Err(LightningError {
                        err: String::from("Not implemented"),
@@ -1011,9 +999,9 @@ impl NetworkGraph {
 #[cfg(test)]
 mod tests {
        use chain;
-       use ln::features::{ChannelFeatures, NodeFeatures};
+       use ln::features::{ChannelFeatures, InitFeatures, NodeFeatures};
        use routing::network_graph::{NetGraphMsgHandler, NetworkGraph};
-       use ln::msgs::{OptionalField, RoutingMessageHandler, UnsignedNodeAnnouncement, NodeAnnouncement,
+       use ln::msgs::{Init, OptionalField, RoutingMessageHandler, UnsignedNodeAnnouncement, NodeAnnouncement,
                UnsignedChannelAnnouncement, ChannelAnnouncement, UnsignedChannelUpdate, ChannelUpdate, HTLCFailChannelUpdate,
                ReplyChannelRange, ReplyShortChannelIdsEnd, QueryChannelRange, QueryShortChannelIds, MAX_VALUE_MSAT};
        use util::test_utils;
@@ -1954,20 +1942,31 @@ mod tests {
                let chain_hash = genesis_block(Network::Testnet).header.block_hash();
                let first_blocknum = 0;
                let number_of_blocks = 0xffff_ffff;
-               net_graph_msg_handler.sync_routing_table(&node_id_1);
+
+               // It should ignore if gossip_queries feature is not enabled
+               {
+                       let init_msg = Init { features: InitFeatures::known().clear_gossip_queries() };
+                       net_graph_msg_handler.sync_routing_table(&node_id_1, &init_msg);
+                       let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
+                       assert_eq!(events.len(), 0);
+               }
 
                // It should send a query_channel_message with the correct information
-               let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
-               assert_eq!(events.len(), 1);
-               match &events[0] {
-                       MessageSendEvent::SendChannelRangeQuery{ node_id, msg } => {
-                               assert_eq!(node_id, &node_id_1);
-                               assert_eq!(msg.chain_hash, chain_hash);
-                               assert_eq!(msg.first_blocknum, first_blocknum);
-                               assert_eq!(msg.number_of_blocks, number_of_blocks);
-                       },
-                       _ => panic!("Expected MessageSendEvent::SendChannelRangeQuery")
-               };
+               {
+                       let init_msg = Init { features: InitFeatures::known() };
+                       net_graph_msg_handler.sync_routing_table(&node_id_1, &init_msg);
+                       let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
+                       assert_eq!(events.len(), 1);
+                       match &events[0] {
+                               MessageSendEvent::SendChannelRangeQuery{ node_id, msg } => {
+                                       assert_eq!(node_id, &node_id_1);
+                                       assert_eq!(msg.chain_hash, chain_hash);
+                                       assert_eq!(msg.first_blocknum, first_blocknum);
+                                       assert_eq!(msg.number_of_blocks, number_of_blocks);
+                               },
+                               _ => panic!("Expected MessageSendEvent::SendChannelRangeQuery")
+                       };
+               }
        }
 
        #[test]
@@ -1982,7 +1981,7 @@ mod tests {
                // matching the SCIDs in the reply
                {
                        // Handle a single successful reply that encompasses the queried channel range
-                       let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
+                       let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, ReplyChannelRange {
                                chain_hash,
                                full_information: true,
                                first_blocknum: 0,
@@ -2023,7 +2022,7 @@ mod tests {
                // full_information=false and short_channel_ids=[] as the signal.
                {
                        // Handle the reply indicating the peer was unable to fulfill our request.
-                       let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, &ReplyChannelRange {
+                       let result = net_graph_msg_handler.handle_reply_channel_range(&node_id_1, ReplyChannelRange {
                                chain_hash,
                                full_information: false,
                                first_blocknum: 1000,
@@ -2045,7 +2044,7 @@ mod tests {
 
                // Test receipt of a successful reply
                {
-                       let result = net_graph_msg_handler.handle_reply_short_channel_ids_end(&node_id, &ReplyShortChannelIdsEnd {
+                       let result = net_graph_msg_handler.handle_reply_short_channel_ids_end(&node_id, ReplyShortChannelIdsEnd {
                                chain_hash,
                                full_information: true,
                        });
@@ -2055,7 +2054,7 @@ mod tests {
                // Test receipt of a reply that indicates the peer does not maintain up-to-date information
                // for the chain_hash requested in the query.
                {
-                       let result = net_graph_msg_handler.handle_reply_short_channel_ids_end(&node_id, &ReplyShortChannelIdsEnd {
+                       let result = net_graph_msg_handler.handle_reply_short_channel_ids_end(&node_id, ReplyShortChannelIdsEnd {
                                chain_hash,
                                full_information: false,
                        });
@@ -2072,7 +2071,7 @@ mod tests {
 
                let chain_hash = genesis_block(Network::Testnet).header.block_hash();
 
-               let result = net_graph_msg_handler.handle_query_channel_range(&node_id, &QueryChannelRange {
+               let result = net_graph_msg_handler.handle_query_channel_range(&node_id, QueryChannelRange {
                        chain_hash,
                        first_blocknum: 0,
                        number_of_blocks: 0xffff_ffff,
@@ -2088,7 +2087,7 @@ mod tests {
 
                let chain_hash = genesis_block(Network::Testnet).header.block_hash();
 
-               let result = net_graph_msg_handler.handle_query_short_channel_ids(&node_id, &QueryShortChannelIds {
+               let result = net_graph_msg_handler.handle_query_short_channel_ids(&node_id, QueryShortChannelIds {
                        chain_hash,
                        short_channel_ids: vec![0x0003e8_000000_0000],
                });