-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathMiner.js
382 lines (339 loc) · 12.8 KB
/
Miner.js
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
const util = require('util');
const os = require('os');
const atob = require('atob');
const WebSocketClient = require('websocket').client;
const Nimiq = require('@nimiq/core');
const Log = Nimiq.Log;
const BigNumber = Nimiq.BigNumber;
let NodeNative;
const P = require('./Protocol.js');
const setTimeoutPromise = util.promisify(setTimeout);
const RANGE = 4096;
const WORKLOADS_PER_THREAD_PC = 75; // 1 thread is 75 * 4096 nonces
const DIFFICULT_PER_THREAD_PC = 2; // 1 thread is 2 difficulty, one share equal to 2*thread*Math.pow(2 ,16) Hashes, 2*thread*65536
class Miner {
constructor(server, address, name, threads, percent, event, cpu) {
/** miner metadata */
this._version = 8;
this._platform = [os.platform(), os.arch(), os.release()].join(' ');
this._address = address;
this._name = name;
this._threads = threads;
this._percent = percent;
this._percentX = (100 - this._percent) / this._percent;
/** miner state data */
this._mining = false;
this._pulling = false;
this._activeThreads = 0;
// hashrate computing
this._prevTime = new Date();
this._prevHashrate = 0; // previous 10s
this._currTime = new Date();
this._currHashrate = 0; // current 10s
this._hashrateValue = 0.0;
this._timer = setInterval(() => { this._hashrate(); }, 10000);
/** current mining block */
this._blockHeaderBase64 = null;
this._compact = Nimiq.BlockUtils.difficultyToCompact(new BigNumber(threads * DIFFICULT_PER_THREAD_PC));
this._workrange4096 = threads * WORKLOADS_PER_THREAD_PC;
this._workrange4096pullThreshold = Math.round(this._workrange4096 / 2);
this._timeNonce36 = '0';
/*
[
{
header,
timeNonce36,
index,
workrange4096queue: []
}
]
*/
this._taskQueue = [];
this._event = event;
this._wsConnect(server);
let pathNodeNative = './node_modules/@nimiq/core/build/Release/nimiq_node.node';
if (global.skypool_package) {
pathNodeNative = __dirname + '/lib/nimiq_node_' + cpu + '.node';
}
NodeNative = require(pathNodeNative);
}
delete() {
if (this._ws) {
this._ws.close();
}
clearInterval(this._timer);
this._mining = false;
}
_wsConnect(server) {
this._wsClient = new WebSocketClient();
this._wsClient.on('connect', connection => {
this._ws = connection;
Log.i(Miner, `connect pool success ${server}`);
this._register();
this._ws.on('message', (data) => {
if (data.type !== 'utf8') {
console.log('ws message type error: ' + JSON.stringify(data));
return;
}
const msg = JSON.parse(data.utf8Data);
switch(msg.t) {
case P.RegisterBack: {
this._onRegisterBack(msg.d);
break;
}
case P.WorkRange: {
this._onWorkRange(msg.d);
break;
}
case P.AssignJob: {
this._onAssignJob(msg.d);
break;
}
case P.PullBack: {
this._onPullBack(msg.d);
break;
}
case P.CloseBanIP: {
Log.w(Miner, 'miner IP has been banned');
break;
}
case P.CloseFirewall: {
Log.w(Miner, 'pool firewall close this miner');
break;
}
case P.Exit: {
process.exit(0);
break;
}
}
});
this._ws.on('error', e => {
Log.w(Miner, `connect pool error ${server}`);
console.log(e);
});
this._ws.on('close', () => {
Log.w(Miner, `closed pool connection`);
this._mining = false;
});
});
this._wsClient.connect(server, null);
}
_onRegisterBack(data) {
if (data === P.RegisterBack_Ok) {
Log.i(Miner, 'register to server ok');
}
else if (data === P.RegisterBack_ParaFail) {
Log.e(Miner, 'register to server fail because of parameters');
this._event.emit('parameter_fail');
}
else if (data === P.RegisterBack_ServerUnable) {
Log.w(Miner, 'server unable, try register in 30s');
setTimeout(() => {
this._register();
}, 30000);
}
else if (data === P.RegisterBack_Full) {
Log.w(Miner, 'server full, try register in 30s');
setTimeout(() => {
this._register();
}, 30000);
}
else if (data === P.RegisterBack_VersionOld) {
Log.e(Miner, 'client version out of date, please download the latest mining client');
this._event.emit('client_old');
}
}
_onWorkRange(data) {
Log.i(Miner, `receive workRange ${data[P.WorkRange_256]} Difficult ${data[P.WorkRange_Difficult]}`)
this._workrange4096 = data[P.WorkRange_256] / 16;
this._workrange4096pullThreshold = Math.round(this._workrange4096 / 2);
this._compact = Nimiq.BlockUtils.difficultyToCompact(new BigNumber(data[P.WorkRange_Difficult]));
}
_onAssignJob(data) {
/*
[
{
header,
timeNonce36,
index,
workrange4096queue: []
}
]
*/
this._timeNonce36 = data[P.AssignJob_TimeNonce36];
this._taskQueue = [];
const task = {};
task.header = Uint8Array.from(atob(data[P.AssignJob_BlockHeaderBase64]), c => c.charCodeAt(0));
task.timeNonce36 = data[P.AssignJob_TimeNonce36];
task.index = data[P.AssignJob_Index];
task.workrange4096queue = [];
let startNonce = parseInt(data[P.AssignJob_CurrentNonce36], 36) / 16;
for (let i = 0; i < this._workrange4096; i++) {
task.workrange4096queue.push(startNonce + i);
}
this._taskQueue.push(task);
this._pulling = false;
Log.i(Miner, `on assignJob, index ${task.index}, timeNonce36 ${task.timeNonce36}, startNonce ${startNonce}`);
if (this._activeThreads < this._threads) {
this._mining = true;
this._startMining(this._threads - this._activeThreads);
}
}
_onPullBack(data) {
this._timeNonce36 = data[P.PullBack_TimeNonce36];
const task = {};
task.header = Uint8Array.from(atob(data[P.AssignJob_BlockHeaderBase64]), c => c.charCodeAt(0));
task.timeNonce36 = data[P.PullBack_TimeNonce36];
task.index = data[P.PullBack_Index];
task.workrange4096queue = [];
let startNonce = parseInt(data[P.PullBack_CurrentNonce36], 36) / 16;
for (let i = 0; i < this._workrange4096; i++) {
task.workrange4096queue.push(startNonce + i);
}
this._taskQueue.push(task);
this._pulling = false;
Log.i(Miner, `on pullBack, index ${task.index}, timeNonce36 ${task.timeNonce36}, startNonce ${startNonce}`);
if (this._activeThreads < this._threads) {
this._mining = true;
this._startMining(this._threads - this._activeThreads);
}
}
_register() {
const data = {
t: P.Register,
d: {
[P.Register_Address]: this._address,
[P.Register_Name]: this._name,
[P.Register_Version]: this._version,
[P.Register_Platform]: this._platform,
[P.Register_Threads]: this._threads,
},
};
Log.i(Miner, 'send register');
this._ws.sendUTF(JSON.stringify(data));
}
_pull() {
if (this._pulling) {
return;
}
this._pulling = true;
this._ws.sendUTF(JSON.stringify({
t: P.Pull,
}));
}
_push(timeNonce36, nonce, index) {
// push
this._ws.sendUTF(JSON.stringify({
t: P.Push,
d: {
[P.Push_TimeNonce36]: timeNonce36,
[P.Push_Nonce]: nonce,
[P.Push_Index]: index,
}
}));
}
_startMining(threads) {
for (let i = 0; i < threads; i++) {
this._singleMiner().catch((e) => Log.e(Miner, e));
}
}
// each singleMiner is an individual thread
async _singleMiner() {
const threadNo = this._activeThreads;
if (this._mining && this._activeThreads < this._threads) {
this._activeThreads++;
} else {
if (!this._mining) {
Log.e(Miner, `start singleMiner ${threadNo} fail, mining state is false`);
} else {
Log.w(Miner, `start singleMiner ${threadNo} fail, active threads are full`);
}
return;
}
Log.i(Miner, `singleMiner ${threadNo} start`);
let percentTime; // one worker start mining time, for percent config
let percentDuration; // one worker mining duration, for percent config
for (;;) {
if (!this._mining) {
Log.e(Miner, `singleMiner ${threadNo} exit, mining state is false`);
this._activeThreads--;
return;
}
if (this._taskQueue.length === 0) {
this._pull();
Log.w(Miner, `thread ${threadNo} waiting for pulling workloads`);
await setTimeoutPromise(1000);
continue;
}
const taskObject = this._taskQueue[0];
const startNonce4096 = taskObject.workrange4096queue.shift();
// if this task run out
if (startNonce4096 === undefined) {
this._taskQueue.shift();
continue;
}
// if this task run to threshold, pull next task
if (taskObject.workrange4096queue.length === this._workrange4096pullThreshold) {
this._pull();
}
const startNonce = startNonce4096 * RANGE;
const endNonce = startNonce + RANGE;
percentTime = new Date();
const result = await this._multiMine(new Nimiq.SerialBuffer(taskObject.header), this._compact, startNonce, endNonce);
percentDuration = new Date() - percentTime;
if (result && taskObject.timeNonce36 === this._timeNonce36) {
Log.i(Miner, `mined a share, timeNonce36 ${taskObject.timeNonce36}, nonce ${result.nonce}`);
this._push(
taskObject.timeNonce36,
result.nonce,
taskObject.index,
);
}
this._currHashrate += RANGE;
if (this._percentX > 0) {
await setTimeoutPromise(percentDuration * this._percentX);
}
}
}
_multiMine(blockHeader, compact, minNonce, maxNonce) {
if (global.skypool_package) {
return new Promise((resolve, fail) => {
NodeNative.node_argon2d_target_async(async (nonce) => {
try {
if (nonce === maxNonce) {
resolve(false);
} else {
resolve({nonce});
}
} catch (e) {
fail(e);
}
}, blockHeader, compact, minNonce, maxNonce - minNonce, 512);
});
} else {
return new Promise((resolve, fail) => {
NodeNative.node_argon2_target_async(async (nonce) => {
try {
if (nonce === maxNonce) {
resolve(false);
} else {
resolve({nonce});
}
} catch (e) {
fail(e);
}
}, blockHeader, compact, minNonce, maxNonce, 512);
});
}
}
_hashrate() {
const nowTime = new Date();
this._hashrateValue = (this._prevHashrate + this._currHashrate) / (nowTime - this._prevTime);
Log.i(Miner, `hashrate ${this._hashrateValue.toFixed(2)} kH/s`);
this._prevTime = this._currTime;
this._prevHashrate = this._currHashrate;
this._currTime = new Date();
this._currHashrate = 0;
}
}
module.exports = Miner;