diff --git a/blockTemplate.js b/blockTemplate.js index 2c9304b..7efc9ca 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(jobId, rpcData, publicKey, reward, extraNoncePlaceholder){ +var BlockTemplate = module.exports = function BlockTemplate(rpcData, publicKey, reward, extraNoncePlaceholder){ //private members @@ -29,7 +29,7 @@ var BlockTemplate = module.exports = function BlockTemplate(jobId, rpcData, publ //public members this.rpcData = rpcData; - this.jobId = jobId; + this.jobId = null; this.target = util.bignumFromBits(rpcData.bits); this.prevHashReversed = util.reverseByteOrder(new Buffer(rpcData.previousblockhash, 'hex')).toString('hex'); this.transactionData = Buffer.concat(rpcData.transactions.map(function(tx){ @@ -44,12 +44,11 @@ var BlockTemplate = module.exports = function BlockTemplate(jobId, rpcData, publ extraNoncePlaceholder ); - /** - * Checks if the rpcData provided as argument contains the same previousblockhash of the current job. - **/ - this.isRPCDataFromSameBlock = function(rpcData) { - return this.rpcData.previousblockhash == rpcData.previousblockhash; + this.setJobId = function (jobId) { + console.log("SETJOBID "+jobId); + this.jobId = jobId; } + this.serializeCoinbase = function(extraNonce1, extraNonce2){ return Buffer.concat([ this.generationTransaction.coinbase[0], diff --git a/init.js b/init.js index 6e94338..f43be4e 100644 --- a/init.js +++ b/init.js @@ -35,6 +35,20 @@ fs.readdir(confFolder, function(err, files){ console.log('Starting pool for ' + coin.options.name); coin.pool = new pool(coin); coins.push(coin); + 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(); + }, + pollingTime + ); + }); + } }); }); @@ -70,16 +84,4 @@ if (config.blockNotifyListener.enabled){ }); }); blockNotifyServer.listen(config.blockNotifyListener.port, function() {}); -} else { - console.log("NOT ENABLED"); - // If blockNotifyListener isn't enabled then we need to set up some polling parameters. - var pollingTime = typeof(config.blockPollingTime) === 'undefined' ? 5000 : parseInt(config.blockPollingTime, 10); - setInterval( - function () { - coins.forEach(function(coin) { - //coin.pool. - }); - }, - pollingTime - ); -} +} \ No newline at end of file diff --git a/jobManager.js b/jobManager.js index ac64689..9c0e8a0 100644 --- a/jobManager.js +++ b/jobManager.js @@ -36,6 +36,10 @@ var JobCounter = function(){ counter++; if (counter % 0xffff === 0) counter = 1; + return this.cur(); + }; + + this.cur = function () { return counter.toString(16); }; }; @@ -50,15 +54,19 @@ var JobManager = module.exports = function JobManager(options){ var jobs = {}; - + /** + * It only checks if the blockTemplate is already in our jobs list. + * @returns true if it's a new block, false otherwise. + * used by onNewTemplate + **/ function CheckNewIfNewBlock(blockTemplate){ var newBlock = true; for(var job in jobs){ - if (jobs[job].rpcData.previousblockhash === blockTemplate.rpcData.previousblockhash) + if (jobs[job].rpcData.previousblockhash === blockTemplate.rpcData.previousblockhash) { newBlock = false; + } } - if (newBlock) - _this.emit('newBlock', blockTemplate); + return newBlock; } var diffDividend = (function(){ @@ -102,9 +110,14 @@ var JobManager = module.exports = function JobManager(options){ this.currentJob; this.newTemplate = function(rpcData, publicKey){ - this.currentJob = new blockTemplate(jobCounter.next(), rpcData, publicKey, _this.extraNoncePlaceholder); - jobs[this.currentJob.jobId] = this.currentJob; - CheckNewIfNewBlock(this.currentJob); + var tmpBlockTemplate = new blockTemplate(rpcData, publicKey, _this.extraNoncePlaceholder); + if ( CheckNewIfNewBlock(tmpBlockTemplate) ) { + tmpBlockTemplate.setJobId(jobCounter.next()); + jobs[tmpBlockTemplate.jobId] = tmpBlockTemplate; + this.currentJob = jobs[tmpBlockTemplate.jobId]; + _this.emit('newBlock', tmpBlockTemplate); + } + }; this.processShare = function(jobId, difficulty, extraNonce1, extraNonce2, nTime, nonce){ diff --git a/pool.js b/pool.js index 00514bb..d8eddfe 100644 --- a/pool.js +++ b/pool.js @@ -9,21 +9,27 @@ var stratum = require('./stratum.js'); var jobManager = require('./jobManager.js'); var util = require('./util.js'); - +console.log("BOIADIO"); var pool = module.exports = function pool(coin){ var _this = this; var publicKeyBuffer; - + var boiadio = this; this.jobManager = new jobManager({ algorithm: coin.options.algorithm, address: coin.options.address }); this.jobManager.on('newBlock', function(blockTemplate){ - _this.stratumServer.broadcastMiningJobs(blockTemplate.getJobParams()); + console.log("NEWBLOCK "); + if ( typeof(_this.stratumServer ) === 'undefined') { + console.warn("Stratum server still not started! cannot broadcast block!"); + } else { + _this.stratumServer.broadcastMiningJobs(blockTemplate.getJobParams()); + } + }).on('blockFound', function(blockHex){ - + console.log("BLOCKFOUND"); if (coin.options.hasSubmitMethod) { _this.daemon.cmd('submitblock', [blockHex], @@ -89,7 +95,7 @@ var pool = module.exports = function pool(coin){ _this.jobManager.newTemplate(results.rpcTemplate, publicKeyBuffer); - StartStatumServer(); + StartStratumServer(); }); @@ -98,13 +104,14 @@ var pool = module.exports = function pool(coin){ }); - function StartStatumServer(){ + function StartStratumServer(){ console.log('Stratum server starting on port ' + coin.options.stratumPort + ' for ' + coin.options.name); - this.stratumServer = new stratum.Server({ + _this.stratumServer = new stratum.Server({ port: coin.options.stratumPort }); - this.stratumServer.on('started', function(){ + _this.stratumServer.on('started', function(){ + _this.emit('started'); console.log('Stratum server started on port ' + coin.options.stratumPort + ' for ' + coin.options.name); }).on('client', function(client){ client.on('subscription', function(params, resultCallback){ @@ -115,7 +122,13 @@ var pool = module.exports = function pool(coin){ extraNonce2Size ); this.sendDifficulty(coin.options.difficulty); - this.sendMiningJob(_this.jobManager.currentJob.getJobParams()); + 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){ resultCallback(null, true); }).on('submit', function(params, resultCallback){ @@ -137,7 +150,6 @@ var pool = module.exports = function pool(coin){ } function GetBlockTemplate(callback){ - console.log("getBlockTemplate"); _this.daemon.cmd('getblocktemplate', [{"capabilities": [ "coinbasetxn", "workid", "coinbase/append" ]}], function(error, result){ @@ -151,18 +163,28 @@ var pool = module.exports = function pool(coin){ ); } + /** + * This method needs to be called to perform a block polling to the daemon so that we can notify our miners + * about new blocks + **/ this.processBlockPolling = function() { GetBlockTemplate(function(error, result) { - console.log(JSON.stringify(result)); + if (error) { + console.error("[processBlockPolling] Error getting block template for " + coin.options.name); + } _this.jobManager.newTemplate(result, publicKeyBuffer); }); } + /** + * This method is being called from the blockNotify so that when a new block is discovered by the daemon + * We can inform our miners about the newly found block + **/ this.processBlockNotify = function(blockHash){ if (blockHash !== _this.jobManager.currentJob.rpcData.previousblockhash){ GetBlockTemplate(function(error, result){ if (error){ - console.log('Error getting block template for ' + coin.options.name); + console.error('[processBlockNotify] Error getting block template for ' + coin.options.name); return; } _this.jobManager.newTemplate(result, publicKeyBuffer); diff --git a/stratum.js b/stratum.js index 17d5809..b32f8ba 100644 --- a/stratum.js +++ b/stratum.js @@ -214,7 +214,7 @@ var StratumServer = exports.Server = function StratumServer(options){ //public members this.broadcastMiningJobs = function(jobParams){ - for (var clientId in _stratumClients){ + for (var clientId in stratumClients){ stratumClients[clientId].sendMiningJob(jobParams) } };