diff --git a/example/init.js b/example/init.js index 8f37aba..334c066 100644 --- a/example/init.js +++ b/example/init.js @@ -24,6 +24,18 @@ var 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){ if (err) throw err; files.forEach(function(file){ @@ -35,7 +47,7 @@ fs.readdir(confFolder, function(err, files){ var coin = new Coin(coinJson); 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); coins.push(coin); diff --git a/index.js b/index.js index 3890b71..1253474 100644 --- a/index.js +++ b/index.js @@ -12,7 +12,7 @@ var util = require('./libs/util.js'); * - '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) */ -var pool = module.exports = function pool(coin){ +var pool = module.exports = function pool(coin, authFn){ var _this = this; var publicKeyBuffer; @@ -23,17 +23,15 @@ var pool = module.exports = function pool(coin){ address: coin.options.address }); - + + // Worker authorizer fn. var authorizeFn; - if ( typeof (coin.authorizeFn) === 'function' ) { - authorizeFn = coin.authorizeFn; + if ( typeof (authFn) === 'function' ) { + authorizeFn = authFn; } else { - authorizeFn = function (ip, workerName, password, cback) { - // Default implementation just returns true - console.log("Athorize ["+ip+"] "+workerName+":"+password); - cback(null, true, true); - }; + // probably undefined. + process.exit("ERROR: an authorize Function is needed.") } @@ -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); }).on('client', function(client){ client.on('subscription', function(params, resultCallback){ + var extraNonce = _this.jobManager.extraNonceCounter.next(); var extraNonce2Size = _this.jobManager.extraNonce2Size; resultCallback(null, extraNonce, extraNonce2Size ); - this.sendDifficulty(coin.options.difficulty); - if (typeof(_this.jobManager.currentJob) === 'undefined') { - console.warn("[subscription] Cannot send job to client. No jobs in jobManager!"); - } else { - this.sendMiningJob(_this.jobManager.currentJob.getJobParams()); - } + var clientThis = this; + + //if (clientThis.authorized) { + clientThis.sendMiningJob(_this.jobManager.currentJob.getJobParams()); + //} + }).on('submit', function(params, resultCallback){ var result =_this.jobManager.processShare( params.jobId, diff --git a/libs/stratum.js b/libs/stratum.js index 7cb932f..5eeeab6 100644 --- a/libs/stratum.js +++ b/libs/stratum.js @@ -53,6 +53,9 @@ var StratumClient = function(options){ } function handleSubscribe(message){ + if (! _this._authorized ) { + _this.requestedSubscriptionBeforeAuth = true; + } _this.emit('subscription', {}, function(error, extraNonce1, extraNonce2Size){ @@ -79,9 +82,10 @@ var StratumClient = function(options){ } function handleAuthorize(message){ - var workerName = message.params[0]; - var workerPass = message.params[1]; - options.authorizeFn(options.socket.address().address, workerName, workerPass, function(err, authorized, shouldCloseSocket) { + this.workerIP = options.socket.address().address; + this.workerName = message.params[0]; + this.workerPass = message.params[1]; + options.authorizeFn(this.workerIP, this.workerName, this.workerPass, function(err, authorized, shouldCloseSocket, difficulty) { _this.authorized = ( ! err && authorized ); sendJson({ id : message.id, @@ -93,6 +97,29 @@ var StratumClient = function(options){ if (typeof(shouldCloseSocket) === 'boolean' && shouldCloseSocket) { 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.difficulty = difficulty; + console.log("SENDING DIFFICULTY "+difficulty); sendJson({ id : null, method: "mining.set_difficulty",