mvc.rs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. use async_std::sync::{Arc, Mutex};
  2. use std::{
  3. collections::{HashMap, HashSet},
  4. fmt, io,
  5. };
  6. use async_executor::Executor;
  7. use async_recursion::async_recursion;
  8. use ripemd::{Digest, Ripemd256};
  9. use smol::future;
  10. use structopt::StructOpt;
  11. use structopt_toml::StructOptToml;
  12. use darkfi::{
  13. async_daemonize,
  14. util::{
  15. cli::{get_log_config, get_log_level, spawn_config},
  16. expand_path,
  17. path::get_config_path,
  18. serial::{Decodable, Encodable, ReadExt, SerialDecodable, SerialEncodable},
  19. },
  20. Result,
  21. };
  22. type EventId = [u8; 32];
  23. #[derive(SerialEncodable, SerialDecodable)]
  24. struct Event {
  25. previous_event_hash: EventId,
  26. action: EventAction,
  27. timestamp: u64,
  28. }
  29. impl Event {
  30. fn hash(&self) -> EventId {
  31. let mut bytes = Vec::new();
  32. self.encode(&mut bytes).expect("serialize failed!");
  33. let mut hasher = Ripemd256::new();
  34. hasher.update(bytes);
  35. let bytes = hasher.finalize().to_vec();
  36. let mut result = [0u8; 32];
  37. result.copy_from_slice(bytes.as_slice());
  38. result
  39. }
  40. }
  41. impl fmt::Debug for Event {
  42. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  43. match &self.action {
  44. EventAction::PrivMsg(event) => {
  45. write!(f, "PRIVMSG {}: {} ({})", event.nick, event.msg, self.timestamp)
  46. }
  47. }
  48. }
  49. }
  50. enum EventAction {
  51. PrivMsg(PrivMsgEvent),
  52. }
  53. impl Encodable for EventAction {
  54. fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
  55. match self {
  56. Self::PrivMsg(event) => {
  57. let mut len = 0;
  58. len += 0u8.encode(&mut s)?;
  59. len += event.encode(s)?;
  60. Ok(len)
  61. }
  62. }
  63. }
  64. }
  65. impl Decodable for EventAction {
  66. fn decode<D: io::Read>(mut d: D) -> Result<Self> {
  67. let type_id = d.read_u8()?;
  68. match type_id {
  69. 0 => Ok(Self::PrivMsg(PrivMsgEvent::decode(d)?)),
  70. _ => Err(darkfi::Error::ParseFailed("Bad type ID byte for Event")),
  71. }
  72. }
  73. }
  74. #[derive(SerialEncodable, SerialDecodable)]
  75. struct PrivMsgEvent {
  76. nick: String,
  77. msg: String,
  78. }
  79. struct EventNode {
  80. // Only current root has this set to None
  81. parent: Option<EventNodePtr>,
  82. event: Event,
  83. children: Mutex<Vec<EventNodePtr>>,
  84. }
  85. type EventNodePtr = Arc<EventNode>;
  86. struct Model {
  87. // This is periodically updated so we discard old nodes
  88. current_root: EventId,
  89. orphans: Vec<Event>,
  90. event_map: HashMap<EventId, EventNodePtr>,
  91. }
  92. impl Model {
  93. fn new() -> Self {
  94. let root_node = Arc::new(EventNode {
  95. parent: None,
  96. event: Event {
  97. previous_event_hash: [0u8; 32],
  98. action: EventAction::PrivMsg(PrivMsgEvent {
  99. nick: "root".to_string(),
  100. msg: "Let there be dark".to_string(),
  101. }),
  102. timestamp: get_current_time(),
  103. },
  104. children: Mutex::new(Vec::new()),
  105. });
  106. let root_node_id = root_node.event.hash();
  107. let event_map = HashMap::from([(root_node_id.clone(), root_node)]);
  108. Self { current_root: root_node_id, orphans: Vec::new(), event_map }
  109. }
  110. async fn add(&mut self, event: Event) {
  111. self.orphans.push(event);
  112. self.reorganize().await;
  113. }
  114. // TODO: Update root only after some time
  115. // Recursively free nodes climbing up from old root to new root
  116. // Also remove entries from event_map
  117. async fn reorganize(&mut self) {
  118. let mut remaining_orphans = Vec::new();
  119. for orphan in std::mem::take(&mut self.orphans) {
  120. let prev_event = orphan.previous_event_hash.clone();
  121. // Parent does not yet exist
  122. if !self.event_map.contains_key(&prev_event) {
  123. remaining_orphans.push(orphan);
  124. // BIGTODO #1:
  125. // TODO: We need to fetch missing ancestors from the network
  126. // Trigger get_blocks() request
  127. continue
  128. }
  129. let parent = self.event_map.get(&prev_event).expect("logic error").clone();
  130. let node = Arc::new(EventNode {
  131. parent: Some(parent.clone()),
  132. event: orphan,
  133. children: Mutex::new(Vec::new()),
  134. });
  135. // BIGTODO #2:
  136. // Reject events which attach to forks too low in the chain
  137. // At some point we ignore all events from old branches
  138. //let depth = self.find_ancestor_depth(node.clone(), self.find_head().await);
  139. //if depth > 10 {
  140. // // Discard
  141. // continue
  142. //}
  143. parent.children.lock().await.push(node.clone());
  144. // Add node to the table
  145. self.event_map.insert(node.event.hash(), node);
  146. }
  147. }
  148. fn get_root(&self) -> EventNodePtr {
  149. let root_id = &self.current_root;
  150. return self.event_map.get(root_id).expect("root ID is not in the event map!").clone()
  151. }
  152. // find_head
  153. // -> recursively call itself
  154. // -> + 1 for every recursion, return self if no children
  155. // -> select max from returned values
  156. // Gets the lead node with the maximal number of events counting from root
  157. async fn find_head(&self) -> EventNodePtr {
  158. let root = self.get_root();
  159. Self::find_longest_chain(root, 0).await.1
  160. }
  161. #[async_recursion]
  162. async fn find_longest_chain(parent_node: EventNodePtr, i: u32) -> (u32, EventNodePtr) {
  163. let children = parent_node.children.lock().await;
  164. if children.is_empty() {
  165. return (i, parent_node.clone())
  166. }
  167. let mut current_max = 0;
  168. let mut current_node = None;
  169. for node in &*children {
  170. let (grandchild_i, grandchild_node) =
  171. Self::find_longest_chain(node.clone(), i + 1).await;
  172. if grandchild_i > current_max {
  173. current_max = grandchild_i;
  174. current_node = Some(grandchild_node.clone());
  175. } else if grandchild_i == current_max {
  176. // Break ties using the timestamp
  177. if grandchild_node.event.timestamp >
  178. current_node.as_ref().expect("current_node should be set!").event.timestamp
  179. {
  180. current_max = grandchild_i;
  181. current_node = Some(grandchild_node.clone());
  182. }
  183. }
  184. }
  185. assert_ne!(current_max, 0);
  186. (current_max, current_node.expect("internal logic error"))
  187. }
  188. fn find_height(&self, mut node: EventNodePtr) -> u32 {
  189. let mut height = 0;
  190. while node.event.hash() != self.current_root {
  191. height += 1;
  192. node = node.parent.as_ref().expect("non-root nodes should have a parent set").clone();
  193. }
  194. height
  195. }
  196. fn find_ancestor_depth(&self, mut node_a: EventNodePtr, mut node_b: EventNodePtr) -> u32 {
  197. let mut depth = 0;
  198. while node_a.event.hash() != node_b.event.hash() {
  199. depth += 1;
  200. node_a =
  201. node_a.parent.as_ref().expect("non-root nodes should have a parent set").clone();
  202. node_b =
  203. node_b.parent.as_ref().expect("non-root nodes should have a parent set").clone();
  204. }
  205. depth
  206. }
  207. async fn debug(&self) {
  208. for (event_id, event_node) in &self.event_map {
  209. let height = self.find_height(event_node.clone());
  210. println!("{}: {:?} [height={}]", hex::encode(&event_id), event_node.event, height);
  211. }
  212. println!("root: {}", hex::encode(&self.get_root().event.hash()));
  213. println!("head: {}", hex::encode(&self.find_head().await.event.hash()));
  214. }
  215. }
  216. pub const CONFIG_FILE: &str = "ircd_config.toml";
  217. pub const CONFIG_FILE_CONTENTS: &str = include_str!("../ircd_config.toml");
  218. #[derive(Clone, Debug, serde::Deserialize, StructOpt, StructOptToml)]
  219. #[serde(default)]
  220. #[structopt(name = "ircd")]
  221. pub struct Args {
  222. #[structopt(long)]
  223. pub config: Option<String>,
  224. /// Increase verbosity
  225. #[structopt(short, parse(from_occurrences))]
  226. pub verbose: u8,
  227. }
  228. fn get_current_time() -> u64 {
  229. let start = std::time::SystemTime::now();
  230. start
  231. .duration_since(std::time::UNIX_EPOCH)
  232. .expect("Time went backwards")
  233. .as_millis()
  234. .try_into()
  235. .unwrap()
  236. }
  237. fn create_message(previous_event_hash: EventId, nick: &str, msg: &str, timestamp: u64) -> Event {
  238. Event {
  239. previous_event_hash,
  240. action: EventAction::PrivMsg(PrivMsgEvent { nick: nick.to_string(), msg: msg.to_string() }),
  241. timestamp,
  242. }
  243. }
  244. struct View {
  245. seen: HashSet<EventId>,
  246. }
  247. impl View {
  248. pub fn new() -> Self {
  249. Self { seen: HashSet::new() }
  250. }
  251. fn process(_model: &Model) {
  252. // This does 2 passes:
  253. // 1. Walk down all chains and get unseen events
  254. // 2. Order those events according to timestamp
  255. // Then the events are replayed to the IRC client
  256. }
  257. }
  258. async_daemonize!(realmain);
  259. async fn realmain(_settings: Args, _executor: Arc<Executor<'_>>) -> Result<()> {
  260. let mut model = Model::new();
  261. let root_id = model.get_root().event.hash();
  262. let timestamp = get_current_time() + 1;
  263. let node1 = create_message(root_id, "alice", "alice message", timestamp);
  264. model.add(node1).await;
  265. let node2 = create_message(root_id, "bob", "bob message", timestamp);
  266. let node2_id = node2.hash();
  267. model.add(node2).await;
  268. let node3 = create_message(root_id, "charlie", "charlie message", timestamp);
  269. let node3_id = node3.hash();
  270. model.add(node3).await;
  271. let node4 = create_message(node2_id, "delta", "delta message", timestamp);
  272. let node4_id = node4.hash();
  273. model.add(node4).await;
  274. assert_eq!(model.find_head().await.event.hash(), node4_id);
  275. // Now lets extend another chain
  276. let node5 = create_message(node3_id, "epsilon", "epsilon message", timestamp);
  277. let node5_id = node5.hash();
  278. model.add(node5).await;
  279. let node6 = create_message(node5_id, "phi", "phi message", timestamp);
  280. let node6_id = node6.hash();
  281. model.add(node6).await;
  282. assert_eq!(model.find_head().await.event.hash(), node6_id);
  283. model.debug().await;
  284. Ok(())
  285. }