-
Notifications
You must be signed in to change notification settings - Fork 17
/
epochs.go
172 lines (159 loc) · 5.63 KB
/
epochs.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
package handlers
import (
"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"
)
// Epochs will return the main "epochs" page using a go template
func Epochs(w http.ResponseWriter, r *http.Request) {
var indexTemplateFiles = append(layoutTemplateFiles,
"epochs/epochs.html",
"_svg/professor.html",
)
var pageTemplate = templates.GetTemplate(indexTemplateFiles...)
data := InitPageData(w, r, "blockchain", "/epochs", "Epochs", indexTemplateFiles)
urlArgs := r.URL.Query()
var firstEpoch uint64 = math.MaxUint64
if urlArgs.Has("epoch") {
firstEpoch, _ = strconv.ParseUint(urlArgs.Get("epoch"), 10, 64)
}
var pageSize uint64 = 50
if urlArgs.Has("count") {
pageSize, _ = strconv.ParseUint(urlArgs.Get("count"), 10, 64)
}
var pageError error
pageError = services.GlobalCallRateLimiter.CheckCallLimit(r, 1)
if pageError == nil {
data.Data, pageError = getEpochsPageData(firstEpoch, pageSize)
}
if pageError != nil {
handlePageError(w, r, pageError)
return
}
w.Header().Set("Content-Type", "text/html")
if handleTemplateError(w, r, "epochs.go", "Epochs", "", pageTemplate.ExecuteTemplate(w, "layout", data)) != nil {
return // an error has occurred and was processed
}
}
func getEpochsPageData(firstEpoch uint64, pageSize uint64) (*models.EpochsPageData, error) {
pageData := &models.EpochsPageData{}
pageCacheKey := fmt.Sprintf("epochs:%v:%v", firstEpoch, pageSize)
pageRes, pageErr := services.GlobalFrontendCache.ProcessCachedPage(pageCacheKey, true, pageData, func(pageCall *services.FrontendCacheProcessingPage) interface{} {
pageData, cacheTimeout := buildEpochsPageData(firstEpoch, pageSize)
pageCall.CacheTimeout = cacheTimeout
return pageData
})
if pageErr == nil && pageRes != nil {
resData, resOk := pageRes.(*models.EpochsPageData)
if !resOk {
return nil, ErrInvalidPageModel
}
pageData = resData
}
return pageData, pageErr
}
func buildEpochsPageData(firstEpoch uint64, pageSize uint64) (*models.EpochsPageData, time.Duration) {
logrus.Debugf("epochs page called: %v:%v", firstEpoch, pageSize)
pageData := &models.EpochsPageData{}
now := time.Now()
currentEpoch := utils.TimeToEpoch(now)
if currentEpoch < 0 {
currentEpoch = 0
}
if firstEpoch > uint64(currentEpoch) {
pageData.IsDefaultPage = true
firstEpoch = uint64(currentEpoch)
}
if pageSize > 100 {
pageSize = 100
}
pagesBefore := (firstEpoch + 1) / pageSize
if ((firstEpoch + 1) % pageSize) > 0 {
pagesBefore++
}
pagesAfter := (uint64(currentEpoch) - firstEpoch) / pageSize
if ((uint64(currentEpoch) - firstEpoch) % pageSize) > 0 {
pagesAfter++
}
pageData.PageSize = pageSize
pageData.TotalPages = pagesBefore + pagesAfter
pageData.CurrentPageIndex = pagesAfter + 1
pageData.CurrentPageEpoch = firstEpoch
pageData.PrevPageIndex = pageData.CurrentPageIndex - 1
pageData.PrevPageEpoch = pageData.CurrentPageEpoch + pageSize
if pageData.CurrentPageEpoch >= pageSize {
pageData.NextPageIndex = pageData.CurrentPageIndex + 1
pageData.NextPageEpoch = pageData.CurrentPageEpoch - pageSize
}
pageData.LastPageEpoch = pageSize - 1
finalizedEpoch, _, justifiedEpoch, _ := services.GlobalBeaconService.GetIndexer().GetFinalizationCheckpoints()
epochLimit := pageSize
// load epochs
pageData.Epochs = make([]*models.EpochsPageDataEpoch, 0)
dbEpochs := services.GlobalBeaconService.GetDbEpochs(uint64(firstEpoch), uint32(epochLimit))
dbIdx := 0
dbCnt := len(dbEpochs)
epochCount := uint64(0)
allFinalized := true
allSynchronized := true
for epochIdx := int64(firstEpoch); epochIdx >= 0 && epochCount < epochLimit; epochIdx-- {
epoch := uint64(epochIdx)
finalized := finalizedEpoch >= epochIdx
if !finalized {
allFinalized = false
}
epochData := &models.EpochsPageDataEpoch{
Epoch: epoch,
Ts: utils.EpochToTime(epoch),
Finalized: finalized,
Justified: justifiedEpoch >= epochIdx,
}
if dbIdx < dbCnt && dbEpochs[dbIdx] != nil && dbEpochs[dbIdx].Epoch == epoch {
dbEpoch := dbEpochs[dbIdx]
dbIdx++
epochData.Synchronized = true
epochData.CanonicalBlockCount = uint64(dbEpoch.BlockCount)
epochData.OrphanedBlockCount = uint64(dbEpoch.OrphanedCount)
epochData.AttestationCount = dbEpoch.AttestationCount
epochData.DepositCount = dbEpoch.DepositCount
epochData.ExitCount = dbEpoch.ExitCount
epochData.ProposerSlashingCount = dbEpoch.ProposerSlashingCount
epochData.AttesterSlashingCount = dbEpoch.AttesterSlashingCount
epochData.EligibleEther = dbEpoch.Eligible
epochData.TargetVoted = dbEpoch.VotedTarget
epochData.HeadVoted = dbEpoch.VotedHead
epochData.TotalVoted = dbEpoch.VotedTotal
if dbEpoch.Eligible > 0 {
epochData.TargetVoteParticipation = float64(dbEpoch.VotedTarget) * 100.0 / float64(dbEpoch.Eligible)
epochData.HeadVoteParticipation = float64(dbEpoch.VotedHead) * 100.0 / float64(dbEpoch.Eligible)
epochData.TotalVoteParticipation = float64(dbEpoch.VotedTotal) * 100.0 / float64(dbEpoch.Eligible)
}
epochData.EthTransactionCount = dbEpoch.EthTransactionCount
} else {
allSynchronized = false
}
pageData.Epochs = append(pageData.Epochs, epochData)
epochCount++
}
pageData.EpochCount = uint64(epochCount)
pageData.FirstEpoch = firstEpoch
pageData.LastEpoch = firstEpoch - pageData.EpochCount + 1
var cacheTimeout time.Duration
if !allSynchronized {
cacheTimeout = 30 * time.Second
} else if allFinalized {
cacheTimeout = 30 * time.Minute
} else if firstEpoch+2 < uint64(currentEpoch) {
cacheTimeout = 10 * time.Minute
} else {
cacheTimeout = 12 * time.Second
}
return pageData, cacheTimeout
}