-
Notifications
You must be signed in to change notification settings - Fork 2
/
roster.go
271 lines (238 loc) · 7.36 KB
/
roster.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package votepower
import (
"encoding/hex"
"encoding/json"
"fmt"
"math/big"
"sort"
"github.com/PositionExchange/posichain/shard"
bls_core "github.com/PositionExchange/bls/ffi/go/bls"
"github.com/PositionExchange/posichain/crypto/bls"
"github.com/PositionExchange/posichain/internal/utils"
"github.com/PositionExchange/posichain/numeric"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
)
var (
// ErrVotingPowerNotEqualOne ..
ErrVotingPowerNotEqualOne = errors.New("voting power not equal to one")
)
// Ballot is a vote cast by a validator
type Ballot struct {
SignerPubKeys []bls.SerializedPublicKey `json:"bls-public-keys"`
BlockHeaderHash common.Hash `json:"block-header-hash"`
Signature []byte `json:"bls-signature"`
Height uint64 `json:"block-height"`
ViewID uint64 `json:"view-id"`
}
// MarshalJSON ..
func (b Ballot) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
A string `json:"bls-public-keys"`
B string `json:"block-header-hash"`
C string `json:"bls-signature"`
E uint64 `json:"block-height"`
F uint64 `json:"view-id"`
}{
fmt.Sprint(b.SignerPubKeys),
b.BlockHeaderHash.Hex(),
hex.EncodeToString(b.Signature),
b.Height,
b.ViewID,
})
}
// Round is a round of voting in any FBFT phase
type Round struct {
AggregatedVote *bls_core.Sign
BallotBox map[bls.SerializedPublicKey]*Ballot
}
func (b Ballot) String() string {
data, _ := json.Marshal(b)
return string(data)
}
// NewRound ..
func NewRound() *Round {
return &Round{
AggregatedVote: &bls_core.Sign{},
BallotBox: map[bls.SerializedPublicKey]*Ballot{},
}
}
// PureStakedVote ..
type PureStakedVote struct {
EarningAccount common.Address `json:"earning-account"`
Identity bls.SerializedPublicKey `json:"bls-public-key"`
GroupPercent numeric.Dec `json:"group-percent"`
EffectiveStake numeric.Dec `json:"effective-stake"`
RawStake numeric.Dec `json:"raw-stake"`
}
// AccommodateHarmonyVote ..
type AccommodateHarmonyVote struct {
PureStakedVote
IsHarmonyNode bool `json:"-"`
OverallPercent numeric.Dec `json:"overall-percent"`
}
// String ..
func (v AccommodateHarmonyVote) String() string {
s, _ := json.Marshal(v)
return string(s)
}
type topLevelRegistry struct {
OurVotingPowerTotalPercentage numeric.Dec
TheirVotingPowerTotalPercentage numeric.Dec
TotalEffectiveStake numeric.Dec
HMYSlotCount int64
}
// Roster ..
type Roster struct {
Voters map[bls.SerializedPublicKey]*AccommodateHarmonyVote
topLevelRegistry
ShardID uint32
OrderedSlots []bls.SerializedPublicKey
}
func (r Roster) String() string {
s, _ := json.Marshal(r)
return string(s)
}
// VoteOnSubcomittee ..
type VoteOnSubcomittee struct {
AccommodateHarmonyVote
ShardID uint32
}
// MarshalJSON ..
func (v VoteOnSubcomittee) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
PureStakedVote
EarningAccount string `json:"earning-account"`
OverallPercent numeric.Dec `json:"overall-percent"`
ShardID uint32 `json:"shard-id"`
}{
v.PureStakedVote,
v.EarningAccount.Hex(),
v.OverallPercent,
v.ShardID,
})
}
// AggregateRosters ..
func AggregateRosters(
rosters []*Roster,
) map[common.Address][]VoteOnSubcomittee {
result := map[common.Address][]VoteOnSubcomittee{}
sort.SliceStable(rosters, func(i, j int) bool {
return rosters[i].ShardID < rosters[j].ShardID
})
for _, roster := range rosters {
for _, voteCard := range roster.Voters {
if !voteCard.IsHarmonyNode {
voterID := VoteOnSubcomittee{
AccommodateHarmonyVote: *voteCard,
ShardID: roster.ShardID,
}
result[voteCard.EarningAccount] = append(
result[voteCard.EarningAccount], voterID,
)
}
}
}
return result
}
// Compute creates a new roster based off the shard.SlotList
func Compute(subComm *shard.Committee, epoch *big.Int) (*Roster, error) {
if epoch == nil {
return nil, errors.New("nil epoch for roster compute")
}
roster, staked := NewRoster(subComm.ShardID), subComm.Slots
for i := range staked {
if e := staked[i].EffectiveStake; e != nil {
roster.TotalEffectiveStake = roster.TotalEffectiveStake.Add(*e)
} else {
roster.HMYSlotCount++
}
}
asDecHMYSlotCount := numeric.NewDec(roster.HMYSlotCount)
// TODO Check for duplicate BLS Keys
ourPercentage := numeric.ZeroDec()
theirPercentage := numeric.ZeroDec()
var lastStakedVoter *AccommodateHarmonyVote
harmonyPercent := shard.Schedule.InstanceForEpoch(epoch).HarmonyVotePercent()
externalPercent := shard.Schedule.InstanceForEpoch(epoch).ExternalVotePercent()
for i := range staked {
member := AccommodateHarmonyVote{
PureStakedVote: PureStakedVote{
EarningAccount: staked[i].EcdsaAddress,
Identity: staked[i].BLSPublicKey,
GroupPercent: numeric.ZeroDec(),
EffectiveStake: numeric.ZeroDec(),
RawStake: numeric.ZeroDec(),
},
OverallPercent: numeric.ZeroDec(),
IsHarmonyNode: false,
}
// Real Staker
if e := staked[i].EffectiveStake; e != nil {
member.EffectiveStake = member.EffectiveStake.Add(*e)
member.GroupPercent = e.Quo(roster.TotalEffectiveStake)
member.OverallPercent = member.GroupPercent.Mul(externalPercent)
theirPercentage = theirPercentage.Add(member.OverallPercent)
lastStakedVoter = &member
} else { // Our node
member.IsHarmonyNode = true
member.OverallPercent = harmonyPercent.Quo(asDecHMYSlotCount)
member.GroupPercent = member.OverallPercent.Quo(harmonyPercent)
ourPercentage = ourPercentage.Add(member.OverallPercent)
}
if _, ok := roster.Voters[staked[i].BLSPublicKey]; !ok {
roster.Voters[staked[i].BLSPublicKey] = &member
} else {
utils.Logger().Debug().Str("blsKey", staked[i].BLSPublicKey.Hex()).Msg("Duplicate BLS key found")
}
}
{
// NOTE Enforce voting power sums to one,
// give diff (expect tiny amt) to last staked voter
if diff := numeric.OneDec().Sub(
ourPercentage.Add(theirPercentage),
); !diff.IsZero() && lastStakedVoter != nil {
lastStakedVoter.OverallPercent =
lastStakedVoter.OverallPercent.Add(diff)
theirPercentage = theirPercentage.Add(diff)
}
if lastStakedVoter != nil &&
!ourPercentage.Add(theirPercentage).Equal(numeric.OneDec()) {
return nil, ErrVotingPowerNotEqualOne
}
}
roster.OurVotingPowerTotalPercentage = ourPercentage
roster.TheirVotingPowerTotalPercentage = theirPercentage
for _, slot := range subComm.Slots {
roster.OrderedSlots = append(roster.OrderedSlots, slot.BLSPublicKey)
}
return roster, nil
}
// NewRoster ..
func NewRoster(shardID uint32) *Roster {
m := map[bls.SerializedPublicKey]*AccommodateHarmonyVote{}
return &Roster{
Voters: m,
topLevelRegistry: topLevelRegistry{
OurVotingPowerTotalPercentage: numeric.ZeroDec(),
TheirVotingPowerTotalPercentage: numeric.ZeroDec(),
TotalEffectiveStake: numeric.ZeroDec(),
},
ShardID: shardID,
}
}
// VotePowerByMask return the vote power with the given BLS mask. The result is a number between 0 and 1.
func (r *Roster) VotePowerByMask(mask *bls.Mask) numeric.Dec {
res := numeric.ZeroDec()
for key, index := range mask.PublicsIndex {
if enabled, err := mask.IndexEnabled(index); err != nil || !enabled {
continue
}
voter, ok := r.Voters[key]
if !ok {
continue
}
res = res.Add(voter.OverallPercent)
}
return res
}