-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayload_test.go
More file actions
476 lines (387 loc) · 12.5 KB
/
Copy pathpayload_test.go
File metadata and controls
476 lines (387 loc) · 12.5 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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
package hotstuff2
import (
"testing"
"github.com/edgedlt/hotstuff2/internal/crypto"
)
// TestProposeMessageCreation tests PROPOSE message creation.
func TestProposeMessageCreation(t *testing.T) {
validators, keys := NewTestValidatorSetWithKeys(4)
block := NewTestBlock(1, NewTestHash("genesis"), 0)
// Create justification QC
votes := make([]*Vote[TestHash], 3)
for i := 0; i < 3; i++ {
votes[i], _ = NewVote(0, NewTestHash("genesis"), uint16(i), keys[i])
}
justifyQC, _ := NewQC(0, NewTestHash("genesis"), votes, validators, CryptoSchemeEd25519)
msg := NewProposeMessage(1, 0, block, justifyQC)
if msg.Type() != MessageProposal {
t.Errorf("Message type should be PROPOSAL, got %s", msg.Type())
}
if msg.View() != 1 {
t.Errorf("View should be 1, got %d", msg.View())
}
if msg.ValidatorIndex() != 0 {
t.Errorf("ValidatorIndex should be 0, got %d", msg.ValidatorIndex())
}
if msg.Block() == nil {
t.Fatal("Block should not be nil")
}
if msg.JustifyQC() == nil {
t.Fatal("JustifyQC should not be nil")
}
}
// TestVoteMessageCreation tests VOTE message creation.
func TestVoteMessageCreation(t *testing.T) {
key, _ := crypto.GenerateEd25519Key()
vote, _ := NewVote(1, NewTestHash("block-1"), 2, key)
msg := NewVoteMessage(1, 2, vote)
if msg.Type() != MessageVote {
t.Errorf("Message type should be VOTE, got %s", msg.Type())
}
if msg.View() != 1 {
t.Errorf("View should be 1, got %d", msg.View())
}
if msg.ValidatorIndex() != 2 {
t.Errorf("ValidatorIndex should be 2, got %d", msg.ValidatorIndex())
}
if msg.Vote() == nil {
t.Fatal("Vote should not be nil")
}
}
// TestNewViewMessageCreation tests NEWVIEW message creation.
func TestNewViewMessageCreation(t *testing.T) {
validators, keys := NewTestValidatorSetWithKeys(4)
// Create highQC
votes := make([]*Vote[TestHash], 3)
for i := 0; i < 3; i++ {
votes[i], _ = NewVote(2, NewTestHash("block-2"), uint16(i), keys[i])
}
highQC, _ := NewQC(2, NewTestHash("block-2"), votes, validators, CryptoSchemeEd25519)
msg := NewNewViewMessage(3, 1, highQC)
if msg.Type() != MessageNewView {
t.Errorf("Message type should be NEWVIEW, got %s", msg.Type())
}
if msg.View() != 3 {
t.Errorf("View should be 3, got %d", msg.View())
}
if msg.ValidatorIndex() != 1 {
t.Errorf("ValidatorIndex should be 1, got %d", msg.ValidatorIndex())
}
if msg.HighQC() == nil {
t.Fatal("HighQC should not be nil")
}
}
// TestMessageAccessors tests message accessor methods.
func TestMessageAccessors(t *testing.T) {
block := NewTestBlock(5, NewTestHash("prev"), 0)
msg := NewProposeMessage[TestHash](10, 3, block, nil)
if msg.Type() != MessageProposal {
t.Error("Type() failed")
}
if msg.View() != 10 {
t.Error("View() failed")
}
if msg.ValidatorIndex() != 3 {
t.Error("ValidatorIndex() failed")
}
if msg.Block().Height() != 5 {
t.Error("Block() failed")
}
// JustifyQC can be nil for genesis
if msg.JustifyQC() != nil {
t.Error("JustifyQC should be nil")
}
}
// TestMessageTypeString tests message type string representation.
func TestMessageTypeString(t *testing.T) {
tests := []struct {
msgType MessageType
want string
}{
{MessageProposal, "PROPOSAL"},
{MessageVote, "VOTE"},
{MessageNewView, "NEWVIEW"},
{MessageType(99), "UNKNOWN"},
}
for _, tt := range tests {
t.Run(tt.want, func(t *testing.T) {
if got := tt.msgType.String(); got != tt.want {
t.Errorf("String() = %v, want %v", got, tt.want)
}
})
}
}
// TestProposeMessageNilJustifyQC tests PROPOSE with nil justify QC (genesis).
func TestProposeMessageNilJustifyQC(t *testing.T) {
genesisBlock := NewTestBlock(0, TestHash{}, 0)
msg := NewProposeMessage[TestHash](0, 0, genesisBlock, nil)
if msg.JustifyQC() != nil {
t.Error("Genesis proposal should have nil justifyQC")
}
if msg.Block().Height() != 0 {
t.Error("Genesis block height should be 0")
}
}
// TestVoteMessageAccessors tests Vote message-specific accessors.
func TestVoteMessageAccessors(t *testing.T) {
key, _ := crypto.GenerateEd25519Key()
vote, _ := NewVote(5, NewTestHash("test-block"), 1, key)
msg := NewVoteMessage(5, 1, vote)
retrievedVote := msg.Vote()
if retrievedVote == nil {
t.Fatal("Vote should not be nil")
}
if retrievedVote.View() != 5 {
t.Errorf("Vote view should be 5, got %d", retrievedVote.View())
}
if retrievedVote.ValidatorIndex() != 1 {
t.Errorf("Vote validator index should be 1, got %d", retrievedVote.ValidatorIndex())
}
}
// TestNewViewMessageAccessors tests NEWVIEW message-specific accessors.
func TestNewViewMessageAccessors(t *testing.T) {
validators, keys := NewTestValidatorSetWithKeys(4)
// Create QC
votes := make([]*Vote[TestHash], 3)
for i := 0; i < 3; i++ {
votes[i], _ = NewVote(10, NewTestHash("block"), uint16(i), keys[i])
}
qc, _ := NewQC(10, NewTestHash("block"), votes, validators, CryptoSchemeEd25519)
msg := NewNewViewMessage(11, 2, qc)
retrievedQC := msg.HighQC()
if retrievedQC == nil {
t.Fatal("HighQC should not be nil")
}
if retrievedQC.View() != 10 {
t.Errorf("HighQC view should be 10, got %d", retrievedQC.View())
}
}
// TestMessageBytes tests message serialization.
func TestMessageBytes(t *testing.T) {
block := NewTestBlock(1, NewTestHash("genesis"), 0)
msg := NewProposeMessage[TestHash](1, 0, block, nil)
bytes := msg.Bytes()
if len(bytes) == 0 {
t.Error("Message bytes should not be empty")
}
// Test Hash()
hash := msg.Hash()
if hash == (TestHash{}) {
t.Error("Message hash should not be empty")
}
}
// TestMessageEquality tests that identical messages produce same hash.
func TestMessageEquality(t *testing.T) {
block := NewTestBlock(1, NewTestHash("genesis"), 0)
msg1 := NewProposeMessage[TestHash](1, 0, block, nil)
msg2 := NewProposeMessage[TestHash](1, 0, block, nil)
// Same content should produce same hash
if !msg1.Hash().Equals(msg2.Hash()) {
t.Error("Identical messages should have identical hashes")
}
}
// TestMessageDifference tests that different messages produce different hashes.
func TestMessageDifference(t *testing.T) {
block1 := NewTestBlock(1, NewTestHash("genesis"), 0)
block2 := NewTestBlock(2, NewTestHash("genesis"), 0)
msg1 := NewProposeMessage[TestHash](1, 0, block1, nil)
msg2 := NewProposeMessage[TestHash](1, 0, block2, nil)
// Different blocks should produce different hashes
if msg1.Hash().Equals(msg2.Hash()) {
t.Error("Different messages should have different hashes")
}
}
// TestProposeMessageSerialization tests PROPOSE message round-trip serialization.
func TestProposeMessageSerialization(t *testing.T) {
// Create a PROPOSE message with QC
block := NewTestBlock(1, NewTestHash("genesis"), 0)
// Create QC for justify
validators := NewTestValidatorSet(4)
keys := make([]*crypto.Ed25519PrivateKey, 3)
votes := make([]*Vote[TestHash], 3)
for i := 0; i < 3; i++ {
keys[i], _ = crypto.GenerateEd25519Key()
votes[i], _ = NewVote(0, NewTestHash("genesis"), uint16(i), keys[i])
}
justifyQC, _ := NewQC(0, NewTestHash("genesis"), votes, validators, CryptoSchemeEd25519)
msg := NewProposeMessage[TestHash](1, 0, block, justifyQC)
// Serialize
bytes := msg.Bytes()
if len(bytes) == 0 {
t.Fatal("Serialization produced empty bytes")
}
// Deserialize
// Helper to parse TestHash
parseHash := func(b []byte) (TestHash, error) {
var h TestHash
copy(h[:], b)
return h, nil
}
// Helper to parse Block
parseBlock := func(b []byte) (Block[TestHash], error) {
// For testing, reconstruct the block
// In production, this would properly deserialize
return block, nil
}
msg2, err := MessageFromBytes[TestHash](bytes, parseHash, parseBlock)
if err != nil {
t.Fatalf("Deserialization failed: %v", err)
}
// Verify type
if msg2.Type() != MessageProposal {
t.Errorf("Expected PROPOSE, got %v", msg2.Type())
}
// Verify fields
if msg2.View() != msg.View() {
t.Error("View mismatch after deserialization")
}
if msg2.ValidatorIndex() != msg.ValidatorIndex() {
t.Error("Validator index mismatch after deserialization")
}
}
// TestVoteMessageSerialization tests VOTE message round-trip serialization.
func TestVoteMessageSerialization(t *testing.T) {
key, _ := crypto.GenerateEd25519Key()
vote, _ := NewVote(1, NewTestHash("block-1"), 0, key)
msg := NewVoteMessage[TestHash](1, 0, vote)
// Serialize
bytes := msg.Bytes()
if len(bytes) == 0 {
t.Fatal("Serialization produced empty bytes")
}
// Deserialize
parseHash := func(b []byte) (TestHash, error) {
var h TestHash
copy(h[:], b)
return h, nil
}
parseBlock := func(b []byte) (Block[TestHash], error) {
return nil, nil // Not used for VOTE messages
}
msg2, err := MessageFromBytes[TestHash](bytes, parseHash, parseBlock)
if err != nil {
t.Fatalf("Deserialization failed: %v", err)
}
// Verify type
if msg2.Type() != MessageVote {
t.Errorf("Expected VOTE, got %v", msg2.Type())
}
// Verify fields
if msg2.View() != msg.View() {
t.Error("View mismatch after deserialization")
}
if msg2.ValidatorIndex() != msg.ValidatorIndex() {
t.Error("Validator index mismatch after deserialization")
}
// Verify vote
vote2 := msg2.Vote()
if vote2 == nil {
t.Fatal("Deserialized vote is nil")
}
if vote2.View() != vote.View() {
t.Error("Vote view mismatch")
}
}
// TestNewViewMessageSerialization tests NEWVIEW message round-trip serialization.
func TestNewViewMessageSerialization(t *testing.T) {
// Create high QC
validators := NewTestValidatorSet(4)
keys := make([]*crypto.Ed25519PrivateKey, 3)
votes := make([]*Vote[TestHash], 3)
for i := 0; i < 3; i++ {
keys[i], _ = crypto.GenerateEd25519Key()
votes[i], _ = NewVote(1, NewTestHash("block-1"), uint16(i), keys[i])
}
highQC, _ := NewQC(1, NewTestHash("block-1"), votes, validators, CryptoSchemeEd25519)
msg := NewNewViewMessage[TestHash](2, 1, highQC)
// Serialize
bytes := msg.Bytes()
if len(bytes) == 0 {
t.Fatal("Serialization produced empty bytes")
}
// Deserialize
parseHash := func(b []byte) (TestHash, error) {
var h TestHash
copy(h[:], b)
return h, nil
}
parseBlock := func(b []byte) (Block[TestHash], error) {
return nil, nil // Not used for NEWVIEW messages
}
msg2, err := MessageFromBytes[TestHash](bytes, parseHash, parseBlock)
if err != nil {
t.Fatalf("Deserialization failed: %v", err)
}
// Verify type
if msg2.Type() != MessageNewView {
t.Errorf("Expected NEWVIEW, got %v", msg2.Type())
}
// Verify fields
if msg2.View() != msg.View() {
t.Error("View mismatch after deserialization")
}
if msg2.ValidatorIndex() != msg.ValidatorIndex() {
t.Error("Validator index mismatch after deserialization")
}
// Verify highQC
highQC2 := msg2.HighQC()
if highQC2 == nil {
t.Fatal("Deserialized highQC is nil")
}
if highQC2.View() != highQC.View() {
t.Error("HighQC view mismatch")
}
}
// TestMessageSerializationRoundTrip tests that all message types can be serialized and deserialized.
func TestMessageSerializationRoundTrip(t *testing.T) {
parseHash := func(b []byte) (TestHash, error) {
var h TestHash
copy(h[:], b)
return h, nil
}
parseBlock := func(b []byte) (Block[TestHash], error) {
// For testing, return a dummy block
return NewTestBlock(1, NewTestHash("test"), 0), nil
}
// Test PROPOSE
block := NewTestBlock(1, NewTestHash("genesis"), 0)
proposeMsg := NewProposeMessage[TestHash](1, 0, block, nil)
proposeBytes := proposeMsg.Bytes()
proposeParsed, err := MessageFromBytes[TestHash](proposeBytes, parseHash, parseBlock)
if err != nil {
t.Errorf("PROPOSE round-trip failed: %v", err)
}
if proposeParsed.Type() != MessageProposal {
t.Error("PROPOSE type mismatch after round-trip")
}
// Test VOTE
key, _ := crypto.GenerateEd25519Key()
vote, _ := NewVote(1, NewTestHash("block-1"), 0, key)
voteMsg := NewVoteMessage[TestHash](1, 0, vote)
voteBytes := voteMsg.Bytes()
voteParsed, err := MessageFromBytes[TestHash](voteBytes, parseHash, parseBlock)
if err != nil {
t.Errorf("VOTE round-trip failed: %v", err)
}
if voteParsed.Type() != MessageVote {
t.Error("VOTE type mismatch after round-trip")
}
// Test NEWVIEW
validators := NewTestValidatorSet(4)
keys := make([]*crypto.Ed25519PrivateKey, 3)
votes := make([]*Vote[TestHash], 3)
for i := 0; i < 3; i++ {
keys[i], _ = crypto.GenerateEd25519Key()
votes[i], _ = NewVote(1, NewTestHash("block-1"), uint16(i), keys[i])
}
highQC, _ := NewQC(1, NewTestHash("block-1"), votes, validators, CryptoSchemeEd25519)
newViewMsg := NewNewViewMessage[TestHash](2, 1, highQC)
newViewBytes := newViewMsg.Bytes()
newViewParsed, err := MessageFromBytes[TestHash](newViewBytes, parseHash, parseBlock)
if err != nil {
t.Errorf("NEWVIEW round-trip failed: %v", err)
}
if newViewParsed.Type() != MessageNewView {
t.Error("NEWVIEW type mismatch after round-trip")
}
}