From ac12b2194bbfd103e79ac03fc1bdfb79b0659829 Mon Sep 17 00:00:00 2001 From: Sick Man <81191073+ISickMan@users.noreply.github.com> Date: Sat, 20 May 2023 19:14:22 +0300 Subject: [PATCH] fix verus difficulty (#10) Co-authored-by: ItsSickGuy <81191073+ItsSickGuy@users.noreply.github.com> --- ccminer.cpp | 14 +++++------ equi/equi-stratum.cpp | 56 ++++++++++++++++++++++++++++++++++++------- miner.h | 1 + 3 files changed, 55 insertions(+), 16 deletions(-) diff --git a/ccminer.cpp b/ccminer.cpp index 58e4c82d9..2017ab37a 100644 --- a/ccminer.cpp +++ b/ccminer.cpp @@ -664,16 +664,16 @@ bool jobj_binary(const json_t *obj, const char *key, void *buf, size_t buflen) return true; } - + /* compute nbits to get the network diff */ static void calc_network_diff(struct work *work) { if (opt_algo == ALGO_EQUIHASH) { - net_diff = equi_network_diff(work); - return; + // net_diff = equi_network_diff(work); + net_diff = verus_network_diff(work); + return; } - } @@ -1013,7 +1013,7 @@ static bool submit_upstream_work(CURL *curl, struct work *work) if (net_diff && stratum.sharediff > net_diff && (opt_debug || opt_debug_diff)) applog(LOG_INFO, "share diff: %.5f, possible block found!!!", - stratum.sharediff); + stratum.sharediff); else if (opt_debug_diff) applog(LOG_DEBUG, "share diff: %.5f (x %.1f)", stratum.sharediff, work->shareratio[idnonce]); @@ -1042,8 +1042,7 @@ static bool submit_upstream_work(CURL *curl, struct work *work) if (check_dups || opt_showdiff) hashlog_remember_submit(work, nonce); stratum.job.shares_count++; - - } else { + } else { int data_size = 128; int adata_sz = data_size / sizeof(uint32_t); @@ -1707,6 +1706,7 @@ static bool stratum_gen_work(struct stratum_ctx *sctx, struct work *work) work_set_target(work, sctx->job.diff / (128.0 * opt_difficulty)); break; case ALGO_EQUIHASH: + memcpy(work->target, sctx->job.extra, 32); equi_work_set_target(work, sctx->job.diff / opt_difficulty); break; default: diff --git a/equi/equi-stratum.cpp b/equi/equi-stratum.cpp index c5670de56..2670fbcb5 100644 --- a/equi/equi-stratum.cpp +++ b/equi/equi-stratum.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include "equihash.h" @@ -33,6 +34,12 @@ double target_to_diff_equi(uint32_t* target) return (double)0xffff0000UL/m; } +double target_to_diff_verus(uint32_t target){ + const unsigned exponent_diff = 8 * (0x20 - ((target >> 24) & 0xFF)); + const double significand = target & 0xFFFFFF; + return std::ldexp(0x0f0f0f / significand, exponent_diff); +} + void diff_to_target_equi(uint32_t *target, double diff) { uint64_t m; @@ -73,19 +80,29 @@ double equi_network_diff(struct work *work) net_target[31-b] = ((uint8_t*)&tgt64)[b]; // applog_hex(net_target, 32); //for (int i = 0; i < 8; i++)work->target[i] = ((uint32_t*)(&net_target))[i]; - + double d = target_to_diff_equi((uint32_t*)net_target); return d; } +double verus_network_diff(struct work *work) +{ + uint32_t nbits = work->data[26]; + + double d = target_to_diff_verus(nbits); + // applog(LOG_BLUE, "target nbits: %08x", nbits); + // applog(LOG_BLUE, "target diff: %f", d); + return d; +} + void equi_work_set_target(struct work* work, double diff) { // target is given as data by the equihash stratum // memcpy(work->target, stratum.job.claim, 32); // claim field is only used for lbry - diff_to_target_equi(work->target, diff); - //applog(LOG_BLUE, "diff %f to target :", diff); - //applog_hex(work->target, 32); + // diff_to_target_equi(work->target, diff); // we already set the target work->targetdiff = diff; + // applog(LOG_BLUE, "diff %f to target :", diff); + // applog_hex(work->target, 32); } bool equi_stratum_set_target(struct stratum_ctx *sctx, json_t *params) @@ -98,16 +115,38 @@ bool equi_stratum_set_target(struct stratum_ctx *sctx, json_t *params) hex2bin(target_bin, target_hex, 32); memset(target_be, 0x00, 32); + + uint8_t *bits_start = nullptr; int filled = 0; - for (int i=0; i<32; i++) { + for (int i = 0; i < 32; i++) + { if (filled == 8) break; - target_be[31-i] = target_bin[i]; - if (target_bin[i]) filled++; + target_be[31 - i] = target_bin[i]; + if (target_bin[i]) + { + filled++; + if(bits_start == nullptr) + bits_start = &target_bin[i]; + } } + + int padding = &target_bin[31] - bits_start; + + uint32_t target_bits; + uint8_t exponent = ((padding * 8 + 1) + 7) / 8; + + memcpy(&target_bits, &target_be[exponent - 3], 3); // coefficient + target_bits |= (exponent << 24); // exponent + + // applog_hex(target_bin, 32); + // applog_hex(target_be, 32); + // applog(LOG_BLUE, "target_bits %08x", target_bits); + memcpy(sctx->job.extra, target_be, 32); pthread_mutex_lock(&stratum_work_lock); - sctx->next_diff = target_to_diff_equi((uint32_t*) &target_be); + // sctx->next_diff = target_to_diff_equi((uint32_t*) &target_be); + sctx->next_diff = target_to_diff_verus(target_bits); pthread_mutex_unlock(&stratum_work_lock); //applog(LOG_BLUE, "low diff %f", sctx->next_diff); @@ -228,7 +267,6 @@ void equi_store_work_solution(struct work* work, uint32_t* hash, void* sol_data) int nonce = work->valid_nonces-1; memcpy(work->extra, sol_data, 1347); bn_store_hash_target_ratio(hash, work->target, work, nonce); - //work->sharediff[nonce] = target_to_diff_equi(hash); } #define JSON_SUBMIT_BUF_LEN (20*1024) diff --git a/miner.h b/miner.h index fc726a93f..d418622d8 100644 --- a/miner.h +++ b/miner.h @@ -710,6 +710,7 @@ void equi_work_set_target(struct work* work, double diff); void equi_store_work_solution(struct work* work, uint32_t* hash, void* sol_data); int equi_verify_sol(void * const hdr, void * const sol); double equi_network_diff(struct work *work); +double verus_network_diff(struct work *work); void hashlog_remember_submit(struct work* work, uint32_t nonce); void hashlog_remember_scan_range(struct work* work);