Added some comments,

Fixed the code that was checking for new blocks.
Removed jobId from the constructor arguments of BlockTemplate and created a setter Instead.
Added "start" emit event from "Pool" that is going to be used by the polling block submit handler
This commit is contained in:
Andrea Baccega 2014-01-12 15:08:58 +01:00
parent cb1e40d8eb
commit d26e00fee2
5 changed files with 76 additions and 40 deletions

View File

@ -6,7 +6,7 @@ var transactions = require('./transactions.js');
var util = require('./util.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 //private members
@ -29,7 +29,7 @@ var BlockTemplate = module.exports = function BlockTemplate(jobId, rpcData, publ
//public members //public members
this.rpcData = rpcData; this.rpcData = rpcData;
this.jobId = jobId; this.jobId = null;
this.target = util.bignumFromBits(rpcData.bits); this.target = util.bignumFromBits(rpcData.bits);
this.prevHashReversed = util.reverseByteOrder(new Buffer(rpcData.previousblockhash, 'hex')).toString('hex'); this.prevHashReversed = util.reverseByteOrder(new Buffer(rpcData.previousblockhash, 'hex')).toString('hex');
this.transactionData = Buffer.concat(rpcData.transactions.map(function(tx){ this.transactionData = Buffer.concat(rpcData.transactions.map(function(tx){
@ -44,12 +44,11 @@ var BlockTemplate = module.exports = function BlockTemplate(jobId, rpcData, publ
extraNoncePlaceholder extraNoncePlaceholder
); );
/** this.setJobId = function (jobId) {
* Checks if the rpcData provided as argument contains the same previousblockhash of the current job. console.log("SETJOBID "+jobId);
**/ this.jobId = jobId;
this.isRPCDataFromSameBlock = function(rpcData) {
return this.rpcData.previousblockhash == rpcData.previousblockhash;
} }
this.serializeCoinbase = function(extraNonce1, extraNonce2){ this.serializeCoinbase = function(extraNonce1, extraNonce2){
return Buffer.concat([ return Buffer.concat([
this.generationTransaction.coinbase[0], this.generationTransaction.coinbase[0],

26
init.js
View File

@ -35,6 +35,20 @@ fs.readdir(confFolder, function(err, files){
console.log('Starting pool for ' + coin.options.name); console.log('Starting pool for ' + coin.options.name);
coin.pool = new pool(coin); coin.pool = new pool(coin);
coins.push(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() {}); 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
);
} }

View File

@ -36,6 +36,10 @@ var JobCounter = function(){
counter++; counter++;
if (counter % 0xffff === 0) if (counter % 0xffff === 0)
counter = 1; counter = 1;
return this.cur();
};
this.cur = function () {
return counter.toString(16); return counter.toString(16);
}; };
}; };
@ -50,15 +54,19 @@ var JobManager = module.exports = function JobManager(options){
var jobs = {}; 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){ function CheckNewIfNewBlock(blockTemplate){
var newBlock = true; var newBlock = true;
for(var job in jobs){ for(var job in jobs){
if (jobs[job].rpcData.previousblockhash === blockTemplate.rpcData.previousblockhash) if (jobs[job].rpcData.previousblockhash === blockTemplate.rpcData.previousblockhash) {
newBlock = false; newBlock = false;
}
} }
if (newBlock) return newBlock;
_this.emit('newBlock', blockTemplate);
} }
var diffDividend = (function(){ var diffDividend = (function(){
@ -102,9 +110,14 @@ var JobManager = module.exports = function JobManager(options){
this.currentJob; this.currentJob;
this.newTemplate = function(rpcData, publicKey){ this.newTemplate = function(rpcData, publicKey){
this.currentJob = new blockTemplate(jobCounter.next(), rpcData, publicKey, _this.extraNoncePlaceholder); var tmpBlockTemplate = new blockTemplate(rpcData, publicKey, _this.extraNoncePlaceholder);
jobs[this.currentJob.jobId] = this.currentJob; if ( CheckNewIfNewBlock(tmpBlockTemplate) ) {
CheckNewIfNewBlock(this.currentJob); 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){ this.processShare = function(jobId, difficulty, extraNonce1, extraNonce2, nTime, nonce){

46
pool.js
View File

@ -9,21 +9,27 @@ var stratum = require('./stratum.js');
var jobManager = require('./jobManager.js'); var jobManager = require('./jobManager.js');
var util = require('./util.js'); var util = require('./util.js');
console.log("BOIADIO");
var pool = module.exports = function pool(coin){ var pool = module.exports = function pool(coin){
var _this = this; var _this = this;
var publicKeyBuffer; var publicKeyBuffer;
var boiadio = this;
this.jobManager = new jobManager({ this.jobManager = new jobManager({
algorithm: coin.options.algorithm, algorithm: coin.options.algorithm,
address: coin.options.address address: coin.options.address
}); });
this.jobManager.on('newBlock', function(blockTemplate){ this.jobManager.on('newBlock', function(blockTemplate){
_this.stratumServer.broadcastMiningJobs(blockTemplate.getJobParams()); console.log("NEWBLOCK ");
}).on('blockFound', function(blockHex){ 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) { if (coin.options.hasSubmitMethod) {
_this.daemon.cmd('submitblock', _this.daemon.cmd('submitblock',
[blockHex], [blockHex],
@ -89,7 +95,7 @@ var pool = module.exports = function pool(coin){
_this.jobManager.newTemplate(results.rpcTemplate, publicKeyBuffer); _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); 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 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); console.log('Stratum server started on port ' + coin.options.stratumPort + ' for ' + coin.options.name);
}).on('client', function(client){ }).on('client', function(client){
client.on('subscription', function(params, resultCallback){ client.on('subscription', function(params, resultCallback){
@ -115,7 +122,13 @@ var pool = module.exports = function pool(coin){
extraNonce2Size extraNonce2Size
); );
this.sendDifficulty(coin.options.difficulty); 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){ }).on('authorize', function(params, resultCallback){
resultCallback(null, true); resultCallback(null, true);
}).on('submit', function(params, resultCallback){ }).on('submit', function(params, resultCallback){
@ -137,7 +150,6 @@ var pool = module.exports = function pool(coin){
} }
function GetBlockTemplate(callback){ function GetBlockTemplate(callback){
console.log("getBlockTemplate");
_this.daemon.cmd('getblocktemplate', _this.daemon.cmd('getblocktemplate',
[{"capabilities": [ "coinbasetxn", "workid", "coinbase/append" ]}], [{"capabilities": [ "coinbasetxn", "workid", "coinbase/append" ]}],
function(error, result){ 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() { this.processBlockPolling = function() {
GetBlockTemplate(function(error, result) { 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.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){ this.processBlockNotify = function(blockHash){
if (blockHash !== _this.jobManager.currentJob.rpcData.previousblockhash){ if (blockHash !== _this.jobManager.currentJob.rpcData.previousblockhash){
GetBlockTemplate(function(error, result){ GetBlockTemplate(function(error, result){
if (error){ if (error){
console.log('Error getting block template for ' + coin.options.name); console.error('[processBlockNotify] Error getting block template for ' + coin.options.name);
return; return;
} }
_this.jobManager.newTemplate(result, publicKeyBuffer); _this.jobManager.newTemplate(result, publicKeyBuffer);

View File

@ -214,7 +214,7 @@ var StratumServer = exports.Server = function StratumServer(options){
//public members //public members
this.broadcastMiningJobs = function(jobParams){ this.broadcastMiningJobs = function(jobParams){
for (var clientId in _stratumClients){ for (var clientId in stratumClients){
stratumClients[clientId].sendMiningJob(jobParams) stratumClients[clientId].sendMiningJob(jobParams)
} }
}; };