-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayload.go
More file actions
346 lines (298 loc) · 8.23 KB
/
payload.go
File metadata and controls
346 lines (298 loc) · 8.23 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
package hotstuff2
import (
"encoding/binary"
"fmt"
)
// ConsensusMessage represents a message in the HotStuff2 protocol.
//
// Maps to TLA+ Message types:
// - PROPOSE: Leader broadcasts block with justification QC
// - VOTE: Replica votes for a proposal
// - NEWVIEW: Replica sends view change message with highQC
type ConsensusMessage[H Hash] struct {
msgType MessageType
view uint32
validatorIndex uint16
// PROPOSE fields
block Block[H]
justifyQC *QC[H]
// VOTE fields
vote *Vote[H]
// NEWVIEW fields
highQC *QC[H]
}
// NewProposeMessage creates a PROPOSE message.
func NewProposeMessage[H Hash](
view uint32,
validatorIndex uint16,
block Block[H],
justifyQC *QC[H],
) *ConsensusMessage[H] {
return &ConsensusMessage[H]{
msgType: MessageProposal,
view: view,
validatorIndex: validatorIndex,
block: block,
justifyQC: justifyQC,
}
}
// NewVoteMessage creates a VOTE message.
func NewVoteMessage[H Hash](
view uint32,
validatorIndex uint16,
vote *Vote[H],
) *ConsensusMessage[H] {
return &ConsensusMessage[H]{
msgType: MessageVote,
view: view,
validatorIndex: validatorIndex,
vote: vote,
}
}
// NewNewViewMessage creates a NEWVIEW message.
func NewNewViewMessage[H Hash](
view uint32,
validatorIndex uint16,
highQC *QC[H],
) *ConsensusMessage[H] {
return &ConsensusMessage[H]{
msgType: MessageNewView,
view: view,
validatorIndex: validatorIndex,
highQC: highQC,
}
}
// Type returns the message type.
func (m *ConsensusMessage[H]) Type() MessageType {
return m.msgType
}
// View returns the view number.
func (m *ConsensusMessage[H]) View() uint32 {
return m.view
}
// ValidatorIndex returns the sender's validator index.
func (m *ConsensusMessage[H]) ValidatorIndex() uint16 {
return m.validatorIndex
}
// Block returns the proposed block (PROPOSE only).
func (m *ConsensusMessage[H]) Block() Block[H] {
return m.block
}
// JustifyQC returns the justification QC (PROPOSE only).
func (m *ConsensusMessage[H]) JustifyQC() *QC[H] {
return m.justifyQC
}
// Vote returns the vote (VOTE only).
func (m *ConsensusMessage[H]) Vote() *Vote[H] {
return m.vote
}
// HighQC returns the highest QC (NEWVIEW only).
func (m *ConsensusMessage[H]) HighQC() *QC[H] {
return m.highQC
}
// Bytes serializes the message to bytes.
// Format: [type:1][view:4][validatorIndex:2][payload...]
func (m *ConsensusMessage[H]) Bytes() []byte {
// Header: type (1) + view (4) + validatorIndex (2) = 7 bytes
header := make([]byte, 7)
header[0] = byte(m.msgType)
binary.BigEndian.PutUint32(header[1:5], m.view)
binary.BigEndian.PutUint16(header[5:7], m.validatorIndex)
var payload []byte
switch m.msgType {
case MessageProposal:
payload = m.serializePropose()
case MessageVote:
payload = m.serializeVote()
case MessageNewView:
payload = m.serializeNewView()
}
return append(header, payload...)
}
func (m *ConsensusMessage[H]) serializePropose() []byte {
// Format: [blockLen:4][block][hasQC:1][qc?]
blockBytes := m.block.Bytes()
blockLen := len(blockBytes)
var qcBytes []byte
hasQC := byte(0)
if m.justifyQC != nil {
hasQC = 1
qcBytes = m.justifyQC.Bytes()
}
result := make([]byte, 4+blockLen+1+len(qcBytes))
binary.BigEndian.PutUint32(result[0:4], uint32(blockLen))
copy(result[4:], blockBytes)
result[4+blockLen] = hasQC
if hasQC == 1 {
copy(result[4+blockLen+1:], qcBytes)
}
return result
}
func (m *ConsensusMessage[H]) serializeVote() []byte {
return m.vote.Bytes()
}
func (m *ConsensusMessage[H]) serializeNewView() []byte {
// Format: [hasQC:1][qc?]
var qcBytes []byte
hasQC := byte(0)
if m.highQC != nil {
hasQC = 1
qcBytes = m.highQC.Bytes()
}
result := make([]byte, 1+len(qcBytes))
result[0] = hasQC
if hasQC == 1 {
copy(result[1:], qcBytes)
}
return result
}
// Hash returns the hash of this message (implementation depends on block hash).
func (m *ConsensusMessage[H]) Hash() H {
// For now, return the block hash for PROPOSE, vote nodeHash for VOTE
// This is a simplified implementation
var zero H
switch m.msgType {
case MessageProposal:
if m.block != nil {
return m.block.Hash()
}
case MessageVote:
if m.vote != nil {
return m.vote.NodeHash()
}
}
return zero
}
// MessageFromBytes deserializes a ConsensusMessage from bytes.
func MessageFromBytes[H Hash](
data []byte,
hashFromBytes func([]byte) (H, error),
blockFromBytes func([]byte) (Block[H], error),
) (*ConsensusMessage[H], error) {
if len(data) < 7 {
return nil, fmt.Errorf("data too short for message header")
}
msgType := MessageType(data[0])
view := binary.BigEndian.Uint32(data[1:5])
validatorIndex := binary.BigEndian.Uint16(data[5:7])
payload := data[7:]
switch msgType {
case MessageProposal:
return deserializePropose(view, validatorIndex, payload, hashFromBytes, blockFromBytes)
case MessageVote:
return deserializeVote(view, validatorIndex, payload, hashFromBytes)
case MessageNewView:
return deserializeNewView(view, validatorIndex, payload, hashFromBytes)
default:
return nil, fmt.Errorf("unknown message type: %d", msgType)
}
}
func deserializePropose[H Hash](
view uint32,
validatorIndex uint16,
payload []byte,
hashFromBytes func([]byte) (H, error),
blockFromBytes func([]byte) (Block[H], error),
) (*ConsensusMessage[H], error) {
if len(payload) < 5 {
return nil, fmt.Errorf("payload too short for PROPOSE")
}
blockLen := binary.BigEndian.Uint32(payload[0:4])
if len(payload) < int(4+blockLen+1) {
return nil, fmt.Errorf("payload too short for block")
}
blockBytes := payload[4 : 4+blockLen]
block, err := blockFromBytes(blockBytes)
if err != nil {
return nil, fmt.Errorf("failed to deserialize block: %w", err)
}
hasQC := payload[4+blockLen]
var justifyQC *QC[H]
if hasQC == 1 {
qcBytes := payload[4+blockLen+1:]
qc, err := QCFromBytes(qcBytes, hashFromBytes)
if err != nil {
return nil, fmt.Errorf("failed to deserialize QC: %w", err)
}
justifyQC = qc
}
return &ConsensusMessage[H]{
msgType: MessageProposal,
view: view,
validatorIndex: validatorIndex,
block: block,
justifyQC: justifyQC,
}, nil
}
func deserializeVote[H Hash](
view uint32,
validatorIndex uint16,
payload []byte,
hashFromBytes func([]byte) (H, error),
) (*ConsensusMessage[H], error) {
vote, err := VoteFromBytes(payload, hashFromBytes)
if err != nil {
return nil, fmt.Errorf("failed to deserialize vote: %w", err)
}
return &ConsensusMessage[H]{
msgType: MessageVote,
view: view,
validatorIndex: validatorIndex,
vote: vote,
}, nil
}
func deserializeNewView[H Hash](
view uint32,
validatorIndex uint16,
payload []byte,
hashFromBytes func([]byte) (H, error),
) (*ConsensusMessage[H], error) {
if len(payload) < 1 {
return nil, fmt.Errorf("payload too short for NEWVIEW")
}
hasQC := payload[0]
var highQC *QC[H]
if hasQC == 1 {
if len(payload) < 2 {
return nil, fmt.Errorf("payload too short for QC")
}
qcBytes := payload[1:]
qc, err := QCFromBytes(qcBytes, hashFromBytes)
if err != nil {
return nil, fmt.Errorf("failed to deserialize QC: %w", err)
}
highQC = qc
}
return &ConsensusMessage[H]{
msgType: MessageNewView,
view: view,
validatorIndex: validatorIndex,
highQC: highQC,
}, nil
}
// MessageCodec provides convenient encoding/decoding of consensus messages.
// Create once and reuse for all message operations.
type MessageCodec[H Hash] struct {
// HashFromBytes deserializes a hash from bytes.
HashFromBytes func([]byte) (H, error)
// BlockFromBytes deserializes a block from bytes.
BlockFromBytes func([]byte) (Block[H], error)
}
// NewMessageCodec creates a new message codec with the given deserializers.
func NewMessageCodec[H Hash](
hashFromBytes func([]byte) (H, error),
blockFromBytes func([]byte) (Block[H], error),
) *MessageCodec[H] {
return &MessageCodec[H]{
HashFromBytes: hashFromBytes,
BlockFromBytes: blockFromBytes,
}
}
// Decode deserializes a consensus message from bytes.
func (c *MessageCodec[H]) Decode(data []byte) (*ConsensusMessage[H], error) {
return MessageFromBytes(data, c.HashFromBytes, c.BlockFromBytes)
}
// Encode serializes a consensus message to bytes.
func (c *MessageCodec[H]) Encode(msg *ConsensusMessage[H]) []byte {
return msg.Bytes()
}