-
Notifications
You must be signed in to change notification settings - Fork 17
/
epoch.go
212 lines (194 loc) · 6.94 KB
/
epoch.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
package handlers
import (
"fmt"
"net/http"
"strconv"
"time"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
"github.com/pk910/dora/services"
"github.com/pk910/dora/templates"
"github.com/pk910/dora/types/models"
"github.com/pk910/dora/utils"
)
// Epoch will return the main "epoch" page using a go template
func Epoch(w http.ResponseWriter, r *http.Request) {
var epochTemplateFiles = append(layoutTemplateFiles,
"epoch/epoch.html",
)
var notfoundTemplateFiles = append(layoutTemplateFiles,
"epoch/notfound.html",
)
var pageTemplate = templates.GetTemplate(epochTemplateFiles...)
var pageData *models.EpochPageData
var epoch uint64
vars := mux.Vars(r)
if vars["epoch"] != "" {
epoch, _ = strconv.ParseUint(vars["epoch"], 10, 64)
} else {
epoch = uint64(utils.TimeToEpoch(time.Now()))
}
var pageError error
pageError = services.GlobalCallRateLimiter.CheckCallLimit(r, 1)
if pageError == nil {
pageData, pageError = getEpochPageData(epoch)
}
if pageError != nil {
handlePageError(w, r, pageError)
return
}
if pageData == nil {
data := InitPageData(w, r, "blockchain", "/epoch", fmt.Sprintf("Epoch %v", epoch), notfoundTemplateFiles)
w.Header().Set("Content-Type", "text/html")
if handleTemplateError(w, r, "slot.go", "Slot", "blockSlot", templates.GetTemplate(notfoundTemplateFiles...).ExecuteTemplate(w, "layout", data)) != nil {
return // an error has occurred and was processed
}
return
}
data := InitPageData(w, r, "blockchain", "/epoch", fmt.Sprintf("Epoch %v", epoch), epochTemplateFiles)
data.Data = pageData
w.Header().Set("Content-Type", "text/html")
if handleTemplateError(w, r, "epoch.go", "Epoch", "", pageTemplate.ExecuteTemplate(w, "layout", data)) != nil {
return // an error has occurred and was processed
}
}
func getEpochPageData(epoch uint64) (*models.EpochPageData, error) {
pageData := &models.EpochPageData{}
pageCacheKey := fmt.Sprintf("epoch:%v", epoch)
pageRes, pageErr := services.GlobalFrontendCache.ProcessCachedPage(pageCacheKey, true, pageData, func(pageCall *services.FrontendCacheProcessingPage) interface{} {
pageData, cacheTimeout := buildEpochPageData(epoch)
pageCall.CacheTimeout = cacheTimeout
return pageData
})
if pageErr == nil && pageRes != nil {
resData, resOk := pageRes.(*models.EpochPageData)
if !resOk {
return nil, ErrInvalidPageModel
}
pageData = resData
}
return pageData, pageErr
}
func buildEpochPageData(epoch uint64) (*models.EpochPageData, time.Duration) {
logrus.Debugf("epoch page called: %v", epoch)
now := time.Now()
currentSlot := utils.TimeToSlot(uint64(now.Unix()))
currentEpoch := utils.EpochOfSlot(currentSlot)
if epoch > currentEpoch {
return nil, -1
}
finalizedEpoch, _ := services.GlobalBeaconService.GetFinalizedEpoch()
slotAssignments, syncedEpochs := services.GlobalBeaconService.GetProposerAssignments(epoch, epoch)
nextEpoch := epoch + 1
if nextEpoch > currentEpoch {
nextEpoch = 0
}
firstSlot := epoch * utils.Config.Chain.Config.SlotsPerEpoch
lastSlot := firstSlot + utils.Config.Chain.Config.SlotsPerEpoch - 1
pageData := &models.EpochPageData{
Epoch: epoch,
PreviousEpoch: epoch - 1,
NextEpoch: nextEpoch,
Ts: utils.EpochToTime(epoch),
Synchronized: syncedEpochs[epoch],
Finalized: finalizedEpoch >= int64(epoch),
}
dbEpochs := services.GlobalBeaconService.GetDbEpochs(epoch, 1)
dbEpoch := dbEpochs[0]
if dbEpoch != nil {
pageData.AttestationCount = dbEpoch.AttestationCount
pageData.DepositCount = dbEpoch.DepositCount
pageData.ExitCount = dbEpoch.ExitCount
pageData.WithdrawalCount = dbEpoch.WithdrawCount
pageData.WithdrawalAmount = dbEpoch.WithdrawAmount
pageData.ProposerSlashingCount = dbEpoch.ProposerSlashingCount
pageData.AttesterSlashingCount = dbEpoch.AttesterSlashingCount
pageData.EligibleEther = dbEpoch.Eligible
pageData.TargetVoted = dbEpoch.VotedTarget
pageData.HeadVoted = dbEpoch.VotedHead
pageData.TotalVoted = dbEpoch.VotedTotal
pageData.SyncParticipation = float64(dbEpoch.SyncParticipation) * 100
pageData.ValidatorCount = dbEpoch.ValidatorCount
if dbEpoch.ValidatorCount > 0 {
pageData.AverageValidatorBalance = dbEpoch.ValidatorBalance / dbEpoch.ValidatorCount
}
if dbEpoch.Eligible > 0 {
pageData.TargetVoteParticipation = float64(dbEpoch.VotedTarget) * 100.0 / float64(dbEpoch.Eligible)
pageData.HeadVoteParticipation = float64(dbEpoch.VotedHead) * 100.0 / float64(dbEpoch.Eligible)
pageData.TotalVoteParticipation = float64(dbEpoch.VotedTotal) * 100.0 / float64(dbEpoch.Eligible)
}
}
// load slots
pageData.Slots = make([]*models.EpochPageDataSlot, 0)
dbSlots := services.GlobalBeaconService.GetDbBlocksForSlots(uint64(lastSlot), uint32(utils.Config.Chain.Config.SlotsPerEpoch), true)
dbIdx := 0
dbCnt := len(dbSlots)
blockCount := uint64(0)
for slotIdx := int64(lastSlot); slotIdx >= int64(firstSlot); slotIdx-- {
slot := uint64(slotIdx)
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
pageData.OrphanedCount++
} else {
pageData.CanonicalCount++
}
slotData := &models.EpochPageDataSlot{
Slot: slot,
Epoch: utils.EpochOfSlot(slot),
Ts: utils.SlotToTime(slot),
Status: blockStatus,
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,
}
if dbSlot.EthBlockNumber != nil {
slotData.WithEthBlock = true
slotData.EthBlockNumber = *dbSlot.EthBlockNumber
}
pageData.Slots = append(pageData.Slots, slotData)
blockCount++
haveBlock = true
}
if !haveBlock {
slotData := &models.EpochPageDataSlot{
Slot: slot,
Epoch: epoch,
Ts: utils.SlotToTime(slot),
Scheduled: slot >= currentSlot,
Status: 0,
Proposer: slotAssignments[slot],
ProposerName: services.GlobalBeaconService.GetValidatorName(slotAssignments[slot]),
}
if slotData.Scheduled {
pageData.ScheduledCount++
} else {
pageData.MissedCount++
}
pageData.Slots = append(pageData.Slots, slotData)
blockCount++
}
}
pageData.BlockCount = uint64(blockCount)
var cacheTimeout time.Duration
if !pageData.Synchronized {
cacheTimeout = 5 * time.Minute
} else if pageData.Finalized {
cacheTimeout = 30 * time.Minute
} else {
cacheTimeout = 12 * time.Second
}
return pageData, cacheTimeout
}