diff --git a/script/research/dhtd/src/main.rs b/script/research/dhtd/src/main.rs index 544039b9a..874420a4c 100644 --- a/script/research/dhtd/src/main.rs +++ b/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() diff --git a/script/research/dhtd/src/structures.rs b/script/research/dhtd/src/structures.rs index cefdb06e1..97546f8b2 100644 --- a/script/research/dhtd/src/structures.rs +++ b/script/research/dhtd/src/structures.rs @@ -12,13 +12,17 @@ use darkfi::{ pub type StatePtr = Arc>; // 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, - /// Daemon seen requests/responses ids, to prevent rebroadcasting and loops + /// Daemon hasmap + pub map: FxHashMap>, + /// Daemon seen requests/responses ids and timestamp, + /// to prevent rebroadcasting and loops pub seen: FxHashMap, } @@ -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, } impl KeyResponse { - pub fn new(daemon: String, key: String, value: String) -> Self { + pub fn new(daemon: String, key: String, value: Vec) -> Self { // Generate a random id let mut rng = rand::thread_rng(); let n: u16 = rng.gen();