Fixed merging shit lol

This commit is contained in:
Andrea 2014-03-14 20:52:26 +00:00 committed by Andrea Baccega
parent a4f302b526
commit bcad84af80
3 changed files with 116 additions and 56 deletions

View File

@ -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. /* 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 The callback function is fired once with the result from each daemon unless streamResults is
set to true. */ set to true. */
@ -55,7 +141,7 @@ function DaemonInterface(options){
async.each(instances, function(instance, eachCallback){ async.each(instances, function(instance, eachCallback){
var itemFinished = function(error, result){ 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); if (streamResults) callback(returnObj);
else results.push(returnObj); else results.push(returnObj);
eachCallback(); eachCallback();
@ -63,55 +149,15 @@ function DaemonInterface(options){
}; };
var requestJson = JSON.stringify({ var requestJson = JSON.stringify({
id: Date.now() + Math.floor(Math.random() * 10),
method: method, method: method,
params: params params: params,
id: Date.now() + Math.floor(Math.random() * 10)
}); });
var options = { performHttpRequest(instance, requestJson, function(error, result){
hostname: (typeof(instance.host) === 'undefined' ? 'localhost' : instance.host), itemFinished(error, result);
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);
});
}); });
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(){ }, function(){
if (!streamResults){ if (!streamResults){
@ -127,6 +173,7 @@ function DaemonInterface(options){
this.init = init; this.init = init;
this.isOnline = isOnline; this.isOnline = isOnline;
this.cmd = cmd; this.cmd = cmd;
this.batchCmd = batchCmd;
} }
DaemonInterface.prototype.__proto__ = events.EventEmitter.prototype; DaemonInterface.prototype.__proto__ = events.EventEmitter.prototype;

View File

@ -265,13 +265,14 @@ var JobManager = module.exports = function JobManager(options){
} }
_this.emit('share', { _this.emit('share', {
job: jobId, job : jobId,
ip: ipAddress, ip : ipAddress,
worker: workerName, worker : workerName,
difficulty: difficulty, difficulty : difficulty,
height: job.rpcData.height, height : job.rpcData.height,
networkDifficulty: job.difficulty, reward : job.rpcData.coinbasevalue,
solution: blockHash networkDifficulty : job.difficulty,
solution : blockHash
}, blockHex); }, blockHex);
return {result: true, error: null, solution: blockHash}; return {result: true, error: null, solution: blockHash};

View File

@ -297,7 +297,7 @@ var StratumServer = exports.Server = function StratumServer(ports, connectionTim
var _this = this; var _this = this;
var stratumClients = {}; var stratumClients = {};
var subscriptionCounter = SubscriptionCounter(); var subscriptionCounter = SubscriptionCounter();
var rebroadcastTimeout;
var bannedIPs = {}; var bannedIPs = {};
//Interval to look through bannedIPs for old bans and remove them in order to prevent a memory leak //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(){ (function init(){
var serversStarted = 0;
Object.keys(ports).forEach(function(port){ Object.keys(ports).forEach(function(port){
net .createServer({allowHalfOpen: true}, function(socket) { _this.handleNewClient(socket); } ) net .createServer({allowHalfOpen: true}, function(socket) { _this.handleNewClient(socket); } )
.listen(parseInt(port), function(){ .listen(parseInt(port), function() {
_this.emit('started'); 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); 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 () { this.getStratumClients = function () {