From 7cf448d1e2f0c9b5510a7121004e67d393a9e07f Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 12 Mar 2014 15:55:58 -0600 Subject: [PATCH] Added reward to share emit (for payment processing ;) ) --- README.md | 1 + lib/daemon.js | 143 +++++++++++++++++++++++++++++----------------- lib/jobManager.js | 1 + 3 files changed, 94 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index 3aa0f83..f0f3bce 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/daemon.js b/lib/daemon.js index 6aee256..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,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; diff --git a/lib/jobManager.js b/lib/jobManager.js index 7e5e135..dc7bcaa 100644 --- a/lib/jobManager.js +++ b/lib/jobManager.js @@ -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);