-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathincluded_deposits.go
More file actions
359 lines (328 loc) · 13 KB
/
Copy pathincluded_deposits.go
File metadata and controls
359 lines (328 loc) · 13 KB
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package handlers
import (
"bytes"
"context"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethpandaops/dora/dbtypes"
"github.com/ethpandaops/dora/services"
"github.com/ethpandaops/dora/templates"
"github.com/ethpandaops/dora/types/models"
v1 "github.com/ethpandaops/go-eth2-client/api/v1"
"github.com/ethpandaops/go-eth2-client/spec/gloas"
"github.com/ethpandaops/go-eth2-client/spec/phase0"
"github.com/sirupsen/logrus"
)
// IncludedDeposits will return the filtered "included_deposits" page using a go template
func IncludedDeposits(w http.ResponseWriter, r *http.Request) {
var templateFiles = append(layoutTemplateFiles,
"included_deposits/included_deposits.html",
"_svg/professor.html",
)
var pageTemplate = templates.GetTemplate(templateFiles...)
data := InitPageData(w, r, "validators", "/validators/included_deposits", "Included Deposits", templateFiles)
urlArgs := r.URL.Query()
var pageSize uint64 = 50
if urlArgs.Has("c") {
pageSize, _ = strconv.ParseUint(urlArgs.Get("c"), 10, 64)
}
var pageIdx uint64 = 1
if urlArgs.Has("p") {
pageIdx, _ = strconv.ParseUint(urlArgs.Get("p"), 10, 64)
if pageIdx < 1 {
pageIdx = 1
}
}
var minIndex uint64
var maxIndex uint64
var publickey string
var vname string
var minAmount uint64
var maxAmount uint64
var withOrphaned uint64
var address string
var withValid uint64
var credTypes []uint8
if urlArgs.Has("f") {
if urlArgs.Has("f.mini") {
minIndex, _ = strconv.ParseUint(urlArgs.Get("f.mini"), 10, 64)
}
if urlArgs.Has("f.maxi") {
maxIndex, _ = strconv.ParseUint(urlArgs.Get("f.maxi"), 10, 64)
}
if urlArgs.Has("f.pubkey") {
publickey = urlArgs.Get("f.pubkey")
}
if urlArgs.Has("f.vname") {
vname = urlArgs.Get("f.vname")
}
if urlArgs.Has("f.mina") {
minAmount, _ = strconv.ParseUint(urlArgs.Get("f.mina"), 10, 64)
}
if urlArgs.Has("f.maxa") {
maxAmount, _ = strconv.ParseUint(urlArgs.Get("f.maxa"), 10, 64)
}
if urlArgs.Has("f.orphaned") {
withOrphaned, _ = strconv.ParseUint(urlArgs.Get("f.orphaned"), 10, 64)
}
if urlArgs.Has("f.address") {
address = urlArgs.Get("f.address")
}
if urlArgs.Has("f.valid") {
withValid, _ = strconv.ParseUint(urlArgs.Get("f.valid"), 10, 64)
}
if vals, ok := urlArgs["f.cred"]; ok {
seen := map[uint8]bool{}
for _, v := range vals {
t, err := strconv.ParseUint(v, 10, 8)
if err != nil || (t > 2 && t != 0xB0) || seen[uint8(t)] {
continue
}
seen[uint8(t)] = true
credTypes = append(credTypes, uint8(t))
}
}
} else {
withOrphaned = 1
withValid = 1
}
var pageError error
pageError = services.GlobalCallRateLimiter.CheckCallLimit(r, 2)
if pageError == nil {
data.Data, pageError = getFilteredIncludedDepositsPageData(pageIdx, pageSize, minIndex, maxIndex, publickey, vname, minAmount, maxAmount, uint8(withOrphaned), address, uint8(withValid), credTypes)
}
if pageError != nil {
handlePageError(w, r, pageError)
return
}
w.Header().Set("Content-Type", "text/html")
if handleTemplateError(w, r, "slots_filtered.go", "SlotsFiltered", "", pageTemplate.ExecuteTemplate(w, "layout", data)) != nil {
return // an error has occurred and was processed
}
}
func getFilteredIncludedDepositsPageData(pageIdx uint64, pageSize uint64, minIndex uint64, maxIndex uint64, publickey string, vname string, minAmount uint64, maxAmount uint64, withOrphaned uint8, address string, withValid uint8, credTypes []uint8) (*models.IncludedDepositsPageData, error) {
pageData := &models.IncludedDepositsPageData{}
pageCacheKey := fmt.Sprintf("included_deposits:%v:%v:%v:%v:%v:%v:%v:%v:%v:%v:%v:%v", pageIdx, pageSize, minIndex, maxIndex, publickey, vname, minAmount, maxAmount, withOrphaned, address, withValid, credTypes)
pageRes, pageErr := services.GlobalFrontendCache.ProcessCachedPage(pageCacheKey, true, pageData, func(pageCall *services.FrontendCacheProcessingPage) interface{} {
pageData, cacheTimeout := buildFilteredIncludedDepositsPageData(pageCall.CallCtx, pageIdx, pageSize, minIndex, maxIndex, publickey, vname, minAmount, maxAmount, withOrphaned, address, withValid, credTypes)
pageCall.CacheTimeout = cacheTimeout
return pageData
})
if pageErr == nil && pageRes != nil {
resData, resOk := pageRes.(*models.IncludedDepositsPageData)
if !resOk {
return nil, ErrInvalidPageModel
}
pageData = resData
}
return pageData, pageErr
}
func buildFilteredIncludedDepositsPageData(ctx context.Context, pageIdx uint64, pageSize uint64, minIndex uint64, maxIndex uint64, publickey string, vname string, minAmount uint64, maxAmount uint64, withOrphaned uint8, address string, withValid uint8, credTypes []uint8) (*models.IncludedDepositsPageData, time.Duration) {
filterArgs := url.Values{}
if minIndex != 0 {
filterArgs.Add("f.mini", fmt.Sprintf("%v", minIndex))
}
if maxIndex != 0 {
filterArgs.Add("f.maxi", fmt.Sprintf("%v", maxIndex))
}
if publickey != "" {
filterArgs.Add("f.pubkey", publickey)
}
if vname != "" {
filterArgs.Add("f.vname", vname)
}
if minAmount != 0 {
filterArgs.Add("f.mina", fmt.Sprintf("%v", minAmount))
}
if maxAmount != 0 {
filterArgs.Add("f.maxa", fmt.Sprintf("%v", maxAmount))
}
if withOrphaned != 0 {
filterArgs.Add("f.orphaned", fmt.Sprintf("%v", withOrphaned))
}
if address != "" {
filterArgs.Add("f.address", address)
}
if withValid != 0 {
filterArgs.Add("f.valid", fmt.Sprintf("%v", withValid))
}
credTypeSet := make(map[uint8]bool, len(credTypes))
for _, t := range credTypes {
credTypeSet[t] = true
filterArgs.Add("f.cred", fmt.Sprintf("%v", t))
}
pageData := &models.IncludedDepositsPageData{
FilterMinIndex: minIndex,
FilterMaxIndex: maxIndex,
FilterPubKey: publickey,
FilterValidatorName: vname,
FilterMinAmount: minAmount,
FilterMaxAmount: maxAmount,
FilterWithOrphaned: withOrphaned,
FilterAddress: address,
FilterWithValid: withValid,
FilterCredTypes: credTypeSet,
}
cacheTimeout := 5 * time.Minute
logrus.Debugf("included_deposits page called: %v:%v [%v,%v,%v,%v,%v,%v,%v]", pageIdx, pageSize, minIndex, maxIndex, publickey, vname, minAmount, maxAmount, credTypes)
if pageIdx == 1 {
pageData.IsDefaultPage = true
} else {
cacheTimeout = 15 * time.Minute
}
if pageSize > 100 {
pageSize = 100
}
pageData.PageSize = pageSize
pageData.TotalPages = pageIdx
pageData.CurrentPageIndex = pageIdx
if pageIdx > 1 {
pageData.PrevPageIndex = pageIdx - 1
}
// Calculate offset from page index
pageOffset := (pageIdx - 1) * pageSize
// Update to use new filter structure
depositFilter := &services.CombinedDepositRequestFilter{
Filter: &dbtypes.DepositTxFilter{
MinIndex: minIndex,
MaxIndex: maxIndex,
PublicKey: common.FromHex(publickey),
ValidatorName: vname,
MinAmount: minAmount,
MaxAmount: maxAmount,
WithOrphaned: withOrphaned,
Address: common.FromHex(address),
WithValid: withValid,
WithdrawalCredTypes: credTypes,
},
}
// Get deposits using new service function with offset
dbDeposits, totalRows := services.GlobalBeaconService.GetDepositRequestsByFilter(ctx, depositFilter, pageOffset, uint32(pageSize))
chainState := services.GlobalBeaconService.GetChainState()
for _, deposit := range dbDeposits {
wdCreds := deposit.WithdrawalCredentials()
isBuilder := len(wdCreds) > 0 && wdCreds[0] == 0xB0
depositData := &models.IncludedDepositsPageDataDeposit{
PublicKey: deposit.PublicKey(),
Withdrawalcredentials: wdCreds,
Amount: deposit.Amount(),
Time: chainState.SlotToTime(phase0.Slot(deposit.Request.SlotNumber)),
SlotNumber: deposit.Request.SlotNumber,
SlotRoot: deposit.Request.SlotRoot,
Orphaned: deposit.RequestOrphaned,
DepositorAddress: deposit.SourceAddress(),
IsBuilder: isBuilder,
}
if deposit.Request != nil {
depositData.HasIndex = true
depositData.Index = *deposit.Request.Index
}
// Add queue status
if deposit.IsQueued {
depositData.IsQueued = true
if !bytes.Equal(deposit.QueueEntry.PendingDeposit.Pubkey[:], deposit.PublicKey()) {
logrus.Warnf("queue entry public key mismatch: %x != %x", deposit.QueueEntry.PendingDeposit.Pubkey[:], deposit.PublicKey())
}
depositData.QueuePosition = deposit.QueueEntry.QueuePos
depositData.EstimatedTime = chainState.EpochToTime(deposit.QueueEntry.EpochEstimate)
}
// Add validator status
if validatorIdx, found := services.GlobalBeaconService.GetValidatorIndexByPubkey(phase0.BLSPubKey(deposit.PublicKey())); !found {
depositData.ValidatorStatus = "Deposited"
depositData.ValidatorExists = false
} else if uint64(validatorIdx)&services.BuilderIndexFlag != 0 {
builderIndex := uint64(validatorIdx) &^ services.BuilderIndexFlag
depositData.IsBuilder = true
depositData.ValidatorExists = true
depositData.ValidatorIndex = builderIndex
depositData.ValidatorName = services.GlobalBeaconService.GetValidatorName(uint64(validatorIdx))
builder := services.GlobalBeaconService.GetBuilderByIndex(gloas.BuilderIndex(builderIndex))
if builder == nil {
depositData.ValidatorStatus = "Deposited"
} else if builder.WithdrawableEpoch <= chainState.CurrentEpoch() {
depositData.ValidatorStatus = "Exited"
} else {
depositData.ValidatorStatus = "Active"
}
} else {
depositData.ValidatorExists = true
depositData.ValidatorIndex = uint64(validatorIdx)
depositData.ProjectedIndex = services.GlobalBeaconService.IsProjectedValidatorIndex(validatorIdx)
depositData.IsBuilder = false
depositData.ValidatorName = services.GlobalBeaconService.GetValidatorName(uint64(validatorIdx))
validator := services.GlobalBeaconService.GetValidatorByIndex(validatorIdx, false)
if validator == nil {
depositData.ValidatorStatus = "Deposited"
} else if strings.HasPrefix(validator.Status.String(), "pending") {
depositData.ValidatorStatus = "Pending"
} else if validator.Status == v1.ValidatorStateActiveOngoing {
depositData.ValidatorStatus = "Active"
depositData.ShowUpcheck = true
} else if validator.Status == v1.ValidatorStateActiveExiting {
depositData.ValidatorStatus = "Exiting"
depositData.ShowUpcheck = true
} else if validator.Status == v1.ValidatorStateActiveSlashed {
depositData.ValidatorStatus = "Slashed"
depositData.ShowUpcheck = true
} else if validator.Status == v1.ValidatorStateExitedUnslashed {
depositData.ValidatorStatus = "Exited"
} else if validator.Status == v1.ValidatorStateExitedSlashed {
depositData.ValidatorStatus = "Slashed"
} else {
depositData.ValidatorStatus = validator.Status.String()
}
if depositData.ShowUpcheck {
depositData.UpcheckActivity = uint8(services.GlobalBeaconService.GetValidatorLiveness(validator.Index, 3))
depositData.UpcheckMaximum = uint8(3)
}
}
// Add transaction details if available
if deposit.Transaction != nil {
depositData.HasTransaction = true
depositData.TransactionHash = deposit.Transaction.TxHash
depositData.TransactionDetails = &models.IncludedDepositsPageDataDepositTxDetails{
BlockNumber: deposit.Transaction.BlockNumber,
BlockHash: fmt.Sprintf("%#x", deposit.Transaction.BlockRoot),
BlockTime: deposit.Transaction.BlockTime,
TxOrigin: common.Address(deposit.Transaction.TxSender).Hex(),
TxTarget: common.Address(deposit.Transaction.TxTarget).Hex(),
TxHash: fmt.Sprintf("%#x", deposit.Transaction.TxHash),
}
if deposit.Transaction.ValidSignature == 0 {
depositData.InvalidSignature = true
}
}
pageData.Deposits = append(pageData.Deposits, depositData)
}
pageData.DepositCount = uint64(len(pageData.Deposits))
if pageData.DepositCount > 0 {
pageData.FirstIndex = pageData.Deposits[0].Index
pageData.LastIndex = pageData.Deposits[pageData.DepositCount-1].Index
}
pageData.TotalPages = totalRows / pageSize
if totalRows%pageSize > 0 {
pageData.TotalPages++
}
pageData.LastPageIndex = pageData.TotalPages
if pageIdx < pageData.TotalPages {
pageData.NextPageIndex = pageIdx + 1
}
// Populate UrlParams for page jump functionality
pageData.UrlParams = make([]models.UrlParam, 0)
for key, values := range filterArgs {
if len(values) > 0 {
pageData.UrlParams = append(pageData.UrlParams, models.UrlParam{Key: key, Value: values[0]})
}
}
pageData.UrlParams = append(pageData.UrlParams, models.UrlParam{Key: "c", Value: fmt.Sprintf("%v", pageData.PageSize)})
pageData.FirstPageLink = fmt.Sprintf("/validators/included_deposits?f&%v&c=%v", filterArgs.Encode(), pageData.PageSize)
pageData.PrevPageLink = fmt.Sprintf("/validators/included_deposits?f&%v&c=%v&p=%v", filterArgs.Encode(), pageData.PageSize, pageData.PrevPageIndex)
pageData.NextPageLink = fmt.Sprintf("/validators/included_deposits?f&%v&c=%v&p=%v", filterArgs.Encode(), pageData.PageSize, pageData.NextPageIndex)
pageData.LastPageLink = fmt.Sprintf("/validators/included_deposits?f&%v&c=%v&p=%v", filterArgs.Encode(), pageData.PageSize, pageData.LastPageIndex)
return pageData, cacheTimeout
}