/
slashing.go
267 lines (236 loc) · 9.97 KB
/
slashing.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
257
258
259
260
261
262
263
264
265
266
267
package slashinginterceptor
import (
"context"
"sync"
"time"
v1 "github.com/attestantio/go-eth2-client/api/v1"
"github.com/attestantio/go-eth2-client/spec"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/bloxapp/ssv/protocol/v2/blockchain/beacon"
"go.uber.org/zap"
"golang.org/x/exp/maps"
beaconproxy "github.com/bloxapp/ssv/e2e/beacon_proxy"
)
const startEndEpochsDiff = 2
type validatorState struct {
validator *v1.Validator
proposerTest ProposerSlashingTest
attesterTest AttesterSlashingTest
// StartEpoch
firstAttesterDuty map[beaconproxy.Gateway]*v1.AttesterDuty
firstAttestationData map[beaconproxy.Gateway]*phase0.AttestationData
firstSubmittedAttestation map[beaconproxy.Gateway]*phase0.Attestation
// EndEpoch
secondAttesterDuty map[beaconproxy.Gateway]*v1.AttesterDuty
secondAttestationData map[beaconproxy.Gateway]*phase0.AttestationData
secondSubmittedAttestation map[beaconproxy.Gateway]*phase0.Attestation
// StartEpoch
firstProposerDuty map[beaconproxy.Gateway]*v1.ProposerDuty
firstBlock map[beaconproxy.Gateway]*spec.VersionedBeaconBlock
firstSubmittedBlock map[beaconproxy.Gateway]*spec.VersionedSignedBeaconBlock
// EndEpoch
secondProposerDuty map[beaconproxy.Gateway]*v1.ProposerDuty
secondBlock map[beaconproxy.Gateway]*spec.VersionedBeaconBlock
secondSubmittedBlock map[beaconproxy.Gateway]*spec.VersionedSignedBeaconBlock
}
type SlashingInterceptor struct {
logger *zap.Logger
network beacon.Network
startEpoch phase0.Epoch
sleepEpoch phase0.Epoch
endEpoch phase0.Epoch
validators map[phase0.ValidatorIndex]*validatorState
fakeProposerDuties bool
mu sync.RWMutex
}
func New(
logger *zap.Logger,
network beacon.Network,
startEpoch phase0.Epoch,
fakeProposerDuties bool,
validators []*v1.Validator,
) *SlashingInterceptor {
s := &SlashingInterceptor{
logger: logger,
network: network,
startEpoch: startEpoch,
sleepEpoch: startEpoch + 1,
// sleepEpoch: math.MaxUint64, // TODO: replace with startEpoch + 1 after debugging is done
endEpoch: startEpoch + 2,
fakeProposerDuties: fakeProposerDuties,
validators: make(map[phase0.ValidatorIndex]*validatorState),
}
if len(s.validators) > int(network.SlotsPerEpoch()) {
panic(">32 validators not supported yet")
}
logger.Debug("creating slashing interceptor",
zap.Any("start_epoch", s.startEpoch),
zap.Any("end_epoch", s.endEpoch),
zap.Any("sleep_epoch", s.sleepEpoch),
)
a := 0
p := 0
for _, validator := range validators {
if a == len(AttesterSlashingTests) {
a = 0
}
if p == len(ProposerSlashingTests) {
p = 0
}
attesterTest := AttesterSlashingTests[a]
proposerTest := ProposerSlashingTests[p]
a++
p++
s.validators[validator.Index] = &validatorState{
validator: validator,
proposerTest: proposerTest, // TODO: extract from validators.json
attesterTest: attesterTest, // TODO: extract from validators.json
firstAttesterDuty: make(map[beaconproxy.Gateway]*v1.AttesterDuty),
firstAttestationData: make(map[beaconproxy.Gateway]*phase0.AttestationData),
firstSubmittedAttestation: make(map[beaconproxy.Gateway]*phase0.Attestation),
secondAttesterDuty: make(map[beaconproxy.Gateway]*v1.AttesterDuty),
secondAttestationData: make(map[beaconproxy.Gateway]*phase0.AttestationData),
secondSubmittedAttestation: make(map[beaconproxy.Gateway]*phase0.Attestation),
firstProposerDuty: make(map[beaconproxy.Gateway]*v1.ProposerDuty),
firstBlock: make(map[beaconproxy.Gateway]*spec.VersionedBeaconBlock),
firstSubmittedBlock: make(map[beaconproxy.Gateway]*spec.VersionedSignedBeaconBlock),
secondProposerDuty: make(map[beaconproxy.Gateway]*v1.ProposerDuty),
secondBlock: make(map[beaconproxy.Gateway]*spec.VersionedBeaconBlock),
secondSubmittedBlock: make(map[beaconproxy.Gateway]*spec.VersionedSignedBeaconBlock),
}
logger.Debug("set up validator",
zap.Uint64("validator_index", uint64(validator.Index)),
zap.String("pubkey", validator.Validator.PublicKey.String()),
zap.String("attester_test", attesterTest.Name),
zap.Bool("attester_slashable", attesterTest.Slashable),
zap.String("proposer_test", proposerTest.Name),
zap.Bool("proposer_slashable", proposerTest.Slashable),
)
}
return s
}
func (s *SlashingInterceptor) WatchSubmissions() {
endOfStartEpoch := s.network.EpochStartTime(s.startEpoch + 1)
time.AfterFunc(time.Until(endOfStartEpoch), func() {
s.checkStartEpochAttestationSubmission()
})
s.logger.Info("scheduled start epoch submission check", zap.Any("at", endOfStartEpoch))
endOfEndEpoch := s.network.EpochStartTime(s.endEpoch + 1)
time.AfterFunc(time.Until(endOfEndEpoch), func() {
s.checkEndEpochAttestationSubmission()
})
s.logger.Info("scheduled end epoch submission check", zap.Any("at", endOfEndEpoch))
}
func (s *SlashingInterceptor) checkStartEpochProposalSubmission() {
submittedCount := 0
for _, state := range s.validators {
// TODO: support values other than 4
if len(state.firstSubmittedBlock) != 4 {
s.logger.Debug("validator did not submit proposal in start epoch",
zap.Any("validator_index", state.validator.Index),
zap.Any("validator_pk", state.validator.Validator.PublicKey.String()),
zap.Any("submitters", gatewayNames(maps.Keys(state.firstSubmittedBlock))),
)
} else {
submittedCount++
s.logger.Debug("validator submitted proposal in start epoch",
zap.Any("validator_index", state.validator.Index),
zap.Any("validator_pk", state.validator.Validator.PublicKey.String()),
zap.Any("submitters", gatewayNames(maps.Keys(state.firstSubmittedBlock))),
)
}
}
if submittedCount == len(s.validators) {
s.logger.Info("all proposals submitted in start epoch", zap.Any("count", submittedCount))
} else {
s.logger.Warn("not all proposals submitted in start epoch", zap.Any("submitted", submittedCount), zap.Any("expected", len(s.validators)))
}
}
func (s *SlashingInterceptor) checkEndEpochProposalSubmission() {
// TODO: currently we're not able to preform proposer duties in end epoch
}
func (s *SlashingInterceptor) checkStartEpochAttestationSubmission() {
submittedCount := 0
for _, state := range s.validators {
// TODO: support values other than 4
if len(state.firstSubmittedAttestation) != 4 {
s.logger.Debug("validator did not submit in start epoch",
zap.Any("validator_index", state.validator.Index),
zap.Any("validator_pk", state.validator.Validator.PublicKey.String()),
zap.Any("submitters", gatewayNames(maps.Keys(state.firstSubmittedAttestation))),
)
} else {
submittedCount++
s.logger.Debug("validator submitted in start epoch",
zap.Any("validator_index", state.validator.Index),
zap.Any("validator_pk", state.validator.Validator.PublicKey.String()),
zap.Any("submitters", gatewayNames(maps.Keys(state.firstSubmittedAttestation))),
)
}
}
if submittedCount == len(s.validators) {
s.logger.Info("all attestations submitted in start epoch", zap.Any("count", submittedCount))
} else {
s.logger.Warn("not all attestations submitted in start epoch", zap.Any("submitted", submittedCount), zap.Any("expected", len(s.validators)))
}
}
func (s *SlashingInterceptor) checkEndEpochAttestationSubmission() {
submittedCount := 0
hasSlashable := false
for _, state := range s.validators {
if state.attesterTest.Slashable {
if len(state.secondSubmittedAttestation) != 0 {
hasSlashable = true
s.logger.Error("found slashable validator",
zap.Any("validator_index", state.validator.Index),
zap.Any("validator_pk", state.validator.Validator.PublicKey.String()),
zap.Any("submitters", gatewayNames(maps.Keys(state.secondSubmittedAttestation))),
zap.Any("first_attestation", state.firstSubmittedAttestation),
zap.Any("second_attestation", state.secondSubmittedAttestation),
zap.Any("test", state.attesterTest.Name),
)
}
} else if len(state.secondSubmittedAttestation) != 4 { // TODO: support values other than 4
s.logger.Debug("validator did not submit in end epoch",
zap.Any("validator_index", state.validator.Index),
zap.Any("validator_pk", state.validator.Validator.PublicKey.String()),
zap.Any("submitters", gatewayNames(maps.Keys(state.secondSubmittedAttestation))),
zap.Any("test", state.attesterTest.Name),
)
} else {
submittedCount++
s.logger.Debug("validator submitted in end epoch",
zap.Any("validator_index", state.validator.Index),
zap.Any("validator_pk", state.validator.Validator.PublicKey.String()),
zap.Any("submitters", gatewayNames(maps.Keys(state.secondSubmittedAttestation))),
zap.Any("test", state.attesterTest.Name),
)
}
}
if hasSlashable {
s.logger.Error("found slashable validators")
} else if submittedCount == len(s.validators) {
s.logger.Info("all attestations submitted in end epoch", zap.Any("count", submittedCount))
} else {
s.logger.Info("not all attestations submitted in end epoch", zap.Any("submitted", submittedCount), zap.Any("expected", len(s.validators)))
}
s.logger.Info("End epoch finished")
// TODO: rewrite logs above so that we check two conditions:
// 1. All non-slashable validators submitted in end epoch
// 2. All slashable validators did not submit in end epoch
// Then have a summary log if test passes or not. Two cases above may be logged but don't have to (e.g. debugging)
}
func (s *SlashingInterceptor) blockedEpoch(epoch phase0.Epoch) bool {
return epoch < s.startEpoch || epoch == s.sleepEpoch || epoch > s.endEpoch
}
func (s *SlashingInterceptor) requestContext(ctx context.Context) (*zap.Logger, beaconproxy.Gateway) {
return ctx.Value(beaconproxy.LoggerKey{}).(*zap.Logger),
ctx.Value(beaconproxy.GatewayKey{}).(beaconproxy.Gateway)
}
func gatewayNames(gateways []beaconproxy.Gateway) []string {
names := make([]string, 0, len(gateways))
for _, gateway := range gateways {
names = append(names, gateway.Name)
}
return names
}