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-05 14:10:50 -08:00
|
|
|
module.exports = function(logger, poolConfig){
|
2014-03-04 00:33:55 -08:00
|
|
|
|
2014-03-05 14:10:50 -08:00
|
|
|
var mposConfig = poolConfig.shareProcessing.mpos;
|
|
|
|
var coin = poolConfig.coin.name;
|
2014-03-04 12:24:02 -08:00
|
|
|
|
2014-03-05 14:10:50 -08:00
|
|
|
var connection;
|
2014-03-04 12:24:02 -08:00
|
|
|
|
2014-03-29 18:59:22 -07:00
|
|
|
var logIdentify = 'MySQL';
|
|
|
|
var logComponent = coin;
|
2014-03-22 23:16:06 -07:00
|
|
|
|
2014-03-05 14:10:50 -08:00
|
|
|
function connect(){
|
|
|
|
connection = 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-29 18:59:22 -07:00
|
|
|
logger.error(logIdentify, logComponent, 'Could not connect to mysql database: ' + JSON.stringify(err))
|
2014-03-05 14:10:50 -08:00
|
|
|
else{
|
2014-03-29 18:59:22 -07:00
|
|
|
logger.debug(logIdentify, logComponent, 'Successful connection to MySQL database');
|
2014-03-04 00:33:55 -08:00
|
|
|
}
|
|
|
|
});
|
2014-03-05 14:10:50 -08:00
|
|
|
connection.on('error', function(err){
|
|
|
|
if(err.code === 'PROTOCOL_CONNECTION_LOST') {
|
2014-03-29 18:59:22 -07:00
|
|
|
logger.warning(logIdentify, logComponent, 'Lost connection to MySQL database, attempting reconnection...');
|
2014-03-05 14:10:50 -08:00
|
|
|
connect();
|
|
|
|
}
|
|
|
|
else{
|
2014-03-29 18:59:22 -07:00
|
|
|
logger.error(logIdentify, logComponent, 'Database error: ' + JSON.stringify(err))
|
2014-03-05 14:10:50 -08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
connect();
|
2014-03-04 00:33:55 -08:00
|
|
|
|
2014-03-05 14:10:50 -08:00
|
|
|
this.handleAuth = function(workerName, password, authCallback){
|
2014-03-04 00:33:55 -08:00
|
|
|
|
2014-03-04 12:24:02 -08:00
|
|
|
connection.query(
|
|
|
|
'SELECT password FROM pool_worker WHERE username = LOWER(?)',
|
2014-03-05 14:10:50 -08:00
|
|
|
[workerName],
|
2014-03-04 12:24:02 -08:00
|
|
|
function(err, result){
|
|
|
|
if (err){
|
2014-03-29 18:59:22 -07:00
|
|
|
logger.error(logIdentify, logComponent, 'Database error when authenticating worker: ' +
|
2014-03-04 12:24:02 -08:00
|
|
|
JSON.stringify(err));
|
2014-03-05 14:10:50 -08:00
|
|
|
authCallback(false);
|
2014-03-04 12:24:02 -08:00
|
|
|
}
|
|
|
|
else if (!result[0])
|
2014-03-05 14:10:50 -08:00
|
|
|
authCallback(false);
|
2014-03-05 15:05:31 -08:00
|
|
|
else if (mposConfig.stratumAuth === 'worker')
|
2014-03-05 14:10:50 -08:00
|
|
|
authCallback(true);
|
|
|
|
else if (result[0].password === password)
|
|
|
|
authCallback(true)
|
2014-03-04 12:24:02 -08:00
|
|
|
else
|
2014-03-05 14:10:50 -08:00
|
|
|
authCallback(false);
|
2014-03-04 12:24:02 -08:00
|
|
|
}
|
|
|
|
);
|
2014-03-04 00:33:55 -08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-03-05 14:10:50 -08:00
|
|
|
this.handleShare = function(isValidShare, isValidBlock, shareData){
|
2014-03-04 00:33:55 -08:00
|
|
|
|
2014-03-04 13:58:00 -08:00
|
|
|
var dbData = [
|
2014-03-05 14:10:50 -08:00
|
|
|
shareData.ip,
|
|
|
|
shareData.worker,
|
2014-04-12 21:29:57 -07:00
|
|
|
isValidShare ? 'Y' : 'N',
|
2014-03-05 14:10:50 -08:00
|
|
|
isValidBlock ? 'Y' : 'N',
|
2014-04-12 21:29:57 -07:00
|
|
|
poolConfig.coin.algorithm === 'x11' ? shareData.difficulty * 256 : shareData.difficulty,
|
2014-03-05 14:10:50 -08:00
|
|
|
typeof(shareData.error) === 'undefined' ? null : shareData.error,
|
2014-04-02 12:01:05 -07:00
|
|
|
shareData.blockHash ? shareData.blockHash : (shareData.blockHashInvalid ? shareData.blockHashInvalid : '')
|
2014-03-04 13:58:00 -08:00
|
|
|
];
|
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)
|
2014-03-29 18:59:22 -07:00
|
|
|
logger.error(logIdentify, logComponent, 'Insert error when adding share: ' + JSON.stringify(err));
|
2014-03-22 23:16:06 -07:00
|
|
|
else
|
2014-03-29 18:59:22 -07:00
|
|
|
logger.debug(logIdentify, logComponent, 'Share inserted');
|
2014-03-04 00:33:55 -08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.handleDifficultyUpdate = function(workerName, diff){
|
|
|
|
|
|
|
|
connection.query(
|
|
|
|
'UPDATE `pool_worker` SET `difficulty` = ' + diff + ' WHERE `username` = ' + connection.escape(workerName),
|
|
|
|
function(err, result){
|
|
|
|
if (err)
|
2014-03-29 18:59:22 -07:00
|
|
|
logger.error(logIdentify, logComponent, 'Error when updating worker diff: ' +
|
2014-03-04 00:33:55 -08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
};
|