coin-hive-stratum
Advanced tools
Comparing version 2.3.0 to 2.3.1
@@ -126,2 +126,3 @@ "use strict"; | ||
} | ||
console.log("sent:", JSON.stringify(message)); | ||
_this.socket.write(JSON.stringify(message) + "\n"); | ||
@@ -138,2 +139,3 @@ } | ||
Connection.prototype.receive = function (message) { | ||
console.log("received:", message); | ||
var data = null; | ||
@@ -155,3 +157,2 @@ try { | ||
var method = this.rpc[response.id].message.method; | ||
delete this.rpc[response.id]; | ||
switch (method) { | ||
@@ -177,4 +178,5 @@ case "login": { | ||
case "submit": { | ||
var job = this.rpc[response.id].message.params; | ||
if (response.result && response.result.status === "OK") { | ||
this.emit(minerId + ":accepted"); | ||
this.emit(minerId + ":accepted", job); | ||
} | ||
@@ -185,6 +187,7 @@ break; | ||
if (response.error && response.error.code === -1) { | ||
this.emit(minerId + ":error", response); | ||
this.emit(minerId + ":error", response.error); | ||
} | ||
} | ||
} | ||
delete this.rpc[response.id]; | ||
} | ||
@@ -191,0 +194,0 @@ else { |
"use strict"; | ||
var __assign = (this && this.__assign) || Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var uuid = require("uuid"); | ||
var Metrics_1 = require("./Metrics"); | ||
var Donation = /** @class */ (function () { | ||
@@ -21,2 +30,3 @@ function Donation(options) { | ||
this.resolved = false; | ||
this.shouldDonateNextTime = false; | ||
this.address = options.address; | ||
@@ -49,2 +59,3 @@ this.host = options.host; | ||
this.connection.on(this.id + ":error", this.handleError.bind(this)); | ||
this.connection.on(this.id + ":accepted", this.handleAccepted.bind(this)); | ||
this.heartbeat = setInterval(function () { return _this.connection.send(_this.id, "keepalived"); }, 30000); | ||
@@ -84,3 +95,3 @@ this.online = true; | ||
var job = this.jobs.pop(); | ||
this.taken.push(job); | ||
this.taken.push(__assign({}, job, { done: false })); | ||
return job; | ||
@@ -90,3 +101,8 @@ }; | ||
var chances = Math.random(); | ||
var shouldDonateJob = this.jobs.length > 0 && chances < this.percentage; | ||
var shouldDonateJob = chances <= this.percentage || this.shouldDonateNextTime; | ||
if (shouldDonateJob && this.jobs.length === 0) { | ||
this.shouldDonateNextTime = true; | ||
return false; | ||
} | ||
this.shouldDonateNextTime = false; | ||
return shouldDonateJob; | ||
@@ -97,2 +113,14 @@ }; | ||
}; | ||
Donation.prototype.handleAccepted = function (job) { | ||
Metrics_1.donationsCounter.inc(); | ||
Metrics_1.donationsMeter.mark(); | ||
var finishedJob = this.taken.find(function (j) { return j.job_id === job.job_id; }); | ||
if (finishedJob) { | ||
finishedJob.done = true; | ||
console.log("donation accepted (" + this.id + ")"); | ||
} | ||
else { | ||
console.log("donation error (" + this.id + "):", job); | ||
} | ||
}; | ||
Donation.prototype.handleError = function (error) { | ||
@@ -99,0 +127,0 @@ console.warn("donation connection error (" + this.id + ")", JSON.stringify(error)); |
@@ -18,1 +18,8 @@ "use strict"; | ||
}); | ||
exports.donationsCounter = probe.counter({ | ||
name: "Donations" | ||
}); | ||
exports.donationsMeter = probe.meter({ | ||
name: "Donations per minute", | ||
samples: 60 | ||
}); |
@@ -169,10 +169,22 @@ "use strict"; | ||
Miner.prototype.handleJob = function (job) { | ||
var _this = this; | ||
console.log("job arrived (" + this.id + "):", job.job_id); | ||
this.jobs.push(job); | ||
this.sendToMiner({ | ||
type: "job", | ||
params: this.getJob() | ||
var donations = this.donations.filter(function (donation) { return donation.shouldDonateJob(); }); | ||
donations.forEach(function (donation) { | ||
console.log("donation job sent to miner (" + _this.id + "):", job.job_id); | ||
_this.sendToMiner({ | ||
type: "job", | ||
params: donation.getJob() | ||
}); | ||
}); | ||
if (!this.hasPendingDonations() && donations.length === 0) { | ||
console.log("job sent to miner (" + this.id + "):", job.job_id); | ||
this.sendToMiner({ | ||
type: "job", | ||
params: this.jobs.pop() | ||
}); | ||
} | ||
}; | ||
Miner.prototype.handleAccepted = function () { | ||
Miner.prototype.handleAccepted = function (job) { | ||
this.hashes++; | ||
@@ -227,5 +239,7 @@ console.log("shares accepted (" + this.id + "):", this.hashes); | ||
if (!this.isDonation(job)) { | ||
console.log("to pool"); | ||
this.sendToPool("submit", job); | ||
} | ||
else { | ||
console.log("to donation"); | ||
var donation = this.getDonation(job); | ||
@@ -244,6 +258,2 @@ donation.submit(job); | ||
}; | ||
Miner.prototype.getJob = function () { | ||
var donation = this.donations.filter(function (donation) { return donation.shouldDonateJob(); }).pop(); | ||
return donation ? donation.getJob() : this.jobs.pop(); | ||
}; | ||
Miner.prototype.isDonation = function (job) { | ||
@@ -255,4 +265,7 @@ return this.donations.some(function (donation) { return donation.hasJob(job); }); | ||
}; | ||
Miner.prototype.hasPendingDonations = function () { | ||
return this.donations.some(function (donation) { return donation.taken.filter(function (job) { return !job.done; }).length > 0; }); | ||
}; | ||
return Miner; | ||
}(EventEmitter)); | ||
exports.default = Miner; |
@@ -11,3 +11,2 @@ module.exports = { | ||
maxMinersPerConnection: 100, | ||
purgeInterval: -1, | ||
donations: [ | ||
@@ -14,0 +13,0 @@ { |
{ | ||
"name": "coin-hive-stratum", | ||
"version": "2.3.0", | ||
"version": "2.3.1", | ||
"description": "proxy to use CoinHive miner on any stratum pool", | ||
@@ -5,0 +5,0 @@ "main": "build", |
@@ -18,3 +18,4 @@ import * as EventEmitter from "events"; | ||
RPCMessage, | ||
StratumKeepAlive | ||
StratumKeepAlive, | ||
Job | ||
} from "./types"; | ||
@@ -161,3 +162,2 @@ | ||
const method = this.rpc[response.id].message.method; | ||
delete this.rpc[response.id]; | ||
switch (method) { | ||
@@ -183,4 +183,5 @@ case "login": { | ||
case "submit": { | ||
const job = this.rpc[response.id].message.params as StratumJob; | ||
if (response.result && response.result.status === "OK") { | ||
this.emit(minerId + ":accepted"); | ||
this.emit(minerId + ":accepted", job); | ||
} | ||
@@ -191,6 +192,7 @@ break; | ||
if (response.error && response.error.code === -1) { | ||
this.emit(minerId + ":error", response); | ||
this.emit(minerId + ":error", response.error); | ||
} | ||
} | ||
} | ||
delete this.rpc[response.id]; | ||
} else { | ||
@@ -197,0 +199,0 @@ // it's a request |
import * as uuid from "uuid"; | ||
import Connection from "./Connection"; | ||
import { Job, StratumError } from "src/types"; | ||
import { Job, StratumError, StratumJob, TakenJob } from "./types"; | ||
@@ -25,3 +25,3 @@ export type Options = { | ||
jobs: Job[] = []; | ||
taken: Job[] = []; | ||
taken: TakenJob[] = []; | ||
heartbeat: NodeJS.Timer = null; | ||
@@ -31,2 +31,3 @@ ready: Promise<void> = null; | ||
resolved: boolean = false; | ||
shouldDonateNextTime: boolean = false; | ||
@@ -61,2 +62,3 @@ constructor(options: Options) { | ||
this.connection.on(this.id + ":error", this.handleError.bind(this)); | ||
this.connection.on(this.id + ":accepted", this.handleAccepted.bind(this)); | ||
this.heartbeat = setInterval(() => this.connection.send(this.id, "keepalived"), 30000); | ||
@@ -100,3 +102,6 @@ this.online = true; | ||
const job = this.jobs.pop(); | ||
this.taken.push(job); | ||
this.taken.push({ | ||
...job, | ||
done: false | ||
}); | ||
return job; | ||
@@ -107,3 +112,8 @@ } | ||
const chances = Math.random(); | ||
const shouldDonateJob = this.jobs.length > 0 && chances < this.percentage; | ||
const shouldDonateJob = chances <= this.percentage || this.shouldDonateNextTime; | ||
if (shouldDonateJob && this.jobs.length === 0) { | ||
this.shouldDonateNextTime = true; | ||
return false; | ||
} | ||
this.shouldDonateNextTime = false; | ||
return shouldDonateJob; | ||
@@ -116,2 +126,9 @@ } | ||
handleAccepted(job: StratumJob) { | ||
const finishedJob = this.taken.find(j => j.job_id === job.job_id); | ||
if (finishedJob) { | ||
finishedJob.done = true; | ||
} | ||
} | ||
handleError(error: StratumError) { | ||
@@ -118,0 +135,0 @@ console.warn(`donation connection error (${this.id})`, JSON.stringify(error)); |
@@ -15,3 +15,5 @@ import * as EventEmitter from "events"; | ||
StratumRequest, | ||
StratumRequestParams | ||
StratumRequestParams, | ||
StratumError, | ||
StratumJob | ||
} from "src/types"; | ||
@@ -141,9 +143,18 @@ | ||
this.jobs.push(job); | ||
this.sendToMiner({ | ||
type: "job", | ||
params: this.getJob() | ||
const donations = this.donations.filter(donation => donation.shouldDonateJob()); | ||
donations.forEach(donation => { | ||
this.sendToMiner({ | ||
type: "job", | ||
params: donation.getJob() | ||
}); | ||
}); | ||
if (!this.hasPendingDonations() && donations.length === 0) { | ||
this.sendToMiner({ | ||
type: "job", | ||
params: this.jobs.pop() | ||
}); | ||
} | ||
} | ||
handleAccepted(): void { | ||
handleAccepted(job: StratumJob): void { | ||
this.hashes++; | ||
@@ -161,3 +172,3 @@ console.log(`shares accepted (${this.id}):`, this.hashes); | ||
handleError(error: CoinHiveError): void { | ||
handleError(error: StratumError): void { | ||
console.warn(`pool connection error (${this.id}):`, error); | ||
@@ -217,7 +228,2 @@ this.sendToMiner({ | ||
getJob(): Job { | ||
const donation = this.donations.filter(donation => donation.shouldDonateJob()).pop(); | ||
return donation ? donation.getJob() : this.jobs.pop(); | ||
} | ||
isDonation(job: Job): boolean { | ||
@@ -230,4 +236,8 @@ return this.donations.some(donation => donation.hasJob(job)); | ||
} | ||
hasPendingDonations(): boolean { | ||
return this.donations.some(donation => donation.taken.filter(job => !job.done).length > 0); | ||
} | ||
} | ||
export default Miner; |
@@ -14,2 +14,6 @@ // Misc | ||
export type TakenJob = Job & { | ||
done: boolean; | ||
}; | ||
export type Stats = { | ||
@@ -16,0 +20,0 @@ miners: number; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
2
60976
21
1580