Bläddra i källkod

mmproxy: Stratum keepalived

parazyd 2 år sedan
förälder
incheckning
a8397e9871
2 ändrade filer med 39 tillägg och 4 borttagningar
  1. 9 2
      bin/darkfi-mmproxy/src/main.rs
  2. 30 2
      bin/darkfi-mmproxy/src/stratum.rs

+ 9 - 2
bin/darkfi-mmproxy/src/main.rs

@@ -34,7 +34,7 @@ use darkfi_serial::async_trait;
 use log::{debug, error, info};
 use serde::Deserialize;
 use smol::{
-    lock::{Mutex, MutexGuard},
+    lock::{Mutex, MutexGuard, RwLock},
     stream::StreamExt,
     Executor,
 };
@@ -76,6 +76,8 @@ struct Args {
 struct MiningProxy {
     /// Worker logins
     logins: HashMap<String, String>,
+    /// Workers UUIDs
+    workers: RwLock<HashMap<String, String>>,
     /// JSON-RPC connection tracker
     rpc_connections: Mutex<HashSet<StoppableTaskPtr>>,
     /// Main async executor reference
@@ -84,7 +86,12 @@ struct MiningProxy {
 
 impl MiningProxy {
     fn new(logins: HashMap<String, String>, executor: Arc<Executor<'static>>) -> Self {
-        Self { logins, rpc_connections: Mutex::new(HashSet::new()), executor }
+        Self {
+            logins,
+            workers: RwLock::new(HashMap::new()),
+            rpc_connections: Mutex::new(HashSet::new()),
+            executor,
+        }
     }
 }
 

+ 30 - 2
bin/darkfi-mmproxy/src/stratum.rs

@@ -19,7 +19,7 @@
 use std::collections::HashMap;
 
 use darkfi::rpc::{
-    jsonrpc::{ErrorCode, JsonError, JsonResult},
+    jsonrpc::{ErrorCode, JsonError, JsonResponse, JsonResult},
     util::JsonValue,
 };
 use uuid::Uuid;
@@ -105,7 +105,35 @@ impl MiningProxy {
         todo!()
     }
 
+    /// Non standard but widely supported protocol extension. Miner sends `keepalived`
+    /// to prevent connection timeout.
     pub async fn stratum_keepalived(&self, id: u16, params: JsonValue) -> JsonResult {
-        todo!()
+        let params = params.get::<Vec<JsonValue>>().unwrap();
+        if params.len() != 1 || !params[0].is_object() {
+            return JsonError::new(ErrorCode::InvalidParams, None, id).into()
+        }
+
+        let params = params[0].get::<HashMap<String, JsonValue>>().unwrap();
+
+        if !params.contains_key("id") {
+            return JsonError::new(ErrorCode::InvalidParams, None, id).into()
+        }
+
+        let Some(uuid) = params["id"].get::<String>() else {
+            return JsonError::new(ErrorCode::InvalidParams, None, id).into()
+        };
+
+        if self.workers.read().await.contains_key(uuid) {
+            return JsonResponse::new(
+                JsonValue::Object(HashMap::from([(
+                    "status".to_string(),
+                    JsonValue::String("KEEPALIVED".to_string()),
+                )])),
+                id,
+            )
+            .into()
+        }
+
+        return JsonError::new(ErrorCode::InvalidParams, None, id).into()
     }
 }