Ver Fonte

dnetview: support for darkfid sync and consensus p2p networks

aggstam há 3 anos atrás
pai
commit
e793bf401e

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

@@ -203,6 +203,8 @@ 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,
 
             // ==================
             // Blockchain methods

+ 26 - 0
bin/darkfid/src/rpc_misc.rs

@@ -43,4 +43,30 @@ impl Darkfid {
     pub async fn misc_clock(&self, id: Value, _params: &[Value]) -> JsonResult {
         JsonResponse::new(json!(Timestamp::current_time()), id).into()
     }
+
+    // RPCAPI:
+    // Returns sync P2P network information.
+    //
+    // --> {"jsonrpc": "2.0", "method": "get_info", "params": [], "id": 42}
+    // <-- {"jsonrpc": "2.0", result": {"nodeID": [], "nodeinfo": [], "id": 42}
+    pub async fn misc_get_info(&self, id: Value, _params: &[Value]) -> JsonResult {
+        let resp = match &self.sync_p2p {
+            Some(p2p) => p2p.get_info().await,
+            None => json!([]),
+        };
+        JsonResponse::new(resp, id).into()
+    }
+
+    // RPCAPI:
+    // Returns consensus P2P network information.
+    //
+    // --> {"jsonrpc": "2.0", "method": "get_consensus_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 {
+        let resp = match &self.consensus_p2p {
+            Some(p2p) => p2p.get_info().await,
+            None => json!([]),
+        };
+        JsonResponse::new(resp, id).into()
+    }
 }

+ 11 - 0
bin/dnetview/dnetview_config.toml

@@ -18,3 +18,14 @@ name = "Node 3"
 rpc_url = "tcp://127.0.0.1:1234"
 node_type = "NORMAL"
 
+# Darkfid node can run both sync and consensus p2p networks,
+# so we need to create a record for each network.
+#[[nodes]]
+#name = "darkfid-sync"
+#rpc_url = "tcp://127.0.0.1:8340"
+#node_type = "NORMAL"
+
+#[[nodes]]
+#name = "darkfid-consensus"
+#rpc_url = "tcp://127.0.0.1:8340"
+#node_type = "CONSENSUS"

+ 1 - 0
bin/dnetview/src/config.rs

@@ -37,4 +37,5 @@ pub struct Node {
 pub enum NodeType {
     LILITH,
     NORMAL,
+    CONSENSUS,
 }

+ 2 - 3
bin/dnetview/src/parser.rs

@@ -88,6 +88,7 @@ impl DataParser {
             let response = match &node.node_type {
                 NodeType::LILITH => client.lilith_spawns().await,
                 NodeType::NORMAL => client.get_info().await,
+                NodeType::CONSENSUS => client.get_consensus_info().await,
             };
 
             // Parse response
@@ -105,9 +106,7 @@ impl DataParser {
                             )
                             .await?
                         }
-                        NodeType::NORMAL => {
-                            self.parse_data(reply.as_object().unwrap(), node.name.clone()).await?
-                        }
+                        _ => self.parse_data(reply.as_object().unwrap(), node.name.clone()).await?,
                     };
                 }
                 Err(e) => return Err(e),

+ 11 - 1
bin/dnetview/src/rpc.rs

@@ -44,7 +44,7 @@ impl RpcConnect {
         self.rpc_client.request(req).await
     }
 
-    // --> {"jsonrpc": "2.0", "method": "poll", "params": [], "id": 42}
+    // --> {"jsonrpc": "2.0", "method": "get_info", "params": [], "id": 42}
     // <-- {"jsonrpc": "2.0", "result": {"nodeID": [], "nodeinfo" [], "id": 42}
     pub async fn get_info(&self) -> DnetViewResult<Value> {
         let req = JsonRequest::new("get_info", json!([]));
@@ -54,6 +54,16 @@ impl RpcConnect {
         }
     }
 
+    // --> {"jsonrpc": "2.0", "method": "get_consensus_info", "params": [], "id": 42}
+    // <-- {"jsonrpc": "2.0", "result": {"nodeID": [], "nodeinfo" [], "id": 42}
+    pub async fn get_consensus_info(&self) -> DnetViewResult<Value> {
+        let req = JsonRequest::new("get_consensus_info", json!([]));
+        match self.rpc_client.request(req).await {
+            Ok(req) => Ok(req),
+            Err(e) => Err(DnetViewError::Darkfi(e)),
+        }
+    }
+
     // Returns all lilith node spawned networks names with their node addresses.
     // --> {"jsonrpc": "2.0", "method": "spawns", "params": [], "id": 42}
     // <-- {"jsonrpc": "2.0", "result": "{spawns}", "id": 42}