Преглед на файлове

script/research/dhtd: changed data storage from String to Vec<u8>

aggstam преди 4 години
родител
ревизия
d585120315
променени са 2 файла, в които са добавени 19 реда и са изтрити 12 реда
  1. 9 4
      script/research/dhtd/src/main.rs
  2. 10 8
      script/research/dhtd/src/structures.rs

+ 9 - 4
script/research/dhtd/src/main.rs

@@ -81,7 +81,8 @@ struct Args {
     verbose: u8,
 }
 
-/// Struct representing DHT daemon state
+/// Struct representing DHT daemon state.
+/// In this example we store String data.
 pub struct Dhtd {
     /// Daemon state
     state: StatePtr,
@@ -118,7 +119,10 @@ impl Dhtd {
         // it will query the P2P network and saves the response in its local cache.
         let key = params[0].to_string();
         match self.state.read().await.map.get(&key) {
-            Some(v) => return JsonResponse::new(json!(v), id).into(),
+            Some(v) => {
+                let string = std::str::from_utf8(&v).unwrap();
+                return JsonResponse::new(json!(string), id).into()
+            }
             None => info!("Requested key doesn't exist, querying the network..."),
         };
 
@@ -146,7 +150,8 @@ impl Dhtd {
                 Some(response) => {
                     info!("Key found!");
                     self.state.write().await.map.insert(response.key, response.value.clone());
-                    JsonResponse::new(json!(response.value), id).into()
+                    let string = std::str::from_utf8(&response.value).unwrap();
+                    JsonResponse::new(json!(string), id).into()
                 }
                 None => {
                     info!("Did not find key: {}", key);
@@ -196,7 +201,7 @@ impl Dhtd {
         let key = params[0].to_string();
         let value = params[1].to_string();
 
-        self.state.write().await.map.insert(key.clone(), value.clone());
+        self.state.write().await.map.insert(key.clone(), value.as_bytes().to_vec());
         // TODO: inform network for the insert/update
 
         JsonResponse::new(json!((key, value)), id).into()

+ 10 - 8
script/research/dhtd/src/structures.rs

@@ -12,13 +12,17 @@ use darkfi::{
 pub type StatePtr = Arc<RwLock<State>>;
 
 // TODO: add lookup table
+// Using string in structures because we are at an external crate
+// and cant use blake3 serialization. To be replaced once merged with core src.
+
 /// Struct representing DHT daemon state.
 pub struct State {
     /// Daemon id
     pub id: blake3::Hash,
-    /// Daemon hasmap, using String as key and value for simplicity
-    pub map: FxHashMap<String, String>,
-    /// Daemon seen requests/responses ids, to prevent rebroadcasting and loops
+    /// Daemon hasmap
+    pub map: FxHashMap<String, Vec<u8>>,
+    /// Daemon seen requests/responses ids and timestamp,
+    /// to prevent rebroadcasting and loops
     pub seen: FxHashMap<String, i64>,
 }
 
@@ -40,9 +44,7 @@ impl State {
 /// This struct represents a DHT key request
 #[derive(Debug, Clone, SerialDecodable, SerialEncodable)]
 pub struct KeyRequest {
-    /// Request id
-    // Using string here because we are at an external crate
-    // and cant use blake3 serialization
+    /// Request id    
     pub id: String,
     /// Daemon id requesting the key
     pub daemon: String,
@@ -76,11 +78,11 @@ pub struct KeyResponse {
     /// Key entry
     pub key: String,
     /// Key value
-    pub value: String,
+    pub value: Vec<u8>,
 }
 
 impl KeyResponse {
-    pub fn new(daemon: String, key: String, value: String) -> Self {
+    pub fn new(daemon: String, key: String, value: Vec<u8>) -> Self {
         // Generate a random id
         let mut rng = rand::thread_rng();
         let n: u16 = rng.gen();