Просмотр исходного кода

darkfid: Add merge_mining_get_chain_id RPC method.

parazyd 2 лет назад
Родитель
Сommit
82c7ef4e0c
2 измененных файлов с 31 добавлено и 1 удалено
  1. 3 0
      bin/darkfid2/src/rpc.rs
  2. 28 1
      bin/darkfid2/src/rpc_blockchain.rs

+ 3 - 0
bin/darkfid2/src/rpc.rs

@@ -68,6 +68,9 @@ impl RequestHandler for Darkfid {
             "blockchain.subscribe_proposals" => {
                 return self.blockchain_subscribe_proposals(req.id, req.params).await
             }
+            "merge_mining_get_chain_id" => {
+                return self.merge_mining_get_chain_id(req.id, req.params).await
+            }
 
             // ===================
             // Transaction methods

+ 28 - 1
bin/darkfid2/src/rpc_blockchain.rs

@@ -16,7 +16,7 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
-use std::str::FromStr;
+use std::{collections::HashMap, str::FromStr};
 
 use darkfi_sdk::crypto::ContractId;
 use darkfi_serial::{deserialize, serialize};
@@ -262,4 +262,31 @@ impl Darkfid {
 
         JsonResponse::new(JsonValue::Array(ret), id).into()
     }
+
+    // RPCAPI:
+    // Returns the `chain_id` used for merge mining. A 32-byte hash of the genesis block.
+    //
+    // --> {"jsonrpc": "2.0", "method": "merge_mining_get_chain_id", "params": [], "id": 0}
+    // <-- {"jsonrpc": "2.0", "result": {"chain_id": 02f8...7863"}, "id": 0}
+    pub async fn merge_mining_get_chain_id(&self, id: u16, _params: JsonValue) -> JsonResult {
+        let chain_id = match self.validator.blockchain.genesis() {
+            Ok((_, v)) => v,
+            Err(e) => {
+                error!(
+                    target: "darkfid::rpc::merge_mining_get_chain_id",
+                    "[RPC] Error looking up genesis block: {}", e,
+                );
+                return JsonError::new(InternalError, None, id).into()
+            }
+        };
+
+        JsonResponse::new(
+            JsonValue::Object(HashMap::from([(
+                "chain_id".to_string(),
+                chain_id.to_hex().to_string().into(),
+            )])),
+            id,
+        )
+        .into()
+    }
 }