瀏覽代碼

src/event_graph: use Timestamp struct as events timestamp

Dastan-glitch 3 年之前
父節點
當前提交
4e9071a08a

+ 1 - 1
bin/darkirc/src/irc/client.rs

@@ -558,7 +558,7 @@ impl<C: AsyncRead + AsyncWrite + Send + Unpin + 'static> IrcClient<C> {
         }
         }
         // Process missed messages if any (sorted by event's timestamp)
         // Process missed messages if any (sorted by event's timestamp)
         let mut hash_vec = self.missed_events.lock().await.clone();
         let mut hash_vec = self.missed_events.lock().await.clone();
-        hash_vec.sort_by(|a, b| a.timestamp.cmp(&b.timestamp));
+        hash_vec.sort_by(|a, b| a.timestamp.0.cmp(&b.timestamp.0));
 
 
         for event in hash_vec {
         for event in hash_vec {
             let mut action = event.action.clone();
             let mut action = event.action.clone();

+ 2 - 3
bin/darkirc/src/irc/server.rs

@@ -28,14 +28,13 @@ use log::{error, info};
 
 
 use darkfi::{
 use darkfi::{
     event_graph::{
     event_graph::{
-        get_current_time,
         model::{Event, EventId, ModelPtr},
         model::{Event, EventId, ModelPtr},
         protocol_event::{Seen, SeenPtr, UnreadEventsPtr},
         protocol_event::{Seen, SeenPtr, UnreadEventsPtr},
         view::ViewPtr,
         view::ViewPtr,
     },
     },
     net::P2pPtr,
     net::P2pPtr,
     system::SubscriberPtr,
     system::SubscriberPtr,
-    util::path::expand_path,
+    util::{path::expand_path, time::Timestamp},
     Error, Result,
     Error, Result,
 };
 };
 
 
@@ -169,7 +168,7 @@ impl IrcServer {
                     let event = Event {
                     let event = Event {
                         previous_event_hash: model.lock().await.get_head_hash(),
                         previous_event_hash: model.lock().await.get_head_hash(),
                         action: msg.clone(),
                         action: msg.clone(),
-                        timestamp: get_current_time(),
+                        timestamp: Timestamp::current_time(),
                         read_confirms: 0,
                         read_confirms: 0,
                     };
                     };
 
 

+ 2 - 2
bin/genev/genevd/src/rpc.rs

@@ -23,7 +23,6 @@ use serde_json::{json, Value};
 
 
 use darkfi::{
 use darkfi::{
     event_graph::{
     event_graph::{
-        get_current_time,
         model::{Event, EventId, ModelPtr},
         model::{Event, EventId, ModelPtr},
         protocol_event::{SeenPtr, UnreadEvents},
         protocol_event::{SeenPtr, UnreadEvents},
     },
     },
@@ -32,6 +31,7 @@ use darkfi::{
         jsonrpc::{ErrorCode, JsonError, JsonRequest, JsonResponse, JsonResult},
         jsonrpc::{ErrorCode, JsonError, JsonRequest, JsonResponse, JsonResult},
         server::RequestHandler,
         server::RequestHandler,
     },
     },
+    util::time::Timestamp,
 };
 };
 
 
 use crate::genevent::GenEvent;
 use crate::genevent::GenEvent;
@@ -105,7 +105,7 @@ impl JsonRpcInterface {
         let event = Event {
         let event = Event {
             previous_event_hash: self.model.lock().await.get_head_hash(),
             previous_event_hash: self.model.lock().await.get_head_hash(),
             action: genevent,
             action: genevent,
-            timestamp: get_current_time(),
+            timestamp: Timestamp::current_time(),
             read_confirms: 0,
             read_confirms: 0,
         };
         };
 
 

+ 2 - 3
bin/tau/taud/src/main.rs

@@ -41,7 +41,6 @@ use darkfi::{
     async_daemonize,
     async_daemonize,
     event_graph::{
     event_graph::{
         events_queue::EventsQueue,
         events_queue::EventsQueue,
-        get_current_time,
         model::{Event, EventId, Model, ModelPtr},
         model::{Event, EventId, Model, ModelPtr},
         protocol_event::{ProtocolEvent, Seen, SeenPtr, UnreadEvents},
         protocol_event::{ProtocolEvent, Seen, SeenPtr, UnreadEvents},
         view::{View, ViewPtr},
         view::{View, ViewPtr},
@@ -49,7 +48,7 @@ use darkfi::{
     },
     },
     net::{self, P2pPtr},
     net::{self, P2pPtr},
     rpc::server::listen_and_serve,
     rpc::server::listen_and_serve,
-    util::path::expand_path,
+    util::{path::expand_path, time::Timestamp},
     Error, Result,
     Error, Result,
 };
 };
 
 
@@ -164,7 +163,7 @@ async fn start_sync_loop(
                     let event = Event {
                     let event = Event {
                         previous_event_hash: model.lock().await.get_head_hash(),
                         previous_event_hash: model.lock().await.get_head_hash(),
                         action: encrypted_task,
                         action: encrypted_task,
-                        timestamp: get_current_time(),
+                        timestamp: Timestamp::current_time(),
                         read_confirms: 0,
                         read_confirms: 0,
                     };
                     };
 
 

+ 0 - 10
src/event_graph/mod.rs

@@ -30,13 +30,3 @@ pub trait EventMsg {
 pub fn gen_id(len: usize) -> String {
 pub fn gen_id(len: usize) -> String {
     thread_rng().sample_iter(&Alphanumeric).take(len).map(char::from).collect()
     thread_rng().sample_iter(&Alphanumeric).take(len).map(char::from).collect()
 }
 }
-
-pub fn get_current_time() -> u64 {
-    let start = std::time::SystemTime::now();
-    start
-        .duration_since(std::time::UNIX_EPOCH)
-        .expect("Time went backwards")
-        .as_millis()
-        .try_into()
-        .unwrap()
-}

+ 11 - 6
src/event_graph/model.rs

@@ -23,7 +23,7 @@ use darkfi_serial::{Decodable, Encodable, SerialDecodable, SerialEncodable};
 use log::error;
 use log::error;
 use ripemd::{Digest, Ripemd256};
 use ripemd::{Digest, Ripemd256};
 
 
-use crate::event_graph::events_queue::EventsQueuePtr;
+use crate::{event_graph::events_queue::EventsQueuePtr, util::time::Timestamp};
 
 
 use super::EventMsg;
 use super::EventMsg;
 
 
@@ -36,7 +36,7 @@ const MAX_HEIGHT: u32 = 300;
 pub struct Event<T: Send + Sync> {
 pub struct Event<T: Send + Sync> {
     pub previous_event_hash: EventId,
     pub previous_event_hash: EventId,
     pub action: T,
     pub action: T,
-    pub timestamp: u64,
+    pub timestamp: Timestamp,
     pub read_confirms: u8,
     pub read_confirms: u8,
 }
 }
 
 
@@ -87,7 +87,7 @@ where
             event: Event {
             event: Event {
                 previous_event_hash: [0u8; 32],
                 previous_event_hash: [0u8; 32],
                 action: T::new(),
                 action: T::new(),
-                timestamp: 1674512021323,
+                timestamp: Timestamp(1674512021323),
                 read_confirms: 0,
                 read_confirms: 0,
             },
             },
             children: Vec::new(),
             children: Vec::new(),
@@ -400,7 +400,7 @@ where
 #[cfg(test)]
 #[cfg(test)]
 mod tests {
 mod tests {
     use super::*;
     use super::*;
-    use crate::event_graph::{events_queue::EventsQueue, get_current_time};
+    use crate::event_graph::events_queue::EventsQueue;
 
 
     #[derive(SerialEncodable, SerialDecodable, Clone, Debug)]
     #[derive(SerialEncodable, SerialDecodable, Clone, Debug)]
     pub struct PrivMsgEvent {
     pub struct PrivMsgEvent {
@@ -426,7 +426,12 @@ mod tests {
     }
     }
 
 
     fn create_message(previous_event_hash: EventId, timestamp: u64) -> Event<PrivMsgEvent> {
     fn create_message(previous_event_hash: EventId, timestamp: u64) -> Event<PrivMsgEvent> {
-        Event { previous_event_hash, action: PrivMsgEvent::new(), timestamp, read_confirms: 4 }
+        Event {
+            previous_event_hash,
+            action: PrivMsgEvent::new(),
+            timestamp: Timestamp(timestamp),
+            read_confirms: 4,
+        }
     }
     }
 
 
     /* THIS IS FAILING
     /* THIS IS FAILING
@@ -615,7 +620,7 @@ mod tests {
         let model = Model::new(events_queue);
         let model = Model::new(events_queue);
         let root_id = model.current_root;
         let root_id = model.current_root;
 
 
-        let timestamp = get_current_time() + 1;
+        let timestamp = Timestamp::current_time().0 + 1;
         let event = create_message(root_id, timestamp);
         let event = create_message(root_id, timestamp);
         let mut event2 = event.clone();
         let mut event2 = event.clone();
 
 

+ 3 - 3
src/event_graph/protocol_event.rs

@@ -27,11 +27,11 @@ use darkfi_serial::{Decodable, Encodable, SerialDecodable, SerialEncodable};
 use log::debug;
 use log::debug;
 use rand::{rngs::OsRng, RngCore};
 use rand::{rngs::OsRng, RngCore};
 
 
-use super::{get_current_time, EventMsg};
+use super::EventMsg;
 use crate::{
 use crate::{
     event_graph::model::{Event, EventId, ModelPtr},
     event_graph::model::{Event, EventId, ModelPtr},
     net,
     net,
-    util::async_util::sleep,
+    util::{async_util::sleep, time::Timestamp},
     Result,
     Result,
 };
 };
 
 
@@ -152,7 +152,7 @@ where
         // prune expired events
         // prune expired events
         let mut prune_ids = vec![];
         let mut prune_ids = vec![];
         for (id, e) in self.events.iter() {
         for (id, e) in self.events.iter() {
-            if e.timestamp + (UNREAD_EVENT_EXPIRE_TIME * 1000) < get_current_time() {
+            if e.timestamp.0 + (UNREAD_EVENT_EXPIRE_TIME * 1000) < Timestamp::current_time().0 {
                 prune_ids.push(*id);
                 prune_ids.push(*id);
             }
             }
         }
         }