This commit is contained in:
Matt 2014-03-05 12:54:26 -07:00
commit 63c131762b
4 changed files with 115 additions and 28 deletions

18
libs/apis.js Normal file
View File

@ -0,0 +1,18 @@
var express = require('express');
var os = require('os');
var app = express();
app.get('/getstatus', function (req, res) {
res.send({
'loadavg': os.loadavg(),
'freemem': os.freemem(),
});
});
module.exports = {
start: function () {
app.listen(9000);
}
}

View File

@ -1,5 +1,5 @@
var mysql = require('mysql'); var mysql = require('mysql');
var cluster = require('cluster');
module.exports = function(logger, poolConfigs){ module.exports = function(logger, poolConfigs){
var dbConnections = (function(){ var dbConnections = (function(){
@ -63,9 +63,9 @@ module.exports = function(logger, poolConfigs){
var sendResult = function(authorized){ var sendResult = function(authorized){
cluster.workers[data.workerId].send({ cluster.workers[data.workerId].send({
type: 'mposAuth', type : 'mposAuth',
callbackId: data.callbackId, callbackId : data.callbackId,
authorized: authorized authorized : authorized
}); });
}; };
@ -92,14 +92,24 @@ module.exports = function(logger, poolConfigs){
}; };
this.handleShare = function(isValidShare, isValidBlock, data){ this.handleShare = function(data){
var isValidShare = data.isValidShare;
var isValidBlock = data.isValidBlock;
if ((!data.coin in dbConnections)) return; if ((!data.coin in dbConnections)) return;
var connection = dbConnections[data.coin]; var connection = dbConnections[data.coin];
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?
];
connection.query( connection.query(
'INSERT INTO `shares` SET time = NOW(), rem_host = ?, username = ?, our_result = ?, upstream_result = ?, difficulty = ?, reason = ?, solution = ?', '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], dbData,
function(err, result) { function(err, result) {
if (err) if (err)
logger.logError('shareProcessor', 'mysql', 'MySQL insert error when adding share: ' + logger.logError('shareProcessor', 'mysql', 'MySQL insert error when adding share: ' +

View File

@ -28,8 +28,9 @@ module.exports = function(logger){
break; break;
case 'mposAuth': case 'mposAuth':
var callbackId = message.callbackId; var callbackId = message.callbackId;
if (callbackId in mposAuthCallbacks) if (callbackId in mposAuthCallbacks) {
mposAuthCallbacks[callbackId](message.authorized); mposAuthCallbacks[callbackId](message.authorized);
}
break; break;
} }
}); });
@ -73,16 +74,17 @@ module.exports = function(logger){
clearTimeout(authTimeout); clearTimeout(authTimeout);
}; };
process.send({ process.send({
type: 'mposAuth', type : 'mposAuth',
coin: poolOptions.coin.name, coin : poolOptions.coin.name,
callbackId: callbackId, callbackId : callbackId,
workerId: cluster.worker.id, workerId : cluster.worker.id,
workerName: workerName, workerName : workerName,
password: password, password : password,
authLevel: mposAuthLevel authLevel : mposAuthLevel
}); });
} }
else{ else{
// if we're here than it means we're on stratumAuth: "none" or something unrecognized by the system!
callback({ callback({
error: null, error: null,
authorized: true, authorized: true,
@ -93,7 +95,7 @@ module.exports = function(logger){
var pool = Stratum.createPool(poolOptions, authorizeFN); var pool = Stratum.createPool(poolOptions, authorizeFN);
pool.on('share', function(isValidShare, isValidBlock, data){ pool.on('share', function(isValidShare, isValidBlock, data, blockHex){
var shareData = JSON.stringify(data); var shareData = JSON.stringify(data);
@ -106,29 +108,30 @@ module.exports = function(logger){
logDebug(logIdentify, 'client', 'Valid share submitted, share data: ' + shareData); logDebug(logIdentify, 'client', 'Valid share submitted, share data: ' + shareData);
process.send({ process.send({
type: 'share', type : 'share',
share: shareData, share : data,
coin: poolOptions.coin.name, coin : poolOptions.coin.name,
isValidShare: isValidShare, isValidShare : isValidShare,
isValidBlock: isValidBlock isValidBlock : isValidBlock,
solution : blockHex // blockHex is undefined is this was not a valid block.
}); });
if (isValidBlock){ if (isValidBlock){
logDebug(logIdentify, 'client', 'Block found, solution: ' + shareData.solution); logDebug(logIdentify, 'client', 'Block found, solution: ' + shareData.solution);
process.send({ process.send({
type: 'block', type : 'block',
share: shareData, share : data,
coin: poolOptions.coin.name coin : poolOptions.coin.name
}); });
} }
}).on('difficultyUpdate', function(workerName, diff){ }).on('difficultyUpdate', function(workerName, diff){
if (poolOptions.shareProcessing.mpos.enabled){ if (poolOptions.shareProcessing.mpos.enabled){
process.send({ process.send({
type: 'difficultyUpdate', type : 'difficultyUpdate',
workerName: workerName, workerName : workerName,
diff: diff, diff : diff,
coin: poolOptions.coin.name coin : poolOptions.coin.name
}); });
} }
}).on('log', function(severity, logKey, logText) { }).on('log', function(severity, logKey, logText) {

56
libs/workerapi.js Normal file
View File

@ -0,0 +1,56 @@
var express = require('express');
var os = require('os');
function workerapi(listen) {
var _this = this;
var app = express();
var counters = {
validShares : 0,
validBlocks : 0,
invalidShares : 0
};
var lastEvents = {
lastValidShare : 0 ,
lastValidBlock : 0,
lastInvalidShare : 0
};
app.get('/stats', function (req, res) {
res.send({
"clients" : Object.keys(_this.poolObj.stratumServer.getStratumClients()).length,
"counters" : counters,
"lastEvents" : lastEvents
});
});
this.start = function (poolObj) {
this.poolObj = poolObj;
this.poolObj.once('started', function () {
app.listen(listen, function (lol) {
console.log("LISTENING ");
});
})
.on('share', function(isValidShare, isValidBlock, shareData) {
var now = Date.now();
if (isValidShare) {
counters.validShares ++;
lastEvents.lastValidShare = now;
if (isValidBlock) {
counters.validBlocks ++;
lastEvents.lastValidBlock = now;
}
} else {
counters.invalidShares ++;
lastEvents.lastInvalidShare = now;
}
});
}
}
module.exports = workerapi;