Skip to content

Commit 37a4541

Browse files
Update varDiff.js
1 parent 27bde74 commit 37a4541

File tree

1 file changed

+52
-82
lines changed

1 file changed

+52
-82
lines changed

lib/varDiff.js

+52-82
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,96 @@
1-
var events = require('events');
2-
3-
/*
4-
5-
Vardiff ported from stratum-mining share-limiter
6-
https://github.com/ahmedbodi/stratum-mining/blob/master/mining/basic_share_limiter.py
7-
8-
*/
9-
10-
11-
function RingBuffer(maxSize){
12-
var data = [];
13-
var cursor = 0;
14-
var isFull = false;
15-
this.append = function(x){
16-
if (isFull){
1+
const events = require('events');
2+
3+
function RingBuffer(maxSize) {
4+
let data = [];
5+
let cursor = 0;
6+
let isFull = false;
7+
this.append = function (x) {
8+
if (isFull) {
179
data[cursor] = x;
1810
cursor = (cursor + 1) % maxSize;
19-
}
20-
else{
11+
} else {
2112
data.push(x);
2213
cursor++;
23-
if (data.length === maxSize){
14+
if (data.length === maxSize) {
2415
cursor = 0;
2516
isFull = true;
2617
}
2718
}
2819
};
29-
this.avg = function(){
30-
var sum = data.reduce(function(a, b){ return a + b });
20+
this.avg = function () {
21+
let sum = data.reduce((a, b) => a + b, 0);
3122
return sum / (isFull ? maxSize : cursor);
3223
};
33-
this.size = function(){
24+
this.size = function () {
3425
return isFull ? maxSize : cursor;
3526
};
36-
this.clear = function(){
27+
this.clear = function () {
3728
data = [];
3829
cursor = 0;
3930
isFull = false;
4031
};
4132
}
4233

43-
// Truncate a number to a fixed amount of decimal places
4434
function toFixed(num, len) {
4535
return parseFloat(num.toFixed(len));
4636
}
4737

48-
var varDiff = module.exports = function varDiff(port, varDiffOptions){
49-
var _this = this;
50-
51-
var bufferSize, tMin, tMax;
52-
53-
//if (!varDiffOptions) return;
54-
55-
var variance = varDiffOptions.targetTime * (varDiffOptions.variancePercent / 100);
56-
57-
58-
bufferSize = varDiffOptions.retargetTime / varDiffOptions.targetTime * 4;
59-
tMin = varDiffOptions.targetTime - variance;
60-
tMax = varDiffOptions.targetTime + variance;
61-
62-
63-
64-
this.manageClient = function(client){
65-
66-
var stratumPort = client.socket.localPort;
67-
68-
if (stratumPort != port) {
69-
console.error("Handling a client which is not of this vardiff?");
70-
}
71-
var options = varDiffOptions;
72-
73-
var lastTs;
74-
var lastRtc;
75-
var timeBuffer;
76-
77-
client.on('submit', function(){
78-
79-
var ts = (Date.now() / 1000) | 0;
80-
81-
if (!lastRtc){
82-
lastRtc = ts - options.retargetTime / 2;
38+
class varDiff extends events.EventEmitter {
39+
constructor(port, varDiffOptions) {
40+
super();
41+
this.port = port;
42+
this.varDiffOptions = varDiffOptions;
43+
this.bufferSize = varDiffOptions.retargetTime / varDiffOptions.targetTime * 4;
44+
this.tMin = varDiffOptions.targetTime - (varDiffOptions.targetTime * (varDiffOptions.variancePercent / 100));
45+
this.tMax = varDiffOptions.targetTime + (varDiffOptions.targetTime * (varDiffOptions.variancePercent / 100));
46+
}
47+
48+
manageClient(client) {
49+
let lastTs;
50+
let lastRtc;
51+
let timeBuffer = new RingBuffer(this.bufferSize);
52+
53+
client.on('submit', () => {
54+
const ts = (Date.now() / 1000) | 0;
55+
56+
if (!lastRtc) {
57+
lastRtc = ts - this.varDiffOptions.retargetTime / 2;
8358
lastTs = ts;
84-
timeBuffer = new RingBuffer(bufferSize);
8559
return;
8660
}
8761

88-
var sinceLast = ts - lastTs;
62+
const sinceLast = ts - lastTs;
8963

9064
timeBuffer.append(sinceLast);
9165
lastTs = ts;
9266

93-
if ((ts - lastRtc) < options.retargetTime && timeBuffer.size() > 0)
67+
if ((ts - lastRtc) < this.varDiffOptions.retargetTime && timeBuffer.size() > 0)
9468
return;
9569

9670
lastRtc = ts;
97-
var avg = timeBuffer.avg();
98-
var ddiff = options.targetTime / avg;
71+
const avg = timeBuffer.avg();
72+
let ddiff = this.varDiffOptions.targetTime / avg;
9973

100-
if (avg > tMax && client.difficulty > options.minDiff) {
101-
if (options.x2mode) {
102-
ddiff = 0.5;
74+
if (avg > this.tMax && client.difficulty > this.varDiffOptions.minDiff) {
75+
ddiff = this.varDiffOptions.x2mode ? 0.5 : ddiff;
76+
if (ddiff * client.difficulty < this.varDiffOptions.minDiff) {
77+
ddiff = this.varDiffOptions.minDiff / client.difficulty;
10378
}
104-
if (ddiff * client.difficulty < options.minDiff) {
105-
ddiff = options.minDiff / client.difficulty;
106-
}
107-
} else if (avg < tMin) {
108-
if (options.x2mode) {
109-
ddiff = 2;
110-
}
111-
var diffMax = options.maxDiff;
79+
} else if (avg < this.tMin) {
80+
ddiff = this.varDiffOptions.x2mode ? 2 : ddiff;
81+
const diffMax = this.varDiffOptions.maxDiff;
11282
if (ddiff * client.difficulty > diffMax) {
11383
ddiff = diffMax / client.difficulty;
11484
}
115-
}
116-
else{
85+
} else {
11786
return;
11887
}
11988

120-
var newDiff = toFixed(client.difficulty * ddiff, 8);
89+
const newDiff = toFixed(client.difficulty * ddiff, 8);
12190
timeBuffer.clear();
122-
_this.emit('newDifficulty', client, newDiff);
91+
this.emit('newDifficulty', client, newDiff);
12392
});
124-
};
125-
};
126-
varDiff.prototype.__proto__ = events.EventEmitter.prototype;
93+
}
94+
}
95+
96+
module.exports = varDiff;

0 commit comments

Comments
 (0)