-
Notifications
You must be signed in to change notification settings - Fork 19
/
topology_stub.go
191 lines (158 loc) · 5.6 KB
/
topology_stub.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
// 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 stubs
import (
"context"
"sort"
"code.vegaprotocol.io/vega/core/events"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/core/validators"
"code.vegaprotocol.io/vega/libs/num"
)
type TopologyStub struct {
validators map[string]string
nodeID string
broker *BrokerStub
minDelegation num.Decimal
}
func NewTopologyStub(nodeID string, broker *BrokerStub) *TopologyStub {
return &TopologyStub{
validators: map[string]string{},
nodeID: nodeID,
broker: broker,
}
}
func (ts *TopologyStub) OnMinDelegationUpdated(_ context.Context, minDelegation num.Decimal) error {
ts.minDelegation = minDelegation
return nil
}
func (ts *TopologyStub) Len() int {
return len(ts.validators)
}
func (ts *TopologyStub) ValidatorPerformanceScore(nodeID string) num.Decimal {
return num.DecimalFromFloat(1)
}
func (ts *TopologyStub) SelfNodeID() string {
return ts.nodeID
}
func (ts *TopologyStub) SelfVegaPubKey() string {
return ts.nodeID
}
func (ts *TopologyStub) IsValidator() bool {
return true
}
func (ts *TopologyStub) IsValidatorVegaPubKey(pubKey string) bool {
return true
}
func (ts *TopologyStub) IsTendermintValidator(pubKey string) bool {
return true
}
func (ts *TopologyStub) IsValidatorNodeID(nodeID string) bool {
_, ok := ts.validators[nodeID]
return ok
}
func (ts *TopologyStub) RecalcValidatorSet(ctx context.Context, epochSeq string, delegationState []*types.ValidatorData, stakeScoreParams types.StakeScoreParams) []*types.PartyContributionScore {
return []*types.PartyContributionScore{}
}
func (ts *TopologyStub) AllNodeIDs() []string {
nodes := make([]string, 0, len(ts.validators))
for n := range ts.validators {
nodes = append(nodes, n)
}
sort.Strings(nodes)
return nodes
}
func (ts *TopologyStub) AllVegaPubKeys() []string {
nodes := make([]string, 0, len(ts.validators))
for _, pk := range ts.validators {
nodes = append(nodes, pk)
}
sort.Strings(nodes)
return nodes
}
func (ts *TopologyStub) Get(key string) *validators.ValidatorData {
if data, ok := ts.validators[key]; ok {
return &validators.ValidatorData{
ID: key,
VegaPubKey: data,
}
}
return nil
}
func (ts *TopologyStub) AddValidator(node string, pubkey string) {
ts.validators[node] = pubkey
}
func (ts *TopologyStub) GetRewardsScores(ctx context.Context, epochSeq string, delegationState []*types.ValidatorData, stakeScoreParams types.StakeScoreParams) (*types.ScoreData, *types.ScoreData) {
tmScores := ts.calculateTMScores(delegationState, stakeScoreParams)
evts := make([]events.Event, 0, len(tmScores.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"))
}
ts.broker.SendBatch(evts)
return tmScores, &types.ScoreData{}
}
// calculateTMScores returns the reward validator scores for the tendermint validatore.
func (ts *TopologyStub) calculateTMScores(delegationState []*types.ValidatorData, stakeScoreParams types.StakeScoreParams) *types.ScoreData {
tmScores := &types.ScoreData{}
validatorSet := make(map[string]struct{}, len(delegationState))
tmScores.PerformanceScores = make(map[string]num.Decimal, len(delegationState))
for _, ds := range delegationState {
validatorSet[ds.NodeID] = struct{}{}
if ds.SelfStake.ToDecimal().GreaterThanOrEqual(ts.minDelegation) {
tmScores.PerformanceScores[ds.NodeID] = num.DecimalFromFloat(1)
} else {
tmScores.PerformanceScores[ds.NodeID] = num.DecimalZero()
}
}
tmDelegation, tmTotalDelegation := validators.CalcDelegation(validatorSet, delegationState)
optStake := validators.GetOptimalStake(tmTotalDelegation, len(tmDelegation), stakeScoreParams)
tv := validators.CalcAntiWhalingScore(tmDelegation, tmTotalDelegation, optStake, stakeScoreParams)
tmScores.RawValScores = tv
tmScores.ValScores = make(map[string]num.Decimal, len(tv))
for k, d := range tv {
tmScores.ValScores[k] = d.Mul(tmScores.PerformanceScores[k])
}
// normalise the scores
tmScores.NormalisedScores = ts.normaliseScores(tmScores.ValScores)
// sort the list of tm validators
tmNodeIDs := make([]string, 0, len(tv))
for k := range tv {
tmNodeIDs = append(tmNodeIDs, k)
}
sort.Strings(tmNodeIDs)
tmScores.NodeIDSlice = tmNodeIDs
return tmScores
}
func (ts *TopologyStub) normaliseScores(scores map[string]num.Decimal) map[string]num.Decimal {
totalScore := num.DecimalZero()
for _, v := range scores {
totalScore = totalScore.Add(v)
}
normScores := make(map[string]num.Decimal, len(scores))
for n, s := range scores {
if totalScore.IsPositive() {
normScores[n] = s.Div(totalScore)
} else {
normScores[n] = num.DecimalZero()
}
}
return normScores
}
func (*TopologyStub) GetVotingPower(pubkey string) int64 {
return 1
}
func (*TopologyStub) GetTotalVotingPower() int64 {
return 1
}