forked from SOELexicon/burst-pool
-
Notifications
You must be signed in to change notification settings - Fork 1
/
burst-pool-share.js
403 lines (366 loc) · 14.2 KB
/
burst-pool-share.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
var config = require('./burst-pool-config');
var poolSession = require('./burst-pool-session');
var poolProtocol = require('./burst-pool-protocol');
var fs = require('fs');
function duplicate(obj){
return JSON.parse(JSON.stringify(obj));
}
function fromDeadline(deadline, blockBaseTarget){
//S:share, D:deadline, Pd:poolDiff, T:netDiff, B:netBasetarget, B0:netBaseTarget-Block0
//S(D) = 100 / ( (D*B0)/(Pd*B) + 1)^5
// 100
// ----
// ( D*Nd / Pd + 1 )^3 ----> ( D * (B0/B) / Pd )
var B0 = parseFloat(poolSession.getGenesisBaseTarget());
var B = parseFloat(blockBaseTarget);
var D = parseFloat(deadline);
var Pd = parseFloat(config.poolDiff);
return 1000 / Math.pow((D*B0) / (Pd*B) + 1, config.poolDiffCurve);
}
function fromDeadlineCurrentBlock(deadline){
return fromDeadline(deadline,poolSession.getCurrentBaseTarget());
}
RoundShare = function(accountId,height, baseTarget){
this.accountId = accountId;
this.baseTarget = baseTarget;
this.height = height;
this.share = 0;
this.deadline = -1;
this.lastUpdate = 0;
};
RoundShare.prototype.updateByNewDeadline = function(deadline){
var assignedShare = 0;
if(this.deadline < 0){
this.deadline = deadline;
var oldShare = this.share;
this.share = fromDeadlineCurrentBlock(deadline);
assignedShare = this.share - oldShare;
}
else if(deadline < this.deadline){
this.deadline = deadline;
var oldShare = this.share;
this.share = fromDeadlineCurrentBlock(deadline);
assignedShare = this.share - oldShare;
}
else if(deadline > this.deadline){
this.substractShare(config.sharePenalty);
assignedShare = 0;
}
this.lastUpdate = new Date().getTime();
return assignedShare;
};
RoundShare.prototype.updateByNewBlock = function(height, baseTarget){
this.baseTarget = baseTarget;
this.height = height;
this.share = 0;
this.deadline = -1;
this.lastUpdate = new Date().getTime();
};
RoundShare.prototype.addShare = function(share){
if(typeof share != 'undefined' && !isNaN(share)){
this.share += share;
}
};
RoundShare.prototype.substractShare = function(share){
if(typeof share != 'undefined' && !isNaN(share)){
if(this.share < share){
this.share = 0;
}
else {
this.share -= share;
}
}
};
AccountShare = function(accountId,height, baseTarget){
this.id = accountId;
this.currentRoundShare = new RoundShare(accountId, height, baseTarget);
this.prevRoundShare = [];
};
AccountShare.prototype.loadFromJSON = function(json){
this.currentRoundShare = new RoundShare(json.currentRoundShare.accountId, json.currentRoundShare.height, json.currentRoundShare.baseTarget);
this.prevRoundShare = [];
for(var i in json.prevRoundShare){
var roundShare = json.prevRoundShare[i];
var newRoundShare = new RoundShare(roundShare.accountId, roundShare.height, roundShare.baseTarget);
newRoundShare.share = roundShare.share;
newRoundShare.deadline = roundShare.deadline;
newRoundShare.lastUpdate = roundShare.lastUpdate;
this.prevRoundShare.push(newRoundShare);
};
};
AccountShare.prototype.updateByNewBlock = function( height, baseTarget){
this.prevRoundShare.unshift(duplicate(this.currentRoundShare));
this.currentRoundShare.updateByNewBlock(height, baseTarget);
};
AccountShare.prototype.updateByNewDeadline = function(deadline){
return this.currentRoundShare.updateByNewDeadline(deadline);
};
AccountShare.prototype.getShareOnBlock = function(height){
if(this.currentRoundShare.height == height){
return this.currentRoundShare.share;
}
else{
for(var i=0 ; i<this.prevRoundShare.length ; i++){
if(this.prevRoundShare[i].height == height){
return this.prevRoundShare[i].share;
}
}
}
return null;
};
AccountShare.prototype.deleteRoundShareByDistance = function(distance){
if(this.prevRoundShare.length > distance){
var blockExpired = this.currentRoundShare.height - distance;
poolProtocol.clientLog('Account '+this.currentRoundShare.accountId+' share below Block#'+blockExpired+' is expired');
this.prevRoundShare.splice(distance,this.prevRoundShare.length - distance);
}
};
AccountShare.prototype.getShare = function(){
return this.currentRoundShare.share;
};
AccountShare.prototype.addShare = function(share){
this.currentRoundShare.addShare(share);
};
AccountShare.prototype.substractShare = function(share){
this.currentRoundShare.substractShare(share);
};
PoolShare = function(){
this.accountShare = [];
this.accountShareIdIndex = {};
};
PoolShare.prototype.updateByNewDeadline = function(accountId, deadline){
var share;
var poolShare;
var userShare = 0;
if(this.accountShareIdIndex.hasOwnProperty(accountId)){
share = this.accountShareIdIndex[accountId].updateByNewDeadline(deadline);
userShare = this.accountShareIdIndex[accountId].getShare();
poolShare = share*config.poolFee;
this.addShareToAccount(config.poolFeePaymentAddr,poolShare);
}
else{
if(poolSession.isAccountIdAssignedToPool(accountId)){
var newAccountShare = new AccountShare(accountId,poolSession.getCurrentBlockHeight(),poolSession.getCurrentBaseTarget());
share = newAccountShare.updateByNewDeadline(deadline);
this.accountShare.push(accountId);
this.accountShareIdIndex[accountId] = newAccountShare;
userShare = this.accountShareIdIndex[accountId].getShare();
poolShare = share*config.poolFee;
this.addShareToAccount(config.poolFeePaymentAddr,poolShare);
}
}
//console.log("share #"+poolSession.getCurrentBlockHeight()+' '+accountId+' ('+userShare.toFixed(4)+') D:'+deadline+'secs S:'+share.toFixed(4)+' PS:'+poolShare.toFixed(4));
};
PoolShare.prototype.updateByNewBlock = function(height, baseTarget){
for(var accountId in this.accountShareIdIndex){
this.accountShareIdIndex[accountId].updateByNewBlock(height,baseTarget);
}
};
PoolShare.prototype.getBlockShare = function(height){
var shareList = [];
for(var accountId in this.accountShareIdIndex){
var account = this.accountShareIdIndex[accountId];
var accountShare = account.getShareOnBlock(height);
var shareItem = {
accountId : accountId,
share : accountShare
};
if(shareItem.share > 0){
shareList.push(shareItem);
}
}
return shareList;
};
PoolShare.prototype.getShares = function(){
var blockShareList = {};
for(var accountId in this.accountShareIdIndex){
var roundShareList = [];
roundShareList.push(this.accountShareIdIndex[accountId].currentRoundShare);
roundShareList = roundShareList.concat(this.accountShareIdIndex[accountId].prevRoundShare);
blockShareList[accountId] = duplicate(roundShareList);
}
return blockShareList;
};
PoolShare.prototype.getCurrentRoundShares = function(){
var blockShareList = {};
var totalShare = 0;
var bestDeadline = -1;
var submitters = 0;
var bestDeadlineAccount = 0;
for(var accountId in this.accountShareIdIndex){
var roundShareList = [];
roundShareList.push(this.accountShareIdIndex[accountId].currentRoundShare);
blockShareList[accountId] = duplicate(roundShareList);
totalShare += this.accountShareIdIndex[accountId].currentRoundShare.share;
if(this.accountShareIdIndex[accountId].currentRoundShare.deadline > 0){
if(bestDeadline == -1){
bestDeadline = this.accountShareIdIndex[accountId].currentRoundShare.deadline;
bestDeadlineAccount = accountId;
}
else if(bestDeadline > this.accountShareIdIndex[accountId].currentRoundShare.deadline){
bestDeadline = this.accountShareIdIndex[accountId].currentRoundShare.deadline;
bestDeadlineAccount = accountId;
}
}
submitters++;
}
blockShareList.totalShare = totalShare;
blockShareList.bestDeadline = bestDeadline;
blockShareList.submitters = submitters;
blockShareList.bestDeadlineAccount = bestDeadlineAccount;
return blockShareList;
};
PoolShare.prototype.getCumulativeShares = function(){
var blockShareList = this.getShares();
var cumulativeShare = {};
for(var accountId in blockShareList){
var shareList = blockShareList[accountId];
shareList.forEach(function(share){
if(cumulativeShare.hasOwnProperty(accountId)){
cumulativeShare[accountId].share += share.share;
cumulativeShare[accountId].roundCount++;
if(share.deadline > 0){
if(cumulativeShare[accountId].deadline < 0){
cumulativeShare[accountId].deadline = share.deadline;
}
else if( share.deadline < cumulativeShare[accountId].deadline){
cumulativeShare[accountId].deadline = share.deadline;
}
}
}
else{
cumulativeShare[accountId] = share;
cumulativeShare[accountId].roundCount = 1;
}
});
}
return cumulativeShare;
};
PoolShare.prototype.getAccountShare = function(accountId){
if(this.accountShareIdIndex.hasOwnProperty(accountId)){
var accountShare = this.accountShareIdIndex[accountId];
return accountShare.currentRoundShare;
}
return null;
};
PoolShare.prototype.deleteAccount = function(accountId){
if(this.accountShareIdIndex.hasOwnProperty(accountId)){
delete this.accountShareIdIndex[accountId];
var ndx = this.accountShare.indexOf(accountId);
if(ndx >= 0 ){
this.accountShare.splice(ndx,1);
}
}
};
PoolShare.prototype.deleteAccountShare = function(accountId){
if(this.accountShareIdIndex.hasOwnProperty(accountId)){
var accountShare = this.accountShareIdIndex[accountId];
accountShare.prevRoundShare = [];
}
};
PoolShare.prototype.deleteAccountShareBelowThresshold = function(shareAmount,numOfRound){
var cumulativeShare = this.getCumulativeShares();
for(var accountId in cumulativeShare){
if( cumulativeShare[accountId].share < shareAmount &&
cumulativeShare[accountId].roundCount > numOfRound){
this.deleteAccount(accountId);
console.log("deleted account "+accountId+" because of low share");
}
}
};
PoolShare.prototype.deleteRoundShareByDistance = function(distance){
for(var accountId in this.accountShareIdIndex){
var account = this.accountShareIdIndex[accountId];
account.deleteRoundShareByDistance(distance);
}
};
PoolShare.prototype.addShareToAccount = function(accountId, share){
if(this.accountShareIdIndex.hasOwnProperty(accountId)){
this.accountShareIdIndex[accountId].addShare(share);
}
else{
if(poolSession.isAccountIdAssignedToPool(accountId)){
var newAccountShare = new AccountShare(accountId,poolSession.getCurrentBlockHeight(),poolSession.getCurrentBaseTarget());
newAccountShare.addShare(share);
this.accountShare.push(accountId);
this.accountShareIdIndex[accountId] = newAccountShare;
}
}
};
PoolShare.prototype.substractShareFromAccount = function(accountId, share){
if(this.accountShareIdIndex.hasOwnProperty(accountId)){
this.accountShareIdIndex[accountId].substractShare(share);
}
};
var poolShare = new PoolShare();
module.exports = {
addShareToAccount : function(accountId,share){
poolShare.addShareToAccount(accountId, accountId);
},
updateByNewBlock : function(height, baseTarget){
poolShare.updateByNewBlock(height, baseTarget);
},
updateByNewDeadline : function(accountId, deadline){
poolShare.updateByNewDeadline(accountId, deadline);
},
getBlockShare : function(height){
return poolShare.getBlockShare(height);
},
getShares : function(){
return poolShare.getShares();
},
getCumulativeShares : function(){
return poolShare.getCumulativeShares();
},
getAccountShare : function(accountId){
return poolShare.getAccountShare(accountId);
},
saveSession : function(){
var poolShareData = JSON.stringify(poolShare,null,2);
fs.writeFileSync('pool-share.json', poolShareData);
},
getCurrentRoundShares : function(){
return poolShare.getCurrentRoundShares();
},
deleteAccountShare : function(accountId){
poolShare.deleteAccountShare(accountId);
},
deleteRoundShareByDistance : function(distance){
poolShare.deleteRoundShareByDistance(distance);
},
deleteAccountShareBelowThresshold : function(shareAmount,numOfRound){
poolShare.deleteAccountShareBelowThresshold(shareAmount,numOfRound);
},
deleteAccount : function(accountId){
poolShare.deleteAccount(accountId);
},
loadSession : function(done){
if( fs.existsSync('pool-share.json')) {
fs.readFile('pool-share.json', function(err, data) {
try{
var loadedData = JSON.parse(data);
if(loadedData.hasOwnProperty('accountShare')){
poolShare.accountShare = loadedData.accountShare;
}
if(loadedData.hasOwnProperty('accountShareIdIndex')){
for(var accountId in loadedData.accountShareIdIndex){
var accountShare = loadedData.accountShareIdIndex[accountId];
poolShare.accountShareIdIndex[accountId] = new AccountShare(0,0,0);
poolShare.accountShareIdIndex[accountId].accountId = accountId;
poolShare.accountShareIdIndex[accountId].loadFromJSON(accountShare);
}
}
}
catch(e){
console.log(e);
console.trace();
}
done();
});
}
else{
done();
}
},
shareFromDeadline : fromDeadline
};