-
Notifications
You must be signed in to change notification settings - Fork 20
/
validator.go
199 lines (183 loc) · 6.99 KB
/
validator.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
package handlers
import (
"bytes"
"encoding/hex"
"fmt"
"net/http"
"strconv"
"strings"
"time"
v1 "github.com/attestantio/go-eth2-client/api/v1"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
"github.com/pk910/dora/dbtypes"
"github.com/pk910/dora/services"
"github.com/pk910/dora/templates"
"github.com/pk910/dora/types/models"
"github.com/pk910/dora/utils"
)
// Validator will return the main "validator" page using a go template
func Validator(w http.ResponseWriter, r *http.Request) {
var validatorTemplateFiles = append(layoutTemplateFiles,
"validator/validator.html",
"validator/recentBlocks.html",
"_svg/timeline.html",
)
var notfoundTemplateFiles = append(layoutTemplateFiles,
"validator/notfound.html",
)
var pageTemplate = templates.GetTemplate(validatorTemplateFiles...)
data := InitPageData(w, r, "validators", "/validator", "Validator", validatorTemplateFiles)
validatorSetRsp := services.GlobalBeaconService.GetCachedValidatorSet()
var validator *v1.Validator
if validatorSetRsp != nil {
vars := mux.Vars(r)
idxOrPubKey := strings.Replace(vars["idxOrPubKey"], "0x", "", -1)
validatorPubKey, err := hex.DecodeString(idxOrPubKey)
if err != nil || len(validatorPubKey) != 48 {
// search by index^
validatorIndex, err := strconv.ParseUint(vars["idxOrPubKey"], 10, 64)
if err == nil && validatorIndex < uint64(len(validatorSetRsp)) {
validator = validatorSetRsp[phase0.ValidatorIndex(validatorIndex)]
}
} else {
// search by pubkey
for _, val := range validatorSetRsp {
if bytes.Equal(val.Validator.PublicKey[:], validatorPubKey) {
validator = val
break
}
}
}
}
if validator == nil {
data := InitPageData(w, r, "blockchain", "/validator", "Validator not found", notfoundTemplateFiles)
w.Header().Set("Content-Type", "text/html")
if handleTemplateError(w, r, "validator.go", "Validator", "", templates.GetTemplate(notfoundTemplateFiles...).ExecuteTemplate(w, "layout", data)) != nil {
return // an error has occurred and was processed
}
return
}
var pageError error
pageError = services.GlobalCallRateLimiter.CheckCallLimit(r, 1)
if pageError == nil {
data.Data, pageError = getValidatorPageData(uint64(validator.Index))
}
if pageError != nil {
handlePageError(w, r, pageError)
return
}
w.Header().Set("Content-Type", "text/html")
if handleTemplateError(w, r, "validators.go", "Validators", "", pageTemplate.ExecuteTemplate(w, "layout", data)) != nil {
return // an error has occurred and was processed
}
}
func getValidatorPageData(validatorIndex uint64) (*models.ValidatorPageData, error) {
pageData := &models.ValidatorPageData{}
pageCacheKey := fmt.Sprintf("validator:%v", validatorIndex)
pageRes, pageErr := services.GlobalFrontendCache.ProcessCachedPage(pageCacheKey, true, pageData, func(pageCall *services.FrontendCacheProcessingPage) interface{} {
pageData, cacheTimeout := buildValidatorPageData(validatorIndex)
pageCall.CacheTimeout = cacheTimeout
return pageData
})
if pageErr == nil && pageRes != nil {
resData, resOk := pageRes.(*models.ValidatorPageData)
if !resOk {
return nil, ErrInvalidPageModel
}
pageData = resData
}
return pageData, pageErr
}
func buildValidatorPageData(validatorIndex uint64) (*models.ValidatorPageData, time.Duration) {
logrus.Debugf("validator page called: %v", validatorIndex)
validatorSetRsp := services.GlobalBeaconService.GetCachedValidatorSet()
validator := validatorSetRsp[phase0.ValidatorIndex(validatorIndex)]
pageData := &models.ValidatorPageData{
CurrentEpoch: uint64(utils.TimeToEpoch(time.Now())),
Index: uint64(validator.Index),
Name: services.GlobalBeaconService.GetValidatorName(uint64(validator.Index)),
PublicKey: validator.Validator.PublicKey[:],
Balance: uint64(validator.Balance),
EffectiveBalance: uint64(validator.Validator.EffectiveBalance),
BeaconState: validator.Status.String(),
WithdrawCredentials: validator.Validator.WithdrawalCredentials,
}
if strings.HasPrefix(validator.Status.String(), "pending") {
pageData.State = "Pending"
} else if validator.Status == v1.ValidatorStateActiveOngoing {
pageData.State = "Active"
pageData.IsActive = true
} else if validator.Status == v1.ValidatorStateActiveExiting {
pageData.State = "Exiting"
pageData.IsActive = true
} else if validator.Status == v1.ValidatorStateActiveSlashed {
pageData.State = "Slashed"
pageData.IsActive = true
} else if validator.Status == v1.ValidatorStateExitedUnslashed {
pageData.State = "Exited"
} else if validator.Status == v1.ValidatorStateExitedSlashed {
pageData.State = "Slashed"
} else {
pageData.State = validator.Status.String()
}
if pageData.IsActive {
// load activity map
activityMap, maxActivity := services.GlobalBeaconService.GetValidatorActivity()
pageData.UpcheckActivity = activityMap[uint64(validator.Index)]
pageData.UpcheckMaximum = uint8(maxActivity)
}
if validator.Validator.ActivationEligibilityEpoch < 18446744073709551615 {
pageData.ShowEligible = true
pageData.EligibleEpoch = uint64(validator.Validator.ActivationEligibilityEpoch)
pageData.EligibleTs = utils.EpochToTime(uint64(validator.Validator.ActivationEligibilityEpoch))
}
if validator.Validator.ActivationEpoch < 18446744073709551615 {
pageData.ShowActivation = true
pageData.ActivationEpoch = uint64(validator.Validator.ActivationEpoch)
pageData.ActivationTs = utils.EpochToTime(uint64(validator.Validator.ActivationEpoch))
}
if validator.Validator.ExitEpoch < 18446744073709551615 {
pageData.ShowExit = true
pageData.WasActive = true
pageData.ExitEpoch = uint64(validator.Validator.ExitEpoch)
pageData.ExitTs = utils.EpochToTime(uint64(validator.Validator.ExitEpoch))
}
if validator.Validator.WithdrawalCredentials[0] == 0x01 {
pageData.ShowWithdrawAddress = true
pageData.WithdrawAddress = validator.Validator.WithdrawalCredentials[12:]
}
// load latest blocks
pageData.RecentBlocks = make([]*models.ValidatorPageDataBlocks, 0)
blocksData := services.GlobalBeaconService.GetDbBlocksByFilter(&dbtypes.BlockFilter{
ProposerIndex: &validatorIndex,
WithOrphaned: 1,
WithMissing: 1,
}, 0, 10)
for _, blockData := range blocksData {
blockStatus := 1
if blockData.Block == nil {
blockStatus = 0
} else if blockData.Block.Orphaned == 1 {
blockStatus = 2
}
blockEntry := models.ValidatorPageDataBlocks{
Epoch: utils.EpochOfSlot(blockData.Slot),
Slot: blockData.Slot,
Ts: utils.SlotToTime(blockData.Slot),
Status: uint64(blockStatus),
}
if blockData.Block != nil {
blockEntry.Graffiti = blockData.Block.Graffiti
blockEntry.BlockRoot = fmt.Sprintf("0x%x", blockData.Block.Root)
if blockData.Block.EthBlockNumber != nil {
blockEntry.WithEthBlock = true
blockEntry.EthBlock = *blockData.Block.EthBlockNumber
}
}
pageData.RecentBlocks = append(pageData.RecentBlocks, &blockEntry)
}
pageData.RecentBlockCount = uint64(len(pageData.RecentBlocks))
return pageData, 10 * time.Minute
}