Implement finding paths for MPP
[rust-lightning] / lightning / src / routing / network_graph.rs
index 48900ecc446ffa7ef456c3a1d78f9782cceded92..6856bb16bcfc4bbb221542d92967f7e32fecfa69 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;
@@ -98,6 +98,13 @@ impl<C: Deref, L: Deref> NetGraphMsgHandler<C, L> where C::Target: chain::Access
                }
        }
 
+       /// Adds a provider used to check new announcements. Does not affect
+       /// existing announcements unless they are updated.
+       /// Add, update or remove the provider would replace the current one.
+       pub fn add_chain_access(&mut self, chain_access: Option<C>) {
+               self.chain_access = chain_access;
+       }
+
        /// Take a read lock on the network_graph and return it in the C-bindings
        /// newtype helper. This is likely only useful when called via the C
        /// bindings as you can call `self.network_graph.read().unwrap()` in Rust
@@ -105,6 +112,18 @@ impl<C: Deref, L: Deref> NetGraphMsgHandler<C, L> where C::Target: chain::Access
        pub fn read_locked_graph<'a>(&'a self) -> LockedNetworkGraph<'a> {
                LockedNetworkGraph(self.network_graph.read().unwrap())
        }
+
+       /// Returns true when a full routing table sync should be performed with a peer.
+       fn should_request_full_sync(&self, _node_id: &PublicKey) -> bool {
+               //TODO: Determine whether to request a full sync based on the network map.
+               const FULL_SYNCS_TO_REQUEST: usize = 5;
+               if self.full_syncs_requested.load(Ordering::Acquire) < FULL_SYNCS_TO_REQUEST {
+                       self.full_syncs_requested.fetch_add(1, Ordering::AcqRel);
+                       true
+               } else {
+                       false
+               }
+       }
 }
 
 impl<'a> LockedNetworkGraph<'a> {
@@ -207,26 +226,27 @@ impl<C: Deref + Sync + Send, L: Deref + Sync + Send> RoutingMessageHandler for N
                result
        }
 
-       fn should_request_full_sync(&self, _node_id: &PublicKey) -> bool {
-               //TODO: Determine whether to request a full sync based on the network map.
-               const FULL_SYNCS_TO_REQUEST: usize = 5;
-               if self.full_syncs_requested.load(Ordering::Acquire) < FULL_SYNCS_TO_REQUEST {
-                       self.full_syncs_requested.fetch_add(1, Ordering::AcqRel);
-                       true
-               } else {
-                       false
-               }
-       }
-
        /// Initiates a stateless sync of routing gossip information with a peer
-       /// by calling query_channel_range. The default strategy used by this
-       /// implementation is to sync for the full block range with several peers.
+       /// using gossip_queries. The default strategy used by this implementation
+       /// is to sync the full block range with several peers.
+       ///
        /// We should expect one or more reply_channel_range messages in response
-       /// to our query. Each reply will enqueue a query_scid message to request
-       /// gossip messages for each channel. The sync is considered complete when
-       /// the final reply_scids_end message is received, though we are not
+       /// to our query_channel_range. Each reply will enqueue a query_scid message
+       /// to request 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) {
+
+               // We will only perform a sync with peers that support gossip_queries.
+               if !init_msg.features.supports_gossip_queries() {
+                       return ();
+               }
+
+               // Check if we need to perform a full synchronization with this peer
+               if !self.should_request_full_sync(their_node_id) {
+                       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,7 +266,10 @@ 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.
+       /// original query range. We also do not validate that the chain_hash
+       /// matches the chain_hash of the NetworkGraph. Any chan_ann message that
+       /// does not match our chain_hash will be rejected when the announcement is
+       /// processed.
        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(),);
 
@@ -996,9 +1019,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;
@@ -1939,20 +1962,51 @@ 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")
+                       };
+               }
+
+               // It should not enqueue a query when should_request_full_sync return false.
+               // The initial implementation allows syncing with the first 5 peers after
+               // which should_request_full_sync will return false
+               {
+                       let (secp_ctx, net_graph_msg_handler) = create_net_graph_msg_handler();
+                       let init_msg = Init { features: InitFeatures::known() };
+                       for n in 1..7 {
+                               let node_privkey = &SecretKey::from_slice(&[n; 32]).unwrap();
+                               let node_id = PublicKey::from_secret_key(&secp_ctx, node_privkey);
+                               net_graph_msg_handler.sync_routing_table(&node_id, &init_msg);
+                               let events = net_graph_msg_handler.get_and_clear_pending_msg_events();
+                               if n <= 5 {
+                                       assert_eq!(events.len(), 1);
+                               } else {
+                                       assert_eq!(events.len(), 0);
+                               }
+
+                       }
+               }
        }
 
        #[test]
@@ -1966,7 +2020,6 @@ mod tests {
                // Test receipt of a single reply that should enqueue an SCID query
                // 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 {
                                chain_hash,
                                full_information: true,