From bcad84af80f8b34ab30dd473bb5ab24c2c58996f Mon Sep 17 00:00:00 2001 From: Andrea Date: Fri, 14 Mar 2014 20:52:26 +0000 Subject: [PATCH] Fixed merging shit lol --- lib/daemon.js | 137 +++++++++++++++++++++++++++++++--------------- lib/jobManager.js | 15 ++--- lib/stratum.js | 20 +++++-- 3 files changed, 116 insertions(+), 56 deletions(-) diff --git a/lib/daemon.js b/lib/daemon.js index a287eb7..6b145ea 100644 --- a/lib/daemon.js +++ b/lib/daemon.js @@ -45,6 +45,92 @@ function DaemonInterface(options){ } + function performHttpRequest(instance, jsonData, callback){ + var options = { + hostname: (typeof(instance.host) === 'undefined' ? 'localhost' : instance.host), + port : instance.port, + method : 'POST', + auth : instance.user + ':' + instance.password, + headers : { + 'Content-Length': jsonData.length + } + }; + + var req = http.request(options, function(res) { + var data = ''; + res.setEncoding('utf8'); + res.on('data', function (chunk) { + data += chunk; + + }); + res.on('end', function(){ + + var dataJson; + var parsingError; + try{ + dataJson = JSON.parse(data); + + } + catch(e){ + if (res.statusCode === 401){ + parsingError = 'unauthorized'; + _this.emit('error', 'Invalid RPC username or password'); + } + else{ + parsingError = e; + _this.emit('error', 'could not parse rpc data with request of: ' + jsonData + + ' on instance ' + instance.index + ' data: ' + data); + } + } + if (typeof(dataJson) !== 'undefined'){ + callback(dataJson.error, dataJson); + } + else + callback(parsingError); + + }); + }); + + req.on('error', function(e) { + if (e.code === 'ECONNREFUSED') + callback({type: 'offline', message: e.message}, null); + else + callback({type: 'request error', message: e.message}, null); + }); + + req.end(jsonData); + }; + + + + //Performs a batch JSON-RPC command - only uses the first configured rpc daemon + /* First argument must have: + [ + [ methodName, [params] ], + [ methodName, [params] ] + ] + */ + + function batchCmd(cmdArray, callback){ + + var requestJson = []; + + for (var i = 0; i < cmdArray.length; i++){ + requestJson.push({ + method: cmdArray[i][0], + params: cmdArray[i][1], + id: Date.now() + Math.floor(Math.random() * 10) + i + }); + } + + var serializedRequest = JSON.stringify(requestJson); + + performHttpRequest(instances[0], serializedRequest, function(error, result){ + callback(error, result); + }, 'fuck'); + + } + /* Sends a JSON RPC (http://json-rpc.org/wiki/specification) command to every configured daemon. The callback function is fired once with the result from each daemon unless streamResults is set to true. */ @@ -55,7 +141,7 @@ function DaemonInterface(options){ async.each(instances, function(instance, eachCallback){ var itemFinished = function(error, result){ - var returnObj = {error: error, response: result, instance: instance}; + var returnObj = {error: error, response: result.result, instance: instance}; if (streamResults) callback(returnObj); else results.push(returnObj); eachCallback(); @@ -63,55 +149,15 @@ function DaemonInterface(options){ }; var requestJson = JSON.stringify({ - id: Date.now() + Math.floor(Math.random() * 10), method: method, - params: params + params: params, + id: Date.now() + Math.floor(Math.random() * 10) }); - var options = { - hostname: (typeof(instance.host) === 'undefined' ? 'localhost' : instance.host), - port : instance.port, - method : 'POST', - auth : instance.user + ':' + instance.password, - headers : { - 'Content-Length': requestJson.length - } - }; - - var req = http.request(options, function(res) { - var data = ''; - res.setEncoding('utf8'); - res.on('data', function (chunk) { - data += chunk; - }); - res.on('end', function(){ - var dataJson; - var parsingError; - try{ - dataJson = JSON.parse(data); - - } - catch(e){ - parsingError = e; - _this.emit('error', 'could not parse rpc data from method: ' + method + - ' on instance ' + instance.index + ' data: ' + data); - } - if (typeof(dataJson) !== 'undefined') - itemFinished(dataJson.error, dataJson.result); - else - itemFinished(parsingError); - - }); + performHttpRequest(instance, requestJson, function(error, result){ + itemFinished(error, result); }); - req.on('error', function(e) { - if (e.code === 'ECONNREFUSED') - itemFinished({type: 'offline', message: e.message}, null); - else - itemFinished({type: 'request error', message: e.message}, null); - }); - - req.end(requestJson); }, function(){ if (!streamResults){ @@ -127,6 +173,7 @@ function DaemonInterface(options){ this.init = init; this.isOnline = isOnline; this.cmd = cmd; + this.batchCmd = batchCmd; } DaemonInterface.prototype.__proto__ = events.EventEmitter.prototype; diff --git a/lib/jobManager.js b/lib/jobManager.js index 7e5e135..342d120 100644 --- a/lib/jobManager.js +++ b/lib/jobManager.js @@ -265,13 +265,14 @@ var JobManager = module.exports = function JobManager(options){ } _this.emit('share', { - job: jobId, - ip: ipAddress, - worker: workerName, - difficulty: difficulty, - height: job.rpcData.height, - networkDifficulty: job.difficulty, - solution: blockHash + job : jobId, + ip : ipAddress, + worker : workerName, + difficulty : difficulty, + height : job.rpcData.height, + reward : job.rpcData.coinbasevalue, + networkDifficulty : job.difficulty, + solution : blockHash }, blockHex); return {result: true, error: null, solution: blockHash}; diff --git a/lib/stratum.js b/lib/stratum.js index 9149ffc..f927149 100644 --- a/lib/stratum.js +++ b/lib/stratum.js @@ -297,7 +297,7 @@ var StratumServer = exports.Server = function StratumServer(ports, connectionTim var _this = this; var stratumClients = {}; var subscriptionCounter = SubscriptionCounter(); - + var rebroadcastTimeout; var bannedIPs = {}; //Interval to look through bannedIPs for old bans and remove them in order to prevent a memory leak @@ -347,11 +347,14 @@ var StratumServer = exports.Server = function StratumServer(ports, connectionTim }; (function init(){ - + var serversStarted = 0; Object.keys(ports).forEach(function(port){ net .createServer({allowHalfOpen: true}, function(socket) { _this.handleNewClient(socket); } ) - .listen(parseInt(port), function(){ - _this.emit('started'); + .listen(parseInt(port), function() { + serversStarted++; + if (serversStarted == Object.kets(ports).length) { + _this.emit('started'); + } }); }); @@ -373,6 +376,15 @@ var StratumServer = exports.Server = function StratumServer(ports, connectionTim client.sendMiningJob(jobParams); } } + + /* Some miners will consider the pool dead if it doesn't receive a job at least every 30 seconds. + So every time broadcast jobs, we set a timeout to rebroadcast in 30 seconds unless cleared. */ + clearTimeout(rebroadcastTimeout); + rebroadcastTimeout = setTimeout(function(){ + var resendParams = jobParams; + resendParams[8] = false; + _this.broadcastMiningJobs(resendParams); + }, 30000); }; this.getStratumClients = function () {