Просмотр исходного кода

bin/ircd2: add get_head_hash() and get_offspring() functions in model + some fixups

Dastan-glitch 3 лет назад
Родитель
Сommit
7102491197
3 измененных файлов с 44 добавлено и 36 удалено
  1. 1 1
      bin/ircd2/src/irc/mod.rs
  2. 25 14
      bin/ircd2/src/model.rs
  3. 18 21
      bin/ircd2/src/protocol_event.rs

+ 1 - 1
bin/ircd2/src/irc/mod.rs

@@ -185,7 +185,7 @@ impl IrcServer {
             match msg {
                 NotifierMsg::Privmsg(msg) => {
                     let event = Event {
-                        previous_event_hash: model.lock().await.get_current_root(),
+                        previous_event_hash: model.lock().await.get_head_hash(),
                         action: EventAction::PrivMsg(msg.clone()),
                         timestamp: get_current_time(),
                         read_confirms: 0,

+ 25 - 14
bin/ircd2/src/model.rs

@@ -109,8 +109,8 @@ impl Model {
         Self { current_root: root_node_id, orphans: HashMap::new(), event_map, events_queue }
     }
 
-    pub fn get_current_root(&self) -> EventId {
-        self.current_root
+    pub fn get_head_hash(&self) -> EventId {
+        self.find_head()
     }
 
     pub async fn add(&mut self, event: Event) {
@@ -140,23 +140,34 @@ impl Model {
         self.event_map.get(event).map(|en| en.event.clone())
     }
 
-    pub fn get_event_children(&self, event: &EventId) -> Vec<Event> {
-        let mut children = vec![];
-        if let Some(ev) = self.event_map.get(event) {
-            for child in ev.children.iter() {
-                let child = self.event_map.get(child).unwrap();
-                children.push(child.event.clone());
+    pub fn get_offspring(&self, event: &EventId) -> Vec<Event> {
+        let mut offspring = vec![];
+        let mut event = *event;
+        let head = self.find_head();
+        loop {
+            if event == head {
+                break
+            }
+            if let Some(ev) = self.event_map.get(&event) {
+                for child in ev.children.iter() {
+                    let child = self.event_map.get(child).unwrap();
+                    offspring.push(child.event.clone());
+                    event = child.event.hash();
+                }
+            } else {
+                break
             }
         }
-        children
+
+        offspring
     }
 
     async fn reorganize(&mut self) {
         for (_, orphan) in std::mem::take(&mut self.orphans) {
-            if self.is_orphan(&orphan) {
-                // TODO should we remove orphan if it's too old
-                continue
-            }
+            // if self.is_orphan(&orphan) {
+            //     // TODO should we remove orphan if it's too old
+            //     continue
+            // }
 
             let prev_event = orphan.previous_event_hash;
 
@@ -310,7 +321,7 @@ impl Model {
                 }
                 Ordering::Less => {
                     // Left a todo here, not sure if it should be handled
-                    todo!();
+                    continue
                 }
             }
         }

+ 18 - 21
bin/ircd2/src/protocol_event.rs

@@ -118,7 +118,7 @@ impl UnreadEvents {
         self.events.contains_key(key)
     }
 
-    fn get(&self, key: &EventId) -> Option<Event> {
+    fn _get(&self, key: &EventId) -> Option<Event> {
         self.events.get(key).cloned()
     }
 
@@ -217,7 +217,7 @@ impl ProtocolEvent {
 
     async fn handle_receive_event(self: Arc<Self>) -> Result<()> {
         debug!(target: "ircd", "ProtocolEvent::handle_receive_event() [START]");
-        let exclude_list = vec![self.channel.address()];
+        // let exclude_list = vec![self.channel.address()];
         loop {
             let event = self.event_sub.receive().await?;
             let mut event = (*event).to_owned();
@@ -230,15 +230,16 @@ impl ProtocolEvent {
 
             event.read_confirms += 1;
 
-            // if event.read_confirms >= MAX_CONFIRM {
-            //     self.new_event(&event).await?;
-            // } else {
-            self.unread_events.lock().await.insert(&event);
+            if event.read_confirms >= MAX_CONFIRM {
+                self.new_event(&event).await?;
+            } else {
+                self.unread_events.lock().await.insert(&event);
+            }
+
             self.send_inv(&event).await?;
-            // }
 
             // Broadcast the msg
-            self.p2p.broadcast_with_exclude(event, &exclude_list).await?;
+            // self.p2p.broadcast_with_exclude(event, &exclude_list).await?;
         }
     }
 
@@ -279,11 +280,11 @@ impl ProtocolEvent {
             let events = (*getdata).to_owned().events;
 
             for event_id in events {
-                let unread_event = self.unread_events.lock().await.get(&event_id);
-                if let Some(event) = unread_event {
-                    self.channel.send(event).await?;
-                    continue
-                }
+                // let unread_event = self.unread_events.lock().await.get(&event_id);
+                // if let Some(event) = unread_event {
+                //     self.channel.send(event).await?;
+                //     continue
+                // }
 
                 let model_event = self.model.lock().await.get_event(&event_id);
                 if let Some(event) = model_event {
@@ -310,7 +311,7 @@ impl ProtocolEvent {
                     continue
                 }
 
-                let children = model.get_event_children(leaf);
+                let children = model.get_offspring(leaf);
 
                 for child in children {
                     self.channel.send(child).await?;
@@ -319,10 +320,10 @@ impl ProtocolEvent {
         }
     }
 
-    // every 2 seconds send a SyncEvent msg
+    // every 6 seconds send a SyncEvent msg
     async fn send_sync_hash_loop(self: Arc<Self>) -> Result<()> {
         loop {
-            sleep(2).await;
+            sleep(6).await;
             let leaves = self.model.lock().await.find_leaves();
             self.channel.send(SyncEvent { leaves }).await?;
         }
@@ -330,11 +331,7 @@ impl ProtocolEvent {
 
     async fn new_event(&self, event: &Event) -> Result<()> {
         let mut model = self.model.lock().await;
-        if model.is_orphan(event) {
-            self.send_getdata(vec![event.hash()]).await?;
-        } else {
-            model.add(event.clone()).await;
-        }
+        model.add(event.clone()).await;
 
         Ok(())
     }