Skip to content

Commit

Permalink
Stability improved (rejected shares after net error, crashs with brow…
Browse files Browse the repository at this point in the history
…ser access, ecc...), general password added, cosmetics changes
  • Loading branch information
BScrk committed Oct 16, 2017
1 parent 7f30188 commit 3c4edac
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 48 deletions.
11 changes: 6 additions & 5 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
{
"wallet": "t1YAdYcnKR2ozADWPUvmgnDgf86gfsxQEEE",
"password" : "x",
"port": 8000,
"proxy_name" : "zecproxy",
"enable_worker_id": true,
"pool" : { "host" : "eu1-zcash.flypool.org" , "port" : 3443, "ssl" : true},
"pool" : { "host" : "zec-eu1.nanopool.org" , "port" : 6666 },
"pool_failover_enabled": true,
"pool_failover" : [ { "host" : "eu1-zcash.flypool.org" , "port" : 3333},
{ "host" : "zec-eu1.nanopool.org" , "port" : 6666 },
"pool_failover" : [ { "host" : "zec-eu2.nanopool.org" , "port" : 6666 },
{ "host" : "eu1-zcash.flypool.org" , "port" : 3443, "ssl" : true},
{ "host" : "eu1-zcash.flypool.org" , "port" : 3333, "ssl" : false},
{ "host" : "us1-zcash.flypool.org" , "port" : 3333 , "ssl" : false },
{ "host" : "cn1-zcash.flypool.org" , "port" : 3333 , "ssl" : false },
{ "host" : "asia1-zcash.flypool.org", "port" : 3333 , "ssl" : false }],
"debug" : false
}

}
22 changes: 16 additions & 6 deletions lib/miners_controller.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
var config = require('../config.json')
var net = require('net');
var util = require('util');
var ldj = require('ldjson-stream');
//var ldj = require('ldjson-stream');
var ldj = require('ndjson');
const chalk = require('chalk');

var logger = require('./stratum_logger.js');
var info = require('../package.json');
const chalk = require('chalk');


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand Down Expand Up @@ -46,22 +48,30 @@ MinersController.prototype.removeMiner = function (peer_id) {
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
MinersController.prototype.destroy = function (obj) {
this.listener.close();
this.reset();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
MinersController.prototype.reset = function (obj) {
this.miners.forEach( (miner,peer_id) => {
miner.connection.end();
this.miners.delete(peer_id);
});
}

// ----------------------------------------------------------------------------
var Miner = function(connection,controller){
logger.dbg('[MINER:NEW_MINER]');
this.id = ++controller.id;
this.ctrl = controller;
this.name = 'pending';
this.name = 'noname';
this.connection = connection;
this.status = "none";
this.reqid = 0;
this.connection.on('error', (err)=>{this.onError(err);});
this.connection.on('data', (data)=>{})
.pipe(ldj.parse()).on('data',(data)=>{
this.onData(data);
});
.pipe(ldj.parse({strict: true}))
.on('data',(data)=>{ this.onData(data); })
.on('error',(e)=>{ logger.err("invalid worker request, "+e); });
this.connection.on('end',()=>{this.onEnd();});
}

Expand Down
86 changes: 51 additions & 35 deletions lib/pool_connector.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var config = require('../config.json')
var net = require('net');
var tls = require('tls');
var ldj = require('ldjson-stream');
//var ldj = require('ldjson-stream');
var ldj = require('ndjson');
const chalk = require('chalk');

var logger = require('./stratum_logger.js');
Expand All @@ -17,11 +18,15 @@ var PoolConnector = function(restartCallback){
this.last_target = null;
this.last_notif = null;
this.poollist = [];
this.current_pool = null;

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
this.onConnect = function(connect){
logger.log("Connected to pool "+ config.pool.host + ":" + config.pool.port);
if(this.miners == null){
logger.log("Connected to pool "+ this.current_pool.host + ":" + this.current_pool.port);
if(this.miners){// Reset workers connections
logger.warn('Resetting workers...');
this.miners.reset();
}else{
this.miners = new minersController.MinersController(this);
}
// Subscribe to pool
Expand All @@ -35,7 +40,11 @@ var PoolConnector = function(restartCallback){
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
this.onError = function(error) {
logger.err('Network error, ' + error);
this.connect();
if(error.code == 'ECONNRESET'){ // connection lost, retry on same pool
this.connect(false);
}else{ // Try with next pool
this.connect(true);
}
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
this.onData = function(obj) {
Expand All @@ -46,13 +55,11 @@ var PoolConnector = function(restartCallback){
this.error(obj.error);
}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"
]
logger.log('Stratum session id ' + this.sessionId);
var auth = { id: 2
, method: "mining.authorize"
, params: [ config.wallet + "." + config.proxy_name
, config.password ]
};
logger.log('Authorizing mining wallet '+ config.wallet);
this.status = "authorizing";
Expand All @@ -63,25 +70,25 @@ var PoolConnector = function(restartCallback){
this.error(obj.error);
} else if (obj.result) {
this.status = 'ready';
logger.log(config.wallet + ' Authorized');
var xtra = {
id: 3,
method: "mining.extranonce.subscribe",
params:[]
logger.log('Mining wallet ' + config.wallet + chalk.green(' authorization granted') );
var xtra = { id: 3
, method: "mining.extranonce.subscribe"
, params:[]
}
this.send(xtra);
} else {
this.error('Failed to authorize: ' + config.wallet);
logger.log('Mining wallet ' + config.wallet + chalk.red(' authorization failed') );
}
} else {
if (obj.method === 'mining.notify' || obj.method === 'mining.set_target') {
// Broadcast to all miners
this.miners.broadcastToMiners(obj);
if (obj.method === 'mining.notify') {
logger.log('New work : ' + obj.params[0]);
logger.log('New work : ' + obj.params[3]);
this.last_notif = obj.params;
} else if (obj.method === 'mining.set_target') {
this.last_target = obj.params;
logger.log('New target : ' + obj.params[0]);
}
} else { //Forward message to the correct miner
this.miners.sendToMiner(obj.id, obj);
Expand Down Expand Up @@ -113,41 +120,43 @@ var PoolConnector = function(restartCallback){


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
this.poollist.push( config.pool );
if(config.pool_failover_enabled){
this.poollist = this.poollist.concat( config.pool_failover );
}

this.connect = function(){
this.connect = function(try_next_pool){
if(this.poollist.length > 0 ){
// Create connection with pool
var pool = this.poollist.shift();
if(try_next_pool){
this.current_pool = this.poollist.shift();
}
if(this.poolSocket){
this.poolSocket.end();
delete this.poolSocket;
this.poolSocket = null;
}
this.status = "offline";
logger.log(chalk.bold('Connecting to ' + pool.host +":"+pool.port) );
if(pool.ssl){
this.poolSocket = tls.connect(pool.port, pool.host, () => {
logger.log(chalk.bold('Connecting to ' + this.current_pool.host +":"+this.current_pool.port) );
if(this.current_pool.ssl){
this.poolSocket = tls.connect(this.current_pool.port, this.current_pool.host, () => {
if (this.poolSocket.authorized) {
// authorization successful
logger.log( chalk.bold('SSL') + ' authorization ' + chalk.green('successful') + '...');
} else {
// authorization failed
logger.log( chalk.bold('SSL') + ' authorization ' + chalk.red('failed') + '...');
this.onError('SSL authorization failed for ' + pool.host + ':' + pool.port);
this.onError('SSL authorization failed for ' + this.current_pool.host + ':' + this.current_pool.port);
}
});
}else{
this.poolSocket = net.createConnection({
port: pool.port,
host: pool.host
port: this.current_pool.port,
host: this.current_pool.host
});
}

this.poolSocket.on('connect', (connect)=>{this.onConnect(connect);});
this.poolSocket.on('error', (err)=>{this.onError(err);});
this.poolSocket.on('data', (data)=>{})
.pipe(ldj.parse()).on('data',(data)=>{
this.onData(data);
});
.pipe(ldj.parse({strict: true}))
.on('data',(data)=>{ this.onData(data); })
.on('error',(e)=>{ logger.err("invalid pool request, "+e); });
this.poolSocket.on('end',()=>{this.onEnd();});
}else{
logger.err('All connections failed...');
Expand All @@ -160,18 +169,25 @@ var PoolConnector = function(restartCallback){
setTimeout( () => { restartCallback(); },10000);
}

this.connect();
// - - - START - - - //
this.poollist.push( config.pool );
if(config.pool_failover_enabled){
this.poollist = this.poollist.concat( config.pool_failover );
}
this.connect(true);
}

// ----------------------------------------------------------------------------
PoolConnector.prototype.destroy = function(){
if(this.poolSocket){
this.poolSocket.end();
delete this.poolSocket;
this.poolSocket = null;
}
if(this.miners){
this.miners.destroy();
delete this.miners;
this.miners = null;
}
};

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "zecproxy",
"version": "1.0.4",
"version": "1.0.5",
"description": "Zcash Stratum Proxy",
"main": "proxy.js",
"scripts": {},
"dependencies": {
"chalk": "^2.1.0",
"date-and-time": "^0.5.0",
"ldjson-stream": "^1.2.1"
"ldjson-stream": "^1.2.1",
"ndjson": "^1.5.0"
}
}

0 comments on commit 3c4edac

Please sign in to comment.