From cbcaa1cf9811cd802d5a6fabb235a8b51cd5dd41 Mon Sep 17 00:00:00 2001 From: Matthew Little Date: Tue, 14 Jan 2014 18:40:19 -0700 Subject: [PATCH] Added proper block hash to share solution. Added members to emitted share data. --- lib/jobManager.js | 35 +++++++++-------------------------- lib/pool.js | 41 ++++++++++++++++++++--------------------- lib/stratum.js | 1 + 3 files changed, 30 insertions(+), 47 deletions(-) diff --git a/lib/jobManager.js b/lib/jobManager.js index 9b38305..17c4b90 100644 --- a/lib/jobManager.js +++ b/lib/jobManager.js @@ -104,24 +104,7 @@ var JobManager = module.exports = function JobManager(options){ } })(); - /** - * Tries to estimate the resulting block hash - * This is only valid for scrypt apparently. - * @author vekexasia - **/ - function blockHashHex(headerBuffer) { - var result = new Buffer(80); - for (var i=0; i<20; i++) { - for (var j=0; j<4; j++) { - result[i*4+j] = headerBuffer[i*4+3-j]; - } - } - var shaed = util.reverseBuffer(util.doublesha(result)); - - - return shaed.toString('hex'); // return the expected block hash - - } + //public members @@ -179,27 +162,27 @@ var JobManager = module.exports = function JobManager(options){ var coinbaseBuffer = job.serializeCoinbase(extraNonce1Buffer, extraNonce2Buffer); var coinbaseHash = util.doublesha(coinbaseBuffer); - var merkleRoot = job.merkleTree.withFirst(coinbaseHash); - merkleRoot = util.reverseBuffer(merkleRoot).toString('hex'); + var merkleRoot = util.reverseBuffer(job.merkleTree.withFirst(coinbaseHash)).toString('hex'); var headerBuffer = job.serializeHeader(merkleRoot, nTime, nonce); var headerHash = hashDigest(headerBuffer, nTimeInt); var headerBigNum = bignum.fromBuffer(headerHash, {endian: 'little', size: 32}); - + var blockHash; if (job.target.ge(headerBigNum)){ - var blockBuf = job.serializeBlock(headerBuffer, coinbaseBuffer); - // console.log("EXPECTED BLOCK HASH: "+blockHashHex(headerBuffer)); // NOT WORKING :(? - _this.emit('blockFound', blockBuf.toString('hex'), blockHashHex(headerBuffer)); - } else { + var blockHex = job.serializeBlock(headerBuffer, coinbaseBuffer).toString('hex'); + blockHash = util.reverseBuffer(util.doublesha(headerBuffer)).toString('hex'); + _this.emit('blockFound', blockHex, blockHash); + } + else { var targetUser = bignum(diffDividend / difficulty); if (headerBigNum.gt(targetUser)){ return {error: [23, 'low difficulty share', null]}; } } - return {result: true, headerHEX: headerBigNum.toString(16)}; + return {result: true, error: null, solution: blockHash}; }; }; JobManager.prototype.__proto__ = events.EventEmitter.prototype; \ No newline at end of file diff --git a/lib/pool.js b/lib/pool.js index a691273..553c53e 100644 --- a/lib/pool.js +++ b/lib/pool.js @@ -42,19 +42,19 @@ var pool = module.exports = function pool(options, authorizeFn){ emitLog('system', 'Detected new block'); _this.stratumServer.broadcastMiningJobs(blockTemplate.getJobParams()); } - }).on('blockFound', function(blockHex, headerHex){ + }).on('blockFound', function(blockHex, blockHash){ if (options.hasSubmitMethod) { _this.daemon.cmd('submitblock', [blockHex], function(error, result){ - emitLog('submitblock', 'Submitted Block using submitblock :'+headerHex); + emitLog('submitblock', 'Submitted Block using submitblock :'+blockHash); } ); } else { _this.daemon.cmd('getblocktemplate', [{'mode': 'submit', 'data': blockHex}], function(error, result){ - emitLog('submitblock', 'Submitted Block using getblocktemplate: '+headerHex); + emitLog('submitblock', 'Submitted Block using getblocktemplate: '+blockHash); } ); } @@ -150,24 +150,23 @@ var pool = module.exports = function pool(options, authorizeFn){ params.nTime, params.nonce ); - if (result.error){ - resultCallback(result.error); - _this.emit('share', false, { - client : client, - error : result.error - }); - } else { - resultCallback(null, true); - _this.emit('share', true, { - client : client, - blockHeaderHex : result.headerHEX, - workerName : params.name, - jobId : params.jobId, - extraNonce2 : params.extraNonce2, - nTime : params.nTime, - nonce : params.nonce - }); - } + + resultCallback(result.error, result.result ? true : null); + + _this.emit('share', !result.error, { + job: params.jobId, + ip: client.socket.remoteAddress, + worker: params.name, + solution: result.solution, + error: result.error ? result.error[1] : undefined, + difficulty: client.difficulty, + timestamp: Date.now() / 1000 | 0, + accepted: !!result.result, + extraNonce2: params.extraNonce2, + nTime: params.nTime, + nonce: params.nonce + }); + }).on('malformedMessage', function (message) { emitWarningLog('client', client.workerName+" has sent us a malformed message: "+message); }).on('socketError', function() { diff --git a/lib/stratum.js b/lib/stratum.js index 97e2c91..d5d993a 100644 --- a/lib/stratum.js +++ b/lib/stratum.js @@ -28,6 +28,7 @@ var SubscriptionCounter = function(){ var StratumClient = function(options){ //private members + this.socket = options.socket; var _this = this;