-
Notifications
You must be signed in to change notification settings - Fork 3
/
cardanoLeaderLogs.js
177 lines (152 loc) · 4.59 KB
/
cardanoLeaderLogs.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
const fs = require('fs')
const cp = require('child_process')
const axios = require('axios')
const prompt = require('prompt')
console.log(' process args:', process.argv)
if (process.argv.length < 4) {
throw Error(
'Usage: node cardanoLeaderLogs.js <path/to/leaderLogs.config> <previous | current | next (epoch)>'
)
}
const params = JSON.parse(fs.readFileSync(process.argv[2]))
if (
!params.hasOwnProperty('poolIdBech32') ||
!params.hasOwnProperty('vrfSkey') ||
!params.hasOwnProperty('genesisShelley') ||
!params.hasOwnProperty('libsodiumBinary') ||
!params.hasOwnProperty('timeZone')
) {
throw Error('Invalid leaderLogsConfig.json')
}
const genesisShelley = JSON.parse(fs.readFileSync(params.genesisShelley))
const targetEpoch = process.argv[3]
const targetSnapshot =
targetEpoch == 'previous'
? 'Go'
: targetEpoch == 'current'
? 'Set'
: targetEpoch == 'next'
? 'Mark'
: 'unknown'
if (targetSnapshot == 'unknown') {
throw Error(
'Invalid target epoch format, please use one of: <previous | current | next>.'
)
}
const poolId = params.poolId
const poolIdBech32 = params.poolIdBech32
const timeZone = params.timeZone
const vrfSkey = JSON.parse(fs.readFileSync(params.vrfSkey)).cborHex
function getFirstSlotOfEpoch (epoch, genesisShelley) {
// First slot of epoch 211
refSlot = 5788800
firstSlotOfEpoch = refSlot + (epoch - 211) * genesisShelley.epochLength
return firstSlotOfEpoch
}
async function getSnapshotDataFromKoios (poolIdBech32, targetSnapshot) {
const poolSnapshotUrl = `https://api.koios.rest/api/v0/pool_stake_snapshot?_pool_bech32=${poolIdBech32}&snapshot=eq.${targetSnapshot}`
const poolSnapshotResponse = await axios.get(poolSnapshotUrl)
const poolSnapshotData = poolSnapshotResponse.data[0]
if (poolSnapshotData.nonce == null) {
throw Error(
`Epoch nonce for epoch ${poolSnapshotData.epoch_no} is unavailable.`
)
}
if (
poolSnapshotData.pool_stake === null ||
poolSnapshotData.active_stake === null
) {
throw `Failed to get pool sigma from Koios for pool: ${poolIdBech32}, epoch: ${epoch}.`
}
console.log(' Epoch:', poolSnapshotData.epoch_no)
console.log(' Active stake:', poolSnapshotData.pool_stake)
console.log(' Total stake:', poolSnapshotData.active_stake)
return {
epoch_no: poolSnapshotData.epoch_no,
nonce: poolSnapshotData.nonce,
sigma: poolSnapshotData.pool_stake / poolSnapshotData.active_stake
}
}
async function getLeaderLogs (
firstSlotOfEpoch,
poolVrfSkey,
sigma,
nonce,
timeZone
) {
let sLeader = cp.spawnSync(
'python3',
[
'./isSlotLeader.py',
'--first-slot-of-epoch',
firstSlotOfEpoch,
'--genesis-start',
genesisShelley.systemStart,
'--epoch-nonce',
nonce,
'--vrf-skey',
poolVrfSkey,
'--sigma',
sigma,
'--d',
0,
'--epoch-length',
genesisShelley.epochLength,
'--active-slots-coeff',
genesisShelley.activeSlotsCoeff,
'--libsodium-binary',
params.libsodiumBinary,
'--time-zone',
timeZone
],
{ encoding: 'utf8' }
)
sLeaderOutput = sLeader.stdout
let slots = JSON.parse(sLeaderOutput)
let expectedBlocks = sigma * 21600
console.log('')
console.log('expected blocks with d == ' + 0 + ':', expectedBlocks.toFixed(2))
console.log(
'assigned blocks with d == ' + 0 + ':',
slots.length,
'max performance:',
((slots.length / expectedBlocks) * 100).toFixed(2) + '%'
)
console.log('')
console.log(slots)
}
async function calculateLeaderLogs () {
console.log(' Network: mainnet')
console.log(
` Starting leader logs calculation for ${targetEpoch} epoch`
)
console.log(` Loading: pool stake snapshot data from Koios`)
const poolSnapshotData = await getSnapshotDataFromKoios(
poolIdBech32,
targetSnapshot
)
console.log(
` Loading: first slot of epoch ${poolSnapshotData.epoch_no}`
)
const firstSlotOfEpoch = await getFirstSlotOfEpoch(
poolSnapshotData.epoch_no,
genesisShelley
)
const poolVrfSkey = vrfSkey.substr(4)
console.log(' firstSlotOfEpoch:', firstSlotOfEpoch)
console.log(' sigma:', poolSnapshotData.sigma)
console.log(' pool VRF sKey:', poolVrfSkey)
console.log('')
console.log(' Calculating leader slots...')
await getLeaderLogs(
firstSlotOfEpoch,
poolVrfSkey,
poolSnapshotData.sigma,
poolSnapshotData.nonce,
timeZone
)
}
async function main () {
await calculateLeaderLogs()
}
main()