forked from dvandal/cryptonote-nodejs-pool
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinit.js
More file actions
403 lines (355 loc) · 11.8 KB
/
init.js
File metadata and controls
403 lines (355 loc) · 11.8 KB
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
/**
* Cryptonite Node.JS Pool
* https://github.com/dvandal/cryptonote-nodejs-pool
*
* Pool initialization script
**/
// Load needed modules
var fs = require('fs');
var cluster = require('cluster');
var os = require('os');
var v8 = require('v8');
// Load configuration
require('./lib/configReader.js');
// Load log system
require('./lib/logger.js');
// Initialize redis database client
var redis = require('redis');
var redisDB = (config.redis.db && config.redis.db > 0) ? config.redis.db : 0;
// Redis client uses modern API format
var redisOptions = {
socket: {
host: config.redis.host,
port: config.redis.port
},
database: redisDB
};
if (config.redis.auth) {
redisOptions.password = config.redis.auth;
}
global.redisClient = redis.createClient(redisOptions);
// Connect to Redis and handle errors
global.redisClient.on('error', function(err) {
if (typeof log === 'function') {
log('error', 'redis', 'Redis client error: %s', [err]);
} else {
console.error('Redis client error:', err);
}
});
// Connect asynchronously - explicit connection required
global.redisClient.connect().catch(function(err) {
if (typeof log === 'function') {
log('error', 'redis', 'Failed to connect to Redis: %s', [err]);
} else {
console.error('Failed to connect to Redis:', err);
}
});
// Load pool modules
if (cluster.isWorker){
switch(process.env.workerType){
case 'pool':
require('./lib/pool.js');
break;
case 'blockUnlocker':
require('./lib/blockUnlocker.js');
break;
case 'paymentProcessor':
require('./lib/paymentProcessor.js');
break;
case 'api':
require('./lib/api.js');
break;
case 'chartsDataCollector':
require('./lib/chartsDataCollector.js');
break;
case 'telegramBot':
require('./lib/telegramBot.js');
break;
}
return;
}
// Initialize log system
var logSystem = 'master';
require('./lib/exceptionWriter.js')(logSystem);
// Pool informations
log('info', logSystem, 'Starting Cryptonote Node.JS pool version %s', [version]);
/**
* CPU Usage Tracking
**/
var lastCpuMeasure = null;
function getCpuUsage() {
const cpus = os.cpus();
let user = 0, nice = 0, sys = 0, idle = 0, irq = 0;
for (let cpu of cpus) {
user += cpu.times.user;
nice += cpu.times.nice;
sys += cpu.times.sys;
idle += cpu.times.idle;
irq += cpu.times.irq;
}
const current = { user, nice, sys, idle, irq };
if (!lastCpuMeasure) {
lastCpuMeasure = current;
return null;
}
const userDiff = current.user - lastCpuMeasure.user;
const niceDiff = current.nice - lastCpuMeasure.nice;
const sysDiff = current.sys - lastCpuMeasure.sys;
const idleDiff = current.idle - lastCpuMeasure.idle;
const irqDiff = current.irq - lastCpuMeasure.irq;
const total = userDiff + niceDiff + sysDiff + idleDiff + irqDiff;
lastCpuMeasure = current;
if (total === 0) return null;
return {
user: ((userDiff / total) * 100).toFixed(2),
nice: ((niceDiff / total) * 100).toFixed(2),
sys: ((sysDiff / total) * 100).toFixed(2),
idle: ((idleDiff / total) * 100).toFixed(2),
irq: ((irqDiff / total) * 100).toFixed(2),
used: (((total - idleDiff) / total) * 100).toFixed(2)
};
}
function logSystemStats() {
// CPU Usage
const cpuUsage = getCpuUsage();
if (cpuUsage) {
log('info', logSystem, 'CPU Usage: %s%% used | user: %s%%, sys: %s%%, nice: %s%%, irq: %s%%, idle: %s%%',
[cpuUsage.used, cpuUsage.user, cpuUsage.sys, cpuUsage.nice, cpuUsage.irq, cpuUsage.idle]);
}
// Memory Usage
const heapStats = v8.getHeapStatistics();
const totalHeapMB = (heapStats.total_heap_size / 1024 / 1024).toFixed(2);
const usedHeapMB = (heapStats.used_heap_size / 1024 / 1024).toFixed(2);
const heapLimitMB = (heapStats.heap_size_limit / 1024 / 1024).toFixed(2);
const heapUsagePercent = ((heapStats.used_heap_size / heapStats.total_heap_size) * 100).toFixed(2);
const memUsage = process.memoryUsage();
const rssMB = (memUsage.rss / 1024 / 1024).toFixed(2);
const externalMB = (memUsage.external / 1024 / 1024).toFixed(2);
log('info', logSystem, 'Memory: RSS %s MB | Heap: %s MB / %s MB (%s%%) | Limit: %s MB | External: %s MB',
[rssMB, usedHeapMB, totalHeapMB, heapUsagePercent, heapLimitMB, externalMB]);
}
// Start system monitoring (every 10 seconds)
setInterval(logSystemStats, 10000);
// Run a single module ?
var singleModule = (function(){
var validModules = ['pool', 'api', 'unlocker', 'payments', 'chartsDataCollector', 'telegramBot'];
for (var i = 0; i < process.argv.length; i++){
if (process.argv[i].indexOf('-module=') === 0){
var moduleName = process.argv[i].split('=')[1];
if (validModules.indexOf(moduleName) > -1)
return moduleName;
log('error', logSystem, 'Invalid module "%s", valid modules: %s', [moduleName, validModules.join(', ')]);
process.exit();
}
}
})();
/**
* Start modules
**/
(function init(){
checkRedisVersion(function(){
if (singleModule){
log('info', logSystem, 'Running in single module mode: %s', [singleModule]);
switch(singleModule){
case 'pool':
spawnPoolWorkers();
break;
case 'unlocker':
spawnBlockUnlocker();
break;
case 'payments':
spawnPaymentProcessor();
break;
case 'api':
spawnApi();
break;
case 'chartsDataCollector':
spawnChartsDataCollector();
break;
case 'telegramBot':
spawnTelegramBot();
break;
}
}
else{
spawnPoolWorkers();
spawnBlockUnlocker();
spawnPaymentProcessor();
spawnApi();
spawnChartsDataCollector();
spawnTelegramBot();
}
});
})();
/**
* Check redis database version
**/
function checkRedisVersion(callback){
// Requires connection first, then we can use callbacks
function doCheck() {
// info() returns a promise, convert to callback
global.redisClient.info().then(function(response){
var parts = response.split('\r\n');
var version;
var versionString;
for (var i = 0; i < parts.length; i++){
if (parts[i].indexOf(':') !== -1){
var valParts = parts[i].split(':');
if (valParts[0] === 'redis_version'){
versionString = valParts[1];
version = parseFloat(versionString);
break;
}
}
}
if (!version){
log('error', logSystem, 'Could not detect redis version - must be super old or broken');
return;
}
else if (version < 2.6){
log('error', logSystem, "You're using redis version %s the minimum required version is 2.6. Follow the damn usage instructions...", [versionString]);
return;
}
callback();
}).catch(function(error){
log('error', logSystem, 'Redis version check failed: %s', [error]);
});
}
// Ensure client is connected
if (global.redisClient.isOpen) {
doCheck();
} else {
global.redisClient.connect().then(doCheck).catch(function(err) {
log('error', logSystem, 'Failed to connect to Redis for version check: %s', [err]);
});
}
}
/**
* Spawn pool workers module
**/
function spawnPoolWorkers(){
if (!config.poolServer || !config.poolServer.enabled || !config.poolServer.ports || config.poolServer.ports.length === 0) return;
if (config.poolServer.ports.length === 0){
log('error', logSystem, 'Pool server enabled but no ports specified');
return;
}
var numForks = (function(){
if (!config.poolServer.clusterForks)
return 1;
if (config.poolServer.clusterForks === 'auto')
return os.cpus().length;
if (isNaN(config.poolServer.clusterForks))
return 1;
return config.poolServer.clusterForks;
})();
var poolWorkers = {};
var createPoolWorker = function(forkId){
var worker = cluster.fork({
workerType: 'pool',
forkId: forkId
});
worker.forkId = forkId;
worker.type = 'pool';
poolWorkers[forkId] = worker;
worker.on('exit', function(code, signal){
log('error', logSystem, 'Pool fork %s died, spawning replacement worker...', [forkId]);
setTimeout(function(){
createPoolWorker(forkId);
}, 2000);
}).on('message', function(msg){
switch(msg.type){
case 'banIP':
Object.keys(cluster.workers).forEach(function(id) {
if (cluster.workers[id].type === 'pool'){
cluster.workers[id].send({type: 'banIP', ip: msg.ip});
}
});
break;
}
});
};
var i = 1;
var spawnInterval = setInterval(function(){
createPoolWorker(i.toString());
i++;
if (i - 1 === numForks){
clearInterval(spawnInterval);
log('info', logSystem, 'Pool spawned on %d thread(s)', [numForks]);
}
}, 10);
}
/**
* Spawn block unlocker module
**/
function spawnBlockUnlocker(){
if (!config.blockUnlocker || !config.blockUnlocker.enabled) return;
var worker = cluster.fork({
workerType: 'blockUnlocker'
});
worker.on('exit', function(code, signal){
log('error', logSystem, 'Block unlocker died, spawning replacement...');
setTimeout(function(){
spawnBlockUnlocker();
}, 2000);
});
}
/**
* Spawn payment processor module
**/
function spawnPaymentProcessor(){
if (!config.payments || !config.payments.enabled) return;
var worker = cluster.fork({
workerType: 'paymentProcessor'
});
worker.on('exit', function(code, signal){
log('error', logSystem, 'Payment processor died, spawning replacement...');
setTimeout(function(){
spawnPaymentProcessor();
}, 2000);
});
}
/**
* Spawn API module
**/
function spawnApi(){
if (!config.api || !config.api.enabled) return;
var worker = cluster.fork({
workerType: 'api'
});
worker.on('exit', function(code, signal){
log('error', logSystem, 'API died, spawning replacement...');
setTimeout(function(){
spawnApi();
}, 2000);
});
}
/**
* Spawn charts data collector module
**/
function spawnChartsDataCollector(){
if (!config.charts) return;
var worker = cluster.fork({
workerType: 'chartsDataCollector'
});
worker.on('exit', function(code, signal){
log('error', logSystem, 'chartsDataCollector died, spawning replacement...');
setTimeout(function(){
spawnChartsDataCollector();
}, 2000);
});
}
/**
* Spawn telegram bot module
**/
function spawnTelegramBot(){
if (!config.telegram || !config.telegram.enabled || !config.telegram.token) return;
var worker = cluster.fork({
workerType: 'telegramBot'
});
worker.on('exit', function(code, signal){
log('error', logSystem, 'telegramBot died, spawning replacement...');
setTimeout(function(){
spawnTelegramBot();
}, 2000);
});
}