From b41dd968268c50990e80f214d9fcf5966f756ceb Mon Sep 17 00:00:00 2001 From: Matthew Little Date: Fri, 10 Jan 2014 18:18:23 -0500 Subject: [PATCH] Updated --- README.md | 24 +++++++++++++++++- init.js | 32 +++++++++++++++++++++--- pool.js | 72 ++++++++++++++++++++++++++++++------------------------ stratum.js | 4 ++- 4 files changed, 95 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 0d71e8e..0d8f5dc 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,29 @@ Requirements Installation ------------ -* .... +* For each coin you would like to start a pool server for, create a file in the "coins" directory titled "(name of coin).json" + Example configuration for dogecoin.json + ```javascript + { + "name": "Dogecoin", + "symbol": "doge", + "algorithm": "scrypt", + "reward": "POW", + "address": "DDt79i6P3Wro3SD3HSnkRLpMgUGUGdiNhS", + "stratumPort": 3333, + "difficulty": 8, + "daemon": { + "bin": "dogecoind", + "port": 8332, + "user": "test", + "password": "test", + "blocknotify": "blockNotify.js doge %s", + "startIfOffline": true + } + } + ``` +* To start the poolserver run init.js + `node init.js` Credits diff --git a/init.js b/init.js index 80700cf..22e7be4 100644 --- a/init.js +++ b/init.js @@ -1,4 +1,6 @@ var net = require('net'); +var fs = require('fs'); +var path = require('path'); var bignum = require('bignum'); @@ -10,6 +12,10 @@ function Coin(options){ } Coin.prototype = {}; + +var coins = []; + +/* var coins = [ new Coin({ name: 'Dogecoin', @@ -28,12 +34,32 @@ var coins = [ startIfOffline: true } }) -]; +];*/ -coins.forEach(function(coin){ - coin.pool = new pool(coin); +var logRef = console.log; +console.log = function(s){ + var time = new Date().toISOString(); + logRef(time + ': ' + s); +}; +var confFolder = 'coins'; + +fs.readdir(confFolder, function(err, files){ + if (err) throw err; + files.forEach(function(file){ + var filePath = confFolder + '/' + file; + if (path.extname(filePath) !== '.json') return; + fs.readFile(filePath, {encoding: 'utf8'}, function(err, data){ + if (err) throw err; + var coinJson = JSON.parse(data) + var coin = new Coin(coinJson); + console.log('Starting pool for ' + coin.options.name); + coin.pool = new pool(coin); + coins.push(coin); + }); + + }); }); diff --git a/pool.js b/pool.js index fe956b2..4d2d560 100644 --- a/pool.js +++ b/pool.js @@ -40,7 +40,7 @@ var pool = module.exports = function pool(coin){ ); }); - + console.log('Connecting to daemon for ' + coin.options.name); this.daemon = new daemon.interface(coin.options.daemon); this.daemon.on('online', function(){ async.parallel({ @@ -89,7 +89,7 @@ var pool = module.exports = function pool(coin){ } }, function(err, results){ if (err) return; - + console.log('Connected to daemon for ' + coin.options.name); coin.options.hasSubmitMethod = results.submitMethod; publicKeyBuffer = coin.options.reward === 'POW' ? @@ -98,6 +98,8 @@ var pool = module.exports = function pool(coin){ _this.jobManager.newTemplate(results.rpcTemplate, publicKeyBuffer); + StartStatumServer(); + }); }).on('startFailed', function(){ @@ -105,36 +107,42 @@ var pool = module.exports = function pool(coin){ }); - this.stratumServer = new stratum.Server({ - port: coin.options.stratumPort - }); - this.stratumServer.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); - 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, - client.difficulty, - client.extraNonce1, - params.extraNonce2, - params.nTime, - params.nonce - ); - if (result.error){ - resultCallback(result.error); - return; - } - resultCallback(null, true); + function StartStatumServer(){ + + console.log('Stratum server starting on port ' + coin.options.stratumPort + ' for ' + coin.options.name); + this.stratumServer = new stratum.Server({ + port: coin.options.stratumPort }); - }); + this.stratumServer.on('started', function(){ + 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); + 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, + client.difficulty, + client.extraNonce1, + params.extraNonce2, + params.nTime, + params.nonce + ); + if (result.error){ + resultCallback(result.error); + return; + } + resultCallback(null, true); + }); + }); + } }; pool.prototype.__proto__ = events.EventEmitter.prototype; \ No newline at end of file diff --git a/stratum.js b/stratum.js index 4e0b5ba..17d5809 100644 --- a/stratum.js +++ b/stratum.js @@ -205,7 +205,9 @@ var StratumServer = exports.Server = function StratumServer(options){ stratumClients[subscriptionId] = client; _this.emit('client', client); }); - _socketServer.listen(options.port, function(){}); + _socketServer.listen(options.port, function(){ + _this.emit('started'); + }); })();