Remove unused `consider_intermediate_updates` flag..optimizing query
[rapid-gossip-sync-server] / src / lookup.rs
index ce3f40c37c814150f8997aec3238beff47914085..0534c1c8a7e82922bc240eab0be214f06d87eec8 100644 (file)
@@ -59,7 +59,7 @@ impl Default for DirectedUpdateDelta {
                        last_update_before_seen: None,
                        mutated_properties: MutatedProperties::default(),
                        latest_update_after_seen: None,
-                       serialization_update_flags: None
+                       serialization_update_flags: None,
                }
        }
 }
@@ -75,6 +75,7 @@ pub(super) async fn connect_to_db() -> (Client, Connection<Socket, NoTlsStream>)
 /// after `last_sync_timestamp`
 pub(super) async fn fetch_channel_announcements(delta_set: &mut DeltaSet, network_graph: Arc<NetworkGraph<TestLogger>>, client: &Client, last_sync_timestamp: u32) {
        println!("Obtaining channel ids from network graph");
+       let last_sync_timestamp_object = SystemTime::UNIX_EPOCH.add(Duration::from_secs(last_sync_timestamp as u64));
        let channel_ids = {
                let read_only_graph = network_graph.read_only();
                println!("Retrieved read-only network graph copy");
@@ -105,113 +106,121 @@ pub(super) async fn fetch_channel_announcements(delta_set: &mut DeltaSet, networ
                });
        }
 
-       println!("Annotating channel announcements whose oldest channel update in a given direction occurred after the last sync");
-       /// Steps:
-       /// — Obtain all updates, distinct by (scid, direction), ordered by seen DESC // to find the oldest update in a given direction
-       /// — From those updates, select distinct by (scid), ordered by seen DESC (to obtain the newer one per direction)
-       /// This will allow us to mark the first time updates in both directions were seen
-
-       // here is where the channels whose first update in either direction occurred after
-       // `last_seen_timestamp` are added to the selection
-       let newer_oldest_directional_updates = client.query("
-               SELECT DISTINCT ON (short_channel_id) *
-               FROM (
-                       SELECT DISTINCT ON (short_channel_id, direction) blob_signed
-                       FROM channel_updates
-                       WHERE short_channel_id = any($1)
-                       ORDER BY seen ASC, short_channel_id ASC, direction ASC
-               ) AS directional_last_seens
-               ORDER BY short_channel_id ASC, seen DESC
-       ", &[&channel_ids]).await.unwrap();
-
-       for current_row in newer_oldest_directional_updates {
-               let blob: Vec<u8> = current_row.get("blob_signed");
-               let mut readable = Cursor::new(blob);
-               let unsigned_update = ChannelUpdate::read(&mut readable).unwrap().contents;
-               let scid = unsigned_update.short_channel_id;
-               let current_seen_timestamp_object: SystemTime = current_row.get("seen");
-               let current_seen_timestamp: u32 = current_seen_timestamp_object.duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs() as u32;
+       {
+               // THIS STEP IS USED TO DETERMINE IF A CHANNEL SHOULD BE OMITTED FROM THE DELTA
+
+               println!("Annotating channel announcements whose oldest channel update in a given direction occurred after the last sync");
+               // Steps:
+               // — Obtain all updates, distinct by (scid, direction), ordered by seen DESC // to find the oldest update in a given direction
+               // — From those updates, select distinct by (scid), ordered by seen DESC (to obtain the newer one per direction)
+               // This will allow us to mark the first time updates in both directions were seen
+
+               // here is where the channels whose first update in either direction occurred after
+               // `last_seen_timestamp` are added to the selection
+               let newer_oldest_directional_updates = client.query("
+                       SELECT * FROM (
+                               SELECT DISTINCT ON (short_channel_id) *
+                               FROM (
+                                       SELECT DISTINCT ON (short_channel_id, direction) short_channel_id, seen
+                                       FROM channel_updates
+                                       WHERE short_channel_id = any($1)
+                                       ORDER BY short_channel_id ASC, direction ASC, seen ASC
+                               ) AS directional_last_seens
+                               ORDER BY short_channel_id ASC, seen DESC
+                       ) AS distinct_chans
+                       WHERE distinct_chans.seen >= $2
+                       ", &[&channel_ids, &last_sync_timestamp_object]).await.unwrap();
+
+               for current_row in newer_oldest_directional_updates {
+                       let scid: i64 = current_row.get("short_channel_id");
+                       let current_seen_timestamp_object: SystemTime = current_row.get("seen");
+                       let current_seen_timestamp: u32 = current_seen_timestamp_object.duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs() as u32;
 
-               if current_seen_timestamp > last_sync_timestamp {
                        // the newer of the two oldest seen directional updates came after last sync timestamp
-                       let current_channel_delta = delta_set.entry(scid).or_insert(ChannelDelta::default());
+                       let current_channel_delta = delta_set.entry(scid as u64).or_insert(ChannelDelta::default());
                        // first time a channel was seen in both directions
                        (*current_channel_delta).first_bidirectional_updates_seen = Some(current_seen_timestamp);
                }
        }
 
-       println!("Annotating channel announcements whose latest channel update in a given direction occurred more than six days ago");
-       /// Steps:
-       /// — Obtain all updates, distinct by (scid, direction), ordered by seen DESC
-       /// — From those updates, select distinct by (scid), ordered by seen ASC (to obtain the older one per direction)
-       let current_timestamp = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs() as u32;
-       let reminder_threshold_timestamp = current_timestamp.saturating_sub(config::CHANNEL_REMINDER_AGE);
-       let mut channels_requiring_reminders: Vec<i64> = vec![];
-
-       let older_latest_directional_updates = client.query("
-               SELECT DISTINCT ON (short_channel_id) *
-               FROM (
-                       SELECT DISTINCT ON (short_channel_id, direction) *
-                       FROM channel_updates
-                       WHERE short_channel_id = any($1)
-                       ORDER BY short_channel_id ASC, direction ASC, seen DESC
-               ) AS directional_last_seens
-               ORDER BY short_channel_id ASC, seen ASC
-       ", &[&channel_ids]).await.unwrap();
+       {
+               // THIS STEP IS USED TO DETERMINE IF A REMINDER UPDATE SHOULD BE SENT
 
-       for current_row in older_latest_directional_updates {
-               let blob: Vec<u8> = current_row.get("blob_signed");
-               let mut readable = Cursor::new(blob);
-               let unsigned_update = ChannelUpdate::read(&mut readable).unwrap().contents;
-               let scid = unsigned_update.short_channel_id;
-               let current_seen_timestamp_object: SystemTime = current_row.get("seen");
-               let current_seen_timestamp: u32 = current_seen_timestamp_object.duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs() as u32;
+               println!("Annotating channel announcements whose latest channel update in a given direction occurred more than six days ago");
+               // Steps:
+               // — Obtain all updates, distinct by (scid, direction), ordered by seen DESC
+               // — From those updates, select distinct by (scid), ordered by seen ASC (to obtain the older one per direction)
+               let reminder_threshold_timestamp = SystemTime::now().checked_sub(config::CHANNEL_REMINDER_AGE).unwrap();
+               let read_only_graph = network_graph.read_only();
+
+               let older_latest_directional_updates = client.query("
+                       SELECT short_channel_id FROM (
+                               SELECT DISTINCT ON (short_channel_id) *
+                               FROM (
+                                       SELECT DISTINCT ON (short_channel_id, direction) short_channel_id, seen
+                                       FROM channel_updates
+                                       WHERE short_channel_id = any($1)
+                                       ORDER BY short_channel_id ASC, direction ASC, seen DESC
+                               ) AS directional_last_seens
+                               ORDER BY short_channel_id ASC, seen ASC
+                       ) AS distinct_chans
+                       WHERE distinct_chans.seen <= $2
+                       ", &[&channel_ids, &reminder_threshold_timestamp]).await.unwrap();
+
+               for current_row in older_latest_directional_updates {
+                       let scid: i64 = current_row.get("short_channel_id");
 
-               if current_seen_timestamp <= reminder_threshold_timestamp {
                        // annotate this channel as requiring that reminders be sent to the client
-                       let current_channel_delta = delta_set.entry(scid).or_insert(ChannelDelta::default());
+                       let current_channel_delta = delta_set.entry(scid as u64).or_insert(ChannelDelta::default());
 
                        // way might be able to get away with not using this
                        (*current_channel_delta).requires_reminder = true;
 
-                       // get the latest seen update in both directions for this channel
-                       channels_requiring_reminders.push(scid as i64);
+                       if let Some(current_channel_info) = read_only_graph.channel(scid as u64) {
+                               if current_channel_info.one_to_two.is_none() || current_channel_info.two_to_one.is_none() {
+                                       // we don't send reminders if we don't have bidirectional update data
+                                       continue;
+                               }
+
+                               if let Some(info) = current_channel_info.one_to_two.as_ref() {
+                                       let flags: u8 = if info.enabled { 0 } else { 2 };
+                                       let current_update = (*current_channel_delta).updates.0.get_or_insert(DirectedUpdateDelta::default());
+                                       current_update.serialization_update_flags = Some(flags);
+                               }
+
+                               if let Some(info) = current_channel_info.two_to_one.as_ref() {
+                                       let flags: u8 = if info.enabled { 1 } else { 3 };
+                                       let current_update = (*current_channel_delta).updates.1.get_or_insert(DirectedUpdateDelta::default());
+                                       current_update.serialization_update_flags = Some(flags);
+                               }
+                       } else {
+                               // we don't send reminders if we don't have the channel
+                               continue;
+                       }
                }
        }
-
-       println!("Fetching latest update data for channels requiring reminders");
-       let latest_reminder_updates = client.query("
-               SELECT DISTINCT ON (short_channel_id, direction) *
-               FROM channel_updates
-               WHERE short_channel_id = any($1)
-               ORDER BY short_channel_id ASC, direction ASC, seen DESC
-       ", &[&channels_requiring_reminders]).await.unwrap();
-
-       for current_update in latest_reminder_updates {
-               let blob: Vec<u8> = current_update.get("blob_signed");
-               let mut readable = Cursor::new(blob);
-               let unsigned_update = ChannelUpdate::read(&mut readable).unwrap().contents;
-               let scid = unsigned_update.short_channel_id;
-               let direction: bool = current_update.get("direction");
-
-               let current_channel_delta = delta_set.entry(scid).or_insert(ChannelDelta::default());
-               let update_delta = if !direction {
-                       (*current_channel_delta).updates.0.get_or_insert(DirectedUpdateDelta::default())
-               } else {
-                       (*current_channel_delta).updates.1.get_or_insert(DirectedUpdateDelta::default())
-               };
-               update_delta.serialization_update_flags = Some(unsigned_update.flags);
-       }
 }
 
-pub(super) async fn fetch_channel_updates(delta_set: &mut DeltaSet, client: &Client, last_sync_timestamp: u32, consider_intermediate_updates: bool) {
+pub(super) async fn fetch_channel_updates(delta_set: &mut DeltaSet, client: &Client, last_sync_timestamp: u32) {
        let start = Instant::now();
        let last_sync_timestamp_object = SystemTime::UNIX_EPOCH.add(Duration::from_secs(last_sync_timestamp as u64));
 
        // get the latest channel update in each direction prior to last_sync_timestamp, provided
        // there was an update in either direction that happened after the last sync (to avoid
        // collecting too many reference updates)
-       let reference_rows = client.query("SELECT DISTINCT ON (short_channel_id, direction) id, direction, blob_signed FROM channel_updates WHERE seen < $1 AND short_channel_id IN (SELECT short_channel_id FROM channel_updates WHERE seen >= $1 GROUP BY short_channel_id) ORDER BY short_channel_id ASC, direction ASC, seen DESC", &[&last_sync_timestamp_object]).await.unwrap();
+       let reference_rows = client.query("
+               SELECT id, direction, blob_signed FROM channel_updates
+               WHERE id IN (
+                       SELECT DISTINCT ON (short_channel_id, direction) id
+                       FROM channel_updates
+                       WHERE seen < $1
+                       ORDER BY short_channel_id ASC, direction ASC, seen DESC
+               ) AND short_channel_id IN (
+                       SELECT DISTINCT ON (short_channel_id) short_channel_id
+                       FROM channel_updates
+                       WHERE seen >= $1
+               )
+               ", &[&last_sync_timestamp_object]).await.unwrap();
 
        println!("Fetched reference rows ({}): {:?}", reference_rows.len(), start.elapsed());
 
@@ -244,13 +253,11 @@ pub(super) async fn fetch_channel_updates(delta_set: &mut DeltaSet, client: &Cli
        // (to calculate the set of mutated fields for snapshotting, where intermediate updates may
        // have been omitted)
 
-       let mut intermediate_update_prefix = "";
-       if !consider_intermediate_updates {
-               intermediate_update_prefix = "DISTINCT ON (short_channel_id, direction)";
-       }
-
-       let query_string = format!("SELECT {} id, direction, blob_signed, seen FROM channel_updates WHERE seen >= $1 ORDER BY short_channel_id ASC, direction ASC, seen DESC", intermediate_update_prefix);
-       let intermediate_updates = client.query(&query_string, &[&last_sync_timestamp_object]).await.unwrap();
+       let intermediate_updates = client.query("
+               SELECT id, direction, blob_signed, seen
+               FROM channel_updates
+               WHERE seen >= $1
+               ", &[&last_sync_timestamp_object]).await.unwrap();
        println!("Fetched intermediate rows ({}): {:?}", intermediate_updates.len(), start.elapsed());
 
        let mut previous_scid = u64::MAX;