Explorar o código

event-graph: fix proto moving window clean + test

darkfi hai 1 ano
pai
achega
aae9126fd6
Modificáronse 1 ficheiros con 21 adicións e 1 borrados
  1. 21 1
      src/event_graph/proto.rs

+ 21 - 1
src/event_graph/proto.rs

@@ -62,7 +62,12 @@ impl MovingWindow {
     /// Clean out expired timestamps from the window.
     fn clean(&mut self) {
         while let Some(ts) = self.times.front() {
-            if ts.elapsed().unwrap() < self.expiry_time {
+            let Ok(elapsed) = ts.elapsed() else {
+                debug!(target: "event_graph::protocol::MovingWindow::clean()", "Timestamp [{}] is in future. Removing...", ts);
+                let _ = self.times.pop_front();
+                continue
+            };
+            if elapsed < self.expiry_time {
                 break
             }
             let _ = self.times.pop_front();
@@ -611,3 +616,18 @@ impl ProtocolEventGraph {
         }
     }
 }
+
+#[cfg(test)]
+mod test {
+    use super::*;
+    use std::time::UNIX_EPOCH;
+
+    #[test]
+    fn test_eventgraph_moving_window_clean_future() {
+        let mut window = MovingWindow::new(NanoTimestamp::from_secs(60));
+        let future = UNIX_EPOCH.elapsed().unwrap().as_secs() + 100;
+        window.times.push_back(NanoTimestamp::from_secs(future.into()));
+        window.clean();
+        assert_eq!(window.count(), 0);
+    }
+}