-
Notifications
You must be signed in to change notification settings - Fork 195
/
election.go
153 lines (135 loc) · 5.76 KB
/
election.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
// Copyright 2017 The Celo Authors
// This file is part of the celo library.
//
// The celo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The celo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the celo library. If not, see <http://www.gnu.org/licenses/>.
package election
import (
"math/big"
"sort"
"github.com/celo-org/celo-blockchain/common"
"github.com/celo-org/celo-blockchain/contracts"
"github.com/celo-org/celo-blockchain/contracts/abis"
"github.com/celo-org/celo-blockchain/core/vm"
"github.com/celo-org/celo-blockchain/log"
"github.com/celo-org/celo-blockchain/params"
)
var (
electValidatorSignersMethod = contracts.NewRegisteredContractMethod(params.ElectionRegistryId, abis.Elections, "electValidatorSigners", params.MaxGasForElectValidators)
getElectableValidatorsMethod = contracts.NewRegisteredContractMethod(params.ElectionRegistryId, abis.Elections, "getElectableValidators", params.MaxGasForGetElectableValidators)
electNValidatorSignersMethod = contracts.NewRegisteredContractMethod(params.ElectionRegistryId, abis.Elections, "electNValidatorSigners", params.MaxGasForElectNValidatorSigners)
getTotalVotesForEligibleValidatorGroupsMethod = contracts.NewRegisteredContractMethod(params.ElectionRegistryId, abis.Elections, "getTotalVotesForEligibleValidatorGroups", params.MaxGasForGetEligibleValidatorGroupsVoteTotals)
getGroupEpochRewardsMethod = contracts.NewRegisteredContractMethod(params.ElectionRegistryId, abis.Elections, "getGroupEpochRewards", params.MaxGasForGetGroupEpochRewards)
distributeEpochRewardsMethod = contracts.NewRegisteredContractMethod(params.ElectionRegistryId, abis.Elections, "distributeEpochRewards", params.MaxGasForDistributeEpochRewards)
)
func GetElectedValidators(vmRunner vm.EVMRunner) ([]common.Address, error) {
// Get the new epoch's validator set
var newValSet []common.Address
err := electValidatorSignersMethod.Query(vmRunner, &newValSet)
if err != nil {
return nil, err
}
return newValSet, nil
}
func ElectNValidatorSigners(vmRunner vm.EVMRunner, additionalAboveMaxElectable int64) ([]common.Address, error) {
// Get the electable min and max
var minElectableValidators *big.Int
var maxElectableValidators *big.Int
err := getElectableValidatorsMethod.Query(vmRunner, &[]interface{}{&minElectableValidators, &maxElectableValidators})
if err != nil {
return nil, err
}
// Run the validator election for up to maxElectable + getTotalVotesForEligibleValidatorGroup
var electedValidators []common.Address
err = electNValidatorSignersMethod.Query(vmRunner, &electedValidators, minElectableValidators, maxElectableValidators.Add(maxElectableValidators, big.NewInt(additionalAboveMaxElectable)))
if err != nil {
return nil, err
}
return electedValidators, nil
}
type voteTotal struct {
Group common.Address
Value *big.Int
}
func getTotalVotesForEligibleValidatorGroups(vmRunner vm.EVMRunner) ([]voteTotal, error) {
var groups []common.Address
var values []*big.Int
err := getTotalVotesForEligibleValidatorGroupsMethod.Query(vmRunner, &[]interface{}{&groups, &values})
if err != nil {
return nil, err
}
voteTotals := make([]voteTotal, len(groups))
for i, group := range groups {
log.Trace("Got group vote total", "group", group, "value", values[i])
voteTotals[i].Group = group
voteTotals[i].Value = values[i]
}
return voteTotals, err
}
func getGroupEpochRewards(vmRunner vm.EVMRunner, group common.Address, maxRewards *big.Int, uptimes []*big.Int) (*big.Int, error) {
var groupEpochRewards *big.Int
err := getGroupEpochRewardsMethod.Query(vmRunner, &groupEpochRewards, group, maxRewards, uptimes)
if err != nil {
return nil, err
}
return groupEpochRewards, nil
}
func DistributeEpochRewards(vmRunner vm.EVMRunner, groups []common.Address, maxTotalRewards *big.Int, uptimes map[common.Address][]*big.Int) (*big.Int, error) {
totalRewards := big.NewInt(0)
voteTotals, err := getTotalVotesForEligibleValidatorGroups(vmRunner)
if err != nil {
return totalRewards, err
}
rewards := make([]*big.Int, len(groups))
for i, group := range groups {
reward, err := getGroupEpochRewards(vmRunner, group, maxTotalRewards, uptimes[group])
if err != nil {
return totalRewards, err
}
rewards[i] = reward
log.Debug("Reward for group voters", "reward", reward, "group", group.String())
}
for i, group := range groups {
reward := rewards[i]
for _, voteTotal := range voteTotals {
if voteTotal.Group == group {
voteTotal.Value.Add(voteTotal.Value, reward)
break
}
}
// Sorting in descending order is necessary to match the order on-chain.
// TODO: We could make this more efficient by only moving the newly vote member.
sort.SliceStable(voteTotals, func(j, k int) bool {
return voteTotals[j].Value.Cmp(voteTotals[k].Value) > 0
})
lesser := common.ZeroAddress
greater := common.ZeroAddress
for j, voteTotal := range voteTotals {
if voteTotal.Group == group {
if j > 0 {
greater = voteTotals[j-1].Group
}
if j+1 < len(voteTotals) {
lesser = voteTotals[j+1].Group
}
break
}
}
err := distributeEpochRewardsMethod.Execute(vmRunner, nil, common.Big0, group, reward, lesser, greater)
if err != nil {
return totalRewards, err
}
totalRewards.Add(totalRewards, reward)
}
return totalRewards, nil
}