6
6
#include < errno.h>
7
7
#include < string.h>
8
8
#include < unistd.h>
9
+ #include < cmath>
9
10
#include < miner.h>
10
11
11
12
#include " equihash.h"
@@ -33,6 +34,12 @@ double target_to_diff_equi(uint32_t* target)
33
34
return (double )0xffff0000UL /m;
34
35
}
35
36
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
+
36
43
void diff_to_target_equi (uint32_t *target, double diff)
37
44
{
38
45
uint64_t m;
@@ -73,19 +80,29 @@ double equi_network_diff(struct work *work)
73
80
net_target[31 -b] = ((uint8_t *)&tgt64)[b];
74
81
// applog_hex(net_target, 32);
75
82
// for (int i = 0; i < 8; i++)work->target[i] = ((uint32_t*)(&net_target))[i];
76
-
83
+
77
84
double d = target_to_diff_equi ((uint32_t *)net_target);
78
85
return d;
79
86
}
80
87
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
+
81
98
void equi_work_set_target (struct work * work, double diff)
82
99
{
83
100
// target is given as data by the equihash stratum
84
101
// 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
88
103
work->targetdiff = diff;
104
+ // applog(LOG_BLUE, "diff %f to target :", diff);
105
+ // applog_hex(work->target, 32);
89
106
}
90
107
91
108
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)
98
115
99
116
hex2bin (target_bin, target_hex, 32 );
100
117
memset (target_be, 0x00 , 32 );
118
+
119
+ uint8_t *bits_start = nullptr ;
101
120
int filled = 0 ;
102
- for (int i=0 ; i<32 ; i++) {
121
+ for (int i = 0 ; i < 32 ; i++)
122
+ {
103
123
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
+ }
106
131
}
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
+
107
145
memcpy (sctx->job .extra , target_be, 32 );
108
146
109
147
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);
111
150
pthread_mutex_unlock (&stratum_work_lock);
112
151
113
152
// 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)
228
267
int nonce = work->valid_nonces -1 ;
229
268
memcpy (work->extra , sol_data, 1347 );
230
269
bn_store_hash_target_ratio (hash, work->target , work, nonce);
231
- // work->sharediff[nonce] = target_to_diff_equi(hash);
232
270
}
233
271
234
272
#define JSON_SUBMIT_BUF_LEN (20 *1024 )
0 commit comments