-
Notifications
You must be signed in to change notification settings - Fork 200
/
ballot.go
220 lines (184 loc) · 5.73 KB
/
ballot.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
package types
import (
"encoding/json"
"fmt"
"math"
"sort"
"strconv"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/NibiruChain/nibiru/x/common/asset"
)
// NOTE: we don't need to implement proto interface on this file
// these are not used in store or rpc response
// ExchangeRateVote is a convenience wrapper to reduce redundant lookup cost
type ExchangeRateVote struct {
Pair asset.Pair
ExchangeRate sdk.Dec // aka price
Voter sdk.ValAddress
Power int64 // how much tendermint consensus power this vote should have
}
// NewExchangeRateVote returns a new ExchangeRateVote instance
func NewExchangeRateVote(rate sdk.Dec, pair asset.Pair, voter sdk.ValAddress, power int64) ExchangeRateVote {
return ExchangeRateVote{
ExchangeRate: rate,
Pair: pair,
Voter: voter,
Power: power,
}
}
// ExchangeRateVotes is a convenience wrapper around a ExchangeRateVote slice
type ExchangeRateVotes []ExchangeRateVote
// ToMap return organized exchange rate map by validator
func (pb ExchangeRateVotes) ToMap() map[string]sdk.Dec {
validatorExchangeRateMap := make(map[string]sdk.Dec)
for _, vote := range pb {
if vote.ExchangeRate.IsPositive() {
validatorExchangeRateMap[string(vote.Voter)] = vote.ExchangeRate
}
}
return validatorExchangeRateMap
}
// ToCrossRate return cross_rate(base/exchange_rate) votes
func (pb ExchangeRateVotes) ToCrossRate(bases map[string]sdk.Dec) (cb ExchangeRateVotes) {
for i := range pb {
vote := pb[i]
if exchangeRateRT, ok := bases[string(vote.Voter)]; ok && vote.ExchangeRate.IsPositive() {
vote.ExchangeRate = exchangeRateRT.Quo(vote.ExchangeRate)
} else {
// If we can't get reference exchange rate, we just convert the vote as abstain vote
vote.ExchangeRate = sdk.ZeroDec()
vote.Power = 0
}
cb = append(cb, vote)
}
return
}
// NumValidVoters returns the number of voters who actually voted (i.e. did not abstain from voting for a pair).
func (v ExchangeRateVotes) NumValidVoters() uint64 {
count := 0
for _, vote := range v {
if vote.ExchangeRate.IsPositive() {
count++
}
}
return uint64(count)
}
// Power returns the total amount of voting power in the votes
func (v ExchangeRateVotes) Power() int64 {
totalPower := int64(0)
for _, vote := range v {
totalPower += vote.Power
}
return totalPower
}
// WeightedMedian returns the median weighted by the power of the ExchangeRateVote.
// CONTRACT: votes must be sorted
func (votes ExchangeRateVotes) WeightedMedian() sdk.Dec {
totalPower := votes.Power()
if votes.Len() > 0 {
pivot := int64(0)
for _, v := range votes {
votePower := v.Power
pivot += votePower
if pivot >= (totalPower / 2) {
return v.ExchangeRate
}
}
}
return sdk.ZeroDec()
}
// WeightedMedianWithAssertion returns the median weighted by the power of the ExchangeRateVote.
func (pb ExchangeRateVotes) WeightedMedianWithAssertion() sdk.Dec {
sort.Sort(pb)
totalPower := pb.Power()
if pb.Len() > 0 {
pivot := int64(0)
for _, v := range pb {
votePower := v.Power
pivot += votePower
if pivot >= (totalPower / 2) {
return v.ExchangeRate
}
}
}
return sdk.ZeroDec()
}
// StandardDeviation returns the standard deviation by the power of the ExchangeRateVote.
func (pb ExchangeRateVotes) StandardDeviation(median sdk.Dec) (standardDeviation sdk.Dec) {
if len(pb) == 0 {
return sdk.ZeroDec()
}
defer func() {
if e := recover(); e != nil {
standardDeviation = sdk.ZeroDec()
}
}()
sum := sdk.ZeroDec()
n := 0
for _, v := range pb {
// ignore abstain votes in std dev calculation
if v.ExchangeRate.IsPositive() {
deviation := v.ExchangeRate.Sub(median)
sum = sum.Add(deviation.Mul(deviation))
n += 1
}
}
variance := sum.QuoInt64(int64(n))
floatNum, _ := strconv.ParseFloat(variance.String(), 64)
floatNum = math.Sqrt(floatNum)
standardDeviation, _ = sdk.NewDecFromStr(fmt.Sprintf("%f", floatNum))
return
}
// Len implements sort.Interface
func (pb ExchangeRateVotes) Len() int {
return len(pb)
}
// Less reports whether the element with
// index i should sort before the element with index j.
func (pb ExchangeRateVotes) Less(i, j int) bool {
return pb[i].ExchangeRate.LT(pb[j].ExchangeRate)
}
// Swap implements sort.Interface.
func (pb ExchangeRateVotes) Swap(i, j int) {
pb[i], pb[j] = pb[j], pb[i]
}
// ValidatorPerformance keeps track of a validator performance in the voting period.
type ValidatorPerformance struct {
// Tendermint consensus voting power
Power int64
// RewardWeight: Weight of rewards the validator should receive in units of
// consensus power.
RewardWeight int64
WinCount int64 // Number of valid votes for which the validator will be rewarded
AbstainCount int64 // Number of abstained votes for which there will be no reward or punishment
MissCount int64 // Number of invalid/punishable votes
ValAddress sdk.ValAddress
}
// NewValidatorPerformance generates a ValidatorPerformance instance.
func NewValidatorPerformance(power int64, recipient sdk.ValAddress) ValidatorPerformance {
return ValidatorPerformance{
Power: power,
RewardWeight: 0,
WinCount: 0,
AbstainCount: 0,
MissCount: 0,
ValAddress: recipient,
}
}
type ValidatorPerformances map[string]ValidatorPerformance
// TotalRewardWeight returns the sum of the reward weight of all the validators included in the map
func (vp ValidatorPerformances) TotalRewardWeight() int64 {
totalRewardWeight := int64(0)
for _, validator := range vp {
totalRewardWeight += validator.RewardWeight
}
return totalRewardWeight
}
func (vp ValidatorPerformances) String() string {
jsonBz, _ := json.MarshalIndent(vp, "", " ")
return string(jsonBz)
}
func (vp ValidatorPerformance) String() string {
jsonBz, _ := json.MarshalIndent(vp, "", " ")
return string(jsonBz)
}