Removed 'authorize' emit event and created which is now replaced by an authorize function.

Added ip address to the authorize event
Added ability for the authorizeFn to ask StratumClient to close connection
This commit is contained in:
Andrea Baccega 2014-01-12 21:54:20 +01:00
parent 3a9b8b94aa
commit 6472baacc2
2 changed files with 56 additions and 34 deletions

View File

@ -22,6 +22,23 @@ var pool = module.exports = function pool(coin){
algorithm: coin.options.algorithm, algorithm: coin.options.algorithm,
address: coin.options.address address: coin.options.address
}); });
// Worker authorizer fn.
var authorizeFn;
if ( typeof (coin.authorizeFn) === 'function' ) {
authorizeFn = coin.authorizeFn;
} else {
authorizeFn = function (ip, workerName, password, cback) {
// Default implementation just returns true
console.log("Athorize ["+ip+"] "+workerName+":"+password);
cback(null, true, true);
};
}
this.jobManager.on('newBlock', function(blockTemplate){ this.jobManager.on('newBlock', function(blockTemplate){
if ( typeof(_this.stratumServer ) === 'undefined') { if ( typeof(_this.stratumServer ) === 'undefined') {
console.warn("Stratum server still not started! cannot broadcast block!"); console.warn("Stratum server still not started! cannot broadcast block!");
@ -108,7 +125,8 @@ var pool = module.exports = function pool(coin){
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,
authorizeFn : authorizeFn,
}); });
_this.stratumServer.on('started', function(){ _this.stratumServer.on('started', function(){
_this.emit('started'); _this.emit('started');
@ -127,8 +145,6 @@ var pool = module.exports = function pool(coin){
} else { } else {
this.sendMiningJob(_this.jobManager.currentJob.getJobParams()); this.sendMiningJob(_this.jobManager.currentJob.getJobParams());
} }
}).on('authorize', function(params, resultCallback){
resultCallback(null, true);
}).on('submit', function(params, resultCallback){ }).on('submit', function(params, resultCallback){
var result =_this.jobManager.processShare( var result =_this.jobManager.processShare(
params.jobId, params.jobId,
@ -147,14 +163,14 @@ var pool = module.exports = function pool(coin){
} else { } else {
resultCallback(null, true); resultCallback(null, true);
_this.emit('share', true, { _this.emit('share', true, {
blockHeaderHex : result.headerHEX, blockHeaderHex : result.headerHEX,
workerName : params.name, workerName : params.name,
jobId : params.jobId, jobId : params.jobId,
clientDifficulty : client.difficulty, clientDifficulty : client.difficulty,
extraNonce1 : client.extraNonce1, extraNonce1 : client.extraNonce1,
extraNonce2 : params.extraNonce2, extraNonce2 : params.extraNonce2,
nTime : params.nTime, nTime : params.nTime,
nonce : params.nonce nonce : params.nonce
}); });
} }

View File

@ -23,7 +23,6 @@ var SubscriptionCounter = function(){
* Defining each client that connects to the stratum server. * Defining each client that connects to the stratum server.
* Emits: * Emits:
* - 'subscription'(obj, cback(error, extraNonce1, extraNonce2Size)) * - 'subscription'(obj, cback(error, extraNonce1, extraNonce2Size))
* - 'authorize'() FIX THIS
* - 'submit' FIX THIS. * - 'submit' FIX THIS.
**/ **/
var StratumClient = function(options){ var StratumClient = function(options){
@ -80,21 +79,21 @@ var StratumClient = function(options){
} }
function handleAuthorize(message){ function handleAuthorize(message){
_this.emit('authorize', var workerName = message.params[0];
{ var workerPass = message.params[1];
name: message.params[0][0], options.authorizeFn(options.socket.address().address, workerName, workerPass, function(err, authorized, shouldCloseSocket) {
password: message.params[0][1] _this.authorized = ( ! err && authorized );
}, sendJson({
function(error, result){ id : message.id,
_this.authorized = result; result : _this.authorized,
error : err
sendJson({
id : message.id,
result: result,
error : error
}); });
// If the authorizer wants us to close the socket lets do it.
if (typeof(shouldCloseSocket) === 'boolean' && shouldCloseSocket) {
options.socket.end();
} }
); });
} }
function handleSubmit(message){ function handleSubmit(message){
@ -117,17 +116,17 @@ var StratumClient = function(options){
console.log("SUBMIT "+JSON.stringify(message)); console.log("SUBMIT "+JSON.stringify(message));
_this.emit('submit', _this.emit('submit',
{ {
name : message.params[0], name : message.params[0],
jobId : message.params[1], jobId : message.params[1],
extraNonce2: message.params[2], extraNonce2 : message.params[2],
nTime : message.params[3], nTime : message.params[3],
nonce : message.params[4] nonce : message.params[4]
}, },
function(error, result){ function(error, result){
sendJson({ sendJson({
id : message.id, id : message.id,
result: result, result : result,
error : error error : error
}); });
} }
); );
@ -187,6 +186,7 @@ var StratumClient = function(options){
params: [difficulty]//[512], params: [difficulty]//[512],
}); });
}; };
this.sendMiningJob = function(jobParams){ this.sendMiningJob = function(jobParams){
sendJson({ sendJson({
id : null, id : null,
@ -211,7 +211,13 @@ var StratumServer = exports.Server = function StratumServer(options){
(function init(){ (function init(){
_socketServer = socketServer = net.createServer(function(c){ _socketServer = socketServer = net.createServer(function(c){
var subscriptionId = subscriptionCounter.next(); var subscriptionId = subscriptionCounter.next();
var client = new StratumClient({subscriptionId: subscriptionId, socket: c}); var client = new StratumClient(
{
subscriptionId : subscriptionId,
socket : c,
authorizeFn : options.authorizeFn
}
);
stratumClients[subscriptionId] = client; stratumClients[subscriptionId] = client;
_this.emit('client', client); _this.emit('client', client);
}); });