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

mmproxy/monero: getblocktemplate stub and TODO

parazyd 2 лет назад
Родитель
Сommit
ed4ac96e44
2 измененных файлов с 53 добавлено и 3 удалено
  1. 2 1
      bin/darkfi-mmproxy/src/main.rs
  2. 51 2
      bin/darkfi-mmproxy/src/monero.rs

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

@@ -159,9 +159,10 @@ impl RequestHandler for MiningProxy {
             "getblockcount" => self.monero_get_block_count(req.id, req.params).await,
             "on_get_block_hash" => self.monero_on_get_block_hash(req.id, req.params).await,
             "on_getblockhash" => self.monero_on_get_block_hash(req.id, req.params).await,
+            "get_block_template" => self.monero_get_block_template(req.id, req.params).await,
+            "getblocktemplate" => self.monero_get_block_template(req.id, req.params).await,
 
             /*
-            "get_block_template" => self.monero_get_block_template(req.id, req.params).await,
             "submit_block" => self.monero_submit_block(req.id, req.params).await,
             "generateblocks" => self.monero_generateblocks(req.id, req.params).await,
             "get_last_block_header" => self.monero_get_last_block_header(req.id, req.params).await,

+ 51 - 2
bin/darkfi-mmproxy/src/monero.rs

@@ -16,6 +16,8 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+use std::collections::HashMap;
+
 use darkfi::{
     rpc::{
         jsonrpc::{
@@ -118,11 +120,58 @@ impl MiningProxy {
         JsonResponse::new(rep, id).into()
     }
 
-    /*
     pub async fn monero_get_block_template(&self, id: u16, params: JsonValue) -> JsonResult {
-        todo!()
+        debug!(target: "rpc::monero", "get_block_template()");
+
+        if !params.is_object() {
+            return JsonError::new(InvalidParams, None, id).into()
+        }
+
+        let params = params.get::<HashMap<String, JsonValue>>().unwrap();
+
+        if !params.contains_key("wallet_address") || !params.contains_key("reserve_size") {
+            return JsonError::new(InvalidParams, None, id).into()
+        }
+
+        let Some(wallet_address) = params["wallet_address"].get::<String>() else {
+            return JsonError::new(InvalidParams, None, id).into()
+        };
+
+        let Some(reserve_size) = params["reserve_size"].get::<f64>() else {
+            return JsonError::new(InvalidParams, None, id).into()
+        };
+
+        // Create request
+        let req = JsonRequest::new(
+            "get_block_template",
+            HashMap::from([
+                ("wallet_address".to_string(), (*wallet_address).clone().into()),
+                ("reserve_size".to_string(), (*reserve_size).into()),
+            ])
+            .into(),
+        );
+
+        let rep = match self.oneshot_request(req).await {
+            Ok(v) => v,
+            Err(e) => {
+                error!(target: "rpc::monero::get_block_template", "{}", e);
+                return JsonError::new(InternalError, Some(e.to_string()), id).into()
+            }
+        };
+
+        // TODO: Now we have to modify the block template.
+        // * reserve_size has to be the size of the data we want to put in the block
+        // * blocktemplate_blob has the reserved bytes, they're in the tx_extra field
+        //   of the coinbase tx, which is then hashed with other transactions into
+        //   merkle root hash which is what's in the blockhashing_blob
+        // * When blocktemplate_blob is modified, blockhashing_blob has to be updated too
+        // * The coinbase tx from monerod should be replaced with the one we create
+        //   containing the merged mining data/info
+
+        JsonResponse::new(rep, id).into()
     }
 
+    /*
     pub async fn monero_submit_block(&self, id: u16, params: JsonValue) -> JsonResult {
         todo!()
     }