-
Notifications
You must be signed in to change notification settings - Fork 5
/
nodeUtils.js
107 lines (68 loc) · 2.15 KB
/
nodeUtils.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
const http = require('http')
const nodeStats = {}
function getEpoch() {
return nodeStats.cardano_node_ChainDB_metrics_epoch_int
}
function getSlot() {
return nodeStats.cardano_node_ChainDB_metrics_slotNum_int
}
function getSlotInEpoch() {
return nodeStats.cardano_node_ChainDB_metrics_slotInEpoch_int
}
function getShelleyTransitionEpoch(byron, shelley) {
const slotInEpoch = getSlotInEpoch()
const slot = getSlot()
const byronEpochLength = 10 * byron.protocolConsts.k
let byronEpochs = getEpoch()
let shelleyEpochs = 0
let calcSlot = 0
while(byronEpochs >= 0) {
calcSlot = (byronEpochs * byronEpochLength) + (shelleyEpochs * shelley.epochLength) + slotInEpoch
if(calcSlot === slot) {
break
}
byronEpochs--
shelleyEpochs++
}
if (calcSlot !== slot || shelleyEpochs === 0) {
return -1
}
return byronEpochs
}
function getFirstSlotOfEpoch(byron, shelley, absoluteSlot) {
const shelleyTransitionEpoch = getShelleyTransitionEpoch(byron, shelley)
if(shelleyTransitionEpoch === -1) { return -1 }
const byronEpochLength = 10 * byron.protocolConsts.k
const byronSlots = byronEpochLength * shelleyTransitionEpoch
const shelleySlots = absoluteSlot - byronSlots
const shelleySlotInEpoch = shelleySlots % shelley.epochLength
return absoluteSlot - shelleySlotInEpoch
}
function updateNodeStats(nodeStatsURL) {
return new Promise((resolve, reject) => {
http.get(nodeStatsURL, (res) => {
let data = '';
res.on('data', (chunk) => { data += chunk; });
res.on('end', () => {
const nodeStatsArray = data.split('\n')
for(let i = 0; i < nodeStatsArray.length; i++) {
const entry = nodeStatsArray[i].split(' ')
if(entry[0] && entry[0].length > 0) {
nodeStats[entry[0]] = Number.parseFloat(entry[1])
}
}
// console.log('updateNodeStats:', nodeStats)
resolve()
});
}).on("error", (err) => {
reject(err)
});
})
}
module.exports = {
getEpoch,
getSlot,
getSlotInEpoch,
getFirstSlotOfEpoch,
updateNodeStats
}