Skip to content

Commit

Permalink
fix verus difficulty (#10)
Browse files Browse the repository at this point in the history
Co-authored-by: ItsSickGuy <[email protected]>
  • Loading branch information
avaziman and avaziman authored May 20, 2023
1 parent 3b78957 commit ac12b21
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 16 deletions.
14 changes: 7 additions & 7 deletions ccminer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


}

Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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:
Expand Down
56 changes: 47 additions & 9 deletions equi/equi-stratum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <cmath>
#include <miner.h>

#include "equihash.h"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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);
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit ac12b21

Please sign in to comment.