78 lines
2.1 KiB
JavaScript
78 lines
2.1 KiB
JavaScript
var events = require('events');
|
|
|
|
var binpack = require('binpack');
|
|
var bignum = require('bignum');
|
|
|
|
var util = require('./util.js');
|
|
var blockTemplate = require('./blockTemplate.js');
|
|
|
|
|
|
|
|
//Unique extranonce per subscriber
|
|
var ExtraNonceCounter = function(){
|
|
var instanceId = 31;
|
|
var counter = instanceId << 27;
|
|
var size = binpack.packUInt32(counter, 'big').length;
|
|
|
|
this.next = function(){
|
|
var extraNonce = binpack.packUInt32(counter++, 'big');
|
|
return extraNonce.toString('hex');
|
|
};
|
|
this.size = function(){
|
|
return size;
|
|
};
|
|
};
|
|
|
|
//Unique job per new block template
|
|
var JobCounter = function(){
|
|
var counter = 0;
|
|
|
|
this.next = function(){
|
|
counter++;
|
|
if (counter % 0xffff == 0)
|
|
counter = 1;
|
|
return counter.toString(16);
|
|
};
|
|
};
|
|
|
|
|
|
var JobManager = module.exports = function JobManager(options){
|
|
|
|
//private members
|
|
|
|
var _this = this;
|
|
var jobCounter = new JobCounter();
|
|
var jobs = {};
|
|
|
|
function CheckNewIfNewBlock(blockTemplate){
|
|
var newBlock = true;
|
|
for(var job in jobs){
|
|
if (jobs[job].rpcData.previousblockhash == blockTemplate.rpcData.previousblockhash)
|
|
newBlock = false;
|
|
}
|
|
if (newBlock)
|
|
_this.emit('newBlock', blockTemplate);
|
|
}
|
|
|
|
|
|
//public members
|
|
|
|
this.extraNonceCounter = new ExtraNonceCounter();
|
|
this.extraNoncePlaceholder = new Buffer('f000000ff111111f', 'hex');
|
|
this.extraNonce2Size = this.extraNoncePlaceholder.length - this.extraNonceCounter.size();
|
|
|
|
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);
|
|
};
|
|
this.processShare = function(jobId, difficulty, extraNonce1, extraNonce2, nTime, nonce){
|
|
|
|
var submitTime = Date.now() / 1000 | 0;
|
|
|
|
|
|
return true;
|
|
};
|
|
};
|
|
JobManager.prototype.__proto__ = events.EventEmitter.prototype; |