From 1daaaed7a6f129e78975c2445408e5235505e6bf Mon Sep 17 00:00:00 2001 From: troy Date: Mon, 9 Oct 2017 20:13:59 -0300 Subject: [PATCH] fixing false share rejection and authentication logging --- .gitignore | 10 ++++++ lib/miners_controller.js | 58 +++++++++++++++++---------------- lib/pool_connector.js | 70 ++++++++++++++++++++++++---------------- package.json | 2 +- 4 files changed, 84 insertions(+), 56 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a1432a6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +# See reference at https://github.com/github/gitignore/ for more configurations + +# dependencies built by NPM update +node_modules/ + +# IntelliJ +*.iml +*.ipr +*.iws +.idea/ diff --git a/lib/miners_controller.js b/lib/miners_controller.js index 365c537..8206ac1 100644 --- a/lib/miners_controller.js +++ b/lib/miners_controller.js @@ -6,7 +6,7 @@ var logger = require('./stratum_logger.js') var info = require('../package.json') const chalk = require('chalk'); - + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - var MinersController = function(poolproxy) { this.miners = new Map(); @@ -86,8 +86,8 @@ Miner.prototype.onData = function(obj) { if(this.ctrl.proxy.status != "ready"){ this.Error("Proxy not authorised yet"); }else{ - - logger.log ( this.ctrl.miners.size + " peer(s) mining on this proxy"); + + logger.log ( this.ctrl.miners.size + " peer(s) mining on this proxy"); this.send({id:this.lastReqId,result:true,error:null}); this.send({id:null,method:"mining.set_target", params:this.ctrl.proxy.last_target}); this.send({id:null,method:"mining.notify", params:this.ctrl.proxy.last_notif}); @@ -100,13 +100,13 @@ Miner.prototype.onData = function(obj) { } logger.log ( "Submit work for " + this.name); this.ctrl.proxy.submit(this.id,obj); - } + } } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Miner.prototype.Error = function(msg) { logger.err("Miner failure on state " + this.status, msg); this.send({id:this.lastReqId,error:msg} ); -} +} // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Miner.prototype.onEnd = function() { @@ -118,30 +118,34 @@ Miner.prototype.onEnd = function() { // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Miner.prototype.send = function(data) { logger.dbg('[MINER<'+this.id+'>:OUT] ',data); - if(data.method && data.method == 'mining.notify' ){ - logger.dbg('[MINER<'+this.id+'>] Notify ' + this.name + ' for new work'); - }else{ - if(this.status == "subscribe"){ - data.id = this.lastReqId; - logger.dbg('[MINER<'+this.id+'>] Subscribe result',data); - }else if(this.status == "authorize"){ - data.id = this.lastReqId; - //authorized = data.result; - logger.dbg('[MINER<'+this.id+'>] Authorize result',data); - //logger.dbg('[MINER<'+this.id+'>] Farm Authorize ',authorized); - }else if(this.status == "submit"){ - data.id = this.lastReqId; - logger.dbg('[MINER<'+this.id+'>] Submit result',data); - if(data.result){ - logger.log ( "Work from " + this.name + " "+chalk.green("accepted")); - }else{ - logger.log ( "Work from " + this.name + " "+chalk.red("rejected")); - } - } + if (data.method && data.method == 'mining.notify' ) { + logger.dbg('[MINER<'+this.id+'>] Notify ' + this.name + ' for new work'); + } else if (this.status == "subscribe") { + data.id = this.lastReqId; + logger.dbg('[MINER<'+this.id+'>] Subscribe result',data); + } else if (this.status == "authorize") { + data.id = this.lastReqId; + //authorized = data.result; + logger.dbg('[MINER<'+this.id+'>] Authorize result',data); + //logger.dbg('[MINER<'+this.id+'>] Farm Authorize ',authorized); + } else if (this.status == "submit") { + data.id = this.lastReqId; + logger.dbg('[MINER<'+this.id+'>] Submit result',data); + if (data.result) { + logger.log("Work from " + this.name + " " + chalk.green("accepted")); + } else if (data.method && data.method === 'mining.set_target') { + // method is returned from some pools and not others + logger.log("Setting target for " + this.name + " " + chalk.blue(data.params[0])); + } else if (data.error && data.error.length > 1) { + // error is returned from some pools and not others + logger.log("Work from " + this.name + " " + chalk.red("rejected: " + data.error[1])); + } else { + logger.log("Work from " + this.name + " " + chalk.red("rejected")); + } } - if(this.connection && !(this.connection.destroyed)){ + if (this.connection && !(this.connection.destroyed)) { this.connection.write(JSON.stringify(data) + '\n'); - }else{ + } else { logger.err(this.name + " offline... removing from pool"); this.ctrl.removeMiner(this.id); } diff --git a/lib/pool_connector.js b/lib/pool_connector.js index 625645b..8f74534 100644 --- a/lib/pool_connector.js +++ b/lib/pool_connector.js @@ -5,9 +5,6 @@ var ldj = require('ldjson-stream'); var logger = require('./stratum_logger.js'); var minersController = require('./miners_controller.js'); - - - // ---------------------------------------------------------------------------- var PoolConnector = function(restartCallback){ @@ -17,7 +14,7 @@ var PoolConnector = function(restartCallback){ this.last_target = null; this.last_notif = null; - this.poollist = []; + this.poollist = []; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - this.onConnect = function(connect){ @@ -46,41 +43,53 @@ var PoolConnector = function(restartCallback){ }else{ this.sessionId = obj.result[1]; logger.log('Stratum session id : ' + this.sessionId); - var auth = {id:2,method:"mining.authorize",params:[config.wallet + "."+config.proxy_name,"x"]}; + var auth = { + id: 2, + method: "mining.authorize", + params: [ + config.wallet + "."+config.proxy_name,"x" + ] + }; logger.log('Authorizing mining wallet '+ config.wallet); this.status = "authorizing"; - this.send(auth); + this.send(auth); } } else if (this.status == 'authorizing' && (obj.id == 2)) { // Authorization (internal) - if(obj.error){ + if (obj.error) { this.error(obj.error); - }else{ + } else if (obj.result) { this.status = 'ready'; - logger.log(config.wallet+' Authorized'); - var xtra = {id:3,method:"mining.extranonce.subscribe",params:[]} + logger.log(config.wallet + ' Authorized'); + var xtra = { + id: 3, + method: "mining.extranonce.subscribe", + params:[] + } this.send(xtra); + } else { + this.error('Failed to authorize: ' + config.wallet); } - } else{ + } else { if (obj.method === 'mining.notify' || obj.method === 'mining.set_target') { - // Broadcast to all miners + // Broadcast to all miners this.miners.broadcastToMiners(obj); - if(obj.method === 'mining.notify'){ - logger.log('New work : ' + obj.params[0]); + if (obj.method === 'mining.notify') { + logger.log('New work : ' + obj.params[0]); this.last_notif = obj.params; - }else if(obj.method === 'mining.set_target'){ + } else if (obj.method === 'mining.set_target') { this.last_target = obj.params; } } else { //Forward message to the correct miner - this.miners.sendToMiner(obj.id,obj); + this.miners.sendToMiner(obj.id, obj); } } logger.dbg('====================================') }; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - this.onEnd = function() { - logger.log('Pool closed the connection... Restart proxy'); - setTimeout( () => { restartCallback(); },1000); -}; + logger.log('Pool closed the connection...'); + this.reconnect(); + }; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - this.send = function(obj){ logger.dbg('[POOL:OUT] ' + obj.method + ': ' + JSON.stringify(obj)); @@ -93,24 +102,24 @@ var PoolConnector = function(restartCallback){ this.poolSocket.write(JSON.stringify(obj) + '\n'); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - this.Error = function(msg) { - logger.err("Proxy failure on state " + this.status, msg); + this.error = function(msg) { + logger.err("Proxy failure on state " + this.status + ' ' + msg); this.poolSocket.end(); } - + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - this.poollist.push( config.pool ); if(config.pool_failover_enabled){ this.poollist = this.poollist.concat( config.pool_failover ); - } + } - this.connect = function(){ + this.connect = function(){ if(this.poollist.length > 0 ){ // Create connection with pool var pool = this.poollist.shift(); logger.log('Connecting to ' + pool.host +":"+pool.port ); - + this.poolSocket = net.createConnection({ port: pool.port, host: pool.host @@ -123,14 +132,19 @@ var PoolConnector = function(restartCallback){ }); this.poolSocket.on('end',()=>{this.onEnd();}); }else{ - logger.err('All connections failed... Stratum will restart after 10 sec'); - setTimeout( () => { restartCallback(); },10000); + logger.err('All connections failed...'); + this.reconnect(); } } + this.reconnect = function () { + logger.err("Waiting 10 seconds before attempting to restart the Stratum Proxy"); + setTimeout( () => { restartCallback(); },10000); + } + this.connect(); } - + // ---------------------------------------------------------------------------- PoolConnector.prototype.destroy = function(){ if(this.poolSocket){ diff --git a/package.json b/package.json index 4e8c5ae..ee34e66 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "zecproxy", - "version": "1.0.2", + "version": "1.0.3", "description": "Zcash Stratum Proxy", "main": "proxy.js", "scripts": {},