Merge pull request #1505 from tnull/2022-05-support-0conf-channeltype
[rust-lightning] / lightning / src / routing / network_graph.rs
index 26719cc279061f180f5617f204846922271f3f3b..18b846cbf8895ce1e10f7ff5ba6411b1a1e91011 100644 (file)
@@ -123,6 +123,7 @@ impl Readable for NodeId {
 
 /// Represents the network as nodes and channels between them
 pub struct NetworkGraph {
+       last_rapid_gossip_sync_timestamp: Mutex<Option<u32>>,
        genesis_hash: BlockHash,
        // Lock order: channels -> nodes
        channels: RwLock<BTreeMap<u64, ChannelInfo>>,
@@ -133,10 +134,12 @@ impl Clone for NetworkGraph {
        fn clone(&self) -> Self {
                let channels = self.channels.read().unwrap();
                let nodes = self.nodes.read().unwrap();
+               let last_rapid_gossip_sync_timestamp = self.get_last_rapid_gossip_sync_timestamp();
                Self {
                        genesis_hash: self.genesis_hash.clone(),
                        channels: RwLock::new(channels.clone()),
                        nodes: RwLock::new(nodes.clone()),
+                       last_rapid_gossip_sync_timestamp: Mutex::new(last_rapid_gossip_sync_timestamp)
                }
        }
 }
@@ -990,7 +993,10 @@ impl Writeable for NetworkGraph {
                        node_info.write(writer)?;
                }
 
-               write_tlv_fields!(writer, {});
+               let last_rapid_gossip_sync_timestamp = self.get_last_rapid_gossip_sync_timestamp();
+               write_tlv_fields!(writer, {
+                       (1, last_rapid_gossip_sync_timestamp, option),
+               });
                Ok(())
        }
 }
@@ -1014,12 +1020,17 @@ impl Readable for NetworkGraph {
                        let node_info = Readable::read(reader)?;
                        nodes.insert(node_id, node_info);
                }
-               read_tlv_fields!(reader, {});
+
+               let mut last_rapid_gossip_sync_timestamp: Option<u32> = None;
+               read_tlv_fields!(reader, {
+                       (1, last_rapid_gossip_sync_timestamp, option),
+               });
 
                Ok(NetworkGraph {
                        genesis_hash,
                        channels: RwLock::new(channels),
                        nodes: RwLock::new(nodes),
+                       last_rapid_gossip_sync_timestamp: Mutex::new(last_rapid_gossip_sync_timestamp),
                })
        }
 }
@@ -1053,6 +1064,7 @@ impl NetworkGraph {
                        genesis_hash,
                        channels: RwLock::new(BTreeMap::new()),
                        nodes: RwLock::new(BTreeMap::new()),
+                       last_rapid_gossip_sync_timestamp: Mutex::new(None),
                }
        }
 
@@ -1066,6 +1078,18 @@ impl NetworkGraph {
                }
        }
 
+       /// The unix timestamp provided by the most recent rapid gossip sync.
+       /// It will be set by the rapid sync process after every sync completion.
+       pub fn get_last_rapid_gossip_sync_timestamp(&self) -> Option<u32> {
+               self.last_rapid_gossip_sync_timestamp.lock().unwrap().clone()
+       }
+
+       /// Update the unix timestamp provided by the most recent rapid gossip sync.
+       /// This should be done automatically by the rapid sync process after every sync completion.
+       pub fn set_last_rapid_gossip_sync_timestamp(&self, last_rapid_gossip_sync_timestamp: u32) {
+               self.last_rapid_gossip_sync_timestamp.lock().unwrap().replace(last_rapid_gossip_sync_timestamp);
+       }
+
        /// Clears the `NodeAnnouncementInfo` field for all nodes in the `NetworkGraph` for testing
        /// purposes.
        #[cfg(test)]
@@ -2359,6 +2383,18 @@ mod tests {
                assert!(<NetworkGraph>::read(&mut io::Cursor::new(&w.0)).unwrap() == network_graph);
        }
 
+       #[test]
+       fn network_graph_tlv_serialization() {
+               let mut network_graph = create_network_graph();
+               network_graph.set_last_rapid_gossip_sync_timestamp(42);
+
+               let mut w = test_utils::TestVecWriter(Vec::new());
+               network_graph.write(&mut w).unwrap();
+               let reassembled_network_graph: NetworkGraph = Readable::read(&mut io::Cursor::new(&w.0)).unwrap();
+               assert!(reassembled_network_graph == network_graph);
+               assert_eq!(reassembled_network_graph.get_last_rapid_gossip_sync_timestamp().unwrap(), 42);
+       }
+
        #[test]
        #[cfg(feature = "std")]
        fn calling_sync_routing_table() {