Bläddra i källkod

dnetview: serialize info into a hex string to create unique ids

lunar-mining 4 år sedan
förälder
incheckning
1301d2576c
3 ändrade filer med 90 tillägg och 68 borttagningar
  1. 1 0
      bin/dnetview/Cargo.toml
  2. 65 47
      bin/dnetview/src/main.rs
  3. 24 21
      bin/dnetview/src/model.rs

+ 1 - 0
bin/dnetview/Cargo.toml

@@ -30,3 +30,4 @@ fxhash = "0.2.1"
 # Encoding and parsing
 serde_json = "1.0.79"
 serde = {version = "1.0.136", features = ["derive"]}
+

+ 65 - 47
bin/dnetview/src/main.rs

@@ -4,7 +4,6 @@ use std::{fs::File, io, io::Read, path::PathBuf};
 use easy_parallel::Parallel;
 use fxhash::{FxHashMap, FxHashSet};
 use log::{debug, info};
-use rand::{thread_rng, Rng};
 use serde_json::{json, Value};
 use simplelog::*;
 use smol::Executor;
@@ -22,13 +21,13 @@ use darkfi::{
     util::{
         async_util,
         cli::{log_config, spawn_config, Config},
-        join_config_path,
+        join_config_path, serial,
     },
 };
 
 use dnetview::{
     config::{DnvConfig, CONFIG_FILE_CONTENTS},
-    model::{ConnectInfo, Model, NodeInfo, SelectableObject, SessionInfo},
+    model::{ConnectInfo, Model, NodeInfo, SelectableObject, Session, SessionInfo},
     options::ProgramOptions,
     ui,
     view::{IdListView, InfoListView, View},
@@ -168,21 +167,21 @@ async fn parse_data(
 
     // first check if we have this node
     let node_name = &client.name;
-    let node_id = make_node_id(node_name.to_string());
+    let node_id = make_node_id(node_name)?;
 
-    let in_session = parse_inbound(inbound, node_id).await?;
-    let out_session = parse_outbound(outbound, node_id).await?;
-    let man_session = parse_manual(manual, node_id).await?;
+    let in_session = parse_inbound(inbound, node_id.clone()).await?;
+    let out_session = parse_outbound(outbound, node_id.clone()).await?;
+    let man_session = parse_manual(manual, node_id.clone()).await?;
 
     sessions.push(in_session);
     sessions.push(out_session);
     sessions.push(man_session);
 
-    let node_info = NodeInfo::new(node_id, node_name.to_string(), sessions);
+    let node_info = NodeInfo::new(node_id.clone(), node_name.to_string(), sessions);
     let node = SelectableObject::Node(node_info.clone());
 
-    model.ids.lock().await.insert(node_id);
-    model.infos.lock().await.insert(node_id, node);
+    model.ids.lock().await.insert(node_id.clone());
+    model.infos.lock().await.insert(node_id.clone(), node);
 
     debug!("IDS: {:?}", model.ids.lock().await);
     debug!("INFOS: {:?}", model.infos.lock().await);
@@ -190,8 +189,9 @@ async fn parse_data(
     Ok(())
 }
 
-async fn parse_inbound(inbound: &Value, node_id: u64) -> Result<SessionInfo> {
-    let session_id = generate_id();
+async fn parse_inbound(inbound: &Value, node_id: String) -> Result<SessionInfo> {
+    let session_type = Session::Inbound;
+    let session_id = make_session_id(node_id.clone(), &session_type)?;
     let mut connects: Vec<ConnectInfo> = Vec::new();
     let connections = &inbound["connected"];
 
@@ -201,12 +201,13 @@ async fn parse_inbound(inbound: &Value, node_id: u64) -> Result<SessionInfo> {
                 true => {
                     // channel is empty. initialize with empty values
                     // TODO: fix this
-                    let connect_id = generate_id();
+                    let id: u64 = 0;
+                    let connect_id = make_connect_id(id)?;
                     let addr = "Null".to_string();
                     let msg = "Null".to_string();
                     let status = "Null".to_string();
                     let is_empty = true;
-                    let parent = session_id;
+                    let parent = session_id.clone();
                     let state = "Null".to_string();
                     let msg_log = Vec::new();
                     let connect_info = ConnectInfo::new(
@@ -216,8 +217,6 @@ async fn parse_inbound(inbound: &Value, node_id: u64) -> Result<SessionInfo> {
                 }
                 false => {
                     // channel is not empty. initialize with whole values
-                    // TODO: we are not saving the connect id
-                    //let connect_id = generate_id();
                     for k in connect.keys() {
                         let node = connect.get(k);
                         let addr = k.to_string();
@@ -226,10 +225,11 @@ async fn parse_inbound(inbound: &Value, node_id: u64) -> Result<SessionInfo> {
                         let status =
                             node.unwrap().get("last_status").unwrap().as_str().unwrap().to_string();
                         // TODO: state, msg log
-                        let connect_id = node.unwrap().get("random_id").unwrap().as_u64().unwrap();
+                        let id = node.unwrap().get("random_id").unwrap().as_u64().unwrap();
+                        let connect_id = make_connect_id(id)?;
                         let state = "state".to_string();
                         let is_empty = false;
-                        let parent = session_id;
+                        let parent = session_id.clone();
                         let msg_log = Vec::new();
                         let connect_info = ConnectInfo::new(
                             connect_id, addr, is_empty, msg, status, state, msg_log, parent,
@@ -238,7 +238,8 @@ async fn parse_inbound(inbound: &Value, node_id: u64) -> Result<SessionInfo> {
                     }
                 }
             }
-            let session_info = SessionInfo::new(session_id, node_id, connects.clone());
+            let session_info =
+                SessionInfo::new(session_id.clone(), node_id.clone(), connects.clone());
             Ok(session_info)
         }
         None => Err(Error::ValueIsNotObject),
@@ -246,16 +247,18 @@ async fn parse_inbound(inbound: &Value, node_id: u64) -> Result<SessionInfo> {
 }
 
 // TODO: placeholder for now
-async fn parse_manual(_manual: &Value, node_id: u64) -> Result<SessionInfo> {
+async fn parse_manual(_manual: &Value, node_id: String) -> Result<SessionInfo> {
+    let session_type = Session::Manual;
     let mut connects: Vec<ConnectInfo> = Vec::new();
 
-    let session_id = generate_id();
-    let connect_id = generate_id();
+    let session_id = make_session_id(node_id.clone(), &session_type)?;
+    let id: u64 = 0;
+    let connect_id = make_connect_id(id)?;
     let addr = "Null".to_string();
     let msg = "Null".to_string();
     let status = "Null".to_string();
     let is_empty = true;
-    let parent = session_id;
+    let parent = session_id.clone();
     let state = "Null".to_string();
     let msg_log = Vec::new();
     let connect_info =
@@ -266,10 +269,11 @@ async fn parse_manual(_manual: &Value, node_id: u64) -> Result<SessionInfo> {
     Ok(session_info)
 }
 
-async fn parse_outbound(outbound: &Value, node_id: u64) -> Result<SessionInfo> {
+async fn parse_outbound(outbound: &Value, node_id: String) -> Result<SessionInfo> {
+    let session_type = Session::Outbound;
     let mut connects: Vec<ConnectInfo> = Vec::new();
     let slots = &outbound["slots"];
-    let session_id = generate_id();
+    let session_id = make_session_id(node_id.clone(), &session_type)?;
 
     match slots.as_array() {
         Some(slots) => {
@@ -278,7 +282,8 @@ async fn parse_outbound(outbound: &Value, node_id: u64) -> Result<SessionInfo> {
                     true => {
                         // channel is empty. initialize with empty values
                         // TODO: fix this
-                        let connect_id = generate_id();
+                        let id: u64 = 0;
+                        let connect_id = make_connect_id(id)?;
                         let is_empty = true;
                         let addr = "Null".to_string();
                         let state = &slot["state"];
@@ -286,7 +291,7 @@ async fn parse_outbound(outbound: &Value, node_id: u64) -> Result<SessionInfo> {
                         let status = "Null".to_string();
                         // TODO: msg log
                         let msg_log = Vec::new();
-                        let parent = session_id;
+                        let parent = session_id.clone();
                         let connect_info = ConnectInfo::new(
                             connect_id,
                             addr,
@@ -304,11 +309,12 @@ async fn parse_outbound(outbound: &Value, node_id: u64) -> Result<SessionInfo> {
                         let channel = &slot["channel"];
                         let last_msg = channel["last_msg"].as_str().unwrap().to_string();
                         let last_status = channel["last_status"].as_str().unwrap().to_string();
-                        let connect_id = channel["random_id"].as_u64().unwrap();
+                        let id = channel["random_id"].as_u64().unwrap();
+                        let connect_id = make_connect_id(id)?;
                         let is_empty = false;
                         let addr = &slot["addr"];
                         let state = &slot["state"];
-                        let parent = session_id;
+                        let parent = session_id.clone();
                         // TODO: deserialize msg_log
                         let _msg_log = channel["log"].as_array().unwrap();
                         let msg_log = Vec::new();
@@ -333,29 +339,41 @@ async fn parse_outbound(outbound: &Value, node_id: u64) -> Result<SessionInfo> {
     }
 }
 
-// create id if not exists
-fn generate_id() -> u64 {
-    let mut rng = thread_rng();
-    let id: u64 = rng.gen();
-    id
+fn make_node_id(node_name: &String) -> Result<String> {
+    Ok(serial::serialize_hex(node_name))
 }
 
-// create id if not exists
-fn make_session_id(connect_id: u64) -> u64 {
-    let id = connect_id * 2;
-    id
-}
+pub fn make_session_id(node_id: String, session: &Session) -> Result<String> {
+    let mut num = 0_u64;
 
-// TODO: this is a pretty weird thing to do lol
-// should probably make id a hash
-fn make_node_id(node_name: String) -> u64 {
-    let mut buf = [0u8; 8];
-    let len = 8.min(node_name.len());
-    buf[..len].copy_from_slice(&node_name.as_bytes()[..len]);
-    let id = u64::from_be_bytes(buf);
-    id
+    match session {
+        Session::Inbound => {
+            for i in ['i', 'n'] {
+                num += i as u64;
+            }
+        }
+        Session::Outbound => {
+            for i in ['o', 'u', 't'] {
+                num += i as u64;
+            }
+        }
+        Session::Manual => {
+            for i in ['m', 'a', 'n'] {
+                num += i as u64;
+            }
+        }
+    }
+
+    for i in node_id.chars() {
+        num += i as u64
+    }
+
+    Ok(serial::serialize_hex(&num))
 }
 
+pub fn make_connect_id(id: u64) -> Result<String> {
+    Ok(serial::serialize_hex(&id))
+}
 //fn is_empty_outbound(slots: Vec<Slot>) -> bool {
 //    return slots.iter().all(|slot| slot.is_empty);
 //}

+ 24 - 21
bin/dnetview/src/model.rs

@@ -1,13 +1,16 @@
 use async_std::sync::Mutex;
 
 use fxhash::{FxHashMap, FxHashSet};
-use serde::Deserialize;
+use serde::{Deserialize, Serialize};
 
-type NodeId = u64;
-type SessionId = u64;
-type ConnectId = u64;
+#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, Hash)]
+pub enum Session {
+    Inbound,
+    Outbound,
+    Manual,
+}
 
-#[derive(Clone, Debug, PartialEq, Eq, Hash)]
+#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, Hash)]
 pub enum SelectableObject {
     Node(NodeInfo),
     Session(SessionInfo),
@@ -15,67 +18,67 @@ pub enum SelectableObject {
 }
 
 pub struct Model {
-    pub ids: Mutex<FxHashSet<u64>>,
-    pub infos: Mutex<FxHashMap<u64, SelectableObject>>,
+    pub ids: Mutex<FxHashSet<String>>,
+    pub infos: Mutex<FxHashMap<String, SelectableObject>>,
 }
 
 impl Model {
     pub fn new(
-        ids: Mutex<FxHashSet<u64>>,
-        infos: Mutex<FxHashMap<u64, SelectableObject>>,
+        ids: Mutex<FxHashSet<String>>,
+        infos: Mutex<FxHashMap<String, SelectableObject>>,
     ) -> Model {
         Model { ids, infos }
     }
 }
 
-#[derive(Clone, Deserialize, Debug, PartialEq, Eq, Hash)]
+#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, Hash)]
 pub struct NodeInfo {
-    pub node_id: NodeId,
+    pub node_id: String,
     pub node_name: String,
     pub children: Vec<SessionInfo>,
 }
 
 impl NodeInfo {
-    pub fn new(node_id: NodeId, node_name: String, children: Vec<SessionInfo>) -> NodeInfo {
+    pub fn new(node_id: String, node_name: String, children: Vec<SessionInfo>) -> NodeInfo {
         NodeInfo { node_id, node_name, children }
     }
 }
 
-#[derive(Clone, Deserialize, Debug, PartialEq, Eq, Hash)]
+#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, Hash)]
 pub struct SessionInfo {
-    pub session_id: SessionId,
-    pub parent: NodeId,
+    pub session_id: String,
+    pub parent: String,
     pub children: Vec<ConnectInfo>,
 }
 
 impl SessionInfo {
-    pub fn new(session_id: SessionId, parent: NodeId, children: Vec<ConnectInfo>) -> SessionInfo {
+    pub fn new(session_id: String, parent: String, children: Vec<ConnectInfo>) -> SessionInfo {
         SessionInfo { session_id, parent, children }
     }
 }
 
-#[derive(Clone, Deserialize, Debug, PartialEq, Eq, Hash)]
+#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, Hash)]
 pub struct ConnectInfo {
-    pub connect_id: ConnectId,
+    pub connect_id: String,
     pub addr: String,
     pub is_empty: bool,
     pub last_msg: String,
     pub last_status: String,
     pub state: String,
     pub msg_log: Vec<String>,
-    pub parent: SessionId,
+    pub parent: String,
 }
 
 impl ConnectInfo {
     pub fn new(
-        connect_id: ConnectId,
+        connect_id: String,
         addr: String,
         is_empty: bool,
         last_msg: String,
         last_status: String,
         state: String,
         msg_log: Vec<String>,
-        parent: SessionId,
+        parent: String,
     ) -> ConnectInfo {
         ConnectInfo { connect_id, addr, is_empty, last_msg, last_status, state, msg_log, parent }
     }