From cd5dfbed0e81c2df8090927446cdf64c67453d80 Mon Sep 17 00:00:00 2001 From: Dastan-glitch Date: Sun, 19 Feb 2023 05:24:52 +0300 Subject: [PATCH] [WIP] event_graph added to src/ and ircd2 utilize it from there --- Cargo.lock | 1 + Cargo.toml | 12 +++ bin/ircd2/Cargo.toml | 2 +- bin/ircd2/src/crypto.rs | 7 +- bin/ircd2/src/irc/client.rs | 8 +- bin/ircd2/src/irc/mod.rs | 22 ++++-- bin/ircd2/src/main.rs | 25 +++--- bin/ircd2/src/privmsg.rs | 45 +---------- bin/ircd2/src/settings.rs | 10 --- .../src => src/event_graph}/events_queue.rs | 4 +- src/event_graph/mod.rs | 77 +++++++++++++++++++ {bin/ircd2/src => src/event_graph}/model.rs | 9 +-- .../src => src/event_graph}/protocol_event.rs | 9 ++- {bin/ircd2/src => src/event_graph}/view.rs | 13 ++-- src/lib.rs | 3 + 15 files changed, 151 insertions(+), 96 deletions(-) rename {bin/ircd2/src => src/event_graph}/events_queue.rs (95%) create mode 100644 src/event_graph/mod.rs rename {bin/ircd2/src => src/event_graph}/model.rs (99%) rename {bin/ircd2/src => src/event_graph}/protocol_event.rs (98%) rename {bin/ircd2/src => src/event_graph}/view.rs (92%) diff --git a/Cargo.lock b/Cargo.lock index 8f02c75b3..f80892047 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1142,6 +1142,7 @@ dependencies = [ "plotters", "rand", "rcgen", + "ripemd", "rustls-pemfile", "serde", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index ccb3458b8..4a42a33ad 100644 --- a/Cargo.toml +++ b/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", diff --git a/bin/ircd2/Cargo.toml b/bin/ircd2/Cargo.toml index 0411358f8..3609ce5cc 100644 --- a/bin/ircd2/Cargo.toml +++ b/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 diff --git a/bin/ircd2/src/crypto.rs b/bin/ircd2/src/crypto.rs index 619b86458..759be0ca7 100644 --- a/bin/ircd2/src/crypto.rs +++ b/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 { diff --git a/bin/ircd2/src/irc/client.rs b/bin/ircd2/src/irc/client.rs index f89cd29f7..6e0c4bdde 100644 --- a/bin/ircd2/src/irc/client.rs +++ b/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, diff --git a/bin/ircd2/src/irc/mod.rs b/bin/ircd2/src/irc/mod.rs index dbb214b0e..da9afe3dd 100644 --- a/bin/ircd2/src/irc/mod.rs +++ b/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; diff --git a/bin/ircd2/src/main.rs b/bin/ircd2/src/main.rs index 5f009fa25..a4f6bc737 100644 --- a/bin/ircd2/src/main.rs +++ b/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); diff --git a/bin/ircd2/src/privmsg.rs b/bin/ircd2/src/privmsg.rs index 62ac4eb94..65e0faadc 100644 --- a/bin/ircd2/src/privmsg.rs +++ b/bin/ircd2/src/privmsg.rs @@ -16,47 +16,6 @@ * along with this program. If not, see . */ -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(&self, mut s: S) -> core::result::Result { - 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(mut d: D) -> core::result::Result { - 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}; diff --git a/bin/ircd2/src/settings.rs b/bin/ircd2/src/settings.rs index c20cf6e9a..5aaacca5b 100644 --- a/bin/ircd2/src/settings.rs +++ b/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 { let bytes: [u8; 32] = bs58::decode(key).into_vec()?.try_into().unwrap(); Ok(crypto_box::SecretKey::from(bytes)) diff --git a/bin/ircd2/src/events_queue.rs b/src/event_graph/events_queue.rs similarity index 95% rename from bin/ircd2/src/events_queue.rs rename to src/event_graph/events_queue.rs index 416dc3e34..1845d53ec 100644 --- a/bin/ircd2/src/events_queue.rs +++ b/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; diff --git a/src/event_graph/mod.rs b/src/event_graph/mod.rs new file mode 100644 index 000000000..f64e7bd4b --- /dev/null +++ b/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 . + */ + +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(&self, mut s: S) -> core::result::Result { + 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(mut d: D) -> core::result::Result { + 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() +} diff --git a/bin/ircd2/src/model.rs b/src/event_graph/model.rs similarity index 99% rename from bin/ircd2/src/model.rs rename to src/event_graph/model.rs index 157ebcf93..fbbe4f295 100644 --- a/bin/ircd2/src/model.rs +++ b/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, diff --git a/bin/ircd2/src/protocol_event.rs b/src/event_graph/protocol_event.rs similarity index 98% rename from bin/ircd2/src/protocol_event.rs rename to src/event_graph/protocol_event.rs index 0e483a29d..6c296f777 100644 --- a/bin/ircd2/src/protocol_event.rs +++ b/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 diff --git a/bin/ircd2/src/view.rs b/src/event_graph/view.rs similarity index 92% rename from bin/ircd2/src/view.rs rename to src/event_graph/view.rs index 1cf5eeaa1..0a7a8c770 100644 --- a/bin/ircd2/src/view.rs +++ b/src/event_graph/view.rs @@ -19,15 +19,16 @@ use async_std::sync::{Arc, Mutex}; use std::collections::HashMap; -use darkfi::Result; +use crate::{ + event_graph::{ + events_queue::EventsQueuePtr, + model::{Event, EventId}, + }, + Result, +}; pub type ViewPtr = Arc>; -use crate::{ - events_queue::EventsQueuePtr, - model::{Event, EventId}, -}; - pub struct View { pub seen: HashMap, pub events_queue: EventsQueuePtr, diff --git a/src/lib.rs b/src/lib.rs index 180d72162..64b761f94 100644 --- a/src/lib.rs +++ b/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;