Sfoglia il codice sorgente

[WIP] event_graph added to src/ and ircd2 utilize it from there

Dastan-glitch 3 anni fa
parent
commit
cd5dfbed0e

+ 1 - 0
Cargo.lock

@@ -1142,6 +1142,7 @@ dependencies = [
  "plotters",
  "rand",
  "rcgen",
+ "ripemd",
  "rustls-pemfile",
  "serde",
  "serde_json",

+ 12 - 0
Cargo.toml

@@ -100,6 +100,7 @@ url = {version = "2.3.1", features = ["serde"], optional = true}
 # TODO: Implement something simple and kill these deps
 indicatif = {version = "0.17.3", optional = true}
 simplelog = {version = "0.12.0", optional = true}
+ripemd = {version = "0.1.3", optional = true}
 
 # Websockets
 async-tungstenite = {version = "0.19.0", optional = true}
@@ -179,6 +180,17 @@ dht = [
     "net",
 ]
 
+event-graph = [
+    "ripemd",
+    "chrono",
+    "hex",
+    "rand",
+
+    "async-runtime",
+    "darkfi-serial",
+    "net",
+]
+
 net = [
     "ed25519-compact",
     "fast-socks5",

+ 1 - 1
bin/ircd2/Cargo.toml

@@ -9,7 +9,7 @@ homepage = "https://dark.fi"
 repository = "https://github.com/darkrenaissance/darkfi"
 
 [dependencies]
-darkfi = {path = "../../", features = ["net", "rpc", "bs58"]}
+darkfi = {path = "../../", features = ["event-graph", "rpc", "bs58"]}
 darkfi-serial = {path = "../../src/serial"}
 
 # Async

+ 3 - 4
bin/ircd2/src/crypto.rs

@@ -24,10 +24,9 @@ use crypto_box::{
 };
 use rand::rngs::OsRng;
 
-use crate::{
-    privmsg::PrivMsgEvent,
-    settings::{ChannelInfo, ContactInfo},
-};
+use darkfi::event_graph::PrivMsgEvent;
+
+use crate::settings::{ChannelInfo, ContactInfo};
 
 #[derive(serde::Serialize)]
 pub struct KeyPair {

+ 5 - 3
bin/ircd2/src/irc/client.rs

@@ -26,12 +26,14 @@ use futures::{
 
 use log::{debug, error, info, warn};
 
-use darkfi::{system::Subscription, Error, Result};
+use darkfi::{
+    event_graph::{model::Event, EventAction, PrivMsgEvent},
+    system::Subscription,
+    Error, Result,
+};
 
 use crate::{
     crypto::{decrypt_privmsg, decrypt_target, encrypt_privmsg},
-    model::Event,
-    privmsg::{EventAction, PrivMsgEvent},
     settings,
     settings::RPL,
     ChannelInfo,

+ 14 - 8
bin/ircd2/src/irc/mod.rs

@@ -26,16 +26,22 @@ use futures::{io::BufReader, AsyncRead, AsyncReadExt, AsyncWrite};
 use futures_rustls::{rustls, TlsAcceptor};
 use log::{error, info};
 
-use darkfi::{net::P2pPtr, system::SubscriberPtr, util::path::expand_path, Error, Result};
-
-use crate::{
-    model::{Event, EventId, ModelPtr},
-    privmsg::{EventAction, PrivMsgEvent},
-    protocol_event::{Seen, SeenPtr, UnreadEventsPtr},
-    settings::{get_current_time, Args, ChannelInfo, ContactInfo},
-    view::ViewPtr,
+use darkfi::{
+    event_graph::{
+        get_current_time,
+        model::{Event, EventId, ModelPtr},
+        protocol_event::{Seen, SeenPtr, UnreadEventsPtr},
+        view::ViewPtr,
+        EventAction, PrivMsgEvent,
+    },
+    net::P2pPtr,
+    system::SubscriberPtr,
+    util::path::expand_path,
+    Error, Result,
 };
 
+use crate::settings::{Args, ChannelInfo, ContactInfo};
+
 mod client;
 
 pub use client::IrcClient;

+ 16 - 9
bin/ircd2/src/main.rs

@@ -22,7 +22,14 @@ use rand::rngs::OsRng;
 use structopt_toml::StructOptToml;
 
 use darkfi::{
-    async_daemonize, net,
+    async_daemonize,
+    event_graph::{
+        events_queue::EventsQueue,
+        model::Model,
+        protocol_event::{ProtocolEvent, Seen, UnreadEvents},
+        view::View,
+    },
+    net,
     rpc::server::listen_and_serve,
     system::Subscriber,
     util::{file::save_json_file, path::expand_path},
@@ -30,24 +37,24 @@ use darkfi::{
 };
 
 pub mod crypto;
-pub mod events_queue;
+// pub mod events_queue;
 pub mod irc;
-pub mod model;
+// pub mod model;
 pub mod privmsg;
-pub mod protocol_event;
+// pub mod protocol_event;
 pub mod rpc;
 pub mod settings;
-pub mod view;
+// pub mod view;
 
 use crate::{
     crypto::KeyPair,
-    events_queue::EventsQueue,
+    // events_queue::EventsQueue,
     irc::IrcServer,
-    model::Model,
-    protocol_event::{ProtocolEvent, Seen, UnreadEvents},
+    // model::Model,
+    // protocol_event::{ProtocolEvent, Seen, UnreadEvents},
     rpc::JsonRpcInterface,
     settings::{Args, ChannelInfo, CONFIG_FILE, CONFIG_FILE_CONTENTS},
-    view::View,
+    // view::View,
 };
 
 async_daemonize!(realmain);

+ 2 - 43
bin/ircd2/src/privmsg.rs

@@ -16,47 +16,6 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
-use std::io;
+// use std::io;
 
-use darkfi_serial::{Decodable, Encodable, ReadExt, SerialDecodable, SerialEncodable};
-
-#[derive(SerialEncodable, SerialDecodable, Clone)]
-pub struct PrivMsgEvent {
-    pub nick: String,
-    pub msg: String,
-    pub target: String,
-}
-
-#[derive(Clone)]
-pub enum EventAction {
-    PrivMsg(PrivMsgEvent),
-}
-
-impl std::string::ToString for PrivMsgEvent {
-    fn to_string(&self) -> String {
-        format!(":{}!anon@dark.fi PRIVMSG {} :{}\r\n", self.nick, self.target, self.msg)
-    }
-}
-
-impl Encodable for EventAction {
-    fn encode<S: io::Write>(&self, mut s: S) -> core::result::Result<usize, io::Error> {
-        match self {
-            Self::PrivMsg(event) => {
-                let mut len = 0;
-                len += 0u8.encode(&mut s)?;
-                len += event.encode(s)?;
-                Ok(len)
-            }
-        }
-    }
-}
-
-impl Decodable for EventAction {
-    fn decode<D: io::Read>(mut d: D) -> core::result::Result<Self, io::Error> {
-        let type_id = d.read_u8()?;
-        match type_id {
-            0 => Ok(Self::PrivMsg(PrivMsgEvent::decode(d)?)),
-            _ => Err(io::Error::new(io::ErrorKind::Other, "Bad type ID byte for Event")),
-        }
-    }
-}
+// use darkfi_serial::{Decodable, Encodable, ReadExt, SerialDecodable, SerialEncodable};

+ 0 - 10
bin/ircd2/src/settings.rs

@@ -184,16 +184,6 @@ impl ChannelInfo {
     }
 }
 
-pub fn get_current_time() -> u64 {
-    let start = std::time::SystemTime::now();
-    start
-        .duration_since(std::time::UNIX_EPOCH)
-        .expect("Time went backwards")
-        .as_millis()
-        .try_into()
-        .unwrap()
-}
-
 fn parse_priv(key: &str) -> Result<crypto_box::SecretKey> {
     let bytes: [u8; 32] = bs58::decode(key).into_vec()?.try_into().unwrap();
     Ok(crypto_box::SecretKey::from(bytes))

+ 1 - 3
bin/ircd2/src/events_queue.rs → src/event_graph/events_queue.rs

@@ -18,9 +18,7 @@
 
 use async_std::sync::Arc;
 
-use darkfi::{Error, Result};
-
-use crate::model::Event;
+use crate::{event_graph::model::Event, Error, Result};
 
 pub type EventsQueuePtr = Arc<EventsQueue>;
 

+ 77 - 0
src/event_graph/mod.rs

@@ -0,0 +1,77 @@
+/* This file is part of DarkFi (https://dark.fi)
+ *
+ * Copyright (C) 2020-2023 Dyne.org foundation
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+use std::io;
+
+use darkfi_serial::{Decodable, Encodable, ReadExt, SerialDecodable, SerialEncodable};
+
+pub mod events_queue;
+pub mod model;
+pub mod protocol_event;
+pub mod view;
+
+#[derive(SerialEncodable, SerialDecodable, Clone)]
+pub struct PrivMsgEvent {
+    pub nick: String,
+    pub msg: String,
+    pub target: String,
+}
+
+#[derive(Clone)]
+pub enum EventAction {
+    PrivMsg(PrivMsgEvent),
+}
+
+impl std::string::ToString for PrivMsgEvent {
+    fn to_string(&self) -> String {
+        format!(":{}!anon@dark.fi PRIVMSG {} :{}\r\n", self.nick, self.target, self.msg)
+    }
+}
+
+impl Encodable for EventAction {
+    fn encode<S: io::Write>(&self, mut s: S) -> core::result::Result<usize, io::Error> {
+        match self {
+            Self::PrivMsg(event) => {
+                let mut len = 0;
+                len += 0u8.encode(&mut s)?;
+                len += event.encode(s)?;
+                Ok(len)
+            }
+        }
+    }
+}
+
+impl Decodable for EventAction {
+    fn decode<D: io::Read>(mut d: D) -> core::result::Result<Self, io::Error> {
+        let type_id = d.read_u8()?;
+        match type_id {
+            0 => Ok(Self::PrivMsg(PrivMsgEvent::decode(d)?)),
+            _ => Err(io::Error::new(io::ErrorKind::Other, "Bad type ID byte for Event")),
+        }
+    }
+}
+
+pub fn get_current_time() -> u64 {
+    let start = std::time::SystemTime::now();
+    start
+        .duration_since(std::time::UNIX_EPOCH)
+        .expect("Time went backwards")
+        .as_millis()
+        .try_into()
+        .unwrap()
+}

+ 4 - 5
bin/ircd2/src/model.rs → src/event_graph/model.rs

@@ -23,10 +23,9 @@ use darkfi_serial::{Encodable, SerialDecodable, SerialEncodable};
 use log::error;
 use ripemd::{Digest, Ripemd256};
 
-use crate::{
-    events_queue::EventsQueuePtr,
-    privmsg::{EventAction, PrivMsgEvent},
-};
+use crate::event_graph::events_queue::EventsQueuePtr;
+
+use super::{EventAction, PrivMsgEvent};
 
 pub type EventId = [u8; 32];
 
@@ -409,7 +408,7 @@ impl Model {
 #[cfg(test)]
 mod tests {
     use super::*;
-    use crate::{events_queue::EventsQueue, settings::get_current_time};
+    use crate::event_graph::{events_queue::EventsQueue, get_current_time};
 
     fn create_message(
         previous_event_hash: EventId,

+ 5 - 4
bin/ircd2/src/protocol_event.rs → src/event_graph/protocol_event.rs

@@ -24,11 +24,12 @@ use darkfi_serial::{SerialDecodable, SerialEncodable};
 use log::debug;
 use rand::{rngs::OsRng, RngCore};
 
-use darkfi::{net, util::async_util::sleep, Result};
-
+use super::get_current_time;
 use crate::{
-    model::{Event, EventId, ModelPtr},
-    settings::get_current_time,
+    event_graph::model::{Event, EventId, ModelPtr},
+    net,
+    util::async_util::sleep,
+    Result,
 };
 
 const UNREAD_EVENT_EXPIRE_TIME: u64 = 3600; // in seconds

+ 7 - 6
bin/ircd2/src/view.rs → src/event_graph/view.rs

@@ -19,15 +19,16 @@
 use async_std::sync::{Arc, Mutex};
 use std::collections::HashMap;
 
-use darkfi::Result;
-
-pub type ViewPtr = Arc<Mutex<View>>;
-
 use crate::{
-    events_queue::EventsQueuePtr,
-    model::{Event, EventId},
+    event_graph::{
+        events_queue::EventsQueuePtr,
+        model::{Event, EventId},
+    },
+    Result,
 };
 
+pub type ViewPtr = Arc<Mutex<View>>;
+
 pub struct View {
     pub seen: HashMap<EventId, Event>,
     pub events_queue: EventsQueuePtr,

+ 3 - 0
src/lib.rs

@@ -30,6 +30,9 @@ pub mod consensus;
 #[cfg(feature = "dht")]
 pub mod dht;
 
+#[cfg(feature = "event-graph")]
+pub mod event_graph;
+
 #[cfg(feature = "net")]
 pub mod net;