-
Notifications
You must be signed in to change notification settings - Fork 180
/
vote.go
346 lines (302 loc) · 8.5 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package types
import (
"bytes"
"errors"
"fmt"
"time"
"github.com/tendermint/go-amino"
"github.com/okex/exchain/libs/tendermint/crypto"
tmbytes "github.com/okex/exchain/libs/tendermint/libs/bytes"
tmproto "github.com/okex/exchain/libs/tendermint/proto/types"
)
const (
// MaxVoteBytes is a maximum vote size (including amino overhead).
MaxVoteBytes int64 = 223
nilVoteStr string = "nil-Vote"
)
var (
ErrVoteUnexpectedStep = errors.New("unexpected step")
ErrVoteInvalidValidatorIndex = errors.New("invalid validator index")
ErrVoteInvalidValidatorAddress = errors.New("invalid validator address")
ErrVoteInvalidSignature = errors.New("invalid signature")
ErrVoteInvalidBlockHash = errors.New("invalid block hash")
ErrVoteNonDeterministicSignature = errors.New("non-deterministic signature")
ErrVoteNil = errors.New("nil vote")
)
type ErrVoteConflictingVotes struct {
*DuplicateVoteEvidence
}
func (err *ErrVoteConflictingVotes) Error() string {
return fmt.Sprintf("Conflicting votes from validator %v", err.PubKey.Address())
}
func NewConflictingVoteError(val *Validator, vote1, vote2 *Vote) *ErrVoteConflictingVotes {
return &ErrVoteConflictingVotes{
NewDuplicateVoteEvidence(val.PubKey, vote1, vote2),
}
}
// Address is hex bytes.
type Address = crypto.Address
// Vote represents a prevote, precommit, or commit vote from validators for
// consensus.
type Vote struct {
Type SignedMsgType `json:"type"`
Height int64 `json:"height"`
Round int `json:"round"`
BlockID BlockID `json:"block_id"` // zero if vote is nil.
Timestamp time.Time `json:"timestamp"`
ValidatorAddress Address `json:"validator_address"`
ValidatorIndex int `json:"validator_index"`
Signature []byte `json:"signature"`
}
func (vote Vote) AminoSize(cdc *amino.Codec) int {
var size = 0
if vote.Type != 0 {
size += 1 + amino.UvarintSize(uint64(vote.Type))
}
if vote.Height != 0 {
size += 1 + amino.UvarintSize(uint64(vote.Height))
}
if vote.Round != 0 {
size += 1 + amino.UvarintSize(uint64(vote.Round))
}
blockIDSize := vote.BlockID.AminoSize(cdc)
if blockIDSize != 0 {
size += 1 + amino.UvarintSize(uint64(blockIDSize)) + blockIDSize
}
timestampSize := amino.TimeSize(vote.Timestamp)
if timestampSize != 0 {
size += 1 + amino.UvarintSize(uint64(timestampSize)) + timestampSize
}
if len(vote.ValidatorAddress) != 0 {
size += 1 + amino.ByteSliceSize(vote.ValidatorAddress)
}
if vote.ValidatorIndex != 0 {
size += 1 + amino.UvarintSize(uint64(vote.ValidatorIndex))
}
if len(vote.Signature) != 0 {
size += 1 + amino.ByteSliceSize(vote.Signature)
}
return size
}
func (vote *Vote) UnmarshalFromAmino(cdc *amino.Codec, data []byte) error {
var dataLen uint64 = 0
var subData []byte
var timestampUpdated bool
for {
data = data[dataLen:]
if len(data) == 0 {
break
}
pos, pbType, err := amino.ParseProtoPosAndTypeMustOneByte(data[0])
if err != nil {
return err
}
data = data[1:]
if pbType == amino.Typ3_ByteLength {
var n int
dataLen, n, err = amino.DecodeUvarint(data)
if err != nil {
return err
}
data = data[n:]
if len(data) < int(dataLen) {
return fmt.Errorf("invalid data len")
}
subData = data[:dataLen]
}
switch pos {
case 1:
u64, n, err := amino.DecodeUvarint(data)
if err != nil {
return err
}
vote.Type = SignedMsgType(u64)
dataLen = uint64(n)
case 2:
u64, n, err := amino.DecodeUvarint(data)
if err != nil {
return err
}
vote.Height = int64(u64)
dataLen = uint64(n)
case 3:
u64, n, err := amino.DecodeUvarint(data)
if err != nil {
return err
}
vote.Round = int(u64)
dataLen = uint64(n)
case 4:
err = vote.BlockID.UnmarshalFromAmino(cdc, subData)
if err != nil {
return err
}
case 5:
vote.Timestamp, _, err = amino.DecodeTime(subData)
if err != nil {
return err
}
timestampUpdated = true
case 6:
vote.ValidatorAddress = make([]byte, len(subData))
copy(vote.ValidatorAddress, subData)
case 7:
u64, n, err := amino.DecodeUvarint(data)
if err != nil {
return err
}
vote.ValidatorIndex = int(u64)
dataLen = uint64(n)
case 8:
vote.Signature = make([]byte, len(subData))
copy(vote.Signature, subData)
default:
return fmt.Errorf("unexpect feild num %d", pos)
}
}
if !timestampUpdated {
vote.Timestamp = amino.ZeroTime
}
return nil
}
// CommitSig converts the Vote to a CommitSig.
func (vote *Vote) CommitSig() CommitSig {
if vote == nil {
return NewCommitSigAbsent()
}
var blockIDFlag BlockIDFlag
switch {
case vote.BlockID.IsComplete():
blockIDFlag = BlockIDFlagCommit
case vote.BlockID.IsZero():
blockIDFlag = BlockIDFlagNil
default:
panic(fmt.Sprintf("Invalid vote %v - expected BlockID to be either empty or complete", vote))
}
return CommitSig{
BlockIDFlag: blockIDFlag,
ValidatorAddress: vote.ValidatorAddress,
Timestamp: vote.Timestamp,
Signature: vote.Signature,
}
}
func (vote *Vote) SignBytes(chainID string) []byte {
bz, err := cdc.MarshalBinaryLengthPrefixed(CanonicalizeVote(chainID, vote))
if err != nil {
panic(err)
}
return bz
}
func (vote *Vote) Copy() *Vote {
voteCopy := *vote
return &voteCopy
}
func (vote *Vote) String() string {
if vote == nil {
return nilVoteStr
}
var typeString string
switch vote.Type {
case PrevoteType:
typeString = "Prevote"
case PrecommitType:
typeString = "Precommit"
default:
panic("Unknown vote type")
}
return fmt.Sprintf("Vote{%v:%X %v/%02d/%v(%v) %X %X @ %s}",
vote.ValidatorIndex,
tmbytes.Fingerprint(vote.ValidatorAddress),
vote.Height,
vote.Round,
vote.Type,
typeString,
tmbytes.Fingerprint(vote.BlockID.Hash),
tmbytes.Fingerprint(vote.Signature),
CanonicalTime(vote.Timestamp),
)
}
func (vote *Vote) Verify(chainID string, pubKey crypto.PubKey) error {
if !bytes.Equal(pubKey.Address(), vote.ValidatorAddress) {
return ErrVoteInvalidValidatorAddress
}
if !pubKey.VerifyBytes(vote.SignBytes(chainID), vote.Signature) {
return ErrVoteInvalidSignature
}
return nil
}
// ValidateBasic performs basic validation.
func (vote *Vote) ValidateBasic() error {
if !IsVoteTypeValid(vote.Type) {
return errors.New("invalid Type")
}
if vote.Height < 0 {
return errors.New("negative Height")
}
if vote.Round < 0 {
return errors.New("negative Round")
}
// NOTE: Timestamp validation is subtle and handled elsewhere.
if err := vote.BlockID.ValidateBasic(); err != nil {
return fmt.Errorf("wrong BlockID: %v", err)
}
// BlockID.ValidateBasic would not err if we for instance have an empty hash but a
// non-empty PartsSetHeader:
if !vote.BlockID.IsZero() && !vote.BlockID.IsComplete() {
return fmt.Errorf("blockID must be either empty or complete, got: %v", vote.BlockID)
}
if len(vote.ValidatorAddress) != crypto.AddressSize {
return fmt.Errorf("expected ValidatorAddress size to be %d bytes, got %d bytes",
crypto.AddressSize,
len(vote.ValidatorAddress),
)
}
if vote.ValidatorIndex < 0 {
return errors.New("negative ValidatorIndex")
}
if len(vote.Signature) == 0 {
return errors.New("signature is missing")
}
if len(vote.Signature) > MaxSignatureSize {
return fmt.Errorf("signature is too big (max: %d)", MaxSignatureSize)
}
return nil
}
// ToProto converts the handwritten type to proto generated type
// return type, nil if everything converts safely, otherwise nil, error
func (vote *Vote) ToProto() *tmproto.Vote {
if vote == nil {
return nil
}
return &tmproto.Vote{
Type: tmproto.SignedMsgType(vote.Type),
Height: vote.Height,
Round: int64(vote.Round),
BlockID: vote.BlockID.ToProto(),
Timestamp: vote.Timestamp,
ValidatorAddress: vote.ValidatorAddress,
ValidatorIndex: int64(vote.ValidatorIndex),
Signature: vote.Signature,
}
}
//FromProto converts a proto generetad type to a handwritten type
// return type, nil if everything converts safely, otherwise nil, error
func VoteFromProto(pv *tmproto.Vote) (*Vote, error) {
if pv == nil {
return nil, errors.New("nil vote")
}
blockID, err := BlockIDFromProto(&pv.BlockID)
if err != nil {
return nil, err
}
vote := new(Vote)
vote.Type = SignedMsgType(pv.Type)
vote.Height = pv.Height
vote.Round = int(pv.Round)
vote.BlockID = *blockID
vote.Timestamp = pv.Timestamp
vote.ValidatorAddress = pv.ValidatorAddress
vote.ValidatorIndex = int(pv.ValidatorIndex)
vote.Signature = pv.Signature
return vote, vote.ValidateBasic()
}