Distinguish between snapshot and symlink intervals.
[rapid-gossip-sync-server] / src / lib.rs
index fe824894171d221cece8b9a2a0a67383445916cb..f56aca260f218c0798df4b6ef2300c8534c4dcf5 100644 (file)
@@ -4,7 +4,6 @@
 #![deny(non_upper_case_globals)]
 #![deny(non_camel_case_types)]
 #![deny(non_snake_case)]
-#![deny(unused_mut)]
 #![deny(unused_variables)]
 #![deny(unused_imports)]
 
@@ -13,24 +12,24 @@ extern crate core;
 use std::collections::{HashMap, HashSet};
 use std::fs::File;
 use std::io::BufReader;
+use std::ops::Deref;
 use std::sync::Arc;
-use std::sync::atomic::{AtomicBool, Ordering};
+use lightning::log_info;
 
-use bitcoin::blockdata::constants::genesis_block;
-use bitcoin::Network;
-use bitcoin::secp256k1::PublicKey;
-use lightning::routing::gossip::NetworkGraph;
+use lightning::routing::gossip::{NetworkGraph, NodeId};
+use lightning::util::logger::Logger;
 use lightning::util::ser::{ReadableArgs, Writeable};
 use tokio::sync::mpsc;
+use tokio_postgres::{Client, NoTls};
+use crate::config::SYMLINK_GRANULARITY_INTERVAL;
 use crate::lookup::DeltaSet;
 
 use crate::persistence::GossipPersister;
-use crate::serialization::UpdateSerializationMechanism;
+use crate::serialization::UpdateSerialization;
 use crate::snapshot::Snapshotter;
-use crate::types::{GossipChainAccess, TestLogger};
+use crate::types::RGSSLogger;
 
 mod downloader;
-mod types;
 mod tracking;
 mod lookup;
 mod persistence;
@@ -40,9 +39,17 @@ mod config;
 mod hex_utils;
 mod verifier;
 
-pub struct RapidSyncProcessor {
-       network_graph: Arc<NetworkGraph<Arc<TestLogger>>>,
-       pub initial_sync_complete: Arc<AtomicBool>,
+pub mod types;
+
+/// The purpose of this prefix is to identify the serialization format, should other rapid gossip
+/// sync formats arise in the future.
+///
+/// The fourth byte is the protocol version in case our format gets updated.
+const GOSSIP_PREFIX: [u8; 4] = [76, 68, 75, 1];
+
+pub struct RapidSyncProcessor<L: Deref> where L::Target: Logger {
+       network_graph: Arc<NetworkGraph<L>>,
+       logger: L
 }
 
 pub struct SerializedResponse {
@@ -54,85 +61,63 @@ pub struct SerializedResponse {
        pub update_count_incremental: u32,
 }
 
-impl RapidSyncProcessor {
-       pub fn new() -> Self {
-               let logger = TestLogger::new();
-               let mut initial_sync_complete = false;
-               let arc_logger = Arc::new(logger);
+impl<L: Deref + Clone + Send + Sync + 'static> RapidSyncProcessor<L> where L::Target: Logger {
+       pub fn new(logger: L) -> Self {
+               let network = config::network();
                let network_graph = if let Ok(file) = File::open(&config::network_graph_cache_path()) {
-                       println!("Initializing from cached network graph…");
+                       log_info!(logger, "Initializing from cached network graph…");
                        let mut buffered_reader = BufReader::new(file);
-                       let network_graph_result = NetworkGraph::read(&mut buffered_reader, Arc::clone(&arc_logger));
+                       let network_graph_result = NetworkGraph::read(&mut buffered_reader, logger.clone());
                        if let Ok(network_graph) = network_graph_result {
-                               initial_sync_complete = true;
-                               network_graph.remove_stale_channels();
-                               println!("Initialized from cached network graph!");
+                               log_info!(logger, "Initialized from cached network graph!");
                                network_graph
                        } else {
-                               println!("Initialization from cached network graph failed: {}", network_graph_result.err().unwrap());
-                               NetworkGraph::new(genesis_block(Network::Bitcoin).header.block_hash(), arc_logger)
+                               log_info!(logger, "Initialization from cached network graph failed: {}", network_graph_result.err().unwrap());
+                               NetworkGraph::new(network, logger.clone())
                        }
                } else {
-                       NetworkGraph::new(genesis_block(Network::Bitcoin).header.block_hash(), arc_logger)
+                       NetworkGraph::new(network, logger.clone())
                };
                let arc_network_graph = Arc::new(network_graph);
-               let (_sync_termination_sender, _sync_termination_receiver) = mpsc::channel::<()>(1);
                Self {
                        network_graph: arc_network_graph,
-                       initial_sync_complete: Arc::new(AtomicBool::new(initial_sync_complete)),
+                       logger
                }
        }
 
        pub async fn start_sync(&self) {
+               log_info!(self.logger, "Starting Rapid Gossip Sync Server");
+               log_info!(self.logger, "Snapshot interval: {} seconds", config::snapshot_generation_interval());
+
                // means to indicate sync completion status within this module
                let (sync_completion_sender, mut sync_completion_receiver) = mpsc::channel::<()>(1);
-               let initial_sync_complete = self.initial_sync_complete.clone();
-
-               let network_graph = self.network_graph.clone();
-               let snapshotter = Snapshotter::new(network_graph.clone());
 
                if config::DOWNLOAD_NEW_GOSSIP {
+                       let (mut persister, persistence_sender) = GossipPersister::new(self.network_graph.clone(), self.logger.clone());
 
-                       let mut persister = GossipPersister::new(sync_completion_sender, self.network_graph.clone());
-
-                       let persistence_sender = persister.gossip_persistence_sender.clone();
-                       println!("Starting gossip download");
-                       let download_future = tracking::download_gossip(persistence_sender, network_graph.clone());
-                       tokio::spawn(async move {
-                               // initiate the whole download stuff in the background
-                               download_future.await;
-                       });
-                       println!("Starting gossip db persistence listener");
-                       tokio::spawn(async move {
-                               // initiate persistence of the gossip data
-                               let persistence_future = persister.persist_gossip();
-                               persistence_future.await;
-                       });
-
+                       log_info!(self.logger, "Starting gossip download");
+                       tokio::spawn(tracking::download_gossip(persistence_sender, sync_completion_sender,
+                               Arc::clone(&self.network_graph), self.logger.clone()));
+                       log_info!(self.logger, "Starting gossip db persistence listener");
+                       tokio::spawn(async move { persister.persist_gossip().await; });
                } else {
                        sync_completion_sender.send(()).await.unwrap();
                }
 
-               {
-                       let sync_completion = sync_completion_receiver.recv().await;
-                       if sync_completion.is_none() {
-                               panic!("Sync failed!");
-                       }
-                       initial_sync_complete.store(true, Ordering::Release);
-                       println!("Initial sync complete!");
-
-                       // start the gossip snapshotting service
-                       snapshotter.snapshot_gossip().await;
+               let sync_completion = sync_completion_receiver.recv().await;
+               if sync_completion.is_none() {
+                       panic!("Sync failed!");
                }
-       }
+               log_info!(self.logger, "Initial sync complete!");
 
-       pub async fn serialize_delta(&self, last_sync_timestamp: u32, consider_intermediate_updates: bool) -> SerializedResponse {
-               crate::serialize_delta(self.network_graph.clone(), last_sync_timestamp, consider_intermediate_updates).await
+               // start the gossip snapshotting service
+               Snapshotter::new(Arc::clone(&self.network_graph), self.logger.clone()).snapshot_gossip().await;
        }
 }
 
-async fn serialize_delta(network_graph: Arc<NetworkGraph<Arc<TestLogger>>>, last_sync_timestamp: u32, consider_intermediate_updates: bool) -> SerializedResponse {
-       let (client, connection) = lookup::connect_to_db().await;
+pub(crate) async fn connect_to_db() -> Client {
+       let connection_config = config::db_connection_config();
+       let (client, connection) = connection_config.connect(NoTls).await.unwrap();
 
        tokio::spawn(async move {
                if let Err(e) = connection.await {
@@ -140,36 +125,76 @@ async fn serialize_delta(network_graph: Arc<NetworkGraph<Arc<TestLogger>>>, last
                }
        });
 
+       client.execute("set time zone UTC", &[]).await.unwrap();
+       client
+}
+
+/// This method generates a no-op blob that can be used as a delta where none exists.
+///
+/// The primary purpose of this method is the scenario of a client retrieving and processing a
+/// given snapshot, and then immediately retrieving the would-be next snapshot at the timestamp
+/// indicated by the one that was just processed.
+/// Previously, there would not be a new snapshot to be processed for that particular timestamp yet,
+/// and the server would return a 404 error.
+///
+/// In principle, this method could also be used to address another unfortunately all too common
+/// pitfall: requesting snapshots from intermediate timestamps, i. e. those that are not multiples
+/// of our granularity constant. Note that for that purpose, this method could be very dangerous,
+/// because if consumed, the `timestamp` value calculated here will overwrite the timestamp that
+/// the client previously had, which could result in duplicated or omitted gossip down the line.
+fn serialize_empty_blob(current_timestamp: u64) -> Vec<u8> {
+       let mut blob = GOSSIP_PREFIX.to_vec();
+
+       let network = config::network();
+       let genesis_block = bitcoin::blockdata::constants::genesis_block(network);
+       let chain_hash = genesis_block.block_hash();
+       chain_hash.write(&mut blob).unwrap();
+
+       let blob_timestamp = Snapshotter::<Arc<RGSSLogger>>::round_down_to_nearest_multiple(current_timestamp, SYMLINK_GRANULARITY_INTERVAL as u64) as u32;
+       blob_timestamp.write(&mut blob).unwrap();
+
+       0u32.write(&mut blob).unwrap(); // node count
+       0u32.write(&mut blob).unwrap(); // announcement count
+       0u32.write(&mut blob).unwrap(); // update count
+
+       blob
+}
+
+async fn serialize_delta<L: Deref + Clone>(network_graph: Arc<NetworkGraph<L>>, last_sync_timestamp: u32, logger: L) -> SerializedResponse where L::Target: Logger {
+       let client = connect_to_db().await;
+
+       network_graph.remove_stale_channels_and_tracking();
+
        let mut output: Vec<u8> = vec![];
+       let snapshot_interval = config::snapshot_generation_interval();
 
        // set a flag if the chain hash is prepended
        // chain hash only necessary if either channel announcements or non-incremental updates are present
        // for announcement-free incremental-only updates, chain hash can be skipped
 
-       let mut node_id_set: HashSet<[u8; 33]> = HashSet::new();
-       let mut node_id_indices: HashMap<[u8; 33], usize> = HashMap::new();
-       let mut node_ids: Vec<PublicKey> = Vec::new();
+       let mut node_id_set: HashSet<NodeId> = HashSet::new();
+       let mut node_id_indices: HashMap<NodeId, usize> = HashMap::new();
+       let mut node_ids: Vec<NodeId> = Vec::new();
        let mut duplicate_node_ids: i32 = 0;
 
-       let mut get_node_id_index = |node_id: PublicKey| {
-               let serialized_node_id = node_id.serialize();
-               if node_id_set.insert(serialized_node_id) {
+       let mut get_node_id_index = |node_id: NodeId| {
+               if node_id_set.insert(node_id) {
                        node_ids.push(node_id);
                        let index = node_ids.len() - 1;
-                       node_id_indices.insert(serialized_node_id, index);
+                       node_id_indices.insert(node_id, index);
                        return index;
                }
                duplicate_node_ids += 1;
-               node_id_indices[&serialized_node_id]
+               node_id_indices[&node_id]
        };
 
        let mut delta_set = DeltaSet::new();
-       lookup::fetch_channel_announcements(&mut delta_set, network_graph, &client, last_sync_timestamp).await;
-       println!("announcement channel count: {}", delta_set.len());
-       lookup::fetch_channel_updates(&mut delta_set, &client, last_sync_timestamp, consider_intermediate_updates).await;
-       println!("update-fetched channel count: {}", delta_set.len());
-       lookup::filter_delta_set(&mut delta_set);
-       println!("update-filtered channel count: {}", delta_set.len());
+       lookup::fetch_channel_announcements(&mut delta_set, network_graph, &client, last_sync_timestamp, logger.clone()).await;
+       log_info!(logger, "announcement channel count: {}", delta_set.len());
+       lookup::fetch_channel_updates(&mut delta_set, &client, last_sync_timestamp, logger.clone()).await;
+       log_info!(logger, "update-fetched channel count: {}", delta_set.len());
+       lookup::filter_delta_set(&mut delta_set, logger.clone());
+       log_info!(logger, "update-filtered channel count: {}", delta_set.len());
        let serialization_details = serialization::serialize_delta_set(delta_set, last_sync_timestamp);
 
        // process announcements
@@ -203,11 +228,11 @@ async fn serialize_delta(network_graph: Arc<NetworkGraph<Arc<TestLogger>>>, last
        let mut update_count_full = 0;
        let mut update_count_incremental = 0;
        for current_update in serialization_details.updates {
-               match &current_update.mechanism {
-                       UpdateSerializationMechanism::Full => {
+               match &current_update {
+                       UpdateSerialization::Full(_) => {
                                update_count_full += 1;
                        }
-                       UpdateSerializationMechanism::Incremental(_) => {
+                       UpdateSerialization::Incremental(_, _) | UpdateSerialization::Reminder(_, _) => {
                                update_count_incremental += 1;
                        }
                };
@@ -215,19 +240,19 @@ async fn serialize_delta(network_graph: Arc<NetworkGraph<Arc<TestLogger>>>, last
                let mut stripped_update = serialization::serialize_stripped_channel_update(&current_update, &default_update_values, previous_update_scid);
                output.append(&mut stripped_update);
 
-               previous_update_scid = current_update.update.short_channel_id;
+               previous_update_scid = current_update.scid();
        }
 
        // some stats
        let message_count = announcement_count + update_count;
 
-       let mut prefixed_output = vec![76, 68, 75, 1];
+       let mut prefixed_output = GOSSIP_PREFIX.to_vec();
 
        // always write the chain hash
        serialization_details.chain_hash.write(&mut prefixed_output).unwrap();
        // always write the latest seen timestamp
        let latest_seen_timestamp = serialization_details.latest_seen;
-       let overflow_seconds = latest_seen_timestamp % config::SNAPSHOT_CALCULATION_INTERVAL;
+       let overflow_seconds = latest_seen_timestamp % snapshot_interval;
        let serialized_seen_timestamp = latest_seen_timestamp.saturating_sub(overflow_seconds);
        serialized_seen_timestamp.write(&mut prefixed_output).unwrap();
 
@@ -240,8 +265,8 @@ async fn serialize_delta(network_graph: Arc<NetworkGraph<Arc<TestLogger>>>, last
 
        prefixed_output.append(&mut output);
 
-       println!("duplicated node ids: {}", duplicate_node_ids);
-       println!("latest seen timestamp: {:?}", serialization_details.latest_seen);
+       log_info!(logger, "duplicated node ids: {}", duplicate_node_ids);
+       log_info!(logger, "latest seen timestamp: {:?}", serialization_details.latest_seen);
 
        SerializedResponse {
                data: prefixed_output,