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,
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){
if ( typeof(_this.stratumServer ) === 'undefined') {
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);
_this.stratumServer = new stratum.Server({
port: coin.options.stratumPort
port : coin.options.stratumPort,
authorizeFn : authorizeFn,
});
_this.stratumServer.on('started', function(){
_this.emit('started');
@ -127,8 +145,6 @@ var pool = module.exports = function pool(coin){
} else {
this.sendMiningJob(_this.jobManager.currentJob.getJobParams());
}
}).on('authorize', function(params, resultCallback){
resultCallback(null, true);
}).on('submit', function(params, resultCallback){
var result =_this.jobManager.processShare(
params.jobId,
@ -147,14 +163,14 @@ var pool = module.exports = function pool(coin){
} else {
resultCallback(null, true);
_this.emit('share', true, {
blockHeaderHex : result.headerHEX,
workerName : params.name,
jobId : params.jobId,
clientDifficulty : client.difficulty,
extraNonce1 : client.extraNonce1,
extraNonce2 : params.extraNonce2,
nTime : params.nTime,
nonce : params.nonce
blockHeaderHex : result.headerHEX,
workerName : params.name,
jobId : params.jobId,
clientDifficulty : client.difficulty,
extraNonce1 : client.extraNonce1,
extraNonce2 : params.extraNonce2,
nTime : params.nTime,
nonce : params.nonce
});
}

View File

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