-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext_test.go
More file actions
730 lines (592 loc) · 19.6 KB
/
context_test.go
File metadata and controls
730 lines (592 loc) · 19.6 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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
package hotstuff2
import (
"testing"
)
// TestContextCreation tests Context initialization.
func TestContextCreation(t *testing.T) {
genesisBlock := NewTestBlock(0, TestHash{}, 0)
validators, keys := NewTestValidatorSetWithKeys(4)
votes := make([]*Vote[TestHash], 3)
for i := range 3 {
votes[i], _ = NewVote(0, genesisBlock.Hash(), uint16(i), keys[i])
}
genesisQC, _ := NewQC(0, genesisBlock.Hash(), votes, validators, CryptoSchemeEd25519)
ctx := NewContext(genesisBlock, genesisQC)
if ctx.View() != 0 {
t.Errorf("Initial view should be 0, got %d", ctx.View())
}
if ctx.LockedQC() == nil {
t.Error("LockedQC should be initialized with genesisQC")
}
if ctx.HighQC() == nil {
t.Error("HighQC should be initialized with genesisQC")
}
// Genesis should be committed
if !ctx.IsCommitted(0) {
t.Error("Genesis block should be committed")
}
}
// TestContextViewManagement tests view number management.
func TestContextViewManagement(t *testing.T) {
ctx := NewContext[TestHash](nil, nil)
if ctx.View() != 0 {
t.Errorf("Initial view should be 0, got %d", ctx.View())
}
ctx.SetView(5)
if ctx.View() != 5 {
t.Errorf("View should be 5 after SetView, got %d", ctx.View())
}
ctx.SetView(6)
if ctx.View() != 6 {
t.Errorf("View should be 6 after SetView(6), got %d", ctx.View())
}
}
// TestContextQCManagement tests QC storage and retrieval.
func TestContextQCManagement(t *testing.T) {
validators, keys := NewTestValidatorSetWithKeys(4)
ctx := NewContext[TestHash](nil, nil)
// Create QC at view 1
block1 := NewTestBlock(1, NewTestHash("genesis"), 0)
votes1 := make([]*Vote[TestHash], 3)
for i := range 3 {
votes1[i], _ = NewVote(1, block1.Hash(), uint16(i), keys[i])
}
qc1, _ := NewQC(1, block1.Hash(), votes1, validators, CryptoSchemeEd25519)
// Add QC
ctx.AddQC(qc1)
// Retrieve QC
retrieved, ok := ctx.GetQC(block1.Hash())
if !ok {
t.Fatal("QC should be retrievable")
}
if retrieved.View() != 1 {
t.Errorf("Retrieved QC view should be 1, got %d", retrieved.View())
}
// GetQC for non-existent block
_, ok = ctx.GetQC(NewTestHash("nonexistent"))
if ok {
t.Error("GetQC should return false for non-existent block")
}
}
// TestContextLockedQCUpdate tests locked QC updates following TLA+ spec.
//
// TLA+ UpdateLockCond: lockedQC' = IF qc.view > lockedQC.view THEN qc ELSE lockedQC
func TestContextLockedQCUpdate(t *testing.T) {
validators, keys := NewTestValidatorSetWithKeys(4)
// Initial locked QC at view 1
block1 := NewTestBlock(1, NewTestHash("genesis"), 0)
votes1 := make([]*Vote[TestHash], 3)
for i := range 3 {
votes1[i], _ = NewVote(1, block1.Hash(), uint16(i), keys[i])
}
qc1, _ := NewQC(1, block1.Hash(), votes1, validators, CryptoSchemeEd25519)
ctx := NewContext[TestHash](nil, qc1)
// Try to update with lower view QC (should fail)
block0 := NewTestBlock(0, NewTestHash("genesis"), 0)
votes0 := make([]*Vote[TestHash], 3)
for i := range 3 {
votes0[i], _ = NewVote(0, block0.Hash(), uint16(i), keys[i])
}
qc0, _ := NewQC(0, block0.Hash(), votes0, validators, CryptoSchemeEd25519)
if ctx.UpdateLockedQC(qc0) {
t.Error("UpdateLockedQC should return false for lower view QC")
}
if ctx.LockedQC().View() != 1 {
t.Error("LockedQC should not change for lower view")
}
// Update with higher view QC (should succeed)
block2 := NewTestBlock(2, block1.Hash(), 0)
votes2 := make([]*Vote[TestHash], 3)
for i := range 3 {
votes2[i], _ = NewVote(2, block2.Hash(), uint16(i), keys[i])
}
qc2, _ := NewQC(2, block2.Hash(), votes2, validators, CryptoSchemeEd25519)
if !ctx.UpdateLockedQC(qc2) {
t.Error("UpdateLockedQC should return true for higher view QC")
}
if ctx.LockedQC().View() != 2 {
t.Errorf("LockedQC view should be 2, got %d", ctx.LockedQC().View())
}
}
// TestContextHighQCUpdate tests highest QC tracking.
func TestContextHighQCUpdate(t *testing.T) {
validators, keys := NewTestValidatorSetWithKeys(4)
ctx := NewContext[TestHash](nil, nil)
// Add QC at view 1
block1 := NewTestBlock(1, NewTestHash("genesis"), 0)
votes1 := make([]*Vote[TestHash], 3)
for i := range 3 {
votes1[i], _ = NewVote(1, block1.Hash(), uint16(i), keys[i])
}
qc1, _ := NewQC(1, block1.Hash(), votes1, validators, CryptoSchemeEd25519)
if !ctx.UpdateHighQC(qc1) {
t.Error("UpdateHighQC should return true for first QC")
}
// Try lower view QC
block0 := NewTestBlock(0, NewTestHash("genesis"), 0)
votes0 := make([]*Vote[TestHash], 3)
for i := range 3 {
votes0[i], _ = NewVote(0, block0.Hash(), uint16(i), keys[i])
}
qc0, _ := NewQC(0, block0.Hash(), votes0, validators, CryptoSchemeEd25519)
if ctx.UpdateHighQC(qc0) {
t.Error("UpdateHighQC should return false for lower view QC")
}
// Higher view QC
block3 := NewTestBlock(3, block1.Hash(), 0)
votes3 := make([]*Vote[TestHash], 3)
for i := range 3 {
votes3[i], _ = NewVote(3, block3.Hash(), uint16(i), keys[i])
}
qc3, _ := NewQC(3, block3.Hash(), votes3, validators, CryptoSchemeEd25519)
if !ctx.UpdateHighQC(qc3) {
t.Error("UpdateHighQC should return true for higher view QC")
}
if ctx.HighQC().View() != 3 {
t.Errorf("HighQC view should be 3, got %d", ctx.HighQC().View())
}
}
// TestContextBlockManagement tests block storage and retrieval.
func TestContextBlockManagement(t *testing.T) {
ctx := NewContext[TestHash](nil, nil)
block1 := NewTestBlock(1, NewTestHash("genesis"), 0)
block2 := NewTestBlock(2, block1.Hash(), 0)
// Add blocks
ctx.AddBlock(block1)
ctx.AddBlock(block2)
// Retrieve blocks
retrieved1, ok1 := ctx.GetBlock(block1.Hash())
if !ok1 {
t.Fatal("Block1 should be retrievable")
}
if retrieved1.Height() != 1 {
t.Errorf("Block1 height should be 1, got %d", retrieved1.Height())
}
_, ok2 := ctx.GetBlock(block2.Hash())
if !ok2 {
t.Fatal("Block2 should be retrievable")
}
// Non-existent block
_, ok := ctx.GetBlock(NewTestHash("nonexistent"))
if ok {
t.Error("GetBlock should return false for non-existent block")
}
}
// TestContextVoteTracking tests vote collection and counting.
func TestContextVoteTracking(t *testing.T) {
_, keys := NewTestValidatorSetWithKeys(4)
ctx := NewContext[TestHash](nil, nil)
view := uint32(1)
block := NewTestBlock(1, NewTestHash("genesis"), 0)
// Add votes from 3 validators
for i := range 3 {
vote, _ := NewVote(view, block.Hash(), uint16(i), keys[i])
ctx.AddVote(vote)
}
// Count votes
count := ctx.VoteCount(view, block.Hash())
if count != 3 {
t.Errorf("Vote count should be 3, got %d", count)
}
// Get votes
votes := ctx.GetVotes(view, block.Hash())
if len(votes) != 3 {
t.Errorf("Should have 3 votes, got %d", len(votes))
}
// Check if has voted
if !ctx.HasVoted(view, 0) {
t.Error("Validator 0 should have voted")
}
if ctx.HasVoted(view, 3) {
t.Error("Validator 3 should not have voted")
}
// Different view
if ctx.VoteCount(2, block.Hash()) != 0 {
t.Error("Should have 0 votes in view 2")
}
}
// TestContextCommitTracking tests block commit tracking.
func TestContextCommitTracking(t *testing.T) {
ctx := NewContext[TestHash](nil, nil)
block1 := NewTestBlock(1, NewTestHash("genesis"), 0)
block2 := NewTestBlock(2, block1.Hash(), 0)
// Initially not committed
if ctx.IsCommitted(1) {
t.Error("Block at height 1 should not be committed initially")
}
// Commit block
ctx.Commit(block1)
if !ctx.IsCommitted(1) {
t.Error("Block at height 1 should be committed")
}
// Get committed block
committed, ok := ctx.GetCommitted(1)
if !ok {
t.Fatal("Should retrieve committed block at height 1")
}
if !committed.Hash().Equals(block1.Hash()) {
t.Error("Retrieved committed block hash mismatch")
}
// Commit another block
ctx.Commit(block2)
if !ctx.IsCommitted(2) {
t.Error("Block at height 2 should be committed")
}
// Get committed blocks
allCommitted := ctx.CommittedBlocks()
if len(allCommitted) != 2 {
t.Errorf("Should have 2 committed blocks, got %d", len(allCommitted))
}
}
// TestContextSafeToVote tests the SafeNode rule from TLA+ spec.
//
// TLA+ SafeNodeRule:
//
// \/ lockedQC[r] = Nil
// \/ (qc /= Nil /\ blockView[qc] > blockView[lockedQC[r]])
// \/ IsAncestor(lockedQC[r], block)
func TestContextSafeToVote(t *testing.T) {
validators, keys := NewTestValidatorSetWithKeys(4)
t.Run("NoLock", func(t *testing.T) {
// No locked QC - should always be safe
ctx := NewContext[TestHash](nil, nil)
block := NewTestBlock(1, NewTestHash("genesis"), 0)
if !ctx.SafeToVote(block, nil) {
t.Error("Should be safe to vote when no lock")
}
})
t.Run("HigherQC", func(t *testing.T) {
// Justify QC view > locked QC view - should be safe
block1 := NewTestBlock(1, NewTestHash("genesis"), 0)
votes1 := make([]*Vote[TestHash], 3)
for i := range 3 {
votes1[i], _ = NewVote(1, block1.Hash(), uint16(i), keys[i])
}
lockedQC, _ := NewQC(1, block1.Hash(), votes1, validators, CryptoSchemeEd25519)
ctx := NewContext[TestHash](nil, lockedQC)
block2 := NewTestBlock(2, block1.Hash(), 0)
votes2 := make([]*Vote[TestHash], 3)
for i := range 3 {
votes2[i], _ = NewVote(2, block2.Hash(), uint16(i), keys[i])
}
justifyQC, _ := NewQC(2, block2.Hash(), votes2, validators, CryptoSchemeEd25519)
block3 := NewTestBlock(3, block2.Hash(), 0)
if !ctx.SafeToVote(block3, justifyQC) {
t.Error("Should be safe to vote when justify QC view > locked QC view")
}
})
t.Run("ExtendsLock", func(t *testing.T) {
// Block extends locked chain - should be safe
genesis := NewTestBlock(0, TestHash{}, 0)
block1 := NewTestBlock(1, genesis.Hash(), 0)
votes1 := make([]*Vote[TestHash], 3)
for i := range 3 {
votes1[i], _ = NewVote(1, block1.Hash(), uint16(i), keys[i])
}
lockedQC, _ := NewQC(1, block1.Hash(), votes1, validators, CryptoSchemeEd25519)
ctx := NewContext(genesis, lockedQC)
// IMPORTANT: Must add block1 to establish parent chain
ctx.AddBlock(block1)
// Block2 extends block1 which is locked
block2 := NewTestBlock(2, block1.Hash(), 0)
// Also add block2 so its parent relationship is known
ctx.AddBlock(block2)
if !ctx.SafeToVote(block2, nil) {
t.Error("Should be safe to vote when block extends locked block")
}
})
t.Run("Conflicting", func(t *testing.T) {
// Block conflicts with lock and justify QC is not higher - should NOT be safe
genesis := NewTestBlock(0, TestHash{}, 0)
// Lock on block1
block1 := NewTestBlock(1, genesis.Hash(), 0)
votes1 := make([]*Vote[TestHash], 3)
for i := range 3 {
votes1[i], _ = NewVote(1, block1.Hash(), uint16(i), keys[i])
}
lockedQC, _ := NewQC(1, block1.Hash(), votes1, validators, CryptoSchemeEd25519)
ctx := NewContext(genesis, lockedQC)
ctx.AddBlock(block1)
// Block1b conflicts with block1 (same height, different hash)
block1b := NewTestBlock(1, genesis.Hash(), 1) // Different proposer
if ctx.SafeToVote(block1b, nil) {
t.Error("Should NOT be safe to vote for conflicting block with no higher justify QC")
}
})
}
// TestContextAncestryChecking tests the IsAncestor function.
func TestContextAncestryChecking(t *testing.T) {
ctx := NewContext[TestHash](nil, nil)
// Build chain: genesis -> b1 -> b2 -> b3
genesis := NewTestBlock(0, TestHash{}, 0)
b1 := NewTestBlock(1, genesis.Hash(), 0)
b2 := NewTestBlock(2, b1.Hash(), 0)
b3 := NewTestBlock(3, b2.Hash(), 0)
ctx.AddBlock(genesis)
ctx.AddBlock(b1)
ctx.AddBlock(b2)
ctx.AddBlock(b3)
// Direct parent
if !ctx.IsAncestor(b2.Hash(), b3.Hash()) {
t.Error("b2 should be ancestor of b3 (direct parent)")
}
// Grandparent
if !ctx.IsAncestor(b1.Hash(), b3.Hash()) {
t.Error("b1 should be ancestor of b3 (grandparent)")
}
// Genesis ancestor
if !ctx.IsAncestor(genesis.Hash(), b3.Hash()) {
t.Error("genesis should be ancestor of b3")
}
// Self
if !ctx.IsAncestor(b2.Hash(), b2.Hash()) {
t.Error("Block should be ancestor of itself")
}
// Not ancestor
if ctx.IsAncestor(b3.Hash(), b2.Hash()) {
t.Error("b3 should NOT be ancestor of b2 (reversed)")
}
// Unrelated blocks
b1alt := NewTestBlock(1, genesis.Hash(), 1) // Fork
ctx.AddBlock(b1alt)
if ctx.IsAncestor(b1alt.Hash(), b3.Hash()) {
t.Error("b1alt should NOT be ancestor of b3 (different chain)")
}
}
// TestContextConcurrency tests thread-safety of Context.
func TestContextConcurrency(t *testing.T) {
ctx := NewContext[TestHash](nil, nil)
done := make(chan struct{})
// Concurrent view updates
go func() {
for range 100 {
v := ctx.View()
ctx.SetView(v + 1)
}
done <- struct{}{}
}()
go func() {
for range 100 {
_ = ctx.View()
}
done <- struct{}{}
}()
<-done
<-done
// View should be >= 100 (one goroutine incremented, but concurrency may cause some overlaps)
if ctx.View() < 100 {
t.Errorf("Expected view to be >= 100, got %d", ctx.View())
}
}
// TestContextNewViewTracking tests NEWVIEW message tracking for view changes.
//
// TLA+ spec (ReplicaTimeout, LeaderProcessNewView):
// - Replicas send NEWVIEW with their highQC on timeout
// - Leader collects 2f+1 NEWVIEWs before proposing
// - Leader selects the highest QC from all NEWVIEWs
func TestContextNewViewTracking(t *testing.T) {
validators, keys := NewTestValidatorSetWithKeys(4)
ctx := NewContext[TestHash](nil, nil)
view := uint32(5)
// Create some QCs at different views for testing
block1 := NewTestBlock(1, NewTestHash("genesis"), 0)
votes1 := make([]*Vote[TestHash], 3)
for i := range 3 {
votes1[i], _ = NewVote(1, block1.Hash(), uint16(i), keys[i])
}
qc1, _ := NewQC(1, block1.Hash(), votes1, validators, CryptoSchemeEd25519)
block2 := NewTestBlock(2, block1.Hash(), 0)
votes2 := make([]*Vote[TestHash], 3)
for i := range 3 {
votes2[i], _ = NewVote(2, block2.Hash(), uint16(i), keys[i])
}
qc2, _ := NewQC(2, block2.Hash(), votes2, validators, CryptoSchemeEd25519)
block3 := NewTestBlock(3, block2.Hash(), 0)
votes3 := make([]*Vote[TestHash], 3)
for i := range 3 {
votes3[i], _ = NewVote(3, block3.Hash(), uint16(i), keys[i])
}
qc3, _ := NewQC(3, block3.Hash(), votes3, validators, CryptoSchemeEd25519)
// Initially no NEWVIEWs
if ctx.NewViewCount(view) != 0 {
t.Error("Should have 0 NEWVIEWs initially")
}
if ctx.HasNewViewQuorum(view, 3) {
t.Error("Should not have quorum initially")
}
// Add NEWVIEW from validator 0 with qc1
count := ctx.AddNewView(view, 0, qc1)
if count != 1 {
t.Errorf("Expected count 1, got %d", count)
}
// Add NEWVIEW from validator 1 with qc3 (highest)
count = ctx.AddNewView(view, 1, qc3)
if count != 2 {
t.Errorf("Expected count 2, got %d", count)
}
// Add NEWVIEW from validator 2 with qc2
count = ctx.AddNewView(view, 2, qc2)
if count != 3 {
t.Errorf("Expected count 3, got %d", count)
}
// Should now have quorum (2f+1 = 3 for n=4)
if !ctx.HasNewViewQuorum(view, 3) {
t.Error("Should have quorum after 3 NEWVIEWs")
}
// Highest QC should be qc3 (view 3)
highestQC := ctx.HighestQCFromNewViews(view)
if highestQC == nil {
t.Fatal("HighestQC should not be nil")
}
if highestQC.View() != 3 {
t.Errorf("Highest QC view should be 3, got %d", highestQC.View())
}
}
// TestContextNewViewWithNilQC tests NEWVIEW tracking when some validators have nil QC.
func TestContextNewViewWithNilQC(t *testing.T) {
validators, keys := NewTestValidatorSetWithKeys(4)
ctx := NewContext[TestHash](nil, nil)
view := uint32(2)
// Create a QC
block1 := NewTestBlock(1, NewTestHash("genesis"), 0)
votes1 := make([]*Vote[TestHash], 3)
for i := range 3 {
votes1[i], _ = NewVote(1, block1.Hash(), uint16(i), keys[i])
}
qc1, _ := NewQC(1, block1.Hash(), votes1, validators, CryptoSchemeEd25519)
// Add NEWVIEW with nil QC (e.g., fresh node at genesis)
ctx.AddNewView(view, 0, nil)
// Add NEWVIEW with qc1
ctx.AddNewView(view, 1, qc1)
// Add another NEWVIEW with nil QC
ctx.AddNewView(view, 2, nil)
// Should have 3 NEWVIEWs
if ctx.NewViewCount(view) != 3 {
t.Errorf("Expected 3 NEWVIEWs, got %d", ctx.NewViewCount(view))
}
// Highest QC should be qc1 (the only non-nil one)
highestQC := ctx.HighestQCFromNewViews(view)
if highestQC == nil {
t.Fatal("HighestQC should not be nil when at least one validator has QC")
}
if highestQC.View() != 1 {
t.Errorf("Highest QC view should be 1, got %d", highestQC.View())
}
}
// TestContextNewViewAllNilQC tests NEWVIEW tracking when all validators have nil QC.
func TestContextNewViewAllNilQC(t *testing.T) {
ctx := NewContext[TestHash](nil, nil)
view := uint32(1)
// All NEWVIEWs with nil QC (e.g., all at genesis)
ctx.AddNewView(view, 0, nil)
ctx.AddNewView(view, 1, nil)
ctx.AddNewView(view, 2, nil)
// Should have quorum
if !ctx.HasNewViewQuorum(view, 3) {
t.Error("Should have quorum with 3 NEWVIEWs even if all nil QC")
}
// Highest QC should be nil
highestQC := ctx.HighestQCFromNewViews(view)
if highestQC != nil {
t.Error("HighestQC should be nil when all validators have nil QC")
}
}
// TestContextNewViewDeduplication tests that duplicate NEWVIEWs from same validator are handled.
func TestContextNewViewDeduplication(t *testing.T) {
validators, keys := NewTestValidatorSetWithKeys(4)
ctx := NewContext[TestHash](nil, nil)
view := uint32(3)
// Create QCs
block1 := NewTestBlock(1, NewTestHash("genesis"), 0)
votes1 := make([]*Vote[TestHash], 3)
for i := range 3 {
votes1[i], _ = NewVote(1, block1.Hash(), uint16(i), keys[i])
}
qc1, _ := NewQC(1, block1.Hash(), votes1, validators, CryptoSchemeEd25519)
block2 := NewTestBlock(2, block1.Hash(), 0)
votes2 := make([]*Vote[TestHash], 3)
for i := range 3 {
votes2[i], _ = NewVote(2, block2.Hash(), uint16(i), keys[i])
}
qc2, _ := NewQC(2, block2.Hash(), votes2, validators, CryptoSchemeEd25519)
// Validator 0 sends NEWVIEW with qc1
count := ctx.AddNewView(view, 0, qc1)
if count != 1 {
t.Errorf("Expected count 1, got %d", count)
}
// Validator 0 sends another NEWVIEW with qc2 (higher) - should update
count = ctx.AddNewView(view, 0, qc2)
if count != 1 {
t.Errorf("Expected count still 1 (same validator), got %d", count)
}
// HasSentNewView should return true
if !ctx.HasSentNewView(view, 0) {
t.Error("Validator 0 should have sent NEWVIEW")
}
// The stored QC should be qc2 (the latest one)
highestQC := ctx.HighestQCFromNewViews(view)
if highestQC == nil || highestQC.View() != 2 {
t.Error("Should store the latest QC from validator")
}
}
// TestContextNewViewPruning tests that old NEWVIEWs are pruned with votes.
func TestContextNewViewPruning(t *testing.T) {
ctx := NewContext[TestHash](nil, nil)
// Add NEWVIEWs for views 1, 5, 10
ctx.AddNewView(1, 0, nil)
ctx.AddNewView(5, 0, nil)
ctx.AddNewView(10, 0, nil)
// Verify they exist
if ctx.NewViewCount(1) != 1 {
t.Error("Should have NEWVIEW for view 1")
}
if ctx.NewViewCount(5) != 1 {
t.Error("Should have NEWVIEW for view 5")
}
if ctx.NewViewCount(10) != 1 {
t.Error("Should have NEWVIEW for view 10")
}
// Prune old views (keep last 5 views from current view 10)
ctx.PruneVotes(10, 5)
// Views older than 10-5=5 should be pruned
if ctx.NewViewCount(1) != 0 {
t.Error("NEWVIEW for view 1 should be pruned")
}
// Views 5 and above should remain
if ctx.NewViewCount(5) != 1 {
t.Error("NEWVIEW for view 5 should remain")
}
if ctx.NewViewCount(10) != 1 {
t.Error("NEWVIEW for view 10 should remain")
}
}
// TestContextNewViewConcurrency tests thread-safety of NEWVIEW tracking.
func TestContextNewViewConcurrency(t *testing.T) {
ctx := NewContext[TestHash](nil, nil)
view := uint32(5)
done := make(chan struct{})
// Concurrent AddNewView from different validators
for i := range 10 {
go func(idx uint16) {
ctx.AddNewView(view, idx, nil)
done <- struct{}{}
}(uint16(i))
}
// Concurrent reads
go func() {
for range 100 {
_ = ctx.NewViewCount(view)
_ = ctx.HasNewViewQuorum(view, 7)
_ = ctx.HighestQCFromNewViews(view)
}
done <- struct{}{}
}()
// Wait for all goroutines
for range 11 {
<-done
}
// Should have exactly 10 NEWVIEWs (one per validator)
if ctx.NewViewCount(view) != 10 {
t.Errorf("Expected 10 NEWVIEWs, got %d", ctx.NewViewCount(view))
}
}