Procházet zdrojové kódy

bin/ircd: major fix to clean the tree and update the root

ghassmo před 3 roky
rodič
revize
6837634d62
3 změnil soubory, kde provedl 30 přidání a 84 odebrání
  1. 0 51
      bin/ircd/src/buffers.rs
  2. 27 29
      bin/ircd/src/mvc.rs
  3. 3 4
      bin/ircd/src/protocol_privmsg2.rs

+ 0 - 51
bin/ircd/src/buffers.rs

@@ -327,7 +327,6 @@ impl UMsgs {
 mod tests {
 mod tests {
     use super::*;
     use super::*;
     use crate::Privmsg;
     use crate::Privmsg;
-    use rand::{seq::SliceRandom, thread_rng};
 
 
     #[test]
     #[test]
     fn test_ring_buffer() {
     fn test_ring_buffer() {
@@ -349,56 +348,6 @@ mod tests {
         assert_eq!(b.iter().last().unwrap(), &"h9");
         assert_eq!(b.iter().last().unwrap(), &"h9");
     }
     }
 
 
-    #[async_std::test]
-    async fn test_privmsgs_buffer() {
-        let pms = PrivmsgsBuffer::new();
-
-        //
-        // Fill the buffer with random generated terms in range 0..3001
-        //
-        let mut terms: Vec<u64> = (1..3001).collect();
-        terms.shuffle(&mut thread_rng());
-
-        for term in terms {
-            let privmsg = Privmsg::new("nick", "#dev", &format!("message_{}", term), term);
-            pms.push(&privmsg).await;
-        }
-
-        assert_eq!(pms.len().await, 3000);
-        assert_eq!(pms.last_term().await, 3000);
-
-        //
-        // Fill the buffer with random generated terms in range 2000..4001
-        // Since the buffer len now is 3000 it will take only the terms from
-        // 3001 to 4000 without overwriting
-        //
-        let mut terms: Vec<u64> = (2000..4001).collect();
-        terms.shuffle(&mut thread_rng());
-
-        for term in terms {
-            let privmsg = Privmsg::new("nick", "#dev", &format!("message_{}", term), term);
-            pms.push(&privmsg).await;
-        }
-
-        assert_eq!(pms.len().await, settings::SIZE_OF_MSGS_BUFFER);
-        assert_eq!(pms.last_term().await, 4000);
-
-        //
-        // Fill the buffer with random generated terms in range 4000..7001
-        // Since the buffer max size is SIZE_OF_MSGS_BUFFER it has to remove the old msges
-        //
-        let mut terms: Vec<u64> = (4001..7001).collect();
-        terms.shuffle(&mut thread_rng());
-
-        for term in terms {
-            let privmsg = Privmsg::new("nick", "#dev", &format!("message_{}", term), term);
-            pms.push(&privmsg).await;
-        }
-
-        assert_eq!(pms.len().await, settings::SIZE_OF_MSGS_BUFFER);
-        assert_eq!(pms.last_term().await, 7000);
-    }
-
     #[async_std::test]
     #[async_std::test]
     async fn test_seen_ids() {
     async fn test_seen_ids() {
         let seen_ids = SeenIds::default();
         let seen_ids = SeenIds::default();

+ 27 - 29
bin/ircd/src/mvc.rs

@@ -150,57 +150,55 @@ impl Model {
             self.event_map.insert(node_hash, node);
             self.event_map.insert(node_hash, node);
 
 
             // clean up the tree from old eventnodes
             // clean up the tree from old eventnodes
-            self.prune_forks();
+            self.prune_chains();
             self.update_root();
             self.update_root();
         }
         }
     }
     }
 
 
-    fn prune_forks(&mut self) {
+    fn prune_chains(&mut self) {
         let head = self.find_head();
         let head = self.find_head();
-        let mut remove_list = vec![];
-        // Reject events which attach to forks too low in the chain
+        let leaves = self.find_leaves();
+
+        // Reject events which attach to chains too low in the chain
         // At some point we ignore all events from old branches
         // At some point we ignore all events from old branches
-        for (event_hash, node) in self.event_map.iter() {
+        for leaf in leaves {
             // skip the head event
             // skip the head event
-            if event_hash == &head {
+            if leaf == head {
                 continue
                 continue
             }
             }
 
 
-            // check if the node is a leaf
-            if node.children.is_empty() {
-                let depth = self.diff_depth(event_hash.clone(), self.find_head());
-                if depth > MAX_DEPTH {
-                    remove_list.push(event_hash.clone());
-                }
+            let depth = self.diff_depth(leaf.clone(), head);
+            if depth > MAX_DEPTH {
+                self.remove_node(leaf);
             }
             }
         }
         }
-
-        for event in remove_list {
-            self.remove_node(event);
-        }
     }
     }
 
 
-    fn update_root(&mut self) {
-        let head = self.find_head();
-
+    fn find_leaves(&self) -> Vec<EventId> {
         // collect the leaves in the tree
         // collect the leaves in the tree
         let mut leaves = vec![];
         let mut leaves = vec![];
 
 
         for (event_hash, node) in self.event_map.iter() {
         for (event_hash, node) in self.event_map.iter() {
-            // skip the head event
-            if event_hash == &head {
-                continue
-            }
-
             // check if the node is a leaf
             // check if the node is a leaf
             if node.children.is_empty() {
             if node.children.is_empty() {
-                leaves.push(event_hash);
+                leaves.push(event_hash.clone());
             }
             }
         }
         }
 
 
-        // find the common ancestor between each leaf and the head event
+        leaves
+    }
+
+    fn update_root(&mut self) {
+        let head = self.find_head();
+        let leaves = self.find_leaves();
+
+        // find the common ancestor between for each leaf and the head event
         let mut ancestors = vec![];
         let mut ancestors = vec![];
         for leaf in leaves {
         for leaf in leaves {
+            if leaf == head {
+                continue
+            }
+
             let ancestor = self.find_ancestor(leaf.clone(), head);
             let ancestor = self.find_ancestor(leaf.clone(), head);
             ancestors.push(ancestor);
             ancestors.push(ancestor);
         }
         }
@@ -503,7 +501,7 @@ mod tests {
     }
     }
 
 
     #[test]
     #[test]
-    fn test_prune_forks() {
+    fn test_prune_chains() {
         let mut model = Model::new();
         let mut model = Model::new();
         let root_id = model.current_root;
         let root_id = model.current_root;
 
 
@@ -569,7 +567,7 @@ mod tests {
         assert_eq!(model.find_head(), id2);
         assert_eq!(model.find_head(), id2);
 
 
         // event_node 3
         // event_node 3
-        // This will start as new fork, but no events will be added
+        // This will start as new chain, but no events will be added
         // since the last event's depth is 14
         // since the last event's depth is 14
         let mut id3 = root_id;
         let mut id3 = root_id;
         for x in 0..3 {
         for x in 0..3 {
@@ -585,7 +583,7 @@ mod tests {
         assert_eq!(model.find_head(), id2);
         assert_eq!(model.find_head(), id2);
 
 
         // Add more events to the event_node 1
         // Add more events to the event_node 1
-        // At the end this fork must overtake the event_node 2
+        // At the end this chain must overtake the event_node 2
         for x in 7..14 {
         for x in 7..14 {
             let timestamp = get_current_time() + 1;
             let timestamp = get_current_time() + 1;
             let node = create_message(id1, &format!("chain 1 msg {}", x), "message", timestamp);
             let node = create_message(id1, &format!("chain 1 msg {}", x), "message", timestamp);

+ 3 - 4
bin/ircd/src/protocol_privmsg2.rs

@@ -128,7 +128,7 @@ struct Inv {
 
 
 #[derive(SerialDecodable, SerialEncodable, Clone, Debug)]
 #[derive(SerialDecodable, SerialEncodable, Clone, Debug)]
 struct SyncEvent {
 struct SyncEvent {
-    head: EventId,
+    leaves: Vec<EventId>,
 }
 }
 
 
 #[derive(SerialDecodable, SerialEncodable, Clone, Debug)]
 #[derive(SerialDecodable, SerialEncodable, Clone, Debug)]
@@ -258,15 +258,14 @@ impl ProtocolEvent {
         debug!(target: "ircd", "ProtocolEvent::handle_receive_syncevent() [START]");
         debug!(target: "ircd", "ProtocolEvent::handle_receive_syncevent() [START]");
         loop {
         loop {
             let syncevent = self.syncevent_sub.receive().await?;
             let syncevent = self.syncevent_sub.receive().await?;
-            let head = (*syncevent).to_owned().head;
         }
         }
     }
     }
 
 
     // every 2 seconds send a Sync msg
     // every 2 seconds send a Sync msg
     async fn send_sync_hash_loop(self: Arc<Self>) -> Result<()> {
     async fn send_sync_hash_loop(self: Arc<Self>) -> Result<()> {
         loop {
         loop {
-            //let head = self.model.fing_longest_chain();
-            //self.channel.send(SyncEvent { head }).await;
+            //let leaves = self.model.find_leaves();
+            //self.channel.send(SyncEvent { leaves }).await;
             sleep(2).await;
             sleep(2).await;
         }
         }
     }
     }