-
Notifications
You must be signed in to change notification settings - Fork 2
/
one-node-one-vote.go
137 lines (114 loc) · 3.91 KB
/
one-node-one-vote.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
package quorum
import (
"encoding/json"
"math/big"
"github.com/pkg/errors"
bls_core "github.com/PositionExchange/bls/ffi/go/bls"
"github.com/PositionExchange/posichain/crypto/bls"
"github.com/PositionExchange/posichain/consensus/votepower"
"github.com/ethereum/go-ethereum/common"
bls_cosi "github.com/PositionExchange/posichain/crypto/bls"
"github.com/PositionExchange/posichain/internal/utils"
"github.com/PositionExchange/posichain/numeric"
"github.com/PositionExchange/posichain/shard"
)
type uniformVoteWeight struct {
SignatureReader
lastPowerSignersCountCache map[Phase]int64
lastParticipantsCount int64
}
// Policy ..
func (v *uniformVoteWeight) Policy() Policy {
return SuperMajorityVote
}
// AddNewVote ..
func (v *uniformVoteWeight) AddNewVote(
p Phase, pubKeys []*bls_cosi.PublicKeyWrapper,
sig *bls_core.Sign, headerHash common.Hash,
height, viewID uint64) (*votepower.Ballot, error) {
pubKeysBytes := make([]bls.SerializedPublicKey, len(pubKeys))
for i, pubKey := range pubKeys {
pubKeysBytes[i] = pubKey.Bytes
}
return v.submitVote(p, pubKeysBytes, sig, headerHash, height, viewID)
}
// IsQuorumAchieved ..
func (v *uniformVoteWeight) IsQuorumAchieved(p Phase) bool {
r := v.SignersCount(p) >= v.TwoThirdsSignersCount()
utils.Logger().Info().Str("phase", p.String()).
Int64("signers-count", v.SignersCount(p)).
Int64("threshold", v.TwoThirdsSignersCount()).
Int64("participants", v.ParticipantsCount()).
Msg("Quorum details")
return r
}
// IsQuorumAchivedByMask ..
func (v *uniformVoteWeight) IsQuorumAchievedByMask(mask *bls_cosi.Mask) bool {
if mask == nil {
return false
}
threshold := v.TwoThirdsSignersCount()
currentTotalPower := utils.CountOneBits(mask.Bitmap)
if currentTotalPower < threshold {
const msg = "[IsQuorumAchievedByMask] Not enough voting power: need %+v, have %+v"
utils.Logger().Warn().Msgf(msg, threshold, currentTotalPower)
return false
}
const msg = "[IsQuorumAchievedByMask] have enough voting power: need %+v, have %+v"
utils.Logger().Debug().
Msgf(msg, threshold, currentTotalPower)
return true
}
// QuorumThreshold ..
func (v *uniformVoteWeight) QuorumThreshold() numeric.Dec {
return numeric.NewDec(v.TwoThirdsSignersCount())
}
// IsAllSigsCollected ..
func (v *uniformVoteWeight) IsAllSigsCollected() bool {
return v.SignersCount(Commit) == v.ParticipantsCount()
}
func (v *uniformVoteWeight) SetVoters(
subCommittee *shard.Committee, epoch *big.Int,
) (*TallyResult, error) {
// NO-OP do not add anything here
return nil, nil
}
func (v *uniformVoteWeight) String() string {
s, _ := json.Marshal(v)
return string(s)
}
func (v *uniformVoteWeight) MarshalJSON() ([]byte, error) {
type t struct {
Policy string `json:"policy"`
Count int `json:"count"`
Participants []string `json:"committee-members"`
}
keysDump := v.Participants()
keys := make([]string, len(keysDump))
for i := range keysDump {
keys[i] = keysDump[i].Bytes.Hex()
}
return json.Marshal(t{v.Policy().String(), len(keys), keys})
}
func (v *uniformVoteWeight) ResetPrepareAndCommitVotes() {
v.lastPowerSignersCountCache[Prepare] = v.SignersCount(Prepare)
v.lastPowerSignersCountCache[Commit] = v.SignersCount(Commit)
v.lastParticipantsCount = v.ParticipantsCount()
v.reset([]Phase{Prepare, Commit})
}
func (v *uniformVoteWeight) ResetViewChangeVotes() {
v.lastPowerSignersCountCache[ViewChange] = v.SignersCount(ViewChange)
v.lastParticipantsCount = v.ParticipantsCount()
v.reset([]Phase{ViewChange})
}
func (v *uniformVoteWeight) CurrentTotalPower(p Phase) (*numeric.Dec, error) {
if v.lastParticipantsCount == 0 {
return nil, errors.New("uniformVoteWeight not cache last participants count")
}
if lastPowerSignersCount, ok := v.lastPowerSignersCountCache[p]; ok {
power := numeric.NewDec(lastPowerSignersCount).Quo(numeric.NewDec(v.lastParticipantsCount))
return &power, nil
} else {
return nil, errors.New("uniformVoteWeight not cache this phase")
}
}