-
Notifications
You must be signed in to change notification settings - Fork 2
/
tally.go
215 lines (193 loc) · 7.88 KB
/
tally.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
package keeper
import (
sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
"github.com/fibonacci-chain/fbc/x/gov/types"
"github.com/fibonacci-chain/fbc/x/staking/exported"
)
// validatorGovInfo used for tallying
type validatorGovInfo struct {
Address sdk.ValAddress // address of the validator operator
BondedTokens sdk.Int // Power of a Validator
DelegatorShares sdk.Dec // Total outstanding delegator shares
DelegatorDeductions sdk.Dec // Delegator deductions from validator's delegators voting independently
Vote types.VoteOption // Vote of the validator
}
func newValidatorGovInfo(address sdk.ValAddress, bondedTokens sdk.Int, delegatorShares,
delegatorDeductions sdk.Dec, vote types.VoteOption) validatorGovInfo {
return validatorGovInfo{
Address: address,
BondedTokens: bondedTokens,
DelegatorShares: delegatorShares,
DelegatorDeductions: delegatorDeductions,
Vote: vote,
}
}
func tallyDelegatorVotes(
ctx sdk.Context, keeper Keeper, currValidators map[string]validatorGovInfo, proposalID uint64,
voteP *types.Vote, voterPower, totalVotedPower *sdk.Dec, results map[types.VoteOption]sdk.Dec,
) {
// iterate over all the votes
votesIterator := keeper.GetVotes(ctx, proposalID)
if voteP != nil {
votesIterator = append(votesIterator, *voteP)
}
for i := 0; i < len(votesIterator); i++ {
vote := votesIterator[i]
// if validator, just record it in the map
// if delegator tally voting power
valAddrStr := sdk.ValAddress(vote.Voter).String()
if val, ok := currValidators[valAddrStr]; ok {
val.Vote = vote.Option
currValidators[valAddrStr] = val
} else {
// iterate over all delegations from voter, deduct from any delegated-to validators
delegation := keeper.sk.Delegator(ctx, vote.Voter)
if delegation == nil {
continue
}
for _, val := range delegation.GetShareAddedValidatorAddresses() {
valAddrStr := val.String()
if valInfo, ok := currValidators[valAddrStr]; ok {
valInfo.DelegatorDeductions = valInfo.DelegatorDeductions.Add(delegation.GetLastAddedShares())
currValidators[valAddrStr] = valInfo
votedPower := delegation.GetLastAddedShares()
// calculate vote power of delegator for voterPowerRate
if voteP != nil && vote.Voter.Equals(voteP.Voter) {
voterPower.Add(votedPower)
}
results[vote.Option] = results[vote.Option].Add(votedPower)
*totalVotedPower = totalVotedPower.Add(votedPower)
}
}
}
}
}
func tallyValidatorVotes(
currValidators map[string]validatorGovInfo, voteP *types.Vote, voterPower,
totalPower, totalVotedPower *sdk.Dec, results map[types.VoteOption]sdk.Dec,
) {
// iterate over the validators again to tally their voting power
for key, val := range currValidators {
// calculate all vote power of current validators including delegated for voterPowerRate
*totalPower = totalPower.Add(val.DelegatorShares)
if val.Vote == types.OptionEmpty {
continue
}
valValidVotedPower := val.DelegatorShares.Sub(val.DelegatorDeductions)
if voteP != nil && sdk.ValAddress(voteP.Voter).String() == key {
// calculate vote power of validator after deduction for voterPowerRate
*voterPower = voterPower.Add(valValidVotedPower)
}
results[val.Vote] = results[val.Vote].Add(valValidVotedPower)
*totalVotedPower = totalVotedPower.Add(valValidVotedPower)
}
}
func preTally(
ctx sdk.Context, keeper Keeper, proposal types.Proposal, voteP *types.Vote,
) (results map[types.VoteOption]sdk.Dec, totalVotedPower sdk.Dec, voterPowerRate sdk.Dec) {
results = make(map[types.VoteOption]sdk.Dec)
results[types.OptionYes] = sdk.ZeroDec()
results[types.OptionAbstain] = sdk.ZeroDec()
results[types.OptionNo] = sdk.ZeroDec()
results[types.OptionNoWithVeto] = sdk.ZeroDec()
totalVotedPower = sdk.ZeroDec()
totalPower := sdk.ZeroDec()
voterPower := sdk.ZeroDec()
currValidators := make(map[string]validatorGovInfo)
// fetch all the current validators except candidate, insert them into currValidators
keeper.sk.IterateBondedValidatorsByPower(ctx, func(index int64, validator exported.ValidatorI) (stop bool) {
currValidators[validator.GetOperator().String()] = newValidatorGovInfo(
validator.GetOperator(),
validator.GetBondedTokens(),
validator.GetDelegatorShares(),
sdk.ZeroDec(),
types.OptionEmpty,
)
return false
})
tallyDelegatorVotes(ctx, keeper, currValidators, proposal.ProposalID,
voteP, &voterPower, &totalVotedPower, results)
tallyValidatorVotes(currValidators, voteP, &voterPower, &totalPower, &totalVotedPower, results)
if totalPower.GT(sdk.ZeroDec()) {
voterPowerRate = voterPower.Quo(totalPower)
} else {
voterPowerRate = sdk.ZeroDec()
}
return results, totalVotedPower, voterPowerRate
}
// tally and return status before voting period end time
func tallyStatusInVotePeriod(
ctx sdk.Context, keeper Keeper, tallyResults types.TallyResult,
) (types.ProposalStatus, bool) {
tallyParams := keeper.GetTallyParams(ctx)
totalPower := tallyResults.TotalPower
// TODO: Upgrade the spec to cover all of these cases & remove pseudocode.
// If there is no staked coins, the proposal fails
if totalPower.IsZero() {
return types.StatusRejected, false
}
// If no one votes (everyone abstains), proposal fails
if totalPower.Sub(tallyResults.Abstain).Equal(sdk.ZeroDec()) {
return types.StatusRejected, false
}
// If more than 1/3 of voters veto, proposal fails
if tallyResults.NoWithVeto.Quo(totalPower).GT(tallyParams.Veto) {
return types.StatusRejected, true
}
// If more than or equal to 1/2 of non-abstain vote not Yes, proposal fails
if tallyResults.NoWithVeto.Add(tallyResults.No).Quo(totalPower.Sub(tallyResults.Abstain)).
GTE(tallyParams.Threshold) {
return types.StatusRejected, false
}
// If more than 2/3 of totalPower vote Yes, proposal passes
if tallyResults.Yes.Quo(totalPower).GT(tallyParams.YesInVotePeriod) {
return types.StatusPassed, false
}
return types.StatusVotingPeriod, false
}
// tally and return status expire voting period end time
func tallyStatusExpireVotePeriod(
ctx sdk.Context, keeper Keeper, tallyResults types.TallyResult,
) (types.ProposalStatus, bool) {
tallyParams := keeper.GetTallyParams(ctx)
totalVoted := tallyResults.TotalVotedPower
totalPower := tallyResults.TotalPower
// TODO: Upgrade the spec to cover all of these cases & remove pseudo code.
// If there is no staked coins, the proposal fails
if totalPower.IsZero() {
return types.StatusRejected, false
}
// If there is not enough quorum of votes, the proposal fails
percentVoting := totalVoted.Quo(totalPower)
if percentVoting.LT(tallyParams.Quorum) {
return types.StatusRejected, true
}
// If no one votes (everyone abstains), proposal fails
if totalVoted.Sub(tallyResults.Abstain).Equal(sdk.ZeroDec()) {
return types.StatusRejected, false
}
// If more than 1/3 of voters veto, proposal fails
if tallyResults.NoWithVeto.Quo(totalVoted).GT(tallyParams.Veto) {
return types.StatusRejected, true
}
// If more than 1/2 of non-abstaining voters vote Yes, proposal passes
if tallyResults.Yes.Quo(totalVoted.Sub(tallyResults.Abstain)).GT(tallyParams.Threshold) {
return types.StatusPassed, false
}
// If more than 1/2 of non-abstaining voters vote No, proposal fails
return types.StatusRejected, false
}
// Tally counts the votes for proposal
func Tally(ctx sdk.Context, keeper Keeper, proposal types.Proposal, isExpireVoteEndTime bool,
) (types.ProposalStatus, bool, types.TallyResult) {
results, totalVotedPower, _ := preTally(ctx, keeper, proposal, nil)
tallyResults := types.NewTallyResultFromMap(results)
tallyResults.TotalPower = keeper.totalPower(ctx)
tallyResults.TotalVotedPower = totalVotedPower
if isExpireVoteEndTime {
status, distribute := tallyStatusExpireVotePeriod(ctx, keeper, tallyResults)
return status, distribute, tallyResults
}
status, distribute := tallyStatusInVotePeriod(ctx, keeper, tallyResults)
return status, distribute, tallyResults
}