-
Notifications
You must be signed in to change notification settings - Fork 15
/
miner.cpp
468 lines (362 loc) · 13.1 KB
/
miner.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
#include "core.h"
unsigned int nBestHeight = 0;
unsigned int nStartTimer = 0;
unsigned int nPrimes = 0;
unsigned int nChains = 0;
unsigned int nSieves = 0;
unsigned int nBlocks = 0;
unsigned int nDifficulty = 0;
namespace Core
{
/** Main Miner Thread. Bound to the class with boost. Might take some rearranging to get working with OpenCL. **/
void MinerThread::PrimeMiner()
{
loop
{
try
{
/** Keep thread at idle CPU usage if waiting to submit or recieve block. **/
Sleep(1);
/** Assure that this thread stays idle when waiting for new block, or share submission. **/
if(fNewBlock || fBlockWaiting)
continue;
/** Lock the Thread at this Mutex when Changing Block Pointer. **/
MUTEX.lock();
CBigNum BaseHash(cBlock.GetHash());
MUTEX.unlock();
mpz_t zPrimeOrigin, zPrimeOriginOffset, zFirstSieveElement, zPrimorialMod, zTempVar, zResidue, zTwo, zN, zOctuplet;
unsigned int i = 0;
unsigned int j = 0;
unsigned int nSize = 0;
unsigned int nPrimeCount = 0;
unsigned int nSieveDifficulty = 0;
uint64 nStart = 0;
uint64 nStop = 0;
unsigned int nLastOffset = 0;
long nElapsedTime = 0;
long nStartTime = 0;
mpz_init(zPrimeOriginOffset);
mpz_init(zFirstSieveElement);
mpz_init(zPrimorialMod);
mpz_init(zOctuplet);
mpz_init(zTempVar);
mpz_init(zPrimeOrigin);
mpz_init(zResidue);
mpz_init_set_ui(zTwo, 2);
mpz_init(zN);
bignum2mpz(&BaseHash, zPrimeOrigin);
nSize = mpz_sizeinbase(zPrimeOrigin, 2);
unsigned char* bit_array_sieve = (unsigned char*)malloc((nBitArray_Size)/8);
for(j=0; j<256 && !fNewBlock && !fBlockWaiting; j++)
{
memset(bit_array_sieve, 0x00, (nBitArray_Size)/8);
mpz_mod(zPrimorialMod, zPrimeOrigin, zPrimorial);
mpz_sub(zPrimorialMod, zPrimorial, zPrimorialMod);
mpz_mod(zPrimorialMod, zPrimorialMod, zPrimorial);
mpz_import( zOctuplet, 1, 1, sizeof(octuplet_origins[j]), 0, 0, &octuplet_origins[j]);
mpz_add(zPrimorialMod, zPrimorialMod, zOctuplet);
mpz_add(zTempVar, zPrimeOrigin, zPrimorialMod);
mpz_set(zFirstSieveElement, zTempVar);
for(unsigned int i=nPrimorialEndPrime; i<nPrimeLimit && !fNewBlock && !fBlockWaiting; i++)
{
unsigned long p = primes[i];
unsigned int inv = inverses[i];
unsigned int base_remainder = mpz_tdiv_ui(zTempVar, p);
unsigned int remainder = base_remainder;
unsigned long r = (p-remainder)*inv;
unsigned int index = r % p;
while(index < nBitArray_Size)
{
bit_array_sieve[(index)>>3] |= (1<<((index)&7));
index += p;
}
remainder = base_remainder + 2;
if (p<remainder)
remainder -= p;
r = (p-remainder)*inv;
index = r % p;
while(index < nBitArray_Size)
{
bit_array_sieve[(index)>>3] |= (1<<((index)&7));
index += p;
}
remainder = base_remainder + 6;
if (p<remainder)
remainder -= p;
r = (p-remainder)*inv;
index = r % p;
while(index < nBitArray_Size)
{
bit_array_sieve[(index)>>3] |= (1<<((index)&7));
index += p;
}
remainder = base_remainder + 8;
if (p<remainder)
remainder -= p;
r = (p - remainder) * inv;
index = r % p;
while(index < nBitArray_Size)
{
bit_array_sieve[(index)>>3] |= (1<<((index)&7));
index += p;
}
remainder = base_remainder + 12;
if (p<remainder)
remainder -= p;
r = (p - remainder) * inv;
index = r % p;
while(index < nBitArray_Size)
{
bit_array_sieve[(index)>>3] |= (1<<((index)&7));
index += p;
}
nSieves++;
}
for(i=0; i<nBitArray_Size && !fNewBlock && !fBlockWaiting; i++)
{
if( bit_array_sieve[(i)>>3] & (1<<((i)&7)) )
continue;
/** Get the Prime origin from Primorial and Sieve. **/
mpz_mul_ui(zTempVar, zPrimorial, i);
mpz_add(zTempVar, zFirstSieveElement, zTempVar);
mpz_set(zPrimeOriginOffset, zTempVar);
/** Ensure Number is Prime before Checking Cluster. **/
mpz_sub_ui(zN, zTempVar, 1);
mpz_powm(zResidue, zTwo, zN, zTempVar);
if (mpz_cmp_ui(zResidue, 1) != 0)
continue;
nPrimes++;
nPrimeCount = 1;
nLastOffset = 2;
unsigned int nPrimeGap = 2;
/** Determine with GMP the size of possible cluster at this prime. **/
while(nPrimeGap <= 12)
{
mpz_add_ui(zTempVar, zTempVar, 2);
mpz_sub_ui(zN, zTempVar, 1);
mpz_powm(zResidue, zTwo, zN, zTempVar);
if (mpz_cmp_ui(zResidue, 1) == 0)
{
nPrimeGap = 2;
nPrimeCount++;
nPrimes++;
}
else
nPrimeGap += 2;
nLastOffset+=2;
}
if(nPrimeCount >= 2)
{
/** Increment the Chain Counter if Cluster is above size 3. **/
nChains++;
/** Obtain the nNonce value from the Temporary mpz. **/
mpz_sub(zTempVar, zPrimeOriginOffset, zPrimeOrigin);
cBlock.nNonce = mpz2uint64(zTempVar);
/** Run Small Check from Sieve Before Costly Cluster Check. **/
//if(SetBits(GetSieveDifficulty(cBlock.GetPrime() + nLastOffset, nPrimeCount)) < cBlock.nBits)
// continue;
/** Check that the Prime Cluster is large enough. **/
std::vector<unsigned int> vPrimes;
unsigned int nBits = GetPrimeBits(cBlock.GetPrime(), 1, vPrimes);
if(nBits >= cBlock.nBits)
{
printf("[MASTER] Prime Cluster Found of Difficulty %f [", nBits / 10000000.0);
for(int nIndex = 0; nIndex < vPrimes.size() - 1; nIndex++)
printf(" + %u,", vPrimes[nIndex]);
printf(" + %u ]\n\n%s\n\n", vPrimes[vPrimes.size() - 1], cBlock.GetPrime().ToString().c_str());
cServerConnection->SubmitBlock(cBlock);
break;
}
}
}
}
mpz_clear(zPrimeOrigin);
mpz_clear(zPrimeOriginOffset);
mpz_clear(zFirstSieveElement);
mpz_clear(zResidue);
mpz_clear(zTwo);
mpz_clear(zN);
mpz_clear(zPrimorialMod);
mpz_clear(zTempVar);
mpz_clear(zOctuplet);
free(bit_array_sieve);
fNewBlock = true;
fBlockWaiting = false;
}
catch(std::exception& e){ printf("ERROR: %s\n", e.what()); }
}
}
/** Reset the block on each of the Threads. **/
void ServerConnection::ResetThreads()
{
/** Reset each individual flag to tell threads to stop mining. **/
for(int nIndex = 0; nIndex < THREADS.size(); nIndex++)
{
THREADS[nIndex]->fNewBlock = true;
THREADS[nIndex]->fBlockWaiting = false;
}
/** Reset the Block Height Timer. **/
HEIGHT_TIMER.Reset();
}
/** Add a Block to the Submit Queue. **/
void ServerConnection::SubmitBlock(CBlock cBlock)
{
CLIENT->SubmitBlock(cBlock.hashMerkleRoot, cBlock.nNonce);
}
/** Main Connection Thread. Handles all the networking to allow
Mining threads the most performance. **/
void ServerConnection::ServerThread()
{
/** Don't begin until all mining threads are Created. **/
while(THREADS.size() != nThreads)
Sleep(1);
/** Initialize the Server Connection. **/
CLIENT = new LLP::Miner(IP, PORT);
/** Initialize a Timer for the Hash Meter. **/
METER_TIMER.Start();
HEIGHT_TIMER.Start();
loop
{
try
{
/** Run this thread at 100 Cycles per Second. **/
Sleep(10);
/** Attempt with best efforts to keep the Connection Alive. **/
if(!CLIENT->Connected() || CLIENT->Errors() || CLIENT->Timeout(nTimeout))
{
ResetThreads();
if(!CLIENT->Connect())
continue;
else
{
CLIENT->SetChannel(1);
CLIENT->GetHeight();
}
}
/** Check the Block Height every Second. **/
if(HEIGHT_TIMER.ElapsedMilliseconds() > 1000)
{
HEIGHT_TIMER.Reset();
CLIENT->GetHeight();
}
/** Show the Meter every 15 Seconds. **/
if(METER_TIMER.Elapsed() > 15)
{
unsigned int SecondsElapsed = (unsigned int)time(0) - nStartTimer;
unsigned int nElapsed = METER_TIMER.Elapsed();
double PPS = (double) nPrimes / nElapsed;
double CPS = (double) nChains / nElapsed;
double CSD = (double) (nBlocks * 60.0) / (SecondsElapsed / 60.0);
nPrimes = 0;
nChains = 0;
printf("[METERS] %f PPS | %f CPS | %u Blocks | %f NXS per Hour | Height = %u | Difficulty %f | %02d:%02d:%02d\n", PPS, CPS, nBlocks, CSD, nBestHeight, nDifficulty / 10000000.0, (SecondsElapsed/3600)%60, (SecondsElapsed/60)%60, (SecondsElapsed)%60);
METER_TIMER.Reset();
ResetThreads();
}
/** Check if there is work to do for each Miner Thread. **/
for(int nIndex = 0; nIndex < THREADS.size(); nIndex++)
{
/** Attempt to get a new block from the Server if Thread needs One. **/
if(THREADS[nIndex]->fNewBlock)
{
CLIENT->GetBlock();
THREADS[nIndex]->fBlockWaiting = true;
THREADS[nIndex]->fNewBlock = false;
}
}
CLIENT->ReadPacket();
if(!CLIENT->PacketComplete())
continue;
/** Handle the New Packet, and Interpret its Data. **/
LLP::Packet PACKET = CLIENT->NewPacket();
CLIENT->ResetPacket();
/** Output if a Share is Accepted. **/
if(PACKET.HEADER == CLIENT->GOOD)
{
nBlocks++;
printf("[MASTER] Nexus : Block ACCEPTED\n");
ResetThreads();
}
/** Output if a Share is Rejected. **/
else if(PACKET.HEADER == CLIENT->FAIL)
{
printf("[MASTER] Nexus : Block REJECTED\n");
ResetThreads();
}
/** Reset the Threads if a New Block came in. **/
else if(PACKET.HEADER == CLIENT->BLOCK_HEIGHT)
{
unsigned int nHeight = bytes2uint(PACKET.DATA);
if(nHeight > nBestHeight)
{
nBestHeight = nHeight;
printf("[MASTER] Nexus : New Block %u.\n", nBestHeight);
ResetThreads();
}
}
/** Set the Block for the Thread if there is a New Block Packet. **/
else if(PACKET.HEADER == CLIENT->BLOCK_DATA)
{
/** Search for a Thread waiting for a New Block to Supply its need. **/
for(int nIndex = 0; nIndex < THREADS.size(); nIndex++)
{
if(THREADS[nIndex]->fBlockWaiting)
{
THREADS[nIndex]->MUTEX.lock();
THREADS[nIndex]->cBlock.nVersion = bytes2uint(std::vector<unsigned char>(PACKET.DATA.begin(), PACKET.DATA.begin() + 4));
THREADS[nIndex]->cBlock.hashPrevBlock.SetBytes (std::vector<unsigned char>(PACKET.DATA.begin() + 4, PACKET.DATA.begin() + 132));
THREADS[nIndex]->cBlock.hashMerkleRoot.SetBytes(std::vector<unsigned char>(PACKET.DATA.begin() + 132, PACKET.DATA.end() - 20));
THREADS[nIndex]->cBlock.nChannel = bytes2uint(std::vector<unsigned char>(PACKET.DATA.end() - 20, PACKET.DATA.end() - 16));
THREADS[nIndex]->cBlock.nHeight = bytes2uint(std::vector<unsigned char>(PACKET.DATA.end() - 16, PACKET.DATA.end() - 12));
THREADS[nIndex]->cBlock.nBits = bytes2uint(std::vector<unsigned char>(PACKET.DATA.end() - 12, PACKET.DATA.end() - 8));
THREADS[nIndex]->cBlock.nNonce = bytes2uint64(std::vector<unsigned char>(PACKET.DATA.end() - 8, PACKET.DATA.end()));
THREADS[nIndex]->MUTEX.unlock();
if(THREADS[nIndex]->cBlock.nHeight < nBestHeight)
{
printf("[MASTER] Received Obsolete Block %u... Requesting New Block.\n", THREADS[nIndex]->cBlock.nHeight);
CLIENT->GetBlock();
break;
}
/** Set the Difficulty from most recent Block Received. **/
nDifficulty = THREADS[nIndex]->cBlock.nBits;
printf("[MASTER] Block %s Height = %u Received on Thread %u\n", THREADS[nIndex]->cBlock.hashMerkleRoot.ToString().substr(0, 20).c_str(), THREADS[nIndex]->cBlock.nHeight, nIndex);
THREADS[nIndex]->fBlockWaiting = false;
break;
}
}
}
}
catch(std::exception& e)
{
printf("%s\n", e.what()); CLIENT = new LLP::Miner(IP, PORT);
}
}
}
}
int main(int argc, char *argv[])
{
if(argc < 3)
{
printf("Too Few Arguments. The Required Arguments are 'IP PORT'\n");
printf("Default Arguments are Total Threads = CPU Cores and Connection Timeout = 10 Seconds\n");
printf("Format for Arguments is 'IP PORT THREADS TIMEOUT'\n");
Sleep(10000);
return 0;
}
std::string IP = argv[1];
std::string PORT = argv[2];
int nThreads = GetTotalCores(), nTimeout = 10;
if(argc > 3)
nThreads = boost::lexical_cast<int>(argv[3]);
if(argc > 4)
nTimeout = boost::lexical_cast<int>(argv[4]);
printf("Coinshield Prime Solo Miner 1.0.0 - Created by Videlicet - Optimized by Supercomputing\n");
printf("The Meter Has 2 Values:\nPPS = Primes Per Second\nCPS = Clusters Per Second [Of larger than 3.x Difficulty]\n\n");
Sleep(2000);
printf("Initializing Miner %s:%s Threads = %i Timeout = %i\n", IP.c_str(), PORT.c_str(), nThreads, nTimeout);
Core::InitializePrimes();
nStartTimer = (unsigned int)time(0);
Core::ServerConnection MINERS(IP, PORT, nThreads, nTimeout);
loop { Sleep(10); }
return 0;
}