-
Notifications
You must be signed in to change notification settings - Fork 268
Expand file tree
/
Copy pathsync_test.go
More file actions
1010 lines (836 loc) · 36.5 KB
/
Copy pathsync_test.go
File metadata and controls
1010 lines (836 loc) · 36.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
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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package block
import (
"context"
"errors"
"fmt"
"sync"
"sync/atomic"
"testing"
"time"
goheaderstore "github.com/celestiaorg/go-header/store"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/evstack/ev-node/pkg/cache"
"github.com/evstack/ev-node/pkg/config"
"github.com/evstack/ev-node/pkg/genesis"
"github.com/evstack/ev-node/pkg/store"
"github.com/evstack/ev-node/test/mocks"
"github.com/evstack/ev-node/types"
)
// setupManagerForSyncLoopTest initializes a Manager instance suitable for SyncLoop testing.
func setupManagerForSyncLoopTest(t *testing.T, initialState types.State) (
*Manager,
*mocks.MockStore,
*mocks.MockExecutor,
context.Context,
context.CancelFunc,
chan NewHeaderEvent,
chan NewDataEvent,
*uint64,
) {
t.Helper()
mockStore := mocks.NewMockStore(t)
mockExec := mocks.NewMockExecutor(t)
headerInCh := make(chan NewHeaderEvent, 10)
dataInCh := make(chan NewDataEvent, 10)
headerStoreCh := make(chan struct{}, 1)
dataStoreCh := make(chan struct{}, 1)
retrieveCh := make(chan struct{}, 1)
cfg := config.DefaultConfig
cfg.DA.BlockTime.Duration = 100 * time.Millisecond
cfg.Node.BlockTime.Duration = 50 * time.Millisecond
genesisDoc := &genesis.Genesis{ChainID: "syncLoopTest"}
// Manager setup
m := &Manager{
store: mockStore,
exec: mockExec,
config: cfg,
genesis: *genesisDoc,
lastState: initialState,
lastStateMtx: new(sync.RWMutex),
logger: zerolog.Nop(),
headerCache: cache.NewCache[types.SignedHeader](),
dataCache: cache.NewCache[types.Data](),
headerInCh: headerInCh,
dataInCh: dataInCh,
headerStoreCh: headerStoreCh,
dataStoreCh: dataStoreCh,
retrieveCh: retrieveCh,
daHeight: &atomic.Uint64{},
metrics: NopMetrics(),
headerStore: &goheaderstore.Store[*types.SignedHeader]{},
dataStore: &goheaderstore.Store[*types.Data]{},
signaturePayloadProvider: types.DefaultSignaturePayloadProvider,
validatorHasherProvider: types.DefaultValidatorHasherProvider,
}
m.daHeight.Store(initialState.DAHeight)
ctx, cancel := context.WithCancel(context.Background())
currentMockHeight := initialState.LastBlockHeight
heightPtr := ¤tMockHeight
mockStore.On("Height", mock.Anything).Return(func(context.Context) uint64 {
return *heightPtr
}, nil).Maybe()
return m, mockStore, mockExec, ctx, cancel, headerInCh, dataInCh, heightPtr
}
// TestSyncLoop_ProcessSingleBlock_HeaderFirst verifies that the sync loop processes a single block when the header arrives before the data.
// 1. Header for H+1 arrives.
// 2. Data for H+1 arrives.
// 3. Block H+1 is successfully validated, applied, and committed.
// 4. State is updated.
// 5. Caches are cleared.
func TestSyncLoop_ProcessSingleBlock_HeaderFirst(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
initialHeight := uint64(10)
initialState := types.State{
LastBlockHeight: initialHeight,
AppHash: []byte("initial_app_hash"),
ChainID: "syncLoopTest",
DAHeight: 5,
}
newHeight := initialHeight + 1
daHeight := initialState.DAHeight
m, mockStore, mockExec, ctx, cancel, headerInCh, dataInCh, _ := setupManagerForSyncLoopTest(t, initialState)
defer cancel()
// Create test block data
header, data, privKey := types.GenerateRandomBlockCustomWithAppHash(&types.BlockConfig{Height: newHeight, NTxs: 2}, initialState.ChainID, initialState.AppHash)
require.NotNil(header)
require.NotNil(data)
require.NotNil(privKey)
expectedNewAppHash := []byte("new_app_hash")
expectedNewState, err := initialState.NextState(header.Header, expectedNewAppHash)
require.NoError(err)
syncChan := make(chan struct{})
var txs [][]byte
for _, tx := range data.Txs {
txs = append(txs, tx)
}
mockExec.On("ExecuteTxs", mock.Anything, txs, newHeight, header.Time(), initialState.AppHash).
Return(expectedNewAppHash, uint64(100), nil).Once()
mockStore.On("SaveBlockData", mock.Anything, header, data, &header.Signature).Return(nil).Once()
mockStore.On("UpdateState", mock.Anything, expectedNewState).Return(nil).Run(func(args mock.Arguments) { close(syncChan) }).Once()
mockStore.On("SetHeight", mock.Anything, newHeight).Return(nil).Once()
// Add expectations for DA inclusion metadata storage
// These calls happen in storeDAInclusionMetadata when syncing blocks
headerKey := fmt.Sprintf("%s/%d/h", store.HeightToDAHeightKey, newHeight)
dataKey := fmt.Sprintf("%s/%d/d", store.HeightToDAHeightKey, newHeight)
mockStore.On("SetMetadata", mock.Anything, headerKey, mock.Anything).Return(nil).Once()
mockStore.On("SetMetadata", mock.Anything, dataKey, mock.Anything).Return(nil).Once()
ctx, loopCancel := context.WithTimeout(context.Background(), 1*time.Second)
defer loopCancel()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
m.SyncLoop(ctx, make(chan<- error))
}()
t.Logf("Sending header event for height %d", newHeight)
headerInCh <- NewHeaderEvent{Header: header, DAHeight: daHeight}
t.Logf("Sending data event for height %d", newHeight)
dataInCh <- NewDataEvent{Data: data, DAHeight: daHeight}
t.Log("Waiting for sync to complete...")
wg.Wait()
select {
case <-syncChan:
t.Log("Sync completed.")
case <-time.After(2 * time.Second):
t.Fatal("Timeout waiting for sync to complete")
}
mockStore.AssertExpectations(t)
mockExec.AssertExpectations(t)
finalState := m.GetLastState()
assert.Equal(expectedNewState.LastBlockHeight, finalState.LastBlockHeight)
assert.Equal(expectedNewState.AppHash, finalState.AppHash)
assert.Equal(expectedNewState.LastBlockTime, finalState.LastBlockTime)
assert.Equal(expectedNewState.DAHeight, finalState.DAHeight)
// Assert caches are cleared for the processed height
assert.Nil(m.headerCache.GetItem(newHeight), "Header cache should be cleared for processed height")
assert.Nil(m.dataCache.GetItem(newHeight), "Data cache should be cleared for processed height")
}
// TestSyncLoop_ProcessSingleBlock_DataFirst verifies that the sync loop processes a single block when the data arrives before the header.
// 1. Data for H+1 arrives.
// 2. Header for H+1 arrives.
// 3. Block H+1 is successfully validated, applied, and committed.
// 4. State is updated.
// 5. Caches are cleared.
func TestSyncLoop_ProcessSingleBlock_DataFirst(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
initialHeight := uint64(20)
initialState := types.State{
LastBlockHeight: initialHeight,
AppHash: []byte("initial_app_hash_data_first"),
ChainID: "syncLoopTest",
DAHeight: 15,
}
newHeight := initialHeight + 1
daHeight := initialState.DAHeight
m, mockStore, mockExec, ctx, cancel, headerInCh, dataInCh, _ := setupManagerForSyncLoopTest(t, initialState)
defer cancel()
// Create test block data
header, data, privKey := types.GenerateRandomBlockCustomWithAppHash(&types.BlockConfig{Height: newHeight, NTxs: 3}, initialState.ChainID, initialState.AppHash)
require.NotNil(header)
require.NotNil(data)
require.NotNil(privKey)
expectedNewAppHash := []byte("new_app_hash_data_first")
expectedNewState, err := initialState.NextState(header.Header, expectedNewAppHash)
require.NoError(err)
syncChan := make(chan struct{})
var txs [][]byte
for _, tx := range data.Txs {
txs = append(txs, tx)
}
mockExec.On("ExecuteTxs", mock.Anything, txs, newHeight, header.Time(), initialState.AppHash).
Return(expectedNewAppHash, uint64(100), nil).Once()
mockStore.On("SaveBlockData", mock.Anything, header, data, &header.Signature).Return(nil).Once()
mockStore.On("UpdateState", mock.Anything, expectedNewState).Return(nil).Run(func(args mock.Arguments) { close(syncChan) }).Once()
mockStore.On("SetHeight", mock.Anything, newHeight).Return(nil).Once()
// Add expectations for DA inclusion metadata storage
// These calls happen in storeDAInclusionMetadata when syncing blocks
headerKey := fmt.Sprintf("%s/%d/h", store.HeightToDAHeightKey, newHeight)
dataKey := fmt.Sprintf("%s/%d/d", store.HeightToDAHeightKey, newHeight)
mockStore.On("SetMetadata", mock.Anything, headerKey, mock.Anything).Return(nil).Once()
mockStore.On("SetMetadata", mock.Anything, dataKey, mock.Anything).Return(nil).Once()
ctx, loopCancel := context.WithTimeout(context.Background(), 1*time.Second)
defer loopCancel()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
m.SyncLoop(ctx, make(chan<- error))
}()
t.Logf("Sending data event for height %d", newHeight)
dataInCh <- NewDataEvent{Data: data, DAHeight: daHeight}
t.Logf("Sending header event for height %d", newHeight)
headerInCh <- NewHeaderEvent{Header: header, DAHeight: daHeight}
t.Log("Waiting for sync to complete...")
wg.Wait()
select {
case <-syncChan:
t.Log("Sync completed.")
case <-time.After(2 * time.Second):
t.Fatal("Timeout waiting for sync to complete")
}
mockStore.AssertExpectations(t)
mockExec.AssertExpectations(t)
finalState := m.GetLastState()
assert.Equal(expectedNewState.LastBlockHeight, finalState.LastBlockHeight)
assert.Equal(expectedNewState.AppHash, finalState.AppHash)
assert.Equal(expectedNewState.LastBlockTime, finalState.LastBlockTime)
assert.Equal(expectedNewState.DAHeight, finalState.DAHeight)
// Assert caches are cleared for the processed height
assert.Nil(m.headerCache.GetItem(newHeight), "Header cache should be cleared for processed height")
assert.Nil(m.dataCache.GetItem(newHeight), "Data cache should be cleared for processed height")
}
// TestSyncLoop_ProcessMultipleBlocks_Sequentially verifies that the sync loop processes multiple blocks arriving in order.
// 1. Events for H+1 arrive (header then data).
// 2. Block H+1 is processed.
// 3. Events for H+2 arrive (header then data).
// 4. Block H+2 is processed.
// 5. Final state is H+2.
func TestSyncLoop_ProcessMultipleBlocks_Sequentially(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
initialHeight := uint64(30)
initialState := types.State{
LastBlockHeight: initialHeight,
AppHash: []byte("initial_app_hash_multi"),
ChainID: "syncLoopTest",
DAHeight: 25,
}
heightH1 := initialHeight + 1
heightH2 := initialHeight + 2
daHeight := initialState.DAHeight
m, mockStore, mockExec, ctx, cancel, headerInCh, dataInCh, heightPtr := setupManagerForSyncLoopTest(t, initialState)
defer cancel()
// --- Block H+1 Data ---
headerH1, dataH1, privKeyH1 := types.GenerateRandomBlockCustomWithAppHash(&types.BlockConfig{Height: heightH1, NTxs: 1}, initialState.ChainID, initialState.AppHash)
require.NotNil(headerH1)
require.NotNil(dataH1)
require.NotNil(privKeyH1)
expectedNewAppHashH1 := []byte("app_hash_h1")
expectedNewStateH1, err := initialState.NextState(headerH1.Header, expectedNewAppHashH1)
require.NoError(err)
var txsH1 [][]byte
for _, tx := range dataH1.Txs {
txsH1 = append(txsH1, tx)
}
// --- Block H+2 Data ---
headerH2, dataH2, privKeyH2 := types.GenerateRandomBlockCustomWithAppHash(&types.BlockConfig{Height: heightH2, NTxs: 2}, initialState.ChainID, expectedNewAppHashH1)
require.NotNil(headerH2)
require.NotNil(dataH2)
require.NotNil(privKeyH2)
expectedNewAppHashH2 := []byte("app_hash_h2")
expectedNewStateH2, err := expectedNewStateH1.NextState(headerH2.Header, expectedNewAppHashH2)
require.NoError(err)
var txsH2 [][]byte
for _, tx := range dataH2.Txs {
txsH2 = append(txsH2, tx)
}
syncChanH1 := make(chan struct{})
syncChanH2 := make(chan struct{})
// --- Mock Expectations for H+1 ---
mockExec.On("ExecuteTxs", mock.Anything, txsH1, heightH1, headerH1.Time(), initialState.AppHash).
Return(expectedNewAppHashH1, uint64(100), nil).Once()
mockStore.On("SaveBlockData", mock.Anything, headerH1, dataH1, &headerH1.Signature).Return(nil).Once()
mockStore.On("UpdateState", mock.Anything, expectedNewStateH1).Return(nil).Run(func(args mock.Arguments) { close(syncChanH1) }).Once()
mockStore.On("SetHeight", mock.Anything, heightH1).Return(nil).
Run(func(args mock.Arguments) {
newHeight := args.Get(1).(uint64)
*heightPtr = newHeight // Update the mocked height
t.Logf("Mock SetHeight called for H+1, updated mock height to %d", newHeight)
}).
Once()
// Add expectations for DA inclusion metadata storage for H+1
headerKeyH1 := fmt.Sprintf("%s/%d/h", store.HeightToDAHeightKey, heightH1)
dataKeyH1 := fmt.Sprintf("%s/%d/d", store.HeightToDAHeightKey, heightH1)
mockStore.On("SetMetadata", mock.Anything, headerKeyH1, mock.Anything).Return(nil).Once()
mockStore.On("SetMetadata", mock.Anything, dataKeyH1, mock.Anything).Return(nil).Once()
// --- Mock Expectations for H+2 ---
mockExec.On("ExecuteTxs", mock.Anything, txsH2, heightH2, headerH2.Time(), expectedNewAppHashH1).
Return(expectedNewAppHashH2, uint64(100), nil).Once()
mockStore.On("SaveBlockData", mock.Anything, headerH2, dataH2, &headerH2.Signature).Return(nil).Once()
mockStore.On("UpdateState", mock.Anything, expectedNewStateH2).Return(nil).Run(func(args mock.Arguments) { close(syncChanH2) }).Once()
mockStore.On("SetHeight", mock.Anything, heightH2).Return(nil).
Run(func(args mock.Arguments) {
newHeight := args.Get(1).(uint64)
*heightPtr = newHeight // Update the mocked height
t.Logf("Mock SetHeight called for H+2, updated mock height to %d", newHeight)
}).
Once()
// Add expectations for DA inclusion metadata storage for H+2
headerKeyH2 := fmt.Sprintf("%s/%d/h", store.HeightToDAHeightKey, heightH2)
dataKeyH2 := fmt.Sprintf("%s/%d/d", store.HeightToDAHeightKey, heightH2)
mockStore.On("SetMetadata", mock.Anything, headerKeyH2, mock.Anything).Return(nil).Once()
mockStore.On("SetMetadata", mock.Anything, dataKeyH2, mock.Anything).Return(nil).Once()
ctx, loopCancel := context.WithTimeout(context.Background(), 1*time.Second)
defer loopCancel()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
m.SyncLoop(ctx, make(chan<- error))
t.Log("SyncLoop exited.")
}()
// --- Process H+1 ---
headerInCh <- NewHeaderEvent{Header: headerH1, DAHeight: daHeight}
dataInCh <- NewDataEvent{Data: dataH1, DAHeight: daHeight}
t.Log("Waiting for Sync H+1 to complete...")
select {
case <-syncChanH1:
t.Log("Sync H+1 completed.")
case <-time.After(2 * time.Second):
t.Fatal("Timeout waiting for sync H+1 to complete")
}
// --- Process H+2 ---
headerInCh <- NewHeaderEvent{Header: headerH2, DAHeight: daHeight}
dataInCh <- NewDataEvent{Data: dataH2, DAHeight: daHeight}
select {
case <-syncChanH2:
t.Log("Sync H+2 completed.")
case <-time.After(2 * time.Second):
t.Fatal("Timeout waiting for sync H+2 to complete")
}
t.Log("Waiting for SyncLoop H+2 to complete...")
wg.Wait()
mockStore.AssertExpectations(t)
mockExec.AssertExpectations(t)
finalState := m.GetLastState()
assert.Equal(expectedNewStateH2.LastBlockHeight, finalState.LastBlockHeight)
assert.Equal(expectedNewStateH2.AppHash, finalState.AppHash)
assert.Equal(expectedNewStateH2.LastBlockTime, finalState.LastBlockTime)
assert.Equal(expectedNewStateH2.DAHeight, finalState.DAHeight)
assert.Nil(m.headerCache.GetItem(heightH1), "Header cache should be cleared for H+1")
assert.Nil(m.dataCache.GetItem(heightH1), "Data cache should be cleared for H+1")
assert.Nil(m.headerCache.GetItem(heightH2), "Header cache should be cleared for H+2")
assert.Nil(m.dataCache.GetItem(heightH2), "Data cache should be cleared for H+2")
}
// TestSyncLoop_ProcessBlocks_OutOfOrderArrival verifies that the sync loop can handle blocks arriving out of order.
// 1. Events for H+2 arrive (header then data). Block H+2 is cached.
// 2. Events for H+1 arrive (header then data).
// 3. Block H+1 is processed.
// 4. Block H+2 is processed immediately after H+1 from the cache.
// 5. Final state is H+2.
func TestSyncLoop_ProcessBlocks_OutOfOrderArrival(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
initialHeight := uint64(40)
initialState := types.State{
LastBlockHeight: initialHeight,
AppHash: []byte("initial_app_hash_ooo"),
ChainID: "syncLoopTest",
DAHeight: 35,
}
heightH1 := initialHeight + 1
heightH2 := initialHeight + 2
daHeight := initialState.DAHeight
m, mockStore, mockExec, ctx, cancel, headerInCh, dataInCh, heightPtr := setupManagerForSyncLoopTest(t, initialState)
defer cancel()
// --- Block H+1 Data ---
headerH1, dataH1, privKeyH1 := types.GenerateRandomBlockCustomWithAppHash(&types.BlockConfig{Height: heightH1, NTxs: 1}, initialState.ChainID, initialState.AppHash)
require.NotNil(headerH1)
require.NotNil(dataH1)
require.NotNil(privKeyH1)
appHashH1 := []byte("app_hash_h1_ooo")
expectedNewStateH1, err := initialState.NextState(headerH1.Header, appHashH1)
require.NoError(err)
var txsH1 [][]byte
for _, tx := range dataH1.Txs {
txsH1 = append(txsH1, tx)
}
// --- Block H+2 Data ---
headerH2, dataH2, privKeyH2 := types.GenerateRandomBlockCustomWithAppHash(&types.BlockConfig{Height: heightH2, NTxs: 2}, initialState.ChainID, appHashH1)
require.NotNil(headerH2)
require.NotNil(dataH2)
require.NotNil(privKeyH2)
appHashH2 := []byte("app_hash_h2_ooo")
expectedStateH2, err := expectedNewStateH1.NextState(headerH2.Header, appHashH2)
require.NoError(err)
var txsH2 [][]byte
for _, tx := range dataH2.Txs {
txsH2 = append(txsH2, tx)
}
syncChanH1 := make(chan struct{})
syncChanH2 := make(chan struct{})
// --- Mock Expectations for H+1 (will be called first despite arrival order) ---
mockStore.On("Height", mock.Anything).Return(initialHeight, nil).Maybe()
mockExec.On("Validate", mock.Anything, &headerH1.Header, dataH1).Return(nil).Maybe()
mockExec.On("ExecuteTxs", mock.Anything, txsH1, heightH1, headerH1.Time(), initialState.AppHash).
Return(appHashH1, uint64(100), nil).Once()
mockStore.On("SaveBlockData", mock.Anything, headerH1, dataH1, &headerH1.Signature).Return(nil).Once()
mockStore.On("UpdateState", mock.Anything, expectedNewStateH1).Return(nil).
Run(func(args mock.Arguments) { close(syncChanH1) }).
Once()
mockStore.On("SetHeight", mock.Anything, heightH1).Return(nil).
Run(func(args mock.Arguments) {
newHeight := args.Get(1).(uint64)
*heightPtr = newHeight // Update the mocked height
t.Logf("Mock SetHeight called for H+1, updated mock height to %d", newHeight)
}).
Once()
// Add expectations for DA inclusion metadata storage for H+1
headerKeyH1 := fmt.Sprintf("%s/%d/h", store.HeightToDAHeightKey, heightH1)
dataKeyH1 := fmt.Sprintf("%s/%d/d", store.HeightToDAHeightKey, heightH1)
mockStore.On("SetMetadata", mock.Anything, headerKeyH1, mock.Anything).Return(nil).Once()
mockStore.On("SetMetadata", mock.Anything, dataKeyH1, mock.Anything).Return(nil).Once()
// --- Mock Expectations for H+2 (will be called second) ---
mockStore.On("Height", mock.Anything).Return(heightH1, nil).Maybe()
mockExec.On("Validate", mock.Anything, &headerH2.Header, dataH2).Return(nil).Maybe()
mockExec.On("ExecuteTxs", mock.Anything, txsH2, heightH2, headerH2.Time(), expectedNewStateH1.AppHash).
Return(appHashH2, uint64(1), nil).Once()
mockStore.On("SaveBlockData", mock.Anything, headerH2, dataH2, &headerH2.Signature).Return(nil).Once()
mockStore.On("SetHeight", mock.Anything, heightH2).Return(nil).
Run(func(args mock.Arguments) {
newHeight := args.Get(1).(uint64)
*heightPtr = newHeight // Update the mocked height
t.Logf("Mock SetHeight called for H+2, updated mock height to %d", newHeight)
}).
Once()
mockStore.On("UpdateState", mock.Anything, expectedStateH2).Return(nil).
Run(func(args mock.Arguments) { close(syncChanH2) }).
Once()
// Add expectations for DA inclusion metadata storage for H+2
headerKeyH2 := fmt.Sprintf("%s/%d/h", store.HeightToDAHeightKey, heightH2)
dataKeyH2 := fmt.Sprintf("%s/%d/d", store.HeightToDAHeightKey, heightH2)
mockStore.On("SetMetadata", mock.Anything, headerKeyH2, mock.Anything).Return(nil).Once()
mockStore.On("SetMetadata", mock.Anything, dataKeyH2, mock.Anything).Return(nil).Once()
ctx, loopCancel := context.WithTimeout(context.Background(), 1*time.Second)
defer loopCancel()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
m.SyncLoop(ctx, make(chan<- error))
t.Log("SyncLoop exited.")
}()
// --- Send H+2 Events First ---
headerInCh <- NewHeaderEvent{Header: headerH2, DAHeight: daHeight}
dataInCh <- NewDataEvent{Data: dataH2, DAHeight: daHeight}
// Wait for H+2 to be cached (but not processed since H+1 is missing)
require.Eventually(func() bool {
return m.headerCache.GetItem(heightH2) != nil && m.dataCache.GetItem(heightH2) != nil
}, 1*time.Second, 10*time.Millisecond, "H+2 header and data should be cached")
assert.Equal(initialHeight, m.GetLastState().LastBlockHeight, "Height should not have advanced yet")
assert.NotNil(m.headerCache.GetItem(heightH2), "Header H+2 should be in cache")
assert.NotNil(m.dataCache.GetItem(heightH2), "Data H+2 should be in cache")
// --- Send H+1 Events Second ---
headerInCh <- NewHeaderEvent{Header: headerH1, DAHeight: daHeight}
dataInCh <- NewDataEvent{Data: dataH1, DAHeight: daHeight}
t.Log("Waiting for Sync H+1 to complete...")
// --- Wait for Processing (H+1 then H+2) ---
select {
case <-syncChanH1:
t.Log("Sync H+1 completed.")
case <-time.After(2 * time.Second):
t.Fatal("Timeout waiting for sync H+1 to complete")
}
t.Log("Waiting for SyncLoop H+2 to complete...")
select {
case <-syncChanH2:
t.Log("Sync H+2 completed.")
case <-time.After(2 * time.Second):
t.Fatal("Timeout waiting for sync H+2 to complete")
}
wg.Wait()
mockStore.AssertExpectations(t)
mockExec.AssertExpectations(t)
finalState := m.GetLastState()
assert.Equal(expectedStateH2.LastBlockHeight, finalState.LastBlockHeight)
assert.Equal(expectedStateH2.AppHash, finalState.AppHash)
assert.Equal(expectedStateH2.LastBlockTime, finalState.LastBlockTime)
assert.Equal(expectedStateH2.DAHeight, finalState.DAHeight)
assert.Nil(m.headerCache.GetItem(heightH1), "Header cache should be cleared for H+1")
assert.Nil(m.dataCache.GetItem(heightH1), "Data cache should be cleared for H+1")
assert.Nil(m.headerCache.GetItem(heightH2), "Header cache should be cleared for H+2")
assert.Nil(m.dataCache.GetItem(heightH2), "Data cache should be cleared for H+2")
}
// TestSyncLoop_IgnoreDuplicateEvents verifies that the SyncLoop correctly processes
// a block once even if the header and data events are received multiple times.
func TestSyncLoop_IgnoreDuplicateEvents(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
initialHeight := uint64(40)
initialState := types.State{
LastBlockHeight: initialHeight,
AppHash: []byte("initial_app_hash_dup"),
ChainID: "syncLoopTest",
DAHeight: 35,
}
heightH1 := initialHeight + 1
daHeight := initialState.DAHeight
m, mockStore, mockExec, ctx, cancel, headerInCh, dataInCh, _ := setupManagerForSyncLoopTest(t, initialState)
defer cancel()
// --- Block H+1 Data ---
headerH1, dataH1, privKeyH1 := types.GenerateRandomBlockCustomWithAppHash(&types.BlockConfig{Height: heightH1, NTxs: 1}, initialState.ChainID, initialState.AppHash)
require.NotNil(headerH1)
require.NotNil(dataH1)
require.NotNil(privKeyH1)
appHashH1 := []byte("app_hash_h1_dup")
expectedStateH1, err := initialState.NextState(headerH1.Header, appHashH1)
require.NoError(err)
var txsH1 [][]byte
for _, tx := range dataH1.Txs {
txsH1 = append(txsH1, tx)
}
syncChanH1 := make(chan struct{})
// --- Mock Expectations (Expect processing exactly ONCE) ---
mockExec.On("ExecuteTxs", mock.Anything, txsH1, heightH1, headerH1.Time(), initialState.AppHash).
Return(appHashH1, uint64(1), nil).Once()
mockStore.On("SaveBlockData", mock.Anything, headerH1, dataH1, &headerH1.Signature).Return(nil).Once()
mockStore.On("SetHeight", mock.Anything, heightH1).Return(nil).Once()
mockStore.On("UpdateState", mock.Anything, expectedStateH1).Return(nil).
Run(func(args mock.Arguments) { close(syncChanH1) }).
Once()
// Add expectations for DA inclusion metadata storage
// These calls happen in storeDAInclusionMetadata when syncing blocks
headerKey := fmt.Sprintf("%s/%d/h", store.HeightToDAHeightKey, heightH1)
dataKey := fmt.Sprintf("%s/%d/d", store.HeightToDAHeightKey, heightH1)
mockStore.On("SetMetadata", mock.Anything, headerKey, mock.Anything).Return(nil).Once()
mockStore.On("SetMetadata", mock.Anything, dataKey, mock.Anything).Return(nil).Once()
ctx, loopCancel := context.WithTimeout(context.Background(), 1*time.Second)
defer loopCancel()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
m.SyncLoop(ctx, make(chan<- error))
t.Log("SyncLoop exited.")
}()
// --- Send First Set of Events ---
headerInCh <- NewHeaderEvent{Header: headerH1, DAHeight: daHeight}
dataInCh <- NewDataEvent{Data: dataH1, DAHeight: daHeight}
t.Log("Waiting for first sync to complete...")
select {
case <-syncChanH1:
t.Log("First sync completed.")
case <-time.After(2 * time.Second):
t.Fatal("Timeout waiting for first sync to complete")
}
// --- Send Duplicate Events ---
headerInCh <- NewHeaderEvent{Header: headerH1, DAHeight: daHeight}
dataInCh <- NewDataEvent{Data: dataH1, DAHeight: daHeight}
// Give the sync loop a chance to process duplicates (if it would)
// Since we expect no processing, we just wait for the context timeout
// The mock expectations will fail if duplicates are processed
wg.Wait()
// Assertions
mockStore.AssertExpectations(t) // Crucial: verifies calls happened exactly once
mockExec.AssertExpectations(t) // Crucial: verifies calls happened exactly once
finalState := m.GetLastState()
assert.Equal(expectedStateH1.LastBlockHeight, finalState.LastBlockHeight)
assert.Equal(expectedStateH1.AppHash, finalState.AppHash)
assert.Equal(expectedStateH1.LastBlockTime, finalState.LastBlockTime)
assert.Equal(expectedStateH1.DAHeight, finalState.DAHeight)
// Assert caches are cleared
assert.Nil(m.headerCache.GetItem(heightH1), "Header cache should be cleared for H+1")
assert.Nil(m.dataCache.GetItem(heightH1), "Data cache should be cleared for H+1")
}
// TestSyncLoop_ErrorOnApplyError verifies that the SyncLoop halts if ApplyBlock fails.
// Halting after sync loop error is handled in full.go and is not tested here.
func TestSyncLoop_ErrorOnApplyError(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
initialHeight := uint64(50)
initialState := types.State{
LastBlockHeight: initialHeight,
AppHash: []byte("initial_app_hash_panic"),
ChainID: "syncLoopTest",
DAHeight: 45,
}
heightH1 := initialHeight + 1
daHeight := initialState.DAHeight
m, mockStore, mockExec, ctx, cancel, headerInCh, dataInCh, _ := setupManagerForSyncLoopTest(t, initialState)
defer cancel() // Ensure context cancellation happens even on panic
// --- Block H+1 Data ---
headerH1, dataH1, privKeyH1 := types.GenerateRandomBlockCustomWithAppHash(&types.BlockConfig{Height: heightH1, NTxs: 1}, initialState.ChainID, initialState.AppHash)
require.NotNil(headerH1)
require.NotNil(dataH1)
require.NotNil(privKeyH1)
var txsH1 [][]byte
for _, tx := range dataH1.Txs {
txsH1 = append(txsH1, tx)
}
applyErrorSignal := make(chan struct{})
applyError := errors.New("apply failed")
// --- Mock Expectations ---
mockExec.On("ExecuteTxs", mock.Anything, txsH1, heightH1, headerH1.Time(), initialState.AppHash).
Return(nil, uint64(100), applyError). // Return the error that should cause panic
Run(func(args mock.Arguments) { close(applyErrorSignal) }). // Signal *before* returning error
Once()
// NO further calls expected after Apply error
ctx, loopCancel := context.WithTimeout(context.Background(), 1*time.Second)
defer loopCancel()
var wg sync.WaitGroup
wg.Add(1)
errCh := make(chan error, 1)
go func() {
defer wg.Done()
m.SyncLoop(ctx, errCh)
}()
// --- Send Events ---
t.Logf("Sending header event for height %d", heightH1)
headerInCh <- NewHeaderEvent{Header: headerH1, DAHeight: daHeight}
t.Logf("Sending data event for height %d", heightH1)
dataInCh <- NewDataEvent{Data: dataH1, DAHeight: daHeight}
t.Log("Waiting for ApplyBlock error...")
select {
case <-applyErrorSignal:
t.Log("ApplyBlock error occurred.")
case <-time.After(2 * time.Second):
t.Fatal("Timeout waiting for ApplyBlock error signal")
}
require.Error(<-errCh, "SyncLoop should return an error when ApplyBlock errors")
wg.Wait()
mockStore.AssertExpectations(t)
mockExec.AssertExpectations(t)
finalState := m.GetLastState()
assert.Equal(initialState.LastBlockHeight, finalState.LastBlockHeight, "State height should not change after panic")
assert.Equal(initialState.AppHash, finalState.AppHash, "State AppHash should not change after panic")
assert.NotNil(m.headerCache.GetItem(heightH1), "Header cache should still contain item for H+1")
assert.NotNil(m.dataCache.GetItem(heightH1), "Data cache should still contain item for H+1")
}
// TestHandleEmptyDataHash tests that handleEmptyDataHash correctly handles the case where a header has an empty data hash, ensuring the data cache is updated with the previous block's data hash and metadata.
func TestHandleEmptyDataHash(t *testing.T) {
require := require.New(t)
ctx := context.Background()
// Mock store and data cache
store := mocks.NewMockStore(t)
dataCache := cache.NewCache[types.Data]()
// Setup the manager with the mock and data cache
m := &Manager{
store: store,
dataCache: dataCache,
}
// Define the test data
headerHeight := 2
header := &types.Header{
DataHash: dataHashForEmptyTxs,
BaseHeader: types.BaseHeader{
Height: 2,
Time: uint64(time.Now().UnixNano()),
},
}
// Mock data for the previous block
lastData := &types.Data{}
lastDataHash := lastData.Hash()
// header.DataHash equals dataHashForEmptyTxs and no error occurs
store.On("GetBlockData", ctx, uint64(headerHeight-1)).Return(nil, lastData, nil)
// Execute the method under test
m.handleEmptyDataHash(ctx, header)
// Assertions
store.AssertExpectations(t)
// make sure that the store has the correct data
d := dataCache.GetItem(header.Height())
require.NotNil(d)
require.Equal(d.LastDataHash, lastDataHash)
require.Equal(d.Metadata.ChainID, header.ChainID())
require.Equal(d.Metadata.Height, header.Height())
require.Equal(d.Metadata.Time, header.BaseHeader.Time)
}
// TestSyncLoop_MultipleHeadersArriveFirst_ThenData verifies that the sync loop correctly processes multiple blocks when all headers arrive first (without data), then their corresponding data arrives (or is handled as empty).
// 1. Headers for H+1 through H+5 arrive (no data yet).
// 2. State should not advance after only headers are received.
// 3. For non-empty blocks, data for H+1, H+2, H+4, and H+5 arrives in order and each block is processed as soon as both header and data are available.
// 4. For empty blocks (H+3 and H+4), no data is sent; the sync loop handles these using the empty data hash logic.
// 5. Final state is H+5, caches are cleared for all processed heights, and mocks are called as expected.
func TestSyncLoop_MultipleHeadersArriveFirst_ThenData(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
initialHeight := uint64(100)
initialState := types.State{
LastBlockHeight: initialHeight,
AppHash: []byte("initial_app_hash_multi_headers"),
ChainID: "syncLoopTest",
DAHeight: 95,
}
numBlocks := 5
blockHeights := make([]uint64, numBlocks)
for i := 0; i < numBlocks; i++ {
blockHeights[i] = initialHeight + uint64(i+1)
}
daHeight := initialState.DAHeight
// Use a real in-memory store
kv, err := store.NewDefaultInMemoryKVStore()
require.NoError(err)
store := store.New(kv)
mockExec := mocks.NewMockExecutor(t)
headerInCh := make(chan NewHeaderEvent, 5)
dataInCh := make(chan NewDataEvent, 5)
headers := make([]*types.SignedHeader, numBlocks)
data := make([]*types.Data, numBlocks)
privKeys := make([]any, numBlocks)
expectedAppHashes := make([][]byte, numBlocks)
expectedStates := make([]types.State, numBlocks)
txs := make([][][]byte, numBlocks)
syncChans := make([]chan struct{}, numBlocks)
prevAppHash := initialState.AppHash
prevState := initialState
// Save initial state in the store
require.NoError(store.UpdateState(context.Background(), initialState))
require.NoError(store.SetHeight(context.Background(), initialHeight))
for i := 0; i < numBlocks; i++ {
// Make blocks 2 and 3 (H+3 and H+4) empty
var nTxs int
if i == 2 || i == 3 {
nTxs = 0
} else {
nTxs = i + 1
}
headers[i], data[i], privKeys[i] = types.GenerateRandomBlockCustomWithAppHash(
&types.BlockConfig{Height: blockHeights[i], NTxs: nTxs},
prevState.ChainID, prevAppHash,
)
require.NotNil(headers[i])
require.NotNil(data[i])
txs[i] = make([][]byte, 0, len(data[i].Txs))
for _, tx := range data[i].Txs {
txs[i] = append(txs[i], tx)
}
expectedAppHashes[i] = fmt.Appendf(nil, "app_hash_h%d", i+1)
var err error
expectedStates[i], err = prevState.NextState(headers[i].Header, expectedAppHashes[i])
require.NoError(err)
syncChans[i] = make(chan struct{})
// Set up mocks for each block
mockExec.On("ExecuteTxs", mock.Anything, txs[i], blockHeights[i], headers[i].Time(), prevAppHash).
Return(expectedAppHashes[i], uint64(100), nil).Run(func(args mock.Arguments) {
close(syncChans[i])
}).Once()
prevAppHash = expectedAppHashes[i]
prevState = expectedStates[i]
}
m := &Manager{
store: store,
exec: mockExec,
config: config.DefaultConfig,
genesis: genesis.Genesis{ChainID: initialState.ChainID},
lastState: initialState,
lastStateMtx: new(sync.RWMutex),
logger: zerolog.Nop(),
headerCache: cache.NewCache[types.SignedHeader](),
dataCache: cache.NewCache[types.Data](),
headerInCh: headerInCh,
dataInCh: dataInCh,
daHeight: &atomic.Uint64{},
metrics: NopMetrics(),
signaturePayloadProvider: types.DefaultSignaturePayloadProvider,
validatorHasherProvider: types.DefaultValidatorHasherProvider,
}
m.daHeight.Store(initialState.DAHeight)
ctx, loopCancel := context.WithTimeout(context.Background(), 2*time.Second)
defer loopCancel()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
m.SyncLoop(ctx, make(chan<- error))
}()
// 1. Send all headers first (no data yet)
for i := 0; i < numBlocks; i++ {
headerInCh <- NewHeaderEvent{Header: headers[i], DAHeight: daHeight}
}
// 2. Wait for headers to be processed and cached by polling the cache state
// This replaces the flaky time.Sleep(50 * time.Millisecond)
require.Eventually(func() bool {
// Check that all headers are cached
for i := 0; i < numBlocks; i++ {
if m.headerCache.GetItem(blockHeights[i]) == nil {
return false
}
}
// Check that empty blocks have data cached, non-empty blocks don't yet
for i := 0; i < numBlocks; i++ {
dataInCache := m.dataCache.GetItem(blockHeights[i]) != nil
if i == 2 || i == 3 {
// Empty blocks should have data in cache
if !dataInCache {
return false
}
} else {
// Non-empty blocks should not have data in cache yet
if dataInCache {
return false
}
}
}
return true
}, 1*time.Second, 10*time.Millisecond, "Headers should be cached and empty blocks should have data cached")
assert.Equal(initialHeight, m.GetLastState().LastBlockHeight, "Height should not have advanced yet after only headers")
for i := 0; i < numBlocks; i++ {
if i == 2 || i == 3 {
assert.NotNil(m.dataCache.GetItem(blockHeights[i]), "Data should be in cache for empty block H+%d", i+1)
} else {
assert.Nil(m.dataCache.GetItem(blockHeights[i]), "Data should not be in cache for H+%d yet", i+1)
}
assert.NotNil(m.headerCache.GetItem(blockHeights[i]), "Header should be in cache for H+%d", i+1)
}
// 3. Send data for each block in order (skip empty blocks)
for i := 0; i < numBlocks; i++ {
if i == 2 || i == 3 {
// Do NOT send data for empty blocks
continue
}
dataInCh <- NewDataEvent{Data: data[i], DAHeight: daHeight}
// Wait for block to be processed
select {
case <-syncChans[i]:
// processed
case <-time.After(2 * time.Second):
t.Fatalf("Timeout waiting for sync of H+%d", i+1)
}
}
wg.Wait()
mockExec.AssertExpectations(t)
finalState := m.GetLastState()
assert.Equal(expectedStates[numBlocks-1].LastBlockHeight, finalState.LastBlockHeight)