Merge pull request #90 from Snipa22/xmr-node-proxy

Xmr node proxy
This commit is contained in:
Snipa22
2017-07-12 17:12:39 -07:00
committed by GitHub
3 changed files with 161 additions and 63 deletions
+2 -2
View File
@@ -271,12 +271,12 @@ function calculatePPLNSPayments(blockHeader) {
paymentData[userIdentifier].amount = paymentData[userIdentifier].amount + amountToPay; paymentData[userIdentifier].amount = paymentData[userIdentifier].amount + amountToPay;
let donations = 0; let donations = 0;
if(global.config.payout.devDonation > 0){ if(global.config.payout.devDonation > 0){
let devDonation = (feesToPay * (global.config.payout.devDonation / 100)); let devDonation = Math.floor(feesToPay * (global.config.payout.devDonation / 100));
donations += devDonation; donations += devDonation;
paymentData[global.coinFuncs.coinDevAddress].amount = paymentData[global.coinFuncs.coinDevAddress].amount + devDonation ; paymentData[global.coinFuncs.coinDevAddress].amount = paymentData[global.coinFuncs.coinDevAddress].amount + devDonation ;
} }
if(global.config.payout.poolDevDonation > 0){ if(global.config.payout.poolDevDonation > 0){
let poolDevDonation = (feesToPay * (global.config.payout.poolDevDonation / 100)); let poolDevDonation = Math.floor(feesToPay * (global.config.payout.poolDevDonation / 100));
donations += poolDevDonation; donations += poolDevDonation;
paymentData[global.coinFuncs.poolDevAddress].amount = paymentData[global.coinFuncs.poolDevAddress].amount + poolDevDonation; paymentData[global.coinFuncs.poolDevAddress].amount = paymentData[global.coinFuncs.poolDevAddress].amount + poolDevDonation;
} }
+38 -2
View File
@@ -69,7 +69,7 @@ function Coin(data){
this.getBlockTemplate = function(walletAddress, callback){ this.getBlockTemplate = function(walletAddress, callback){
global.support.rpcDaemon('getblocktemplate', { global.support.rpcDaemon('getblocktemplate', {
reserve_size: 8, reserve_size: 17,
wallet_address: walletAddress wallet_address: walletAddress
}, function(body){ }, function(body){
return callback(body); return callback(body);
@@ -102,22 +102,58 @@ function Coin(data){
}; };
this.BlockTemplate = function(template) { this.BlockTemplate = function(template) {
/*
Generating a block template is a simple thing. Ask for a boatload of information, and go from there.
Important things to consider.
The reserved space is 13 bytes long now in the following format:
Assuming that the extraNonce starts at byte 130:
|130-133|134-137|138-141|142-145|
|minerNonce/extraNonce - 4 bytes|instanceId - 4 bytes|clientPoolNonce - 4 bytes|clientNonce - 4 bytes|
This is designed to allow a single block template to be used on up to 4 billion poolSlaves (clientPoolNonce)
Each with 4 billion clients. (clientNonce)
While being unique to this particular pool thread (instanceId)
With up to 4 billion clients (minerNonce/extraNonce)
Overkill? Sure. But that's what we do here. Overkill.
*/
// Set this.blob equal to the BT blob that we get from upstream.
this.blob = template.blocktemplate_blob; this.blob = template.blocktemplate_blob;
// Set this.diff equal to the known diff for this block.
this.difficulty = template.difficulty; this.difficulty = template.difficulty;
// Set this.height equal to the known height for this block.
this.height = template.height; this.height = template.height;
// Set this.reserveOffset to the byte location of the reserved offset.
this.reserveOffset = template.reserved_offset; this.reserveOffset = template.reserved_offset;
// Set this.buffer to the binary decoded version of the BT blob.
this.buffer = new Buffer(this.blob, 'hex'); this.buffer = new Buffer(this.blob, 'hex');
// Copy the Instance ID to the reserve offset + 4 bytes deeper. Copy in 4 bytes.
instanceId.copy(this.buffer, this.reserveOffset + 4, 0, 3); instanceId.copy(this.buffer, this.reserveOffset + 4, 0, 3);
// Generate a clean, shiny new buffer.
this.previous_hash = new Buffer(32); this.previous_hash = new Buffer(32);
// Copy in bytes 7 through 39 to this.previous_hash from the current BT.
this.buffer.copy(this.previous_hash, 0, 7, 39); this.buffer.copy(this.previous_hash, 0, 7, 39);
// Reset the Nonce. - This is the per-miner/pool nonce
this.extraNonce = 0; this.extraNonce = 0;
// The clientNonceLocation is the location at which the client pools should set the nonces for each of their clients.
this.clientNonceLocation = this.reserveOffset + 12;
// The clientPoolLocation is for multi-thread/multi-server pools to handle the nonce for each of their tiers.
this.clientPoolLocation = this.reserveOffset + 8;
this.nextBlob = function () { this.nextBlob = function () {
// Write a 32 bit integer, big-endian style to the 0 byte of the reserve offset.
this.buffer.writeUInt32BE(++this.extraNonce, this.reserveOffset); this.buffer.writeUInt32BE(++this.extraNonce, this.reserveOffset);
// Convert the blob into something hashable.
return global.coinFuncs.convertBlob(this.buffer).toString('hex'); return global.coinFuncs.convertBlob(this.buffer).toString('hex');
}; };
// Make it so you can get the raw block blob out.
this.nextBlobWithChildNonce = function () {
// Write a 32 bit integer, big-endian style to the 0 byte of the reserve offset.
this.buffer.writeUInt32BE(++this.extraNonce, this.reserveOffset);
// Don't convert the blob to something hashable. You bad.
return this.buffer.toString('hex');
};
}; };
this.cryptoNight = multiHashing['cryptonight']; this.cryptoNight = multiHashing.cryptonight;
} }
+120 -58
View File
@@ -23,7 +23,7 @@ let threadName;
let minerCount = []; let minerCount = [];
let BlockTemplate = global.coinFuncs.BlockTemplate; let BlockTemplate = global.coinFuncs.BlockTemplate;
let hexMatch = new RegExp("^[0-9a-f]+$"); let hexMatch = new RegExp("^[0-9a-f]+$");
let totalShares=0, trustedShares=0, normalShares=0, invalidShares=0; let totalShares = 0, trustedShares = 0, normalShares = 0, invalidShares = 0;
Buffer.prototype.toByteArray = function () { Buffer.prototype.toByteArray = function () {
@@ -33,12 +33,12 @@ Buffer.prototype.toByteArray = function () {
if (cluster.isMaster) { if (cluster.isMaster) {
threadName = "(Master) "; threadName = "(Master) ";
setInterval(function(){ setInterval(function () {
console.log(`Processed ${trustedShares}/${normalShares}/${invalidShares}/${totalShares} Trusted/Validated/Invalid/Total shares in the last 30 seconds`); console.log(`Processed ${trustedShares}/${normalShares}/${invalidShares}/${totalShares} Trusted/Validated/Invalid/Total shares in the last 30 seconds`);
totalShares=0; totalShares = 0;
trustedShares=0; trustedShares = 0;
normalShares=0; normalShares = 0;
invalidShares=0; invalidShares = 0;
}, 30000); }, 30000);
} else { } else {
threadName = "(Worker " + cluster.worker.id + " - " + process.pid + ") "; threadName = "(Worker " + cluster.worker.id + " - " + process.pid + ") ";
@@ -59,7 +59,7 @@ function registerPool() {
[global.config.pool_id, global.config.bind_ip, true, global.config.hostname, true]); [global.config.pool_id, global.config.bind_ip, true, global.config.hostname, true]);
global.mysql.query("DELETE FROM ports WHERE pool_id = ?", [global.config.pool_id]).then(function () { global.mysql.query("DELETE FROM ports WHERE pool_id = ?", [global.config.pool_id]).then(function () {
global.config.ports.forEach(function (port) { global.config.ports.forEach(function (port) {
if ('ssl' in port && port['ssl'] === true) { if ('ssl' in port && port.ssl === true) {
global.mysql.query("INSERT INTO ports (pool_id, network_port, starting_diff, port_type, description, hidden, ip_address, ssl_port) values (?, ?, ?, ?, ?, ?, ?, 1)", global.mysql.query("INSERT INTO ports (pool_id, network_port, starting_diff, port_type, description, hidden, ip_address, ssl_port) values (?, ?, ?, ?, ?, ?, ?, 1)",
[global.config.pool_id, port.port, port.difficulty, port.portType, port.desc, port.hidden, global.config.bind_ip]); [global.config.pool_id, port.port, port.difficulty, port.portType, port.desc, port.hidden, global.config.bind_ip]);
} else { } else {
@@ -171,7 +171,7 @@ function templateUpdate(repeating) {
newBlockTemplate(rpcResponse); newBlockTemplate(rpcResponse);
} }
} }
if (repeating !== false){ if (repeating !== false) {
setTimeout(templateUpdate, 300); setTimeout(templateUpdate, 300);
} }
} }
@@ -215,9 +215,13 @@ function Miner(id, login, pass, ipAddress, startingDiff, messageSender, protoVer
let pass_split = pass.split(":"); let pass_split = pass.split(":");
this.error = ""; this.error = "";
this.identifier = pass_split[0]; this.identifier = pass_split[0];
if (agent && agent.includes('MinerGate')){ this.proxy = false;
if (agent && agent.includes('MinerGate')) {
this.identifier = "MinerGate"; this.identifier = "MinerGate";
} }
if (agent && agent.includes('xmr-node-proxy')) {
this.proxy = true;
}
this.paymentID = null; this.paymentID = null;
this.valid_miner = true; this.valid_miner = true;
this.port = port; this.port = port;
@@ -244,7 +248,7 @@ function Miner(id, login, pass, ipAddress, startingDiff, messageSender, protoVer
this.fixed_diff = false; this.fixed_diff = false;
this.difficulty = startingDiff; this.difficulty = startingDiff;
this.connectTime = Date.now(); this.connectTime = Date.now();
if (agent && agent.includes('NiceHash')){ if (agent && agent.includes('NiceHash')) {
this.fixed_diff = true; this.fixed_diff = true;
this.difficulty = global.coinFuncs.niceHashDiff; this.difficulty = global.coinFuncs.niceHashDiff;
} }
@@ -312,7 +316,7 @@ function Miner(id, login, pass, ipAddress, startingDiff, messageSender, protoVer
this.error = "Exchange addresses need payment IDs"; this.error = "Exchange addresses need payment IDs";
this.valid_miner = false; this.valid_miner = false;
} }
if (!activeBlockTemplate){ if (!activeBlockTemplate) {
this.error = "No active block template"; this.error = "No active block template";
this.valid_miner = false; this.valid_miner = false;
} }
@@ -343,7 +347,7 @@ function Miner(id, login, pass, ipAddress, startingDiff, messageSender, protoVer
}; };
} }
this.validJobs = global.support.circularBuffer(10); this.validJobs = global.support.circularBuffer(4);
this.cachedJob = null; this.cachedJob = null;
@@ -388,9 +392,15 @@ function Miner(id, login, pass, ipAddress, startingDiff, messageSender, protoVer
} }
}; };
this.updateDifficulty = function(){ this.updateDifficulty = function () {
if (this.hashes > 0) { if (this.hashes > 0) {
this.setNewDiff(Math.floor(this.hashes / (Math.floor((Date.now() - this.connectTime) / 1000))) * global.config.pool.targetTime); let newDiff = 0;
if (this.proxy) {
newDiff = Math.floor(Math.floor(this.hashes / (Math.floor((Date.now() - this.connectTime) / 1000))));
} else {
newDiff = Math.floor(this.hashes / (Math.floor((Date.now() - this.connectTime) / 1000))) * global.config.pool.targetTime;
}
this.setNewDiff(newDiff);
} else { } else {
this.updateDifficultyOld(); this.updateDifficultyOld();
} }
@@ -399,7 +409,7 @@ function Miner(id, login, pass, ipAddress, startingDiff, messageSender, protoVer
this.setNewDiff = function (difficulty) { this.setNewDiff = function (difficulty) {
this.newDiff = Math.round(difficulty); this.newDiff = Math.round(difficulty);
debug(threadName + "Difficulty: " + this.newDiff + " For: " + this.logString + " Time Average: " + this.shareTimeBuffer.average(this.lastShareTime) + " Entries: " + this.shareTimeBuffer.size() + " Sum: " + this.shareTimeBuffer.sum()); debug(threadName + "Difficulty: " + this.newDiff + " For: " + this.logString + " Time Average: " + this.shareTimeBuffer.average(this.lastShareTime) + " Entries: " + this.shareTimeBuffer.size() + " Sum: " + this.shareTimeBuffer.sum());
if (this.newDiff > global.config.pool.maxDifficulty) { if (this.newDiff > global.config.pool.maxDifficulty && !this.proxy) {
this.newDiff = global.config.pool.maxDifficulty; this.newDiff = global.config.pool.maxDifficulty;
} }
if (this.difficulty === this.newDiff) { if (this.difficulty === this.newDiff) {
@@ -409,10 +419,10 @@ function Miner(id, login, pass, ipAddress, startingDiff, messageSender, protoVer
this.newDiff = global.config.pool.minDifficulty; this.newDiff = global.config.pool.minDifficulty;
} }
debug(threadName + "Difficulty change to: " + this.newDiff + " For: " + this.logString); debug(threadName + "Difficulty change to: " + this.newDiff + " For: " + this.logString);
if (this.hashes > 0){ if (this.hashes > 0) {
debug(threadName + "Hashes: " + this.hashes + " in: " + Math.floor((Date.now() - this.connectTime)/1000) + " seconds gives: " + debug(threadName + "Hashes: " + this.hashes + " in: " + Math.floor((Date.now() - this.connectTime) / 1000) + " seconds gives: " +
Math.floor(this.hashes/(Math.floor((Date.now() - this.connectTime)/1000))) + " hashes/second or: " + Math.floor(this.hashes / (Math.floor((Date.now() - this.connectTime) / 1000))) + " hashes/second or: " +
Math.floor(this.hashes/(Math.floor((Date.now() - this.connectTime)/1000))) * global.config.pool.targetTime + " difficulty versus: " + this.newDiff); Math.floor(this.hashes / (Math.floor((Date.now() - this.connectTime) / 1000))) * global.config.pool.targetTime + " difficulty versus: " + this.newDiff);
} }
this.messageSender('job', this.getJob()); this.messageSender('job', this.getJob());
}; };
@@ -463,27 +473,60 @@ function Miner(id, login, pass, ipAddress, startingDiff, messageSender, protoVer
return this.cachedJob; return this.cachedJob;
} }
let blob = activeBlockTemplate.nextBlob(); if (!this.proxy) {
let target = this.getTargetHex(); let blob = activeBlockTemplate.nextBlob();
this.lastBlockHeight = activeBlockTemplate.height; let target = this.getTargetHex();
this.lastBlockHeight = activeBlockTemplate.height;
let newJob = { let newJob = {
id: crypto.pseudoRandomBytes(21).toString('base64'), id: crypto.pseudoRandomBytes(21).toString('base64'),
extraNonce: activeBlockTemplate.extraNonce, extraNonce: activeBlockTemplate.extraNonce,
height: activeBlockTemplate.height, height: activeBlockTemplate.height,
difficulty: this.difficulty, difficulty: this.difficulty,
diffHex: this.diffHex, diffHex: this.diffHex,
submissions: [] submissions: []
}; };
this.validJobs.enq(newJob); this.validJobs.enq(newJob);
this.cachedJob = { this.cachedJob = {
blob: blob, blob: blob,
job_id: newJob.id, job_id: newJob.id,
target: target, target: target,
id: this.id id: this.id
}; };
} else {
let blob = activeBlockTemplate.nextBlobWithChildNonce();
if (this.newDiff) {
this.difficulty = this.newDiff;
this.newDiff = null;
}
this.lastBlockHeight = activeBlockTemplate.height;
let newJob = {
id: crypto.pseudoRandomBytes(21).toString('base64'),
extraNonce: activeBlockTemplate.extraNonce,
height: activeBlockTemplate.height,
difficulty: this.difficulty,
diffHex: this.diffHex,
clientPoolLocation: activeBlockTemplate.clientPoolLocation,
clientNonceLocation: activeBlockTemplate.clientNonceLocation,
submissions: []
};
this.validJobs.enq(newJob);
this.cachedJob = {
blocktemplate_blob: blob,
difficulty: activeBlockTemplate.difficulty,
height: activeBlockTemplate.height,
reserved_offset: activeBlockTemplate.reserveOffset,
client_nonce_offset: activeBlockTemplate.clientNonceLocation,
client_pool_offset: activeBlockTemplate.clientPoolLocation,
target_diff: this.difficulty,
target_diff_hex: this.diffHex,
job_id: newJob.id,
id: this.id
};
}
return this.cachedJob; return this.cachedJob;
}; };
} }
@@ -526,10 +569,19 @@ function recordShareData(miner, job, shareDiff, blockCandidate, hashHex, shareTy
} }
function processShare(miner, job, blockTemplate, nonce, resultHash) { function processShare(miner, job, blockTemplate, params) {
let nonce = params.nonce;
let resultHash = params.result;
let template = new Buffer(blockTemplate.buffer.length); let template = new Buffer(blockTemplate.buffer.length);
blockTemplate.buffer.copy(template); if (!miner.proxy) {
template.writeUInt32BE(job.extraNonce, blockTemplate.reserveOffset); blockTemplate.buffer.copy(template);
template.writeUInt32BE(job.extraNonce, blockTemplate.reserveOffset);
} else {
blockTemplate.buffer.copy(template);
template.writeUInt32BE(job.extraNonce, blockTemplate.reserveOffset);
template.writeUInt32BE(params.poolNonce, job.clientPoolLocation);
template.writeUInt32BE(params.workerNonce, job.clientNonceLocation);
}
let shareBuffer = global.coinFuncs.constructNewBlob(template, new Buffer(nonce, 'hex')); let shareBuffer = global.coinFuncs.constructNewBlob(template, new Buffer(nonce, 'hex'));
let convertedBlob; let convertedBlob;
@@ -549,7 +601,7 @@ function processShare(miner, job, blockTemplate, nonce, resultHash) {
if (hash.toString('hex') !== resultHash) { if (hash.toString('hex') !== resultHash) {
console.error(threadName + "Bad share from miner " + miner.logString); console.error(threadName + "Bad share from miner " + miner.logString);
process.send({type: 'invalidShare'}); process.send({type: 'invalidShare'});
if (miner.incremented === false){ if (miner.incremented === false) {
miner.newDiff = miner.difficulty + 1; miner.newDiff = miner.difficulty + 1;
miner.incremented = true; miner.incremented = true;
} else { } else {
@@ -661,24 +713,34 @@ function handleMinerData(method, params, ip, portData, sendReply, pushMessage) {
global.database.storeInvalidShare(miner.invalidShareProto); global.database.storeInvalidShare(miner.invalidShareProto);
return; return;
} }
if (!miner.proxy) {
if (job.submissions.indexOf(params.nonce) !== -1) { if (job.submissions.indexOf(params.nonce) !== -1) {
console.warn(threadName + 'Duplicate share: ' + JSON.stringify(params) + ' from ' + miner.logString); console.warn(threadName + 'Duplicate share: ' + JSON.stringify(params) + ' from ' + miner.logString);
miner.checkBan(false); miner.checkBan(false);
sendReply('Duplicate share'); sendReply('Duplicate share');
global.database.storeInvalidShare(miner.invalidShareProto); global.database.storeInvalidShare(miner.invalidShareProto);
return; return;
}
job.submissions.push(params.nonce);
} else {
let nonce_test = `${params.nonce}_${params.poolNonce}_${params.workerNonce}`;
if (job.submissions.indexOf(nonce_test) !== -1) {
console.warn(threadName + 'Duplicate share: ' + JSON.stringify(params) + ' from ' + miner.logString);
miner.checkBan(false);
sendReply('Duplicate share');
global.database.storeInvalidShare(miner.invalidShareProto);
return;
}
job.submissions.push(nonce_test);
} }
job.submissions.push(params.nonce);
let blockTemplate = activeBlockTemplate.height === job.height ? activeBlockTemplate : pastBlockTemplates.toarray().filter(function (t) { let blockTemplate = activeBlockTemplate.height === job.height ? activeBlockTemplate : pastBlockTemplates.toarray().filter(function (t) {
return t.height === job.height; return t.height === job.height;
})[0]; })[0];
if (!blockTemplate) { if (!blockTemplate) {
console.warn(threadName + 'Block expired, Height: ' + job.height + ' from ' + miner.logString); console.warn(threadName + 'Block expired, Height: ' + job.height + ' from ' + miner.logString);
if (miner.incremented === false){ if (miner.incremented === false) {
miner.newDiff = miner.difficulty + 1; miner.newDiff = miner.difficulty + 1;
miner.incremented = true; miner.incremented = true;
} else { } else {
@@ -691,7 +753,7 @@ function handleMinerData(method, params, ip, portData, sendReply, pushMessage) {
return; return;
} }
let shareAccepted = processShare(miner, job, blockTemplate, params.nonce, params.result); let shareAccepted = processShare(miner, job, blockTemplate, params);
miner.checkBan(shareAccepted); miner.checkBan(shareAccepted);
if (global.config.pool.trustedMiners) { if (global.config.pool.trustedMiners) {
@@ -768,7 +830,7 @@ if (cluster.isMaster) {
workerList.push(worker); workerList.push(worker);
}); });
templateUpdate(); templateUpdate();
global.support.sendEmail(global.config.general.adminEmail, "Pool server "+global.config.hostname+" online", "The pool server: "+global.config.hostname+" with IP: "+global.config.bind_ip+" is online"); global.support.sendEmail(global.config.general.adminEmail, "Pool server " + global.config.hostname + " online", "The pool server: " + global.config.hostname + " with IP: " + global.config.bind_ip + " is online");
} else { } else {
setInterval(checkAliveMiners, 30000); setInterval(checkAliveMiners, 30000);
setInterval(retargetMiners, global.config.pool.retargetTime * 1000); setInterval(retargetMiners, global.config.pool.retargetTime * 1000);
@@ -778,7 +840,7 @@ if (cluster.isMaster) {
templateUpdate(false); templateUpdate(false);
}, 60000); }, 60000);
async.each(global.config.ports, function (portData) { async.each(global.config.ports, function (portData) {
if (global.config[portData.portType].enable !== true){ if (global.config[portData.portType].enable !== true) {
return; return;
} }
let handleMessage = function (socket, jsonData, pushMessage) { let handleMessage = function (socket, jsonData, pushMessage) {
@@ -830,7 +892,7 @@ if (cluster.isMaster) {
socket.on('data', function (d) { socket.on('data', function (d) {
dataBuffer += d; dataBuffer += d;
if (Buffer.byteLength(dataBuffer, 'utf8') > 10240) { //10KB if (Buffer.byteLength(dataBuffer, 'utf8') > 102400) { //10KB
dataBuffer = null; dataBuffer = null;
console.warn(threadName + 'Excessive packet size from: ' + socket.remoteAddress); console.warn(threadName + 'Excessive packet size from: ' + socket.remoteAddress);
socket.destroy(); socket.destroy();
@@ -879,7 +941,7 @@ if (cluster.isMaster) {
}); });
} }
if ('ssl' in portData && portData['ssl'] === true) { if ('ssl' in portData && portData.ssl === true) {
tls.createServer({ tls.createServer({
key: fs.readFileSync('cert.key'), key: fs.readFileSync('cert.key'),
cert: fs.readFileSync('cert.pem') cert: fs.readFileSync('cert.pem')