mmproxy/stratum: Mining target calculation

This commit is contained in:
parazyd
2023-11-19 11:21:10 +01:00
parent fd934a8e3a
commit b8b02aee80
3 changed files with 20 additions and 2 deletions

1
Cargo.lock generated
View File

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

View File

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

View File

@@ -31,6 +31,7 @@ use darkfi::{
};
use log::{debug, error, info, warn};
use monero::blockdata::transaction::{ExtraField, RawExtraField, SubField::MergeMining};
use num_bigint::BigUint;
use smol::{channel, lock::RwLock};
use url::Url;
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
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>()
.unwrap()
.strip_prefix("0x")
.unwrap()
.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
let height = *rep["result"]["height"].get::<f64>().unwrap();
let seed_hash = rep["result"]["seed_hash"].get::<String>().unwrap().to_string();