-
Notifications
You must be signed in to change notification settings - Fork 808
/
ballot.go
executable file
·108 lines (90 loc) · 2.89 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
package keeper
import (
"sort"
"strings"
"github.com/sei-protocol/sei-chain/x/oracle/types"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)
// OrganizeBallotByDenom collects all oracle votes for the period, categorized by the votes' denom parameter
func (k Keeper) OrganizeBallotByDenom(ctx sdk.Context, validatorClaimMap map[string]types.Claim) (votes map[string]types.ExchangeRateBallot) {
votes = map[string]types.ExchangeRateBallot{}
// Organize aggregate votes
aggregateHandler := func(voterAddr sdk.ValAddress, vote types.AggregateExchangeRateVote) (stop bool) {
// organize ballot only for the active validators
claim, ok := validatorClaimMap[vote.Voter]
if ok {
power := claim.Power
for _, tuple := range vote.ExchangeRateTuples {
tmpPower := power
if !tuple.ExchangeRate.IsPositive() {
// Make the power of abstain vote zero
tmpPower = 0
}
votes[tuple.Denom] = append(votes[tuple.Denom],
types.NewVoteForTally(
tuple.ExchangeRate,
tuple.Denom,
voterAddr,
tmpPower,
),
)
}
}
return false
}
k.IterateAggregateExchangeRateVotes(ctx, aggregateHandler)
// sort created ballot
for denom, ballot := range votes {
sort.Sort(ballot)
votes[denom] = ballot
}
return votes
}
// ClearBallots clears all tallied votes from the store
func (k Keeper) ClearBallots(ctx sdk.Context, _ uint64) {
// Clear all aggregate votes
k.IterateAggregateExchangeRateVotes(ctx, func(voterAddr sdk.ValAddress, aggregateVote types.AggregateExchangeRateVote) (stop bool) {
k.DeleteAggregateExchangeRateVote(ctx, voterAddr)
return false
})
}
// ApplyWhitelist update vote target denom list with params whitelist
func (k Keeper) ApplyWhitelist(ctx sdk.Context, whitelist types.DenomList, voteTargets map[string]types.Denom) {
// check is there any update in whitelist params
updateRequired := false
if len(voteTargets) != len(whitelist) {
updateRequired = true
} else {
for _, item := range whitelist {
if _, ok := voteTargets[item.Name]; !ok {
updateRequired = true
break
}
}
}
if updateRequired {
k.ClearVoteTargets(ctx)
for _, item := range whitelist {
k.SetVoteTarget(ctx, item.Name)
// Register meta data to bank module
if _, ok := k.bankKeeper.GetDenomMetaData(ctx, item.Name); !ok {
base := item.Name
display := base[1:]
nameSymbol := strings.ToUpper(display)
k.bankKeeper.SetDenomMetaData(ctx, banktypes.Metadata{
Description: display,
DenomUnits: []*banktypes.DenomUnit{
{Denom: "u" + display, Exponent: uint32(0), Aliases: []string{"micro" + display}},
{Denom: "m" + display, Exponent: uint32(3), Aliases: []string{"milli" + display}},
{Denom: display, Exponent: uint32(6), Aliases: []string{}},
},
Base: base,
Display: display,
Name: nameSymbol,
Symbol: nameSymbol,
})
}
}
}
}