Allow setting custom gossip seen timestamps.
[rapid-gossip-sync-server] / src / persistence.rs
index 22abf0216d8575692a1e3e7f9b7d4ce66844df66..7b451b7ec252970050d908f231603730b13fd50a 100644 (file)
@@ -1,43 +1,38 @@
 use std::fs::OpenOptions;
 use std::io::{BufWriter, Write};
+use std::ops::Deref;
 use std::sync::Arc;
 use std::time::{Duration, Instant};
+use lightning::log_info;
 use lightning::routing::gossip::NetworkGraph;
 use lightning::util::logger::Logger;
 use lightning::util::ser::Writeable;
 use tokio::sync::mpsc;
-use tokio_postgres::NoTls;
 
 use crate::config;
 use crate::types::GossipMessage;
 
 const POSTGRES_INSERT_TIMEOUT: Duration = Duration::from_secs(15);
 
-pub(crate) struct GossipPersister<L: Logger> {
+pub(crate) struct GossipPersister<L: Deref> where L::Target: Logger {
        gossip_persistence_receiver: mpsc::Receiver<GossipMessage>,
-       network_graph: Arc<NetworkGraph<Arc<L>>>,
+       network_graph: Arc<NetworkGraph<L>>,
+       logger: L
 }
 
-impl<L: Logger> GossipPersister<L> {
-       pub fn new(network_graph: Arc<NetworkGraph<Arc<L>>>) -> (Self, mpsc::Sender<GossipMessage>) {
+impl<L: Deref> GossipPersister<L> where L::Target: Logger {
+       pub fn new(network_graph: Arc<NetworkGraph<L>>, logger: L) -> (Self, mpsc::Sender<GossipMessage>) {
                let (gossip_persistence_sender, gossip_persistence_receiver) =
                        mpsc::channel::<GossipMessage>(100);
                (GossipPersister {
                        gossip_persistence_receiver,
-                       network_graph
+                       network_graph,
+                       logger
                }, gossip_persistence_sender)
        }
 
        pub(crate) async fn persist_gossip(&mut self) {
-               let connection_config = config::db_connection_config();
-               let (mut client, connection) =
-                       connection_config.connect(NoTls).await.unwrap();
-
-               tokio::spawn(async move {
-                       if let Err(e) = connection.await {
-                               panic!("connection error: {}", e);
-                       }
-               });
+               let mut client = crate::connect_to_db().await;
 
                {
                        // initialize the database
@@ -53,6 +48,11 @@ impl<L: Logger> GossipPersister<L> {
                                config::upgrade_db(cur_schema[0].get(0), &mut client).await;
                        }
 
+                       let preparation = client.execute("set time zone UTC", &[]).await;
+                       if let Err(preparation_error) = preparation {
+                               panic!("db preparation error: {}", preparation_error);
+                       }
+
                        let initialization = client
                                .execute(
                                        // TODO: figure out a way to fix the id value without Postgres complaining about
@@ -100,7 +100,7 @@ impl<L: Logger> GossipPersister<L> {
                        i += 1; // count the persisted gossip messages
 
                        if latest_persistence_log.elapsed().as_secs() >= 60 {
-                               println!("Persisting gossip message #{}", i);
+                               log_info!(self.logger, "Persisting gossip message #{}", i);
                                latest_persistence_log = Instant::now();
                        }
 
@@ -111,7 +111,7 @@ impl<L: Logger> GossipPersister<L> {
                        }
 
                        match &gossip_message {
-                               GossipMessage::ChannelAnnouncement(announcement) => {
+                               GossipMessage::ChannelAnnouncement(announcement, _) => {
                                        let scid = announcement.contents.short_channel_id as i64;
 
                                        // start with the type prefix, which is already known a priori
@@ -127,7 +127,7 @@ impl<L: Logger> GossipPersister<L> {
                                                        &announcement_signed
                                                ])).await.unwrap().unwrap();
                                }
-                               GossipMessage::ChannelUpdate(update) => {
+                               GossipMessage::ChannelUpdate(update, seen_override) => {
                                        let scid = update.contents.short_channel_id as i64;
 
                                        let timestamp = update.contents.timestamp as i64;
@@ -146,10 +146,11 @@ impl<L: Logger> GossipPersister<L> {
                                        let mut update_signed = Vec::new();
                                        update.write(&mut update_signed).unwrap();
 
-                                       tokio::time::timeout(POSTGRES_INSERT_TIMEOUT, client
-                                               .execute("INSERT INTO channel_updates (\
+                                       let insertion_statement = if cfg!(test) {
+                                               "INSERT INTO channel_updates (\
                                                        short_channel_id, \
                                                        timestamp, \
+                                                       seen, \
                                                        channel_flags, \
                                                        direction, \
                                                        disable, \
@@ -159,9 +160,32 @@ impl<L: Logger> GossipPersister<L> {
                                                        fee_proportional_millionths, \
                                                        htlc_maximum_msat, \
                                                        blob_signed \
-                                               ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)  ON CONFLICT DO NOTHING", &[
+                                               ) VALUES ($1, $2, TO_TIMESTAMP($3), $4, $5, $6, $7, $8, $9, $10, $11, $12)  ON CONFLICT DO NOTHING"
+                                       } else {
+                                               "INSERT INTO channel_updates (\
+                                                       short_channel_id, \
+                                                       timestamp, \
+                                                       channel_flags, \
+                                                       direction, \
+                                                       disable, \
+                                                       cltv_expiry_delta, \
+                                                       htlc_minimum_msat, \
+                                                       fee_base_msat, \
+                                                       fee_proportional_millionths, \
+                                                       htlc_maximum_msat, \
+                                                       blob_signed \
+                                               ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)  ON CONFLICT DO NOTHING"
+                                       };
+
+                                       // this may not be used outside test cfg
+                                       let _seen_timestamp = seen_override.unwrap_or(timestamp as u32) as f64;
+
+                                       tokio::time::timeout(POSTGRES_INSERT_TIMEOUT, client
+                                               .execute(insertion_statement, &[
                                                        &scid,
                                                        &timestamp,
+                                                       #[cfg(test)]
+                                                               &_seen_timestamp,
                                                        &(update.contents.flags as i16),
                                                        &direction,
                                                        &disable,
@@ -178,7 +202,7 @@ impl<L: Logger> GossipPersister<L> {
        }
 
        fn persist_network_graph(&self) {
-               println!("Caching network graph…");
+               log_info!(self.logger, "Caching network graph…");
                let cache_path = config::network_graph_cache_path();
                let file = OpenOptions::new()
                        .create(true)
@@ -190,6 +214,6 @@ impl<L: Logger> GossipPersister<L> {
                let mut writer = BufWriter::new(file);
                self.network_graph.write(&mut writer).unwrap();
                writer.flush().unwrap();
-               println!("Cached network graph!");
+               log_info!(self.logger, "Cached network graph!");
        }
 }