Skip to content

Commit 70b8cdf

Browse files
author
Alexander Blair
committed
Update to the proxy to allow --workers=<number> Also fixes bug with miners not getting removed from memory.
1 parent 74f5ba9 commit 70b8cdf

File tree

3 files changed

+46
-15
lines changed

3 files changed

+46
-15
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"debug": "2.5.1",
2828
"express": "4.14.0",
2929
"jsonwebtoken": "^7.2.1",
30+
"minimist": "1.2.0",
3031
"moment": "2.17.1",
3132
"range": "0.0.3",
3233
"request": "^2.79.0",

proxy.js

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const uuidV4 = require('uuid/v4');
88
const support = require('./lib/support.js')();
99
global.config = require('./config.json');
1010

11+
1112
/*
1213
General file design/where to find things.
1314
@@ -588,14 +589,18 @@ function enumerateWorkerStats(){
588589
for (let workerID in activeWorkers[poolID]){
589590
if (activeWorkers[poolID].hasOwnProperty(workerID)) {
590591
let workerData = activeWorkers[poolID][workerID];
591-
if (workerData.lastContact < ((Math.floor((Date.now())/1000) - 120))){
592+
if (typeof workerData !== 'undefined') {
593+
if (workerData.lastContact < ((Math.floor((Date.now())/1000) - 120))){
594+
delete activeWorkers[poolID][workerID];
595+
continue;
596+
}
597+
stats.miners += 1;
598+
stats.hashes += workerData.hashes;
599+
stats.hashRate += workerData.avgSpeed;
600+
stats.diff += workerData.diff;
601+
} else {
592602
delete activeWorkers[poolID][workerID];
593-
continue;
594603
}
595-
stats.miners += 1;
596-
stats.hashes += workerData.hashes;
597-
stats.hashRate += workerData.avgSpeed;
598-
stats.diff += workerData.diff;
599604
}
600605
}
601606
global_stats.miners += stats.miners;
@@ -716,7 +721,7 @@ function handleNewBlockTemplate(blockTemplate, hostname){
716721
}
717722

718723
// Miner Definition
719-
function Miner(id, params, ip, pushMessage, portData) {
724+
function Miner(id, params, ip, pushMessage, portData, minerSocket) {
720725
// Arguments
721726
// minerId, params, ip, pushMessage, portData
722727
// Username Layout - <address in BTC or XMR>.<Difficulty>
@@ -733,6 +738,7 @@ function Miner(id, params, ip, pushMessage, portData) {
733738
this.password = params.pass; // Documentation purposes only.
734739
this.agent = params.agent; // Documentation purposes only.
735740
this.ip = ip; // Documentation purposes only.
741+
this.socket = minerSocket;
736742
this.messageSender = pushMessage;
737743
this.error = "";
738744
this.valid_miner = true;
@@ -777,6 +783,10 @@ function Miner(id, params, ip, pushMessage, portData) {
777783
this.cachedJob = null;
778784

779785
this.minerStats = function(){
786+
if (this.socket.destroyed){
787+
delete activeMiners[this.id];
788+
return;
789+
}
780790
return {
781791
shares: this.shares,
782792
blocks: this.blocks,
@@ -823,7 +833,7 @@ function Miner(id, params, ip, pushMessage, portData) {
823833
}
824834

825835
// Slave Functions
826-
function handleMinerData(method, params, ip, portData, sendReply, pushMessage) {
836+
function handleMinerData(method, params, ip, portData, sendReply, pushMessage, minerSocket) {
827837
/*
828838
Deals with handling the data from miners in a sane-ish fashion.
829839
*/
@@ -838,7 +848,7 @@ function handleMinerData(method, params, ip, portData, sendReply, pushMessage) {
838848
case 'login':
839849
let difficulty = portData.difficulty;
840850
let minerId = uuidV4();
841-
miner = new Miner(minerId, params, ip, pushMessage, portData);
851+
miner = new Miner(minerId, params, ip, pushMessage, portData, minerSocket);
842852
if (!miner.valid_miner) {
843853
console.log("Invalid miner, disconnecting due to: " + miner.error);
844854
sendReply(miner.error);
@@ -851,7 +861,7 @@ function handleMinerData(method, params, ip, portData, sendReply, pushMessage) {
851861
job: miner.getJob(miner, activePools[miner.pool].activeBlocktemplate),
852862
status: 'OK'
853863
});
854-
break;
864+
return minerId;
855865
case 'getjob':
856866
if (!miner) {
857867
sendReply('Unauthenticated');
@@ -946,7 +956,7 @@ function activatePorts() {
946956
if (activePorts.indexOf(portData.port) !== -1) {
947957
return;
948958
}
949-
let handleMessage = function (socket, jsonData, pushMessage) {
959+
let handleMessage = function (socket, jsonData, pushMessage, minerSocket) {
950960
if (!jsonData.id) {
951961
console.warn('Miner RPC request missing RPC id');
952962
return;
@@ -973,8 +983,8 @@ function activatePorts() {
973983
debug.miners(`Data sent to miner (sendReply): ${sendData}`);
974984
socket.write(sendData);
975985
};
976-
handleMinerData(jsonData.method, jsonData.params, socket.remoteAddress, portData, sendReply, pushMessage);
977-
};
986+
handleMinerData(jsonData.method, jsonData.params, socket.remoteAddress, portData, sendReply, pushMessage, minerSocket);
987+
};
978988

979989
function socketConn(socket) {
980990
socket.setKeepAlive(true);
@@ -1031,18 +1041,22 @@ function activatePorts() {
10311041
socket.destroy();
10321042
break;
10331043
}
1034-
handleMessage(socket, jsonData, pushMessage);
1044+
handleMessage(socket, jsonData, pushMessage, socket);
10351045
}
10361046
dataBuffer = incomplete;
10371047
}
10381048
}).on('error', function (err) {
10391049
if (err.code !== 'ECONNRESET') {
10401050
console.warn(global.threadName + "Socket Error from " + socket.remoteAddress + " " + err);
10411051
}
1052+
socket.end();
1053+
socket.destroy();
10421054
}).on('close', function () {
10431055
pushMessage = function () {
10441056
};
10451057
debug.miners('Miner disconnected via standard close');
1058+
socket.end();
1059+
socket.destroy();
10461060
});
10471061
}
10481062

@@ -1097,7 +1111,18 @@ function checkActivePools() {
10971111
// System Init
10981112

10991113
if (cluster.isMaster) {
1100-
let numWorkers = require('os').cpus().length;
1114+
let numWorkers;
1115+
try {
1116+
let argv = require('minimist')(process.argv.slice(2));
1117+
if (typeof argv.workers !== 'undefined') {
1118+
numWorkers = Number(argv.workers);
1119+
} else {
1120+
numWorkers = require('os').cpus().length;
1121+
}
1122+
} catch (err) {
1123+
console.error(`Unable to set the number of workers via arguments. Make sure to run npm install!`);
1124+
numWorkers = require('os').cpus().length;
1125+
}
11011126
global.threadName = 'Master ';
11021127
console.log('Cluster master setting up ' + numWorkers + ' workers...');
11031128
cluster.on('message', masterMessageHandler);

update.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
git checkout .
2+
git pull
3+
npm install
4+
echo "Proxy updated! Please go ahead and restart with the correct pm2 command"
5+
echo "This is usually pm2 restart proxy, however, you can use pm2 list to check for your exact proxy command"

0 commit comments

Comments
 (0)