Fix multiplication overflow bug.
[rapid-gossip-sync-server] / src / snapshot.rs
index 96c1e4d28a08c7e6b99ec5c2b12a6f3dfd0ba75b..65f6fad03823b56cedd249c2eff87e137dcc56b9 100644 (file)
@@ -26,7 +26,7 @@ impl<L: Deref + Clone> Snapshotter<L> where L::Target: Logger {
                log_info!(self.logger, "Initiating snapshotting service");
 
                let snapshot_sync_day_factors = [1, 2, 3, 4, 5, 6, 7, 14, 21, u64::MAX];
-               let round_day_seconds = config::SNAPSHOT_CALCULATION_INTERVAL as u64;
+               const DAY_SECONDS: u64 = 60 * 60 * 24;
 
                let pending_snapshot_directory = format!("{}/snapshots_pending", cache_path());
                let pending_symlink_directory = format!("{}/symlinks_pending", cache_path());
@@ -38,7 +38,7 @@ impl<L: Deref + Clone> Snapshotter<L> where L::Target: Logger {
                loop {
                        // 1. get the current timestamp
                        let snapshot_generation_timestamp = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs();
-                       let reference_timestamp = Self::round_down_to_nearest_multiple(snapshot_generation_timestamp, round_day_seconds);
+                       let reference_timestamp = Self::round_down_to_nearest_multiple(snapshot_generation_timestamp, config::SNAPSHOT_CALCULATION_INTERVAL as u64);
                        log_info!(self.logger, "Capturing snapshots at {} for: {}", snapshot_generation_timestamp, reference_timestamp);
 
                        // 2. sleep until the next round 24 hours
@@ -70,7 +70,7 @@ impl<L: Deref + Clone> Snapshotter<L> where L::Target: Logger {
                        let mut snapshot_sync_timestamps: Vec<(u64, u64)> = Vec::new();
                        for factor in &snapshot_sync_day_factors {
                                // basically timestamp - day_seconds * factor
-                               let timestamp = reference_timestamp.saturating_sub(round_day_seconds.saturating_mul(factor.clone()));
+                               let timestamp = reference_timestamp.saturating_sub(DAY_SECONDS.saturating_mul(factor.clone()));
                                snapshot_sync_timestamps.push((factor.clone(), timestamp));
                        };
 
@@ -105,7 +105,9 @@ impl<L: Deref + Clone> Snapshotter<L> where L::Target: Logger {
                                symlink(&relative_dummy_snapshot_path, &dummy_symlink_path).unwrap();
                        }
 
-                       for i in 0..10_001u64 {
+                       // Number of intervals since Jan 1, 2022, a few months before RGS server was released.
+                       let symlink_count = (reference_timestamp - 1640995200) / config::SNAPSHOT_CALCULATION_INTERVAL as u64;
+                       for i in 0..symlink_count {
                                // let's create non-dummy-symlinks
 
                                // first, determine which snapshot range should be referenced
@@ -113,11 +115,27 @@ impl<L: Deref + Clone> Snapshotter<L> where L::Target: Logger {
                                        // special-case 0 to always refer to a full/initial sync
                                        u64::MAX
                                } else {
+                                       /*
+                                       We have snapshots for 6-day- and 7-day-intervals, but the next interval is
+                                       14 days. So if somebody requests an update with a timestamp that is 10 days old,
+                                       there is no longer a snapshot for that specific interval.
+
+                                       The correct snapshot will be the next highest interval, i. e. for 14 days.
+
+                                       The `snapshot_sync_day_factors` array is sorted ascendingly, so find() will
+                                       return on the first iteration that is at least equal to the requested interval.
+
+                                       Note, however, that the last value in the array is u64::max, which means that
+                                       multiplying it with DAY_SECONDS will overflow. To avoid that, we use
+                                       saturating_mul.
+                                        */
+
                                        // find min(x) in snapshot_sync_day_factors where x >= i
                                        snapshot_sync_day_factors.iter().find(|x| {
-                                               x >= &&i
+                                               DAY_SECONDS.saturating_mul(**x) >= i * config::SNAPSHOT_CALCULATION_INTERVAL as u64
                                        }).unwrap().clone()
                                };
+                               log_info!(self.logger, "i: {}, referenced day range: {}", i, referenced_day_range);
 
                                let snapshot_filename = snapshot_filenames_by_day_range.get(&referenced_day_range).unwrap();
                                let relative_snapshot_path = format!("{}/{}", relative_symlink_to_snapshot_path, snapshot_filename);
@@ -126,7 +144,7 @@ impl<L: Deref + Clone> Snapshotter<L> where L::Target: Logger {
                                        // special-case 0 to always refer to a full/initial sync
                                        0
                                } else {
-                                       reference_timestamp.saturating_sub(round_day_seconds.saturating_mul(i))
+                                       reference_timestamp.saturating_sub((config::SNAPSHOT_CALCULATION_INTERVAL as u64).saturating_mul(i))
                                };
                                let symlink_path = format!("{}/{}.bin", pending_symlink_directory, canonical_last_sync_timestamp);
 
@@ -149,8 +167,8 @@ impl<L: Deref + Clone> Snapshotter<L> where L::Target: Logger {
 
                        // constructing the snapshots may have taken a while
                        let current_time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs();
-                       let remainder = current_time % round_day_seconds;
-                       let time_until_next_day = round_day_seconds - remainder;
+                       let remainder = current_time % config::SNAPSHOT_CALCULATION_INTERVAL as u64;
+                       let time_until_next_day = config::SNAPSHOT_CALCULATION_INTERVAL as u64 - remainder;
 
                        log_info!(self.logger, "Sleeping until next snapshot capture: {}s", time_until_next_day);
                        // add in an extra five seconds to assure the rounding down works correctly