Jelajahi Sumber

darkfid/rpc: use an fn to return miner status responses for cleaner code

skoupidi 7 bulan lalu
induk
melakukan
597b216396
3 mengubah file dengan 64 tambahan dan 106 penghapusan
  1. 40 27
      bin/darkfid/src/error.rs
  2. 13 44
      bin/darkfid/src/rpc/stratum.rs
  3. 11 35
      bin/darkfid/src/rpc/xmr.rs

+ 40 - 27
bin/darkfid/src/error.rs

@@ -16,7 +16,11 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
-use darkfi::rpc::jsonrpc::{ErrorCode::ServerError, JsonError, JsonResult};
+use std::collections::HashMap;
+
+use tinyjson::JsonValue;
+
+use darkfi::rpc::jsonrpc::{ErrorCode::ServerError, JsonError, JsonResponse, JsonResult};
 
 /// Custom RPC errors available for darkfid.
 /// Please sort them sensefully.
@@ -59,33 +63,32 @@ pub enum RpcError {
     MinerUnknownClient = -32317,
     MinerMissingJobId = -32318,
     MinerInvalidJobId = -32319,
-    MinerUnknownJob = -32320,
-    MinerMissingNonce = -32321,
-    MinerInvalidNonce = -32322,
-    MinerMissingResult = -32323,
-    MinerInvalidResult = -32324,
+    MinerMissingNonce = -32320,
+    MinerInvalidNonce = -32321,
+    MinerMissingResult = -32322,
+    MinerInvalidResult = -32323,
 
     // Merge mining errors
-    MinerMissingAddress = -32325,
-    MinerInvalidAddress = -32326,
-    MinerMissingAuxHash = -32327,
-    MinerInvalidAuxHash = -32328,
-    MinerMissingHeight = -32329,
-    MinerInvalidHeight = -32330,
-    MinerMissingPrevId = -32331,
-    MinerInvalidPrevId = -32332,
-    MinerMissingAuxBlob = -32333,
-    MinerInvalidAuxBlob = -32334,
-    MinerMissingBlob = -32335,
-    MinerInvalidBlob = -32336,
-    MinerMissingMerkleProof = -32337,
-    MinerInvalidMerkleProof = -32338,
-    MinerMissingPath = -32339,
-    MinerInvalidPath = -32340,
-    MinerMissingSeedHash = -32341,
-    MinerInvalidSeedHash = -32342,
-    MinerMerkleProofConstructionFailed = -32343,
-    MinerMoneroPowDataConstructionFailed = -32344,
+    MinerMissingAddress = -32324,
+    MinerInvalidAddress = -32325,
+    MinerMissingAuxHash = -32326,
+    MinerInvalidAuxHash = -32327,
+    MinerMissingHeight = -32328,
+    MinerInvalidHeight = -32329,
+    MinerMissingPrevId = -32330,
+    MinerInvalidPrevId = -32331,
+    MinerMissingAuxBlob = -32332,
+    MinerInvalidAuxBlob = -32333,
+    MinerMissingBlob = -32334,
+    MinerInvalidBlob = -32335,
+    MinerMissingMerkleProof = -32336,
+    MinerInvalidMerkleProof = -32337,
+    MinerMissingPath = -32338,
+    MinerInvalidPath = -32339,
+    MinerMissingSeedHash = -32340,
+    MinerInvalidSeedHash = -32341,
+    MinerMerkleProofConstructionFailed = -32342,
+    MinerMoneroPowDataConstructionFailed = -32343,
 }
 
 fn to_tuple(e: RpcError) -> (i32, String) {
@@ -130,7 +133,6 @@ fn to_tuple(e: RpcError) -> (i32, String) {
         RpcError::MinerUnknownClient => "Request client is unknown",
         RpcError::MinerMissingJobId => "Request is missing the job ID",
         RpcError::MinerInvalidJobId => "Request job ID is invalid",
-        RpcError::MinerUnknownJob => "Request job is unknown",
         RpcError::MinerMissingNonce => "Request is missing the nonce",
         RpcError::MinerInvalidNonce => "Request nonce is invalid",
         RpcError::MinerMissingResult => "Request is missing the result",
@@ -177,3 +179,14 @@ pub fn server_error(e: RpcError, id: u16, msg: Option<&str>) -> JsonResult {
 
     JsonError::new(ServerError(code), Some(default_msg), id).into()
 }
+
+pub fn miner_status_response(id: u16, status: &str) -> JsonResult {
+    JsonResponse::new(
+        JsonValue::from(HashMap::from([(
+            "status".to_string(),
+            JsonValue::from(String::from(status)),
+        )])),
+        id,
+    )
+    .into()
+}

+ 13 - 44
bin/darkfid/src/rpc/stratum.rs

@@ -33,7 +33,11 @@ use darkfi::{
     system::StoppableTaskPtr,
 };
 
-use crate::{registry::model::MinerRewardsRecipientConfig, server_error, DarkfiNode, RpcError};
+use crate::{
+    error::{miner_status_response, server_error, RpcError},
+    registry::model::MinerRewardsRecipientConfig,
+    DarkfiNode,
+};
 
 // https://github.com/xmrig/xmrig-proxy/blob/master/doc/STRATUM.md
 // https://github.com/xmrig/xmrig-proxy/blob/master/doc/STRATUM_EXT.md
@@ -237,14 +241,7 @@ impl DarkfiNode {
     pub async fn stratum_submit(&self, id: u16, params: JsonValue) -> JsonResult {
         // Check if node is synced before responding
         if !*self.validator.synced.read().await {
-            return JsonResponse::new(
-                JsonValue::from(HashMap::from([(
-                    "status".to_string(),
-                    JsonValue::from(String::from("rejected")),
-                )])),
-                id,
-            )
-            .into()
+            return miner_status_response(id, "rejected")
         }
 
         // Grab registry submissions lock
@@ -266,7 +263,7 @@ impl DarkfiNode {
         // If we don't know about this client, we can just abort here
         let mut jobs = self.registry.jobs.write().await;
         let Some(client) = jobs.get(client_id) else {
-            return server_error(RpcError::MinerUnknownClient, id, None)
+            return miner_status_response(id, "rejected")
         };
 
         // Parse job id
@@ -280,27 +277,20 @@ impl DarkfiNode {
         // If this job doesn't match the client one, we can just abort
         // here.
         if &client.job != job_id {
-            return server_error(RpcError::MinerUnknownJob, id, None)
+            return miner_status_response(id, "rejected")
         }
 
         // If this client job wallet template doesn't exist, we can
         // just abort here.
         let mut block_templates = self.registry.block_templates.write().await;
         let Some(block_template) = block_templates.get_mut(&client.wallet) else {
-            return server_error(RpcError::MinerUnknownJob, id, None)
+            return miner_status_response(id, "rejected")
         };
 
         // If this template has been already submitted, reject this
         // submission.
         if block_template.submitted {
-            return JsonResponse::new(
-                JsonValue::from(HashMap::from([(
-                    "status".to_string(),
-                    JsonValue::from(String::from("rejected")),
-                )])),
-                id,
-            )
-            .into()
+            return miner_status_response(id, "rejected")
         }
 
         // Parse nonce
@@ -364,14 +354,7 @@ impl DarkfiNode {
             drop(mm_jobs);
             drop(submit_lock);
 
-            return JsonResponse::new(
-                JsonValue::from(HashMap::from([(
-                    "status".to_string(),
-                    JsonValue::from(String::from("rejected")),
-                )])),
-                id,
-            )
-            .into()
+            return miner_status_response(id, "rejected")
         }
 
         // Mark block as submitted
@@ -382,14 +365,7 @@ impl DarkfiNode {
         drop(jobs);
         drop(submit_lock);
 
-        JsonResponse::new(
-            JsonValue::from(HashMap::from([(
-                "status".to_string(),
-                JsonValue::from(String::from("OK")),
-            )])),
-            id,
-        )
-        .into()
+        miner_status_response(id, "OK")
     }
 
     // RPCAPI:
@@ -423,13 +399,6 @@ impl DarkfiNode {
         };
 
         // Respond with keepalived message
-        JsonResponse::new(
-            JsonValue::from(HashMap::from([(
-                "status".to_string(),
-                JsonValue::from(String::from("KEEPALIVED")),
-            )])),
-            id,
-        )
-        .into()
+        miner_status_response(id, "KEEPALIVED")
     }
 }

+ 11 - 35
bin/darkfid/src/rpc/xmr.rs

@@ -46,7 +46,11 @@ use darkfi::{
 };
 use darkfi_sdk::crypto::keypair::Network;
 
-use crate::{registry::model::MinerRewardsRecipientConfig, server_error, DarkfiNode, RpcError};
+use crate::{
+    error::{miner_status_response, server_error, RpcError},
+    registry::model::MinerRewardsRecipientConfig,
+    DarkfiNode,
+};
 
 // https://github.com/SChernykh/p2pool/blob/master/docs/MERGE_MINING.MD
 
@@ -282,14 +286,7 @@ impl DarkfiNode {
     pub async fn xmr_merge_mining_submit_solution(&self, id: u16, params: JsonValue) -> JsonResult {
         // Check if node is synced before responding to p2pool
         if !*self.validator.synced.read().await {
-            return JsonResponse::new(
-                JsonValue::from(HashMap::from([(
-                    "status".to_string(),
-                    JsonValue::from(String::from("rejected")),
-                )])),
-                id,
-            )
-            .into()
+            return miner_status_response(id, "rejected")
         }
 
         // Grab registry submissions lock
@@ -314,27 +311,20 @@ impl DarkfiNode {
         // If we don't know about this mm job, we can just abort here
         let mut mm_jobs = self.registry.mm_jobs.write().await;
         let Some(wallet) = mm_jobs.get(aux_hash) else {
-            return server_error(RpcError::MinerUnknownJob, id, None)
+            return miner_status_response(id, "rejected")
         };
 
         // If this job wallet template doesn't exist, we can just
         // abort here.
         let mut block_templates = self.registry.block_templates.write().await;
         let Some(block_template) = block_templates.get_mut(wallet) else {
-            return server_error(RpcError::MinerUnknownJob, id, None)
+            return miner_status_response(id, "rejected")
         };
 
         // If this template has been already submitted, reject this
         // submission.
         if block_template.submitted {
-            return JsonResponse::new(
-                JsonValue::from(HashMap::from([(
-                    "status".to_string(),
-                    JsonValue::from(String::from("rejected")),
-                )])),
-                id,
-            )
-            .into()
+            return miner_status_response(id, "rejected")
         }
 
         // Parse aux_blob
@@ -459,14 +449,7 @@ impl DarkfiNode {
             drop(mm_jobs);
             drop(submit_lock);
 
-            return JsonResponse::new(
-                JsonValue::from(HashMap::from([(
-                    "status".to_string(),
-                    JsonValue::from(String::from("rejected")),
-                )])),
-                id,
-            )
-            .into()
+            return miner_status_response(id, "rejected")
         }
 
         // Mark block as submitted
@@ -477,13 +460,6 @@ impl DarkfiNode {
         drop(mm_jobs);
         drop(submit_lock);
 
-        JsonResponse::new(
-            JsonValue::from(HashMap::from([(
-                "status".to_string(),
-                JsonValue::from(String::from("accepted")),
-            )])),
-            id,
-        )
-        .into()
+        miner_status_response(id, "accepted")
     }
 }