Skip to content

Commit

Permalink
fixing false share rejection and authentication logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ztimin committed Oct 9, 2017
1 parent 581580a commit 1daaaed
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 56 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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/
58 changes: 31 additions & 27 deletions lib/miners_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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});
Expand All @@ -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() {
Expand All @@ -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);
}
Expand Down
70 changes: 42 additions & 28 deletions lib/pool_connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){

Expand All @@ -17,7 +14,7 @@ var PoolConnector = function(restartCallback){

this.last_target = null;
this.last_notif = null;
this.poollist = [];
this.poollist = [];

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
this.onConnect = function(connect){
Expand Down Expand Up @@ -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));
Expand All @@ -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
Expand All @@ -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){
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zecproxy",
"version": "1.0.2",
"version": "1.0.3",
"description": "Zcash Stratum Proxy",
"main": "proxy.js",
"scripts": {},
Expand Down

0 comments on commit 1daaaed

Please sign in to comment.