This repository has been archived by the owner on Mar 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
util.cpp
771 lines (609 loc) · 14.6 KB
/
util.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
#include "stratum.h"
#include <math.h>
#include <limits.h>
////////////////////////////////////////////////////////////////////////////////
bool json_get_bool(json_value *json, const char *name)
{
for(int i=0; i<json->u.object.length; i++)
{
if(!strcmp(json->u.object.values[i].name, name))
return json->u.object.values[i].value->u.boolean;
}
return false;
}
json_int_t json_get_int(json_value *json, const char *name)
{
for(int i=0; i<json->u.object.length; i++)
{
if(!strcmp(json->u.object.values[i].name, name))
return json->u.object.values[i].value->u.integer;
}
return 0;
}
double json_get_double(json_value *json, const char *name)
{
for(int i=0; i<json->u.object.length; i++)
{
if(!strcmp(json->u.object.values[i].name, name))
return json->u.object.values[i].value->u.dbl;
}
return 0;
}
const char *json_get_string(json_value *json, const char *name)
{
for(int i=0; i<json->u.object.length; i++)
{
if(!strcmp(json->u.object.values[i].name, name))
return json->u.object.values[i].value->u.string.ptr;
}
return NULL;
}
json_value *json_get_array(json_value *json, const char *name)
{
for(int i=0; i<json->u.object.length; i++)
{
// if(json->u.object.values[i].value->type == json_array && !strcmp(json->u.object.values[i].name, name))
if(!strcmp(json->u.object.values[i].name, name))
return json->u.object.values[i].value;
}
return NULL;
}
//json_value *json_get_array_from_array(json_value *json, const char *name)
//{
// for(int i=0; i<json->u.array.length; i++)
// {
// if(!strcmp(json->u.array.values[i].name, name))
// return json->u.array.values[i].value;
// }
//
// return NULL;
//}
json_value *json_get_object(json_value *json, const char *name)
{
for(int i=0; i<json->u.object.length; i++)
{
if(!strcmp(json->u.object.values[i].name, name))
return json->u.object.values[i].value;
}
return NULL;
}
///////////////////////////////////////////////////////////////////////////////////////////////
FILE *g_debuglog = NULL;
FILE *g_stratumlog = NULL;
FILE *g_clientlog = NULL;
FILE *g_rejectlog = NULL;
void initlog(const char *algo)
{
char debugfile[1024];
sprintf(debugfile, "%s.log", algo);
g_debuglog = fopen(debugfile, "w");
g_stratumlog = fopen("stratum.log", "a");
g_clientlog = fopen("client.log", "a");
g_rejectlog = fopen("reject.log", "a");
}
void closelogs()
{
if (g_debuglog) {
fflush(g_debuglog); fclose(g_debuglog);
}
if (g_stratumlog) {
fflush(g_stratumlog); fclose(g_stratumlog);
}
if (g_clientlog) {
fflush(g_clientlog); fclose(g_clientlog);
}
if (g_rejectlog) {
fflush(g_rejectlog); fclose(g_rejectlog);
}
}
void clientlog(YAAMP_CLIENT *client, const char *format, ...)
{
char buffer[YAAMP_SMALLBUFSIZE];
va_list args;
va_start(args, format);
vsprintf(buffer, format, args);
va_end(args);
time_t rawtime;
struct tm * timeinfo;
char buffer2[80];
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(buffer2, 80, "%Y-%m-%d %H:%M:%S", timeinfo);
char buffer3[YAAMP_SMALLBUFSIZE];
sprintf(buffer3, "%s [%s] %s, %s, %s\n", buffer2, client->sock->ip, client->username, g_current_algo->name, buffer);
printf("%s", buffer3);
if(g_debuglog)
{
fprintf(g_debuglog, "%s", buffer3);
fflush(g_debuglog);
}
if(g_clientlog)
{
fprintf(g_clientlog, "%s", buffer3);
if (fflush(g_clientlog) == EOF) {
// reopen if wiped
fclose(g_clientlog);
g_clientlog = fopen("client.log", "a");
}
}
}
void debuglog(const char *format, ...)
{
char buffer[YAAMP_SMALLBUFSIZE];
va_list args;
va_start(args, format);
vsprintf(buffer, format, args);
va_end(args);
time_t rawtime;
struct tm * timeinfo;
char buffer2[80];
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(buffer2, 80, "%H:%M:%S", timeinfo);
printf("%s: %s", buffer2, buffer);
if(g_debuglog)
{
fprintf(g_debuglog, "%s: %s", buffer2, buffer);
fflush(g_debuglog);
}
}
void debuglog_hex(void *data, int len)
{
uint8_t* const bin = (uint8_t*) data;
char *hex = (char*) calloc(1, len*2 + 2);
if (!hex) return;
for(int i=0; i < len; i++)
sprintf(hex+strlen(hex), "%02x", bin[i]);
strcpy(hex+strlen(hex), "\n");
debuglog(hex);
free(hex);
}
void stratumlog(const char *format, ...)
{
char buffer[YAAMP_SMALLBUFSIZE];
va_list args;
va_start(args, format);
vsprintf(buffer, format, args);
va_end(args);
time_t rawtime;
struct tm * timeinfo;
char buffer2[80];
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(buffer2, 80, "%H:%M:%S", timeinfo);
printf("%s: %s", buffer2, buffer);
if(g_debuglog)
{
fprintf(g_debuglog, "%s: %s", buffer2, buffer);
fflush(g_debuglog);
}
if(g_stratumlog)
{
fprintf(g_stratumlog, "%s: %s", buffer2, buffer);
if (fflush(g_stratumlog) == EOF) {
fclose(g_stratumlog);
g_stratumlog = fopen("stratum.log", "a");
}
}
}
void stratumlogdate(const char *format, ...)
{
char buffer[YAAMP_SMALLBUFSIZE];
char date[16];
va_list args;
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(date, 16, "%Y-%m-%d", timeinfo);
va_start(args, format);
vsprintf(buffer, format, args);
va_end(args);
stratumlog("%s %s", date, buffer);
}
void rejectlog(const char *format, ...)
{
char buffer[YAAMP_SMALLBUFSIZE];
va_list args;
va_start(args, format);
vsnprintf(buffer, YAAMP_SMALLBUFSIZE-1, format, args);
va_end(args);
time_t rawtime;
struct tm * timeinfo;
char buffer2[80];
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(buffer2, 80, "%Y-%m-%d %H:%M:%S", timeinfo);
printf("%s: %s", buffer2, buffer);
if(g_rejectlog)
{
fprintf(g_rejectlog, "%s: %s", buffer2, buffer);
if (fflush(g_rejectlog) == EOF) {
fclose(g_rejectlog);
g_rejectlog = fopen("reject.log", "a");
}
}
}
bool yaamp_error(char const *message)
{
debuglog("ERROR: %d %s\n", errno, message);
closelogs();
exit(1);
}
void yaamp_create_mutex(pthread_mutex_t *mutex)
{
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(mutex, &attr);
pthread_mutexattr_destroy(&attr);
}
const char *header_value(const char *data, const char *search, char *value)
{
value[0] = 0;
char *p = (char *)strstr(data, search);
if(!p) return value;
p += strlen(search);
while(*p == ' ' || *p == ':') p++;
char *p2 = (char *)strstr(p, "\r\n");
if(!p2)
{
strncpy(value, p, 1024);
return value;
}
strncpy(value, p, min(1024, p2 - p));
value[min(1023, p2 - p)] = 0;
return value;
}
////////////////////////////////////////////////////////////////////////////////////////////
const unsigned char g_base64_tab[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
void base64_encode(char *base64, const char *normal)
{
int cb = strlen((char *)normal);
while(cb >= 3)
{
unsigned char b0 = ((normal[0] >> 2) & 0x3F);
unsigned char b1 = ((normal[0] & 0x03) << 4) | ((normal[1] >> 4) & 0x0F);
unsigned char b2 = ((normal[1] & 0x0F) << 2) | ((normal[2] >> 6) & 0x03);
unsigned char b3 = ((normal[2] & 0x3F));
*base64++ = g_base64_tab[b0];
*base64++ = g_base64_tab[b1];
*base64++ = g_base64_tab[b2];
*base64++ = g_base64_tab[b3];
normal += 3;
cb -= 3;
}
if(cb == 1)
{
unsigned char b0 = ((normal[0] >> 2) & 0x3F);
unsigned char b1 = ((normal[0] & 0x03) << 4) | 0;
*base64++ = g_base64_tab[b0];
*base64++ = g_base64_tab[b1];
*base64++ = '=';
*base64++ = '=';
}
else if(cb == 2)
{
unsigned char b0 = ((normal[0] >> 2) & 0x3F);
unsigned char b1 = ((normal[0] & 0x03) << 4) | ((normal[1] >> 4) & 0x0F);
unsigned char b2 = ((normal[1] & 0x0F) << 2) | 0;
*base64++ = g_base64_tab[b0];
*base64++ = g_base64_tab[b1];
*base64++ = g_base64_tab[b2];
*base64++ = '=';
}
*base64 = 0;
}
void base64_decode(char *normal, const char *base64)
{
int i;
unsigned char decoding_tab[256];
memset(decoding_tab, 255, 256);
for(i = 0; i < 64; i++)
decoding_tab[g_base64_tab[i]] = i;
unsigned long current = 0;
int bit_filled = 0;
for(i = 0; base64[i]; i++)
{
if(base64[i] == 0x0A || base64[i] == 0x0D || base64[i] == 0x20 || base64[i] == 0x09)
continue;
if(base64[i] == '=')
break;
unsigned char digit = decoding_tab[base64[i]];
current <<= 6;
current |= digit;
bit_filled += 6;
if(bit_filled >= 8)
{
unsigned long b = (current >> (bit_filled - 8));
*normal++ = (unsigned char)(b & 0xFF);
bit_filled -= 8;
}
}
*normal = 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////
void hexlify(char *hex, const unsigned char *bin, int len)
{
hex[0] = 0;
for(int i=0; i < len; i++)
sprintf(hex+strlen(hex), "%02x", bin[i]);
}
bool ishexa(char *hex, int len)
{
for(int i=0; i<len; i++) {
if (!isxdigit(hex[i])) return false;
}
return true;
}
unsigned char binvalue(const char v)
{
if(v >= '0' && v <= '9')
return v-'0';
if(v >= 'a' && v <= 'f')
return v-'a'+10;
return 0;
}
void binlify(unsigned char *bin, const char *hex)
{
int len = strlen(hex);
for(int i=0; i<len/2; i++)
bin[i] = binvalue(hex[i*2])<<4 | binvalue(hex[i*2+1]);
}
void strprecatchar(char *buffer, char c)
{
char tmp[64];
sprintf(tmp, "%02x%s", c, buffer);
strcpy(buffer, tmp);
}
////////////////////////////////////////////////////////////////////////////////
void ser_number(int n, char *a)
{
unsigned char s[32];
memset(s, 0, 32);
memset(a, 0, 32);
s[0] = 1;
while(n > 127)
{
s[s[0]] = n % 256;
n /= 256;
s[0]++;
}
s[s[0]] = n;
a[0] = 0;
for(int i=0; i<=s[0]; i++)
{
char tmp[32];
sprintf(tmp, "%02x", s[i]);
strcat(a, tmp);
}
// printf("ser_number %d, %s\n", n, a);
}
void ser_string_be(const char *input, char *output, int len)
{
for(int i=0; i<len; i++)
for(int j=0; j<8; j+=2)
memcpy(output + i*8 + (6-j), input + i*8 + j, 2);
}
void ser_string_be2(const char *input, char *output, int len)
{
for(int i=0; i<len; i++)
memcpy(output + i*8, input + (len-i-1)*8, 8);
}
void string_be(const char *input, char *output)
{
int len = strlen(input)/2;
for(int i=0; i<len; i++)
memcpy(output + (len-i-1)*2, input + i*2, 2);
}
void string_be1(char *s)
{
char s2[1024];
strcpy(s2, s);
int len = strlen(s2)/2;
for(int i=0; i<len; i++)
memcpy(s + (len-i-1)*2, s2 + i*2, 2);
}
uint64_t diff_to_target(double difficulty)
{
if(!difficulty) return 0;
uint64_t t = 0x0000ffff00000000*g_current_algo->diff_multiplier/difficulty;
return t;
}
double target_to_diff(uint64_t target)
{
if(!target) return 0;
double d = (double)0x0000ffff00000000/target;
return d;
}
uint64_t decode_compact(const char *input)
{
uint64_t c = htoi64(input);
int nShift = (c >> 24) & 0xff;
double d = (double)0x0000ffff / (double)(c & 0x00ffffff);
while (nShift < 29)
{
d *= 256.0;
nShift++;
}
while (nShift > 29)
{
d /= 256.0;
nShift--;
}
uint64_t v = 0x0000ffff00000000/d;
// debuglog("decode_compact %s -> %f -> %016llx\n", input, d, v);
// int nbytes = (c >> 24) & 0xFF;
//
// nbytes -= 25;
// v = (c & 0xFFFFFF) << (8 * nbytes);
//
// debuglog("decode_compact %s -> %016llx\n", input, v);
return v;
}
//def uint256_from_compact(c):
// c = int(c)
// nbytes = (c >> 24) & 0xFF
// v = (c & 0xFFFFFFL) << (8 * (nbytes - 3))
// return v
uint64_t get_hash_difficulty(unsigned char *input)
{
unsigned char *p = (unsigned char *)input;
uint64_t v =
(uint64_t)p[29] << 56 |
(uint64_t)p[28] << 48 |
(uint64_t)p[27] << 40 |
(uint64_t)p[26] << 32 |
(uint64_t)p[25] << 24 |
(uint64_t)p[24] << 16 |
(uint64_t)p[23] << 8 |
(uint64_t)p[22] << 0;
// char toto[1024];
// hexlify(toto, input, 32);
// debuglog("hash diff %s %016llx\n", toto, v);
return v;
}
unsigned int htoi(const char *s)
{
unsigned int val = 0;
int x = 0;
if(s[x] == '0' && (s[x+1] == 'x' || s[x+1] == 'X'))
x += 2;
while(s[x])
{
if(val > UINT_MAX)
return 0;
else if(s[x] >= '0' && s[x] <='9')
val = val * 16 + s[x] - '0';
else if(s[x]>='A' && s[x] <='F')
val = val * 16 + s[x] - 'A' + 10;
else if(s[x]>='a' && s[x] <='f')
val = val * 16 + s[x] - 'a' + 10;
else
return 0;
x++;
}
return val;
}
uint64_t htoi64(const char *s)
{
uint64_t val = 0;
int x = 0;
if(s[x] == '0' && (s[x+1] == 'x' || s[x+1] == 'X'))
x += 2;
while(s[x])
{
if(val > ULLONG_MAX)
return 0;
else if(s[x] >= '0' && s[x] <='9')
val = val * 16 + s[x] - '0';
else if(s[x]>='A' && s[x] <='F')
val = val * 16 + s[x] - 'A' + 10;
else if(s[x]>='a' && s[x] <='f')
val = val * 16 + s[x] - 'a' + 10;
else
return 0;
x++;
}
return val;
}
#if 0
// gettimeofday seems deprecated in POSIX
long long current_timestamp()
{
long long milliseconds;
struct timeval te;
gettimeofday(&te, NULL);
milliseconds = te.tv_sec*1000LL + te.tv_usec/1000;
return milliseconds;
}
#else
long long current_timestamp()
{
long long milliseconds;
struct timespec te;
clock_gettime(CLOCK_REALTIME, &te);
milliseconds = 1000LL*te.tv_sec + round(te.tv_nsec/1e6);
return milliseconds;
}
#endif
long long current_timestamp_dms() // allow 0.1 ms time
{
long long dms;
struct timespec te;
clock_gettime(CLOCK_REALTIME, &te);
dms = 10000LL*te.tv_sec + round(te.tv_nsec/1e5);
return dms;
}
int opened_files()
{
int fds = 0;
DIR *d = opendir("/proc/self/fd");
if (d) {
while (readdir(d)) fds++;
closedir(d);
}
return fds;
}
int resident_size()
{
int sz, res = 0;
FILE *fp = fopen("/proc/self/statm", "r");
if (fp) {
int p = fscanf(fp, "%d", &sz);
if (p) p += fscanf(fp, "%d", &res);
fclose(fp);
}
return res;
}
void string_lower(char *s)
{
for(int i = 0; s[i]; i++)
s[i] = tolower(s[i]);
}
void string_upper(char *s)
{
for(int i = 0; s[i]; i++)
s[i] = toupper(s[i]);
}
//////////////////////////////////////////////////////////////////////////////////////
int getblocheight(const char *coinb1)
{
unsigned char coinb1_bin[1024];
binlify(coinb1_bin, coinb1);
int height = 0;
uint8_t hlen = 0, *p, *m;
// find 0xffff tag
p = (uint8_t*)coinb1_bin + 32;
m = p + 128;
while (*p != 0xff && p < m) p++;
while (*p == 0xff && p < m) p++;
if (*(p-1) == 0xff && *(p-2) == 0xff)
{
p++; hlen = *p;
p++; height = le16dec(p);
p += 2;
switch (hlen)
{
case 4:
height += 0x10000UL * le16dec(p);
break;
case 3:
height += 0x10000UL * (*p);
break;
}
}
return height;
}
void sha256_double_hash_hex(const char *input, char *output, unsigned int len)
{
char output1[32];
sha256_double_hash(input, output1, len);
hexlify(output, (unsigned char *)output1, 32);
}
void sha256_hash_hex(const char *input, char *output, unsigned int len)
{
char output1[32];
sha256_hash(input, output1, len);
hexlify(output, (unsigned char *)output1, 32);
}