Require DB insertions to complete in fifteen seconds
[rapid-gossip-sync-server] / src / persistence.rs
index fcc3f300a34f0fe472df9dcc61f3d54228fcb585..ac667330c6e0c445d0e7e82f81e2649ba12111db 100644 (file)
@@ -7,9 +7,11 @@ 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>,
        network_graph: Arc<NetworkGraph<TestLogger>>,
@@ -110,28 +112,19 @@ impl GossipPersister {
                        match &gossip_message {
                                GossipMessage::ChannelAnnouncement(announcement) => {
                                        let scid = announcement.contents.short_channel_id as i64;
-                                       // 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;
 
                                        // start with the type prefix, which is already known a priori
                                        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, \
                                                        announcement_signed \
-                                               ) VALUES ($1, $2, $3) ON CONFLICT (short_channel_id) DO NOTHING", &[
+                                               ) VALUES ($1, $2) ON CONFLICT (short_channel_id) DO NOTHING", &[
                                                        &scid,
-                                                       &block_height,
                                                        &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 as i64;
@@ -141,8 +134,6 @@ impl GossipPersister {
                                        let direction = (update.contents.flags & 1) == 1;
                                        let disable = (update.contents.flags & 2) > 0;
 
-                                       let composite_index = hex_utils::to_composite_index(scid, timestamp, direction);
-
                                        let cltv_expiry_delta = update.contents.cltv_expiry_delta as i32;
                                        let htlc_minimum_msat = update.contents.htlc_minimum_msat as i64;
                                        let fee_base_msat = update.contents.fee_base_msat as i32;
@@ -154,9 +145,8 @@ impl GossipPersister {
                                        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, \
                                                        short_channel_id, \
                                                        timestamp, \
                                                        channel_flags, \
@@ -168,11 +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)  ON CONFLICT (composite_index) DO NOTHING", &[
-                                                       &composite_index,
+                                               ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)  ON CONFLICT DO NOTHING", &[
                                                        &scid,
                                                        &timestamp,
-                                                       &(update.contents.flags as i32),
+                                                       &(update.contents.flags as i16),
                                                        &direction,
                                                        &disable,
                                                        &cltv_expiry_delta,
@@ -181,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();
                                }
                        }
                }
@@ -199,7 +185,7 @@ 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();