-
Notifications
You must be signed in to change notification settings - Fork 26
/
slots.go
337 lines (315 loc) · 10.2 KB
/
slots.go
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
package handlers
import (
"bytes"
"fmt"
"math"
"net/http"
"strconv"
"time"
"github.com/pk910/dora/services"
"github.com/pk910/dora/templates"
"github.com/pk910/dora/types/models"
"github.com/pk910/dora/utils"
"github.com/sirupsen/logrus"
)
// Slots will return the main "slots" page using a go template
func Slots(w http.ResponseWriter, r *http.Request) {
var slotsTemplateFiles = append(layoutTemplateFiles,
"slots/slots.html",
"_svg/professor.html",
)
var pageTemplate = templates.GetTemplate(slotsTemplateFiles...)
data := InitPageData(w, r, "blockchain", "/slots", "Slots", slotsTemplateFiles)
urlArgs := r.URL.Query()
var pageSize uint64 = 50
if urlArgs.Has("c") {
pageSize, _ = strconv.ParseUint(urlArgs.Get("c"), 10, 64)
}
var firstSlot uint64 = math.MaxUint64
if urlArgs.Has("s") {
firstSlot, _ = strconv.ParseUint(urlArgs.Get("s"), 10, 64)
}
var pageError error
pageError = services.GlobalCallRateLimiter.CheckCallLimit(r, 1)
if pageError == nil {
data.Data, pageError = getSlotsPageData(firstSlot, pageSize)
}
if pageError != nil {
handlePageError(w, r, pageError)
return
}
w.Header().Set("Content-Type", "text/html")
if handleTemplateError(w, r, "slots.go", "Slots", "", pageTemplate.ExecuteTemplate(w, "layout", data)) != nil {
return // an error has occurred and was processed
}
}
func getSlotsPageData(firstSlot uint64, pageSize uint64) (*models.SlotsPageData, error) {
pageData := &models.SlotsPageData{}
pageCacheKey := fmt.Sprintf("slots:%v:%v", firstSlot, pageSize)
pageRes, pageErr := services.GlobalFrontendCache.ProcessCachedPage(pageCacheKey, true, pageData, func(pageCall *services.FrontendCacheProcessingPage) interface{} {
pageData, cacheTimeout := buildSlotsPageData(firstSlot, pageSize)
pageCall.CacheTimeout = cacheTimeout
return pageData
})
if pageErr == nil && pageRes != nil {
resData, resOk := pageRes.(*models.SlotsPageData)
if !resOk {
return nil, ErrInvalidPageModel
}
pageData = resData
}
return pageData, pageErr
}
func buildSlotsPageData(firstSlot uint64, pageSize uint64) (*models.SlotsPageData, time.Duration) {
logrus.Debugf("slots page called: %v:%v", firstSlot, pageSize)
pageData := &models.SlotsPageData{}
now := time.Now()
currentSlot := utils.TimeToSlot(uint64(now.Unix()))
currentEpoch := utils.EpochOfSlot(currentSlot)
maxSlot := currentSlot + 8
if maxSlot >= (currentEpoch+1)*utils.Config.Chain.Config.SlotsPerEpoch {
maxSlot = ((currentEpoch + 1) * utils.Config.Chain.Config.SlotsPerEpoch) - 1
}
if firstSlot > uint64(maxSlot) {
pageData.IsDefaultPage = true
firstSlot = uint64(maxSlot)
}
if pageSize > 100 {
pageSize = 100
}
pagesBefore := (firstSlot + 1) / pageSize
if ((firstSlot + 1) % pageSize) > 0 {
pagesBefore++
}
pagesAfter := (maxSlot - firstSlot) / pageSize
if ((maxSlot - firstSlot) % pageSize) > 0 {
pagesAfter++
}
pageData.PageSize = pageSize
pageData.TotalPages = pagesBefore + pagesAfter
pageData.CurrentPageIndex = pagesAfter + 1
pageData.CurrentPageSlot = firstSlot
pageData.PrevPageIndex = pageData.CurrentPageIndex - 1
pageData.PrevPageSlot = pageData.CurrentPageSlot + pageSize
if pageData.CurrentPageSlot >= pageSize {
pageData.NextPageIndex = pageData.CurrentPageIndex + 1
pageData.NextPageSlot = pageData.CurrentPageSlot - pageSize
}
pageData.LastPageSlot = pageSize - 1
finalizedEpoch, _ := services.GlobalBeaconService.GetFinalizedEpoch()
slotLimit := pageSize - 1
var lastSlot uint64
if firstSlot > uint64(slotLimit) {
lastSlot = firstSlot - uint64(slotLimit)
} else {
lastSlot = 0
}
// get slot assignments
firstEpoch := utils.EpochOfSlot(firstSlot)
lastEpoch := utils.EpochOfSlot(lastSlot)
slotAssignments, syncedEpochs := services.GlobalBeaconService.GetProposerAssignments(firstEpoch, lastEpoch)
// load slots
pageData.Slots = make([]*models.SlotsPageDataSlot, 0)
dbSlots := services.GlobalBeaconService.GetDbBlocksForSlots(uint64(firstSlot), uint32(pageSize), true)
dbIdx := 0
dbCnt := len(dbSlots)
blockCount := uint64(0)
allFinalized := true
allSynchronized := true
isFirstPage := firstSlot >= currentSlot
openForks := map[int][]byte{}
maxOpenFork := 0
for slotIdx := int64(firstSlot); slotIdx >= int64(lastSlot); slotIdx-- {
slot := uint64(slotIdx)
finalized := finalizedEpoch >= int64(utils.EpochOfSlot(slot))
if !finalized {
allFinalized = false
}
haveBlock := false
for dbIdx < dbCnt && dbSlots[dbIdx] != nil && dbSlots[dbIdx].Slot == slot {
dbSlot := dbSlots[dbIdx]
dbIdx++
blockStatus := uint8(1)
if dbSlot.Orphaned == 1 {
blockStatus = 2
}
slotData := &models.SlotsPageDataSlot{
Slot: slot,
Epoch: utils.EpochOfSlot(slot),
Ts: utils.SlotToTime(slot),
Finalized: finalized,
Status: blockStatus,
Synchronized: true,
Proposer: dbSlot.Proposer,
ProposerName: services.GlobalBeaconService.GetValidatorName(dbSlot.Proposer),
AttestationCount: dbSlot.AttestationCount,
DepositCount: dbSlot.DepositCount,
ExitCount: dbSlot.ExitCount,
ProposerSlashingCount: dbSlot.ProposerSlashingCount,
AttesterSlashingCount: dbSlot.AttesterSlashingCount,
SyncParticipation: float64(dbSlot.SyncParticipation) * 100,
EthTransactionCount: dbSlot.EthTransactionCount,
Graffiti: dbSlot.Graffiti,
BlockRoot: dbSlot.Root,
ParentRoot: dbSlot.ParentRoot,
ForkGraph: make([]*models.SlotsPageDataForkGraph, 0),
}
if dbSlot.EthBlockNumber != nil {
slotData.WithEthBlock = true
slotData.EthBlockNumber = *dbSlot.EthBlockNumber
}
pageData.Slots = append(pageData.Slots, slotData)
blockCount++
haveBlock = true
buildSlotsPageSlotGraph(pageData, slotData, &maxOpenFork, openForks, isFirstPage)
}
if !haveBlock {
epoch := utils.EpochOfSlot(slot)
slotData := &models.SlotsPageDataSlot{
Slot: slot,
Epoch: epoch,
Ts: utils.SlotToTime(slot),
Finalized: finalized,
Scheduled: epoch >= currentEpoch,
Status: 0,
Synchronized: syncedEpochs[epoch],
Proposer: slotAssignments[slot],
ProposerName: services.GlobalBeaconService.GetValidatorName(slotAssignments[slot]),
}
if !slotData.Synchronized {
allSynchronized = false
}
pageData.Slots = append(pageData.Slots, slotData)
blockCount++
buildSlotsPageSlotGraph(pageData, slotData, &maxOpenFork, openForks, isFirstPage)
}
}
pageData.SlotCount = uint64(blockCount)
pageData.FirstSlot = firstSlot
pageData.LastSlot = lastSlot
pageData.ForkTreeWidth = (maxOpenFork * 20) + 20
var cacheTimeout time.Duration
if !allSynchronized {
cacheTimeout = 30 * time.Second
} else if allFinalized {
cacheTimeout = 30 * time.Minute
} else if firstEpoch < uint64(currentEpoch) {
cacheTimeout = 10 * time.Minute
} else {
cacheTimeout = 12 * time.Second
}
return pageData, cacheTimeout
}
func buildSlotsPageSlotGraph(pageData *models.SlotsPageData, slotData *models.SlotsPageDataSlot, maxOpenFork *int, openForks map[int][]byte, isFirstPage bool) {
// fork tree
var forkGraphIdx int = -1
var freeForkIdx int = -1
getForkGraph := func(slotData *models.SlotsPageDataSlot, forkIdx int) *models.SlotsPageDataForkGraph {
forkGraph := &models.SlotsPageDataForkGraph{}
graphCount := len(slotData.ForkGraph)
if graphCount > forkIdx {
forkGraph = slotData.ForkGraph[forkIdx]
} else {
for graphCount <= forkIdx {
forkGraph = &models.SlotsPageDataForkGraph{
Index: graphCount,
Left: 10 + (graphCount * 20),
Tiles: map[string]bool{},
}
slotData.ForkGraph = append(slotData.ForkGraph, forkGraph)
graphCount++
}
}
return forkGraph
}
for forkIdx := 0; forkIdx < *maxOpenFork; forkIdx++ {
forkGraph := getForkGraph(slotData, forkIdx)
if openForks[forkIdx] == nil {
if freeForkIdx == -1 {
freeForkIdx = forkIdx
}
continue
} else {
forkGraph.Tiles["vline"] = true
if bytes.Equal(openForks[forkIdx], slotData.BlockRoot) {
if forkGraphIdx != -1 {
continue
}
forkGraphIdx = forkIdx
openForks[forkIdx] = slotData.ParentRoot
forkGraph.Block = true
for targetIdx := forkIdx + 1; targetIdx < *maxOpenFork; targetIdx++ {
if openForks[targetIdx] == nil || !bytes.Equal(openForks[targetIdx], slotData.BlockRoot) {
continue
}
for idx := forkIdx + 1; idx <= targetIdx; idx++ {
splitGraph := getForkGraph(slotData, idx)
if idx == targetIdx {
splitGraph.Tiles["tline"] = true
splitGraph.Tiles["lline"] = true
splitGraph.Tiles["fork"] = true
} else {
splitGraph.Tiles["hline"] = true
}
}
forkGraph.Tiles["rline"] = true
openForks[targetIdx] = nil
}
}
}
}
if forkGraphIdx == -1 && slotData.Status > 0 {
// fork head
hasHead := false
hasForks := false
if !isFirstPage {
// get blocks that build on top of this
refBlocks := services.GlobalBeaconService.GetDbBlocksByParentRoot(slotData.BlockRoot)
refBlockCount := len(refBlocks)
if refBlockCount > 0 {
freeForkIdx = *maxOpenFork
*maxOpenFork++
hasHead = true
// add additional forks
if refBlockCount > 1 {
for idx := 1; idx < refBlockCount; idx++ {
graphIdx := *maxOpenFork
*maxOpenFork++
splitGraph := getForkGraph(slotData, graphIdx)
splitGraph.Tiles["tline"] = true
splitGraph.Tiles["lline"] = true
splitGraph.Tiles["fork"] = true
if idx < refBlockCount-1 {
splitGraph.Tiles["hline"] = true
}
}
}
// add line up to the top for each fork
for _, slot := range pageData.Slots {
if bytes.Equal(slot.BlockRoot, slotData.BlockRoot) {
continue
}
for idx := 0; idx < refBlockCount; idx++ {
splitGraph := getForkGraph(slot, freeForkIdx+idx)
splitGraph.Tiles["vline"] = true
}
}
}
}
if freeForkIdx == -1 {
freeForkIdx = *maxOpenFork
*maxOpenFork++
}
openForks[freeForkIdx] = slotData.ParentRoot
forkGraph := getForkGraph(slotData, freeForkIdx)
forkGraph.Block = true
if hasHead {
forkGraph.Tiles["vline"] = true
if hasForks {
forkGraph.Tiles["rline"] = true
}
} else {
forkGraph.Tiles["bline"] = true
}
}
}