forked from Gingeropolous/wolf-xmr-miner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
2070 lines (1644 loc) · 57.3 KB
/
main.c
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
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <signal.h>
#include <pthread.h>
#include <stdbool.h>
#include <jansson.h>
#include <stdatomic.h>
#include <cpuid.h>
#ifdef __linux__
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sched.h>
#else
#include <winsock2.h>
#undef __cpuid
#endif
#include <CL/cl.h>
#include "cryptonight.h"
#include "minerutils.h"
#include "minerlog.h"
#include "minernet.h"
#include "stratum.h"
#include "miner.h"
#include "ocl.h"
#define STRATUM_TIMEOUT_SECONDS 120
// I know, it's lazy.
#define STRATUM_MAX_MESSAGE_LEN_BYTES 4096
typedef struct _StatusInfo
{
uint64_t SolvedWork;
uint64_t RejectedWork;
double *ThreadHashCounts;
double *ThreadTimes;
} StatusInfo;
pthread_mutex_t StatusMutex = PTHREAD_MUTEX_INITIALIZER;
StatusInfo GlobalStatus;
static cryptonight_func *cryptonight_hash_ctx;
typedef struct _WorkerInfo
{
char *User;
char *Pass;
struct _WorkerInfo *NextWorker;
} WorkerInfo;
typedef struct _PoolInfo
{
SOCKET sockfd;
char *PoolName;
char *StrippedURL;
char *Port;
WorkerInfo WorkerData;
uint32_t MinerThreadCount;
uint32_t *MinerThreads;
atomic_uint StratumID;
char XMRAuthID[64];
} PoolInfo;
atomic_bool *RestartMining;
bool ExitFlag = false;
int ExitPipe[2];
JobInfo Jobs[2];
volatile JobInfo *CurrentJob;
volatile int JobIdx;
typedef struct _Share
{
struct _Share *next;
JobInfo *Job;
uint32_t Nonce;
int Gothash;
uint8_t Blob[32];
} Share;
typedef struct _ShareQueue
{
Share *first;
Share *last;
} ShareQueue;
Share *ShareList;
Share *GetShare()
{
Share *ret;
if (ShareList) {
ret = ShareList;
ShareList = ret->next;
} else {
ret = malloc(sizeof(Share));
}
return ret;
}
void SubmitShare(ShareQueue *queue, Share *NewShare)
{
NewShare->next = NULL;
if(!queue->first) queue->first = queue->last = NewShare;
else queue->last = queue->last->next = NewShare;
}
Share *RemoveShare(ShareQueue *queue)
{
Share *tmp = queue->first;
if(queue->first) queue->first = queue->first->next;
return(tmp);
}
void FreeShare(Share *share)
{
share->next = ShareList;
ShareList = share;
}
ShareQueue CurrentQueue;
pthread_mutex_t QueueMutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t QueueCond = PTHREAD_COND_INITIALIZER;
typedef struct _PoolBroadcastInfo
{
int poolsocket;
WorkerInfo WorkerData;
} PoolBroadcastInfo;
int sendit(int fd, char *buf, int len)
{
int rc;
do
{
rc = send(fd, buf, len, 0);
if (rc == -1)
return rc;
buf += rc;
len -= rc;
} while (len > 0);
return rc < 1 ? -1 : 0;
}
#define BIG_BUF_LEN 262144
void *DaemonUpdateThreadProc(void *Info)
{
uint64_t id = 10;
PoolInfo *pbinfo = (PoolInfo *)Info;
char s[BIG_BUF_LEN];
void *c_ctx = cryptonight_ctx();
pthread_mutex_lock(&QueueMutex);
for(;;)
{
pthread_cond_wait(&QueueCond, &QueueMutex);
for(Share *CurShare = RemoveShare(&CurrentQueue); CurShare; CurShare = RemoveShare(&CurrentQueue))
{
char ASCIINonce[9];
char *ptr;
int ret, len, hdrlen;
if (!CurShare->Job->blockblob)
{
sleep(1);
continue;
}
BinaryToASCIIHex(ASCIINonce, &CurShare->Nonce, 4U);
memcpy(CurShare->Job->blockblob+78, ASCIINonce, 8);
hdrlen = sprintf(s, "POST /json_rpc HTTP/1.0\r\nContent-Length: xxx\r\n\r\n");
ptr = s + hdrlen;
len = snprintf(ptr, BIG_BUF_LEN - hdrlen, "{\"method\": \"submitblock\", \"params\": "
"[\"%s\"]}", CurShare->Job->blockblob);
sprintf(ptr - 7, "%d", len);
ptr[-4] = '\r';
free(CurShare->Job->blockblob);
CurShare->Job->blockblob = NULL;
FreeShare(CurShare);
ret = sendit(pbinfo->sockfd, s, len + hdrlen);
if (ret == -1)
break;
pthread_mutex_lock(&StatusMutex);
GlobalStatus.SolvedWork++;
pthread_mutex_unlock(&StatusMutex);
Log(LOG_NETDEBUG, "Request: %s", s);
}
}
pthread_mutex_unlock(&QueueMutex);
// free(c_ctx);
return(NULL);
}
#define JSON_BUF_LEN 345
void *PoolBroadcastThreadProc(void *Info)
{
uint64_t id = 10;
PoolInfo *pbinfo = (PoolInfo *)Info;
char s[JSON_BUF_LEN];
void *c_ctx = cryptonight_ctx();
pthread_mutex_lock(&QueueMutex);
for(;;)
{
pthread_cond_wait(&QueueCond, &QueueMutex);
for(Share *CurShare = RemoveShare(&CurrentQueue); CurShare; CurShare = RemoveShare(&CurrentQueue))
{
char ASCIINonce[9], ASCIIResult[65];
uint8_t HashResult[32];
int ret, len;
BinaryToASCIIHex(ASCIINonce, &CurShare->Nonce, 4U);
if (!CurShare->Gothash) {
((uint32_t *)(CurShare->Job->XMRBlob + 39))[0] = CurShare->Nonce;
cryptonight_hash_ctx(HashResult, CurShare->Job->XMRBlob, c_ctx);
BinaryToASCIIHex(ASCIIResult, HashResult, 32);
} else {
BinaryToASCIIHex(ASCIIResult, CurShare->Blob, 32);
}
len = snprintf(s, JSON_BUF_LEN,
"{\"method\": \"submit\", \"params\": {\"id\": \"%s\", "
"\"job_id\": \"%s\", \"nonce\": \"%s\", \"result\": \"%s\"}, "
"\"id\":1}\r\n\n",
pbinfo->XMRAuthID, CurShare->Job->ID, ASCIINonce, ASCIIResult);
FreeShare(CurShare);
pthread_mutex_lock(&StatusMutex);
GlobalStatus.SolvedWork++;
pthread_mutex_unlock(&StatusMutex);
Log(LOG_NETDEBUG, "Request: %s", s);
ret = sendit(pbinfo->sockfd, s, len);
if (ret == -1)
break;
}
}
pthread_mutex_unlock(&QueueMutex);
// free(c_ctx);
return(NULL);
}
int32_t XMRSetKernelArgs(AlgoContext *HashData, void *HashInput, uint32_t Target)
{
cl_int retval;
cl_uint zero = 0;
size_t GlobalThreads = HashData->GlobalSize, LocalThreads = HashData->WorkSize;
if(!HashData || !HashInput) return(ERR_STUPID_PARAMS);
retval = clEnqueueWriteBuffer(*HashData->CommandQueues, HashData->InputBuffer, CL_TRUE, 0, 76, HashInput, 0, NULL, NULL);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clEnqueueWriteBuffer to fill input buffer.", retval);
return(ERR_OCL_API);
}
retval = clSetKernelArg(HashData->Kernels[0], 0, sizeof(cl_mem), &HashData->InputBuffer);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clSetKernelArg for kernel %d, argument %d.", retval, 0, 0);
return(ERR_OCL_API);
}
// Scratchpads
retval = clSetKernelArg(HashData->Kernels[0], 1, sizeof(cl_mem), HashData->ExtraBuffers + 0);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clSetKernelArg for kernel %d, argument %d.", retval, 0, 1);
return(ERR_OCL_API);
}
// States
retval = clSetKernelArg(HashData->Kernels[0], 2, sizeof(cl_mem), HashData->ExtraBuffers + 1);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clSetKernelArg for kernel %d, argument %d.", retval, 0, 2);
return(ERR_OCL_API);
}
// CN2 Kernel
// Scratchpads
retval = clSetKernelArg(HashData->Kernels[1], 0, sizeof(cl_mem), HashData->ExtraBuffers + 0);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clSetKernelArg for kernel %d, argument %d.", retval, 1, 0);
return(ERR_OCL_API);
}
// States
retval = clSetKernelArg(HashData->Kernels[1], 1, sizeof(cl_mem), HashData->ExtraBuffers + 1);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clSetKernelArg for kernel %d, argument %d.", retval, 1, 1);
return(ERR_OCL_API);
}
// CN3 Kernel
// Scratchpads
retval = clSetKernelArg(HashData->Kernels[2], 0, sizeof(cl_mem), HashData->ExtraBuffers + 0);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clSetKernelArg for kernel %d, argument %d.", retval, 2, 0);
return(ERR_OCL_API);
}
// States
retval = clSetKernelArg(HashData->Kernels[2], 1, sizeof(cl_mem), HashData->ExtraBuffers + 1);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clSetKernelArg for kernel %d, argument %d.", retval, 2, 1);
return(ERR_OCL_API);
}
// Branch 0
retval = clSetKernelArg(HashData->Kernels[2], 2, sizeof(cl_mem), HashData->ExtraBuffers + 2);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clSetKernelArg for kernel %d, argument %d.", retval, 2, 2);
return(ERR_OCL_API);
}
// Branch 1
retval = clSetKernelArg(HashData->Kernels[2], 3, sizeof(cl_mem), HashData->ExtraBuffers + 3);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clSetKernelArg for kernel %d, argument %d.", retval, 2, 3);
return(ERR_OCL_API);
}
// Branch 2
retval = clSetKernelArg(HashData->Kernels[2], 4, sizeof(cl_mem), HashData->ExtraBuffers + 4);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clSetKernelArg for kernel %d, argument %d.", retval, 2, 4);
return(ERR_OCL_API);
}
// Branch 3
retval = clSetKernelArg(HashData->Kernels[2], 5, sizeof(cl_mem), HashData->ExtraBuffers + 5);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clSetKernelArg for kernel %d, argument %d.", retval, 2, 5);
return(ERR_OCL_API);
}
for(int i = 0; i < 4; ++i)
{
// States
retval = clSetKernelArg(HashData->Kernels[i + 3], 0, sizeof(cl_mem), HashData->ExtraBuffers + 1);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clSetKernelArg for kernel %d, argument %d.", retval, i + 3, 0);
return(ERR_OCL_API);
}
// Nonce buffer
retval = clSetKernelArg(HashData->Kernels[i + 3], 1, sizeof(cl_mem), HashData->ExtraBuffers + (i + 2));
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clSetKernelArg for kernel %d, argument %d.", retval, i + 3, 1);
return(ERR_OCL_API);
}
// Output
retval = clSetKernelArg(HashData->Kernels[i + 3], 2, sizeof(cl_mem), &HashData->OutputBuffer);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clSetKernelArg for kernel %d, argument %d.", retval, i + 3, 2);
return(ERR_OCL_API);
}
// Target
retval = clSetKernelArg(HashData->Kernels[i + 3], 3, sizeof(cl_uint), &Target);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clSetKernelArg for kernel %d, argument %d.", retval, i + 3, 3);
return(ERR_OCL_API);
}
}
return(ERR_SUCCESS);
}
int32_t RunXMRTest(AlgoContext *HashData, void *HashOutput)
{
cl_int retval;
cl_uint zero = 0;
size_t GlobalThreads = HashData->GlobalSize, LocalThreads = HashData->WorkSize;
size_t BranchNonces[4] = {0};
if(!HashData || !HashOutput) return(ERR_STUPID_PARAMS);
for(int i = 2; i < 6; ++i)
{
retval = clEnqueueWriteBuffer(*HashData->CommandQueues, HashData->ExtraBuffers[i], CL_FALSE, sizeof(cl_uint) * GlobalThreads, sizeof(cl_uint), &zero, 0, NULL, NULL);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clEnqueueWriteBuffer to zero branch buffer counter %d.", retval, i - 2);
return(ERR_OCL_API);
}
}
retval = clEnqueueWriteBuffer(*HashData->CommandQueues, HashData->OutputBuffer, CL_FALSE, sizeof(cl_uint) * 0xFF, sizeof(cl_uint), &zero, 0, NULL, NULL);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clEnqueueReadBuffer to fetch results.", retval);
return(ERR_OCL_API);
}
clFinish(*HashData->CommandQueues);
size_t Nonce[2] = {HashData->Nonce, 1}, gthreads[2] = { GlobalThreads, 8 }, lthreads[2] = { LocalThreads, 8 };
{
retval = clEnqueueNDRangeKernel(*HashData->CommandQueues, HashData->Kernels[0], 2, Nonce, gthreads, lthreads, 0, NULL, NULL);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clEnqueueNDRangeKernel for kernel %d.", retval, 0);
return(ERR_OCL_API);
}
}
/*for(int i = 1; i < 3; ++i)
{
retval = clEnqueueNDRangeKernel(*HashData->CommandQueues, HashData->Kernels[i], 1, &HashData->Nonce, &GlobalThreads, &LocalThreads, 0, NULL, NULL);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clEnqueueNDRangeKernel for kernel %d.", retval, i);
return(ERR_OCL_API);
}
}*/
retval = clEnqueueNDRangeKernel(*HashData->CommandQueues, HashData->Kernels[1], 1, &HashData->Nonce, &GlobalThreads, &LocalThreads, 0, NULL, NULL);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clEnqueueNDRangeKernel for kernel %d.", retval, 1);
return(ERR_OCL_API);
}
retval = clEnqueueNDRangeKernel(*HashData->CommandQueues, HashData->Kernels[2], 2, Nonce, gthreads, lthreads, 0, NULL, NULL);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clEnqueueNDRangeKernel for kernel %d.", retval, 2);
return(ERR_OCL_API);
}
retval = clEnqueueReadBuffer(*HashData->CommandQueues, HashData->ExtraBuffers[2], CL_FALSE, sizeof(cl_uint) * GlobalThreads, sizeof(cl_uint), BranchNonces, 0, NULL, NULL);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clEnqueueReadBuffer to fetch results.", retval);
return(ERR_OCL_API);
}
retval = clEnqueueReadBuffer(*HashData->CommandQueues, HashData->ExtraBuffers[3], CL_FALSE, sizeof(cl_uint) * GlobalThreads, sizeof(cl_uint), BranchNonces + 1, 0, NULL, NULL);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clEnqueueReadBuffer to fetch results.", retval);
return(ERR_OCL_API);
}
retval = clEnqueueReadBuffer(*HashData->CommandQueues, HashData->ExtraBuffers[4], CL_FALSE, sizeof(cl_uint) * GlobalThreads, sizeof(cl_uint), BranchNonces + 2, 0, NULL, NULL);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clEnqueueReadBuffer to fetch results.", retval);
return(ERR_OCL_API);
}
retval = clEnqueueReadBuffer(*HashData->CommandQueues, HashData->ExtraBuffers[5], CL_FALSE, sizeof(cl_uint) * GlobalThreads, sizeof(cl_uint), BranchNonces + 3, 0, NULL, NULL);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clEnqueueReadBuffer to fetch results.", retval);
return(ERR_OCL_API);
}
clFinish(*HashData->CommandQueues);
for(int i = 0; i < 4; ++i)
{
if(BranchNonces[i])
{
//retval = clEnqueueNDRangeKernel(*HashData->CommandQueues, HashData->Kernels[i + 1], 1, &HashData->Nonce, BranchNonces + i, &LocalThreads, 0, NULL, NULL);
retval = clEnqueueNDRangeKernel(*HashData->CommandQueues, HashData->Kernels[i + 3], 1, &HashData->Nonce, BranchNonces + i, &LocalThreads, 0, NULL, NULL);
if(retval != CL_SUCCESS)
{
//Log(LOG_CRITICAL, "Error %d when calling clEnqueueNDRangeKernel for kernel %d.", retval, i + 1);
Log(LOG_CRITICAL, "Error %d when calling clEnqueueNDRangeKernel for kernel %d.", retval, i + 3);
return(ERR_OCL_API);
}
}
}
retval = clEnqueueReadBuffer(*HashData->CommandQueues, HashData->OutputBuffer, CL_TRUE, 0, sizeof(cl_uint) * 0x100, HashOutput, 0, NULL, NULL);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clEnqueueReadBuffer to fetch results.", retval);
return(ERR_OCL_API);
}
clFinish(*HashData->CommandQueues);
HashData->Nonce += GlobalThreads;
return(ERR_SUCCESS);
}
int32_t XMRCleanup(AlgoContext *HashData)
{
//for(int i = 0; i < 5; ++i) clReleaseKernel(HashData->Kernels[i]);
for(int i = 0; i < 7; ++i) clReleaseKernel(HashData->Kernels[i]);
clReleaseProgram(HashData->Program);
clReleaseMemObject(HashData->InputBuffer);
for(int i = 0; i < 6; ++i) clReleaseMemObject(HashData->ExtraBuffers[i]);
clReleaseMemObject(HashData->OutputBuffer);
free(HashData->ExtraBuffers);
clReleaseCommandQueue(*HashData->CommandQueues);
free(HashData->CommandQueues);
free(HashData->GPUIdxs);
}
int32_t SetupXMRTest(AlgoContext *HashData, OCLPlatform *OCL, uint32_t DeviceIdx)
{
size_t len;
cl_int retval;
char *KernelSource, *BuildLog, *Options;
size_t GlobalThreads = OCL->Devices[DeviceIdx].rawIntensity, LocalThreads = OCL->Devices[DeviceIdx].WorkSize;
#ifdef CL_VERSION_2_0
const cl_queue_properties CommandQueueProperties[] = { 0, 0, 0 };
#else
const cl_command_queue_properties CommandQueueProperties = { 0 };
#endif
// Sanity checks
if(!HashData || !OCL) return(ERR_STUPID_PARAMS);
HashData->GlobalSize = GlobalThreads;
HashData->WorkSize = LocalThreads;
HashData->CommandQueues = (cl_command_queue *)malloc(sizeof(cl_command_queue));
#ifdef CL_VERSION_2_0
*HashData->CommandQueues = clCreateCommandQueueWithProperties(OCL->Context, OCL->Devices[DeviceIdx].DeviceID, CommandQueueProperties, &retval);
#else
*HashData->CommandQueues = clCreateCommandQueue(OCL->Context, OCL->Devices[DeviceIdx].DeviceID, CommandQueueProperties, &retval);
#endif
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clCreateCommandQueueWithProperties.", retval);
return(ERR_OCL_API);
}
// One extra buffer for the scratchpads is required, one for the states, and one for
// each of the four possible branches at the end.
HashData->ExtraBuffers = (cl_mem *)malloc(sizeof(cl_mem) * 6);
HashData->InputBuffer = clCreateBuffer(OCL->Context, CL_MEM_READ_ONLY, 80, NULL, &retval);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clCreateBuffer to create input buffer.", retval);
return(ERR_OCL_API);
}
// Scratchpads
HashData->ExtraBuffers[0] = clCreateBuffer(OCL->Context, CL_MEM_READ_WRITE, (1 << 21) * GlobalThreads, NULL, &retval);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clCreateBuffer to create hash scratchpads buffer.", retval);
return(ERR_OCL_API);
}
// States
HashData->ExtraBuffers[1] = clCreateBuffer(OCL->Context, CL_MEM_READ_WRITE, 200 * GlobalThreads, NULL, &retval);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clCreateBuffer to create hash states buffer.", retval);
return(ERR_OCL_API);
}
// Blake-256 branches
HashData->ExtraBuffers[2] = clCreateBuffer(OCL->Context, CL_MEM_READ_WRITE, sizeof(cl_uint) * (GlobalThreads + 2), NULL, &retval);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clCreateBuffer to create Branch 0 buffer.", retval);
return(ERR_OCL_API);
}
// Groestl-256 branches
HashData->ExtraBuffers[3] = clCreateBuffer(OCL->Context, CL_MEM_READ_WRITE, sizeof(cl_uint) * (GlobalThreads + 2), NULL, &retval);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clCreateBuffer to create Branch 1 buffer.", retval);
return(ERR_OCL_API);
}
// JH-256 branches
HashData->ExtraBuffers[4] = clCreateBuffer(OCL->Context, CL_MEM_READ_WRITE, sizeof(cl_uint) * (GlobalThreads + 2), NULL, &retval);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clCreateBuffer to create Branch 2 buffer.", retval);
return(ERR_OCL_API);
}
// Skein-512 branches
HashData->ExtraBuffers[5] = clCreateBuffer(OCL->Context, CL_MEM_READ_WRITE, sizeof(cl_uint) * (GlobalThreads + 2), NULL, &retval);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clCreateBuffer to create Branch 3 buffer.", retval);
return(ERR_OCL_API);
}
// Assume we may find up to 0xFF nonces in one run - it's reasonable
HashData->OutputBuffer = clCreateBuffer(OCL->Context, CL_MEM_READ_WRITE, sizeof(cl_uint) * 0x100, NULL, &retval);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clCreateBuffer to create output buffer.", retval);
return(ERR_OCL_API);
}
len = LoadTextFile(&KernelSource, "cryptonight.cl");
HashData->Program = clCreateProgramWithSource(OCL->Context, 1, (const char **)&KernelSource, NULL, &retval);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clCreateProgramWithSource on the contents of %s.", retval, "cryptonight.cl");
return(ERR_OCL_API);
}
Options = (char *)malloc(sizeof(char) * 32);
snprintf(Options, 31, "-I. -DWORKSIZE=%d", LocalThreads);
retval = clBuildProgram(HashData->Program, 1, &OCL->Devices[DeviceIdx].DeviceID, Options, NULL, NULL);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clBuildProgram.", retval);
retval = clGetProgramBuildInfo(HashData->Program, OCL->Devices[DeviceIdx].DeviceID, CL_PROGRAM_BUILD_LOG, 0, NULL, &len);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clGetProgramBuildInfo for length of build log output.", retval);
return(ERR_OCL_API);
}
BuildLog = (char *)malloc(sizeof(char) * (len + 2));
retval = clGetProgramBuildInfo(HashData->Program, OCL->Devices[DeviceIdx].DeviceID, CL_PROGRAM_BUILD_LOG, len, BuildLog, NULL);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clGetProgramBuildInfo for build log.", retval);
return(ERR_OCL_API);
}
Log(LOG_CRITICAL, "Build Log:\n%s", BuildLog);
free(BuildLog);
return(ERR_OCL_API);
}
cl_build_status status;
do
{
retval = clGetProgramBuildInfo(HashData->Program, OCL->Devices[DeviceIdx].DeviceID, CL_PROGRAM_BUILD_STATUS, sizeof(cl_build_status), &status, NULL);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clGetProgramBuildInfo for status of build.", retval);
return(ERR_OCL_API);
}
sleep(1);
} while(status == CL_BUILD_IN_PROGRESS);
retval = clGetProgramBuildInfo(HashData->Program, OCL->Devices[DeviceIdx].DeviceID, CL_PROGRAM_BUILD_LOG, 0, NULL, &len);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clGetProgramBuildInfo for length of build log output.", retval);
return(ERR_OCL_API);
}
BuildLog = (char *)malloc(sizeof(char) * (len + 2));
retval = clGetProgramBuildInfo(HashData->Program, OCL->Devices[DeviceIdx].DeviceID, CL_PROGRAM_BUILD_LOG, len, BuildLog, NULL);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clGetProgramBuildInfo for build log.", retval);
return(ERR_OCL_API);
}
Log(LOG_DEBUG, "Build Log:\n%s", BuildLog);
free(BuildLog);
free(KernelSource);
HashData->Kernels = (cl_kernel *)malloc(sizeof(cl_kernel) * 7);
const char *KernelNames[] = { "cn0", "cn1", "cn2", "Blake", "Groestl", "JH", "Skein" };
for(int i = 0; i < 7; ++i)
{
HashData->Kernels[i] = clCreateKernel(HashData->Program, KernelNames[i], &retval);
if(retval != CL_SUCCESS)
{
Log(LOG_CRITICAL, "Error %d when calling clCreateKernel for kernel %s.", retval, KernelNames[i]);
return(ERR_OCL_API);
}
}
HashData->Nonce = 0;
// Hardcode one GPU per thread in this version
HashData->GPUIdxs = (size_t *)malloc(sizeof(size_t));
*HashData->GPUIdxs = DeviceIdx;
return(ERR_SUCCESS);
}
static void RestartMiners(PoolInfo *Pool)
{
for(int i = 0; i < Pool->MinerThreadCount; ++i)
atomic_store(RestartMining + i, true);
}
static const char getblkc[] = "POST /json_rpc HTTP/1.0\r\nContent-Length: 27\r\n\r\n"
"{\"method\": \"getblockcount\"}";
#define WALLETLEN 95
static char getblkt[] = "POST /json_rpc HTTP/1.0\r\nContent-Length: 178\r\n\r\n"
"{\"method\": \"getblocktemplate\", \"params\": {\"reserve_size\": 8, \"wallet_address\": "
"\"9xaXMreKDK7bctpHtTE9zUUTgffkRvwZJ7UvyJGAQHkvBFqUYWwhVWWendW6NAdvtB8nn883WQxtU7cpe5eyJiUxLZ741t5\"}}";
void *DaemonThreadProc(void *InfoPtr)
{
PoolInfo *Pool = (PoolInfo *)InfoPtr;
JobInfo *NextJob;
char *l, *crlf;
int poolsocket, ret;
size_t PartialMessageOffset;
char rawresponse[BIG_BUF_LEN];
int len, delay = 32;
int rlen;
uint64_t height, prevheight = 0;
time_t job_time;
poolsocket = Pool->sockfd;
if (strlen(Pool->WorkerData.User) != WALLETLEN)
{
Log(LOG_ERROR, "Invalid username / wallet address\n");
return(NULL);
}
memcpy(getblkt+128, Pool->WorkerData.User, WALLETLEN);
ret = sendit(poolsocket, (char *)getblkt, sizeof(getblkt)-1);
if (ret == -1)
return(NULL);
NextJob = &Jobs[0];
PartialMessageOffset = 0;
l = NULL;
crlf = NULL;
rlen = 0;
// Listen for work until termination.
for(;;)
{
char *tmsg;
int mlen;
// receive
ret = recv(poolsocket, rawresponse + PartialMessageOffset, 256, 0);
if (ret <= 0)
{
fail:
closesocket(poolsocket);
RestartMiners(Pool);
retry:
poolsocket = Pool->sockfd = ConnectToPool(Pool->StrippedURL, Pool->Port);
if(poolsocket == INVALID_SOCKET)
{
Log(LOG_ERROR, "Unable to reconnect to daemon. Sleeping 10 seconds...\n");
sleep(10);
goto retry;
}
ret = sendit(poolsocket, (char *)getblkc, sizeof(getblkc)-1);
if (ret == -1)
return(NULL);
PartialMessageOffset = 0;
l = NULL;
crlf = NULL;
rlen = 0;
continue;
}
PartialMessageOffset += ret;
rawresponse[PartialMessageOffset] = 0x00;
if (!l)
{
l = strstr(rawresponse, "Content-Length: ");
if (!l)
continue;
}
if (!crlf)
{
crlf = strstr(l, "\r\n\r\n");
if (!crlf)
continue;
}
if (!rlen)
{
if (sscanf(l + sizeof("Content-Length:"), "%d", &rlen) != 1)
{
goto fail;
}
tmsg = crlf + 4;
tmsg[rlen] = 0;
}
mlen = PartialMessageOffset - (crlf - rawresponse) - 4;
mlen = rlen - mlen;
if (mlen)
{
ret = recv(poolsocket, rawresponse + PartialMessageOffset, mlen, 0);
if (ret <= 0)
goto fail;
PartialMessageOffset += ret;
if (ret < mlen)
continue;
}
// We now have a complete message
PartialMessageOffset = 0;
l = NULL;
crlf = NULL;
rlen = 0;
json_t *msg, *result, *err;
double TotalHashrate = 0;
Log(LOG_NETDEBUG, "Got something: %s", tmsg);
msg = json_loads(tmsg, 0, NULL);
if(!msg)
{
Log(LOG_CRITICAL, "Error parsing JSON from daemon.");
closesocket(poolsocket);
return(NULL);
}
result = json_object_get(msg, "result");
if (result)
{
json_t *jcount, *jheight;
if ((jcount = json_object_get(result, "count")))
{
height = json_integer_value(jcount);
// new height, get the block info
if (height != prevheight)
{
ret = sendit(poolsocket, getblkt, sizeof(getblkt)-1);
if (ret == -1)
return(NULL);
json_decref(msg);
continue;
}
// height is the same, wait and poll again
} else if ((jheight = json_object_get(result, "height")))
{
height = json_integer_value(jheight);
const char *tmpl = json_string_value(json_object_get(result, "blocktemplate_blob"));
const char *hasher = json_string_value(json_object_get(result, "blockhashing_blob"));
uint64_t diff = json_integer_value(json_object_get(result, "difficulty"));
ASCIIHexToBinary(NextJob->XMRBlob, hasher, strlen(hasher));
Log(LOG_NOTIFY, "New block at diff %lu", diff);
diff = 0xffffffffffffffffUL / diff;
NextJob->XMRTarget = diff >> 32;
NextJob->blockblob = strdup(tmpl);
CurrentJob = NextJob;
JobIdx++;
NextJob = &Jobs[JobIdx&1];
RestartMiners(Pool);
// reduce polling frequency right after
// a new block has been announced.
delay = 32;
prevheight = height;
time(&job_time);
job_time += 240;
}
if (jcount || jheight)
{
struct timeval timeout;
timeout.tv_sec = delay;
timeout.tv_usec = 0;
// reduce delay between polls
if (delay > 1)
delay >>= 1;
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(poolsocket, &readfds);
ret = select(poolsocket + 1, &readfds, NULL, NULL, &timeout);
if(ret != 1 || !FD_ISSET(poolsocket, &readfds))
{
// reduce polling impact:
// getblockcount is nearly zero cost
// but get a new template if we've spent too long on this job
if (time(NULL) > job_time)
ret = sendit(Pool->sockfd, (char *)getblkt, sizeof(getblkt)-1);
else
ret = sendit(Pool->sockfd, (char *)getblkc, sizeof(getblkc)-1);
if (ret == -1)
return(NULL);
}
json_decref(msg);
continue;
}
}
err = json_object_get(msg, "error");
pthread_mutex_lock(&StatusMutex);
if(!err && !strcmp(json_string_value(json_object_get(result, "status")), "OK"))
{
Log(LOG_INFO, "Block accepted: %d/%d (%.02f%%)", GlobalStatus.SolvedWork - GlobalStatus.RejectedWork, GlobalStatus.SolvedWork, (double)(GlobalStatus.SolvedWork - GlobalStatus.RejectedWork) / GlobalStatus.SolvedWork * 100.0);
}