-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmsg.go
More file actions
367 lines (296 loc) · 9.07 KB
/
msg.go
File metadata and controls
367 lines (296 loc) · 9.07 KB
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package simplex
import (
"bytes"
"encoding/asn1"
"encoding/binary"
"fmt"
)
type Message struct {
BlockMessage *BlockMessage
VerifiedBlockMessage *VerifiedBlockMessage
EmptyNotarization *EmptyNotarization
VoteMessage *Vote
EmptyVoteMessage *EmptyVote
Notarization *Notarization
FinalizeVote *FinalizeVote
Finalization *Finalization
ReplicationResponse *ReplicationResponse
VerifiedReplicationResponse *VerifiedReplicationResponse
ReplicationRequest *ReplicationRequest
BlockDigestRequest *BlockDigestRequest
}
type EmptyVoteMetadata struct {
Round uint64
Epoch uint64
}
type ToBeSignedEmptyVote struct {
EmptyVoteMetadata
}
func (v *ToBeSignedEmptyVote) Bytes() []byte {
bytes := make([]byte, 1+8+8) // Version + Epoch + Round
binary.BigEndian.PutUint64(bytes[1:9], v.EmptyVoteMetadata.Epoch)
binary.BigEndian.PutUint64(bytes[9:17], v.EmptyVoteMetadata.Round)
return bytes
}
func (v *ToBeSignedEmptyVote) FromBytes(buff []byte) error {
if len(buff) != 17 {
return fmt.Errorf("invalid buffer length, expected 17, got %d", len(buff))
}
epoch := binary.BigEndian.Uint64(buff[1:9])
round := binary.BigEndian.Uint64(buff[9:17])
v.EmptyVoteMetadata = EmptyVoteMetadata{
Round: round,
Epoch: epoch,
}
return nil
}
func (v *ToBeSignedEmptyVote) Sign(signer Signer) ([]byte, error) {
context := "ToBeSignedEmptyVote"
msg := v.Bytes()
return signContext(signer, msg, context)
}
func (v *ToBeSignedEmptyVote) Verify(signature []byte, verifier SignatureVerifier, signers NodeID) error {
context := "ToBeSignedEmptyVote"
msg := v.Bytes()
return verifyContext(signature, verifier, msg, context, signers)
}
type ToBeSignedVote struct {
BlockHeader
}
func (v *ToBeSignedVote) Sign(signer Signer) ([]byte, error) {
context := "ToBeSignedVote"
msg := v.Bytes()
return signContext(signer, msg, context)
}
func (v *ToBeSignedVote) Verify(signature []byte, verifier SignatureVerifier, signers NodeID) error {
context := "ToBeSignedVote"
msg := v.Bytes()
return verifyContext(signature, verifier, msg, context, signers)
}
type ToBeSignedFinalization struct {
BlockHeader
}
func (f *ToBeSignedFinalization) Sign(signer Signer) ([]byte, error) {
context := "ToBeSignedFinalization"
msg := f.Bytes()
return signContext(signer, msg, context)
}
func (f *ToBeSignedFinalization) Verify(signature []byte, verifier SignatureVerifier, signers NodeID) error {
context := "ToBeSignedFinalization"
msg := f.Bytes()
return verifyContext(signature, verifier, msg, context, signers)
}
func signContext(signer Signer, msg []byte, context string) ([]byte, error) {
sm := SignedMessage{Payload: msg, Context: context}
toBeSigned, err := asn1.Marshal(sm)
if err != nil {
return nil, err
}
return signer.Sign(toBeSigned)
}
func verifyContext(signature []byte, verifier SignatureVerifier, msg []byte, context string, signers NodeID) error {
sm := SignedMessage{Payload: msg, Context: context}
toBeSigned, err := asn1.Marshal(sm)
if err != nil {
return err
}
return verifier.Verify(toBeSigned, signature, signers)
}
func verifyContextQC(qc QuorumCertificate, msg []byte, context string) error {
sm := SignedMessage{Payload: msg, Context: context}
toBeSigned, err := asn1.Marshal(sm)
if err != nil {
return err
}
return qc.Verify(toBeSigned)
}
// Vote represents a signed vote for a block.
type Vote struct {
Vote ToBeSignedVote
Signature Signature
}
func (v *Vote) Signer() NodeID {
return v.Signature.Signer
}
// EmptyVote represents a signed vote for an empty block.
type EmptyVote struct {
Vote ToBeSignedEmptyVote
Signature Signature
}
func (v *EmptyVote) Signer() NodeID {
return v.Signature.Signer
}
// FinalizeVote represents a vote to finalize a block.
type FinalizeVote struct {
Finalization ToBeSignedFinalization
Signature Signature
}
func (v *FinalizeVote) Signer() NodeID {
return v.Signature.Signer
}
// Finalization represents a block that has reached quorum on block. This
// means that block can be included in the chain and finalized.
type Finalization struct {
Finalization ToBeSignedFinalization
QC QuorumCertificate
}
func (f *Finalization) Verify() error {
context := "ToBeSignedFinalization"
return verifyContextQC(f.QC, f.Finalization.Bytes(), context)
}
// Notarization represents a block that has reached a quorum of votes.
type Notarization struct {
Vote ToBeSignedVote
QC QuorumCertificate
}
func (n *Notarization) Verify() error {
context := "ToBeSignedVote"
return verifyContextQC(n.QC, n.Vote.Bytes(), context)
}
type BlockMessage struct {
Block Block
Vote Vote
}
type VerifiedBlockMessage struct {
VerifiedBlock VerifiedBlock
Vote Vote
}
type EmptyNotarization struct {
Vote ToBeSignedEmptyVote
QC QuorumCertificate
}
func (en *EmptyNotarization) Verify() error {
context := "ToBeSignedEmptyVote"
return verifyContextQC(en.QC, en.Vote.Bytes(), context)
}
type SignedMessage struct {
Payload []byte
Context string
}
// QuorumCertificate is equivalent to a collection of signatures from a quorum of nodes,
type QuorumCertificate interface {
// Signers returns who participated in creating this QuorumCertificate.
Signers() []NodeID
// Verify checks whether the nodes participated in creating this QuorumCertificate,
// signed the given message.
Verify(msg []byte) error
// Bytes returns a raw representation of the given QuorumCertificate.
Bytes() []byte
}
type ReplicationRequest struct {
Seqs []uint64 // sequences we are requesting
Rounds []uint64 // rounds we are requesting
LatestRound uint64 // latest round that we are aware of
LatestFinalizedSeq uint64 // latest finalized sequence that we are aware of
}
type ReplicationResponse struct {
Data []QuorumRound
LatestRound *QuorumRound
LatestSeq *QuorumRound
}
type VerifiedReplicationResponse struct {
Data []VerifiedQuorumRound
LatestRound *VerifiedQuorumRound
LatestFinalizedSeq *VerifiedQuorumRound
}
// QuorumRound represents a round that has achieved quorum on either
// (empty notarization), (block & notarization), or (block, finalization)
type QuorumRound struct {
Block Block
Notarization *Notarization
Finalization *Finalization
EmptyNotarization *EmptyNotarization
}
// isWellFormed returns an error if the QuorumRound has either
// (block, notarization) or (block, finalization) or
// (empty notarization)
func (q *QuorumRound) IsWellFormed() error {
if q.Block == nil && q.EmptyNotarization == nil {
return fmt.Errorf("malformed QuorumRound, empty block and notarization fields")
}
if q.Block != nil && (q.Notarization == nil && q.Finalization == nil) {
return fmt.Errorf("malformed QuorumRound, block but no notarization or finalization")
}
return nil
}
func (q *QuorumRound) GetRound() uint64 {
if q.EmptyNotarization != nil {
return q.EmptyNotarization.Vote.Round
}
if q.Block != nil {
return q.Block.BlockHeader().Round
}
return 0
}
func (q *QuorumRound) GetSequence() uint64 {
if q.Block != nil {
return q.Block.BlockHeader().Seq
}
return 0
}
func (q *QuorumRound) VerifyQCConsistentWithBlock() error {
if err := q.IsWellFormed(); err != nil {
return err
}
if q.Block == nil {
return nil
}
// if an empty notarization is included, ensure the round is equal to the block round
if q.EmptyNotarization != nil && q.EmptyNotarization.Vote.Round != q.Block.BlockHeader().Round {
return fmt.Errorf("empty round does not match block round")
}
// ensure the finalization or notarization we get relates to the block
blockDigest := q.Block.BlockHeader().Digest
if q.Finalization != nil {
if !bytes.Equal(blockDigest[:], q.Finalization.Finalization.Digest[:]) {
return fmt.Errorf("finalization does not match the block")
}
}
if q.Notarization != nil {
if !bytes.Equal(blockDigest[:], q.Notarization.Vote.Digest[:]) {
return fmt.Errorf("notarization does not match the block")
}
}
return nil
}
// String returns a string representation of the QuorumRound.
// It is meant as a debugging aid for logs.
func (q *QuorumRound) String() string {
if q != nil {
err := q.IsWellFormed()
if err != nil {
return fmt.Sprintf("QuorumRound{Error: %s}", err)
} else {
return fmt.Sprintf("QuorumRound{Round: %d, Seq: %d, Finalized: %t}", q.GetRound(), q.GetSequence(), q.Finalization != nil)
}
}
return "QuorumRound{nil}"
}
type VerifiedQuorumRound struct {
VerifiedBlock VerifiedBlock
Notarization *Notarization
Finalization *Finalization
EmptyNotarization *EmptyNotarization
}
func (q *VerifiedQuorumRound) GetRound() uint64 {
if q.EmptyNotarization != nil {
return q.EmptyNotarization.Vote.Round
}
if q.VerifiedBlock != nil {
return q.VerifiedBlock.BlockHeader().Round
}
return 0
}
type VerifiedFinalizedBlock struct {
VerifiedBlock VerifiedBlock
Finalization Finalization
}
type verifiableMessage interface {
Verify() error
}
type BlockDigestRequest struct {
Seq uint64
Digest Digest
}