forked from Nexusoft/PrimeSoloMiner
-
Notifications
You must be signed in to change notification settings - Fork 4
/
core.h
306 lines (238 loc) · 7.67 KB
/
core.h
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
#ifndef NEXUS_LLP_CORE_H
#define NEXUS_LLP_CORE_H
#include "types.h"
#include "bignum.h"
#ifndef _WIN32
#include <gmp.h>
#include <gmpxx.h>
#else
#include <mpir.h>
#include <mpirxx.h>
#endif
namespace Core
{
class CBlock
{
public:
/** Begin of Header. BEGIN(nVersion) **/
unsigned int nVersion;
uint1024 hashPrevBlock;
uint512 hashMerkleRoot;
unsigned int nChannel;
unsigned int nHeight;
unsigned int nBits;
uint64 nNonce;
/** End of Header. END(nNonce).
All the components to build an SK1024 Block Hash. **/
CBlock()
{
nVersion = 0;
hashPrevBlock = 0;
hashMerkleRoot = 0;
nChannel = 0;
nHeight = 0;
nBits = 0;
nNonce = 0;
}
uint1024 GetHash() const
{
return SK1024(BEGIN(nVersion), END(nBits));
}
CBigNum GetPrime() const
{
return CBigNum(GetHash() + nNonce);
}
};
class work_info
{
public:
work_info() {}
work_info(CBigNum basehash, unsigned int difficulty, unsigned int height, unsigned int thr_idx, uint32_t *nonce_begin, uint32_t *nonce_end, mpz_t firstsieveelement, uint512 merkle, unsigned int primelimit) :
BaseHash(basehash), nDifficulty(difficulty), nHeight(height), zFirstSieveElement(firstsieveelement), nNonce(false), nNonceDifficulty(0), merkleRoot(merkle), nPrimes(primelimit)
{ gpu_thread = thr_idx; nonce_offsets.assign(nonce_begin, nonce_end); }
~work_info() {}
// input
unsigned int nHeight;
unsigned int nDifficulty;
CBigNum BaseHash;
uint512 merkleRoot;
mpz_class zFirstSieveElement;
unsigned int nPrimes;
// gpu intermediate result
unsigned int gpu_thread;
std::vector<uint32_t> nonce_offsets;
// result
uint64_t nNonce;
unsigned int nNonceDifficulty;
#if _WIN32
LARGE_INTEGER StartingTime, EndingTime;
#endif
};
void InitializePrimes();
unsigned int SetBits(double nDiff);
double GetPrimeDifficulty(CBigNum prime, int checks);
unsigned int GetPrimeBits(CBigNum prime, int checks);
unsigned int GetFractionalDifficulty(CBigNum composite);
std::vector<unsigned int> Eratosthenes(int nSieveSize);
bool DivisorCheck(CBigNum test);
void PrimeSieve(int threadIndex, CBigNum BaseHash, unsigned int nDifficulty, unsigned int nHeight, uint512 merkeRoot);
bool PrimeQuery();
bool PrimeCheck(CBigNum test, int checks);
CBigNum FermatTest(CBigNum n, CBigNum a);
bool Miller_Rabin(CBigNum n, int checks);
}
namespace LLP
{
class Outbound : public Connection
{
Service_t IO_SERVICE;
std::string IP, PORT;
public:
Packet ReadNextPacket(int nTimeout = 10)
{
Packet NULL_PACKET;
while(!PacketComplete())
{
if(Timeout(nTimeout) || Errors())
return NULL_PACKET;
ReadPacket();
Sleep(1);
}
return this->INCOMING;
}
public:
/** Outgoing Client Connection Constructor **/
Outbound(std::string ip, std::string port) : IP(ip), PORT(port), Connection() { }
bool Connect()
{
try
{
using boost::asio::ip::tcp;
tcp::resolver RESOLVER(IO_SERVICE);
tcp::resolver::query QUERY (tcp::v4(), IP.c_str(), PORT.c_str());
tcp::resolver::iterator ADDRESS = RESOLVER.resolve(QUERY);
this->SOCKET = Socket_t(new tcp::socket(IO_SERVICE));
this->SOCKET -> connect(*ADDRESS, this->ERROR_HANDLE);
if(Errors())
{
this->Disconnect();
printf("Failed to Connect to Mining LLP Server...\n");
return false;
}
this->CONNECTED = true;
this->TIMER.Start();
printf("Connected to %s:%s...\n", IP.c_str(), PORT.c_str());
return true;
}
catch(...){ }
this->CONNECTED = false;
return false;
}
};
class Miner : public Outbound
{
public:
Miner(std::string ip, std::string port) : Outbound(ip, port){}
enum
{
/** DATA PACKETS **/
BLOCK_DATA = 0,
SUBMIT_BLOCK = 1,
BLOCK_HEIGHT = 2,
SET_CHANNEL = 3,
/** REQUEST PACKETS **/
GET_BLOCK = 129,
GET_HEIGHT = 130,
/** RESPONSE PACKETS **/
GOOD = 200,
FAIL = 201,
/** GENERIC **/
PING = 253,
CLOSE = 254
};
inline void SetChannel(unsigned int nChannel)
{
Packet packet;
packet.HEADER = SET_CHANNEL;
packet.LENGTH = 4;
packet.DATA = uint2bytes(nChannel);
this -> WritePacket(packet);
}
inline Core::CBlock* GetBlock(int nTimeout = 30)
{
Packet packet;
packet.HEADER = GET_BLOCK;
this -> WritePacket(packet);
Packet RESPONSE = ReadNextPacket(nTimeout);
if(RESPONSE.IsNull())
return NULL;
Core::CBlock* BLOCK = DeserializeBlock(RESPONSE.DATA);
ResetPacket();
return BLOCK;
}
inline unsigned int GetHeight(int nTimeout = 30)
{
Packet packet;
packet.HEADER = GET_HEIGHT;
this -> WritePacket(packet);
Packet RESPONSE = ReadNextPacket(nTimeout);
if(RESPONSE.IsNull())
return 0;
unsigned int nHeight = bytes2uint(RESPONSE.DATA);
ResetPacket();
return nHeight;
}
inline unsigned char SubmitBlock(uint512 hashMerkleRoot, uint64 nNonce, int nTimeout = 30)
{
Packet PACKET;
PACKET.HEADER = SUBMIT_BLOCK;
PACKET.DATA = hashMerkleRoot.GetBytes();
std::vector<unsigned char> NONCE = uint2bytes64(nNonce);
PACKET.DATA.insert(PACKET.DATA.end(), NONCE.begin(), NONCE.end());
PACKET.LENGTH = 72;
this->WritePacket(PACKET);
Packet RESPONSE = ReadNextPacket(nTimeout);
if(RESPONSE.IsNull())
return 0;
ResetPacket();
return RESPONSE.HEADER;
}
private:
Core::CBlock* DeserializeBlock(std::vector<unsigned char> DATA)
{
Core::CBlock* BLOCK = new Core::CBlock();
BLOCK->nVersion = bytes2uint(std::vector<unsigned char>(DATA.begin(), DATA.begin() + 4));
BLOCK->hashPrevBlock.SetBytes (std::vector<unsigned char>(DATA.begin() + 4, DATA.begin() + 132));
BLOCK->hashMerkleRoot.SetBytes(std::vector<unsigned char>(DATA.begin() + 132, DATA.end() - 20));
BLOCK->nChannel = bytes2uint(std::vector<unsigned char>(DATA.end() - 20, DATA.end() - 16));
BLOCK->nHeight = bytes2uint(std::vector<unsigned char>(DATA.end() - 16, DATA.end() - 12));
BLOCK->nBits = bytes2uint(std::vector<unsigned char>(DATA.end() - 12, DATA.end() - 8));
BLOCK->nNonce = bytes2uint64(std::vector<unsigned char>(DATA.end() - 8, DATA.end()));
return BLOCK;
}
/** Convert a 32 bit Unsigned Integer to Byte Vector using Bitwise Shifts. **/
std::vector<unsigned char> uint2bytes(unsigned int UINT)
{
std::vector<unsigned char> BYTES(4, 0);
BYTES[0] = UINT >> 24;
BYTES[1] = UINT >> 16;
BYTES[2] = UINT >> 8;
BYTES[3] = UINT;
return BYTES;
}
unsigned int bytes2uint(std::vector<unsigned char> BYTES, int nOffset = 0) { return (BYTES[0 + nOffset] << 24) + (BYTES[1 + nOffset] << 16) + (BYTES[2 + nOffset] << 8) + BYTES[3 + nOffset]; }
/** Convert a 64 bit Unsigned Integer to Byte Vector using Bitwise Shifts. **/
std::vector<unsigned char> uint2bytes64(uint64 UINT)
{
std::vector<unsigned char> INTS[2];
INTS[0] = uint2bytes((unsigned int) UINT);
INTS[1] = uint2bytes((unsigned int) (UINT >> 32));
std::vector<unsigned char> BYTES;
BYTES.insert(BYTES.end(), INTS[0].begin(), INTS[0].end());
BYTES.insert(BYTES.end(), INTS[1].begin(), INTS[1].end());
return BYTES;
}
uint64 bytes2uint64(std::vector<unsigned char> BYTES) { return (bytes2uint(BYTES) | ((uint64)bytes2uint(BYTES, 4) << 32)); }
};
}
#endif