-
Notifications
You must be signed in to change notification settings - Fork 21
/
validator_score.go
359 lines (315 loc) · 14.1 KB
/
validator_score.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
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
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package validators
import (
"context"
"math/rand"
"sort"
"code.vegaprotocol.io/vega/core/events"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/libs/num"
"code.vegaprotocol.io/vega/logging"
)
type valScore struct {
ID string
score num.Decimal
}
// getStakeScore returns a score for the validator based on their relative score of the total score.
// No anti-whaling is applied.
func getStakeScore(delegationState []*types.ValidatorData) map[string]num.Decimal {
totalStake := num.UintZero()
for _, ds := range delegationState {
totalStake.AddSum(num.Sum(ds.SelfStake, ds.StakeByDelegators))
}
totalStakeD := totalStake.ToDecimal()
scores := make(map[string]num.Decimal, len(delegationState))
for _, ds := range delegationState {
if totalStakeD.IsPositive() {
scores[ds.NodeID] = num.Sum(ds.SelfStake, ds.StakeByDelegators).ToDecimal().Div(totalStakeD)
} else {
scores[ds.NodeID] = num.DecimalZero()
}
}
return scores
}
// getPerformanceScore returns the performance score of the validators.
// if the node has been a tendermint validator for the epoch it returns its tendermint performance score (as the ratio between the blocks proposed
// and the number of times it was expected to propose)
// if the node has less than the minimum stake they get 0 performance score
// if the node is ersatz or waiting list validators and has not yet forwarded or voted first - their score is 0
// if the node is not tm node - their score is based on the number of times out of the last 10 that they signed every 1000 blocks.
func (t *Topology) getPerformanceScore(delegationState []*types.ValidatorData) map[string]num.Decimal {
scores := make(map[string]num.Decimal, len(delegationState))
totalTmPower := int64(0)
for _, vs := range t.validators {
totalTmPower += vs.validatorPower
}
for _, ds := range delegationState {
vd := t.validators[ds.NodeID]
performanceScore := num.DecimalZero()
if ds.SelfStake.LT(t.minimumStake) {
scores[ds.NodeID] = performanceScore
continue
}
if vd.status == ValidatorStatusTendermint {
scores[ds.NodeID] = t.validatorPerformance.ValidatorPerformanceScore(vd.data.TmPubKey, vd.validatorPower, totalTmPower, t.performanceScalingFactor)
continue
}
if vd.numberOfEthereumEventsForwarded < t.minimumEthereumEventsForNewValidator {
scores[ds.NodeID] = performanceScore
continue
}
for _, v := range t.validators[ds.NodeID].heartbeatTracker.blockSigs {
if v {
performanceScore = performanceScore.Add(PerformanceIncrement)
}
}
scores[ds.NodeID] = performanceScore
}
return scores
}
// getRankingScore returns the score for ranking as stake_score x performance_score.
// for validators in ersatz or tendermint it is scaled by 1+incumbent factor.
func (t *Topology) getRankingScore(delegationState []*types.ValidatorData) (map[string]num.Decimal, map[string]num.Decimal, map[string]num.Decimal) {
stakeScores := getStakeScore(delegationState)
performanceScores := t.getPerformanceScore(delegationState)
rankingScores := t.getRankingScoreInternal(stakeScores, performanceScores)
return stakeScores, performanceScores, rankingScores
}
func (t *Topology) getRankingScoreInternal(stakeScores, perfScores map[string]num.Decimal) map[string]num.Decimal {
if len(stakeScores) != len(perfScores) {
t.log.Panic("incompatible slice length for stakeScores and perfScores")
}
rankingScore := make(map[string]num.Decimal, len(stakeScores))
for nodeID := range stakeScores {
vd := t.validators[nodeID]
ranking := stakeScores[nodeID].Mul(perfScores[nodeID])
if vd.status == ValidatorStatusTendermint || vd.status == ValidatorStatusErsatz {
ranking = ranking.Mul(t.validatorIncumbentBonusFactor)
}
rankingScore[nodeID] = ranking
}
return rankingScore
}
// normaliseScores normalises the given scores with respect to their sum, making sure they don't go above 1.
func normaliseScores(scores map[string]num.Decimal, rng *rand.Rand) map[string]num.Decimal {
totalScore := num.DecimalZero()
for _, v := range scores {
totalScore = totalScore.Add(v)
}
normScores := make(map[string]num.Decimal, len(scores))
if totalScore.IsZero() {
for k := range scores {
normScores[k] = num.DecimalZero()
}
return normScores
}
scoreSum := num.DecimalZero()
for n, s := range scores {
normScores[n] = s.Div(totalScore)
scoreSum = scoreSum.Add(normScores[n])
}
if scoreSum.LessThanOrEqual(DecimalOne) {
return normScores
}
keys := make([]string, 0, len(normScores))
for k := range normScores {
keys = append(keys, k)
}
sort.Strings(keys)
precisionError := scoreSum.Sub(num.DecimalFromInt64(1))
unluckyValidator := rng.Intn(len(keys))
normScores[keys[unluckyValidator]] = num.MaxD(normScores[keys[unluckyValidator]].Sub(precisionError), num.DecimalZero())
return normScores
}
// calcValidatorScore calculates the stake based raw validator score with anti whaling.
func CalcValidatorScore(valStake, totalStake, optStake num.Decimal, stakeScoreParams types.StakeScoreParams) num.Decimal {
if totalStake.IsZero() {
return num.DecimalZero()
}
return antiwhale(valStake, totalStake, optStake, stakeScoreParams)
}
func antiwhale(valStake, totalStake, optStake num.Decimal, stakeScoreParams types.StakeScoreParams) num.Decimal {
penaltyFlatAmt := num.MaxD(num.DecimalZero(), valStake.Sub(optStake))
penaltyDownAmt := num.MaxD(num.DecimalZero(), valStake.Sub(stakeScoreParams.OptimalStakeMultiplier.Mul(optStake)))
linearScore := valStake.Sub(penaltyFlatAmt).Sub(penaltyDownAmt).Div(totalStake) // totalStake guaranteed to be non zero at this point
linearScore = num.MinD(num.DecimalOne(), num.MaxD(num.DecimalZero(), linearScore))
return linearScore
}
// getValScore returns the multiplications of the corresponding score for each validator.
func getValScore(inScores ...map[string]num.Decimal) map[string]num.Decimal {
if len(inScores) == 0 {
return map[string]num.Decimal{}
}
scores := make(map[string]num.Decimal, len(inScores[0]))
for k := range inScores[0] {
s := num.DecimalFromFloat(1)
for _, v := range inScores {
s = s.Mul(v[k])
}
scores[k] = s
}
return scores
}
// getMultisigScore (applies to tm validators only) returns multisigScore as:
// if the val_score = raw_score x performance_score is in the top <numberEthMultisigSigners> and the validator is on the multisig contract => 1
// else 0
// that means a validator in tendermint set only gets a reward if it is in the top <numberEthMultisigSigners> and their registered with the multisig contract.
func getMultisigScore(log *logging.Logger, status ValidatorStatus, rawScores map[string]num.Decimal, perfScore map[string]num.Decimal, multiSigTopology MultiSigTopology, numberEthMultisigSigners int, nodeIDToEthAddress map[string]string) map[string]num.Decimal {
if status == ValidatorStatusErsatz {
scores := make(map[string]num.Decimal, len(rawScores))
for k := range rawScores {
scores[k] = decimalOne
}
return scores
}
ethAddresses := make([]string, 0, len(rawScores))
for k := range rawScores {
if eth, ok := nodeIDToEthAddress[k]; !ok {
log.Panic("missing eth address in mapping", logging.String("node-id", k))
} else {
ethAddresses = append(ethAddresses, eth)
}
}
sort.Strings(ethAddresses)
if multiSigTopology.ExcessSigners(ethAddresses) {
res := make(map[string]num.Decimal, len(rawScores))
for rs := range rawScores {
res[rs] = num.DecimalZero()
}
return res
}
valScores := make([]valScore, 0, len(rawScores))
for k, d := range rawScores {
valScores = append(valScores, valScore{ID: k, score: d.Mul(perfScore[k])})
}
sort.SliceStable(valScores, func(i, j int) bool {
if valScores[i].score.Equal(valScores[j].score) {
return valScores[i].ID < valScores[j].ID
}
return valScores[i].score.GreaterThan(valScores[j].score)
})
res := make(map[string]num.Decimal, len(valScores))
for i, vs := range valScores {
if i < numberEthMultisigSigners {
if eth, ok := nodeIDToEthAddress[vs.ID]; !ok {
log.Panic("missing eth address in mapping", logging.String("node-id", vs.ID))
} else {
if multiSigTopology.IsSigner(eth) {
res[vs.ID] = decimalOne
}
}
continue
}
// everyone else is a 1
res[vs.ID] = decimalOne
}
for k := range rawScores {
if _, ok := res[k]; !ok {
res[k] = num.DecimalZero()
}
}
return res
}
// GetRewardsScores returns the reward scores (raw, performance, multisig, validator_score, and normalised) for tm and ersatz validaor sets.
func (t *Topology) GetRewardsScores(ctx context.Context, epochSeq string, delegationState []*types.ValidatorData, stakeScoreParams types.StakeScoreParams) (*types.ScoreData, *types.ScoreData) {
t.mu.RLock()
defer t.mu.RUnlock()
tmScores, optStake := t.calculateScores(delegationState, ValidatorStatusTendermint, stakeScoreParams, nil)
ezScores, _ := t.calculateScores(delegationState, ValidatorStatusErsatz, stakeScoreParams, &optStake)
evts := make([]events.Event, 0, len(tmScores.NodeIDSlice)+len(ezScores.NodeIDSlice))
for _, nodeID := range tmScores.NodeIDSlice {
evts = append(evts, events.NewValidatorScore(ctx, nodeID, epochSeq, tmScores.ValScores[nodeID], tmScores.NormalisedScores[nodeID], tmScores.RawValScores[nodeID], tmScores.PerformanceScores[nodeID], tmScores.MultisigScores[nodeID], "tendermint"))
}
for _, nodeID := range ezScores.NodeIDSlice {
evts = append(evts, events.NewValidatorScore(ctx, nodeID, epochSeq, ezScores.ValScores[nodeID], ezScores.NormalisedScores[nodeID], ezScores.RawValScores[nodeID], ezScores.PerformanceScores[nodeID], decimalOne, "ersatz"))
}
t.broker.SendBatch(evts)
return tmScores, ezScores
}
func (t *Topology) calculateScores(delegationState []*types.ValidatorData, validatorStatus ValidatorStatus, stakeScoreParams types.StakeScoreParams, optStake *num.Decimal) (*types.ScoreData, num.Decimal) {
scores := &types.ScoreData{}
// identify validators for the status for the epoch
validatorsForStatus := map[string]struct{}{}
nodeIDToEthAddress := map[string]string{}
for k, d := range t.validators {
if d.status == validatorStatus {
validatorsForStatus[k] = struct{}{}
}
nodeIDToEthAddress[d.data.ID] = d.data.EthereumAddress
}
// calculate the delegation and anti-whaling score for the validators with the given status
delegationForStatus, totalDelegationForStatus := CalcDelegation(validatorsForStatus, delegationState)
if optStake == nil {
optimalkStake := GetOptimalStake(totalDelegationForStatus, len(delegationForStatus), stakeScoreParams)
optStake = &optimalkStake
}
scores.RawValScores = CalcAntiWhalingScore(delegationForStatus, totalDelegationForStatus, *optStake, stakeScoreParams)
// calculate performance score based on performance of the validators with the given status
scores.PerformanceScores = t.getPerformanceScore(delegationForStatus)
// calculate multisig score for the validators
scores.MultisigScores = getMultisigScore(t.log, validatorStatus, scores.RawValScores, scores.PerformanceScores, t.multiSigTopology, t.numberEthMultisigSigners, nodeIDToEthAddress)
// calculate the final score
scores.ValScores = getValScore(scores.RawValScores, scores.PerformanceScores, scores.MultisigScores)
// normalise the scores
scores.NormalisedScores = normaliseScores(scores.ValScores, t.rng)
// sort the list of tm validators
tmNodeIDs := make([]string, 0, len(delegationForStatus))
for k := range scores.RawValScores {
tmNodeIDs = append(tmNodeIDs, k)
}
sort.Strings(tmNodeIDs)
scores.NodeIDSlice = tmNodeIDs
for _, k := range tmNodeIDs {
t.log.Info("reward scores for", logging.String("node-id", k), logging.String("stake-score", scores.RawValScores[k].String()), logging.String("performance-score", scores.PerformanceScores[k].String()), logging.String("multisig-score", scores.MultisigScores[k].String()), logging.String("validator-score", scores.ValScores[k].String()), logging.String("normalised-score", scores.NormalisedScores[k].String()))
}
return scores, *optStake
}
// CalcDelegation extracts the delegation of the validator set from the delegation state slice and returns the total delegation.
func CalcDelegation(validators map[string]struct{}, delegationState []*types.ValidatorData) ([]*types.ValidatorData, num.Decimal) {
tv := map[string]num.Decimal{}
tvTotal := num.UintZero()
tvDelegation := []*types.ValidatorData{}
// split the delegation into tendermint and ersatz and count their respective totals
for _, ds := range delegationState {
if _, ok := validators[ds.NodeID]; ok {
tv[ds.NodeID] = num.DecimalZero()
stake := num.Sum(ds.SelfStake, ds.StakeByDelegators)
tvTotal.AddSum(stake)
tvDelegation = append(tvDelegation, ds)
}
}
tvTotalD := tvTotal.ToDecimal()
return tvDelegation, tvTotalD
}
func GetOptimalStake(tmTotalDelegation num.Decimal, numValidators int, params types.StakeScoreParams) num.Decimal {
if tmTotalDelegation.IsPositive() {
numVal := num.DecimalFromInt64(int64(numValidators))
return tmTotalDelegation.Div(num.MaxD(params.MinVal, numVal.Div(params.CompLevel)))
}
return num.DecimalZero()
}
// CalcAntiWhalingScore calculates the anti-whaling stake score for the validators represented in the given delegation set.
func CalcAntiWhalingScore(delegationState []*types.ValidatorData, totalStakeD, optStake num.Decimal, stakeScoreParams types.StakeScoreParams) map[string]num.Decimal {
stakeScore := make(map[string]num.Decimal, len(delegationState))
for _, ds := range delegationState {
if totalStakeD.IsPositive() {
stakeScore[ds.NodeID] = CalcValidatorScore(num.Sum(ds.SelfStake, ds.StakeByDelegators).ToDecimal(), totalStakeD, optStake, stakeScoreParams)
} else {
stakeScore[ds.NodeID] = num.DecimalZero()
}
}
return stakeScore
}