Bläddra i källkod

mmproxy/stratum: Mining target calculation

parazyd 2 år sedan
förälder
incheckning
b8b02aee80
3 ändrade filer med 20 tillägg och 2 borttagningar
  1. 1 0
      Cargo.lock
  2. 1 0
      bin/darkfi-mmproxy/Cargo.toml
  3. 18 2
      bin/darkfi-mmproxy/src/stratum.rs

+ 1 - 0
Cargo.lock

@@ -1813,6 +1813,7 @@ dependencies = [
  "hex",
  "hex",
  "log",
  "log",
  "monero",
  "monero",
+ "num-bigint",
  "serde",
  "serde",
  "signal-hook",
  "signal-hook",
  "signal-hook-async-std",
  "signal-hook-async-std",

+ 1 - 0
bin/darkfi-mmproxy/Cargo.toml

@@ -23,6 +23,7 @@ surf = "2.3.2"
 # Encoding
 # Encoding
 bs58 = "0.5.0"
 bs58 = "0.5.0"
 hex = "0.4.3"
 hex = "0.4.3"
+num-bigint = "0.4.4"
 url = "2.4.1"
 url = "2.4.1"
 uuid = {version = "1.5.0", features = ["v4"]}
 uuid = {version = "1.5.0", features = ["v4"]}
 
 

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

@@ -31,6 +31,7 @@ use darkfi::{
 };
 };
 use log::{debug, error, info, warn};
 use log::{debug, error, info, warn};
 use monero::blockdata::transaction::{ExtraField, RawExtraField, SubField::MergeMining};
 use monero::blockdata::transaction::{ExtraField, RawExtraField, SubField::MergeMining};
+use num_bigint::BigUint;
 use smol::{channel, lock::RwLock};
 use smol::{channel, lock::RwLock};
 use url::Url;
 use url::Url;
 use uuid::Uuid;
 use uuid::Uuid;
@@ -203,14 +204,29 @@ async fn getblocktemplate(endpoint: &Url, wallet_address: &monero::Address) -> R
     // Modify the coinbase tx with our additional merge mining data
     // Modify the coinbase tx with our additional merge mining data
     block_template.miner_tx.prefix.extra = tx_extra;
     block_template.miner_tx.prefix.extra = tx_extra;
 
 
-    // Get the difficulty target
-    let target = rep["result"]["wide_difficulty"]
+    // Decode the difficulty and calculate the mining target
+    let mut difficulty_hex = rep["result"]["wide_difficulty"]
         .get::<String>()
         .get::<String>()
         .unwrap()
         .unwrap()
         .strip_prefix("0x")
         .strip_prefix("0x")
         .unwrap()
         .unwrap()
         .to_string();
         .to_string();
 
 
+    // Needed because hex::decode doesn't accept odd-length
+    if difficulty_hex.len() % 2 == 0 {
+        difficulty_hex = format!("0{}", difficulty_hex);
+    }
+
+    // TODO: Check if this is little or big-endian
+    let difficulty_raw = hex::decode(&difficulty_hex).unwrap();
+    let difficulty = BigUint::from_radix_le(&difficulty_raw, 16).unwrap();
+
+    // Calculate the target
+    let target = (BigUint::from_bytes_be(&[0xFF; 32]) / &difficulty).to_str_radix(16);
+
+    info!(target: "stratum::getblocktemplate", "[STRATUM] Difficulty: {}", difficulty_hex);
+    info!(target: "stratum::getblocktemplate", "[STRATUM] Target: {}", target);
+
     // Get the remaining metadata
     // Get the remaining metadata
     let height = *rep["result"]["height"].get::<f64>().unwrap();
     let height = *rep["result"]["height"].get::<f64>().unwrap();
     let seed_hash = rep["result"]["seed_hash"].get::<String>().unwrap().to_string();
     let seed_hash = rep["result"]["seed_hash"].get::<String>().unwrap().to_string();