Kaynağa Gözat

event_graph: fix a secs/millis bug that caused a newly generated genesis after pruning get a wrong timestamp and sleep for wrong amount of seconds, and validate events accordingly

dasman 1 yıl önce
ebeveyn
işleme
d21438223c
2 değiştirilmiş dosya ile 7 ekleme ve 4 silme
  1. 5 2
      src/event_graph/event.rs
  2. 2 2
      src/event_graph/util.rs

+ 5 - 2
src/event_graph/event.rs

@@ -103,8 +103,11 @@ impl Event {
             return Ok(false)
             return Ok(false)
         }
         }
 
 
+        let timestamp =
+            if self.timestamp > 1e10 as u64 { self.timestamp / 1000 } else { self.timestamp };
+
         // Check if the event timestamp is after genesis timestamp
         // Check if the event timestamp is after genesis timestamp
-        if self.timestamp < genesis_timestamp - EVENT_TIME_DRIFT / 1000 {
+        if timestamp < genesis_timestamp - EVENT_TIME_DRIFT / 1000 {
             return Ok(false)
             return Ok(false)
         }
         }
 
 
@@ -112,7 +115,7 @@ impl Event {
         // is after the next genesis timestamp
         // is after the next genesis timestamp
         if days_rotation > 0 {
         if days_rotation > 0 {
             let next_genesis_timestamp = next_rotation_timestamp(INITIAL_GENESIS, days_rotation);
             let next_genesis_timestamp = next_rotation_timestamp(INITIAL_GENESIS, days_rotation);
-            if self.timestamp > next_genesis_timestamp + EVENT_TIME_DRIFT / 1000 {
+            if timestamp > next_genesis_timestamp + EVENT_TIME_DRIFT / 1000 {
                 return Ok(false)
                 return Ok(false)
             }
             }
         }
         }

+ 2 - 2
src/event_graph/util.rs

@@ -52,7 +52,7 @@ pub(super) fn midnight_timestamp(days: i64) -> u64 {
     let cur_midnight = (now / DAY) * DAY;
     let cur_midnight = (now / DAY) * DAY;
 
 
     // Adjust for days_from_now
     // Adjust for days_from_now
-    (cur_midnight + (DAY * days)) as u64
+    ((cur_midnight + (DAY * days)) / 1000) as u64
 }
 }
 
 
 /// Calculate the number of days since a given midnight timestamp.
 /// Calculate the number of days since a given midnight timestamp.
@@ -103,7 +103,7 @@ pub fn seconds_until_next_rotation(next_rotation: u64) -> u64 {
     // Store `now` in a variable in order to avoid a TOCTOU error.
     // Store `now` in a variable in order to avoid a TOCTOU error.
     // There may be a drift of one second between this panic check and
     // There may be a drift of one second between this panic check and
     // the return value if we get unlucky.
     // the return value if we get unlucky.
-    let now = UNIX_EPOCH.elapsed().unwrap().as_millis() as u64;
+    let now = UNIX_EPOCH.elapsed().unwrap().as_secs() as u64;
     if next_rotation < now {
     if next_rotation < now {
         panic!("Next rotation timestamp is in the past");
         panic!("Next rotation timestamp is in the past");
     }
     }