forked from zone117x/node-open-mining-portal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.js
389 lines (333 loc) · 13.1 KB
/
init.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
383
384
385
386
387
388
389
const fs = require('fs');
const path = require('path');
const os = require('os');
const cluster = require('cluster');
const extend = require('extend');
const PoolLogger = require('./libs/logUtil.js');
const CliListener = require('./libs/cliListener.js');
const PoolWorker = require('./libs/poolWorker.js');
const BlockUnlocker = require('./libs/blockUnlocker.js');
const PaymentProcessor = require('./libs/paymentProcessor.js');
const Website = require('./libs/website.js');
const PoolApi = require('./libs/poolApi.js');
const algos = {
...require('stratum-pool/lib/algoProperties.js'),
...require('kawpow-stratum-pool/lib/algoProperties.js')
};
console.log(algos)
JSON.minify = JSON.minify || require("node-json-minify");
if (!fs.existsSync('config.json')){
console.log('config.json file does not exist. Read the installation/setup instructions.');
return;
}
const portalConfig = JSON.parse(JSON.minify(fs.readFileSync("config.json", {encoding: 'utf8'})));
const defaultValues = {
type: 'pplns',
pplns: 10000,
redis: {
db: 0,
},
poolApi: {
rpcInterval: 5,
graphInterval: 300,
hrWindow: 1800,
largeHrWindow: 10800
},
};
let poolConfigs;
let logger = new PoolLogger({
logLevel: portalConfig.logLevel,
logColors: portalConfig.logColors
});
if (cluster.isWorker){
switch(process.env.workerType){
case 'pool':
new PoolWorker(logger);
break;
case 'blockUnlocker':
new BlockUnlocker(logger);
break;
case 'paymentProcessor':
new PaymentProcessor(logger);
break;
case 'website':
new Website(logger);
break;
case 'poolApi':
new PoolApi(logger);
break;
}
return;
}
// Read all pool configs from pool_configs and join them with their coin profile
const buildPoolConfigs = function(){
const configDir = 'pool_configs/';
let configs = {};
let poolConfigFiles = [];
/* Get filenames of pool config json files that are enabled */
fs.readdirSync(configDir).forEach(function(file){
if (!fs.existsSync(configDir + file) || path.extname(configDir + file) !== '.json') return;
let poolOptions = JSON.parse(JSON.minify(fs.readFileSync(configDir + file, {encoding: 'utf8'})));
if (!poolOptions.enabled) return;
poolOptions.fileName = file;
poolConfigFiles.push(poolOptions);
});
/* Ensure no pool uses any of the same ports as another pool */
for (let i = 0; i < poolConfigFiles.length; i++){
let ports = Object.keys(poolConfigFiles[i].ports);
for (let f = 0; f < poolConfigFiles.length; f++){
if (f === i) continue;
let portsF = Object.keys(poolConfigFiles[f].ports);
for (let g = 0; g < portsF.length; g++){
if (ports.indexOf(portsF[g]) !== -1){
logger.error('Master', poolConfigFiles[f].fileName, 'Has same configured port of ' + portsF[g] + ' as ' + poolConfigFiles[i].fileName);
process.exit(1);
return;
}
}
// if (poolConfigFiles[f].coin === poolConfigFiles[i].coin){
// logger.error('Master', poolConfigFiles[f].fileName, 'Pool has same configured coin file coins/' + poolConfigFiles[f].coin + ' as ' + poolConfigFiles[i].fileName + ' pool');
// process.exit(1);
// return;
// }
}
}
poolConfigFiles.forEach(function(poolOptions){
poolOptions.coinFileName = poolOptions.coin;
let coinFilePath = 'coins/' + poolOptions.coinFileName;
if (!fs.existsSync(coinFilePath)){
logger.error('Master', poolOptions.coinFileName, 'could not find file: ' + coinFilePath);
return;
}
let coinProfile = JSON.parse(JSON.minify(fs.readFileSync(coinFilePath, {encoding: 'utf8'})));
poolOptions.coin = coinProfile;
poolOptions.coin.name = poolOptions.coin.name.toLowerCase();
if (! poolOptions.name) {
poolOptions.name = `${poolOptions.coin.name}-${poolOptions.type}`
}
if (poolOptions.name in configs){
logger.error('Master', poolOptions.fileName, 'coins/' + poolOptions.coinFileName
+ ' has same configured coin name ' + poolOptions.coin.name + ' as coins/'
+ configs[poolOptions.coin.name].coinFileName + ' used by pool config '
+ configs[poolOptions.coin.name].fileName);
process.exit(1);
return;
}
for (const option in portalConfig.defaultPoolConfigs){
if (!(option in poolOptions)) {
let toCloneOption = portalConfig.defaultPoolConfigs[option];
let clonedOption = {};
if (toCloneOption.constructor === Object) {
extend(true, clonedOption, toCloneOption);
} else {
clonedOption = toCloneOption;
}
poolOptions[option] = clonedOption;
}
}
configs[poolOptions.name] = poolOptions;
if (!(coinProfile.algorithm in algos)) {
logger.error('Master', coinProfile.name, 'Cannot run a pool for unsupported algorithm "' + coinProfile.algorithm + '"');
delete configs[poolOptions.coin.name];
}
// set default values
if (!poolOptions.type || !['pplns', 'solo'].includes(poolOptions.type)) {
poolOptions.type = defaultValues.type; // default
logger.error('Master', poolOptions.name, `Pool type is not set, using default: ${poolOptions.type}`);
}
if (poolOptions.type === 'pplns' && !poolOptions.pplns) {
poolOptions.pplns = defaultValues.pplns; // default
logger.error('Master', poolOptions.name, `Pplns is not set, using default: ${defaultValues.pplns}`);
}
if (!poolOptions.redis.baseName) {
poolOptions.redis.baseName = poolOptions.coin.name;
logger.error('Master', poolOptions.name, `Redis baseName is not set, using default: ${poolOptions.coin.name}`);
}
if (!poolOptions.redis.db) {
poolOptions.redis.db = defaultValues.redis.db;
logger.error('Master', poolOptions.name, `Redis db is not set, using default: ${defaultValues.redis.db}`);
}
if (poolOptions.poolApi?.enabled) {
for (const option of Object.keys(defaultValues.poolApi)) {
if (!poolOptions.poolApi[option]) {
poolOptions.poolApi[option] = defaultValues.poolApi[option];
logger.error('Master', poolOptions.name, `PoolAPI ${option} not set, using default: ${defaultValues.poolApi[option]}`);
}
}
}
// set default values end
});
return configs;
};
const spawnPoolWorkers = function(){
Object.keys(poolConfigs).forEach(function(coin){
let p = poolConfigs[coin];
if (!Array.isArray(p.daemons) || p.daemons.length < 1){
logger.error('Master', coin, 'No daemons configured so a pool cannot be started for this coin.');
delete poolConfigs[coin];
}
});
if (Object.keys(poolConfigs).length === 0){
logger.warning('Master', 'PoolSpawner', 'No pool configs exists or are enabled in pool_configs folder. No pools spawned.');
return;
}
let serializedConfigs = JSON.stringify(poolConfigs);
let numForks = (function(){
if (!portalConfig.clustering || !portalConfig.clustering.enabled) return 1;
if (portalConfig.clustering.forks === 'auto') return os.cpus().length;
if (!portalConfig.clustering.forks || isNaN(portalConfig.clustering.forks)) return 1;
return portalConfig.clustering.forks;
})();
let poolWorkers = {};
let createPoolWorker = function(forkId){
let worker = cluster.fork({
workerType: 'pool',
forkId: forkId,
pools: serializedConfigs,
portalConfig: JSON.stringify(portalConfig)
});
worker.forkId = forkId;
worker.type = 'pool';
poolWorkers[forkId] = worker;
worker.on('exit', function(code, signal){
logger.error('Master', 'PoolSpawner', `Fork ${forkId} died (code ${code}, signal ${signal}), spawning replacement...`);
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;
}
});
};
let i = 0;
let spawnInterval = setInterval(function(){
createPoolWorker(i);
i++;
if (i === numForks){
clearInterval(spawnInterval);
logger.debug('Master', 'PoolSpawner', 'Spawned ' + Object.keys(poolConfigs).length + ' pool(s) on ' + numForks + ' thread(s)');
}
}, 250);
};
const startCliListener = function(){
let cliPort = portalConfig.cliPort;
const listener = new CliListener(cliPort);
listener.on('log', function(text){
logger.debug('Master', 'CLI', text);
}).on('command', function(command, params, options, reply){
switch(command){
case 'blocknotify':
Object.keys(cluster.workers).forEach(function(id) {
cluster.workers[id].send({type: 'blocknotify', coin: params[0], hash: params[1]});
});
reply('Pool workers notified');
break;
case 'reloadpool':
Object.keys(cluster.workers).forEach(function(id) {
cluster.workers[id].send({type: 'reloadpool', coin: params[0] });
});
reply('reloaded pool ' + params[0]);
break;
default:
reply('unrecognized command "' + command + '"');
break;
}
}).start();
};
const startBlockUnlocker = function(){
let enabledForAny = false;
for (const pool in poolConfigs){
const p = poolConfigs[pool];
const enabled = p.enabled && p.blockUnlocker && p.blockUnlocker.enabled;
if (enabled) {
enabledForAny = true;
break;
}
}
if (!enabledForAny) return;
const worker = cluster.fork({
workerType: 'blockUnlocker',
pools: JSON.stringify(poolConfigs)
});
worker.on('exit', function(code, signal){
logger.error('Master', 'Block Unlocker', `Block unlocker died (code ${code}, signal ${signal}), spawning replacement...`);
setTimeout(function(){
startBlockUnlocker(poolConfigs);
}, 2000);
});
};
const startPaymentProcessor = function(){
let enabledForAny = false;
for (const pool in poolConfigs){
const p = poolConfigs[pool];
const enabled = p.enabled && p.paymentProcessing?.enabled;
if (enabled){
enabledForAny = true;
break;
}
}
if (!enabledForAny) return;
const worker = cluster.fork({
workerType: 'paymentProcessor',
pools: JSON.stringify(poolConfigs)
});
worker.on('exit', function(code, signal){
logger.error('Master', 'Payment Processor', `Payment processor died (code ${code}, signal ${signal}), spawning replacement...`);
setTimeout(function(){
startBlockUnlocker(poolConfigs);
}, 2000);
});
};
const startWebsite = function(){
if (!portalConfig.website.enabled) return;
const worker = cluster.fork({
workerType: 'website',
pools: JSON.stringify(poolConfigs),
portalConfig: JSON.stringify(portalConfig)
});
worker.on('exit', function(code, signal){
logger.error('Master', 'Website', `Website process died (code ${code}, signal ${signal}), spawning replacement...`);
setTimeout(function(){
startWebsite(portalConfig, poolConfigs);
}, 2000);
});
};
const startPoolApi = function(){
let enabledForAny = false;
for (const pool in poolConfigs){
const p = poolConfigs[pool];
const enabled = p.enabled && p.poolApi?.enabled;
if (enabled){
enabledForAny = true;
break;
}
}
if (!enabledForAny) return;
const worker = cluster.fork({
workerType: 'poolApi',
pools: JSON.stringify(poolConfigs),
portalConfig: JSON.stringify(portalConfig)
});
worker.on('exit', function(code, signal){
logger.error('Master', 'PoolApi', `Pool API process died (code ${code}, signal ${signal}), spawning replacement...`);
setTimeout(function(){
startPoolApi(portalConfig, poolConfigs);
}, 2000);
});
};
(function init(){
poolConfigs = buildPoolConfigs();
spawnPoolWorkers();
startBlockUnlocker();
startPaymentProcessor();
startWebsite();
startPoolApi();
startCliListener();
})();