فهرست منبع

doc/book: hashchain: add EventsQueue type

ghassmo 3 سال پیش
والد
کامیت
09d04ca29f
3فایلهای تغییر یافته به همراه34 افزوده شده و 20 حذف شده
  1. 17 12
      bin/ircd/src/model.rs
  2. 3 3
      bin/ircd/src/view.rs
  3. 14 5
      doc/src/misc/hashchain/architecture.md

+ 17 - 12
bin/ircd/src/model.rs

@@ -12,12 +12,12 @@ use darkfi::{
 use crate::settings::get_current_time;
 
 pub type EventId = [u8; 32];
-pub type EventQueueArc = Arc<EventQueue>;
+pub type EventsQueueArc = Arc<EventsQueue>;
 
-pub struct EventQueue(async_channel::Sender<Event>, async_channel::Receiver<Event>);
+pub struct EventsQueue(async_channel::Sender<Event>, async_channel::Receiver<Event>);
 
-impl EventQueue {
-    pub fn new() -> EventQueueArc {
+impl EventsQueue {
+    pub fn new() -> EventsQueueArc {
         let (sn, rv) = async_channel::unbounded();
         Arc::new(Self(sn, rv))
     }
@@ -110,16 +110,16 @@ struct EventNode {
     children: Vec<EventId>,
 }
 
-#[derive(Debug)]
 pub struct Model {
     // This is periodically updated so we discard old nodes
     current_root: EventId,
     orphans: FxHashMap<EventId, Event>,
     event_map: FxHashMap<EventId, EventNode>,
+    events_queue: EventsQueueArc,
 }
 
 impl Model {
-    pub fn new() -> Self {
+    pub fn new(events_queue: EventsQueueArc) -> Self {
         let root_node = EventNode {
             parent: None,
             event: Event {
@@ -140,7 +140,7 @@ impl Model {
         let mut event_map = FxHashMap::default();
         event_map.insert(root_node_id.clone(), root_node);
 
-        Self { current_root: root_node_id, orphans: FxHashMap::default(), event_map }
+        Self { current_root: root_node_id, orphans: FxHashMap::default(), event_map, events_queue }
     }
 
     pub fn add(&mut self, event: Event) {
@@ -433,7 +433,8 @@ mod tests {
 
     #[test]
     fn test_update_root() {
-        let mut model = Model::new();
+        let events_queue = EventsQueue::new();
+        let mut model = Model::new(events_queue);
         let root_id = model.current_root;
 
         // event_node 1
@@ -484,7 +485,8 @@ mod tests {
 
     #[test]
     fn test_find_height() {
-        let mut model = Model::new();
+        let events_queue = EventsQueue::new();
+        let mut model = Model::new(events_queue);
         let root_id = model.current_root;
 
         // event_node 1
@@ -513,7 +515,8 @@ mod tests {
 
     #[test]
     fn test_prune_chains() {
-        let mut model = Model::new();
+        let events_queue = EventsQueue::new();
+        let mut model = Model::new(events_queue);
         let root_id = model.current_root;
 
         // event_node 1
@@ -550,7 +553,8 @@ mod tests {
 
     #[test]
     fn test_diff_depth() {
-        let mut model = Model::new();
+        let events_queue = EventsQueue::new();
+        let mut model = Model::new(events_queue);
         let root_id = model.current_root;
 
         // event_node 1
@@ -607,7 +611,8 @@ mod tests {
 
     #[test]
     fn test_event_hash() {
-        let mut model = Model::new();
+        let events_queue = EventsQueue::new();
+        let mut model = Model::new(events_queue);
         let root_id = model.current_root;
 
         let timestamp = get_current_time() + 1;

+ 3 - 3
bin/ircd/src/view.rs

@@ -2,7 +2,7 @@ use fxhash::FxHashMap;
 
 use darkfi::Result;
 
-use crate::model::{Event, EventId, EventQueueArc, Model};
+use crate::model::{Event, EventId, EventsQueueArc, Model};
 
 struct View {
     seen: FxHashMap<EventId, Event>,
@@ -13,9 +13,9 @@ impl View {
         Self { seen: FxHashMap::default() }
     }
 
-    pub async fn process(&mut self, event_queue: EventQueueArc) -> Result<()> {
+    pub async fn process(&mut self, events_queue: EventsQueueArc) -> Result<()> {
         loop {
-            let new_event = event_queue.fetch().await?;
+            let new_event = events_queue.fetch().await?;
             // TODO sort the events
             self.seen.insert(new_event.hash(), new_event);
         }

+ 14 - 5
doc/src/misc/hashchain/architecture.md

@@ -50,6 +50,7 @@ All the chains share a root `Event` to preserve the tree structure.
 | current_root  | `EventId` 	  		  		   | The root `Event` for the tree |
 | orphans       | HashMap<`EventId`, `Event`>  	   | Recently added `Event`s 	   |
 | event_map     | HashMap<`EventId`, `EventNode`>  | The actual tree  		 	   |
+| events_queue  | `EventsQueue`					   | Communication channel 
 
 ## View 
 
@@ -57,9 +58,16 @@ The `View` check the `Model` for new `Event`s, then dispatch these `Event`s to t
 
 `Event`s are sorted according to the timestamp attached to each `Event`.
 
-| Description   | Data Type      	   | Comments               |
-|-------------- | -------------------- | ---------------------- |
-| seen  		| HashMap<`EventId`>   | A list of `Event`s 	|
+| Description   | Data Type      	   		    | Comments               |
+|-------------- | ----------------------------- | ---------------------- |
+| seen  		| HashMap<`EventId`, `Event`>   | A list of `Event`s 	 |
+
+## EventsQueue 
+
+The `EventsQueue` used to transport the event from `Model` to `View`.
+
+The `Model` fill The `EventsQueue` with the new `Event`, while the `View` keep
+fetching `Event`s from queue continuously.
 
 # Architecture 
 
@@ -70,10 +78,11 @@ from it continuously.
 
 ## Add new Event
 
-On receiving new `Event` from the network protocol, the `Event` add to the
+Once receiving new `Event` from the network protocol, the `Event` will be add to the
 orphans list. 
 
-`Event` from orphans list add to chains according to its ancestor. 
+After the ancestor for the new orphan gets found, The orphan `Event` will be add to the chain
+according to its ancestor.
 
 For example, in the <em> Example1 </em> below, An `Event` add to the first chain if
 its previous hash is Event-A1