-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
421 lines (359 loc) · 13 KB
/
Copy pathindex.js
File metadata and controls
421 lines (359 loc) · 13 KB
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
const mongoose = require('mongoose');
const lodash = require('lodash');
const cache = require('memory-cache');
const async = require('async');
const mpapi = require('./utils/mpapi');
const config = require('./config');
const constants = require('./constants');
const runPayment = require('./payment');
const Settings = require('./models/settings')();
const BakerCycle = require('./models/bakerCycle')();
const Reward = require('./models/reward')();
const RewardState = require('./models/rewardState')();
const PRESERVES_CYCLE = 5 + 2;
const BLOCKS_IN_CYCLE = 1440;
const TIME_BETWEEN_BLOCKS = 60;
const STEP_PROCESS_CYCLE = 500;
const blocksCache = new cache.Cache();
const blockConstantsCache = new cache.Cache();
const cycleInfoCache = new cache.Cache();
const getBlock = async (level = 'head') => {
const cachedBlock = blocksCache.get(level);
if (!cachedBlock) {
const block = await mpapi.rpc.getHead(level);
blocksCache.put(block.header.level, block, BLOCKS_IN_CYCLE * PRESERVES_CYCLE * TIME_BETWEEN_BLOCKS);
return block;
}
return cachedBlock;
}
const getBlockConstants = async (level) => {
if (!lodash.isNumber(level)) {
throw new Error('Level must be a number');
}
const cachedBlockConstants = blockConstantsCache.get(level);
if (!cachedBlockConstants) {
const blockConstants = await mpapi.rpc.getConstants(level);
blockConstantsCache.put(level, blockConstants, BLOCKS_IN_CYCLE * PRESERVES_CYCLE * TIME_BETWEEN_BLOCKS);
return blockConstants;
}
return cachedBlockConstants;
}
const getCycleInfo = async (cycle) => {
if (!lodash.isNumber(cycle)) {
throw new Error('Cycle must be a number');
}
const cachedCycleInfo = cycleInfoCache.get(cycle);
if (!cachedCycleInfo) {
const cycleInfo = await mpapi.rpc.getLevelsInCurrentCycle(BLOCKS_IN_CYCLE * cycle + 1)
cycleInfoCache.put(cycle, cycleInfo, BLOCKS_IN_CYCLE * PRESERVES_CYCLE * TIME_BETWEEN_BLOCKS);
return cycleInfo;
}
return cachedCycleInfo;
}
const isInBakerList = (baker) => config.BAKER_LIST.indexOf(baker) >= 0;
const getBlockEndorsers = (operations) => {
const findEndorsers = (operations) => {
return operations.filter(operation => {
if (Array.isArray(operation)) {
return findEndorsers(operation).length > 0 ? true : false;
} else {
if (operation.contents)
return findEndorsers(operation.contents).length > 0 ? true : false;
else {
return operation.kind === 'endorsement' ? true : false;
}
}
})
}
const endorserOperations = lodash.flattenDeep(findEndorsers(operations));
return endorserOperations.map(operation => ({
address: operation.contents[0].metadata.delegate,
slots: operation.contents[0].metadata.slots.length,
level: operation.contents[0].level,
}))
}
const getDelegatedAddresses = async (baker, level) => {
const delegatedAddresses = await mpapi.rpc.getDelegatedAddresses(baker, level);
return await async.mapLimit(
delegatedAddresses.filter(address => address !== baker),
2,
async (address) => ({
address,
balance: mpapi.utility.totez(
await mpapi.rpc.getMineBalance(address, level)
)
})
)
}
const getBakerCycle = async (baker, cycle) => {
const bakerCycle = await BakerCycle.findOne({
address: baker,
cycle: cycle,
});
if (bakerCycle) {
return bakerCycle;
}
const cycleInfo = await getCycleInfo(cycle);
let minDelegatorsBalances = [];
let minFullStakingBalance = 0;
let minOwnBalance = 0;
let minDelegatedBalance = 0;
for (let level = cycleInfo.first; level <= cycleInfo.last; level += STEP_PROCESS_CYCLE) {
console.log(`Start checking for ${baker} in ${level}`);
const gettingData = async (attemp) => {
try {
const levelDelegatorsBalances = await getDelegatedAddresses(baker, level);
const fullStakingBalance = mpapi.utility.totez(await mpapi.rpc.getStakingMineBalance(baker, level));
const ownBalance = mpapi.utility.totez(await mpapi.rpc.getOwnStakingMineBalance(baker, level));
const delegatedBalance = mpapi.utility.totez(await mpapi.rpc.getDelegatedBalance(baker, level));
return {
levelDelegatorsBalances,
fullStakingBalance,
ownBalance,
delegatedBalance
};
} catch (error) {
console.log(`There is an error ${error} at getting data, attemp ${attemp}`);
console.log('Repeat for getting data');
return await gettingData(++attemp);
}
}
const {levelDelegatorsBalances, fullStakingBalance, ownBalance, delegatedBalance} = await gettingData(0);
if (level == cycleInfo.first) {
minDelegatorsBalances = levelDelegatorsBalances;
minFullStakingBalance = fullStakingBalance;
minOwnBalance = ownBalance;
minDelegatedBalance = delegatedBalance;
}
minFullStakingBalance = lodash.min([minFullStakingBalance, fullStakingBalance]);
minOwnBalance = lodash.min([minOwnBalance, ownBalance]);
minDelegatedBalance = lodash.min([minDelegatedBalance, delegatedBalance]);
const stableLevelDelegators = lodash.intersectionBy(levelDelegatorsBalances, minDelegatorsBalances, 'address');
if (!stableLevelDelegators.length) {
minDelegatorsBalances = [];
break;
}
if (stableLevelDelegators.length !== minDelegatorsBalances.length) {
minDelegatorsBalances = lodash.intersectionBy(minDelegatorsBalances, stableLevelDelegators, 'address');
}
minDelegatorsBalances = lodash.zipWith(stableLevelDelegators, minDelegatorsBalances, (levelDelegator, cycleDelegator) => {
return {
address: levelDelegator.address,
balance: lodash.min([cycleDelegator.balance, levelDelegator.balance])
};
});
}
return await BakerCycle.findOneAndUpdate({
address: baker,
cycle: cycle,
}, {
$set: {
baker,
cycle,
minFullStakingBalance,
minOwnBalance,
minDelegatedBalance,
fullCycleDelegators: minDelegatorsBalances.map(delegator => ({
address: delegator.address,
minDelegatedBalance: delegator.balance
}))
}
}, {
upsert: true,
new: true
});
}
const getRewards = async (block, type = constants.REWARD_TYPES.FOR_BAKING, baker, {endorsers = [], slots = 0}) => {
const level = block.metadata.level.level;
const cycle = block.metadata.level.cycle;
const priority = block.header.priority;
const {baking_reward_per_endorsement, endorsement_reward} = await getBlockConstants(level);
const bakerCycle = await getBakerCycle(baker, cycle - PRESERVES_CYCLE);
if (!bakerCycle) {
return [];
}
let totalReward = 0;
switch (type) {
case constants.REWARD_TYPES.FOR_BAKING:
const countEndorsers = endorsers.reduce((count, endorser) => count + endorser.slots, 0)
if (baking_reward_per_endorsement) {
if (priority === 0)
totalReward = baking_reward_per_endorsement[0] * countEndorsers;
else
totalReward = baking_reward_per_endorsement[1] * countEndorsers;
}
break;
case constants.REWARD_TYPES.FOR_ENDORSING:
if (endorsement_reward) {
if (priority === 0)
totalReward = endorsement_reward[0] * slots;
else
totalReward = endorsement_reward[1] * slots;
}
break;
}
totalReward = mpapi.utility.totez(totalReward);
let rewardOfAddresses = [];
if (bakerCycle.fullCycleDelegators.length) {
rewardOfAddresses = bakerCycle.fullCycleDelegators.map(delegator => ({
address: delegator.address,
reward: lodash.floor(totalReward / bakerCycle.minFullStakingBalance * delegator.minDelegatedBalance, 7),
type,
metadata: {
cycle,
priority,
level,
totalReward,
countEndorsers: endorsers.length,
countSlots: slots,
bakingRewardConstant: baking_reward_per_endorsement,
endorsementRewardConstant: endorsement_reward,
minDelegatedBalance: delegator.minDelegatedBalance
}
}));
}
return rewardOfAddresses;
}
const getRewardsForBaker = async (block, bakerAddress, endorsers) => {
return await getRewards(block, constants.REWARD_TYPES.FOR_BAKING, bakerAddress, {endorsers});
}
const getRewardsForEndorser = async (block, endorserAddress, slots) => {
return await getRewards(block, constants.REWARD_TYPES.FOR_ENDORSING, endorserAddress, {slots});
}
const saveRewards = async (bakerAddress, rewards) => {
const updateDataReward = []
const updateDataRewardState = []
for (const reward of rewards) {
if (reward.reward > 0) {
updateDataReward.push({
updateOne: {
filter: {
from: bakerAddress,
to: reward.address,
level: reward.metadata.level,
type: reward.type
},
update: {
$set: {
from: bakerAddress,
to: reward.address,
amount: reward.reward,
level: reward.metadata.level,
type: reward.type,
metadata: reward.metadata
}
},
upsert: true
}
});
updateDataRewardState.push({
updateOne: {
filter: {
from: bakerAddress,
to: reward.address,
cycle: reward.metadata.cycle,
type: reward.type
},
update: {
$inc: {
amount: reward.reward,
}
},
upsert: true
}
})
}
}
await Reward.bulkWrite(updateDataReward)
await RewardState.bulkWrite(updateDataRewardState)
}
const handleBlock = async (block, nextBlock) => {
const baker = block.metadata.baker;
const blockEndorsers = getBlockEndorsers(nextBlock.operations);
if (isInBakerList(baker)) {
const rewards = await getRewardsForBaker(block, baker, blockEndorsers);
console.log(`Found ${rewards.length} rewards for baking ${baker}`);
let startTime = new Date().getTime();
console.log(`Start save rewards. Run time: ${startTime}`);
await saveRewards(baker, rewards);
console.log(`End save rewards. Run time: ${new Date().getTime() - startTime}`);
}
await async.eachLimit(blockEndorsers, 1, async (endorser) => {
if (isInBakerList(endorser.address)) {
const rewards = await getRewardsForEndorser(block, endorser.address, endorser.slots);
console.log(`Found ${rewards.length} rewards for endorsing ${endorser.address}`);
let startTime = new Date().getTime();
console.log(`Start save rewards. Run time: ${startTime}`);
await saveRewards(endorser.address, rewards);
console.log(`End save rewards. Run time: ${new Date().getTime() - startTime}`);
}
});
}
mongoose.connect(config.MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false
}, async (error) => {
if (error) throw error;
const startIndex = async () => {
const {lastIndexedLevel} = await Settings.findOne() || {};
const head = await getBlock();
let level = lodash.max([
(lastIndexedLevel || 0) + 1,
config.START_INDEXING_LEVEL,
BLOCKS_IN_CYCLE * PRESERVES_CYCLE,
]);
console.log('Starting from', level)
while (true) {
// There must be at least one block ahead.
// We need it to get the next block.
if (level >= head.header.level) {
break;
}
try {
const block = await getBlock(level);
const nextBlock = await getBlock(level + 1);
const cycleInfo = await getCycleInfo(block.metadata.level.cycle);
console.log(`Current level is ${level}, block hash is ${block.hash}`);
let startTime = new Date().getTime();
await handleBlock(block, nextBlock);
const endTime = new Date().getTime();
console.log(`End of block handling. Run time: ${endTime - startTime}`);
await Settings.findOneAndUpdate({}, {
$set: {
lastIndexedLevel: level,
}
}, {
upsert: true
});
console.log(`Start cleaning old cycles lower than: ${level - BLOCKS_IN_CYCLE * 60}`);
startTime = new Date().getTime();
await Reward.deleteMany({
level: {$lt: level - BLOCKS_IN_CYCLE * 120},
});
await RewardState.deleteMany({
cycle: {$lt: block.metadata.level.cycle - 120},
});
console.log(`End cleaning old cycles. Run time: ${new Date().getTime() - startTime}`);
if (config.PAYMENT_SCRIPT.ENABLED_AUTOPAYMENT) {
if (level === cycleInfo.first + lodash.max([5, config.PAYMENT_SCRIPT.AUTOPAYMENT_LEVEL])) {
await Promise.all(lodash.map(config.PAYMENT_SCRIPT.BAKER_PRIVATE_KEYS, async (privateKey) => {
const bakerKeys = mpapi.crypto.extractKeys(privateKey);
await runPayment({bakerKeys, cycle: block.metadata.level.cycle - 1});
}))
}
}
} catch (error) {
console.log('Error on', level, error);
break;
}
level += 1;
}
console.log('Level is greater than the head, waiting...');
setTimeout(() => {
console.log('Continue indexing');
startIndex();
}, 1000 * 60)
};
startIndex();
});