Require DB insertions to complete in fifteen seconds
[rapid-gossip-sync-server] / src / persistence.rs
index cd7ff400b2ca985eb4f54d0ecbb9abe5113bc1a8..ac667330c6e0c445d0e7e82f81e2649ba12111db 100644 (file)
@@ -1,35 +1,35 @@
 use std::fs::OpenOptions;
-use std::io::BufWriter;
+use std::io::{BufWriter, Write};
 use std::sync::Arc;
-use std::time::Instant;
+use std::time::{Duration, Instant};
 use lightning::routing::gossip::NetworkGraph;
 use lightning::util::ser::Writeable;
 use tokio::sync::mpsc;
 use tokio_postgres::NoTls;
 
-use crate::{config, hex_utils, TestLogger};
+use crate::{config, TestLogger};
 use crate::types::GossipMessage;
 
+const POSTGRES_INSERT_TIMEOUT: Duration = Duration::from_secs(15);
+
 pub(crate) struct GossipPersister {
        gossip_persistence_receiver: mpsc::Receiver<GossipMessage>,
-       server_sync_completion_sender: mpsc::Sender<()>,
        network_graph: Arc<NetworkGraph<TestLogger>>,
 }
 
 impl GossipPersister {
-       pub fn new(server_sync_completion_sender: mpsc::Sender<()>, network_graph: Arc<NetworkGraph<TestLogger>>) -> (Self, mpsc::Sender<GossipMessage>) {
+       pub fn new(network_graph: Arc<NetworkGraph<TestLogger>>) -> (Self, mpsc::Sender<GossipMessage>) {
                let (gossip_persistence_sender, gossip_persistence_receiver) =
                        mpsc::channel::<GossipMessage>(100);
                (GossipPersister {
                        gossip_persistence_receiver,
-                       server_sync_completion_sender,
                        network_graph
                }, gossip_persistence_sender)
        }
 
        pub(crate) async fn persist_gossip(&mut self) {
                let connection_config = config::db_connection_config();
-               let (client, connection) =
+               let (mut client, connection) =
                        connection_config.connect(NoTls).await.unwrap();
 
                tokio::spawn(async move {
@@ -47,6 +47,11 @@ impl GossipPersister {
                                panic!("db init error: {}", initialization_error);
                        }
 
+                       let cur_schema = client.query("SELECT db_schema FROM config WHERE id = $1", &[&1]).await.unwrap();
+                       if !cur_schema.is_empty() {
+                               config::upgrade_db(cur_schema[0].get(0), &mut client).await;
+                       }
+
                        let initialization = client
                                .execute(
                                        // TODO: figure out a way to fix the id value without Postgres complaining about
@@ -83,107 +88,51 @@ impl GossipPersister {
                        }
                }
 
-               // print log statement every 10,000 messages
-               let mut persistence_log_threshold = 10000;
+               // print log statement every minute
+               let mut latest_persistence_log = Instant::now() - Duration::from_secs(60);
                let mut i = 0u32;
-               let mut server_sync_completion_sent = false;
-               let mut latest_graph_cache_time: Option<Instant> = None;
+               let mut latest_graph_cache_time = Instant::now();
                // TODO: it would be nice to have some sort of timeout here so after 10 seconds of
                // inactivity, some sort of message could be broadcast signaling the activation of request
                // processing
                while let Some(gossip_message) = &self.gossip_persistence_receiver.recv().await {
                        i += 1; // count the persisted gossip messages
 
-                       if i == 1 || i % persistence_log_threshold == 0 {
+                       if latest_persistence_log.elapsed().as_secs() >= 60 {
                                println!("Persisting gossip message #{}", i);
+                               latest_persistence_log = Instant::now();
                        }
 
-                       if let Some(last_cache_time) = latest_graph_cache_time {
-                               // has it been ten minutes? Just cache it
-                               if last_cache_time.elapsed().as_secs() >= 600 {
-                                       self.persist_network_graph();
-                                       latest_graph_cache_time = Some(Instant::now());
-                               }
-                       } else {
-                               // initialize graph cache timer
-                               latest_graph_cache_time = Some(Instant::now());
+                       // has it been ten minutes? Just cache it
+                       if latest_graph_cache_time.elapsed().as_secs() >= 600 {
+                               self.persist_network_graph();
+                               latest_graph_cache_time = Instant::now();
                        }
 
                        match &gossip_message {
-                               GossipMessage::InitialSyncComplete => {
-                                       // signal to the server that it may now serve dynamic responses and calculate
-                                       // snapshots
-                                       // we take this detour through the persister to ensure that all previous
-                                       // messages have already been persisted to the database
-                                       println!("Persister caught up with gossip!");
-                                       i -= 1; // this wasn't an actual gossip message that needed persisting
-                                       persistence_log_threshold = 50;
-                                       if !server_sync_completion_sent {
-                                               server_sync_completion_sent = true;
-                                               self.server_sync_completion_sender.send(()).await.unwrap();
-                                               println!("Server has been notified of persistence completion.");
-                                       }
-
-                                       // now, cache the persisted network graph
-                                       // also persist the network graph here
-                                       let mut too_soon = false;
-                                       if let Some(latest_graph_cache_time) = latest_graph_cache_time {
-                                               let time_since_last_cached = latest_graph_cache_time.elapsed().as_secs();
-                                               // don't cache more frequently than every 2 minutes
-                                               too_soon = time_since_last_cached < 120;
-                                       }
-                                       if too_soon {
-                                               println!("Network graph has been cached too recently.");
-                                       }else {
-                                               latest_graph_cache_time = Some(Instant::now());
-                                               self.persist_network_graph();
-                                       }
-                               }
                                GossipMessage::ChannelAnnouncement(announcement) => {
-
-                                       let scid = announcement.contents.short_channel_id;
-                                       let scid_hex = hex_utils::hex_str(&scid.to_be_bytes());
-                                       // scid is 8 bytes
-                                       // block height is the first three bytes
-                                       // to obtain block height, shift scid right by 5 bytes (40 bits)
-                                       let block_height = (scid >> 5 * 8) as i32;
-                                       let chain_hash = announcement.contents.chain_hash.as_ref();
-                                       let chain_hash_hex = hex_utils::hex_str(chain_hash);
+                                       let scid = announcement.contents.short_channel_id as i64;
 
                                        // start with the type prefix, which is already known a priori
-                                       let mut announcement_signed = Vec::new(); // vec![1, 0];
+                                       let mut announcement_signed = Vec::new();
                                        announcement.write(&mut announcement_signed).unwrap();
 
-                                       let result = client
+                                       tokio::time::timeout(POSTGRES_INSERT_TIMEOUT, client
                                                .execute("INSERT INTO channel_announcements (\
                                                        short_channel_id, \
-                                                       block_height, \
-                                                       chain_hash, \
                                                        announcement_signed \
-                                               ) VALUES ($1, $2, $3, $4) ON CONFLICT (short_channel_id) DO NOTHING", &[
-                                                       &scid_hex,
-                                                       &block_height,
-                                                       &chain_hash_hex,
+                                               ) VALUES ($1, $2) ON CONFLICT (short_channel_id) DO NOTHING", &[
+                                                       &scid,
                                                        &announcement_signed
-                                               ]).await;
-                                       if result.is_err() {
-                                               panic!("error: {}", result.err().unwrap());
-                                       }
+                                               ])).await.unwrap().unwrap();
                                }
                                GossipMessage::ChannelUpdate(update) => {
-                                       let scid = update.contents.short_channel_id;
-                                       let scid_hex = hex_utils::hex_str(&scid.to_be_bytes());
-
-                                       let chain_hash = update.contents.chain_hash.as_ref();
-                                       let chain_hash_hex = hex_utils::hex_str(chain_hash);
+                                       let scid = update.contents.short_channel_id as i64;
 
                                        let timestamp = update.contents.timestamp as i64;
 
-                                       let channel_flags = update.contents.flags as i32;
-                                       let direction = channel_flags & 1;
-                                       let disable = (channel_flags & 2) > 0;
-
-                                       let composite_index = format!("{}:{}:{}", scid_hex, timestamp, direction);
+                                       let direction = (update.contents.flags & 1) == 1;
+                                       let disable = (update.contents.flags & 2) > 0;
 
                                        let cltv_expiry_delta = update.contents.cltv_expiry_delta as i32;
                                        let htlc_minimum_msat = update.contents.htlc_minimum_msat as i64;
@@ -193,13 +142,11 @@ impl GossipPersister {
                                        let htlc_maximum_msat = update.contents.htlc_maximum_msat as i64;
 
                                        // start with the type prefix, which is already known a priori
-                                       let mut update_signed = Vec::new(); // vec![1, 2];
+                                       let mut update_signed = Vec::new();
                                        update.write(&mut update_signed).unwrap();
 
-                                       let result = client
+                                       tokio::time::timeout(POSTGRES_INSERT_TIMEOUT, client
                                                .execute("INSERT INTO channel_updates (\
-                                                       composite_index, \
-                                                       chain_hash, \
                                                        short_channel_id, \
                                                        timestamp, \
                                                        channel_flags, \
@@ -211,12 +158,10 @@ impl GossipPersister {
                                                        fee_proportional_millionths, \
                                                        htlc_maximum_msat, \
                                                        blob_signed \
-                                               ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)  ON CONFLICT (composite_index) DO NOTHING", &[
-                                                       &composite_index,
-                                                       &chain_hash_hex,
-                                                       &scid_hex,
+                                               ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)  ON CONFLICT DO NOTHING", &[
+                                                       &scid,
                                                        &timestamp,
-                                                       &channel_flags,
+                                                       &(update.contents.flags as i16),
                                                        &direction,
                                                        &disable,
                                                        &cltv_expiry_delta,
@@ -225,10 +170,7 @@ impl GossipPersister {
                                                        &fee_proportional_millionths,
                                                        &htlc_maximum_msat,
                                                        &update_signed
-                                               ]).await;
-                                       if result.is_err() {
-                                               panic!("error: {}", result.err().unwrap());
-                                       }
+                                               ])).await.unwrap().unwrap();
                                }
                        }
                }
@@ -243,9 +185,10 @@ impl GossipPersister {
                        .truncate(true)
                        .open(&cache_path)
                        .unwrap();
-               self.network_graph.remove_stale_channels();
+               self.network_graph.remove_stale_channels_and_tracking();
                let mut writer = BufWriter::new(file);
                self.network_graph.write(&mut writer).unwrap();
+               writer.flush().unwrap();
                println!("Cached network graph!");
        }
 }