forked from DeckerSU/yiimp-stratum-equihash
-
Notifications
You must be signed in to change notification settings - Fork 3
/
client_submit.cpp
877 lines (715 loc) · 30.5 KB
/
client_submit.cpp
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
#include "stratum.h"
uint64_t lyra2z_height = 0;
//#define MERKLE_DEBUGLOG
//#define DONTSUBMIT
void build_submit_values(YAAMP_JOB_VALUES *submitvalues, YAAMP_JOB_TEMPLATE *templ,
const char *nonce1, const char *nonce2, const char *ntime, const char *nonce)
{
sprintf(submitvalues->coinbase, "%s%s%s%s", templ->coinb1, nonce1, nonce2, templ->coinb2);
int coinbase_len = strlen(submitvalues->coinbase);
unsigned char coinbase_bin[1024];
memset(coinbase_bin, 0, 1024);
binlify(coinbase_bin, submitvalues->coinbase);
char doublehash[128];
memset(doublehash, 0, 128);
// some (old) wallet/algos need a simple SHA256 (blakecoin, whirlcoin, groestlcoin...)
YAAMP_HASH_FUNCTION merkle_hash = sha256_double_hash_hex;
if (g_current_algo->merkle_func)
merkle_hash = g_current_algo->merkle_func;
merkle_hash((char *)coinbase_bin, doublehash, coinbase_len/2);
string merkleroot = merkle_with_first(templ->txsteps, doublehash);
ser_string_be(merkleroot.c_str(), submitvalues->merkleroot_be, 8);
#ifdef MERKLE_DEBUGLOG
printf("merkle root %s\n", merkleroot.c_str());
#endif
if (!strcmp(g_stratum_algo, "lbry")) {
sprintf(submitvalues->header, "%s%s%s%s%s%s%s", templ->version, templ->prevhash_be, submitvalues->merkleroot_be,
templ->claim_be, ntime, templ->nbits, nonce);
ser_string_be(submitvalues->header, submitvalues->header_be, 112/4);
} else if (strlen(templ->extradata_be) == 128) { // LUX SC
sprintf(submitvalues->header, "%s%s%s%s%s%s%s", templ->version, templ->prevhash_be, submitvalues->merkleroot_be,
ntime, templ->nbits, nonce, templ->extradata_be);
ser_string_be(submitvalues->header, submitvalues->header_be, 36); // 80+64 / sizeof(u32)
} else {
sprintf(submitvalues->header, "%s%s%s%s%s%s", templ->version, templ->prevhash_be, submitvalues->merkleroot_be,
ntime, templ->nbits, nonce);
ser_string_be(submitvalues->header, submitvalues->header_be, 20);
}
binlify(submitvalues->header_bin, submitvalues->header_be);
// printf("%s\n", submitvalues->header_be);
int header_len = strlen(submitvalues->header)/2;
g_current_algo->hash_function((char *)submitvalues->header_bin, (char *)submitvalues->hash_bin, header_len);
hexlify(submitvalues->hash_hex, submitvalues->hash_bin, 32);
string_be(submitvalues->hash_hex, submitvalues->hash_be);
}
/////////////////////////////////////////////
void build_submit_values_equi(YAAMP_JOB_VALUES *submitvalues, YAAMP_JOB_TEMPLATE *templ,
const char *nonce1, const char *nonce2, const char *ntime, const char *nonce, const char *equi_solution)
{
/*
std::cerr << "build_submit_values_equi" << std::endl;
std::cerr << "------------------------" << std::endl;
std::cerr << "nonce1 = " << nonce1 << std::endl;
std::cerr << "nonce2 = " << nonce2 << std::endl;
std::cerr << "ntime = " << ntime << " (" << templ->ntime << ")" << std::endl;
std::cerr << "nonce = " << nonce << std::endl;
*/
sprintf(submitvalues->coinbase, "%s", templ->coinbase);
int coinbase_len = strlen(submitvalues->coinbase);
//std::cerr << "[2] Txes count: " << templ->txdata.size() << std::endl;
unsigned char coinbase_bin[1024];
memset(coinbase_bin, 0, 1024);
binlify(coinbase_bin, submitvalues->coinbase);
char doublehash[128];
memset(doublehash, 0, 128);
// some (old) wallet/algos need a simple SHA256 (blakecoin, whirlcoin, groestlcoin...)
YAAMP_HASH_FUNCTION merkle_hash = sha256_double_hash_hex;
if (g_current_algo->merkle_func)
merkle_hash = g_current_algo->merkle_func;
merkle_hash((char *)coinbase_bin, doublehash, coinbase_len/2);
string merkleroot = merkle_with_first(templ->txsteps, doublehash);
strcpy(submitvalues->merkleroot_be, merkleroot.c_str());
// std::cerr << "Merkle (build): " << merkleroot << std::endl;
#ifdef MERKLE_DEBUGLOG
printf("merkle root %s\n", merkleroot.c_str());
#endif
{
char rev_version[32] = {0};
char rev_ntime[32] = {0};
char rev_nbits[32] = {0};
string_be(templ->version,rev_version);
string_be(ntime,rev_ntime);
//string_be(templ->ntime,rev_ntime);
string_be(templ->nbits,rev_nbits);
sprintf(submitvalues->header, "%s%s%s%s%s%s%s%s%s", rev_version, templ->prevhash_be, submitvalues->merkleroot_be,
templ->extradata_be, rev_ntime, rev_nbits, nonce1, nonce, equi_solution);
//std::cerr << " header: " << submitvalues->header << std::endl;
memset(submitvalues->header_be, 0, EQUI_HEADER_SIZE * 2 + 1);
strcpy(submitvalues->header_be,submitvalues->header);
}
binlify(submitvalues->header_bin, submitvalues->header_be);
int header_len = strlen(submitvalues->header)/2;
g_current_algo->hash_function((char *)submitvalues->header_bin, (char *)submitvalues->hash_bin, header_len);
hexlify(submitvalues->hash_hex, submitvalues->hash_bin, 32);
string_be(submitvalues->hash_hex, submitvalues->hash_be);
//std::cerr << " blockhash: " << submitvalues->hash_be << std::endl;
//debuglog("blockhash: %s\n",submitvalues->hash_be);
}
/////////////////////////////////////////////
static void create_decred_header(YAAMP_JOB_TEMPLATE *templ, YAAMP_JOB_VALUES *out,
const char *ntime, const char *nonce, const char *nonce2, const char *vote, bool usegetwork)
{
struct __attribute__((__packed__)) {
uint32_t version;
char prevblock[32];
char merkleroot[32];
char stakeroot[32];
uint16_t votebits;
char finalstate[6];
uint16_t voters;
uint8_t freshstake;
uint8_t revoc;
uint32_t poolsize;
uint32_t nbits;
uint64_t sbits;
uint32_t height;
uint32_t size;
uint32_t ntime;
uint32_t nonce;
unsigned char extra[32];
uint32_t stakever;
uint32_t hashtag[3];
} header;
memcpy(&header, templ->header, sizeof(header));
memset(header.extra, 0, 32);
sscanf(nonce, "%08x", &header.nonce);
if (strcmp(vote, "")) {
uint16_t votebits = 0;
sscanf(vote, "%04hx", &votebits);
header.votebits = (header.votebits & 1) | (votebits & 0xfffe);
}
binlify(header.extra, nonce2);
hexlify(out->header, (const unsigned char*) &header, 180);
memcpy(out->header_bin, &header, sizeof(header));
}
static void build_submit_values_decred(YAAMP_JOB_VALUES *submitvalues, YAAMP_JOB_TEMPLATE *templ,
const char *nonce1, const char *nonce2, const char *ntime, const char *nonce, const char *vote, bool usegetwork)
{
if (!usegetwork) {
// not used yet
char doublehash[128] = { 0 };
sprintf(submitvalues->coinbase, "%s%s%s%s", templ->coinb1, nonce1, nonce2, templ->coinb2);
int coinbase_len = strlen(submitvalues->coinbase);
unsigned char coinbase_bin[1024];
memset(coinbase_bin, 0, 1024);
binlify(coinbase_bin, submitvalues->coinbase);
YAAMP_HASH_FUNCTION merkle_hash = sha256_double_hash_hex;
if (g_current_algo->merkle_func)
merkle_hash = g_current_algo->merkle_func;
merkle_hash((char *)coinbase_bin, doublehash, coinbase_len/2);
string merkleroot = merkle_with_first(templ->txsteps, doublehash);
ser_string_be(merkleroot.c_str(), submitvalues->merkleroot_be, 8);
#ifdef MERKLE_DEBUGLOG
printf("merkle root %s\n", merkleroot.c_str());
#endif
}
create_decred_header(templ, submitvalues, ntime, nonce, nonce2, vote, usegetwork);
int header_len = strlen(submitvalues->header)/2;
g_current_algo->hash_function((char *)submitvalues->header_bin, (char *)submitvalues->hash_bin, header_len);
hexlify(submitvalues->hash_hex, submitvalues->hash_bin, 32);
string_be(submitvalues->hash_hex, submitvalues->hash_be);
}
/////////////////////////////////////////////////////////////////////////////////
static void client_do_submit(YAAMP_CLIENT *client, YAAMP_JOB *job, YAAMP_JOB_VALUES *submitvalues,
char *extranonce2, char *ntime, char *nonce, char *vote)
{
YAAMP_COIND *coind = job->coind;
YAAMP_JOB_TEMPLATE *templ = job->templ;
if(job->block_found) return;
if(job->deleted) return;
uint64_t hash_int, coin_target;
if (g_current_algo->name && !strcmp(g_current_algo->name,"equihash"))
{
uint32_t bits; char bits_str[5] = { 0 };
string_be(templ->nbits/*_from_target*/, bits_str); binlify((unsigned char *)&bits, bits_str);
hash_int = diff_to_target(target_to_diff_equi((uint32_t *)submitvalues->hash_bin));
coin_target = diff_to_target(nbits_to_diff_equi(&bits));
}
else
{
hash_int = get_hash_difficulty(submitvalues->hash_bin);
coin_target = decode_compact(templ->nbits);
};
if (templ->nbits && !coin_target) coin_target = 0xFFFF000000000000ULL;
int block_size = YAAMP_SMALLBUFSIZE;
vector<string>::const_iterator i;
for(i = templ->txdata.begin(); i != templ->txdata.end(); ++i)
block_size += strlen((*i).c_str());
char *block_hex = (char *)malloc(block_size);
if(!block_hex) return;
// do aux first
for(int i=0; i<templ->auxs_size; i++)
{
if(!templ->auxs[i]) continue;
YAAMP_COIND *coind_aux = templ->auxs[i]->coind;
if(!coind_aux || !strcmp(coind->symbol, coind_aux->symbol2))
continue;
unsigned char target_aux[1024];
binlify(target_aux, coind_aux->aux.target);
uint64_t coin_target_aux = get_hash_difficulty(target_aux);
if(hash_int <= coin_target_aux)
{
memset(block_hex, 0, block_size);
strcat(block_hex, submitvalues->coinbase); // parent coinbase
strcat(block_hex, submitvalues->hash_be); // parent hash
////////////////////////////////////////////////// parent merkle steps
sprintf(block_hex+strlen(block_hex), "%02x", (unsigned char)templ->txsteps.size());
vector<string>::const_iterator i;
for(i = templ->txsteps.begin(); i != templ->txsteps.end(); ++i)
sprintf(block_hex + strlen(block_hex), "%s", (*i).c_str());
strcat(block_hex, "00000000");
////////////////////////////////////////////////// auxs merkle steps
vector<string> lresult = coind_aux_merkle_branch(templ->auxs, templ->auxs_size, coind_aux->aux.index);
sprintf(block_hex+strlen(block_hex), "%02x", (unsigned char)lresult.size());
for(i = lresult.begin(); i != lresult.end(); ++i)
sprintf(block_hex+strlen(block_hex), "%s", (*i).c_str());
sprintf(block_hex+strlen(block_hex), "%02x000000", (unsigned char)coind_aux->aux.index);
////////////////////////////////////////////////// parent header
strcat(block_hex, submitvalues->header_be);
bool b = coind_submitgetauxblock(coind_aux, coind_aux->aux.hash, block_hex);
if(b)
{
debuglog("*** ACCEPTED %s %d (+1)\n", coind_aux->name, coind_aux->height);
block_add(client->userid, client->workerid, coind_aux->id, coind_aux->height, target_to_diff(coin_target_aux),
target_to_diff(hash_int), coind_aux->aux.hash, "", 0);
}
else
debuglog("%s %d REJECTED\n", coind_aux->name, coind_aux->height);
}
}
if(hash_int <= coin_target)
{
// adding transactions to block
char count_hex[8] = { 0 };
// adding tx count after blockheader
if (templ->txcount <= 252)
sprintf(count_hex, "%02x", templ->txcount & 0xFF);
else
sprintf(count_hex, "fd%02x%02x", templ->txcount & 0xFF, templ->txcount >> 8);
memset(block_hex, 0, block_size);
sprintf(block_hex, "%s%s%s", submitvalues->header_be, count_hex, submitvalues->coinbase);
if (g_current_algo->name && !strcmp("jha", g_current_algo->name)) {
// block header of 88 bytes
sprintf(block_hex, "%s8400000008000000%s%s", submitvalues->header_be, count_hex, submitvalues->coinbase);
}
// adding tx data itself
vector<string>::const_iterator i;
for(i = templ->txdata.begin(); i != templ->txdata.end(); ++i)
sprintf(block_hex+strlen(block_hex), "%s", (*i).c_str());
// POS coins need a zero byte appended to block, the daemon replaces it with the signature
if(coind->pos)
strcat(block_hex, "00");
if(!strcmp("DCR", coind->rpcencoding)) {
// submit the regenerated block header
char hex[384];
hexlify(hex, submitvalues->header_bin, 180);
if (coind->usegetwork)
snprintf(block_hex, block_size, "%s8000000100000000000005a0", hex);
else
snprintf(block_hex, block_size, "%s", hex);
}
// std::cerr << "Block hex: " << block_hex << std::endl;
bool b = coind_submit(coind, block_hex);
if(b)
{
debuglog("*** ACCEPTED %s %d (diff %g) by %s (id: %d)\n", coind->name, templ->height,
target_to_diff(hash_int), client->sock->ip, client->userid);
job->block_found = true;
char doublehash2[128];
memset(doublehash2, 0, 128);
YAAMP_HASH_FUNCTION merkle_hash = sha256_double_hash_hex;
//if (g_current_algo->merkle_func)
// merkle_hash = g_current_algo->merkle_func;
merkle_hash((char *)submitvalues->header_bin, doublehash2, strlen(submitvalues->header_be)/2);
char hash1[1024];
memset(hash1, 0, 1024);
string_be(doublehash2, hash1);
if(coind->usegetwork && !strcmp("DCR", coind->rpcencoding)) {
// no merkle stuff
strcpy(hash1, submitvalues->hash_hex);
}
block_add(client->userid, client->workerid, coind->id, templ->height,
target_to_diff(coin_target), target_to_diff(hash_int),
hash1, submitvalues->hash_be, templ->has_segwit_txs);
if(!strcmp("DCR", coind->rpcencoding)) {
// delay between dcrd and dcrwallet
sleep(1);
}
if(!strcmp(coind->lastnotifyhash,submitvalues->hash_be)) {
block_confirm(coind->id, submitvalues->hash_be);
}
if (g_debuglog_hash) {
debuglog("--------------------------------------------------------------\n");
debuglog("hash1 %s\n", hash1);
debuglog("hash2 %s\n", submitvalues->hash_be);
}
}
else {
debuglog("*** REJECTED :( %s block %d %d txs\n", coind->name, templ->height, templ->txcount);
rejectlog("REJECTED %s block %d\n", coind->symbol, templ->height);
if (g_debuglog_hash) {
//debuglog("block %s\n", block_hex);
debuglog("--------------------------------------------------------------\n");
}
}
}
free(block_hex);
}
bool dump_submit_debug(const char *title, YAAMP_CLIENT *client, YAAMP_JOB *job, char *extranonce2, char *ntime, char *nonce)
{
debuglog("ERROR %s, %s subs %d, job %x, %s, id %x, %d, %s, %s %s\n",
title, client->sock->ip, client->extranonce_subscribe, job? job->id: 0, client->extranonce1,
client->extranonce1_id, client->extranonce2size, extranonce2, ntime, nonce);
}
void client_submit_error(YAAMP_CLIENT *client, YAAMP_JOB *job, int id, const char *message, char *extranonce2, char *ntime, char *nonce)
{
// if(job->templ->created+2 > time(NULL))
if(job && job->deleted)
client_send_result(client, "true");
else
{
client_send_error(client, id, message);
share_add(client, job, false, extranonce2, ntime, nonce, 0, id);
client->submit_bad++;
if (g_debuglog_hash) {
dump_submit_debug(message, client, job, extranonce2, ntime, nonce);
}
}
object_unlock(job);
}
static bool ntime_valid_range(const char ntimehex[])
{
time_t rawtime = 0;
uint32_t ntime = 0;
if (strlen(ntimehex) != 8) return false;
sscanf(ntimehex, "%8x", &ntime);
if (ntime < 0x5b000000 || ntime > 0x60000000) // 14 Jan 2021
return false;
time(&rawtime);
return (abs(rawtime - ntime) < (30 * 60));
}
static bool valid_string_params(json_value *json_params)
{
for(int p=0; p < json_params->u.array.length; p++) {
if (!json_is_string(json_params->u.array.values[p]))
return false;
}
return true;
}
bool client_submit(YAAMP_CLIENT *client, json_value *json_params)
{
// submit(worker_name, jobid, extranonce2, ntime, nonce):
if(json_params->u.array.length<5 || !valid_string_params(json_params)) {
debuglog("%s - %s bad message\n", client->username, client->sock->ip);
client->submit_bad++;
return false;
}
char extranonce2[32] = { 0 };
char extra[160] = { 0 };
char nonce[80] = { 0 };
char ntime[32] = { 0 };
char vote[8] = { 0 };
if (strlen(json_params->u.array.values[1]->u.string.ptr) > 32) {
clientlog(client, "bad json, wrong jobid len");
client->submit_bad++;
return false;
}
int jobid = htoi(json_params->u.array.values[1]->u.string.ptr);
strncpy(extranonce2, json_params->u.array.values[2]->u.string.ptr, 31);
strncpy(ntime, json_params->u.array.values[3]->u.string.ptr, 31);
strncpy(nonce, json_params->u.array.values[4]->u.string.ptr, 31);
string_lower(extranonce2);
string_lower(ntime);
string_lower(nonce);
if (json_params->u.array.length == 6) {
if (strstr(g_stratum_algo, "phi")) {
// lux optional field, smart contral root hashes (not mandatory on shares submit)
strncpy(extra, json_params->u.array.values[5]->u.string.ptr, 128);
string_lower(extra);
} else {
// heavycoin vote
strncpy(vote, json_params->u.array.values[5]->u.string.ptr, 7);
string_lower(vote);
}
}
if (g_debuglog_hash) {
debuglog("submit %s (uid %d) %d, %s, t=%s, n=%s, extra=%s\n", client->sock->ip, client->userid,
jobid, extranonce2, ntime, nonce, extra);
}
YAAMP_JOB *job = (YAAMP_JOB *)object_find(&g_list_job, jobid, true);
if(!job)
{
client_submit_error(client, NULL, 21, "Invalid job id", extranonce2, ntime, nonce);
return true;
}
if(job->deleted)
{
client_send_result(client, "true");
object_unlock(job);
return true;
}
bool is_decred = job->coind && !strcmp("DCR", job->coind->rpcencoding);
YAAMP_JOB_TEMPLATE *templ = job->templ;
if(strlen(nonce) != YAAMP_NONCE_SIZE*2 || !ishexa(nonce, YAAMP_NONCE_SIZE*2)) {
client_submit_error(client, job, 20, "Invalid nonce size", extranonce2, ntime, nonce);
return true;
}
if(strcmp(ntime, templ->ntime))
{
if (!ishexa(ntime, 8) || !ntime_valid_range(ntime)) {
client_submit_error(client, job, 23, "Invalid time rolling", extranonce2, ntime, nonce);
return true;
}
// dont allow algos permutations change over time (can lead to different speeds)
if (!g_allow_rolltime) {
client_submit_error(client, job, 23, "Invalid ntime (rolling not allowed)", extranonce2, ntime, nonce);
return true;
}
}
YAAMP_SHARE *share = share_find(job->id, extranonce2, ntime, nonce, client->extranonce1);
if(share)
{
client_submit_error(client, job, 22, "Duplicate share", extranonce2, ntime, nonce);
return true;
}
if(strlen(extranonce2) != client->extranonce2size*2)
{
client_submit_error(client, job, 24, "Invalid extranonce2 size", extranonce2, ntime, nonce);
return true;
}
// check if the submitted extranonce is valid
if(is_decred && client->extranonce2size > 4) {
char extra1_id[16], extra2_id[16];
int cmpoft = client->extranonce2size*2 - 8;
strcpy(extra1_id, &client->extranonce1[cmpoft]);
strcpy(extra2_id, &extranonce2[cmpoft]);
int extradiff = (int) strcmp(extra2_id, extra1_id);
int extranull = (int) !strcmp(extra2_id, "00000000");
if (extranull && client->extranonce2size > 8)
extranull = (int) !strcmp(&extranonce2[8], "00000000" "00000000");
if (extranull) {
debuglog("extranonce %s is empty!, should be %s - %s\n", extranonce2, extra1_id, client->sock->ip);
client_submit_error(client, job, 27, "Invalid extranonce2 suffix", extranonce2, ntime, nonce);
return true;
}
if (extradiff) {
// some ccminer pre-release doesn't fill correctly the extranonce
client_submit_error(client, job, 27, "Invalid extranonce2 suffix", extranonce2, ntime, nonce);
socket_send(client->sock, "{\"id\":null,\"method\":\"mining.set_extranonce\",\"params\":[\"%s\",%d]}\n",
client->extranonce1, client->extranonce2size);
return true;
}
}
else if(!ishexa(extranonce2, client->extranonce2size*2)) {
client_submit_error(client, job, 27, "Invalid nonce2", extranonce2, ntime, nonce);
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////
YAAMP_JOB_VALUES submitvalues;
memset(&submitvalues, 0, sizeof(submitvalues));
if(is_decred)
build_submit_values_decred(&submitvalues, templ, client->extranonce1, extranonce2, ntime, nonce, vote, true);
else
build_submit_values(&submitvalues, templ, client->extranonce1, extranonce2, ntime, nonce);
if (templ->height && !strcmp(g_current_algo->name,"lyra2z")) {
lyra2z_height = templ->height;
}
// minimum hash diff begins with 0000, for all...
uint8_t pfx = submitvalues.hash_bin[30] | submitvalues.hash_bin[31];
if(pfx) {
if (g_debuglog_hash) {
debuglog("Possible %s error, hash starts with %02x%02x%02x%02x\n", g_current_algo->name,
(int) submitvalues.hash_bin[31], (int) submitvalues.hash_bin[30],
(int) submitvalues.hash_bin[29], (int) submitvalues.hash_bin[28]);
}
client_submit_error(client, job, 25, "Invalid share", extranonce2, ntime, nonce);
return true;
}
uint64_t hash_int = get_hash_difficulty(submitvalues.hash_bin);
uint64_t user_target = diff_to_target(client->difficulty_actual);
uint64_t coin_target = decode_compact(templ->nbits);
if (templ->nbits && !coin_target) coin_target = 0xFFFF000000000000ULL;
if (g_debuglog_hash) {
debuglog("%016llx actual\n", hash_int);
debuglog("%016llx target\n", user_target);
debuglog("%016llx coin\n", coin_target);
}
if(hash_int > user_target && hash_int > coin_target)
{
client_submit_error(client, job, 26, "Low difficulty share", extranonce2, ntime, nonce);
return true;
}
if(job->coind)
client_do_submit(client, job, &submitvalues, extranonce2, ntime, nonce, vote);
else
remote_submit(client, job, &submitvalues, extranonce2, ntime, nonce);
client_send_result(client, "true");
client_record_difficulty(client);
client->submit_bad = 0;
client->shares++;
if (client->shares <= 200 && (client->shares % 50) == 0) {
// 4 records are enough per miner
if (!client_ask_stats(client)) client->stats = false;
}
double share_diff = diff_to_target(hash_int);
// if (g_current_algo->diff_multiplier != 0) {
// share_diff = share_diff / g_current_algo->diff_multiplier;
// }
if (g_debuglog_hash) {
// only log a few...
if (share_diff > (client->difficulty_actual * 16))
debuglog("submit %s (uid %d) %d, %s, %s, %s, %.3f/%.3f\n", client->sock->ip, client->userid,
jobid, extranonce2, ntime, nonce, share_diff, client->difficulty_actual);
}
share_add(client, job, true, extranonce2, ntime, nonce, share_diff, 0);
object_unlock(job);
return true;
}
// -------------------------------------------------------------
bool client_submit_equi(YAAMP_CLIENT *client, json_value *json_params)
{
// submit(worker_name, jobid, extranonce2, ntime, nonce):
if(json_params->u.array.length<5 || !valid_string_params(json_params)) {
debuglog("%s - %s bad message\n", client->username, client->sock->ip);
client->submit_bad++;
return false;
}
/*
for (int i = 0; i < json_params->u.array.length - 1; i++) {
std::cerr << "[" << i << "] " << json_params->u.array.values[i]->u.string.ptr << std::endl;
}
*/
char extranonce2[32] = { 0 };
char extra[160] = { 0 };
char nonce[65] = { 0 };
char ntime[9] = { 0 };
char vote[8] = { 0 };
char equi_solution[1344*2 + 64] = { 0 }; // 1344*2 + 64 as solhex field in ccminer
// but should be 1344*2 + 3*2 (fd4005 bytes = 0xfd, 0x0540)
if (strlen(json_params->u.array.values[1]->u.string.ptr) > 32) {
clientlog(client, "bad json, wrong jobid len");
client->submit_bad++;
return false;
}
int jobid = htoi(json_params->u.array.values[1]->u.string.ptr);
// 0 - ?, 1 - xnonce1 (extranonce1) [string], 2 - xn2_size
// ccminer: xn1_size = (int)strlen(xnonce1) / 2, xn2_size = 32 - xn1_size; // xn1_size = 4, xn2_size = 28
//strncpy(extranonce2, json_params->u.array.values[2]->u.string.ptr, 31);
// we should reverse some params, see job_send.cpp
char rev_ntime[9] = {0};
strncpy(rev_ntime, json_params->u.array.values[2]->u.string.ptr, 8);
string_be(rev_ntime,ntime);
strncpy(nonce, json_params->u.array.values[3]->u.string.ptr, 64);
strncpy(equi_solution, json_params->u.array.values[4]->u.string.ptr, 1344*2 + 3*2 );
//string_lower(extranonce2);
string_lower(ntime);
string_lower(nonce);
string_lower(equi_solution);
if (g_debuglog_hash) {
debuglog("submit %s (uid %d) %d, ntime = %s, nonce = %s, sol = %s\n", client->sock->ip, client->userid,
jobid, ntime, nonce, "..." /*equi_solution*/);
}
YAAMP_JOB *job = (YAAMP_JOB *)object_find(&g_list_job, jobid, true);
if(!job)
{
client_submit_error(client, NULL, 21, "Invalid job id", extranonce2, ntime, nonce);
return true;
}
if(job->deleted)
{
client_send_result(client, "true");
object_unlock(job);
return true;
}
YAAMP_JOB_TEMPLATE *templ = job->templ;
/*
std::cerr << "strlen(nonce) = " << strlen(nonce) << ", YAAMP_EQUI_NONCE_SIZE*2 = " << YAAMP_EQUI_NONCE_SIZE*2 << std::endl;
// from equi-stratum.cpp ccminer, actually nonce is 32 - 4 = 28 bytes (56 in hex representation)
size_t nonce_len = 32 - stratum.xnonce1_size;
// long nonce without pool prefix (extranonce)
noncestr = bin2hex(&nonce[stratum.xnonce1_size], nonce_len);
*/
if(strlen(nonce) != YAAMP_EQUI_NONCE_SIZE*2 || !ishexa(nonce, YAAMP_EQUI_NONCE_SIZE*2)) {
client_submit_error(client, job, 20, "Invalid nonce size", extranonce2, ntime, nonce);
return true;
}
if(strcmp(ntime, templ->ntime))
{
if (!ishexa(ntime, 8) || !ntime_valid_range(ntime)) {
client_submit_error(client, job, 23, "Invalid time rolling", extranonce2, ntime, nonce);
return true;
}
// dont allow algos permutations change over time (can lead to different speeds)
if (!g_allow_rolltime) {
client_submit_error(client, job, 23, "Invalid ntime (rolling not allowed)", extranonce2, ntime, nonce);
return true;
}
}
// TODO: look how share_find is work to check duplicate shares
YAAMP_SHARE *share = share_find(job->id, extranonce2, ntime, nonce, client->extranonce1);
if(share)
{
client_submit_error(client, job, 22, "Duplicate share", extranonce2, ntime, nonce);
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////
//std::cerr << "height: " << templ->height << std::endl;
YAAMP_JOB_VALUES submitvalues;
memset(&submitvalues, 0, sizeof(submitvalues));
build_submit_values_equi(&submitvalues, templ, client->extranonce1, extranonce2, ntime, nonce, equi_solution);
// minimum hash diff begins with 0000, for all...
uint8_t pfx = submitvalues.hash_bin[30] | submitvalues.hash_bin[31];
if(0 && pfx) {
if (g_debuglog_hash) {
debuglog("Possible %s error, hash starts with %02x%02x%02x%02x\n", g_current_algo->name,
(int) submitvalues.hash_bin[31], (int) submitvalues.hash_bin[30],
(int) submitvalues.hash_bin[29], (int) submitvalues.hash_bin[28]);
}
client_submit_error(client, job, 25, "Invalid share", extranonce2, ntime, nonce);
return true;
}
// TODO: check equihash solution here and submit a error if it's invalid
if (g_current_algo->name && !strcmp(g_current_algo->name,"equihash")) {
/*
fprintf(stderr,"blockheader:\n");
for (int i=0; i<140; i++) {
fprintf(stderr,"%02x",submitvalues.header_bin[i] & 0xff);
}
fprintf(stderr,"\n");
*/
const char *EH_hdr = (char *)submitvalues.header_bin;
const char *EH_sln = (char *)submitvalues.header_bin + 143;
//std::cerr << "verifyEH - " << verifyEH(EH_hdr, EH_sln) << std::endl;
if (!verifyEH(EH_hdr, EH_sln))
{
client_submit_error(client, job, 25, "Invalid equihash solution", extranonce2, ntime, nonce);
return true;
}
}
uint64_t hash_int, user_target, coin_target;
/* we will submit block to daemon in client_do_submit only if (hash_int <= coin_target) */
if (g_current_algo->name && !strcmp(g_current_algo->name,"equihash"))
{
uint32_t bits;
uint8_t equi_target[32] = { 0 }; char target_str[65]; target_str[64] = 0; char target_str_be[65]; target_str_be[64] = 0;
diff_to_target_equi((uint32_t *)equi_target, client->difficulty_actual);
hexlify(target_str, equi_target, 32); string_be(target_str, target_str_be);
if (g_debuglog_hash) {
debuglog(" blockhash: %s\n", submitvalues.hash_be);
debuglog("loc target: %s\n", target_str_be);
uint32_t tmp_bits; char tmp_bits_str[5]; char tmp_target[65];
string_be(templ->nbits, tmp_bits_str); binlify((unsigned char *)&tmp_bits, tmp_bits_str);
bits2target(tmp_bits, tmp_target); tmp_target[64] = '\0';
debuglog("net target: %s\n", tmp_target);
string_be(templ->nbits_from_target, tmp_bits_str); binlify((unsigned char *)&tmp_bits, tmp_bits_str);
bits2target(tmp_bits, tmp_target); tmp_target[64] = '\0';
debuglog("--- target: %s\n", tmp_target);
debuglog(" share diff: %.3f\n", target_to_diff_equi((uint32_t *)submitvalues.hash_bin));
debuglog("client diff: %.3f\n", client->difficulty_actual);
}
// templ->nbits is just a 32 char string, like "200f0f0f", so to convert it to diff we should convert it to binary value.
char bits_str[5] = { 0 };
string_be(templ->nbits, bits_str); binlify((unsigned char *)&bits, bits_str);
if (g_debuglog_hash) debuglog(" coin diff: %.3f\n", nbits_to_diff_equi(&bits));
// so, let's convert double diff values to corresponding uint64_t
hash_int = diff_to_target(target_to_diff_equi((uint32_t *)submitvalues.hash_bin));
user_target = diff_to_target(client->difficulty_actual);
coin_target = diff_to_target(nbits_to_diff_equi(&bits));
}
else
{
hash_int = get_hash_difficulty(submitvalues.hash_bin);
user_target = diff_to_target(client->difficulty_actual);
coin_target = decode_compact(templ->nbits);
}
if (templ->nbits && !coin_target) coin_target = 0xFFFF000000000000ULL;
if (g_debuglog_hash) {
debuglog("%016llx actual\n", hash_int);
debuglog("%016llx target\n", user_target);
debuglog("%016llx coin\n", coin_target);
}
if(hash_int > user_target && hash_int > coin_target)
{
client_submit_error(client, job, 26, "Low difficulty share", extranonce2, ntime, nonce);
return true;
}
if(job->coind)
client_do_submit(client, job, &submitvalues, extranonce2, ntime, nonce, vote);
else
remote_submit(client, job, &submitvalues, extranonce2, ntime, nonce);
client_send_result(client, "true");
client_record_difficulty(client);
client->submit_bad = 0;
client->shares++;
if (client->shares <= 200 && (client->shares % 50) == 0) {
// 4 records are enough per miner
if (!client_ask_stats(client)) client->stats = false;
}
double share_diff = 0.0;
if (g_current_algo->name && !strcmp(g_current_algo->name,"equihash")) {
share_diff = target_to_diff_equi((uint32_t *)submitvalues.hash_bin);
} else
share_diff = diff_to_target(hash_int);
// if (g_current_algo->diff_multiplier != 0) {
// share_diff = share_diff / g_current_algo->diff_multiplier;
// }
if (g_debuglog_hash) {
// only log a few...
// if (share_diff > (client->difficulty_actual * 16))
debuglog("submit %s (uid %d) %d, %s, %s, %s, %.3f/%.3f\n", client->sock->ip, client->userid,
jobid, extranonce2, ntime, nonce, share_diff, client->difficulty_actual);
}
share_add(client, job, true, extranonce2, ntime, nonce, share_diff, 0);
object_unlock(job);
return true;
}