s-nomp/libs/poolWorker.js

188 lines
7.0 KiB
JavaScript
Raw Normal View History

var Stratum = require('stratum-pool');
2014-03-14 12:02:55 -07:00
var Vardiff = require('stratum-pool/lib/varDiff.js');
var net = require('net');
var MposCompatibility = require('./mposCompatibility.js');
var ShareProcessor = require('./shareProcessor.js');
module.exports = function(logger){
2014-03-14 12:02:55 -07:00
var poolConfigs = JSON.parse(process.env.pools);
var portalConfig = JSON.parse(process.env.portalConfig);
2014-03-14 12:02:55 -07:00
var forkId = process.env.forkId;
var pools = {};
var varDiffsInstances = {}; // contains all the vardiffs for the profit switching pool
2014-03-14 12:02:55 -07:00
var proxyStuff = {}
2014-03-04 12:24:02 -08:00
//Handle messages from master process sent via IPC
process.on('message', function(message) {
2014-03-04 12:24:02 -08:00
switch(message.type){
case 'blocknotify':
var pool = pools[message.coin.toLowerCase()]
if (pool) pool.processBlockNotify(message.hash)
2014-03-04 12:24:02 -08:00
break;
2014-03-14 12:02:55 -07:00
case 'switch':
var newCoinPool = pools[message.coin.toLowerCase()];
if (newCoinPool) {
var oldPool = pools[proxyStuff.curActivePool];
oldPool.relinquishMiners(
function (miner, cback) {
// relinquish miners that are attached to one of the "Auto-switch" ports and leave the others there.
cback(typeof(portalConfig.proxy.ports[miner.client.socket.localPort]) !== 'undefined')
},
function (clients) {
newCoinPool.attachMiners(clients);
proxyStuff.curActivePool = message.coin.toLowerCase();
}
)
}
break;
}
});
2014-03-03 12:51:11 -08:00
Object.keys(poolConfigs).forEach(function(coin) {
2014-03-03 12:51:11 -08:00
var poolOptions = poolConfigs[coin];
2014-03-09 19:31:58 -07:00
var logIdentify = 'Pool Fork ' + forkId + ' (' + coin + ')';
var poolLogger = {
debug: function(key, text){
logger.logDebug(logIdentify, key, text);
},
warning: function(key, text){
logger.logWarning(logIdentify, key, text);
},
error: function(key, text){
logger.logError(logIdentify, key, text);
}
};
var handlers = {
auth: function(){},
share: function(){},
diff: function(){}
};
var shareProcessing = poolOptions.shareProcessing;
//Functions required for MPOS compatibility
if (shareProcessing.mpos && shareProcessing.mpos.enabled){
var mposCompat = new MposCompatibility(poolLogger, poolOptions)
handlers.auth = function(workerName, password, authCallback){
mposCompat.handleAuth(workerName, password, authCallback);
};
handlers.share = function(isValidShare, isValidBlock, data){
mposCompat.handleShare(isValidShare, isValidBlock, data);
};
handlers.diff = function(workerName, diff){
mposCompat.handleDifficultyUpdate(workerName, diff);
2014-03-04 12:24:02 -08:00
}
}
//Functions required for internal payment processing
else if (shareProcessing.internal && shareProcessing.internal.enabled){
var shareProcessor = new ShareProcessor(poolLogger, poolOptions)
handlers.auth = function(workerName, password, authCallback){
pool.daemon.cmd('validateaddress', [workerName], function(results){
var isValid = results.filter(function(r){return r.response.isvalid}).length > 0;
authCallback(isValid);
2014-03-04 12:24:02 -08:00
});
};
handlers.share = function(isValidShare, isValidBlock, data){
shareProcessor.handleShare(isValidShare, isValidBlock, data);
};
}
var authorizeFN = function (ip, workerName, password, callback) {
handlers.auth(workerName, password, function(authorized){
var authString = authorized ? 'Authorized' : 'Unauthorized ';
poolLogger.debug('client', authString + ' [' + ip + '] ' + workerName + ':' + password);
callback({
error: null,
authorized: authorized,
disconnect: false
});
});
};
var pool = Stratum.createPool(poolOptions, authorizeFN);
pool.on('share', function(isValidShare, isValidBlock, data){
var shareData = JSON.stringify(data);
if (data.solution && !isValidBlock)
2014-03-06 15:00:57 -08:00
poolLogger.debug('client', 'We thought a block solution was found but it was rejected by the daemon, share data: ' + shareData);
else if (isValidBlock)
poolLogger.debug('client', 'Block found, solution: ' + data.solution);
if (isValidShare)
2014-03-06 15:00:57 -08:00
poolLogger.debug('client', 'Valid share submitted, share data: ' + shareData);
else if (!isValidShare)
2014-03-06 15:00:57 -08:00
poolLogger.debug('client', 'Invalid share submitted, share data: ' + shareData)
handlers.share(isValidShare, isValidBlock, data)
}).on('difficultyUpdate', function(workerName, diff){
handlers.diff(workerName, diff);
}).on('log', function(severity, logKey, logText) {
if (severity == 'debug') {
poolLogger.debug(logKey, logText);
} else if (severity == 'warning') {
poolLogger.warning(logKey, logText);
} else if (severity == 'error') {
poolLogger.error(logKey, logText);
}
});
pool.start();
pools[poolOptions.coin.name.toLowerCase()] = pool;
});
2014-03-14 12:02:55 -07:00
if (typeof(portalConfig.proxy) !== 'undefined' && portalConfig.proxy.enabled === true) {
proxyStuff.curActivePool = Object.keys(pools)[0];
proxyStuff.proxys = {};
proxyStuff.varDiffs = {};
Object.keys(portalConfig.proxy.ports).forEach(function(port) {
proxyStuff.varDiffs[port] = new Vardiff(port, portalConfig.proxy.ports[port].varDiff);
});
Object.keys(pools).forEach(function (coinName) {
var p = pools[coinName];
Object.keys(proxyStuff.varDiffs).forEach(function(port) {
p.setVarDiff(port, proxyStuff.varDiffs[port]);
});
});
Object.keys(portalConfig.proxy.ports).forEach(function (port) {
proxyStuff.proxys[port] = net
.createServer({allowHalfOpen: true}, function(socket) {
console.log(proxyStuff.curActivePool);
pools[proxyStuff.curActivePool].getStratumServer().handleNewClient(socket);
} )
.listen(parseInt(port), function(){
console.log("Proxy listening on "+port);
});
});
}
};