Refactored Matthew fix for vardiff.

Fixed .size() bug that was causing crash

Merge branch 'master' of https://github.com/zone117x/node-stratum

Conflicts:
	lib/jobManager.js
This commit is contained in:
Andrea Baccega 2014-02-20 00:45:32 +01:00
commit 3ed4e93b5f
3 changed files with 43 additions and 47 deletions

View File

@ -1,4 +1,5 @@
var events = require('events'); var events = require('events');
var crypto = require('crypto');
var bignum = require('bignum'); var bignum = require('bignum');
@ -14,18 +15,17 @@ var blockTemplate = require('./blockTemplate.js');
//Unique extranonce per subscriber //Unique extranonce per subscriber
var ExtraNonceCounter = function(){ var ExtraNonceCounter = function(configInstanceId){
var instanceId = 31;
var instanceId = configInstanceId || crypto.randomBytes(4).readUInt32LE(0);
var counter = instanceId << 27; var counter = instanceId << 27;
var size = util.packUInt32BE(Math.abs(counter)).length;
this.next = function(){ this.next = function(){
var extraNonce = util.packUInt32BE(Math.abs(counter++)); var extraNonce = util.packUInt32BE(Math.abs(counter++));
return extraNonce.toString('hex'); return extraNonce.toString('hex');
}; };
this.size = function(){
return size; this.size = 4; //bytes
};
}; };
//Unique job per new block template //Unique job per new block template
@ -126,9 +126,9 @@ var JobManager = module.exports = function JobManager(options){
//public members //public members
this.extraNonceCounter = new ExtraNonceCounter(); this.extraNonceCounter = new ExtraNonceCounter(options.instanceId);
this.extraNoncePlaceholder = new Buffer('f000000ff111111f', 'hex'); this.extraNoncePlaceholder = new Buffer('f000000ff111111f', 'hex');
this.extraNonce2Size = this.extraNoncePlaceholder.length - this.extraNonceCounter.size(); this.extraNonce2Size = this.extraNoncePlaceholder.length - this.extraNonceCounter.size;
this.currentJob; this.currentJob;

View File

@ -50,31 +50,10 @@ var pool = module.exports = function pool(options, authorizeFn){
emitLog('varDiff', 'Difficulty requested for vardiff'); emitLog('varDiff', 'Difficulty requested for vardiff');
if (_this.stratumServer) if (_this.stratumServer)
RequestDifficulty(function(){}); RequestDifficulty(function(){});
}).on('newDifficulty', function(client, newDiff){ }).on('newDifficulty', function(client, newDiff) {
client.sendDifficulty(newDiff); // We request to set the newDiff @ the next difficulty retarget
// (which should happen when a new job comes in - AKA BLOCK)
/* client.enqueueNextDifficulty(newDiff);
Disabled this line of code because it will force the miner
to restart and submit duplicate shares. Stratum-python sends out a
mining.notify but rolls the jobID and sets "clean jobs" to false.
Meaning that the worker will only start on the new work once it
exhausts its current nonce range. But if the miner were to start the
new job, the shares would be invalidated since stratum-python doesn't
insert the new jobID that the share-limiter generated into the jobs
array. Even worse, since the new work is only sent with a new jobID
but with the same extranonce and other job parameters as the last job,
the shares will be duplicate. Perhaps this bug has gone unnoticed
because of how likely it is for a miner to exhaust the nonce range
before new work is sent.
Here is where stratum-python is bugged:
https://github.com/Crypto-Expert/stratum-mining/blob/master/mining/basic_share_limiter.py#L171-L178
So lets only send a new difficulty, and the worker will use it when
it receives a new job from when the block template updates.
*/
//client.sendMiningJob(_this.jobManager.currentJob.getJobParams());
}); });
emitLog("system", "VarDiff enabled and setup"); emitLog("system", "VarDiff enabled and setup");
} }

View File

@ -24,7 +24,7 @@ var SubscriptionCounter = function(){
* - submit(data(name, jobID, extraNonce2, ntime, nonce)) * - submit(data(name, jobID, extraNonce2, ntime, nonce))
**/ **/
var StratumClient = function(options){ var StratumClient = function(options){
var pendingDifficulty = null;
//private members //private members
this.socket = options.socket; this.socket = options.socket;
@ -139,9 +139,9 @@ var StratumClient = function(options){
}); });
} }
); );
if (_this.lastEmittedDiff !== _this.difficulty){ if (_this.lastEmittedDiff !== _this.currentDifficulty){
_this.lastEmittedDiff = _this.difficulty _this.lastEmittedDiff = _this.currentDifficulty
_this.emit('difficultyChanged', _this.difficulty); _this.emit('difficultyChanged', _this.currentDifficulty);
} }
} }
@ -193,6 +193,18 @@ var StratumClient = function(options){
}); });
} }
this.enqueueNextDifficulty = function(requestedNewDifficulty) {
if (typeof(requestedNewDifficulty) != 'number') {
console.error('[StratumClient.enqueueNextDifficulty] given difficulty parameter is not a number: ['+requestedNewDifficulty+']');
return false;
} else {
console.log('[StratumClient.enqueueNextDifficulty] next difficulty should be: ['+requestedNewDifficulty+']');
pendingDifficulty = requestedNewDifficulty;
return true;
}
};
//public members //public members
@ -206,26 +218,31 @@ var StratumClient = function(options){
return false; return false;
} }
if (difficulty !== this.difficulty) { if (difficulty === this.difficulty)
this.difficulty = difficulty;
sendJson({
id : null,
method: "mining.set_difficulty",
params: [difficulty]//[512],
});
return true;
} else {
return false; return false;
}
_this.difficulty = difficulty;
sendJson({
id : null,
method: "mining.set_difficulty",
params: [difficulty]//[512],
});
return true;
}; };
this.sendMiningJob = function(jobParams){ this.sendMiningJob = function(jobParams){
if (pendingDifficulty !== null){
_this.sendDifficulty(pendingDifficulty);
pendingDifficulty = null;
}
sendJson({ sendJson({
id : null, id : null,
method: "mining.notify", method: "mining.notify",
params: jobParams params: jobParams
}); });
}; };
}; };
StratumClient.prototype.__proto__ = events.EventEmitter.prototype; StratumClient.prototype.__proto__ = events.EventEmitter.prototype;