/
ballot.go
202 lines (161 loc) · 4.23 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
package types
import (
"bytes"
"sort"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// VoteForTally is a convenience wrapper to reduce redundant lookup cost.
type VoteForTally struct {
Denom string
ExchangeRate sdk.Dec
Voter sdk.ValAddress
Power int64
}
// NewVoteForTally returns a new VoteForTally instance.
func NewVoteForTally(rate sdk.Dec, denom string, voter sdk.ValAddress, power int64) VoteForTally {
return VoteForTally{
ExchangeRate: rate,
Denom: denom,
Voter: voter,
Power: power,
}
}
// ExchangeRateBallot is a convenience wrapper around a ExchangeRateVote slice.
type ExchangeRateBallot []VoteForTally
// ToMap return organized exchange rate map by validator.
func (pb ExchangeRateBallot) ToMap() map[string]sdk.Dec {
exchangeRateMap := make(map[string]sdk.Dec)
for _, vote := range pb {
if vote.ExchangeRate.IsPositive() {
exchangeRateMap[vote.Voter.String()] = vote.ExchangeRate
}
}
return exchangeRateMap
}
// Power returns the total amount of voting power in the ballot.
func (pb ExchangeRateBallot) Power() int64 {
var totalPower int64
for _, vote := range pb {
totalPower += vote.Power
}
return totalPower
}
// WeightedMedian returns the median weighted by the power of the ExchangeRateVote.
// CONTRACT: The ballot must be sorted.
func (pb ExchangeRateBallot) WeightedMedian() (sdk.Dec, error) {
if !sort.IsSorted(pb) {
return sdk.ZeroDec(), ErrBallotNotSorted
}
if pb.Len() > 0 {
totalPower := pb.Power()
var pivot int64
for _, v := range pb {
votePower := v.Power
pivot += votePower
if pivot >= (totalPower / 2) {
return v.ExchangeRate, nil
}
}
}
return sdk.ZeroDec(), nil
}
// StandardDeviation returns the standard deviation by the power of the ExchangeRateVote.
func (pb ExchangeRateBallot) StandardDeviation() (sdk.Dec, error) {
if len(pb) == 0 {
return sdk.ZeroDec(), nil
}
median, err := pb.WeightedMedian()
if err != nil {
return sdk.ZeroDec(), err
}
sum := sdk.ZeroDec()
ballotLength := int64(len(pb))
for _, v := range pb {
func() {
defer func() {
if e := recover(); e != nil {
ballotLength--
}
}()
deviation := v.ExchangeRate.Sub(median)
sum = sum.Add(deviation.Mul(deviation))
}()
}
variance := sum.QuoInt64(ballotLength)
standardDeviation, err := variance.ApproxSqrt()
if err != nil {
return sdk.ZeroDec(), err
}
return standardDeviation, nil
}
// Len implements sort.Interface
func (pb ExchangeRateBallot) Len() int {
return len(pb)
}
// Less reports whether the element with
// index i should sort before the element with index j.
func (pb ExchangeRateBallot) Less(i, j int) bool {
return pb[i].ExchangeRate.LT(pb[j].ExchangeRate)
}
// Swap implements sort.Interface.
func (pb ExchangeRateBallot) Swap(i, j int) {
pb[i], pb[j] = pb[j], pb[i]
}
// BallotDenom is a convenience wrapper for setting rates deterministically.
type BallotDenom struct {
Ballot ExchangeRateBallot
Denom string
}
// BallotMapToSlice returns an array of sorted exchange rate ballots.
func BallotMapToSlice(votes map[string]ExchangeRateBallot) []BallotDenom {
b := make([]BallotDenom, len(votes))
i := 0
for denom, ballot := range votes {
b[i] = BallotDenom{
Denom: denom,
Ballot: ballot,
}
i++
}
sort.Slice(b, func(i, j int) bool {
return b[i].Denom < b[j].Denom
})
return b
}
// Claim is an interface that directs its rewards to an attached bank account.
type Claim struct {
Power int64
Weight int64
WinCount int64
Recipient sdk.ValAddress
}
// NewClaim generates a Claim instance.
func NewClaim(power, weight, winCount int64, recipient sdk.ValAddress) Claim {
return Claim{
Power: power,
Weight: weight,
WinCount: winCount,
Recipient: recipient,
}
}
// ClaimMapToSlice returns an array of sorted exchange rate ballots.
func ClaimMapToSlice(claims map[string]Claim) []Claim {
c := make([]Claim, len(claims))
i := 0
for _, claim := range claims {
c[i] = Claim{
Power: claim.Power,
Weight: claim.Weight,
WinCount: claim.WinCount,
Recipient: claim.Recipient,
}
i++
}
sort.Slice(c, func(i, j int) bool {
if val := bytes.Compare(c[i].Recipient.Bytes(), c[j].Recipient.Bytes()); val == -1 {
return true
}
return false
})
return c
}