Browse Source

event_graph: WIP integration test.

parazyd 3 năm trước cách đây
mục cha
commit
3db33c07b4
4 tập tin đã thay đổi với 92 bổ sung13 xóa
  1. 1 1
      Cargo.toml
  2. 78 0
      src/event_graph/mod.rs
  3. 7 6
      src/event_graph/model.rs
  4. 6 6
      src/event_graph/protocol_event.rs

+ 1 - 1
Cargo.toml

@@ -194,7 +194,7 @@ event-graph = [
     "rand",
 
     "async-runtime",
-    "darkfi-serial",
+    "darkfi-serial/hash",
     "net",
 ]
 

+ 78 - 0
src/event_graph/mod.rs

@@ -30,3 +30,81 @@ pub trait EventMsg {
 pub fn gen_id(len: usize) -> String {
     thread_rng().sample_iter(&Alphanumeric).take(len).map(char::from).collect()
 }
+
+#[cfg(test)]
+mod tests {
+    use super::{
+        events_queue::EventsQueue,
+        model::{Event, EventId, Model},
+        protocol_event::{Inv, InvId, InvItem, Seen, SeenPtr},
+        view::View,
+        EventMsg,
+    };
+    use crate::util::time::Timestamp;
+    use darkfi_serial::{SerialDecodable, SerialEncodable};
+    use rand::{rngs::OsRng, RngCore};
+
+    #[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
+    struct TestEvent {
+        pub nick: String,
+        pub msg: String,
+    }
+
+    impl EventMsg for TestEvent {
+        fn new() -> Self {
+            Self { nick: "groot".to_string(), msg: "I am groot!!".to_string() }
+        }
+    }
+
+    #[async_std::test]
+    async fn event_graph_integration() {
+        // Base structures
+        let events_queue = EventsQueue::<TestEvent>::new();
+        let mut model = Model::new(events_queue.clone());
+        let view = View::new(events_queue);
+
+        // Buffers
+        let seen_event: SeenPtr<EventId> = Seen::new();
+        let seen_inv: SeenPtr<InvId> = Seen::new();
+
+        let seen_ids = Seen::new();
+        // Keeps track of the events we received, but haven't read yet
+        let mut unread_msgs = vec![];
+
+        let test_event0 =
+            TestEvent { nick: "brawndo".to_string(), msg: "Electrolytes".to_string() };
+        let test_event1 =
+            TestEvent { nick: "camacho".to_string(), msg: "Shieeeeeeeet".to_string() };
+
+        // We create an event and broadcast it
+        let head_hash = model.get_head_hash();
+        let event0 = Event {
+            previous_event_hash: head_hash,
+            action: test_event0,
+            timestamp: Timestamp::current_time(),
+        };
+
+        // Simulate receiving the event
+        assert!(seen_ids.push(&event0.hash()).await);
+        // Simulate receiving the event again
+        assert!(!seen_ids.push(&event0.hash()).await);
+
+        // Add the event into the model
+        model.add(event0.clone()).await;
+
+        // Send inventory? Why is there both an ID and a hash?
+        let id0 = OsRng.next_u64();
+        let inv0 = Inv { invs: vec![InvItem { id: id0, hash: event0.hash() }] };
+        // Simulate recieving the inventory
+        assert!(seen_inv.push(&inv0.invs[0].id).await);
+        // Simulate recieving the inventory again
+        assert!(!seen_inv.push(&inv0.invs[0].id).await);
+
+        // TODO: getdata (self.send_getdata(vec![inv_item.hash]).await?)
+
+        // Add the event to the unread msgs vec
+        unread_msgs.push(event0);
+
+        // TODO: Simulate network behaviour, etc.
+    }
+}

+ 7 - 6
src/event_graph/model.rs

@@ -27,7 +27,8 @@ use crate::{event_graph::events_queue::EventsQueuePtr, util::time::Timestamp};
 
 use super::EventMsg;
 
-pub type EventId = [u8; blake3::OUT_LEN];
+//pub type EventId = [u8; blake3::OUT_LEN];
+pub type EventId = blake3::Hash;
 
 const MAX_DEPTH: u32 = 300;
 const MAX_HEIGHT: u32 = 300;
@@ -44,7 +45,7 @@ where
     T: Send + Sync + Encodable + Decodable + Clone + EventMsg,
 {
     pub fn hash(&self) -> EventId {
-        *blake3::hash(&serialize(self)).as_bytes()
+        blake3::hash(&serialize(self))
     }
 }
 
@@ -74,7 +75,7 @@ where
         let root_node = EventNode {
             parent: None,
             event: Event {
-                previous_event_hash: [0u8; blake3::OUT_LEN],
+                previous_event_hash: blake3::hash(b""), // This is a blake3 hash of NULL
                 action: T::new(),
                 timestamp: Timestamp(1674512021323),
             },
@@ -377,11 +378,11 @@ where
     fn _debug(&self) {
         for (event_id, event_node) in &self.event_map {
             let depth = self.find_depth(*event_id, &self.current_root);
-            println!("{}: {:?} [depth={}]", hex::encode(event_id), event_node.event, depth);
+            println!("{}: {:?} [depth={}]", event_id, event_node.event, depth);
         }
 
-        println!("root: {}", hex::encode(self.current_root));
-        println!("head: {}", hex::encode(self.find_head()));
+        println!("root: {}", self.current_root);
+        println!("head: {}", self.find_head());
     }
 }
 

+ 6 - 6
src/event_graph/protocol_event.rs

@@ -35,17 +35,17 @@ use crate::{
 const SIZE_OF_SEEN_BUFFER: usize = 65536;
 // const MAX_CONFIRM: u8 = 3;
 
-type InvId = u64;
+pub type InvId = u64;
 
 #[derive(SerialEncodable, SerialDecodable, Clone, Debug, PartialEq, Eq, Hash)]
-struct InvItem {
-    id: InvId,
-    hash: EventId,
+pub struct InvItem {
+    pub id: InvId,
+    pub hash: EventId,
 }
 
 #[derive(SerialDecodable, SerialEncodable, Clone, Debug)]
-struct Inv {
-    invs: Vec<InvItem>,
+pub struct Inv {
+    pub invs: Vec<InvItem>,
 }
 
 #[derive(SerialDecodable, SerialEncodable, Clone, Debug)]