cosmos-sdk/x/gov/tally.go

127 lines
4.1 KiB
Go
Raw Normal View History

2018-06-21 17:19:14 -07:00
package gov
import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
// 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
Minus sdk.Dec // Minus of validator, used to compute validator's voting power
2018-07-06 00:06:53 -07:00
Vote VoteOption // Vote of the validator
2018-06-21 17:19:14 -07:00
}
func newValidatorGovInfo(address sdk.ValAddress, bondedTokens sdk.Int, delegatorShares,
minus sdk.Dec, vote VoteOption) validatorGovInfo {
return validatorGovInfo{
Address: address,
BondedTokens: bondedTokens,
DelegatorShares: delegatorShares,
Minus: minus,
Vote: vote,
}
}
func tally(ctx sdk.Context, keeper Keeper, proposal Proposal) (passes bool, tallyResults TallyResult) {
results := make(map[VoteOption]sdk.Dec)
results[OptionYes] = sdk.ZeroDec()
results[OptionAbstain] = sdk.ZeroDec()
results[OptionNo] = sdk.ZeroDec()
results[OptionNoWithVeto] = sdk.ZeroDec()
2018-06-21 17:19:14 -07:00
totalVotingPower := sdk.ZeroDec()
2018-06-21 17:19:14 -07:00
currValidators := make(map[string]validatorGovInfo)
keeper.vs.IterateBondedValidatorsByPower(ctx, func(index int64, validator sdk.Validator) (stop bool) {
currValidators[validator.GetOperator().String()] = newValidatorGovInfo(
validator.GetOperator(),
validator.GetBondedTokens(),
validator.GetDelegatorShares(),
sdk.ZeroDec(),
OptionEmpty,
)
2018-06-21 17:19:14 -07:00
return false
})
// iterate over all the votes
votesIterator := keeper.GetVotes(ctx, proposal.GetProposalID())
defer votesIterator.Close()
2018-06-21 17:19:14 -07:00
for ; votesIterator.Valid(); votesIterator.Next() {
vote := &Vote{}
keeper.cdc.MustUnmarshalBinaryLengthPrefixed(votesIterator.Value(), vote)
2018-06-21 17:19:14 -07:00
// 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 {
2018-06-21 17:19:14 -07:00
val.Vote = vote.Option
currValidators[valAddrStr] = val
2018-06-21 17:19:14 -07:00
} else {
keeper.ds.IterateDelegations(ctx, vote.Voter, func(index int64, delegation sdk.Delegation) (stop bool) {
valAddrStr := delegation.GetValidatorAddr().String()
if val, ok := currValidators[valAddrStr]; ok {
val.Minus = val.Minus.Add(delegation.GetShares())
currValidators[valAddrStr] = val
2018-06-21 17:19:14 -07:00
delegatorShare := delegation.GetShares().Quo(val.DelegatorShares)
votingPower := delegatorShare.MulInt(val.BondedTokens)
2018-06-21 17:19:14 -07:00
results[vote.Option] = results[vote.Option].Add(votingPower)
totalVotingPower = totalVotingPower.Add(votingPower)
}
2018-06-21 17:19:14 -07:00
return false
})
}
keeper.deleteVote(ctx, vote.ProposalID, vote.Voter)
}
// iterate over the validators again to tally their voting power
2018-06-21 17:19:14 -07:00
for _, val := range currValidators {
if val.Vote == OptionEmpty {
continue
}
2018-06-21 17:19:14 -07:00
sharesAfterMinus := val.DelegatorShares.Sub(val.Minus)
percentAfterMinus := sharesAfterMinus.Quo(val.DelegatorShares)
votingPower := percentAfterMinus.MulInt(val.BondedTokens)
2018-06-21 17:19:14 -07:00
results[val.Vote] = results[val.Vote].Add(votingPower)
totalVotingPower = totalVotingPower.Add(votingPower)
}
tallyParams := keeper.GetTallyParams(ctx)
tallyResults = NewTallyResultFromMap(results)
2018-12-10 02:50:20 -08:00
// If there is no staked coins, the proposal fails
if keeper.vs.TotalPower(ctx).IsZero() {
2018-12-10 02:50:20 -08:00
return false, tallyResults
}
// If there is not enough quorum of votes, the proposal fails
percentVoting := totalVotingPower.Quo(sdk.NewDecFromInt(keeper.vs.TotalPower(ctx)))
if percentVoting.LT(tallyParams.Quorum) {
2018-12-10 02:50:20 -08:00
return false, tallyResults
}
// If no one votes (everyone abstains), proposal fails
if totalVotingPower.Sub(results[OptionAbstain]).Equal(sdk.ZeroDec()) {
return false, tallyResults
2018-06-21 17:19:14 -07:00
}
// If more than 1/3 of voters veto, proposal fails
if results[OptionNoWithVeto].Quo(totalVotingPower).GT(tallyParams.Veto) {
return false, tallyResults
2018-06-21 17:19:14 -07:00
}
// If more than 1/2 of non-abstaining voters vote Yes, proposal passes
if results[OptionYes].Quo(totalVotingPower.Sub(results[OptionAbstain])).GT(tallyParams.Threshold) {
return true, tallyResults
2018-06-21 17:19:14 -07:00
}
// If more than 1/2 of non-abstaining voters vote No, proposal fails
return false, tallyResults
2018-06-21 17:19:14 -07:00
}