mirror of https://github.com/BTCPrivate/z-nomp.git
92 lines
3.5 KiB
JavaScript
92 lines
3.5 KiB
JavaScript
|
var mysql = require('mysql');
|
||
|
|
||
|
module.exports = function(logger, poolConfigs){
|
||
|
|
||
|
var dbConnections = (function(){
|
||
|
var connections = {};
|
||
|
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)
|
||
|
logger.logError('shareProcessor', 'mysql', config.coin.name +
|
||
|
' - could not connect to mysql database: ' + JSON.stringify(err))
|
||
|
else{
|
||
|
logger.logDebug('shareProcessor', 'mysql', config.coin.name +
|
||
|
' - successful connection to MySQL database');
|
||
|
}
|
||
|
});
|
||
|
connection.on('error', function(err){
|
||
|
if(err.code === 'PROTOCOL_CONNECTION_LOST') {
|
||
|
logger.logWarning('shareProcessor', 'mysql', config.coin.name +
|
||
|
' - lost connection to MySQL database, attempting reconnection...');
|
||
|
connect();
|
||
|
}
|
||
|
else{
|
||
|
logger.logError('shareProcessor', 'mysql', config.coin.name +
|
||
|
' - mysql database error: ' + JSON.stringify(err))
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
connect();
|
||
|
});
|
||
|
return connections;
|
||
|
});
|
||
|
|
||
|
|
||
|
this.handleAuth = function(allowCallback){
|
||
|
|
||
|
};
|
||
|
|
||
|
this.handleShare = function(isValidShare, isValidBlock, data){
|
||
|
|
||
|
if ((!data.coin in dbConnections)) return;
|
||
|
|
||
|
var connection = dbConnections[data.coin];
|
||
|
connection.query(
|
||
|
'INSERT INTO `shares` SET time = NOW(), rem_host = ?, username = ?, our_result = ?, upstream_result = ?, difficulty = ?, reason = ?, solution = ?',
|
||
|
[data.ip, data.worker, isValidShare ? 'Y' : 'N', isValidBlock ? 'Y' : 'N', data.difficulty, data.error, data.solution],
|
||
|
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);
|
||
|
}
|
||
|
);
|
||
|
};
|
||
|
|
||
|
|
||
|
};
|