-
Notifications
You must be signed in to change notification settings - Fork 199
/
ratingsData.go
249 lines (221 loc) · 9.61 KB
/
ratingsData.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
package rating
import (
"fmt"
"math"
"github.com/ElrondNetwork/elrond-go/config"
"github.com/ElrondNetwork/elrond-go/process"
)
var _ process.RatingsInfoHandler = (*RatingsData)(nil)
const milisecondsInHour = 3600 * 1000
type computeRatingStepArg struct {
shardSize uint32
consensusSize uint32
roundTimeMilis uint64
startRating uint32
maxRating uint32
hoursToMaxRatingFromStartRating uint32
proposerDecreaseFactor float32
validatorDecreaseFactor float32
consecutiveMissedBlocksPenalty float32
proposerValidatorImportance float32
}
// RatingsData will store information about ratingsComputation
type RatingsData struct {
startRating uint32
maxRating uint32
minRating uint32
signedBlocksThreshold float32
metaRatingsStepData process.RatingsStepHandler
shardRatingsStepData process.RatingsStepHandler
selectionChances []process.SelectionChance
}
// RatingsDataArg contains information for the creation of the new ratingsData
type RatingsDataArg struct {
Config config.RatingsConfig
ShardConsensusSize uint32
MetaConsensusSize uint32
ShardMinNodes uint32
MetaMinNodes uint32
RoundDurationMiliseconds uint64
}
// NewRatingsData creates a new RatingsData instance
func NewRatingsData(args RatingsDataArg) (*RatingsData, error) {
ratingsConfig := args.Config
err := verifyRatingsConfig(ratingsConfig)
if err != nil {
return nil, err
}
chances := make([]process.SelectionChance, 0)
for _, chance := range ratingsConfig.General.SelectionChances {
chances = append(chances, &SelectionChance{
MaxThreshold: chance.MaxThreshold,
ChancePercent: chance.ChancePercent,
})
}
arg := computeRatingStepArg{
shardSize: args.ShardMinNodes,
consensusSize: args.ShardConsensusSize,
roundTimeMilis: args.RoundDurationMiliseconds,
startRating: ratingsConfig.General.StartRating,
maxRating: ratingsConfig.General.MaxRating,
hoursToMaxRatingFromStartRating: ratingsConfig.ShardChain.HoursToMaxRatingFromStartRating,
proposerDecreaseFactor: ratingsConfig.ShardChain.ProposerDecreaseFactor,
validatorDecreaseFactor: ratingsConfig.ShardChain.ValidatorDecreaseFactor,
consecutiveMissedBlocksPenalty: ratingsConfig.ShardChain.ConsecutiveMissedBlocksPenalty,
proposerValidatorImportance: ratingsConfig.ShardChain.ProposerValidatorImportance,
}
shardRatingStep, err := computeRatingStep(arg)
if err != nil {
return nil, err
}
arg = computeRatingStepArg{
shardSize: args.MetaMinNodes,
consensusSize: args.MetaConsensusSize,
roundTimeMilis: args.RoundDurationMiliseconds,
startRating: ratingsConfig.General.StartRating,
maxRating: ratingsConfig.General.MaxRating,
hoursToMaxRatingFromStartRating: ratingsConfig.MetaChain.HoursToMaxRatingFromStartRating,
proposerDecreaseFactor: ratingsConfig.MetaChain.ProposerDecreaseFactor,
validatorDecreaseFactor: ratingsConfig.MetaChain.ValidatorDecreaseFactor,
consecutiveMissedBlocksPenalty: ratingsConfig.MetaChain.ConsecutiveMissedBlocksPenalty,
proposerValidatorImportance: ratingsConfig.MetaChain.ProposerValidatorImportance,
}
metaRatingStep, err := computeRatingStep(arg)
if err != nil {
return nil, err
}
return &RatingsData{
startRating: ratingsConfig.General.StartRating,
maxRating: ratingsConfig.General.MaxRating,
minRating: ratingsConfig.General.MinRating,
signedBlocksThreshold: ratingsConfig.General.SignedBlocksThreshold,
metaRatingsStepData: metaRatingStep,
shardRatingsStepData: shardRatingStep,
selectionChances: chances,
}, nil
}
func verifyRatingsConfig(settings config.RatingsConfig) error {
if settings.General.MinRating < 1 {
return process.ErrMinRatingSmallerThanOne
}
if settings.General.MinRating > settings.General.MaxRating {
return fmt.Errorf("%w: minRating: %v, maxRating: %v",
process.ErrMaxRatingIsSmallerThanMinRating,
settings.General.MinRating,
settings.General.MaxRating)
}
if settings.General.MaxRating < settings.General.StartRating || settings.General.MinRating > settings.General.StartRating {
return fmt.Errorf("%w: minRating: %v, startRating: %v, maxRating: %v",
process.ErrStartRatingNotBetweenMinAndMax,
settings.General.MinRating,
settings.General.StartRating,
settings.General.MaxRating)
}
if settings.General.SignedBlocksThreshold > 1 || settings.General.SignedBlocksThreshold < 0 {
return fmt.Errorf("%w signedBlocksThreshold: %v",
process.ErrSignedBlocksThresholdNotBetweenZeroAndOne,
settings.General.SignedBlocksThreshold)
}
if settings.ShardChain.HoursToMaxRatingFromStartRating == 0 {
return fmt.Errorf("%w hoursToMaxRatingFromStartRating: shardChain",
process.ErrHoursToMaxRatingFromStartRatingZero)
}
if settings.MetaChain.HoursToMaxRatingFromStartRating == 0 {
return fmt.Errorf("%w hoursToMaxRatingFromStartRating: metachain",
process.ErrHoursToMaxRatingFromStartRatingZero)
}
if settings.MetaChain.ConsecutiveMissedBlocksPenalty < 1 {
return fmt.Errorf("%w: metaChain consecutiveMissedBlocksPenalty: %v",
process.ErrConsecutiveMissedBlocksPenaltyLowerThanOne,
settings.MetaChain.ConsecutiveMissedBlocksPenalty)
}
if settings.ShardChain.ConsecutiveMissedBlocksPenalty < 1 {
return fmt.Errorf("%w: shardChain consecutiveMissedBlocksPenalty: %v",
process.ErrConsecutiveMissedBlocksPenaltyLowerThanOne,
settings.ShardChain.ConsecutiveMissedBlocksPenalty)
}
if settings.ShardChain.ProposerDecreaseFactor > -1 || settings.ShardChain.ValidatorDecreaseFactor > -1 {
return fmt.Errorf("%w: shardChain decrease steps - proposer: %v, validator: %v",
process.ErrDecreaseRatingsStepMoreThanMinusOne,
settings.ShardChain.ProposerDecreaseFactor,
settings.ShardChain.ValidatorDecreaseFactor)
}
if settings.MetaChain.ProposerDecreaseFactor > -1 || settings.MetaChain.ValidatorDecreaseFactor > -1 {
return fmt.Errorf("%w: metachain decrease steps - proposer: %v, validator: %v",
process.ErrDecreaseRatingsStepMoreThanMinusOne,
settings.MetaChain.ProposerDecreaseFactor,
settings.MetaChain.ValidatorDecreaseFactor)
}
return nil
}
func computeRatingStep(
arg computeRatingStepArg,
) (process.RatingsStepHandler, error) {
blocksProducedInHours := uint64(arg.hoursToMaxRatingFromStartRating*milisecondsInHour) / arg.roundTimeMilis
ratingDifference := arg.maxRating - arg.startRating
proposerProbability := float32(blocksProducedInHours) / float32(arg.shardSize)
validatorProbability := proposerProbability * float32(arg.consensusSize)
totalImportance := arg.proposerValidatorImportance + 1
ratingFromProposer := float32(ratingDifference) * (arg.proposerValidatorImportance / totalImportance)
ratingFromValidator := float32(ratingDifference) * (1 / totalImportance)
proposerIncrease := ratingFromProposer / proposerProbability
validatorIncrease := ratingFromValidator / validatorProbability
proposerDecrease := proposerIncrease * arg.proposerDecreaseFactor
validatorDecrease := validatorIncrease * arg.validatorDecreaseFactor
if proposerIncrease > math.MaxInt32 {
return nil, fmt.Errorf("%w proposerIncrease overflowed %v", process.ErrOverflow, proposerIncrease)
}
if validatorIncrease > math.MaxInt32 {
return nil, fmt.Errorf("%w validatorIncrease overflowed %v", process.ErrOverflow, validatorIncrease)
}
if proposerDecrease < math.MinInt32 {
return nil, fmt.Errorf("%w proposerDecrease overflowed %v", process.ErrOverflow, proposerDecrease)
}
if validatorDecrease < math.MinInt32 {
return nil, fmt.Errorf("%w validatorDecrease overflowed %v", process.ErrOverflow, validatorDecrease)
}
if int32(proposerIncrease) < 1 {
return nil, fmt.Errorf("%w proposerIncrease zero: %v", process.ErrIncreaseStepLowerThanOne, proposerIncrease)
}
if int32(validatorIncrease) < 1 {
return nil, fmt.Errorf("%w validatorIncrease zero: %v", process.ErrIncreaseStepLowerThanOne, validatorIncrease)
}
return &RatingStep{
proposerIncreaseRatingStep: int32(proposerIncrease),
proposerDecreaseRatingStep: int32(proposerDecrease),
validatorIncreaseRatingStep: int32(validatorIncrease),
validatorDecreaseRatingStep: int32(validatorDecrease),
consecutiveMissedBlocksPenalty: arg.consecutiveMissedBlocksPenalty}, nil
}
// StartRating will return the start rating
func (rd *RatingsData) StartRating() uint32 {
return rd.startRating
}
// MaxRating will return the max rating
func (rd *RatingsData) MaxRating() uint32 {
return rd.maxRating
}
// MinRating will return the min rating
func (rd *RatingsData) MinRating() uint32 {
return rd.minRating
}
// SignedBlocksThreshold will return the signed blocks threshold
func (rd *RatingsData) SignedBlocksThreshold() float32 {
return rd.signedBlocksThreshold
}
// SelectionChances will return the array of selectionChances and thresholds
func (rd *RatingsData) SelectionChances() []process.SelectionChance {
return rd.selectionChances
}
// MetaChainRatingsStepHandler returns the RatingsStepHandler used for the Metachain
func (rd *RatingsData) MetaChainRatingsStepHandler() process.RatingsStepHandler {
return rd.metaRatingsStepData
}
// ShardChainRatingsStepHandler returns the RatingsStepHandler used for the ShardChains
func (rd *RatingsData) ShardChainRatingsStepHandler() process.RatingsStepHandler {
return rd.shardRatingsStepData
}
// IsInterfaceNil returns true if underlying object is nil
func (rd *RatingsData) IsInterfaceNil() bool {
return rd == nil
}