forked from tpruvot/ccminer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashlog.cpp
More file actions
361 lines (332 loc) · 8.87 KB
/
hashlog.cpp
File metadata and controls
361 lines (332 loc) · 8.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/**
* Hash log of submitted job nonces
<<<<<<< HEAD
* Prevent duplicate shares and remember shares diff
*
* (to be merged later with stats)
*
* tpruvot@github 2014 - 2017
=======
* Prevent duplicate shares
*
* (to be merged later with stats)
*
* tpruvot@github 2014
>>>>>>> 8c320ca... added xevan
*/
#include <stdlib.h>
#include <memory.h>
#include <map>
#include "miner.h"
#define HI_DWORD(u64) ((uint32_t) (u64 >> 32))
#define LO_DWORD(u64) ((uint32_t) u64)
#define MK_HI64(u32) (0x100000000ULL * u32)
/* from miner.h
struct hashlog_data {
<<<<<<< HEAD
uint8_t npool;
uint8_t pool_type;
uint8_t nonce_id;
uint8_t job_nonce_id;
uint32_t height;
double sharediff;
uint32_t njobid;
uint32_t nonce;
=======
uint32_t tm_sent;
uint32_t height;
>>>>>>> 8c320ca... added xevan
uint32_t scanned_from;
uint32_t scanned_to;
uint32_t last_from;
uint32_t tm_add;
uint32_t tm_upd;
<<<<<<< HEAD
uint32_t tm_sent;
=======
>>>>>>> 8c320ca... added xevan
};
*/
static std::map<uint64_t, hashlog_data> tlastshares;
#define LOG_PURGE_TIMEOUT 5*60
<<<<<<< HEAD
extern struct stratum_ctx stratum;
=======
>>>>>>> 8c320ca... added xevan
/**
* str hex to uint32
*/
static uint64_t hextouint(char* jobid)
{
char *ptr;
/* dont use strtoull(), only since VS2013 */
return (uint64_t) strtoul(jobid, &ptr, 16);
}
/**
* @return time of a job/nonce submission (or last nonce if nonce is 0)
*/
uint32_t hashlog_already_submittted(char* jobid, uint32_t nonce)
{
uint32_t ret = 0;
uint64_t njobid = hextouint(jobid);
uint64_t key = (njobid << 32) + nonce;
if (nonce == 0) {
// search last submitted nonce for job
ret = hashlog_get_last_sent(jobid);
} else if (tlastshares.find(key) != tlastshares.end()) {
hashlog_data data = tlastshares[key];
ret = data.tm_sent;
}
return ret;
}
/**
* Store submitted nonces of a job
*/
void hashlog_remember_submit(struct work* work, uint32_t nonce)
{
uint64_t njobid = hextouint(work->job_id);
uint64_t key = (njobid << 32) + nonce;
hashlog_data data;
memset(&data, 0, sizeof(data));
<<<<<<< HEAD
data.nonce_id = work->submit_nonce_id;
data.scanned_from = work->scanned_from;
data.scanned_to = work->scanned_to;
data.sharediff = work->sharediff[data.nonce_id];
data.height = work->height;
data.njobid = (uint32_t) njobid;
data.tm_add = data.tm_upd = data.tm_sent = (uint32_t) time(NULL);
data.npool = (uint8_t) cur_pooln;
data.pool_type = pools[cur_pooln].type;
data.job_nonce_id = (uint8_t) stratum.job.shares_count;
=======
data.scanned_from = work->scanned_from;
data.scanned_to = nonce;
data.height = work->height;
data.njobid = (uint32_t) njobid;
data.tm_add = data.tm_upd = data.tm_sent = (uint32_t) time(NULL);
>>>>>>> 8c320ca... added xevan
tlastshares[key] = data;
}
/**
* Update job scanned range
*/
void hashlog_remember_scan_range(struct work* work)
{
uint64_t njobid = hextouint(work->job_id);
uint64_t key = (njobid << 32);
uint64_t range = hashlog_get_scan_range(work->job_id);
hashlog_data data;
// global scan range of a job
data = tlastshares[key];
if (range == 0) {
memset(&data, 0, sizeof(data));
data.njobid = (uint32_t) njobid;
} else {
// get min and max from all sent records
data.scanned_from = LO_DWORD(range);
data.scanned_to = HI_DWORD(range);
}
if (data.tm_add == 0)
data.tm_add = (uint32_t) time(NULL);
data.last_from = work->scanned_from;
if (work->scanned_from < work->scanned_to) {
if (data.scanned_to == 0 || work->scanned_from == data.scanned_to + 1)
data.scanned_to = work->scanned_to;
if (data.scanned_from == 0)
data.scanned_from = work->scanned_from ? work->scanned_from : 1; // min 1
else if (work->scanned_from < data.scanned_from || work->scanned_to == (data.scanned_from - 1))
data.scanned_from = work->scanned_from;
}
data.tm_upd = (uint32_t) time(NULL);
tlastshares[key] = data;
/* applog(LOG_BLUE, "job %s range : %x %x -> %x %x", jobid,
scanned_from, scanned_to, data.scanned_from, data.scanned_to); */
}
/**
* Returns the range of a job
* @return uint64_t to|from
*/
uint64_t hashlog_get_scan_range(char* jobid)
{
uint64_t ret = 0;
uint64_t njobid = hextouint(jobid);
uint64_t keypfx = (njobid << 32);
uint64_t keymsk = (0xffffffffULL << 32);
hashlog_data data;
data.scanned_from = 0;
data.scanned_to = 0;
std::map<uint64_t, hashlog_data>::iterator i = tlastshares.begin();
while (i != tlastshares.end()) {
if ((keymsk & i->first) == keypfx && i->second.scanned_to > ret) {
if (i->second.scanned_to > data.scanned_to)
data.scanned_to = i->second.scanned_to;
if (i->second.scanned_from < data.scanned_from || data.scanned_from == 0)
data.scanned_from = i->second.scanned_from;
}
i++;
}
ret = data.scanned_from;
ret += MK_HI64(data.scanned_to);
return ret;
}
/**
* Search last submitted nonce for a job
* @return max nonce
*/
uint32_t hashlog_get_last_sent(char* jobid)
{
uint32_t nonce = 0;
<<<<<<< HEAD
uint64_t njobid = jobid ? hextouint(jobid) : UINT32_MAX;
uint64_t keypfx = (njobid << 32);
std::map<uint64_t, hashlog_data>::reverse_iterator i = tlastshares.rbegin();
while (i != tlastshares.rend()) {
if ((keypfx & i->first) == keypfx && i->second.tm_sent) {
nonce = LO_DWORD(i->first);
break;
=======
uint64_t njobid = hextouint(jobid);
uint64_t keypfx = (njobid << 32);
std::map<uint64_t, hashlog_data>::iterator i = tlastshares.begin();
while (i != tlastshares.end()) {
if ((keypfx & i->first) == keypfx && i->second.tm_sent > 0) {
nonce = LO_DWORD(i->first);
>>>>>>> 8c320ca... added xevan
}
i++;
}
return nonce;
}
/**
<<<<<<< HEAD
* To display correcly second nonce(s) share diff (on pool accept)
*/
double hashlog_get_sharediff(char* jobid, int job_nonceid, double defvalue)
{
double diff = defvalue;
const uint64_t njobid = jobid ? hextouint(jobid) : UINT32_MAX;
const uint64_t keypfx = (njobid << 32);
const uint64_t keymsk = (0xffffffffULL << 32);
std::map<uint64_t, hashlog_data>::reverse_iterator it = tlastshares.rbegin();
while (it != tlastshares.rend()) {
if ((keymsk & it->first) == keypfx) {
if ((int) it->second.job_nonce_id == job_nonceid && it->second.tm_sent) {
diff = it->second.sharediff;
// applog(LOG_BLUE, "sharediff nonce %x:%d (%d) match %g",
// njobid, (int) it->second.nonce_id, job_nonceid, diff);
break;
}
}
++it;
}
return diff;
}
/**
=======
>>>>>>> 8c320ca... added xevan
* Export data for api calls
*/
int hashlog_get_history(struct hashlog_data *data, int max_records)
{
int records = 0;
std::map<uint64_t, hashlog_data>::reverse_iterator it = tlastshares.rbegin();
while (it != tlastshares.rend() && records < max_records) {
memcpy(&data[records], &(it->second), sizeof(struct hashlog_data));
data[records].nonce = LO_DWORD(it->first);
data[records].njobid = (uint32_t) HI_DWORD(it->first);
records++;
++it;
}
return records;
}
/**
* Remove entries of a job...
*/
void hashlog_purge_job(char* jobid)
{
int deleted = 0;
uint64_t njobid = hextouint(jobid);
uint64_t keypfx = (njobid << 32);
<<<<<<< HEAD
uint32_t sz = (uint32_t) tlastshares.size();
=======
uint32_t sz = tlastshares.size();
>>>>>>> 8c320ca... added xevan
std::map<uint64_t, hashlog_data>::iterator i = tlastshares.begin();
while (i != tlastshares.end()) {
if ((keypfx & i->first) == keypfx) {
deleted++;
tlastshares.erase(i++);
}
else ++i;
}
if (opt_debug && deleted) {
applog(LOG_DEBUG, "hashlog: purge job %s, del %d/%d", jobid, deleted, sz);
}
}
/**
* Remove old entries to reduce memory usage
*/
void hashlog_purge_old(void)
{
int deleted = 0;
uint32_t now = (uint32_t) time(NULL);
<<<<<<< HEAD
uint32_t sz = (uint32_t) tlastshares.size();
=======
uint32_t sz = tlastshares.size();
>>>>>>> 8c320ca... added xevan
std::map<uint64_t, hashlog_data>::iterator i = tlastshares.begin();
while (i != tlastshares.end()) {
if ((now - i->second.tm_sent) > LOG_PURGE_TIMEOUT) {
deleted++;
tlastshares.erase(i++);
}
else ++i;
}
if (opt_debug && deleted) {
applog(LOG_DEBUG, "hashlog: %d/%d purged", deleted, sz);
}
}
/**
* Reset the submitted nonces cache
*/
void hashlog_purge_all(void)
{
tlastshares.clear();
}
/**
* API meminfo
*/
void hashlog_getmeminfo(uint64_t *mem, uint32_t *records)
{
<<<<<<< HEAD
(*records) = (uint32_t) tlastshares.size();
=======
(*records) = tlastshares.size();
>>>>>>> 8c320ca... added xevan
(*mem) = (*records) * sizeof(hashlog_data);
}
/**
* Used to debug ranges...
*/
void hashlog_dump_job(char* jobid)
{
if (opt_debug) {
uint64_t njobid = hextouint(jobid);
uint64_t keypfx = (njobid << 32);
// uint32_t sz = tlastshares.size();
std::map<uint64_t, hashlog_data>::iterator i = tlastshares.begin();
while (i != tlastshares.end()) {
if ((keypfx & i->first) == keypfx) {
if (i->first != keypfx)
applog(LOG_DEBUG, CL_YLW "job %s, found %08x ", jobid, LO_DWORD(i->first));
else
applog(LOG_DEBUG, CL_YLW "job %s(%u) range done: %08x-%08x", jobid,
i->second.height, i->second.scanned_from, i->second.scanned_to);
}
i++;
}
}
}