Skip to content

Commit ac12b21

Browse files
authored
fix verus difficulty (#10)
Co-authored-by: ItsSickGuy <[email protected]>
1 parent 3b78957 commit ac12b21

File tree

3 files changed

+55
-16
lines changed

3 files changed

+55
-16
lines changed

ccminer.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -664,16 +664,16 @@ bool jobj_binary(const json_t *obj, const char *key, void *buf, size_t buflen)
664664

665665
return true;
666666
}
667-
667+
668668
/* compute nbits to get the network diff */
669669
static void calc_network_diff(struct work *work)
670670
{
671671

672672
if (opt_algo == ALGO_EQUIHASH) {
673-
net_diff = equi_network_diff(work);
674-
return;
673+
// net_diff = equi_network_diff(work);
674+
net_diff = verus_network_diff(work);
675+
return;
675676
}
676-
677677

678678
}
679679

@@ -1013,7 +1013,7 @@ static bool submit_upstream_work(CURL *curl, struct work *work)
10131013

10141014
if (net_diff && stratum.sharediff > net_diff && (opt_debug || opt_debug_diff))
10151015
applog(LOG_INFO, "share diff: %.5f, possible block found!!!",
1016-
stratum.sharediff);
1016+
stratum.sharediff);
10171017
else if (opt_debug_diff)
10181018
applog(LOG_DEBUG, "share diff: %.5f (x %.1f)",
10191019
stratum.sharediff, work->shareratio[idnonce]);
@@ -1042,8 +1042,7 @@ static bool submit_upstream_work(CURL *curl, struct work *work)
10421042
if (check_dups || opt_showdiff)
10431043
hashlog_remember_submit(work, nonce);
10441044
stratum.job.shares_count++;
1045-
1046-
} else {
1045+
} else {
10471046

10481047
int data_size = 128;
10491048
int adata_sz = data_size / sizeof(uint32_t);
@@ -1707,6 +1706,7 @@ static bool stratum_gen_work(struct stratum_ctx *sctx, struct work *work)
17071706
work_set_target(work, sctx->job.diff / (128.0 * opt_difficulty));
17081707
break;
17091708
case ALGO_EQUIHASH:
1709+
memcpy(work->target, sctx->job.extra, 32);
17101710
equi_work_set_target(work, sctx->job.diff / opt_difficulty);
17111711
break;
17121712
default:

equi/equi-stratum.cpp

+47-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <errno.h>
77
#include <string.h>
88
#include <unistd.h>
9+
#include <cmath>
910
#include <miner.h>
1011

1112
#include "equihash.h"
@@ -33,6 +34,12 @@ double target_to_diff_equi(uint32_t* target)
3334
return (double)0xffff0000UL/m;
3435
}
3536

37+
double target_to_diff_verus(uint32_t target){
38+
const unsigned exponent_diff = 8 * (0x20 - ((target >> 24) & 0xFF));
39+
const double significand = target & 0xFFFFFF;
40+
return std::ldexp(0x0f0f0f / significand, exponent_diff);
41+
}
42+
3643
void diff_to_target_equi(uint32_t *target, double diff)
3744
{
3845
uint64_t m;
@@ -73,19 +80,29 @@ double equi_network_diff(struct work *work)
7380
net_target[31-b] = ((uint8_t*)&tgt64)[b];
7481
// applog_hex(net_target, 32);
7582
//for (int i = 0; i < 8; i++)work->target[i] = ((uint32_t*)(&net_target))[i];
76-
83+
7784
double d = target_to_diff_equi((uint32_t*)net_target);
7885
return d;
7986
}
8087

88+
double verus_network_diff(struct work *work)
89+
{
90+
uint32_t nbits = work->data[26];
91+
92+
double d = target_to_diff_verus(nbits);
93+
// applog(LOG_BLUE, "target nbits: %08x", nbits);
94+
// applog(LOG_BLUE, "target diff: %f", d);
95+
return d;
96+
}
97+
8198
void equi_work_set_target(struct work* work, double diff)
8299
{
83100
// target is given as data by the equihash stratum
84101
// memcpy(work->target, stratum.job.claim, 32); // claim field is only used for lbry
85-
diff_to_target_equi(work->target, diff);
86-
//applog(LOG_BLUE, "diff %f to target :", diff);
87-
//applog_hex(work->target, 32);
102+
// diff_to_target_equi(work->target, diff); // we already set the target
88103
work->targetdiff = diff;
104+
// applog(LOG_BLUE, "diff %f to target :", diff);
105+
// applog_hex(work->target, 32);
89106
}
90107

91108
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)
98115

99116
hex2bin(target_bin, target_hex, 32);
100117
memset(target_be, 0x00, 32);
118+
119+
uint8_t *bits_start = nullptr;
101120
int filled = 0;
102-
for (int i=0; i<32; i++) {
121+
for (int i = 0; i < 32; i++)
122+
{
103123
if (filled == 8) break;
104-
target_be[31-i] = target_bin[i];
105-
if (target_bin[i]) filled++;
124+
target_be[31 - i] = target_bin[i];
125+
if (target_bin[i])
126+
{
127+
filled++;
128+
if(bits_start == nullptr)
129+
bits_start = &target_bin[i];
130+
}
106131
}
132+
133+
int padding = &target_bin[31] - bits_start;
134+
135+
uint32_t target_bits;
136+
uint8_t exponent = ((padding * 8 + 1) + 7) / 8;
137+
138+
memcpy(&target_bits, &target_be[exponent - 3], 3); // coefficient
139+
target_bits |= (exponent << 24); // exponent
140+
141+
// applog_hex(target_bin, 32);
142+
// applog_hex(target_be, 32);
143+
// applog(LOG_BLUE, "target_bits %08x", target_bits);
144+
107145
memcpy(sctx->job.extra, target_be, 32);
108146

109147
pthread_mutex_lock(&stratum_work_lock);
110-
sctx->next_diff = target_to_diff_equi((uint32_t*) &target_be);
148+
// sctx->next_diff = target_to_diff_equi((uint32_t*) &target_be);
149+
sctx->next_diff = target_to_diff_verus(target_bits);
111150
pthread_mutex_unlock(&stratum_work_lock);
112151

113152
//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)
228267
int nonce = work->valid_nonces-1;
229268
memcpy(work->extra, sol_data, 1347);
230269
bn_store_hash_target_ratio(hash, work->target, work, nonce);
231-
//work->sharediff[nonce] = target_to_diff_equi(hash);
232270
}
233271

234272
#define JSON_SUBMIT_BUF_LEN (20*1024)

miner.h

+1
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,7 @@ void equi_work_set_target(struct work* work, double diff);
710710
void equi_store_work_solution(struct work* work, uint32_t* hash, void* sol_data);
711711
int equi_verify_sol(void * const hdr, void * const sol);
712712
double equi_network_diff(struct work *work);
713+
double verus_network_diff(struct work *work);
713714

714715
void hashlog_remember_submit(struct work* work, uint32_t nonce);
715716
void hashlog_remember_scan_range(struct work* work);

0 commit comments

Comments
 (0)