forked from LiskArchive/lisk-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
round.js
368 lines (309 loc) · 9.2 KB
/
round.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
var async = require("async");
var util = require("util");
var pgp = require("pg-promise");
var slots = require("../helpers/slots.js");
var constants = require("../helpers/constants.js");
var sandboxHelper = require("../helpers/sandbox.js");
var sql = require("../sql/round.js");
// Private fields
var modules, library, self, private = {}, shared = {};
private.loaded = false;
private.ticking = false;
private.feesByRound = {};
private.rewardsByRound = {};
private.delegatesByRound = {};
private.unFeesByRound = {};
private.unRewardsByRound = {};
private.unDelegatesByRound = {};
// Constructor
function Round(cb, scope) {
library = scope;
self = this;
self.__private = private;
setImmediate(cb, null, self);
}
// Round changes
function RoundChanges (round) {
var roundFees = Math.floor(private.feesByRound[round]) || 0;
var roundRewards = (private.rewardsByRound[round] || []);
this.at = function (index) {
var fees = Math.floor(roundFees / slots.delegates),
feesRemaining = roundFees - (fees * slots.delegates),
rewards = Math.floor(roundRewards[index]) || 0;
return {
fees : fees,
feesRemaining : feesRemaining,
rewards : rewards,
balance : fees + rewards
};
}
}
// Round promiser
function RoundPromiser (scope, t) {
var self = this;
this.mergeBlockGenerator = function () {
return t.none(
modules.accounts.mergeAccountAndGet({
publicKey: scope.block.generatorPublicKey,
producedblocks: (scope.backwards ? -1 : 1),
blockId: scope.block.id,
round: scope.round
})
);
}
this.updateMissedBlocks = function () {
if (scope.outsiders.length == 0) {
return t;
}
return t.none(sql.updateMissedBlocks, [scope.outsiders]);
}
this.getVotes = function () {
return t.query(sql.getVotes, { round: scope.round });
}
this.updateVotes = function () {
return self.getVotes(scope.round).then(function (votes) {
var queries = votes.map(function (vote) {
return pgp.as.format(sql.updateVotes, {
address: modules.accounts.generateAddressByPublicKey(vote.delegate),
amount: Math.floor(vote.amount)
});
}).join("");
if (queries.length > 0) {
return t.none(queries);
} else {
return t;
}
});
}
this.flushRound = function () {
return t.none(sql.flush, { round: scope.round });
}
this.applyRound = function () {
var roundChanges = new RoundChanges(scope.round);
var queries = "";
for (var i = 0; i < scope.delegates.length; i++) {
var delegate = scope.delegates[i],
changes = roundChanges.at(i);
queries += modules.accounts.mergeAccountAndGet({
publicKey: delegate,
balance: (scope.backwards ? -changes.balance : changes.balance),
u_balance: (scope.backwards ? -changes.balance : changes.balance),
blockId: scope.block.id,
round: scope.round,
fees: (scope.backwards ? -changes.fees : changes.fees),
rewards: (scope.backwards ? -changes.rewards : changes.rewards)
});
if (i === scope.delegates.length - 1) {
queries += modules.accounts.mergeAccountAndGet({
publicKey: delegate,
balance: (scope.backwards ? -changes.feesRemaining : changes.feesRemaining),
u_balance: (scope.backwards ? -changes.feesRemaining : changes.feesRemaining),
blockId: scope.block.id,
round: scope.round,
fees: (scope.backwards ? -changes.feesRemaining : changes.feesRemaining),
});
}
}
return t.none(queries);
}
this.land = function () {
private.ticking = true;
return this.updateVotes()
.then(this.updateMissedBlocks)
.then(this.flushRound)
.then(this.applyRound)
.then(this.updateVotes)
.then(this.flushRound)
.then(function () {
private.ticking = false;
return t;
});
}
}
// Public methods
Round.prototype.loaded = function () {
return private.loaded;
}
Round.prototype.ticking = function () {
return private.ticking;
}
Round.prototype.calc = function (height) {
return Math.floor(height / slots.delegates) + (height % slots.delegates > 0 ? 1 : 0);
}
Round.prototype.flush = function (round, cb) {
library.db.none(sql.flush, { round: round }).then(function () {
return cb();
}).catch(function (err) {
library.logger.error(err.toString());
return cb("Round#flush error");
});
}
Round.prototype.directionSwap = function (direction, lastBlock, cb) {
if (direction == "backward") {
private.feesByRound = {};
private.rewardsByRound = {};
private.delegatesByRound = {};
self.flush(self.calc(lastBlock.height), cb);
} else {
private.unFeesByRound = {};
private.unRewardsByRound = {};
private.unDelegatesByRound = {};
self.flush(self.calc(lastBlock.height), cb);
}
}
Round.prototype.backwardTick = function (block, previousBlock, done) {
var round = self.calc(block.height);
var prevRound = self.calc(previousBlock.height);
private.unFeesByRound[round] = Math.floor(private.unFeesByRound[round]) || 0;
private.unFeesByRound[round] += Math.floor(block.totalFee);
private.unRewardsByRound[round] = (private.rewardsByRound[round] || []);
private.unRewardsByRound[round].push(block.reward);
private.unDelegatesByRound[round] = private.unDelegatesByRound[round] || [];
private.unDelegatesByRound[round].push(block.generatorPublicKey);
var scope = {
block: block,
round: round,
backwards: true,
delegates: private.unDelegatesByRound[round]
};
scope.finishRound = (
(prevRound !== round && private.unDelegatesByRound[round].length == slots.delegates) ||
(previousBlock.height == 1)
);
function BackwardTick (t) {
var promised = new RoundPromiser(scope, t);
return promised.mergeBlockGenerator().then(function () {
if (scope.finishRound) {
return promised.land().then(function () {
delete private.unFeesByRound[round];
delete private.unRewardsByRound[round];
delete private.unDelegatesByRound[round];
});
}
});
}
async.series([
function (cb) {
if (scope.finishRound) {
return private.getOutsiders(scope, cb);
} else {
return cb();
}
},
function (cb) {
library.db.tx(BackwardTick).then(function () {
return cb();
}).catch(function (err) {
library.logger.error(err.toString());
return cb(err);
});
}
], function (err) {
return done(err);
});
}
Round.prototype.tick = function (block, done) {
var round = self.calc(block.height);
var nextRound = self.calc(block.height + 1);
private.feesByRound[round] = Math.floor(private.feesByRound[round]) || 0;
private.feesByRound[round] += Math.floor(block.totalFee);
private.rewardsByRound[round] = (private.rewardsByRound[round] || []);
private.rewardsByRound[round].push(block.reward);
private.delegatesByRound[round] = private.delegatesByRound[round] || [];
private.delegatesByRound[round].push(block.generatorPublicKey);
var scope = {
block: block,
round: round,
backwards: false,
delegates: private.delegatesByRound[round]
};
scope.finishRound = (
(round !== nextRound && private.delegatesByRound[round].length == slots.delegates) ||
(block.height == 1 || block.height == 101)
);
function Tick (t) {
var promised = new RoundPromiser(scope, t);
return promised.mergeBlockGenerator().then(function () {
if (scope.finishRound) {
return promised.land().then(function () {
delete private.feesByRound[round];
delete private.rewardsByRound[round];
delete private.delegatesByRound[round];
library.bus.message("finishRound", round);
});
}
});
}
async.series([
function (cb) {
if (scope.finishRound) {
return private.getOutsiders(scope, cb);
} else {
return cb();
}
},
function (cb) {
library.db.tx(Tick).then(function () {
return cb();
}).catch(function (err) {
library.logger.error(err.toString());
return cb(err);
});
}
], function (err) {
return done(err);
});
}
Round.prototype.sandboxApi = function (call, args, cb) {
sandboxHelper.callMethod(shared, call, args, cb);
}
// Events
Round.prototype.onBind = function (scope) {
modules = scope;
}
Round.prototype.onBlockchainReady = function () {
var round = self.calc(modules.blocks.getLastBlock().height);
library.db.query(sql.summedRound, { round: round, activeDelegates:constants.activeDelegates }).then(function (rows) {
var rewards = [];
rows[0].rewards.forEach(function (reward) {
rewards.push(Math.floor(reward));
});
private.feesByRound[round] = Math.floor(rows[0].fees);
private.rewardsByRound[round] = rewards;
private.delegatesByRound[round] = rows[0].delegates;
private.loaded = true;
}).catch(function (err) {
library.logger.error(err.toString());
return cb("Round#onBlockchainReady error");
});
}
Round.prototype.onFinishRound = function (round) {
library.network.io.sockets.emit("rounds/change", {number: round});
}
Round.prototype.cleanup = function (cb) {
private.loaded = false;
cb();
}
// Private
private.getOutsiders = function (scope, cb) {
scope.outsiders = [];
if (scope.block.height == 1) {
return cb();
}
modules.delegates.generateDelegateList(scope.block.height, function (err, roundDelegates) {
if (err) {
return cb(err);
}
async.eachSeries(roundDelegates, function (delegate, eachCb) {
if (scope.delegates.indexOf(delegate) == -1) {
scope.outsiders.push(modules.accounts.generateAddressByPublicKey(delegate));
}
return eachCb();
}, function (err) {
return cb(err);
});
});
}
// Shared
// Export
module.exports = Round;