2014-03-04 00:33:55 -08:00
|
|
|
var mysql = require('mysql');
|
2014-03-04 13:58:00 -08:00
|
|
|
var cluster = require('cluster');
|
2014-03-04 00:33:55 -08:00
|
|
|
module.exports = function(logger, poolConfigs){
|
|
|
|
|
|
|
|
var dbConnections = (function(){
|
|
|
|
var connections = {};
|
2014-03-04 12:24:02 -08:00
|
|
|
|
|
|
|
|
2014-03-04 00:33:55 -08:00
|
|
|
Object.keys(poolConfigs).forEach(function(coin) {
|
|
|
|
|
|
|
|
var config = poolConfigs[coin];
|
|
|
|
|
|
|
|
if (!config.shareProcessing || !config.shareProcessing.mpos || !config.shareProcessing.mpos.enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var mposConfig = config.shareProcessing.mpos;
|
|
|
|
|
|
|
|
function connect(){
|
|
|
|
var connection = connections[coin] = mysql.createConnection({
|
|
|
|
host: mposConfig.host,
|
|
|
|
port: mposConfig.port,
|
|
|
|
user: mposConfig.user,
|
|
|
|
password: mposConfig.password,
|
|
|
|
database: mposConfig.database
|
|
|
|
});
|
|
|
|
connection.connect(function(err){
|
|
|
|
if (err)
|
2014-03-04 12:24:02 -08:00
|
|
|
logger.logError('shareProcessor', 'mysql', coin +
|
2014-03-04 00:33:55 -08:00
|
|
|
' - could not connect to mysql database: ' + JSON.stringify(err))
|
|
|
|
else{
|
2014-03-04 12:24:02 -08:00
|
|
|
logger.logDebug('shareProcessor', 'mysql', coin +
|
2014-03-04 00:33:55 -08:00
|
|
|
' - successful connection to MySQL database');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
connection.on('error', function(err){
|
|
|
|
if(err.code === 'PROTOCOL_CONNECTION_LOST') {
|
2014-03-04 12:24:02 -08:00
|
|
|
logger.logWarning('shareProcessor', 'mysql', coin +
|
2014-03-04 00:33:55 -08:00
|
|
|
' - lost connection to MySQL database, attempting reconnection...');
|
|
|
|
connect();
|
|
|
|
}
|
|
|
|
else{
|
2014-03-04 12:24:02 -08:00
|
|
|
logger.logError('shareProcessor', 'mysql', coin +
|
2014-03-04 00:33:55 -08:00
|
|
|
' - mysql database error: ' + JSON.stringify(err))
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
connect();
|
|
|
|
});
|
|
|
|
return connections;
|
2014-03-04 12:24:02 -08:00
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
|
|
this.handleAuth = function(data){
|
|
|
|
/*
|
|
|
|
type: 'mposAuth',
|
|
|
|
coin: poolOptions.coin.name,
|
|
|
|
callbackId: callbackId,
|
|
|
|
workerId: cluster.worker.id,
|
|
|
|
workerName: workerName,
|
|
|
|
password: password,
|
|
|
|
authLevel: authLevel
|
|
|
|
*/
|
2014-03-04 00:33:55 -08:00
|
|
|
|
2014-03-04 12:24:02 -08:00
|
|
|
var sendResult = function(authorized){
|
|
|
|
cluster.workers[data.workerId].send({
|
2014-03-04 14:35:43 -08:00
|
|
|
type : 'mposAuth',
|
|
|
|
callbackId : data.callbackId,
|
|
|
|
authorized : authorized
|
2014-03-04 12:24:02 -08:00
|
|
|
});
|
|
|
|
};
|
2014-03-04 00:33:55 -08:00
|
|
|
|
2014-03-04 12:24:02 -08:00
|
|
|
var connection = dbConnections[data.coin];
|
|
|
|
connection.query(
|
|
|
|
'SELECT password FROM pool_worker WHERE username = LOWER(?)',
|
|
|
|
[data.workerName],
|
|
|
|
function(err, result){
|
|
|
|
if (err){
|
|
|
|
logger.logError('shareProcessor', 'mysql', 'MySQL error when authenticating worker: ' +
|
|
|
|
JSON.stringify(err));
|
|
|
|
sendResult(false);
|
|
|
|
}
|
|
|
|
else if (!result[0])
|
|
|
|
sendResult(false);
|
|
|
|
else if (data.authLevel === 'worker')
|
|
|
|
sendResult(true);
|
|
|
|
else if (result[0].password === data.password)
|
|
|
|
sendResult(true)
|
|
|
|
else
|
|
|
|
sendResult(false);
|
|
|
|
}
|
|
|
|
);
|
2014-03-04 00:33:55 -08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-03-04 13:58:00 -08:00
|
|
|
this.handleShare = function(data){
|
|
|
|
var isValidShare = data.isValidShare;
|
|
|
|
var isValidBlock = data.isValidBlock;
|
2014-03-04 00:33:55 -08:00
|
|
|
if ((!data.coin in dbConnections)) return;
|
|
|
|
|
|
|
|
var connection = dbConnections[data.coin];
|
2014-03-04 13:58:00 -08:00
|
|
|
var dbData = [
|
|
|
|
data.share.ip,
|
|
|
|
data.share.worker,
|
|
|
|
isValidShare ? 'Y' : 'N',
|
|
|
|
isValidBlock ? 'Y' : 'N',
|
|
|
|
data.share.difficulty,
|
|
|
|
typeof(data.share.error)==='undefined'?null:data.share.error,
|
|
|
|
typeof(data.solution)==='undefined'?'':data.solution // solution?
|
|
|
|
];
|
2014-03-04 00:33:55 -08:00
|
|
|
connection.query(
|
|
|
|
'INSERT INTO `shares` SET time = NOW(), rem_host = ?, username = ?, our_result = ?, upstream_result = ?, difficulty = ?, reason = ?, solution = ?',
|
2014-03-04 13:58:00 -08:00
|
|
|
dbData,
|
2014-03-04 00:33:55 -08:00
|
|
|
function(err, result) {
|
|
|
|
if (err)
|
|
|
|
logger.logError('shareProcessor', 'mysql', 'MySQL insert error when adding share: ' +
|
|
|
|
JSON.stringify(err));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.handleDifficultyUpdate = function(workerName, diff){
|
|
|
|
|
|
|
|
if ((!data.coin in dbConnections)) return;
|
|
|
|
|
|
|
|
var connection = dbConnections[data.coin];
|
|
|
|
connection.query(
|
|
|
|
'UPDATE `pool_worker` SET `difficulty` = ' + diff + ' WHERE `username` = ' + connection.escape(workerName),
|
|
|
|
function(err, result){
|
|
|
|
if (err)
|
|
|
|
logger.logError('shareProcessor', 'mysql', 'MySQL error when updating worker diff: ' +
|
|
|
|
JSON.stringify(err));
|
|
|
|
else if (result.affectedRows === 0){
|
|
|
|
connection.query('INSERT INTO `pool_worker` SET ?', {username: workerName, difficulty: diff});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
console.log('Updated difficulty successfully', result);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
};
|