-
Notifications
You must be signed in to change notification settings - Fork 0
/
vote.go
305 lines (269 loc) · 7.27 KB
/
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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package core
import (
"bytes"
"encoding/json"
"fmt"
"io"
"sort"
log "github.com/sirupsen/logrus"
"github.com/dnerochain/dnero/common"
"github.com/dnerochain/dnero/common/result"
"github.com/dnerochain/dnero/crypto"
"github.com/dnerochain/dnero/rlp"
)
// Proposal represents a proposal of a new block.
type Proposal struct {
Block *Block `rlp:"nil"`
ProposerID common.Address
Votes *VoteSet `rlp:"nil"`
}
func (p Proposal) String() string {
return fmt.Sprintf("Proposal{block: %v, proposer: %v, votes: %v}", p.Block, p.ProposerID, p.Votes)
}
// CommitCertificate represents a commit made a majority of validators.
type CommitCertificate struct {
Votes *VoteSet `rlp:"nil"`
BlockHash common.Hash
}
// Copy creates a copy of this commit certificate.
func (cc CommitCertificate) Copy() CommitCertificate {
ret := CommitCertificate{
BlockHash: cc.BlockHash,
}
if cc.Votes != nil {
ret.Votes = cc.Votes.Copy()
}
return ret
}
func (cc CommitCertificate) String() string {
return fmt.Sprintf("CC{BlockHash: %v, Votes: %v}", cc.BlockHash.Hex(), cc.Votes)
}
// IsValid checks if a CommitCertificate is valid.
func (cc CommitCertificate) IsValid(validators *ValidatorSet) bool {
if cc.Votes == nil || cc.Votes.IsEmpty() {
return false
}
filtered := cc.Votes.UniqueVoter()
if filtered.Size() != cc.Votes.Size() {
return false
}
if filtered.Size() > validators.Size() {
return false
}
for _, vote := range filtered.Votes() {
if vote.Block != cc.BlockHash {
return false
}
if vote.Validate().IsError() {
return false
}
}
return validators.HasMajority(filtered)
}
// Vote represents a vote on a block by a validaor.
type Vote struct {
Block common.Hash // Hash of the tip as seen by the voter.
Height uint64 // Height of the tip
Epoch uint64 // Voter's current epoch. It doesn't need to equal the epoch in the block above.
ID common.Address // Voter's address.
Signature *crypto.Signature
}
func (v Vote) String() string {
return fmt.Sprintf("Vote{ID: %s, block: %s, Epoch: %v}", v.ID, v.Block.Hex(), v.Epoch)
}
// SignBytes returns raw bytes to be signed.
func (v Vote) SignBytes() common.Bytes {
vv := Vote{
Block: v.Block,
Epoch: v.Epoch,
ID: v.ID,
}
raw, _ := rlp.EncodeToBytes(vv)
return raw
}
// Sign signs the vote using given private key.
func (v *Vote) Sign(priv *crypto.PrivateKey) {
sig, err := priv.Sign(v.SignBytes())
if err != nil {
// Should not happen.
logger.WithFields(log.Fields{"error": err}).Panic("Failed to sign vote")
}
v.SetSignature(sig)
}
// SetSignature sets given signature in vote.
func (v *Vote) SetSignature(sig *crypto.Signature) {
v.Signature = sig
}
// Validate checks the vote is legitimate.
func (v Vote) Validate() result.Result {
if v.Block.IsEmpty() {
return result.Error("Block is not specified")
}
if v.ID.IsEmpty() {
return result.Error("Voter is not specified")
}
if v.Signature == nil || v.Signature.IsEmpty() {
return result.Error("Vote is not signed")
}
if !v.Signature.Verify(v.SignBytes(), v.ID) {
return result.Error("Signature verification failed")
}
return result.OK
}
// Hash calculates vote's hash.
func (v Vote) Hash() common.Hash {
raw, _ := rlp.EncodeToBytes(v)
return crypto.Keccak256Hash(raw)
}
// VoteSet represents a set of votes on a proposal.
type VoteSet struct {
votes map[string]Vote // Voter ID to vote
}
// NewVoteSet creates an instance of VoteSet.
func NewVoteSet() *VoteSet {
return &VoteSet{
votes: make(map[string]Vote),
}
}
// Copy creates a copy of this vote set.
func (s *VoteSet) Copy() *VoteSet {
ret := NewVoteSet()
for _, vote := range s.Votes() {
ret.AddVote(vote)
}
return ret
}
// AddVote adds a vote to vote set. Duplicate votes are ignored.
func (s *VoteSet) AddVote(vote Vote) {
key := fmt.Sprintf("%s:%s:%d", vote.ID, vote.Block, vote.Epoch)
s.votes[key] = vote
}
// Size returns the number of votes in the vote set.
func (s *VoteSet) Size() int {
return len(s.votes)
}
// IsEmpty returns wether the vote set is empty.
func (s *VoteSet) IsEmpty() bool {
return s.Size() == 0
}
// Votes return a slice of votes in the vote set.
func (s *VoteSet) Votes() []Vote {
ret := make([]Vote, 0, len(s.votes))
for _, v := range s.votes {
ret = append(ret, v)
}
sort.Sort(VoteByID(ret))
return ret
}
// Validate checks the vote set is legitimate.
func (s *VoteSet) Validate() result.Result {
for _, vote := range s.votes {
if vote.Validate().IsError() {
return result.Error("Contains invalid vote: %s", vote.String())
}
}
return result.OK
}
func (s *VoteSet) String() string {
if s == nil {
return "nil"
}
return fmt.Sprintf("%v", s.Votes())
}
// MarshalJSON implements json.Marshaler
func (s *VoteSet) MarshalJSON() ([]byte, error) {
return json.Marshal(s.Votes())
}
// UnmarshalJSON implements json.Marshaler
func (s *VoteSet) UnmarshalJSON(b []byte) error {
votes := []Vote{}
if err := json.Unmarshal(b, &votes); err != nil {
return err
}
s.votes = make(map[string]Vote)
for _, v := range votes {
s.AddVote(v)
}
return nil
}
var _ rlp.Encoder = (*VoteSet)(nil)
// EncodeRLP implements RLP Encoder interface.
func (s *VoteSet) EncodeRLP(w io.Writer) error {
if s == nil {
return rlp.Encode(w, []Vote{})
}
return rlp.Encode(w, s.Votes())
}
var _ rlp.Decoder = (*VoteSet)(nil)
// DecodeRLP implements RLP Decoder interface.
func (s *VoteSet) DecodeRLP(stream *rlp.Stream) error {
votes := []Vote{}
err := stream.Decode(&votes)
if err != nil {
return err
}
s.votes = make(map[string]Vote)
for _, v := range votes {
s.AddVote(v)
}
return nil
}
// Merge combines two vote sets.
func (s *VoteSet) Merge(another *VoteSet) *VoteSet {
ret := NewVoteSet()
for _, vote := range s.Votes() {
ret.AddVote(vote)
}
for _, vote := range another.Votes() {
ret.AddVote(vote)
}
return ret
}
// UniqueVoterAndBlock consolidate vote set by removing votes from the same voter to same block
// in older epoches.
func (s *VoteSet) UniqueVoterAndBlock() *VoteSet {
latestVotes := make(map[string]Vote)
for _, vote := range s.votes {
key := fmt.Sprintf("%s:%s", vote.ID, vote.Block)
if prev, ok := latestVotes[key]; ok && prev.Epoch >= vote.Epoch {
continue
}
latestVotes[key] = vote
}
ret := NewVoteSet()
for _, vote := range latestVotes {
ret.AddVote(vote)
}
return ret
}
// UniqueVoter consolidate vote set by removing votes from the same voter in older epoches.
func (s *VoteSet) UniqueVoter() *VoteSet {
latestVotes := make(map[string]Vote)
for _, vote := range s.votes {
key := fmt.Sprintf("%s", vote.ID)
if prev, ok := latestVotes[key]; ok && prev.Epoch >= vote.Epoch {
continue
}
latestVotes[key] = vote
}
ret := NewVoteSet()
for _, vote := range latestVotes {
ret.AddVote(vote)
}
return ret
}
// FilterByValidators removes votes from non-validators.
func (s *VoteSet) FilterByValidators(validators *ValidatorSet) *VoteSet {
ret := NewVoteSet()
for _, vote := range s.votes {
if _, err := validators.GetValidator(vote.ID); err == nil {
ret.AddVote(vote)
}
}
return ret
}
// VoteByID implements sort.Interface for []Vote based on Voter's ID.
type VoteByID []Vote
func (a VoteByID) Len() int { return len(a) }
func (a VoteByID) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a VoteByID) Less(i, j int) bool { return bytes.Compare(a[i].ID.Bytes(), a[j].ID.Bytes()) < 0 }