-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommitment_tracker_test.go
More file actions
645 lines (544 loc) · 16.6 KB
/
commitment_tracker_test.go
File metadata and controls
645 lines (544 loc) · 16.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
package dasmon
import (
"context"
"errors"
"fmt"
"sync"
"testing"
"time"
)
// mockFetcher implements CommitmentsFetcher for testing
type mockFetcher struct {
data map[uint64]*BlockCommitmentInfo
err error
}
var _ Fetcher = (*mockFetcher)(nil)
func (m *mockFetcher) FetchCommitments(ctx context.Context, slots Range[uint64]) ([]*BlockCommitmentInfo, error) {
if m.err != nil {
return nil, m.err
}
var res []*BlockCommitmentInfo
var fetchErr error
for slot := range slots.Values() {
bci, ok := m.data[slot]
if !ok {
fetchErr = ErrSlotUnavailable
break
}
res = append(res, bci)
}
if fetchErr != nil {
return nil, fetchErr
}
return res, nil
}
func (m *mockFetcher) MaxSlotsPerFetch() uint64 {
return 10
}
func (m *mockFetcher) Close() error {
return nil
}
// Helper function to create a BlockCommitmentInfo
func makeBlockInfo(slot uint64, numBlobs int) BlockCommitmentInfo {
root := [32]byte{}
root[0] = byte(slot)
commitments := make([][]byte, numBlobs)
for i := 0; i < numBlobs; i++ {
commitments[i] = []byte(fmt.Sprintf("commitment-%d-%d", slot, i))
}
return BlockCommitmentInfo{
Slot: slot,
BlockRoot: root,
BlobCommitments: commitments,
}
}
// TestNewCommitmentTracker tests the constructor
func TestNewCommitmentTracker(t *testing.T) {
var (
filter = &BlockFilter{
StartSlot: -100,
EndSlot: 0,
}
head = makeBlockInfo(1000, 3)
tracker = NewCommitmentTracker(filter, head)
expectedLength = filter.RequiredBufferSize()
idx = int(head.Slot) % tracker.length
stored = tracker.entries[idx]
)
if tracker == nil {
t.Fatal("NewCommitmentTracker returned nil")
}
if tracker.head != 1000 {
t.Errorf("Expected head=1000, got %d", tracker.head)
}
// After initialization with a head block, tail should equal head (single entry)
if tracker.tail != 1000 {
t.Errorf("Expected tail=1000, got %d", tracker.tail)
}
if tracker.length != expectedLength {
t.Errorf("Expected length=%d, got %d", expectedLength, tracker.length)
}
if len(tracker.entries) != expectedLength {
t.Errorf("Expected entries length=%d, got %d", expectedLength, len(tracker.entries))
}
// Verify head was stored correctly
if stored.Slot != head.Slot {
t.Errorf("Expected stored slot=%d, got %d", head.Slot, stored.Slot)
}
if stored.BlockRoot != head.BlockRoot {
t.Errorf("BlockRoot mismatch")
}
}
// TestAppendContiguous tests sequential appending
func TestAppendContiguous(t *testing.T) {
var (
filter = &BlockFilter{StartSlot: -10, EndSlot: 0}
head = makeBlockInfo(100, 2)
tracker = NewCommitmentTracker(filter, head)
root101 = [32]byte{101}
commits101 = [][]byte{[]byte("commit-101")}
)
// Append slot 101
if !tracker.Append(101, root101, commits101) {
t.Fatal("Append failed")
}
if tracker.head != 101 {
t.Errorf("Expected head=101, got %d", tracker.head)
}
// tail should now be initialized to the head (100) since this is the first append after construction
if tracker.tail != 100 {
t.Errorf("Expected tail=100, got %d", tracker.tail)
}
// Append slot 102
var (
root102 = [32]byte{102}
commits102 = [][]byte{[]byte("commit-102-1"), []byte("commit-102-2")}
)
tracker.Append(102, root102, commits102)
if tracker.head != 102 {
t.Errorf("Expected head=102, got %d", tracker.head)
}
if tracker.tail != 100 {
t.Errorf("Expected tail=100, got %d", tracker.tail)
}
}
// TestAppendNonContiguousPanics tests that non-contiguous appends panic
func TestAppendNonContiguousPanics(t *testing.T) {
var (
filter = &BlockFilter{StartSlot: -10, EndSlot: 0}
head = makeBlockInfo(100, 2)
tracker = NewCommitmentTracker(filter, head)
)
defer func() {
if r := recover(); r == nil {
t.Error("Expected panic for non-contiguous append")
}
}()
// Try to append slot 105 (should panic, expected 101)
tracker.Append(105, [32]byte{}, nil)
}
// TestAppendWrapping tests buffer wrapping behavior
func TestAppendWrapping(t *testing.T) {
var (
filter = &BlockFilter{StartSlot: -5, EndSlot: 0}
bufferSize = filter.RequiredBufferSize() // Should be 6
head = makeBlockInfo(100, 1)
tracker = NewCommitmentTracker(filter, head)
)
// Fill the buffer completely
for i := uint64(1); i <= uint64(bufferSize); i++ {
slot := 100 + i
tracker.Append(slot, [32]byte{byte(slot)}, [][]byte{[]byte(fmt.Sprintf("c-%d", slot))})
}
// At this point:
// head = 106, tail = 100, filled = 7 entries (100-106)
// But buffer size is 6, so tail should be 101 now
if tracker.head != 100+uint64(bufferSize) {
t.Errorf("Expected head=%d, got %d", 100+uint64(bufferSize), tracker.head)
}
expectedTail := uint64(101) // oldest entry after wrapping
if tracker.tail != expectedTail {
t.Errorf("Expected tail=%d, got %d", expectedTail, tracker.tail)
}
// Append one more to verify continued wrapping
nextSlot := 100 + uint64(bufferSize) + 1
tracker.Append(nextSlot, [32]byte{byte(nextSlot)}, nil)
if tracker.head != nextSlot {
t.Errorf("Expected head=%d, got %d", nextSlot, tracker.head)
}
if tracker.tail != 102 {
t.Errorf("Expected tail=102 after another wrap, got %d", tracker.tail)
}
}
// TestGet tests retrieving commitments
func TestGet(t *testing.T) {
var (
filter = &BlockFilter{StartSlot: -10, EndSlot: 0}
head = makeBlockInfo(100, 2)
tracker = NewCommitmentTracker(filter, head)
)
// Append a few slots
tracker.Append(101, [32]byte{101}, [][]byte{[]byte("c-101")})
tracker.Append(102, [32]byte{102}, [][]byte{[]byte("c-102")})
// Get slot 101
info, ok := tracker.Get(101)
if !ok {
t.Fatal("Expected to find slot 101")
}
if info.Slot != 101 {
t.Errorf("Expected slot=101, got %d", info.Slot)
}
if info.BlockRoot[0] != 101 {
t.Errorf("BlockRoot mismatch")
}
// Get slot outside range (too old)
_, ok = tracker.Get(50)
if ok {
t.Error("Should not find slot 50 (before tail)")
}
// Get slot outside range (too new)
_, ok = tracker.Get(200)
if ok {
t.Error("Should not find slot 200 (after head)")
}
}
// TestUndo tests removing the most recent entry
func TestUndo(t *testing.T) {
var (
filter = &BlockFilter{StartSlot: -10, EndSlot: 0}
head = makeBlockInfo(100, 2)
tracker = NewCommitmentTracker(filter, head)
)
tracker.Append(101, [32]byte{101}, [][]byte{[]byte("c-101")})
tracker.Append(102, [32]byte{102}, [][]byte{[]byte("c-102")})
// Undo slot 102
slot, ok := tracker.Undo()
if !ok {
t.Fatal("Undo should succeed")
}
if slot != 102 {
t.Errorf("Expected undone slot=102, got %d", slot)
}
if tracker.head != 101 {
t.Errorf("Expected head=101 after undo, got %d", tracker.head)
}
// Undo slot 101
slot, ok = tracker.Undo()
if !ok {
t.Fatal("Undo should succeed")
}
if slot != 101 {
t.Errorf("Expected undone slot=101, got %d", slot)
}
if tracker.head != 100 {
t.Errorf("Expected head=100 after undo, got %d", tracker.head)
}
// Try to undo the last entry (slot 100) - should fail because tail == head
// The buffer maintains at least one entry (the initial head)
_, ok = tracker.Undo()
if ok {
t.Error("Undo should fail when only the initial entry remains (tail == head)")
}
// Verify state is unchanged
if tracker.head != 100 {
t.Errorf("Expected head=100 (unchanged), got %d", tracker.head)
}
if tracker.tail != 100 {
t.Errorf("Expected tail=100 (unchanged), got %d", tracker.tail)
}
}
// TestBackfillSuccess tests successful backfilling
func TestBackfillSuccess(t *testing.T) {
var (
ctx = t.Context()
filter = &BlockFilter{StartSlot: -10, EndSlot: 0}
head = makeBlockInfo(100, 2)
tracker = NewCommitmentTracker(filter, head)
mockData = make(map[uint64]*BlockCommitmentInfo)
)
// Create mock data for slots 80-99 (provides buffer for batch fetching)
// Filter requires 90-99, but backfill fetches in batches of 10
for slot := uint64(80); slot < 100; slot++ {
info := makeBlockInfo(slot, 2)
mockData[slot] = &info
}
fetcher := &mockFetcher{data: mockData}
err := tracker.Backfill(ctx, fetcher)
if err != nil {
t.Fatalf("Backfill failed: %v", err)
}
// Verify tail moved back to 90
if tracker.tail != 90 {
t.Errorf("Expected tail=90 after backfill, got %d", tracker.tail)
}
// Verify head unchanged
if tracker.head != 100 {
t.Errorf("Expected head=100 after backfill, got %d", tracker.head)
}
// Verify we can retrieve backfilled data
for slot := uint64(90); slot < 100; slot++ {
info, ok := tracker.Get(slot)
if !ok {
t.Errorf("Should find backfilled slot %d", slot)
}
if info.Slot != slot {
t.Errorf("Slot mismatch for %d", slot)
}
}
}
// TestBackfillStopsOnUnavailableSlot tests that backfill stops on missing slots
func TestBackfillStopsOnUnavailableSlot(t *testing.T) {
var (
ctx = t.Context()
filter = &BlockFilter{StartSlot: -10, EndSlot: 0}
head = makeBlockInfo(100, 2)
tracker = NewCommitmentTracker(filter, head)
mockData = make(map[uint64]*BlockCommitmentInfo)
)
// Create mock data with a gap (slot 97 missing)
for slot := uint64(95); slot < 100; slot++ {
if slot == 97 {
continue // Create gap
}
info := makeBlockInfo(slot, 2)
mockData[slot] = &info
}
fetcher := &mockFetcher{data: mockData}
err := tracker.Backfill(ctx, fetcher)
if err == nil {
t.Fatal("Expected backfill to fail on unavailable slot")
}
if !errors.Is(err, ErrSlotUnavailable) {
t.Errorf("Expected ErrSlotUnavailable in error, got: %v", err)
}
}
// TestBackfillWithZeroHead tests backfill behavior when head is at genesis
func TestBackfillWithZeroHead(t *testing.T) {
var (
ctx = t.Context()
filter = &BlockFilter{StartSlot: -10, EndSlot: 0}
head = makeBlockInfo(0, 0)
tracker = NewCommitmentTracker(filter, head)
fetcher = &mockFetcher{data: make(map[uint64]*BlockCommitmentInfo)}
)
err := tracker.Backfill(ctx, fetcher)
if err != nil {
t.Errorf("Backfill should succeed with no-op at genesis: %v", err)
}
}
// TestBackfillStopsWhenOutOfRange tests backfill respects filter range
func TestBackfillStopsWhenOutOfRange(t *testing.T) {
var (
ctx = t.Context()
filter = &BlockFilter{StartSlot: -5, EndSlot: 0} // Only 6 slots back
head = makeBlockInfo(100, 2)
tracker = NewCommitmentTracker(filter, head)
mockData = make(map[uint64]*BlockCommitmentInfo)
)
// Create mock data for many slots (well beyond filter range)
for slot := uint64(80); slot < 100; slot++ {
info := makeBlockInfo(slot, 1)
mockData[slot] = &info
}
fetcher := &mockFetcher{data: mockData}
err := tracker.Backfill(ctx, fetcher)
if err != nil {
t.Fatalf("Backfill failed: %v", err)
}
// Backfill should stop at slot 95 (100 - 5 = 95)
start := filter.EffectiveRange(100).Start
if tracker.tail != start {
t.Errorf("Expected tail=%d (filter start), got %d", start, tracker.tail)
}
}
// TestBackfillWithConcurrentAppends tests backfill while appends are happening
func TestBackfillWithConcurrentAppends(t *testing.T) {
var (
ctx = t.Context()
filter = &BlockFilter{StartSlot: -20, EndSlot: 0}
head = makeBlockInfo(100, 1)
tracker = NewCommitmentTracker(filter, head)
mockData = make(map[uint64]*BlockCommitmentInfo)
wg sync.WaitGroup
)
// Create backfill data
for slot := uint64(85); slot < 100; slot++ {
info := makeBlockInfo(slot, 1)
mockData[slot] = &info
}
// Slow fetcher to allow concurrent appends
slowFetcher := &mockFetcher{data: mockData}
// Start backfill in background
wg.Go(func() {
tracker.Backfill(ctx, slowFetcher)
})
// Give backfill time to start
time.Sleep(10 * time.Millisecond)
// Concurrently append new slots
wg.Go(func() {
for slot := uint64(101); slot <= 110; slot++ {
tracker.Append(slot, [32]byte{byte(slot)}, [][]byte{[]byte(fmt.Sprintf("c-%d", slot))})
time.Sleep(5 * time.Millisecond)
}
})
wg.Wait()
// Verify final state is consistent
if tracker.head != 110 {
t.Errorf("Expected head=110, got %d", tracker.head)
}
// Tail should be somewhere reasonable (backfill may have stopped early due to head advancement)
if tracker.tail > 100 {
t.Errorf("Expected tail <= 100, got %d", tracker.tail)
}
}
// TestConcurrentAppendAndGet tests concurrent appends and gets
func TestConcurrentAppendAndGet(t *testing.T) {
var (
filter = &BlockFilter{StartSlot: -100, EndSlot: 0}
head = makeBlockInfo(1000, 2)
tracker = NewCommitmentTracker(filter, head)
wg sync.WaitGroup
)
// Writer goroutine
wg.Go(func() {
for slot := uint64(1001); slot <= 1100; slot++ {
tracker.Append(slot, [32]byte{byte(slot)}, [][]byte{[]byte(fmt.Sprintf("c-%d", slot))})
}
})
// Reader goroutines
for range 3 {
wg.Go(func() {
for j := range 50 {
tracker.Get(1000 + uint64(j))
time.Sleep(time.Millisecond)
}
})
}
wg.Wait()
// Verify final state
if tracker.head != 1100 {
t.Errorf("Expected head=1100, got %d", tracker.head)
}
}
// TestConcurrentUndoAndAppend tests concurrent undo and append operations
func TestConcurrentUndoAndAppend(t *testing.T) {
var (
filter = &BlockFilter{StartSlot: -50, EndSlot: 0}
head = makeBlockInfo(100, 2)
tracker = NewCommitmentTracker(filter, head)
wg sync.WaitGroup
)
// Pre-populate some entries
for slot := uint64(101); slot <= 110; slot++ {
tracker.Append(slot, [32]byte{byte(slot)}, [][]byte{[]byte(fmt.Sprintf("c-%d", slot))})
}
// Undo some entries
wg.Go(func() {
for range 5 {
tracker.Undo()
time.Sleep(5 * time.Millisecond)
}
})
// Wait for undos to complete
wg.Wait()
// Verify head moved back
if tracker.head != 105 {
t.Errorf("Expected head=105 after 5 undos, got %d", tracker.head)
}
// Now append more
for slot := uint64(106); slot <= 115; slot++ {
tracker.Append(slot, [32]byte{byte(slot)}, [][]byte{[]byte(fmt.Sprintf("c-%d", slot))})
}
if tracker.head != 115 {
t.Errorf("Expected head=115 after re-appending, got %d", tracker.head)
}
}
// TestCapacity tests the Capacity method
func TestCapacity(t *testing.T) {
var (
filter = &BlockFilter{StartSlot: -10, EndSlot: 0}
head = makeBlockInfo(100, 2)
tracker = NewCommitmentTracker(filter, head)
expectedCapacity = filter.RequiredBufferSize()
)
if tracker.Capacity() != expectedCapacity {
t.Errorf("Expected capacity=%d, got %d", expectedCapacity, tracker.Capacity())
}
}
// TestCommitmentDeepCopy verifies that commitments are deep copied on append
func TestCommitmentDeepCopy(t *testing.T) {
var (
filter = &BlockFilter{StartSlot: -10, EndSlot: 0}
head = makeBlockInfo(100, 1)
tracker = NewCommitmentTracker(filter, head)
original = [][]byte{[]byte("original-commitment")}
)
// Append
tracker.Append(101, [32]byte{101}, original)
// Modify original
original[0][0] = 'X'
// Retrieve and verify it wasn't modified
info, ok := tracker.Get(101)
if !ok {
t.Fatal("Should find slot 101")
}
if info.BlobCommitments[0][0] == 'X' {
t.Error("Commitment was not deep copied, modification affected stored value")
}
if string(info.BlobCommitments[0]) != "original-commitment" {
t.Errorf("Expected 'original-commitment', got %s", string(info.BlobCommitments[0]))
}
}
// TestBufferWrappingPreservesData tests that wrapping doesn't corrupt data
func TestBufferWrappingPreservesData(t *testing.T) {
var (
filter = &BlockFilter{StartSlot: -2, EndSlot: 0} // Buffer size = 3 (-2 + 1 = 3)
head = makeBlockInfo(100, 1)
tracker = NewCommitmentTracker(filter, head)
)
// Fill buffer: 100, 101, 102
for slot := uint64(101); slot <= 102; slot++ {
tracker.Append(slot, [32]byte{byte(slot)}, [][]byte{[]byte(fmt.Sprintf("c-%d", slot))})
}
// Now buffer is full [100, 101, 102]
// Append 103 - should wrap and overwrite 100
tracker.Append(103, [32]byte{103}, [][]byte{[]byte("c-103")})
// Tail should now be 101
if tracker.tail != 101 {
t.Errorf("Expected tail=101 after wrap, got %d", tracker.tail)
}
// Slot 100 should not be accessible
_, ok := tracker.Get(100)
if ok {
t.Error("Slot 100 should not be accessible after wrapping")
}
// Slots 101-103 should be accessible
for slot := uint64(101); slot <= 103; slot++ {
info, ok := tracker.Get(slot)
if !ok {
t.Errorf("Should find slot %d", slot)
}
if info.Slot != slot {
t.Errorf("Slot mismatch: expected %d, got %d", slot, info.Slot)
}
}
}
// TestBackfillErrorPropagation tests that fetcher errors are properly propagated
func TestBackfillErrorPropagation(t *testing.T) {
var (
ctx = t.Context()
filter = &BlockFilter{StartSlot: -10, EndSlot: 0}
head = makeBlockInfo(100, 1)
tracker = NewCommitmentTracker(filter, head)
expectedErr = errors.New("network timeout")
fetcher = &mockFetcher{err: expectedErr}
)
err := tracker.Backfill(ctx, fetcher)
if err == nil {
t.Fatal("Expected backfill to return error")
}
// Should wrap the error
if !errors.Is(err, expectedErr) {
t.Errorf("Expected error to contain network timeout, got: %v", err)
}
}