diff --git a/blockTemplate.js b/blockTemplate.js index 7efc9ca..8a4ffe1 100644 --- a/blockTemplate.js +++ b/blockTemplate.js @@ -6,7 +6,7 @@ var transactions = require('./transactions.js'); var util = require('./util.js'); -var BlockTemplate = module.exports = function BlockTemplate(rpcData, publicKey, reward, extraNoncePlaceholder){ +var BlockTemplate = module.exports = function BlockTemplate(rpcData, publicKey, extraNoncePlaceholder){ //private members @@ -40,12 +40,10 @@ var BlockTemplate = module.exports = function BlockTemplate(rpcData, publicKey, this.generationTransaction = new transactions.Generation( rpcData, publicKey, - reward, extraNoncePlaceholder ); this.setJobId = function (jobId) { - console.log("SETJOBID "+jobId); this.jobId = jobId; } diff --git a/init.js b/init.js index f43be4e..7a7f9b7 100644 --- a/init.js +++ b/init.js @@ -3,6 +3,7 @@ var fs = require('fs'); var path = require('path'); var pool = require('./pool.js'); +var ShareManager = require('./shareManager.js').ShareManager; var logRef = console.log; console.log = function(s){ @@ -33,14 +34,18 @@ fs.readdir(confFolder, function(err, files){ var coinJson = JSON.parse(data) var coin = new Coin(coinJson); console.log('Starting pool for ' + coin.options.name); + coin.pool = new pool(coin); + coin.shareManager = new ShareManager(coin.pool); + coins.push(coin); + + // If the block notify listener is not enabled lets set up the polling. if ( ! config.blockNotifyListener.enabled ) { // as soon as the pool is started we start polling var pollingTime = typeof(config.blockPollingTime) === 'undefined' ? 5000 : parseInt(config.blockPollingTime, 10); coin.pool.on('started', function() { var curPool = this; - console.log("STARTED?"); setInterval( function() { curPool.processBlockPolling(); @@ -48,6 +53,8 @@ fs.readdir(confFolder, function(err, files){ pollingTime ); }); + + } }); diff --git a/jobManager.js b/jobManager.js index 9c0e8a0..697c2c5 100644 --- a/jobManager.js +++ b/jobManager.js @@ -157,17 +157,18 @@ var JobManager = module.exports = function JobManager(options){ var headerHash = hashDigest(headerBuffer, nTimeInt); var headerBigNum = bignum.fromBuffer(headerHash, {endian: 'little', size: 32}); - if (job.target.ge(headerBigNum)){ - var blockBuf = job.serializeBlock(headerBuffer, coinbaseBuffer); - _this.emit('blockFound', blockBuf.toString('hex')); - } - var targetUser = bignum(diffDividend / difficulty); if (headerBigNum.gt(targetUser)){ return {error: [23, 'low difficulty share', null]}; } - return {result: true}; + if (job.target.ge(headerBigNum)){ + var blockBuf = job.serializeBlock(headerBuffer, coinbaseBuffer); + + _this.emit('blockFound', blockBuf.toString('hex'), headerBigNum.toString(16), coinbaseHash.toString('hex')); + } + + return {result: true, headerHEX: headerBigNum.toString(16)}; }; }; JobManager.prototype.__proto__ = events.EventEmitter.prototype; \ No newline at end of file diff --git a/pool.js b/pool.js index f63a787..f9e0b9a 100644 --- a/pool.js +++ b/pool.js @@ -14,6 +14,7 @@ var pool = module.exports = function pool(coin){ var _this = this; var publicKeyBuffer; + this.shareManager = undefined; // just for us to know that the variable should be this one. this.jobManager = new jobManager({ algorithm: coin.options.algorithm, address: coin.options.address @@ -25,7 +26,10 @@ var pool = module.exports = function pool(coin){ _this.stratumServer.broadcastMiningJobs(blockTemplate.getJobParams()); } - }).on('blockFound', function(blockHex){ + }).on('blockFound', function(blockHex, headerHex, third){ + console.log("BLOCK "+blockHex); + console.log("HEADER "+headerHex); + console.log("THIRD "+third); if (coin.options.hasSubmitMethod) { _this.daemon.cmd('submitblock', [blockHex], @@ -121,8 +125,6 @@ var pool = module.exports = function pool(coin){ if (typeof(_this.jobManager.currentJob) === 'undefined') { console.warn("[subscription] Cannot send job to client. No jobs in jobManager!"); } else { - console.log("ANTANI?"); - console.log(JSON.stringify(_this.jobManager.currentJob.getJobParams())); this.sendMiningJob(_this.jobManager.currentJob.getJobParams()); } }).on('authorize', function(params, resultCallback){ @@ -138,9 +140,24 @@ var pool = module.exports = function pool(coin){ ); if (result.error){ resultCallback(result.error); - return; + _this.emit('share', false, { + workerName : params.name, + error : result.error + }); + } else { + resultCallback(null, true); + _this.emit('share', true, { + blockHeaderHex : result.headerHEX, + workerName : params.name, + jobId : params.jobId, + clientDifficulty : client.difficulty, + extraNonce1 : client.extraNonce1, + extraNonce2 : params.extraNonce2, + nTime : params.nTime, + nonce : params.nonce + }); } - resultCallback(null, true); + }); }); } @@ -149,10 +166,9 @@ var pool = module.exports = function pool(coin){ _this.daemon.cmd('getblocktemplate', [{"capabilities": [ "coinbasetxn", "workid", "coinbase/append" ]}], function(error, result){ - if (error){ + if (error) { callback(error); - } - else{ + } else { callback(null, result); } } diff --git a/shareManager.js b/shareManager.js index e69de29..636aa39 100644 --- a/shareManager.js +++ b/shareManager.js @@ -0,0 +1,41 @@ +var events = require('events'); + +/** + * This ShareManager Events table: will emit the following events: + * + * LISTENS on: + * - pool('share') +**/ + +var ShareManager = exports.ShareManager = function(pool) { + pool.on('share', function(isValid, data) { + if (isValid) { + handleValidShare( + data.workerName, + data.blockHeaderHex, + data.jobId, + data.clientDifficulty, + data.extraNonce1, + data.extraNonce2, + data.nTime, + data.nonce); + } else { + handleInvalidShare( + data.workerName, + data.error[0], + data.error[1]); + } + }); + + function handleValidShare(workerName, headerHex, jobId, clientDifficulty, extraNonce1, extraNonce2, nTime, nonce) { + console.log("A new Valid share from "+workerName+" has arrived! - "+headerHex); + } + + function handleInvalidShare(workerName, errorCode, errorDescription) { + console.log("Invalid share form "+workerName+" ErrorCode: "+errorCode+ " ErrorDescription: "+errorDescription); + } +}; + +ShareManager.prototype.__proto__ = events.EventEmitter.prototype; + + diff --git a/stratum.js b/stratum.js index b32f8ba..2486600 100644 --- a/stratum.js +++ b/stratum.js @@ -79,6 +79,12 @@ var StratumClient = function(options){ }, function(error, result){ _this.authorized = result; + /*if (_this.authorized) { + // if authorized lets store the workername + // so that when a share is found we can get it for the accounting + _this.workerName = message.params[0][0]; + }*/ + sendJson({ id: message.id, result: result, @@ -105,6 +111,7 @@ var StratumClient = function(options){ }); return; } + console.log("SUBMIT "+JSON.stringify(message)); _this.emit('submit', { name: message.params[0],