Repository navigation
Expand file tree
/
Copy pathdutieschecker_service.go
More file actions
259 lines (221 loc) · 7.99 KB
/
Copy pathdutieschecker_service.go
File metadata and controls
259 lines (221 loc) · 7.99 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
package services
import (
"context"
"time"
"github.com/dappnode/validator-tracker/internal/application/domain"
"github.com/dappnode/validator-tracker/internal/application/ports"
"github.com/dappnode/validator-tracker/internal/logger"
)
type DutiesChecker struct {
Beacon ports.BeaconChainAdapter
Brain ports.BrainAdapter
Notifier ports.NotifierPort
Dappmanager ports.DappManagerPort
PollInterval time.Duration
lastJustifiedEpoch domain.Epoch
lastRunHadError bool
SlashedNotified map[domain.ValidatorIndex]bool
// Tracking previous states for notifications
PreviouslyAllLive bool
PreviouslyOffline bool
// ValidatorCache is populated on each epoch change for the HTTP API
ValidatorCache *domain.ValidatorCache
}
func (a *DutiesChecker) Run(ctx context.Context) {
ticker := time.NewTicker(a.PollInterval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
justifiedEpoch, err := a.Beacon.GetJustifiedEpoch(ctx)
if err != nil {
logger.Error("Error fetching justified epoch: %v", err)
a.lastRunHadError = true
continue
}
if justifiedEpoch == a.lastJustifiedEpoch && !a.lastRunHadError {
logger.Debug("Justified epoch %d unchanged and last run was successful, skipping check.", justifiedEpoch)
continue
}
a.lastJustifiedEpoch = justifiedEpoch
a.lastRunHadError = a.performChecks(ctx, justifiedEpoch) != nil
case <-ctx.Done():
return
}
}
}
func (a *DutiesChecker) performChecks(ctx context.Context, justifiedEpoch domain.Epoch) error {
logger.Info("New justified epoch %d detected.", justifiedEpoch)
notificationsEnabled, err := a.Dappmanager.GetNotificationsEnabled(ctx)
if err != nil {
logger.Warn("Error fetching notifications enabled, notification will not be sent: %v", err)
}
pubkeys, err := a.Brain.GetValidatorPubkeys()
if err != nil {
logger.Error("Error fetching pubkeys from brain: %v", err)
return err
}
if len(pubkeys) == 0 {
logger.Debug("No pubkeys found in brain for epoch %d, nothing to check.", justifiedEpoch)
if a.ValidatorCache != nil {
a.ValidatorCache.Update([]domain.ValidatorInfo{}, justifiedEpoch)
}
return nil
}
validatorInfos, err := a.Beacon.GetValidatorsByPubkeys(ctx, pubkeys)
if err != nil {
logger.Error("Error fetching validators from beacon node: %v", err)
return err
}
// Update validator cache for HTTP API
if a.ValidatorCache != nil {
a.ValidatorCache.Update(validatorInfos, justifiedEpoch)
}
if len(validatorInfos) == 0 {
logger.Debug("No validators found to check for epoch %d", justifiedEpoch)
return nil
}
// Extract indices for active validators only (duties checks don't apply to exited/pending)
var indices []domain.ValidatorIndex
for _, v := range validatorInfos {
if isActiveValidator(v.Status) {
indices = append(indices, v.Index)
}
}
if len(indices) == 0 {
logger.Debug("No active validators to check for epoch %d", justifiedEpoch)
return nil
}
logger.Info("Checking duties for %d active validators in epoch %d", len(indices), justifiedEpoch)
offline, online, allLive, err := a.checkLiveness(ctx, justifiedEpoch, indices)
if err != nil {
logger.Error("Error checking liveness for validators: %v", err)
return err
}
// Debug print: show offline, online, and allLive status
logger.Debug("Liveness check: offline=%v, online=%v, allLive=%v", offline, online, allLive)
logger.Debug("Previously all live: %v, previously offline: %v", a.PreviouslyAllLive, a.PreviouslyOffline)
// Check for the first condition: 1 or more validators offline when all were previously live
if len(offline) > 0 && a.PreviouslyAllLive {
if notificationsEnabled[domain.Notifications.Liveness] {
logger.Debug("Sending notification for validators going offline: %v", offline)
if err := a.Notifier.SendValidatorLivenessNot(offline, justifiedEpoch, false); err != nil {
logger.Warn("Error sending validator liveness notification: %v", err)
}
}
a.PreviouslyAllLive = false
a.PreviouslyOffline = true
}
// Check for the second condition: all validators online after 1 or more were offline
if allLive && a.PreviouslyOffline {
if notificationsEnabled[domain.Notifications.Liveness] {
logger.Debug("Sending notification for all validators back online: %v", indices)
if err := a.Notifier.SendValidatorLivenessNot(indices, justifiedEpoch, true); err != nil {
logger.Warn("Error sending validator liveness notification: %v", err)
}
}
a.PreviouslyAllLive = true
a.PreviouslyOffline = false
}
// Check block proposals (successful or missed)
proposed, missed, err := a.checkProposals(ctx, justifiedEpoch, indices)
if err != nil {
logger.Error("Error checking block proposals: %v", err)
return err
}
if len(proposed) > 0 && notificationsEnabled[domain.Notifications.Proposal] {
if err := a.Notifier.SendBlockProposalNot(proposed, justifiedEpoch, true); err != nil {
logger.Warn("Error sending block proposal notification: %v", err)
}
}
if len(missed) > 0 && notificationsEnabled[domain.Notifications.Proposal] {
if err := a.Notifier.SendBlockProposalNot(missed, justifiedEpoch, false); err != nil {
logger.Warn("Error sending block proposal notification: %v", err)
}
}
// Check for slashed validators
slashed, err := a.Beacon.GetSlashedValidators(ctx, indices)
if err != nil {
logger.Error("Error fetching slashed validators: %v", err)
return err
}
// Notify about slashed validators only if they haven't been notified before
var toNotify []domain.ValidatorIndex
for _, index := range slashed {
if !a.SlashedNotified[index] {
toNotify = append(toNotify, index)
a.SlashedNotified[index] = true
}
}
if len(toNotify) > 0 && notificationsEnabled[domain.Notifications.Slashed] {
if err := a.Notifier.SendValidatorsSlashedNot(toNotify, justifiedEpoch); err != nil {
logger.Warn("Error sending validator slashed notification: %v", err)
}
}
return nil
}
func (a *DutiesChecker) checkLiveness(
ctx context.Context,
epochToTrack domain.Epoch,
indices []domain.ValidatorIndex,
) (offline []domain.ValidatorIndex, online []domain.ValidatorIndex, allLive bool, err error) {
if len(indices) == 0 {
logger.Warn("No validators to check liveness for in epoch %d", epochToTrack)
return nil, nil, false, nil
}
livenessMap, err := a.Beacon.GetValidatorsLiveness(ctx, epochToTrack, indices)
if err != nil {
return nil, nil, false, err
}
allLive = true
for _, idx := range indices {
isLive, ok := livenessMap[idx]
if !ok || !isLive {
offline = append(offline, idx)
allLive = false
logger.Warn("❌ Validator %d was not seen in epoch %d", idx, epochToTrack)
} else {
online = append(online, idx)
logger.Info("✅ Validator %d seen in epoch %d", idx, epochToTrack)
}
}
return offline, online, allLive, nil
}
func (a *DutiesChecker) checkProposals(
ctx context.Context,
epochToTrack domain.Epoch,
indices []domain.ValidatorIndex,
) (proposed []domain.ValidatorIndex, missed []domain.ValidatorIndex, err error) {
proposerDuties, err := a.Beacon.GetProposerDuties(ctx, epochToTrack, indices)
if err != nil {
return nil, nil, err
}
if len(proposerDuties) == 0 {
logger.Warn("No proposer duties for any validators in epoch %d", epochToTrack)
return nil, nil, nil
}
for _, duty := range proposerDuties {
didPropose, err := a.Beacon.DidProposeBlock(ctx, duty.Slot)
if err != nil {
logger.Warn("⚠️ Could not determine if block was proposed at slot %d: %v", duty.Slot, err)
continue
}
if didPropose {
proposed = append(proposed, duty.ValidatorIndex)
logger.Info("✅ Validator %d successfully proposed a block at slot %d", duty.ValidatorIndex, duty.Slot)
} else {
missed = append(missed, duty.ValidatorIndex)
logger.Warn("❌ Validator %d was scheduled to propose at slot %d but did not", duty.ValidatorIndex, duty.Slot)
}
}
return proposed, missed, nil
}
// isActiveValidator returns true if the validator status indicates it can have duties
func isActiveValidator(status string) bool {
switch status {
case "active_ongoing", "active_exiting", "active_slashed":
return true
default:
return false
}
}