Added reward to share emit (for payment processing ;) )

This commit is contained in:
Matt 2014-03-12 15:55:58 -06:00
parent 585baf7b92
commit 7cf448d1e2
3 changed files with 94 additions and 51 deletions

View File

@ -186,6 +186,7 @@ Listen to pool events
ip: '71.33.19.37', //ip address of client
worker: 'matt.worker1', //stratum worker name
difficulty: 64, //stratum client difficulty
reward: 5000000000, //the number of satoshis received as payment for solving this block
height: 443795, //block height
networkDifficulty: 3349 //network difficulty for this block

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.
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,61 +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){
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 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){
@ -133,6 +173,7 @@ function DaemonInterface(options){
this.init = init;
this.isOnline = isOnline;
this.cmd = cmd;
this.batchCmd = batchCmd;
}
DaemonInterface.prototype.__proto__ = events.EventEmitter.prototype;

View File

@ -270,6 +270,7 @@ var JobManager = module.exports = function JobManager(options){
worker: workerName,
difficulty: difficulty,
height: job.rpcData.height,
reward: job.rpcData.coinbasevalue,
networkDifficulty: job.difficulty,
solution: blockHash
}, blockHex);