|
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) { |
17 | 9 | data[cursor] = x;
|
18 | 10 | cursor = (cursor + 1) % maxSize;
|
19 |
| - } |
20 |
| - else{ |
| 11 | + } else { |
21 | 12 | data.push(x);
|
22 | 13 | cursor++;
|
23 |
| - if (data.length === maxSize){ |
| 14 | + if (data.length === maxSize) { |
24 | 15 | cursor = 0;
|
25 | 16 | isFull = true;
|
26 | 17 | }
|
27 | 18 | }
|
28 | 19 | };
|
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); |
31 | 22 | return sum / (isFull ? maxSize : cursor);
|
32 | 23 | };
|
33 |
| - this.size = function(){ |
| 24 | + this.size = function () { |
34 | 25 | return isFull ? maxSize : cursor;
|
35 | 26 | };
|
36 |
| - this.clear = function(){ |
| 27 | + this.clear = function () { |
37 | 28 | data = [];
|
38 | 29 | cursor = 0;
|
39 | 30 | isFull = false;
|
40 | 31 | };
|
41 | 32 | }
|
42 | 33 |
|
43 |
| -// Truncate a number to a fixed amount of decimal places |
44 | 34 | function toFixed(num, len) {
|
45 | 35 | return parseFloat(num.toFixed(len));
|
46 | 36 | }
|
47 | 37 |
|
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; |
83 | 58 | lastTs = ts;
|
84 |
| - timeBuffer = new RingBuffer(bufferSize); |
85 | 59 | return;
|
86 | 60 | }
|
87 | 61 |
|
88 |
| - var sinceLast = ts - lastTs; |
| 62 | + const sinceLast = ts - lastTs; |
89 | 63 |
|
90 | 64 | timeBuffer.append(sinceLast);
|
91 | 65 | lastTs = ts;
|
92 | 66 |
|
93 |
| - if ((ts - lastRtc) < options.retargetTime && timeBuffer.size() > 0) |
| 67 | + if ((ts - lastRtc) < this.varDiffOptions.retargetTime && timeBuffer.size() > 0) |
94 | 68 | return;
|
95 | 69 |
|
96 | 70 | 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; |
99 | 73 |
|
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; |
103 | 78 | }
|
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; |
112 | 82 | if (ddiff * client.difficulty > diffMax) {
|
113 | 83 | ddiff = diffMax / client.difficulty;
|
114 | 84 | }
|
115 |
| - } |
116 |
| - else{ |
| 85 | + } else { |
117 | 86 | return;
|
118 | 87 | }
|
119 | 88 |
|
120 |
| - var newDiff = toFixed(client.difficulty * ddiff, 8); |
| 89 | + const newDiff = toFixed(client.difficulty * ddiff, 8); |
121 | 90 | timeBuffer.clear();
|
122 |
| - _this.emit('newDifficulty', client, newDiff); |
| 91 | + this.emit('newDifficulty', client, newDiff); |
123 | 92 | });
|
124 |
| - }; |
125 |
| -}; |
126 |
| -varDiff.prototype.__proto__ = events.EventEmitter.prototype; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +module.exports = varDiff; |
0 commit comments