From 7dbcc4f8637bbef729ffae3fb1d5754ecfdd844e Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 6 Mar 2014 01:50:43 -0700 Subject: [PATCH] Updated readme desc --- README.md | 1 + lib/daemon.js | 2 ++ lib/index.js | 4 +++- lib/peer.js | 4 ++++ lib/pool.js | 9 ++++----- lib/stratum.js | 19 ++++++++++++++----- 6 files changed, 28 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 912df61..4817fcd 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,7 @@ var pool = Stratum.createPool({ //instanceId: 37, //Recommend not using this because a crypto-random one will be generated + "connectionTimeout": 120, //Remove workers that haven't been in contact for this many seconds /* Each pool can have as many ports for your miners to connect to as you wish. Each port can be configured to use its own pool difficulty and variable difficulty settings. varDiff is diff --git a/lib/daemon.js b/lib/daemon.js index 5ae0b28..a287eb7 100644 --- a/lib/daemon.js +++ b/lib/daemon.js @@ -39,6 +39,8 @@ function DaemonInterface(options){ return !results.error; }); callback(allOnline); + if (!allOnline) + _this.emit('connectionFailed', results); }); } diff --git a/lib/index.js b/lib/index.js index dc54b7f..4ffb015 100644 --- a/lib/index.js +++ b/lib/index.js @@ -3,7 +3,9 @@ var events = require('events'); var pool = require('./pool.js'); +exports.daemon = require('./daemon.js'); + exports.createPool = function(poolOptions, authorizeFn){ var newPool = new pool(poolOptions, authorizeFn); return newPool; -}; \ No newline at end of file +}; diff --git a/lib/peer.js b/lib/peer.js index 74f45dd..4186cc5 100644 --- a/lib/peer.js +++ b/lib/peer.js @@ -4,6 +4,10 @@ var events = require('events'); var util = require('./util.js'); + +//Example of p2p in node from TheSeven: http://paste.pm/e54.js + + var commandStringBuffer = function(s){ var buff = new Buffer(12); buff.fill(0); diff --git a/lib/pool.js b/lib/pool.js index eadea66..9d285ff 100644 --- a/lib/pool.js +++ b/lib/pool.js @@ -317,8 +317,8 @@ var pool = module.exports = function pool(options, authorizeFn){ } }); - }).on('connectionFailed', function(instance){ - emitErrorLog('system','Failed to start daemon'); + }).on('connectionFailed', function(error){ + emitErrorLog('system','Failed to connect daemon(s): ' + JSON.stringify(error)); }).on('error', function(message){ emitErrorLog('system', message); }); @@ -328,7 +328,7 @@ var pool = module.exports = function pool(options, authorizeFn){ function StartStratumServer(){ - _this.stratumServer = new stratum.Server(options.ports, authorizeFn); + _this.stratumServer = new stratum.Server(options.ports, options.connectionTimeout, authorizeFn); _this.stratumServer.on('started', function(){ emitLog('system','Stratum server started on port(s): ' + Object.keys(options.ports).join(', ')); _this.emit('started'); @@ -480,5 +480,4 @@ var pool = module.exports = function pool(options, authorizeFn){ } }; -pool.prototype.__proto__ = events.EventEmitter.prototype; -pool.daemon = daemon; \ No newline at end of file +pool.prototype.__proto__ = events.EventEmitter.prototype; \ No newline at end of file diff --git a/lib/stratum.js b/lib/stratum.js index 6342548..6774697 100644 --- a/lib/stratum.js +++ b/lib/stratum.js @@ -30,6 +30,9 @@ var StratumClient = function(options){ var _this = this; + this.lastActivity = Date.now(); + + (function init(){ setupSocket(); })(); @@ -43,15 +46,16 @@ var StratumClient = function(options){ handleAuthorize(message, true /*reply to socket*/); break; case 'mining.submit': + _this.lastActivity = Date.now(); handleSubmit(message); break; - case 'mining.get_transactions': + /*case 'mining.get_transactions': sendJson({ id : null, result : [], error : true }); - break; + break;*/ default: _this.emit('unknownStratumMethod', message); break; @@ -254,7 +258,7 @@ StratumClient.prototype.__proto__ = events.EventEmitter.prototype; * - 'client.disconnected'(StratumClientInstance) - when a miner disconnects. Be aware that the socket cannot be used anymore. * - 'started' - when the server is up and running **/ -var StratumServer = exports.Server = function StratumServer(ports, authorizeFn){ +var StratumServer = exports.Server = function StratumServer(ports, connectionTimeout, authorizeFn){ //private members @@ -300,8 +304,13 @@ var StratumServer = exports.Server = function StratumServer(ports, authorizeFn){ for (var clientId in stratumClients) { // if a client gets disconnected WHILE doing this loop a crash might happen. // 'm not sure if that can ever happn but an if here doesn't hurt! - if (typeof(stratumClients[clientId]) !== 'undefined') { - stratumClients[clientId].sendMiningJob(jobParams); + var client = stratumClients[clientId]; + if (typeof(client) !== 'undefined') { + if (Date.now() - client.lastActivity > connectionTimeout){ + client.socket.end(); + return; + } + client.sendMiningJob(jobParams); } } };