-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmsg.go
232 lines (179 loc) · 5.31 KB
/
msg.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
// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package simplex
import (
"encoding/asn1"
"encoding/binary"
"fmt"
)
type Message struct {
BlockMessage *BlockMessage
EmptyNotarization *EmptyNotarization
VoteMessage *Vote
EmptyVoteMessage *EmptyVote
Notarization *Notarization
Finalization *Finalization
FinalizationCertificate *FinalizationCertificate
ReplicationResponse *ReplicationResponse
ReplicationRequest *ReplicationRequest
}
type ToBeSignedEmptyVote struct {
ProtocolMetadata
}
func (v *ToBeSignedEmptyVote) Bytes() []byte {
buff := make([]byte, protocolMetadataLen)
var pos int
buff[pos] = v.Version
pos++
binary.BigEndian.PutUint64(buff[pos:], v.Epoch)
pos += metadataEpochLen
binary.BigEndian.PutUint64(buff[pos:], v.Round)
pos += metadataRoundLen
binary.BigEndian.PutUint64(buff[pos:], v.Seq)
pos += metadataSeqLen
copy(buff[pos:], v.Prev[:])
return buff
}
func (v *ToBeSignedEmptyVote) FromBytes(buff []byte) error {
if len(buff) != protocolMetadataLen {
return fmt.Errorf("invalid buffer length %d, expected %d", len(buff), metadataLen)
}
var pos int
v.Version = buff[pos]
pos++
v.Epoch = binary.BigEndian.Uint64(buff[pos:])
pos += metadataEpochLen
v.Round = binary.BigEndian.Uint64(buff[pos:])
pos += metadataRoundLen
v.Seq = binary.BigEndian.Uint64(buff[pos:])
pos += metadataSeqLen
copy(v.Prev[:], buff[pos:pos+metadataPrevLen])
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)
}
type Vote struct {
Vote ToBeSignedVote
Signature Signature
}
type EmptyVote struct {
Vote ToBeSignedEmptyVote
Signature Signature
}
type Finalization struct {
Finalization ToBeSignedFinalization
Signature Signature
}
type FinalizationCertificate struct {
Finalization ToBeSignedFinalization
QC QuorumCertificate
}
func (fc *FinalizationCertificate) Verify() error {
context := "ToBeSignedFinalization"
return verifyContextQC(fc.QC, fc.Finalization.Bytes(), context)
}
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 EmptyNotarization struct {
Vote ToBeSignedEmptyVote
QC QuorumCertificate
}
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 {
FinalizationCertificateRequest *FinalizationCertificateRequest
}
type ReplicationResponse struct {
FinalizationCertificateResponse *FinalizationCertificateResponse
}
// request a finalization certificate for the given sequence number
type FinalizationCertificateRequest struct {
Sequences []uint64
}
type FinalizedBlock struct {
Block Block
FCert FinalizationCertificate
}
type FinalizationCertificateResponse struct {
Data []FinalizedBlock
}