diff --git a/lib/jobManager.js b/lib/jobManager.js index 55dbfff..d0d7233 100644 --- a/lib/jobManager.js +++ b/lib/jobManager.js @@ -179,6 +179,7 @@ var JobManager = module.exports = function JobManager(options){ var blockHash; var blockHex; + if (job.target.ge(headerBigNum)){ blockHex = job.serializeBlock(headerBuffer, coinbaseBuffer).toString('hex'); blockHash = util.reverseBuffer(util.doublesha(headerBuffer)).toString('hex'); diff --git a/lib/pool.js b/lib/pool.js index b51ffa4..fb2a786 100644 --- a/lib/pool.js +++ b/lib/pool.js @@ -34,13 +34,20 @@ var pool = module.exports = function pool(options, authorizeFn){ var emitErrorLog = function(key, text) { _this.emit('log', 'error' , key, text); }; (function Init(){ - SetupJobManager(); - SetupVarDiff(); - SetupDaemonInterface(); + //timeout used so onLog event can be hooked before we start initializing + setTimeout(function(){ + SetupJobManager(); + SetupVarDiff(); + SetupDaemonInterface(); + }, 10); })(); function SetupVarDiff(){ + if (!options.varDiff.enabled){ + emitLog('system', 'VarDiff has been disabled'); + return; + } _this.varDiff = new varDiff(options.varDiff, options.difficulty); _this.varDiff.on('difficultyRequest', function(){ emitLog('varDiff', 'Difficulty requested for vardiff'); @@ -50,8 +57,10 @@ var pool = module.exports = function pool(options, authorizeFn){ client.sendDifficulty(newDiff); client.sendMiningJob(_this.jobManager.currentJob.getJobParams()); }); + emitLog("system", "VarDiff enabled and setup"); } + function RequestDifficulty(callback){ _this.daemon.cmd('getdifficulty', [], @@ -59,7 +68,8 @@ var pool = module.exports = function pool(options, authorizeFn){ if (error) { emitErrorLog('getdifficulty', 'Error requesting difficulty from daemon for vardiff'); } else { - _this.varDiff.setNetworkDifficulty(result); + if (options.varDiff.enabled) + _this.varDiff.setNetworkDifficulty(result); callback(error, result); } } @@ -138,7 +148,7 @@ var pool = module.exports = function pool(options, authorizeFn){ _this.daemon = new daemon.interface(options.daemon); _this.daemon.on('online', function(){ async.parallel({ - difficulty: RequestDifficulty, + networkDifficulty: RequestDifficulty, addressInfo: function(callback){ _this.daemon.cmd('validateaddress', [options.address], @@ -180,6 +190,16 @@ var pool = module.exports = function pool(options, authorizeFn){ util.script_to_address(results.addressInfo.address) : util.script_to_pubkey(results.addressInfo.pubkey); + if (options.difficulty > results.networkDifficulty && options.difficulty > 16){ + var newDiff = results.networkDifficulty > 16 ? results.networkDifficulty : 16; + emitWarningLog('system', 'pool difficulty was set higher than network difficulty'); + emitWarningLog('system', 'lowering pool diff from ' + options.difficulty + ' to ' + newDiff); + + options.difficulty = newDiff + + if (options.varDiff.enabled) + _this.varDiff.setPoolDifficulty(options.difficulty); + } GetBlockTemplate(function(error, result){ if (error) @@ -210,7 +230,8 @@ var pool = module.exports = function pool(options, authorizeFn){ _this.emit('started'); }).on('client.connected', function(client){ - _this.varDiff.manageClient(client); + if (options.varDiff.enabled) + _this.varDiff.manageClient(client); client.on('subscription', function(params, resultCallback){ @@ -268,7 +289,7 @@ var pool = module.exports = function pool(options, authorizeFn){ }); }, pollingInterval); - emitLog('system', 'Block polling setup for every ' + pollingInterval + ' milliseconds'); + emitLog('system', 'Block polling every ' + pollingInterval + ' milliseconds'); } function GetBlockTemplate(callback){ diff --git a/lib/varDiff.js b/lib/varDiff.js index 92ab1cf..2c3e51d 100644 --- a/lib/varDiff.js +++ b/lib/varDiff.js @@ -27,10 +27,8 @@ function RingBuffer(maxSize){ } }; this.avg = function(){ - var total = data.reduce(function(a, b){ - return a + b; - }); - return total / (isFull ? maxSize : cursor); + var sum = data.reduce(function(a, b){ return a + b }); + return sum / (isFull ? maxSize : cursor); }; this.size = function(){ return isFull ? maxSize : cursor; @@ -47,14 +45,6 @@ var varDiff = module.exports = function varDiff(options, poolDifficulty){ var _this = this; - this.setNetworkDifficulty = function(diff){ - networkDifficulty = diff; - }; - - if (!options.enabled){ - return; - } - var networkDifficulty; var bufferSize = options.retargetTime / options.targetTime * 4; var variance = options.targetTime * (options.variancePercent / 100); @@ -62,11 +52,19 @@ var varDiff = module.exports = function varDiff(options, poolDifficulty){ var tMax = options.targetTime + variance; - setInterval(function(){ _this.emit('difficultyRequest'); }, options.daemonDiffUpdateFrequency * 1000); + + this.setNetworkDifficulty = function(diff){ + networkDifficulty = diff; + }; + this.setPoolDifficulty = function(diff){ + poolDifficulty = diff; + }; + + this.manageClient = function(client){ var lastTs; var lastRtc; @@ -80,7 +78,7 @@ var varDiff = module.exports = function varDiff(options, poolDifficulty){ lastRtc = ts - options.retargetTime / 2; lastTs = ts; timeBuffer = new RingBuffer(bufferSize); - console.log('first time share vardiff'); + //console.log('first time share vardiff'); return; } @@ -88,7 +86,7 @@ var varDiff = module.exports = function varDiff(options, poolDifficulty){ lastTs = ts; if ((ts - lastRtc) < options.retargetTime && timeBuffer.size() > 0){ - console.log('do not retarget'); + //console.log('do not retarget'); return; } @@ -101,10 +99,14 @@ var varDiff = module.exports = function varDiff(options, poolDifficulty){ var ddiff = (client.difficulty * (options.targetTime / avg)) - client.difficulty; if (avg > tMax && client.difficulty > options.minDifficulty){ - if (ddiff > -1) + + if (ddiff > -1){ ddiff = -1; + } if (ddiff + client.difficulty < poolDifficulty) ddiff = options.minDifficulty - client.difficulty; + + //console.log('decreasing difficulty, ddiff: ' + ddiff); } else if (avg < tMin){ if (ddiff < 1) @@ -112,16 +114,18 @@ var varDiff = module.exports = function varDiff(options, poolDifficulty){ var diffMax = networkDifficulty < options.maxDifficulty ? networkDifficulty : options.maxDifficulty; if (ddiff + client.difficulty > diffMax) ddiff = diffMax - client.difficulty; + + //console.log('increasing difficulty, ddiff: ' + ddiff); } else{ - console.log('hashrate in range ' + JSON.stringify({ddiff: ddiff, avg: avg}) ); + //console.log('hashrate in range ' + JSON.stringify({ddiff: ddiff, avg: avg}) ); return; } var newDiff = client.difficulty * ddiff; timeBuffer.clear(); - console.log('sending new difficutly ' + newDiff); + //console.log('sending new difficutly ' + newDiff); _this.emit('newDifficulty', client, newDiff); }); diff --git a/package.json b/package.json index 7bb3866..7810b80 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "stratum-pool", - "version": "0.0.2", + "version": "0.0.3", "author": "Matthew Little", "description": "High performance Stratum poolserver in Node.js", "contributors": [