Allow passing a timestamp override to snapshot calculation.
[rapid-gossip-sync-server] / src / lookup.rs
index 5504279c63d2198b731856e96ab2f6b01233e444..76b7c2089d80c8b6a8f3484ec46078a16e2e78fb 100644 (file)
@@ -76,7 +76,7 @@ impl Default for DirectedUpdateDelta {
 /// whether they had been seen before.
 /// Also include all announcements for which the first update was announced
 /// after `last_sync_timestamp`
-pub(super) async fn fetch_channel_announcements<L: Deref>(delta_set: &mut DeltaSet, network_graph: Arc<NetworkGraph<L>>, client: &Client, last_sync_timestamp: u32, logger: L) where L::Target: Logger {
+pub(super) async fn fetch_channel_announcements<L: Deref>(delta_set: &mut DeltaSet, network_graph: Arc<NetworkGraph<L>>, client: &Client, last_sync_timestamp: u32, snapshot_calculation_time: Option<SystemTime>, logger: L) where L::Target: Logger {
        log_info!(logger, "Obtaining channel ids from network graph");
        let channel_ids = {
                let read_only_graph = network_graph.read_only();
@@ -92,15 +92,27 @@ pub(super) async fn fetch_channel_announcements<L: Deref>(delta_set: &mut DeltaS
        log_info!(logger, "Last sync timestamp: {}", last_sync_timestamp);
        let last_sync_timestamp_float = last_sync_timestamp as f64;
 
-       let current_time = SystemTime::now();
+       let current_time = snapshot_calculation_time.unwrap_or(SystemTime::now());
        let current_timestamp = current_time.duration_since(UNIX_EPOCH).unwrap().as_secs();
        log_info!(logger, "Current timestamp: {}", current_timestamp);
 
-       let current_day = current_timestamp / (24 * 3600);
-       let snapshot_scope = current_timestamp.saturating_sub(last_sync_timestamp as u64);
-       log_debug!(logger, "Current day index: {}", current_day);
-       log_debug!(logger, "Snapshot scope: {}s", snapshot_scope);
-       let include_reminders = ((current_day % 5) == 0 || snapshot_scope > (40 * 3600));
+       let include_reminders = {
+               let current_hour = current_timestamp / 3600;
+               let current_day = current_timestamp / (24 * 3600);
+
+               log_debug!(logger, "Current day index: {}", current_day);
+               log_debug!(logger, "Current hour: {}", current_hour);
+
+               // anytime between 11pm and 1am
+               let is_reminder_hour = current_hour < 2 || current_hour > 22;
+               let is_reminder_day = (current_day % 5) == 0;
+
+               let snapshot_scope = current_timestamp.saturating_sub(last_sync_timestamp as u64);
+               let is_reminder_scope = snapshot_scope > (40 * 3600);
+               log_debug!(logger, "Snapshot scope: {}s", snapshot_scope);
+
+               (is_reminder_hour && is_reminder_day) || is_reminder_scope
+       };
 
        log_info!(logger, "Obtaining corresponding database entries");
        // get all the channel announcements that are currently in the network graph