Added difficulty parameter to the authorizeFn

This commit is contained in:
Andrea Baccega 2014-01-12 23:43:02 +01:00
parent 6472baacc2
commit 475dab8aab
3 changed files with 58 additions and 19 deletions

View File

@ -24,6 +24,18 @@ var coins = [];
var confFolder = 'coins'; var confFolder = 'coins';
var authorizeFN = function (ip, workerName, password, cback) {
// Default implementation just returns true
console.log("Athorize ["+ip+"] "+workerName+":"+password);
cback(
null, // error
true, // authorized?
false, // should disconnect user?
16 // difficulty
);
}
fs.readdir(confFolder, function(err, files){ fs.readdir(confFolder, function(err, files){
if (err) throw err; if (err) throw err;
files.forEach(function(file){ files.forEach(function(file){
@ -35,7 +47,7 @@ fs.readdir(confFolder, function(err, files){
var coin = new Coin(coinJson); var coin = new Coin(coinJson);
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, authorizeFN );
coin.shareManager = new ShareManager(coin.pool); coin.shareManager = new ShareManager(coin.pool);
coins.push(coin); coins.push(coin);

View File

@ -12,7 +12,7 @@ var util = require('./libs/util.js');
* - 'started'() - when the pool is effectively started. * - 'started'() - when the pool is effectively started.
* - 'share'(isValid, dataObj) - In case it's valid the dataObj variable will contain (TODO) and in case it's invalid (TODO) * - 'share'(isValid, dataObj) - In case it's valid the dataObj variable will contain (TODO) and in case it's invalid (TODO)
*/ */
var pool = module.exports = function pool(coin){ var pool = module.exports = function pool(coin, authFn){
var _this = this; var _this = this;
var publicKeyBuffer; var publicKeyBuffer;
@ -24,16 +24,14 @@ var pool = module.exports = function pool(coin){
}); });
// Worker authorizer fn. // Worker authorizer fn.
var authorizeFn; var authorizeFn;
if ( typeof (coin.authorizeFn) === 'function' ) { if ( typeof (authFn) === 'function' ) {
authorizeFn = coin.authorizeFn; authorizeFn = authFn;
} else { } else {
authorizeFn = function (ip, workerName, password, cback) { // probably undefined.
// Default implementation just returns true process.exit("ERROR: an authorize Function is needed.")
console.log("Athorize ["+ip+"] "+workerName+":"+password);
cback(null, true, true);
};
} }
@ -133,18 +131,19 @@ var pool = module.exports = function pool(coin){
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){
var extraNonce = _this.jobManager.extraNonceCounter.next(); var extraNonce = _this.jobManager.extraNonceCounter.next();
var extraNonce2Size = _this.jobManager.extraNonce2Size; var extraNonce2Size = _this.jobManager.extraNonce2Size;
resultCallback(null, resultCallback(null,
extraNonce, extraNonce,
extraNonce2Size extraNonce2Size
); );
this.sendDifficulty(coin.options.difficulty); var clientThis = this;
if (typeof(_this.jobManager.currentJob) === 'undefined') {
console.warn("[subscription] Cannot send job to client. No jobs in jobManager!"); //if (clientThis.authorized) {
} else { clientThis.sendMiningJob(_this.jobManager.currentJob.getJobParams());
this.sendMiningJob(_this.jobManager.currentJob.getJobParams()); //}
}
}).on('submit', function(params, resultCallback){ }).on('submit', function(params, resultCallback){
var result =_this.jobManager.processShare( var result =_this.jobManager.processShare(
params.jobId, params.jobId,

View File

@ -53,6 +53,9 @@ var StratumClient = function(options){
} }
function handleSubscribe(message){ function handleSubscribe(message){
if (! _this._authorized ) {
_this.requestedSubscriptionBeforeAuth = true;
}
_this.emit('subscription', _this.emit('subscription',
{}, {},
function(error, extraNonce1, extraNonce2Size){ function(error, extraNonce1, extraNonce2Size){
@ -79,9 +82,10 @@ var StratumClient = function(options){
} }
function handleAuthorize(message){ function handleAuthorize(message){
var workerName = message.params[0]; this.workerIP = options.socket.address().address;
var workerPass = message.params[1]; this.workerName = message.params[0];
options.authorizeFn(options.socket.address().address, workerName, workerPass, function(err, authorized, shouldCloseSocket) { this.workerPass = message.params[1];
options.authorizeFn(this.workerIP, this.workerName, this.workerPass, function(err, authorized, shouldCloseSocket, difficulty) {
_this.authorized = ( ! err && authorized ); _this.authorized = ( ! err && authorized );
sendJson({ sendJson({
id : message.id, id : message.id,
@ -93,6 +97,29 @@ var StratumClient = function(options){
if (typeof(shouldCloseSocket) === 'boolean' && shouldCloseSocket) { if (typeof(shouldCloseSocket) === 'boolean' && shouldCloseSocket) {
options.socket.end(); options.socket.end();
} }
// Send difficulty
if (typeof(difficulty) === 'function') {
difficulty(_this.workerName, function(err, diff) {
if (err) {
console.error("Cannot set difficulty for "+_this.workernName+" error: "+JSON.stringify(err));
options.socket.end();
} else {
_this.sendDifficulty(diff);
}
});
} else if (typeof(difficulty) === 'number') {
_this.sendDifficulty(difficulty);
} else {
process.exit("Difficulty from authorizeFn callback is neither a function or a number");
}
if (_this.requestedSubscriptionBeforeAuth === true) {
delete _this.requestedSubscriptionBeforeAuth; // we do not need this anymore.
//TODO: We should send a mining job here. For now we send it before the auth has taken place but.....
}
}); });
} }
@ -180,6 +207,7 @@ var StratumClient = function(options){
this.sendDifficulty = function(difficulty){ this.sendDifficulty = function(difficulty){
_this.difficulty = difficulty; _this.difficulty = difficulty;
console.log("SENDING DIFFICULTY "+difficulty);
sendJson({ sendJson({
id : null, id : null,
method: "mining.set_difficulty", method: "mining.set_difficulty",