Browse Source

src/dht: feature created(lib structs moved from research/dhtd)

aggstam 4 years ago
parent
commit
109ef9a517
9 changed files with 34 additions and 26 deletions
  1. 4 0
      Cargo.toml
  2. 1 5
      script/research/dhtd/Cargo.toml
  3. 3 9
      script/research/dhtd/src/main.rs
  4. 5 9
      src/dht/dht.rs
  5. 1 1
      src/dht/messages.rs
  6. 6 0
      src/dht/mod.rs
  7. 2 2
      src/dht/protocol.rs
  8. 9 0
      src/error.rs
  9. 3 0
      src/lib.rs

+ 4 - 0
Cargo.toml

@@ -300,6 +300,10 @@ tx = [
 	"util",
 ]
 
+dht = [
+    "util",
+]
+
 [[example]]
 name = "net"
 path = "example/net.rs"

+ 1 - 5
script/research/dhtd/Cargo.toml

@@ -5,7 +5,7 @@ edition = "2021"
 
 [dependencies.darkfi]
 path = "../../../"
-features = ["util"]
+features = ["dht"]
 
 [dependencies]
 async-channel = "1.6.1"
@@ -13,14 +13,10 @@ async-executor = "1.4.1"
 async-std = "1.12.0"
 async-trait = "0.1.56"
 blake3 = "1.3.1"
-chrono = "0.4.19"
 ctrlc-async = {version = "3.2.2", default-features = false, features = ["async-std", "termination"]}
 easy-parallel = "3.2.0"
-futures = "0.3.21"
 futures-lite = "1.12.0"
-fxhash = "0.2.1"
 log = "0.4.17"
-rand = "0.8.5"
 serde_json = "1.0.82"
 simplelog = "0.12.0"
 url = "2.2.2"

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

@@ -10,7 +10,9 @@ use structopt_toml::StructOptToml;
 use url::Url;
 
 use darkfi::{
-    async_daemonize, cli_desc, net,
+    async_daemonize, cli_desc,
+    dht::{waiting_for_response, Dht, DhtPtr},
+    net,
     rpc::{
         jsonrpc::{
             ErrorCode::{InvalidParams, MethodNotFound},
@@ -28,14 +30,6 @@ use darkfi::{
 
 mod error;
 use error::{server_error, RpcError};
-
-mod dht;
-use dht::{waiting_for_response, Dht, DhtPtr};
-
-mod messages;
-
-mod protocol;
-
 const CONFIG_FILE: &str = "dhtd_config.toml";
 const CONFIG_FILE_CONTENTS: &str = include_str!("../dhtd_config.toml");
 

+ 5 - 9
script/research/dhtd/src/dht.rs → src/dht/dht.rs

@@ -7,15 +7,15 @@ use log::{debug, error};
 use rand::Rng;
 use std::{collections::HashSet, time::Duration};
 
-use darkfi::{
+use crate::{
     net,
     net::P2pPtr,
     util::{serial::serialize, sleep},
-    Error::TorError,
+    Error::{NetworkNotConnected, UnknownKey},
     Result,
 };
 
-use crate::{
+use super::{
     messages::{KeyRequest, KeyResponse, LookupRequest},
     protocol::Protocol,
 };
@@ -192,10 +192,7 @@ impl Dht {
         // Verify the key exist in the lookup map.
         let peers = match self.lookup.get(&key) {
             Some(v) => v.clone(),
-            None => {
-                error!("Key doesn't exist.");
-                return Err(TorError("Key doesn't exist.".to_string()))
-            }
+            None => return Err(UnknownKey),
         };
 
         debug!("Key is in peers: {:?}", peers);
@@ -205,8 +202,7 @@ impl Dht {
         // Using len here because is_empty() uses unstable library feature
         // called 'exact_size_is_empty'.
         if self.p2p.channels().lock().await.values().len() == 0 {
-            error!("Node is not connected to other nodes.");
-            return Err(TorError("Node is not connected to other nodes.".to_string()))
+            return Err(NetworkNotConnected)
         }
 
         // We create a key request, and broadcast it to the network

+ 1 - 1
script/research/dhtd/src/messages.rs → src/dht/messages.rs

@@ -1,6 +1,6 @@
 use rand::Rng;
 
-use darkfi::{
+use crate::{
     net,
     util::serial::{serialize, SerialDecodable, SerialEncodable},
 };

+ 6 - 0
src/dht/mod.rs

@@ -0,0 +1,6 @@
+pub mod dht;
+pub use dht::{waiting_for_response, Dht, DhtPtr};
+
+mod messages;
+
+mod protocol;

+ 2 - 2
script/research/dhtd/src/protocol.rs → src/dht/protocol.rs

@@ -4,7 +4,7 @@ use async_trait::async_trait;
 use chrono::Utc;
 use log::{debug, error};
 
-use darkfi::{
+use crate::{
     net::{
         ChannelPtr, MessageSubscription, P2pPtr, ProtocolBase, ProtocolBasePtr,
         ProtocolJobsManager, ProtocolJobsManagerPtr,
@@ -12,7 +12,7 @@ use darkfi::{
     Result,
 };
 
-use crate::{
+use super::{
     dht::DhtPtr,
     messages::{KeyRequest, KeyResponse, LookupRequest},
 };

+ 9 - 0
src/error.rs

@@ -156,6 +156,9 @@ pub enum Error {
     #[error("Tor error: {0}")]
     TorError(String),
 
+    #[error("Node is not connected to other nodes.")]
+    NetworkNotConnected,
+
     // =============
     // Crypto errors
     // =============
@@ -318,6 +321,12 @@ pub enum Error {
 
     #[error(transparent)]
     ClientFailed(#[from] ClientFailed),
+
+    // ==============
+    // DHT errors
+    // ==============
+    #[error("Did not find key")]
+    UnknownKey,
 }
 
 /// Transaction verification errors

+ 3 - 0
src/lib.rs

@@ -10,6 +10,9 @@ pub mod consensus;
 #[cfg(feature = "crypto")]
 pub mod crypto;
 
+#[cfg(feature = "dht")]
+pub mod dht;
+
 #[cfg(feature = "crypto")]
 pub mod zk;