Explorar o código

event_graph: clean up and add some comments

Dastan-glitch %!s(int64=2) %!d(string=hai) anos
pai
achega
0c2282ed58
Modificáronse 3 ficheiros con 11 adicións e 29 borrados
  1. 1 23
      src/event_graph/model.rs
  2. 10 4
      src/event_graph/protocol_event.rs
  3. 0 2
      src/event_graph/view.rs

+ 1 - 23
src/event_graph/model.rs

@@ -137,29 +137,6 @@ where
         Ok(())
         Ok(())
     }
     }
 
 
-    pub fn reset_root(&mut self, timestamp: Timestamp) {
-        let root_node = EventNode {
-            parent: None,
-            event: Event {
-                previous_event_hash: blake3::hash(b""), // This is a blake3 hash of NULL
-                action: T::new(),
-                timestamp,
-            },
-            children: Vec::new(),
-        };
-
-        let root_node_id = root_node.event.hash();
-
-        let mut event_map = HashMap::new();
-        event_map.insert(root_node_id, root_node);
-
-        self.current_root = root_node_id;
-        self.orphans = HashMap::new();
-        self.event_map = event_map;
-
-        info!("reset current root to: {:?}", self.current_root);
-    }
-
     /// Loops through all events, checks if the event is older than the
     /// Loops through all events, checks if the event is older than the
     /// given timestamp, if older then it gets removed from the tree,
     /// given timestamp, if older then it gets removed from the tree,
     /// and reorganizes the resulted tree so the oldest event(s) is
     /// and reorganizes the resulted tree so the oldest event(s) is
@@ -217,6 +194,7 @@ where
         Ok(())
         Ok(())
     }
     }
 
 
+    /// Is event (basically orphan) older than ORPHAN_EXPIRE_TIME?
     pub fn is_old(&self, event: &Event<T>) -> bool {
     pub fn is_old(&self, event: &Event<T>) -> bool {
         !self.event_map.contains_key(&event.previous_event_hash) &&
         !self.event_map.contains_key(&event.previous_event_hash) &&
             event.timestamp.0 + ORPHAN_EXPIRE_TIME < Timestamp::current_time().0
             event.timestamp.0 + ORPHAN_EXPIRE_TIME < Timestamp::current_time().0

+ 10 - 4
src/event_graph/protocol_event.rs

@@ -140,6 +140,9 @@ where
         })
         })
     }
     }
 
 
+    // Receives an event, checks if we already have it, if not, add it
+    // to tree, broadcast an inventory of the event and rebroadcast
+    // the event itself.
     async fn handle_receive_event(self: Arc<Self>) -> Result<()> {
     async fn handle_receive_event(self: Arc<Self>) -> Result<()> {
         debug!(target: "event_graph", "ProtocolEvent::handle_receive_event() [START]");
         debug!(target: "event_graph", "ProtocolEvent::handle_receive_event() [START]");
         let exclude_list = vec![self.channel.address().clone()];
         let exclude_list = vec![self.channel.address().clone()];
@@ -161,6 +164,9 @@ where
         }
         }
     }
     }
 
 
+    // Receives an inventory msg, checks if we already have it, if not,
+    // and if hash in inv is not in tree then ask for the event,
+    // and then rebroadcast the inv anyways.
     async fn handle_receive_inv(self: Arc<Self>) -> Result<()> {
     async fn handle_receive_inv(self: Arc<Self>) -> Result<()> {
         debug!(target: "event_graph", "ProtocolEvent::handle_receive_inv() [START]");
         debug!(target: "event_graph", "ProtocolEvent::handle_receive_inv() [START]");
         let exclude_list = vec![self.channel.address().clone()];
         let exclude_list = vec![self.channel.address().clone()];
@@ -169,7 +175,6 @@ where
             let inv = (*inv).to_owned();
             let inv = (*inv).to_owned();
             let inv_item = inv.invs[0].clone();
             let inv_item = inv.invs[0].clone();
 
 
-            // for inv in inv.invs.iter() {
             if !self.seen_inv.push(&inv_item.hash).await {
             if !self.seen_inv.push(&inv_item.hash).await {
                 continue
                 continue
             }
             }
@@ -178,12 +183,12 @@ where
                 self.send_getdata(vec![inv_item.hash]).await?;
                 self.send_getdata(vec![inv_item.hash]).await?;
             }
             }
 
 
-            // }
-
-            // Broadcast the inv msg
             self.p2p.broadcast_with_exclude(&inv, &exclude_list).await;
             self.p2p.broadcast_with_exclude(&inv, &exclude_list).await;
         }
         }
     }
     }
+
+    // Receives getdata msg, retrieve event data of contained eventID
+    // from our tree and sends it.
     async fn handle_receive_getdata(self: Arc<Self>) -> Result<()> {
     async fn handle_receive_getdata(self: Arc<Self>) -> Result<()> {
         debug!(target: "event_graph", "ProtocolEvent::handle_receive_getdata() [START]");
         debug!(target: "event_graph", "ProtocolEvent::handle_receive_getdata() [START]");
         loop {
         loop {
@@ -199,6 +204,7 @@ where
         }
         }
     }
     }
 
 
+    // Receives sencevent, gets the offspring of the contained eventID and sends them.
     async fn handle_receive_syncevent(self: Arc<Self>) -> Result<()> {
     async fn handle_receive_syncevent(self: Arc<Self>) -> Result<()> {
         debug!(target: "event_graph", "ProtocolEvent::handle_receive_syncevent() [START]");
         debug!(target: "event_graph", "ProtocolEvent::handle_receive_syncevent() [START]");
         loop {
         loop {

+ 0 - 2
src/event_graph/view.rs

@@ -47,9 +47,7 @@ where
     }
     }
 
 
     pub async fn process(&mut self) -> Result<Event<T>> {
     pub async fn process(&mut self) -> Result<Event<T>> {
-        // loop {
         let new_event = self.events_queue.fetch().await?;
         let new_event = self.events_queue.fetch().await?;
         Ok(new_event)
         Ok(new_event)
-        // }
     }
     }
 }
 }