diff --git a/lib/blockTemplate.js b/lib/blockTemplate.js index 8a586eb..d6840b8 100644 --- a/lib/blockTemplate.js +++ b/lib/blockTemplate.js @@ -5,13 +5,12 @@ var transactions = require('./transactions.js'); var util = require('./util.js'); -var maxDifficulty = 0x00000000ffff0000000000000000000000000000000000000000000000000000; /** * The BlockTemplate class holds a single job. * and provides several methods to validate and submit it to the daemon coin **/ -var BlockTemplate = module.exports = function BlockTemplate(jobId, rpcData, publicKey, extraNoncePlaceholder, reward, txMessages){ +var BlockTemplate = module.exports = function BlockTemplate(maxDifficulty, jobId, rpcData, publicKey, extraNoncePlaceholder, reward, txMessages){ //private members @@ -44,7 +43,7 @@ var BlockTemplate = module.exports = function BlockTemplate(jobId, rpcData, publ bignum.fromBuffer(new Buffer(rpcData.target, 'hex')) : util.bignumFromBits(rpcData.bits); - this.difficulty = (maxDifficulty / this.target.toNumber()) * 65536; + this.difficulty = maxDifficulty.div(this.target).toNumber(); this.prevHashReversed = util.reverseByteOrder(new Buffer(rpcData.previousblockhash, 'hex')).toString('hex'); this.transactionData = Buffer.concat(rpcData.transactions.map(function(tx){ diff --git a/lib/jobManager.js b/lib/jobManager.js index 407fc0a..13efcbe 100644 --- a/lib/jobManager.js +++ b/lib/jobManager.js @@ -56,25 +56,25 @@ var JobManager = module.exports = function JobManager(options){ var _this = this; var jobCounter = new JobCounter(); - //Which number to use as dividend when converting difficulty to target - var diffDividend = bignum((function(){ + var maxDifficulty = bignum((function(){ switch(options.coin.algorithm){ case 'sha256': case 'skein': return '00000000ffff0000000000000000000000000000000000000000000000000000'; case 'scrypt': case 'scrypt-jane': + case 'x11': return '0000ffff00000000000000000000000000000000000000000000000000000000'; case 'quark': - case 'x11': - return '000000ffff000000000000000000000000000000000000000000000000000000'; + return '0000ffff00000000000000000000000000000000000000000000000000000000'; case 'max': return 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000' } })(), 16); + //On initialization lets figure out which hashing algorithm to use var hashDigest = (function(){ switch(options.coin.algorithm){ @@ -168,6 +168,7 @@ var JobManager = module.exports = function JobManager(options){ if (isNewBlock || updatedTransactions){ var tmpBlockTemplate = new blockTemplate( + maxDifficulty, jobCounter.next(), rpcData, publicKey, @@ -257,7 +258,7 @@ var JobManager = module.exports = function JobManager(options){ blockHash = util.reverseBuffer(util.doublesha(headerBuffer)).toString('hex'); } else { - var targetUser = diffDividend.div(difficulty); + var targetUser = maxDifficulty.div(difficulty); if (headerBigNum.gt(targetUser)){ return shareError([23, 'low difficulty share']); } diff --git a/lib/pool.js b/lib/pool.js index 7b2e8a3..3676aad 100644 --- a/lib/pool.js +++ b/lib/pool.js @@ -274,7 +274,7 @@ var pool = module.exports = function pool(options, authorizeFn){ return true; }); if (couldNotDetectMethod){ - emitErrorLog('Could not detect block submission RPC method'); + emitErrorLog('Could not detect block submission RPC method, ' + JSON.stringify(results)); callback('block submission detection failed'); } }); @@ -295,13 +295,9 @@ var pool = module.exports = function pool(options, authorizeFn){ util.script_to_address(results.addressInfo.address) : util.script_to_pubkey(results.addressInfo.pubkey); - var networkDifficulty = Math.round(results.info.difficulty * 65536); - var network = results.info.testnet ? 'testnet' : 'live blockchain'; + var networkType = results.info.testnet ? 'testnet' : 'live blockchain'; + - emitLog('Connected to ' + network + - '; detected ' + options.coin.reward + - ' reward type; block height of ' + results.info.blocks + - '; difficulty of ' + networkDifficulty); GetBlockTemplate(function(error, result){ if (error) { @@ -315,11 +311,16 @@ var pool = module.exports = function pool(options, authorizeFn){ } else { Object.keys(options.ports).forEach(function(port){ var portDiff = options.ports[port].diff; - if (portDiff > networkDifficulty) + if (portDiff > _this.jobManager.currentJob.difficulty) emitWarningLog('Pool difficulty of ' + portDiff + ' on port ' + port + - ' was set higher than network difficulty of ' + networkDifficulty); + ' was set higher than network difficulty of ' + _this.jobManager.currentJob.difficulty); }); + emitLog('Connected to ' + networkType + + '; detected ' + options.coin.reward + + ' reward type; block height of ' + results.info.blocks + + '; difficulty of ' + _this.jobManager.currentJob.difficulty); + SetupBlockPolling(); StartStratumServer(); SetupPeer(); @@ -366,7 +367,7 @@ var pool = module.exports = function pool(options, authorizeFn){ _this.daemon.cmd('getblocktemplate', [], function(results){ var synced = results.every(function(r){ - return r.error.code !== -10; + return !r.error || r.error.code !== -10; }); if (synced){ SetupDaemonInterface(); @@ -435,7 +436,7 @@ var pool = module.exports = function pool(options, authorizeFn){ resultCallback(result.error, result.result ? true : null); }).on('malformedMessage', function (message) { - emitWarningLog(client.workerName + " has sent us a malformed message: " + message); + emitWarningLog((client.workerName || ('Unauthorized miner [' + client.socket.remoteAddress + ']')) + " has sent us a malformed message: " + message); }).on('socketError', function(err) { emitWarningLog(client.workerName + " has somehow had a socket error: " + JSON.stringify(err));