Quellcode durchsuchen

bin: Update daemon methods for dnet.

parazyd vor 3 Jahren
Ursprung
Commit
3b9c016ef6

+ 1 - 1
Cargo.toml

@@ -30,7 +30,7 @@ members = [
     "bin/genev/genevd",
     "bin/genev/genev-cli",
     "bin/darkirc",
-    "bin/dnetview",
+    #"bin/dnetview",
     "bin/tau/taud",
     "bin/tau/tau-cli",
     #"bin/darkwiki/darkwikid",

+ 8 - 2
bin/darkfid/src/main.rs

@@ -215,8 +215,14 @@ impl RequestHandler for Darkfid {
             // =====================
             Some("ping") => return self.misc_pong(req.id, params).await,
             Some("clock") => return self.misc_clock(req.id, params).await,
-            Some("get_info") => return self.misc_get_info(req.id, params).await,
-            Some("get_consensus_info") => return self.misc_get_consensus_info(req.id, params).await,
+            Some("sync_dnet_switch") => return self.misc_sync_dnet_switch(req.id, params).await,
+            Some("sync_dnet_info") => return self.misc_sync_dnet_info(req.id, params).await,
+            Some("consensus_dnet_switch") => {
+                return self.misc_consensus_dnet_switch(req.id, params).await
+            }
+            Some("consensus_dnet_info") => {
+                return self.misc_consensus_dnet_info(req.id, params).await
+            }
 
             // ==================
             // Blockchain methods

+ 54 - 7
bin/darkfid/src/rpc_misc.rs

@@ -19,7 +19,8 @@
 use serde_json::{json, Value};
 
 use darkfi::{
-    rpc::jsonrpc::{JsonResponse, JsonResult},
+    net::P2p,
+    rpc::jsonrpc::{ErrorCode, JsonError, JsonResponse, JsonResult},
     util::time::Timestamp,
 };
 
@@ -44,27 +45,73 @@ impl Darkfid {
         JsonResponse::new(json!(Timestamp::current_time()), id).into()
     }
 
+    // RPCAPI:
+    // Activate or deactivate dnet in the sync P2P stack.
+    // By sending `true`, dnet will be activated, and by sending `false` dnet
+    // will be deactivated. Returns `true` on success.
+    //
+    // --> {"jsonrpc": "2.0", "method": "sync_dnet_switch", "params": [true], "id": 42}
+    // <-- {"jsonrpc": "2.0", "result": true, "id": 42}
+    pub async fn misc_sync_dnet_switch(&self, id: Value, params: &[Value]) -> JsonResult {
+        if params.len() != 1 && params[0].as_bool().is_none() {
+            return JsonError::new(ErrorCode::InvalidParams, None, id).into()
+        }
+
+        // FIXME: Unwrapping here because lazy
+
+        if params[0].as_bool().unwrap() {
+            self.sync_p2p.as_ref().unwrap().dnet_enable().await;
+        } else {
+            self.sync_p2p.as_ref().unwrap().dnet_disable().await;
+        }
+
+        JsonResponse::new(json!(true), id).into()
+    }
+
     // RPCAPI:
     // Returns sync P2P network information.
     //
-    // --> {"jsonrpc": "2.0", "method": "get_info", "params": [], "id": 42}
+    // --> {"jsonrpc": "2.0", "method": "sync_dnet_info", "params": [], "id": 42}
     // <-- {"jsonrpc": "2.0", result": {"nodeID": [], "nodeinfo": [], "id": 42}
-    pub async fn misc_get_info(&self, id: Value, _params: &[Value]) -> JsonResult {
+    pub async fn misc_sync_dnet_info(&self, id: Value, _params: &[Value]) -> JsonResult {
         let resp = match &self.sync_p2p {
-            Some(p2p) => p2p.get_info().await,
+            Some(p2p) => P2p::map_dnet_info(p2p.dnet_info().await),
             None => json!([]),
         };
         JsonResponse::new(resp, id).into()
     }
 
+    // RPCAPI:
+    // Activate or deactivate dnet in the consensus P2P stack.
+    // By sending `true`, dnet will be activated, and by sending `false` dnet
+    // will be deactivated. Returns `true` on success.
+    //
+    // --> {"jsonrpc": "2.0", "method": "consensus_dnet_switch", "params": [true], "id": 42}
+    // <-- {"jsonrpc": "2.0", "result": true, "id": 42}
+    pub async fn misc_consensus_dnet_switch(&self, id: Value, params: &[Value]) -> JsonResult {
+        if params.len() != 1 && params[0].as_bool().is_none() {
+            return JsonError::new(ErrorCode::InvalidParams, None, id).into()
+        }
+
+        // FIXME: Unwrapping here because lazy
+
+        if params[0].as_bool().unwrap() {
+            self.consensus_p2p.as_ref().unwrap().dnet_enable().await;
+        } else {
+            self.consensus_p2p.as_ref().unwrap().dnet_disable().await;
+        }
+
+        JsonResponse::new(json!(true), id).into()
+    }
+
     // RPCAPI:
     // Returns consensus P2P network information.
     //
-    // --> {"jsonrpc": "2.0", "method": "get_consensus_info", "params": [], "id": 42}
+    // --> {"jsonrpc": "2.0", "method": "consensus_dnet_info", "params": [], "id": 42}
     // <-- {"jsonrpc": "2.0", result": {"nodeID": [], "nodeinfo": [], "id": 42}
-    pub async fn misc_get_consensus_info(&self, id: Value, _params: &[Value]) -> JsonResult {
+    pub async fn misc_consensus_dnet_info(&self, id: Value, _params: &[Value]) -> JsonResult {
         let resp = match &self.consensus_p2p {
-            Some(p2p) => p2p.get_info().await,
+            Some(p2p) => P2p::map_dnet_info(p2p.dnet_info().await),
             None => json!([]),
         };
         JsonResponse::new(resp, id).into()

+ 33 - 7
bin/darkirc/src/rpc.rs

@@ -41,11 +41,15 @@ impl RequestHandler for JsonRpcInterface {
             return JsonError::new(ErrorCode::InvalidRequest, None, req.id).into()
         }
 
+        let params = req.params.as_array().unwrap();
+
         debug!(target: "RPC", "--> {}", serde_json::to_string(&req).unwrap());
 
         match req.method.as_str() {
-            Some("ping") => self.pong(req.id, req.params).await,
-            Some("get_info") => self.get_info(req.id, req.params).await,
+            Some("ping") => self.pong(req.id, params).await,
+
+            Some("dnet_switch") => self.dnet_switch(req.id, params).await,
+            Some("dnet_info") => self.dnet_info(req.id, params).await,
             Some(_) | None => JsonError::new(ErrorCode::MethodNotFound, None, req.id).into(),
         }
     }
@@ -56,16 +60,38 @@ impl JsonRpcInterface {
     // Replies to a ping method.
     // --> {"jsonrpc": "2.0", "method": "ping", "params": [], "id": 42}
     // <-- {"jsonrpc": "2.0", "result": "pong", "id": 42}
-    async fn pong(&self, id: Value, _params: Value) -> JsonResult {
+    async fn pong(&self, id: Value, _params: &[Value]) -> JsonResult {
         JsonResponse::new(json!("pong"), id).into()
     }
 
+    // RPCAPI:
+    // Activate or deactivate dnet in the P2P stack.
+    // By sending `true`, dnet will be activated, and by sending `false` dnet
+    // will be deactivated. Returns `true` on success.
+    //
+    // --> {"jsonrpc": "2.0", "method": "dnet_switch", "params": [true], "id": 42}
+    // <-- {"jsonrpc": "2.0", "result": true, "id": 42}
+    async fn dnet_switch(&self, id: Value, params: &[Value]) -> JsonResult {
+        if params.len() != 1 && params[0].as_bool().is_none() {
+            return JsonError::new(ErrorCode::InvalidParams, None, id).into()
+        }
+
+        if params[0].as_bool().unwrap() {
+            self.p2p.dnet_enable().await;
+        } else {
+            self.p2p.dnet_disable().await;
+        }
+
+        JsonResponse::new(json!(true), id).into()
+    }
+
     // RPCAPI:
     // Retrieves P2P network information.
-    // --> {"jsonrpc": "2.0", "method": "get_info", "params": [], "id": 42}
+    //
+    // --> {"jsonrpc": "2.0", "method": "dnet_info", "params": [], "id": 42}
     // <-- {"jsonrpc": "2.0", result": {"nodeID": [], "nodeinfo": [], "id": 42}
-    async fn get_info(&self, id: Value, _params: Value) -> JsonResult {
-        let resp = self.p2p.get_info().await;
-        JsonResponse::new(resp, id).into()
+    async fn dnet_info(&self, id: Value, _params: &[Value]) -> JsonResult {
+        let dnet_info = self.p2p.dnet_info().await;
+        JsonResponse::new(net::P2p::map_dnet_info(dnet_info), id).into()
     }
 }

+ 28 - 5
bin/fud/fud/src/main.rs

@@ -32,6 +32,7 @@ use darkfi::{
     net,
     rpc::{
         jsonrpc::{
+            ErrorCode,
             ErrorCode::{InvalidParams, MethodNotFound},
             JsonError, JsonRequest, JsonResponse, JsonResult,
         },
@@ -358,13 +359,34 @@ impl Fud {
         JsonResponse::new(json!("pong"), id).into()
     }
 
+    // RPCAPI:
+    // Activate or deactivate dnet in the P2P stack.
+    // By sending `true`, dnet will be activated, and by sending `false` dnet
+    // will be deactivated. Returns `true` on success.
+    //
+    // --> {"jsonrpc": "2.0", "method": "dnet_switch", "params": [true], "id": 42}
+    // <-- {"jsonrpc": "2.0", "result": true, "id": 42}
+    async fn dnet_switch(&self, id: Value, params: &[Value]) -> JsonResult {
+        if params.len() != 1 && params[0].as_bool().is_none() {
+            return JsonError::new(ErrorCode::InvalidParams, None, id).into()
+        }
+
+        if params[0].as_bool().unwrap() {
+            self.dht.read().await.p2p.dnet_enable().await;
+        } else {
+            self.dht.read().await.p2p.dnet_disable().await;
+        }
+
+        JsonResponse::new(json!(true), id).into()
+    }
+
     // RPCAPI:
     // Retrieves P2P network information.
-    // --> {"jsonrpc": "2.0", "method": "get_info", "params": [], "id": 42}
+    // --> {"jsonrpc": "2.0", "method": "dnet_info", "params": [], "id": 42}
     // <-- {"jsonrpc": "2.0", result": {"nodeID": [], "nodeinfo": [], "id": 42}
-    async fn get_info(&self, id: Value, _params: &[Value]) -> JsonResult {
-        let resp = self.dht.read().await.p2p.get_info().await;
-        JsonResponse::new(resp, id).into()
+    async fn dnet_info(&self, id: Value, _params: &[Value]) -> JsonResult {
+        let dnet_info = self.dht.read().await.p2p.dnet_info().await;
+        JsonResponse::new(net::P2p::map_dnet_info(dnet_info), id).into()
     }
 }
 
@@ -382,7 +404,8 @@ impl RequestHandler for Fud {
             Some("sync") => return self.sync(req.id, params).await,
             Some("get") => return self.get(req.id, params).await,
             Some("ping") => return self.pong(req.id, params).await,
-            Some("get_info") => return self.get_info(req.id, params).await,
+            Some("dnet_switch") => return self.dnet_switch(req.id, params).await,
+            Some("dnet_info") => return self.dnet_info(req.id, params).await,
             Some(_) | None => return JsonError::new(MethodNotFound, None, req.id).into(),
         }
     }

+ 27 - 5
bin/genev/genevd/src/rpc.rs

@@ -55,7 +55,8 @@ impl RequestHandler for JsonRpcInterface {
             Some("add") => self.add(req.id, req.params).await,
             Some("list") => self.list(req.id, req.params).await,
             Some("ping") => self.pong(req.id, req.params).await,
-            Some("get_info") => self.get_info(req.id, req.params).await,
+            Some("dnet_switch") => self.dnet_switch(req.id, req.params).await,
+            Some("dnet_info") => self.dnet_info(req.id, req.params).await,
             Some(_) | None => return JsonError::new(ErrorCode::MethodNotFound, None, req.id).into(),
         }
     }
@@ -80,13 +81,34 @@ impl JsonRpcInterface {
         JsonResponse::new(json!("pong"), id).into()
     }
 
+    // RPCAPI:
+    // Activate or deactivate dnet in the P2P stack.
+    // By sending `true`, dnet will be activated, and by sending `false` dnet
+    // will be deactivated. Returns `true` on success.
+    //
+    // --> {"jsonrpc": "2.0", "method": "dnet_switch", "params": [true], "id": 42}
+    // <-- {"jsonrpc": "2.0", "result": true, "id": 42}
+    async fn dnet_switch(&self, id: Value, params: &[Value]) -> JsonResult {
+        if params.len() != 1 && params[0].as_bool().is_none() {
+            return JsonError::new(ErrorCode::InvalidParams, None, id).into()
+        }
+
+        if params[0].as_bool().unwrap() {
+            self.p2p.dnet_enable().await;
+        } else {
+            self.p2p.dnet_disable().await;
+        }
+
+        JsonResponse::new(json!(true), id).into()
+    }
+
     // RPCAPI:
     // Retrieves P2P network information.
-    // --> {"jsonrpc": "2.0", "method": "get_info", "params": [], "id": 42}
+    // --> {"jsonrpc": "2.0", "method": "dnet_info", "params": [], "id": 42}
     // <-- {"jsonrpc": "2.0", result": {"nodeID": [], "nodeinfo": [], "id": 42}
-    async fn get_info(&self, id: Value, _params: Value) -> JsonResult {
-        let resp = self.p2p.get_info().await;
-        JsonResponse::new(resp, id).into()
+    async fn dnet_info(&self, id: Value, _params: Value) -> JsonResult {
+        let dnet_info = self.p2p.dnet_info().await;
+        JsonResponse::new(net::P2p::map_dnet_info(dnet_info), id).into()
     }
 
     // RPCAPI:

+ 31 - 6
bin/tau/taud/src/jsonrpc.rs

@@ -84,7 +84,9 @@ impl RequestHandler for JsonRpcInterface {
             Some("import") => self.import_from(params).await,
             Some("get_stop_tasks") => self.get_stop_tasks(params).await,
             Some("ping") => self.pong(params).await,
-            Some("get_info") => self.get_info(params).await,
+
+            Some("dnet_switch") => self.dnet_switch(params).await,
+            Some("dnet_info") => self.dnet_info(params).await,
             Some(_) | None => return JsonError::new(ErrorCode::MethodNotFound, None, req.id).into(),
         };
 
@@ -106,19 +108,42 @@ impl JsonRpcInterface {
 
     // RPCAPI:
     // Replies to a ping method.
+    //
     // --> {"jsonrpc": "2.0", "method": "ping", "params": [], "id": 42}
     // <-- {"jsonrpc": "2.0", "result": "pong", "id": 42}
     async fn pong(&self, _params: &[Value]) -> TaudResult<Value> {
         Ok(json!("pong"))
     }
 
+    // RPCAPI:
+    // Activate or deactivate dnet in the P2P stack.
+    // By sending `true`, dnet will be activated, and by sending `false` dnet will
+    // be deactivated. Returns `true` on success.
+    //
+    // --> {"jsonrpc": "2.0", "method": "dnet_switch", "params": [true], "id": 42}
+    // <-- {"jsonrpc": "2.0", "result": true, "id": 42}
+    async fn dnet_switch(&self, params: &[Value]) -> TaudResult<Value> {
+        if params.len() != 1 && params[0].as_bool().is_none() {
+            return Err(TaudError::InvalidData("Invalid parameters".into()))
+        }
+
+        if params[0].as_bool().unwrap() {
+            self.p2p.dnet_enable().await;
+        } else {
+            self.p2p.dnet_disable().await;
+        }
+
+        Ok(json!(true))
+    }
+
     // RPCAPI:
     // Retrieves P2P network information.
-    // --> {"jsonrpc": "2.0", "method": "get_info", "params": [], "id": 42}
+    //
+    // --> {"jsonrpc": "2.0", "method": "dnet_info", "params": [], "id": 42}
     // <-- {"jsonrpc": "2.0", result": {"nodeID": [], "nodeinfo": [], "id": 42}
-    async fn get_info(&self, _params: &[Value]) -> TaudResult<Value> {
-        let resp = self.p2p.get_info().await;
-        Ok(resp)
+    async fn dnet_info(&self, _params: &[Value]) -> TaudResult<Value> {
+        let dnet_info = self.p2p.dnet_info().await;
+        Ok(net::P2p::map_dnet_info(dnet_info))
     }
 
     // RPCAPI:
@@ -228,7 +253,7 @@ impl JsonRpcInterface {
         debug!(target: "tau", "JsonRpc::set_comment() params {:?}", params);
 
         if params.len() != 2 {
-            return Err(TaudError::InvalidData("len of params should be 3".into()))
+            return Err(TaudError::InvalidData("len of params should be 2".into()))
         }
 
         let comment_content: String = serde_json::from_value(params[1].clone())?;

+ 3 - 1
contrib/compile_all_bins.sh

@@ -1,6 +1,8 @@
 #!/bin/sh
 set -e
 
+CARGO="${CARGO:-cargo +nightly}"
+
 toplevel="$(git rev-parse --show-toplevel)"
 dirs="$(grep '"bin/' "$toplevel/Cargo.toml" | grep -v '#' | tr -d '", \t')"
 
@@ -9,4 +11,4 @@ for i in $dirs; do
 	bins="$bins $(grep '^name = ' "$i/Cargo.toml" | cut -d' ' -f3 | tr -d '"')"
 done
 
-make BINS="$bins"
+make CARGO="$CARGO" BINS="$bins"