-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathverification.go
256 lines (234 loc) · 6.98 KB
/
verification.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
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
package main
import (
"fmt"
"time"
"gopkg.in/inconshreveable/log15.v2"
)
var (
DefaultBeaconCheckDelay = time.Second * 12
DefaultExecutionCheckDelay = time.Second * 12
)
func NewVerificationProbes(client Client, verifications []Verification) VerificationProbes {
clientLayer := client.ClientLayer()
verifProbes := make([]*VerificationProbe, 0)
for _, v := range verifications {
if v.ClientLayer == clientLayer {
requiredClientFound := true
if requiredClients, ok := MetricClientTypeRequirements[v.MetricName]; ok {
requiredClientFound = false
for _, requiredClient := range requiredClients {
if requiredClient == client.ClientType() {
requiredClientFound = true
break
}
}
}
if requiredClientFound {
dpoints := make(DataPoints)
verif := v
vProbe := VerificationProbe{
Verification: &verif,
Client: client,
DataPointsPerSlotBlock: dpoints,
}
verifProbes = append(verifProbes, &vProbe)
}
}
}
return verifProbes
}
func (vps *VerificationProbes) AnySyncing() bool {
if vps == nil {
return false
}
for _, v := range *vps {
if v.IsSyncing {
return true
}
}
return false
}
func (vps *VerificationProbes) AllPassing() bool {
if vps == nil {
return false
}
for _, v := range *vps {
if !v.CurrentOutcome.Success {
return false
}
}
return true
}
func (v *VerificationProbe) Loop(stop <-chan interface{}) {
var checkDelay time.Duration
if v.Verification.ClientLayer == Beacon {
checkDelay = DefaultBeaconCheckDelay
} else if v.Verification.ClientLayer == Execution {
checkDelay = DefaultExecutionCheckDelay
}
for {
select {
case <-stop:
return
case <-time.After(checkDelay):
}
if v.Verification.PostMerge {
ttdBlockSlot, err := v.Client.UpdateGetTTDBlockSlot()
if err != nil {
log15.Warn("Error getting ttd block/slot", "client", v.Client.ClientType(), "clientID", v.Client.ClientID(), "error", err)
continue
}
if ttdBlockSlot == nil {
continue
}
if *ttdBlockSlot > v.PreviousDataPointSlotBlock {
v.PreviousDataPointSlotBlock = *ttdBlockSlot
}
}
latestBlockSlot, err := v.Client.GetLatestBlockSlotNumber()
if err != nil {
log15.Warn("Error getting latest block/slot number", "error", err)
continue
}
if latestBlockSlot > v.PreviousDataPointSlotBlock {
finishedSyncing := false
if !v.IsSyncing && (latestBlockSlot-v.PreviousDataPointSlotBlock) > 10 {
log15.Info("Syncing data", "type", v.Verification.MetricName)
v.IsSyncing = true
}
currentBlockSlot := v.PreviousDataPointSlotBlock + 1
for ; currentBlockSlot <= latestBlockSlot; currentBlockSlot++ {
newDataPoint, err := v.Client.GetDataPoint(v.Verification.MetricName, currentBlockSlot)
if err != nil {
if latestBlockSlot-currentBlockSlot <= 64 {
log15.Debug("Error during datapoint fetch, will retry", "client", v.Client.ClientType(), "clientID", v.Client.ClientID(), "datatype", v.Verification.MetricName, "block/slot", currentBlockSlot, "error", err)
break
}
// This data will be considered empty for given block/slot
log15.Debug("Unable to fetch datapoint, considered empty", "client", v.Client.ClientType(), "clientID", v.Client.ClientID(), "datatype", v.Verification.MetricName, "block/slot", currentBlockSlot, "error", err)
} else {
v.DataPointsPerSlotBlock[currentBlockSlot] = newDataPoint
}
v.PreviousDataPointSlotBlock = currentBlockSlot
if currentBlockSlot == latestBlockSlot {
finishedSyncing = true
}
}
if v.IsSyncing && finishedSyncing {
log15.Info("Finished syncing data", "datatype", v.Verification.MetricName)
v.IsSyncing = false
if !v.AllProbesClient.AnySyncing() {
log15.Info("Finished syncing all data", "client", v.Client.ClientType(), "clientID", v.Client.ClientID())
}
}
}
if !v.IsSyncing {
v.CurrentOutcome, _ = v.Verify()
}
}
}
func (v *VerificationProbe) Verify() (VerificationOutcome, error) {
if dataLayer, ok := DataTypesPerLayer[v.Verification.ClientLayer]; ok {
if dataType, ok := dataLayer[v.Verification.MetricName]; ok {
switch dataType {
case Uint64:
return v.VerifyUint64()
case BigInt:
return v.VerifyBigInt()
}
}
}
return VerificationOutcome{}, fmt.Errorf("unknown data: %s", v.Verification.MetricName)
}
func (v *VerificationProbe) VerifyBigInt() (VerificationOutcome, error) {
aggregatedValue, err := v.DataPointsPerSlotBlock.AggregateBigInt(v.Verification.AggregateFunction, v.Verification.AggregateFunctionValue)
if err != nil {
return VerificationOutcome{}, err
}
passValue, err := v.Verification.PassValue.ToBigInt()
if err != nil {
return VerificationOutcome{}, err
}
switch v.Verification.PassCriteria {
case MinimumValue:
if aggregatedValue.Cmp(passValue) >= 0 {
return VerificationOutcome{
Success: true,
Message: fmt.Sprintf("%v >= %v", aggregatedValue, passValue),
}, nil
} else {
return VerificationOutcome{
Success: false,
Message: fmt.Sprintf("%v < %v", aggregatedValue, passValue),
}, nil
}
case MaximumValue:
if aggregatedValue.Cmp(passValue) <= 0 {
return VerificationOutcome{
Success: true,
Message: fmt.Sprintf("%v <= %v", aggregatedValue, passValue),
}, nil
} else {
return VerificationOutcome{
Success: false,
Message: fmt.Sprintf("%v > %v", aggregatedValue, passValue),
}, nil
}
}
return VerificationOutcome{}, fmt.Errorf("invalid pass criteria for bigInt: %s", v.Verification.PassCriteria)
}
func (v *VerificationProbe) VerifyUint64() (VerificationOutcome, error) {
aggregatedValue, err := v.DataPointsPerSlotBlock.AggregateUint64(v.Verification.AggregateFunction, v.Verification.AggregateFunctionValue)
if err != nil {
return VerificationOutcome{}, err
}
passValue, err := v.Verification.PassValue.ToUint64()
if err != nil {
return VerificationOutcome{}, err
}
switch v.Verification.PassCriteria {
case MinimumValue:
if aggregatedValue >= passValue {
return VerificationOutcome{
Success: true,
Message: fmt.Sprintf("%d >= %d", aggregatedValue, passValue),
}, nil
} else {
return VerificationOutcome{
Success: false,
Message: fmt.Sprintf("%d < %d", aggregatedValue, passValue),
}, nil
}
case MaximumValue:
if aggregatedValue <= passValue {
return VerificationOutcome{
Success: true,
Message: fmt.Sprintf("%d <= %d", aggregatedValue, passValue),
}, nil
} else {
return VerificationOutcome{
Success: false,
Message: fmt.Sprintf("%d > %d", aggregatedValue, passValue),
}, nil
}
}
return VerificationOutcome{}, fmt.Errorf("invalid pass criteria for uint64: %s", v.Verification.PassCriteria)
}
func (vps VerificationProbes) ExecutionVerifications() uint64 {
retVal := uint64(0)
for _, vp := range vps {
if vp.Verification.ClientLayer == Execution {
retVal++
}
}
return retVal
}
func (vps VerificationProbes) BeaconVerifications() uint64 {
retVal := uint64(0)
for _, vp := range vps {
if vp.Verification.ClientLayer == Beacon {
retVal++
}
}
return retVal
}