-
Notifications
You must be signed in to change notification settings - Fork 34
/
coind_template.cpp
744 lines (591 loc) · 23.4 KB
/
coind_template.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
#include "stratum.h"
void coind_getauxblock(YAAMP_COIND *coind)
{
if(!coind->isaux) return;
json_value *json = rpc_call(&coind->rpc, "getauxblock", "[]");
if(!json)
{
coind_error(coind, "coind_getauxblock");
return;
}
json_value *json_result = json_get_object(json, "result");
if(!json_result)
{
coind_error(coind, "coind_getauxblock");
return;
}
// coind->aux.height = coind->height+1;
coind->aux.chainid = json_get_int(json_result, "chainid");
const char *p = json_get_string(json_result, "target");
if(p) strcpy(coind->aux.target, p);
p = json_get_string(json_result, "hash");
if(p) strcpy(coind->aux.hash, p);
// if(strcmp(coind->symbol, "UNO") == 0)
// {
// string_be1(coind->aux.target);
// string_be1(coind->aux.hash);
// }
json_value_free(json);
}
YAAMP_JOB_TEMPLATE *coind_create_template_memorypool(YAAMP_COIND *coind)
{
json_value *json = rpc_call(&coind->rpc, "getmemorypool");
if(!json || json->type == json_null)
{
coind_error(coind, "getmemorypool");
return NULL;
}
json_value *json_result = json_get_object(json, "result");
if(!json_result || json_result->type == json_null)
{
coind_error(coind, "getmemorypool");
json_value_free(json);
return NULL;
}
YAAMP_JOB_TEMPLATE *templ = new YAAMP_JOB_TEMPLATE;
memset(templ, 0, sizeof(YAAMP_JOB_TEMPLATE));
templ->created = time(NULL);
templ->value = json_get_int(json_result, "coinbasevalue");
// templ->height = json_get_int(json_result, "height");
sprintf(templ->version, "%08x", (unsigned int)json_get_int(json_result, "version"));
sprintf(templ->ntime, "%08x", (unsigned int)json_get_int(json_result, "time"));
strcpy(templ->nbits, json_get_string(json_result, "bits"));
strcpy(templ->prevhash_hex, json_get_string(json_result, "previousblockhash"));
json_value_free(json);
json = rpc_call(&coind->rpc, "getmininginfo", "[]");
if(!json || json->type == json_null)
{
coind_error(coind, "coind getmininginfo");
return NULL;
}
json_result = json_get_object(json, "result");
if(!json_result || json_result->type == json_null)
{
coind_error(coind, "coind getmininginfo");
json_value_free(json);
return NULL;
}
templ->height = json_get_int(json_result, "blocks")+1;
json_value_free(json);
coind_getauxblock(coind);
coind->usememorypool = true;
return templ;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
static int decred_parse_header(YAAMP_JOB_TEMPLATE *templ, const char *header_hex, bool getwork)
{
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;
//debuglog("HEADER: %s\n", header_hex);
binlify((unsigned char*) &header, header_hex);
templ->height = header.height;
// reversed to tell its not a normal stratum coinbase
sprintf(templ->version, "%08x", getwork ? bswap32(header.version) : header.version);
sprintf(templ->ntime, "%08x", header.ntime);
sprintf(templ->nbits, "%08x", header.nbits);
templ->prevhash_hex[64] = '\0';
uint32_t* prev32 = (uint32_t*) header.prevblock;
for(int i=0; i < 8; i++)
sprintf(&templ->prevhash_hex[i*8], "%08x", getwork ? prev32[7-i] : bswap32(prev32[7-i]));
ser_string_be2(templ->prevhash_hex, templ->prevhash_be, 8);
// store all other stuff
memcpy(templ->header, &header, sizeof(header));
return 0;
}
// decred getwork over stratum
static YAAMP_JOB_TEMPLATE *decred_create_worktemplate(YAAMP_COIND *coind)
{
char rpc_error[1024] = { 0 };
#define GETWORK_RETRY_MAX 3
int retry_cnt = GETWORK_RETRY_MAX;
retry:
json_value *gw = rpc_call(&coind->rpc, "getwork", "[]");
if(!gw || json_is_null(gw)) {
usleep(500*YAAMP_MS); // too much connections ? no data received
if (--retry_cnt > 0) {
if (coind->rpc.curl)
rpc_curl_get_lasterr(rpc_error, 1023);
debuglog("%s getwork retry %d\n", coind->symbol, GETWORK_RETRY_MAX-retry_cnt);
goto retry;
}
debuglog("%s error getwork %s\n", coind->symbol, rpc_error);
return NULL;
}
json_value *gwr = json_get_object(gw, "result");
if(!gwr) {
debuglog("%s no getwork json result!\n", coind->symbol);
return NULL;
}
else if (json_is_null(gwr)) {
json_value *jr = json_get_object(gw, "error");
if (!jr || json_is_null(jr)) return NULL;
const char *err = json_get_string(jr, "message");
if (err && !strcmp(err, "internal error")) {
usleep(500*YAAMP_MS); // not enough voters (testnet)
if (--retry_cnt > 0) {
goto retry;
}
debuglog("%s getwork failed after %d tries: %s\n",
coind->symbol, GETWORK_RETRY_MAX, err);
}
return NULL;
}
const char *header_hex = json_get_string(gwr, "data");
if (!header_hex || !strlen(header_hex)) {
debuglog("%s no getwork data!\n", coind->symbol);
return NULL;
}
YAAMP_JOB_TEMPLATE *templ = new YAAMP_JOB_TEMPLATE;
memset(templ, 0, sizeof(YAAMP_JOB_TEMPLATE));
templ->created = time(NULL);
decred_parse_header(templ, header_hex, true);
json_value_free(gw);
// bypass coinbase and merkle for now... send without nonce/extradata
const unsigned char *hdr = (unsigned char *) &templ->header[36];
hexlify(templ->coinb1, hdr, 192 - 80);
const unsigned char *sfx = (unsigned char *) &templ->header[176];
hexlify(templ->coinb2, sfx, 180 - 176); // stake version
vector<string> txhashes;
txhashes.push_back("");
templ->txmerkles[0] = 0;
templ->txcount = txhashes.size();
templ->txsteps = merkle_steps(txhashes);
txhashes.clear();
return templ;
}
// for future decred real stratum
static void decred_fix_template(YAAMP_COIND *coind, YAAMP_JOB_TEMPLATE *templ, json_value *json)
{
const char *header_hex = json_get_string(json, "header");
if (!header_hex || !strlen(header_hex)) {
stratumlog("decred error, no block header in json!\n");
return;
}
// todo ?
// "mintime": 1455511962,
// "maxtime": 1455522081,
decred_parse_header(templ, header_hex, false);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
YAAMP_JOB_TEMPLATE *coind_create_template(YAAMP_COIND *coind)
{
if(coind->usememorypool)
return coind_create_template_memorypool(coind);
char params[512] = "[{}]";
if(!strcmp(coind->symbol, "PPC")) strcpy(params, "[]");
else if(g_stratum_segwit) strcpy(params, "[{\"rules\":[\"segwit\"]}]");
json_value *json = rpc_call(&coind->rpc, "getblocktemplate", params);
if(!json || json_is_null(json))
{
// coind_error() reset auto_ready, and DCR gbt can fail
if (strcmp(coind->rpcencoding, "DCR") == 0)
debuglog("decred getblocktemplate failed\n");
else
coind_error(coind, "getblocktemplate");
return NULL;
}
json_value *json_result = json_get_object(json, "result");
if(!json_result || json_is_null(json_result))
{
coind_error(coind, "getblocktemplate result");
json_value_free(json);
return NULL;
}
// segwit rule
json_value *json_rules = json_get_array(json_result, "rules");
if(json_rules && !strlen(coind->witness_magic) && json_rules->u.array.length) {
for (int i=0; i<json_rules->u.array.length; i++) {
json_value *val = json_rules->u.array.values[i];
if(!strcmp(val->u.string.ptr, "segwit")) {
const char *commitment = json_get_string(json_result, "default_witness_commitment");
strcpy(coind->witness_magic, "aa21a9ed");
if (commitment && strlen(commitment) > 12) {
strncpy(coind->witness_magic, &commitment[4], 8);
coind->witness_magic[8] = '\0';
}
coind->usesegwit |= g_stratum_segwit;
if (coind->usesegwit)
debuglog("%s segwit enabled, magic %s\n", coind->symbol, coind->witness_magic);
break;
}
}
}
json_value *json_tx = json_get_array(json_result, "transactions");
if(!json_tx)
{
coind_error(coind, "getblocktemplate transactions");
json_value_free(json);
return NULL;
}
json_value *json_coinbaseaux = json_get_object(json_result, "coinbaseaux");
if(!json_coinbaseaux && coind->isaux)
{
coind_error(coind, "getblocktemplate coinbaseaux");
json_value_free(json);
return NULL;
}
YAAMP_JOB_TEMPLATE *templ = new YAAMP_JOB_TEMPLATE;
memset(templ, 0, sizeof(YAAMP_JOB_TEMPLATE));
templ->created = time(NULL);
templ->value = json_get_int(json_result, "coinbasevalue");
templ->height = json_get_int(json_result, "height");
sprintf(templ->version, "%08x", (unsigned int)json_get_int(json_result, "version"));
sprintf(templ->ntime, "%08x", (unsigned int)json_get_int(json_result, "curtime"));
const char *bits = json_get_string(json_result, "bits");
strcpy(templ->nbits, bits ? bits : "");
const char *prev = json_get_string(json_result, "previousblockhash");
strcpy(templ->prevhash_hex, prev ? prev : "");
// equihash
if (!strcmp(g_stratum_algo, "equihash")) {
/*
// (!) bits <- target (?)
// https://bitcoin.stackexchange.com/questions/30467/what-are-the-equations-to-convert-between-bits-and-difficulty
*/
// override nbits from target
const char *target = json_get_string(json_result, "target");
uint32_t bits_bin = target2bits(target); // bits_bin = 0x1e3a79c3, templ->nbits = "1e3a79c3"
char bits_str[5] = {0};
hexlify(bits_str, (const unsigned char *)&bits_bin, 4);
string_be(bits_str, templ->nbits_from_target);
const char *finalsaplingroothash = json_get_string(json_result, "finalsaplingroothash");
strcpy(templ->extradata_hex, finalsaplingroothash ? finalsaplingroothash : "");
string_be(templ->extradata_hex,templ->extradata_be);
}
const char *flags;
if(!json_coinbaseaux && coind->isaux)
flags = json_get_string(json_coinbaseaux, "flags");
else
flags = NULL;
strcpy(templ->flags, flags ? flags : "");
// LBC Claim Tree (with wallet gbt patch)
const char *claim = json_get_string(json_result, "claimtrie");
if (claim) {
strcpy(templ->claim_hex, claim);
// debuglog("claimtrie: %s\n", templ->claim_hex);
}
else if (strcmp(coind->symbol, "LBC") == 0) {
json_value *json_claim = rpc_call(&coind->rpc, "getclaimtrie");
if (!json_claim || json_claim->type != json_object)
return NULL;
json_value *json_cls = json_get_array(json_claim, "result");
if (!json_cls || !json_is_array(json_cls))
return NULL;
// get first claim "", seems the root
// if empty need 0000000000000000000000000000000000000000000000000000000000000001
json_value *json_obj = json_cls->u.array.values[0];
if (!json_obj || json_claim->type != json_object)
return NULL;
claim = json_get_string(json_obj, "hash");
if (claim) {
strcpy(templ->claim_hex, claim);
debuglog("claim_hex: %s\n", templ->claim_hex);
}
}
const char *sc_root = json_get_string(json_result, "stateroot");
const char *sc_utxo = json_get_string(json_result, "utxoroot");
if (sc_root && sc_utxo) {
// LUX Smart Contracts, 144-bytes block headers
strcpy(&templ->extradata_hex[ 0], sc_root); // 32-bytes hash (64 in hexa)
strcpy(&templ->extradata_hex[64], sc_utxo); // 32-bytes hash too
// same weird byte order as previousblockhash field
ser_string_be2(sc_root, &templ->extradata_be[ 0], 8);
ser_string_be2(sc_utxo, &templ->extradata_be[64], 8);
}
if (strcmp(coind->rpcencoding, "DCR") == 0) {
decred_fix_template(coind, templ, json_result);
}
if (!templ->height || !templ->nbits || !strlen(templ->prevhash_hex)) {
stratumlog("%s warning, gbt incorrect : version=%s height=%d value=%d bits=%s time=%s prev=%s\n",
coind->symbol, templ->version, templ->height, templ->value, templ->nbits, templ->ntime, templ->prevhash_hex);
}
// temporary hack, until wallet is fixed...
if (!strcmp(coind->symbol, "MBL")) { // MBL: chainid in version
unsigned int nVersion = (unsigned int)json_get_int(json_result, "version");
if (nVersion & 0xFFFF0000UL == 0) {
nVersion |= (0x16UL << 16);
debuglog("%s version %s >> %08x\n", coind->symbol, templ->version, nVersion);
}
sprintf(templ->version, "%08x", nVersion);
}
// debuglog("%s ntime %s\n", coind->symbol, templ->ntime);
// uint64_t target = decode_compact(json_get_string(json_result, "bits"));
// coind->difficulty = target_to_diff(target);
// string_lower(templ->ntime);
// string_lower(templ->nbits);
// char target[1024];
// strcpy(target, json_get_string(json_result, "target"));
// uint64_t coin_target = decode_compact(templ->nbits);
// debuglog("nbits %s\n", templ->nbits);
// debuglog("target %s\n", target);
// debuglog("0000%016llx\n", coin_target);
if(coind->isaux)
{
json_value_free(json);
coind_getauxblock(coind);
return templ;
}
//////////////////////////////////////////////////////////////////////////////////////////
vector<string> txhashes;
vector<string> txids;
txhashes.push_back("");
txids.push_back("");
templ->has_segwit_txs = false;
templ->has_filtered_txs = false;
templ->filtered_txs_fee = 0;
for(int i = 0; i < json_tx->u.array.length; i++)
{
const char *p = json_get_string(json_tx->u.array.values[i], "hash");
char hash_be[256] = { 0 };
if (templ->has_filtered_txs) {
templ->filtered_txs_fee += json_get_int(json_tx->u.array.values[i], "fee");
continue;
}
string_be(p, hash_be);
txhashes.push_back(hash_be);
const char *txid = json_get_string(json_tx->u.array.values[i], "txid");
if(txid && strlen(txid)) {
char txid_be[256] = { 0 };
string_be(txid, txid_be);
txids.push_back(txid_be);
if (strcmp(hash_be, txid_be)) {
templ->has_segwit_txs = true; // if not, its useless to generate a segwit block, bigger
}
} else {
templ->has_segwit_txs = false; // force disable if not supported (no txid fields)
}
const char *d = json_get_string(json_tx->u.array.values[i], "data");
templ->txdata.push_back(d);
// if wanted, we can limit the count of txs to include
if (g_limit_txs_per_block && i >= g_limit_txs_per_block-2) {
debuglog("limiting block to %d first txs (of %d)\n", g_limit_txs_per_block, json_tx->u.array.length);
templ->has_filtered_txs = true;
}
}
// std::cerr << "[1] Txes count: " << templ->txdata.size() << std::endl;
// for equihash we need to insert coinbasetxn here
if (!strcmp(g_stratum_algo, "equihash")) {
json_value *json_coinbasetxn = json_get_object(json_result, "coinbasetxn");
if(!json_coinbasetxn)
{
coind_error(coind, "getblocktemplate coinbasetxn");
json_value_free(json);
return NULL;
}
// coinbase_create_equi(coind, templ, json_result);
// here we will take coinbase tx from rpcdata, instead of manually creation
// TODO: implement coinbase_create_equi
templ->value = json_get_int(json_coinbasetxn, "coinbasevalue");
const char *p = json_get_string(json_coinbasetxn, "hash");
const char *d = json_get_string(json_coinbasetxn, "data");
if ((strlen(d) + 1) < (sizeof(templ->coinbase)/sizeof(templ->coinbase[0])))
{
strcpy(templ->coinbase, d);
// std::cerr << templ->coinbase << std::endl;
}
else
{
coind_error(coind, "coinbasetxn doesn't fit in template");
json_value_free(json);
return NULL;
}
char hash_be[256] = { 0 };
string_be(p, hash_be);
/*
std::cout << "txhashes.size() = " << txhashes.size() << " - " << (txhashes.size() % 2) << std::endl;
std::cerr << "[ Decker ] txhashes[" << txhashes.size() << "] = " << std::endl;
for (int i=0; i < txhashes.size(); i++) {
std::string hex(txhashes[i]);
for (std::string::iterator it=hex.begin(); it != hex.end(); it += 2) std::swap(it[0], it[1]);
std::string hex_reversed(hex.rbegin(), hex.rend());
std::cerr << "[" << i << "] \"" << txhashes[i] << "\" - " "\"" << hex_reversed << "\""<< std::endl;
}
*/
vector<std::string> txsteps = merkle_steps(txhashes);
/*
std::cerr << "merkle steps [" << txsteps.size() << "] :" << std::endl;
for (int i=0; i<txsteps.size(); i++) {
std::string hex(txsteps[i]);
for (std::string::iterator it=hex.begin(); it != hex.end(); it += 2) std::swap(it[0], it[1]);
std::string hex_reversed(hex.rbegin(), hex.rend());
std::cerr << "[" << i << "] \"" << txsteps[i] << "\" - " "\"" << hex_reversed << "\""<< std::endl;
}
*/
if (txsteps.size() > 0)
{
std::string mr = merkle_with_first(txsteps, hash_be);
std::string hex(mr);
for (std::string::iterator it=hex.begin(); it != hex.end(); it += 2) std::swap(it[0], it[1]);
std::string hex_reversed(hex.rbegin(), hex.rend());
//std::cerr << hex_reversed << std::endl;
strcpy(templ->mr_hex,hex_reversed.c_str());
//std::cerr << "Merkle (many): " << templ->mr_hex << std::endl;
} else
{
std::string hex(p);
strcpy(templ->mr_hex,hex.c_str());
//std::cerr << "Merkle (one): " << templ->mr_hex << std::endl;
}
/*
// standart - merkle_arr = txsteps = templ->txmerkles - // https://github.com/slushpool/poclbm-zcash/wiki/Stratum-protocol-changes-for-ZCash
// equishash - merkle_arr->merkleroot (including coinbase) - // https://en.bitcoin.it/wiki/Stratum_mining_protocol#mining.notify
https://en.bitcoin.it/wiki/Stratum_mining_protocol#mining.notify
List of merkle branches. The generation transaction is hashed against the merkle branches to build the final merkle root.
*/
}
if (templ->has_filtered_txs) {
// coinbasevalue is a total with all tx fees, need to reduce it if some are skipped
templ->value -= templ->filtered_txs_fee;
}
templ->txmerkles[0] = '\0';
if(templ->has_segwit_txs) {
templ->txcount = txids.size();
templ->txsteps = merkle_steps(txids);
} else {
templ->txcount = txhashes.size();
templ->txsteps = merkle_steps(txhashes);
}
if(templ->has_segwit_txs) {
// * We compute the witness hash (which is the hash including witnesses) of all the block's transactions, except the
// coinbase (where 0x0000....0000 is used instead).
// * The coinbase scriptWitness is a stack of a single 32-byte vector, containing a witness nonce (unconstrained).
// * We build a merkle tree with all those witness hashes as leaves (similar to the hashMerkleRoot in the block header).
// * There must be at least one output whose scriptPubKey is a single 36-byte push, the first 4 bytes (magic) of which are
// {0xaa, 0x21, 0xa9, 0xed}, and the following 32 bytes are SHA256^2(witness root, witness nonce). In case there are
/*
char bin[YAAMP_HASHLEN_BIN*2];
char witness[128] = { 0 };
vector<string> mt_verify = merkle_steps(txhashes);
string witness_mt = merkle_with_first(mt_verify, "0000000000000000000000000000000000000000000000000000000000000000");
mt_verify.clear();
witness_mt = witness_mt + "0000000000000000000000000000000000000000000000000000000000000000";
binlify((unsigned char *)bin, witness_mt.c_str());
sha256_double_hash_hex(bin, witness, YAAMP_HASHLEN_BIN*2);
int clen = (int) (strlen(coind->witness_magic) + strlen(witness)); // 4 + 32 = 36 = 0x24
sprintf(coind->commitment, "6a%02x%s%s", clen/2, coind->witness_magic, witness);
*/
// default commitment is already computed correctly
const char *commitment = json_get_string(json_result, "default_witness_commitment");
if (commitment) {
sprintf(coind->commitment, "%s", commitment);
} else {
templ->has_segwit_txs = false;
}
}
txhashes.clear();
txids.clear();
vector<string>::const_iterator i;
for(i = templ->txsteps.begin(); i != templ->txsteps.end(); ++i)
sprintf(templ->txmerkles + strlen(templ->txmerkles), "\"%s\",", (*i).c_str());
if(templ->txmerkles[0])
templ->txmerkles[strlen(templ->txmerkles)-1] = 0;
// debuglog("merkle transactions %d [%s]\n", templ->txcount, templ->txmerkles);
if (!strcmp(g_stratum_algo, "equihash")) {
string_be(templ->prevhash_hex, templ->prevhash_be);
} else ser_string_be2(templ->prevhash_hex, templ->prevhash_be, 8);
if(!strcmp(coind->symbol, "LBC"))
ser_string_be2(templ->claim_hex, templ->claim_be, 8);
if(!coind->pos)
coind_aux_build_auxs(templ);
coinbase_create(coind, templ, json_result);
json_value_free(json);
return templ;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool coind_create_job(YAAMP_COIND *coind, bool force)
{
// debuglog("create job %s\n", coind->symbol);
bool b = rpc_connected(&coind->rpc);
if(!b) return false;
CommonLock(&coind->mutex);
YAAMP_JOB_TEMPLATE *templ;
// DCR gbt block header is not compatible with getwork submit, so...
if (coind->usegetwork && strcmp(coind->rpcencoding, "DCR") == 0)
templ = decred_create_worktemplate(coind);
else
templ = coind_create_template(coind);
if(!templ)
{
CommonUnlock(&coind->mutex);
// debuglog("%s: create job template failed!\n", coind->symbol);
return false;
}
YAAMP_JOB *job_last = coind->job;
if( !force && job_last && job_last->templ && job_last->templ->created + 45 > time(NULL) &&
templ->height == job_last->templ->height &&
templ->txcount == job_last->templ->txcount &&
strcmp(templ->coinb2, job_last->templ->coinb2) == 0)
{
// debuglog("coind_create_job %s %d same template %x \n", coind->name, coind->height, coind->job->id);
if (templ->txcount) {
templ->txsteps.clear();
templ->txdata.clear();
}
delete templ;
CommonUnlock(&coind->mutex);
return true;
}
////////////////////////////////////////////////////////////////////////////////////////
int height = coind->height;
coind->height = templ->height-1;
if(height > coind->height)
{
stratumlog("%s went from %d to %d\n", coind->name, height, coind->height);
// coind->auto_ready = false;
}
if(height < coind->height && !coind->newblock)
{
if(coind->auto_ready && coind->notreportingcounter++ > 5)
stratumlog("%s %d not reporting\n", coind->name, coind->height);
}
uint64_t 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, bits_str); binlify((unsigned char *)&bits, bits_str);
coin_target = diff_to_target(nbits_to_diff_equi(&bits));
}
else
{
coin_target = decode_compact(templ->nbits);
};
if (templ->nbits && !coin_target) coin_target = 0xFFFF000000000000ULL; // under decode_compact min diff
coind->difficulty = target_to_diff(coin_target);
stratumlog("%s %d diff %g %llx %s\n", coind->name, height, coind->difficulty, coin_target, templ->nbits);
coind->newblock = false;
////////////////////////////////////////////////////////////////////////////////////////
object_delete(coind->job);
coind->job = new YAAMP_JOB;
memset(coind->job, 0, sizeof(YAAMP_JOB));
sprintf(coind->job->name, "%s", coind->symbol);
coind->job->id = job_get_jobid();
coind->job->templ = templ;
coind->job->profit = coind_profitability(coind);
coind->job->maxspeed = coind_nethash(coind) *
(g_current_algo->profit? min(1.0, coind_profitability(coind)/g_current_algo->profit): 1);
coind->job->coind = coind;
coind->job->remote = NULL;
g_list_job.AddTail(coind->job);
CommonUnlock(&coind->mutex);
// debuglog("coind_create_job %s %d new job %x\n", coind->name, coind->height, coind->job->id);
return true;
}