-
Notifications
You must be signed in to change notification settings - Fork 179
/
engine_test.go
828 lines (684 loc) · 28 KB
/
engine_test.go
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
package ingestion
import (
"context"
"math/rand"
"os"
"sync"
"testing"
"time"
"github.com/dgraph-io/badger/v2"
"github.com/rs/zerolog"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
hotmodel "github.com/onflow/flow-go/consensus/hotstuff/model"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/model/flow/filter"
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/module/component"
"github.com/onflow/flow-go/module/counters"
downloadermock "github.com/onflow/flow-go/module/executiondatasync/execution_data/mock"
"github.com/onflow/flow-go/module/irrecoverable"
"github.com/onflow/flow-go/module/mempool/stdmap"
"github.com/onflow/flow-go/module/metrics"
modulemock "github.com/onflow/flow-go/module/mock"
"github.com/onflow/flow-go/module/signature"
"github.com/onflow/flow-go/module/state_synchronization/indexer"
"github.com/onflow/flow-go/network/channels"
"github.com/onflow/flow-go/network/mocknetwork"
protocol "github.com/onflow/flow-go/state/protocol/mock"
storerr "github.com/onflow/flow-go/storage"
bstorage "github.com/onflow/flow-go/storage/badger"
storage "github.com/onflow/flow-go/storage/mock"
"github.com/onflow/flow-go/utils/unittest"
"github.com/onflow/flow-go/utils/unittest/mocks"
)
type Suite struct {
suite.Suite
// protocol state
proto struct {
state *protocol.FollowerState
snapshot *protocol.Snapshot
params *protocol.Params
}
me *modulemock.Local
net *mocknetwork.Network
request *modulemock.Requester
obsIdentity *flow.Identity
provider *mocknetwork.Engine
blocks *storage.Blocks
headers *storage.Headers
collections *storage.Collections
transactions *storage.Transactions
receipts *storage.ExecutionReceipts
results *storage.ExecutionResults
seals *storage.Seals
conduit *mocknetwork.Conduit
downloader *downloadermock.Downloader
sealedBlock *flow.Header
finalizedBlock *flow.Header
log zerolog.Logger
blockMap map[uint64]*flow.Block
rootBlock flow.Block
collectionExecutedMetric *indexer.CollectionExecutedMetricImpl
ctx context.Context
cancel context.CancelFunc
db *badger.DB
dbDir string
lastFullBlockHeight *counters.PersistentStrictMonotonicCounter
}
func TestIngestEngine(t *testing.T) {
suite.Run(t, new(Suite))
}
// TearDownTest stops the engine and cleans up the db
func (s *Suite) TearDownTest() {
s.cancel()
err := os.RemoveAll(s.dbDir)
s.Require().NoError(err)
}
func (s *Suite) SetupTest() {
s.log = zerolog.New(os.Stderr)
s.ctx, s.cancel = context.WithCancel(context.Background())
s.db, s.dbDir = unittest.TempBadgerDB(s.T())
s.obsIdentity = unittest.IdentityFixture(unittest.WithRole(flow.RoleAccess))
s.blocks = storage.NewBlocks(s.T())
// mock out protocol state
s.proto.state = new(protocol.FollowerState)
s.proto.snapshot = new(protocol.Snapshot)
s.proto.params = new(protocol.Params)
s.finalizedBlock = unittest.BlockHeaderFixture(unittest.WithHeaderHeight(0))
s.proto.state.On("Identity").Return(s.obsIdentity, nil)
s.proto.state.On("Params").Return(s.proto.params)
s.proto.snapshot.On("Head").Return(
func() *flow.Header {
return s.finalizedBlock
},
nil,
).Maybe()
s.me = modulemock.NewLocal(s.T())
s.me.On("NodeID").Return(s.obsIdentity.NodeID).Maybe()
s.net = mocknetwork.NewNetwork(s.T())
conduit := mocknetwork.NewConduit(s.T())
s.net.On("Register", channels.ReceiveReceipts, mock.Anything).
Return(conduit, nil).
Once()
s.request = modulemock.NewRequester(s.T())
s.provider = mocknetwork.NewEngine(s.T())
s.blocks = storage.NewBlocks(s.T())
s.headers = storage.NewHeaders(s.T())
s.collections = new(storage.Collections)
s.receipts = new(storage.ExecutionReceipts)
s.transactions = new(storage.Transactions)
s.results = new(storage.ExecutionResults)
collectionsToMarkFinalized, err := stdmap.NewTimes(100)
require.NoError(s.T(), err)
collectionsToMarkExecuted, err := stdmap.NewTimes(100)
require.NoError(s.T(), err)
blocksToMarkExecuted, err := stdmap.NewTimes(100)
require.NoError(s.T(), err)
blockTransactions, err := stdmap.NewIdentifierMap(100)
require.NoError(s.T(), err)
s.proto.state.On("Identity").Return(s.obsIdentity, nil)
s.proto.state.On("Params").Return(s.proto.params)
blockCount := 5
s.blockMap = make(map[uint64]*flow.Block, blockCount)
s.rootBlock = unittest.BlockFixture()
s.rootBlock.Header.Height = 0
parent := s.rootBlock.Header
for i := 0; i < blockCount; i++ {
block := unittest.BlockWithParentFixture(parent)
// update for next iteration
parent = block.Header
s.blockMap[block.Header.Height] = block
}
s.finalizedBlock = parent
s.blocks.On("ByHeight", mock.AnythingOfType("uint64")).Return(
mocks.ConvertStorageOutput(
mocks.StorageMapGetter(s.blockMap),
func(block *flow.Block) *flow.Block { return block },
),
).Maybe()
s.proto.snapshot.On("Head").Return(
func() *flow.Header {
return s.finalizedBlock
},
nil,
).Maybe()
s.proto.state.On("Final").Return(s.proto.snapshot, nil)
// Mock the finalized root block header with height 0.
header := unittest.BlockHeaderFixture(unittest.WithHeaderHeight(0))
s.proto.params.On("FinalizedRoot").Return(header, nil)
s.collectionExecutedMetric, err = indexer.NewCollectionExecutedMetricImpl(
s.log,
metrics.NewNoopCollector(),
collectionsToMarkFinalized,
collectionsToMarkExecuted,
blocksToMarkExecuted,
s.collections,
s.blocks,
blockTransactions,
)
require.NoError(s.T(), err)
}
// initIngestionEngine create new instance of ingestion engine and waits when it starts
func (s *Suite) initIngestionEngine(ctx irrecoverable.SignalerContext) *Engine {
processedHeight := bstorage.NewConsumerProgress(s.db, module.ConsumeProgressIngestionEngineBlockHeight)
var err error
s.lastFullBlockHeight, err = counters.NewPersistentStrictMonotonicCounter(
bstorage.NewConsumerProgress(s.db, module.ConsumeProgressLastFullBlockHeight),
s.finalizedBlock.Height,
)
require.NoError(s.T(), err)
eng, err := New(
s.log,
s.net,
s.proto.state,
s.me,
s.request,
s.blocks,
s.headers,
s.collections,
s.transactions,
s.results,
s.receipts,
s.collectionExecutedMetric,
processedHeight,
s.lastFullBlockHeight,
nil,
)
require.NoError(s.T(), err)
eng.ComponentManager.Start(ctx)
<-eng.Ready()
return eng
}
// mockCollectionsForBlock mocks collections for block
func (s *Suite) mockCollectionsForBlock(block flow.Block) {
// we should query the block once and index the guarantee payload once
for _, g := range block.Payload.Guarantees {
collection := unittest.CollectionFixture(1)
light := collection.Light()
s.collections.On("LightByID", g.CollectionID).Return(&light, nil).Twice()
}
}
// generateBlock prepares block with payload and specified guarantee.SignerIndices
func (s *Suite) generateBlock(clusterCommittee flow.IdentitySkeletonList, snap *protocol.Snapshot) flow.Block {
block := unittest.BlockFixture()
block.SetPayload(unittest.PayloadFixture(
unittest.WithGuarantees(unittest.CollectionGuaranteesFixture(4)...),
unittest.WithExecutionResults(unittest.ExecutionResultFixture()),
unittest.WithSeals(unittest.Seal.Fixture()),
))
refBlockID := unittest.IdentifierFixture()
for _, guarantee := range block.Payload.Guarantees {
guarantee.ReferenceBlockID = refBlockID
// guarantee signers must be cluster committee members, so that access will fetch collection from
// the signers that are specified by guarantee.SignerIndices
indices, err := signature.EncodeSignersToIndices(clusterCommittee.NodeIDs(), clusterCommittee.NodeIDs())
require.NoError(s.T(), err)
guarantee.SignerIndices = indices
}
s.proto.state.On("AtBlockID", refBlockID).Return(snap)
return block
}
// TestOnFinalizedBlock checks that when a block is received, a request for each individual collection is made
func (s *Suite) TestOnFinalizedBlockSingle() {
cluster := new(protocol.Cluster)
epoch := new(protocol.Epoch)
epochs := new(protocol.EpochQuery)
snap := new(protocol.Snapshot)
epoch.On("ClusterByChainID", mock.Anything).Return(cluster, nil)
epochs.On("Current").Return(epoch)
snap.On("Epochs").Return(epochs)
// prepare cluster committee members
clusterCommittee := unittest.IdentityListFixture(32 * 4).Filter(filter.HasRole[flow.Identity](flow.RoleCollection)).ToSkeleton()
cluster.On("Members").Return(clusterCommittee, nil)
irrecoverableCtx := irrecoverable.NewMockSignalerContext(s.T(), s.ctx)
eng := s.initIngestionEngine(irrecoverableCtx)
block := s.generateBlock(clusterCommittee, snap)
block.Header.Height = s.finalizedBlock.Height + 1
s.blockMap[block.Header.Height] = &block
s.mockCollectionsForBlock(block)
s.finalizedBlock = block.Header
hotstuffBlock := hotmodel.Block{
BlockID: block.ID(),
}
// expect that the block storage is indexed with each of the collection guarantee
s.blocks.On("IndexBlockForCollections", block.ID(), []flow.Identifier(flow.GetIDs(block.Payload.Guarantees))).Return(nil).Once()
for _, seal := range block.Payload.Seals {
s.results.On("Index", seal.BlockID, seal.ResultID).Return(nil).Once()
}
missingCollectionCount := 4
wg := sync.WaitGroup{}
wg.Add(missingCollectionCount)
for _, cg := range block.Payload.Guarantees {
s.request.On("EntityByID", cg.CollectionID, mock.Anything).Return().Run(func(args mock.Arguments) {
// Ensure the test does not complete its work faster than necessary
wg.Done()
}).Once()
}
// process the block through the finalized callback
eng.OnFinalizedBlock(&hotstuffBlock)
unittest.RequireReturnsBefore(s.T(), wg.Wait, 100*time.Millisecond, "expect to process new block before timeout")
// assert that the block was retrieved and all collections were requested
s.headers.AssertExpectations(s.T())
s.request.AssertNumberOfCalls(s.T(), "EntityByID", len(block.Payload.Guarantees))
s.results.AssertNumberOfCalls(s.T(), "Index", len(block.Payload.Seals))
}
// TestOnFinalizedBlockSeveralBlocksAhead checks OnFinalizedBlock with a block several blocks newer than the last block processed
func (s *Suite) TestOnFinalizedBlockSeveralBlocksAhead() {
cluster := new(protocol.Cluster)
epoch := new(protocol.Epoch)
epochs := new(protocol.EpochQuery)
snap := new(protocol.Snapshot)
epoch.On("ClusterByChainID", mock.Anything).Return(cluster, nil)
epochs.On("Current").Return(epoch)
snap.On("Epochs").Return(epochs)
// prepare cluster committee members
clusterCommittee := unittest.IdentityListFixture(32 * 4).Filter(filter.HasRole[flow.Identity](flow.RoleCollection)).ToSkeleton()
cluster.On("Members").Return(clusterCommittee, nil)
irrecoverableCtx := irrecoverable.NewMockSignalerContext(s.T(), s.ctx)
eng := s.initIngestionEngine(irrecoverableCtx)
newBlocksCount := 3
startHeight := s.finalizedBlock.Height + 1
blocks := make([]flow.Block, newBlocksCount)
// generate the test blocks, cgs and collections
for i := 0; i < newBlocksCount; i++ {
block := s.generateBlock(clusterCommittee, snap)
block.Header.Height = startHeight + uint64(i)
s.blockMap[block.Header.Height] = &block
blocks[i] = block
s.mockCollectionsForBlock(block)
s.finalizedBlock = block.Header
}
// latest of all the new blocks which are newer than the last block processed
latestBlock := blocks[2]
// block several blocks newer than the last block processed
hotstuffBlock := hotmodel.Block{
BlockID: latestBlock.ID(),
}
missingCollectionCountPerBlock := 4
wg := sync.WaitGroup{}
wg.Add(missingCollectionCountPerBlock * newBlocksCount)
// expected all new blocks after last block processed
for _, block := range blocks {
s.blocks.On("IndexBlockForCollections", block.ID(), []flow.Identifier(flow.GetIDs(block.Payload.Guarantees))).Return(nil).Once()
for _, cg := range block.Payload.Guarantees {
s.request.On("EntityByID", cg.CollectionID, mock.Anything).Return().Run(func(args mock.Arguments) {
// Ensure the test does not complete its work faster than necessary, so we can check all expected results
wg.Done()
}).Once()
}
for _, seal := range block.Payload.Seals {
s.results.On("Index", seal.BlockID, seal.ResultID).Return(nil).Once()
}
}
eng.OnFinalizedBlock(&hotstuffBlock)
unittest.RequireReturnsBefore(s.T(), wg.Wait, 100*time.Millisecond, "expect to process all blocks before timeout")
expectedEntityByIDCalls := 0
expectedIndexCalls := 0
for _, block := range blocks {
expectedEntityByIDCalls += len(block.Payload.Guarantees)
expectedIndexCalls += len(block.Payload.Seals)
}
s.headers.AssertExpectations(s.T())
s.blocks.AssertNumberOfCalls(s.T(), "IndexBlockForCollections", newBlocksCount)
s.request.AssertNumberOfCalls(s.T(), "EntityByID", expectedEntityByIDCalls)
s.results.AssertNumberOfCalls(s.T(), "Index", expectedIndexCalls)
}
// TestOnCollection checks that when a Collection is received, it is persisted
func (s *Suite) TestOnCollection() {
irrecoverableCtx := irrecoverable.NewMockSignalerContext(s.T(), s.ctx)
s.initIngestionEngine(irrecoverableCtx)
collection := unittest.CollectionFixture(5)
light := collection.Light()
// we should store the light collection and index its transactions
s.collections.On("StoreLightAndIndexByTransaction", &light).Return(nil).Once()
// for each transaction in the collection, we should store it
needed := make(map[flow.Identifier]struct{})
for _, txID := range light.Transactions {
needed[txID] = struct{}{}
}
s.transactions.On("Store", mock.Anything).Return(nil).Run(
func(args mock.Arguments) {
tx := args.Get(0).(*flow.TransactionBody)
_, pending := needed[tx.ID()]
s.Assert().True(pending, "tx not pending (%x)", tx.ID())
},
)
err := indexer.HandleCollection(&collection, s.collections, s.transactions, s.log, s.collectionExecutedMetric)
require.NoError(s.T(), err)
// check that the collection was stored and indexed, and we stored all transactions
s.collections.AssertExpectations(s.T())
s.transactions.AssertNumberOfCalls(s.T(), "Store", len(collection.Transactions))
}
// TestExecutionReceiptsAreIndexed checks that execution receipts are properly indexed
func (s *Suite) TestExecutionReceiptsAreIndexed() {
irrecoverableCtx := irrecoverable.NewMockSignalerContext(s.T(), s.ctx)
eng := s.initIngestionEngine(irrecoverableCtx)
originID := unittest.IdentifierFixture()
collection := unittest.CollectionFixture(5)
light := collection.Light()
// we should store the light collection and index its transactions
s.collections.On("StoreLightAndIndexByTransaction", &light).Return(nil).Once()
block := &flow.Block{
Header: &flow.Header{Height: 0},
Payload: &flow.Payload{Guarantees: []*flow.CollectionGuarantee{}},
}
s.blocks.On("ByID", mock.Anything).Return(block, nil)
// for each transaction in the collection, we should store it
needed := make(map[flow.Identifier]struct{})
for _, txID := range light.Transactions {
needed[txID] = struct{}{}
}
s.transactions.On("Store", mock.Anything).Return(nil).Run(
func(args mock.Arguments) {
tx := args.Get(0).(*flow.TransactionBody)
_, pending := needed[tx.ID()]
s.Assert().True(pending, "tx not pending (%x)", tx.ID())
},
)
er1 := unittest.ExecutionReceiptFixture()
er2 := unittest.ExecutionReceiptFixture()
s.receipts.On("Store", mock.Anything).Return(nil)
s.blocks.On("ByID", er1.ExecutionResult.BlockID).Return(nil, storerr.ErrNotFound)
s.receipts.On("Store", mock.Anything).Return(nil)
s.blocks.On("ByID", er2.ExecutionResult.BlockID).Return(nil, storerr.ErrNotFound)
err := eng.handleExecutionReceipt(originID, er1)
require.NoError(s.T(), err)
err = eng.handleExecutionReceipt(originID, er2)
require.NoError(s.T(), err)
s.receipts.AssertExpectations(s.T())
s.results.AssertExpectations(s.T())
s.receipts.AssertExpectations(s.T())
}
// TestOnCollectionDuplicate checks that when a duplicate collection is received, the node doesn't
// crash but just ignores its transactions.
func (s *Suite) TestOnCollectionDuplicate() {
irrecoverableCtx := irrecoverable.NewMockSignalerContext(s.T(), s.ctx)
s.initIngestionEngine(irrecoverableCtx)
collection := unittest.CollectionFixture(5)
light := collection.Light()
// we should store the light collection and index its transactions
s.collections.On("StoreLightAndIndexByTransaction", &light).Return(storerr.ErrAlreadyExists).Once()
// for each transaction in the collection, we should store it
needed := make(map[flow.Identifier]struct{})
for _, txID := range light.Transactions {
needed[txID] = struct{}{}
}
s.transactions.On("Store", mock.Anything).Return(nil).Run(
func(args mock.Arguments) {
tx := args.Get(0).(*flow.TransactionBody)
_, pending := needed[tx.ID()]
s.Assert().True(pending, "tx not pending (%x)", tx.ID())
},
)
err := indexer.HandleCollection(&collection, s.collections, s.transactions, s.log, s.collectionExecutedMetric)
require.NoError(s.T(), err)
// check that the collection was stored and indexed, and we stored all transactions
s.collections.AssertExpectations(s.T())
s.transactions.AssertNotCalled(s.T(), "Store", "should not store any transactions")
}
// TestRequestMissingCollections tests that the all missing collections are requested on the call to requestMissingCollections
func (s *Suite) TestRequestMissingCollections() {
irrecoverableCtx := irrecoverable.NewMockSignalerContext(s.T(), s.ctx)
eng := s.initIngestionEngine(irrecoverableCtx)
blkCnt := 3
startHeight := uint64(1000)
// prepare cluster committee members
clusterCommittee := unittest.IdentityListFixture(32 * 4).Filter(filter.HasRole[flow.Identity](flow.RoleCollection)).ToSkeleton()
// generate the test blocks and collections
var collIDs []flow.Identifier
refBlockID := unittest.IdentifierFixture()
for i := 0; i < blkCnt; i++ {
block := unittest.BlockFixture()
block.SetPayload(unittest.PayloadFixture(
unittest.WithGuarantees(
unittest.CollectionGuaranteesFixture(4, unittest.WithCollRef(refBlockID))...),
))
// some blocks may not be present hence add a gap
height := startHeight + uint64(i)
block.Header.Height = height
s.blockMap[block.Header.Height] = &block
s.finalizedBlock = block.Header
for _, c := range block.Payload.Guarantees {
collIDs = append(collIDs, c.CollectionID)
c.ReferenceBlockID = refBlockID
// guarantee signers must be cluster committee members, so that access will fetch collection from
// the signers that are specified by guarantee.SignerIndices
indices, err := signature.EncodeSignersToIndices(clusterCommittee.NodeIDs(), clusterCommittee.NodeIDs())
require.NoError(s.T(), err)
c.SignerIndices = indices
}
}
// consider collections are missing for all blocks
err := s.lastFullBlockHeight.Set(startHeight - 1)
s.Require().NoError(err)
// consider the last test block as the head
// p is the probability of not receiving the collection before the next poll and it
// helps simulate the slow trickle of the requested collections being received
var p float32
// rcvdColl is the map simulating the collection storage key-values
rcvdColl := make(map[flow.Identifier]struct{})
// for the first lookup call for each collection, it will be reported as missing from db
// for the subsequent calls, it will be reported as present with the probability p
s.collections.On("LightByID", mock.Anything).Return(
func(cID flow.Identifier) *flow.LightCollection {
return nil // the actual collection object return is never really read
},
func(cID flow.Identifier) error {
if _, ok := rcvdColl[cID]; ok {
return nil
}
if rand.Float32() >= p {
rcvdColl[cID] = struct{}{}
}
return storerr.ErrNotFound
}).
// simulate some db i/o contention
After(time.Millisecond * time.Duration(rand.Intn(5)))
// setup the requester engine mock
// entityByID should be called once per collection
for _, c := range collIDs {
s.request.On("EntityByID", c, mock.Anything).Return()
}
// force should be called once
s.request.On("Force").Return()
cluster := new(protocol.Cluster)
cluster.On("Members").Return(clusterCommittee, nil)
epoch := new(protocol.Epoch)
epoch.On("ClusterByChainID", mock.Anything).Return(cluster, nil)
epochs := new(protocol.EpochQuery)
epochs.On("Current").Return(epoch)
snap := new(protocol.Snapshot)
snap.On("Epochs").Return(epochs)
s.proto.state.On("AtBlockID", refBlockID).Return(snap)
assertExpectations := func() {
s.request.AssertExpectations(s.T())
s.collections.AssertExpectations(s.T())
s.proto.snapshot.AssertExpectations(s.T())
s.blocks.AssertExpectations(s.T())
}
// test 1 - collections are not received before timeout
s.Run("timeout before all missing collections are received", func() {
// simulate that collection are never received
p = 1
// timeout after 3 db polls
ctx, cancel := context.WithTimeout(context.Background(), 100*defaultCollectionCatchupDBPollInterval)
defer cancel()
err := eng.requestMissingCollections(ctx)
require.Error(s.T(), err)
require.Contains(s.T(), err.Error(), "context deadline exceeded")
assertExpectations()
})
// test 2 - all collections are eventually received before the deadline
s.Run("all missing collections are received", func() {
// 90% of the time, collections are reported as not received when the collection storage is queried
p = 0.9
ctx, cancel := context.WithTimeout(context.Background(), defaultCollectionCatchupTimeout)
defer cancel()
err := eng.requestMissingCollections(ctx)
require.NoError(s.T(), err)
require.Len(s.T(), rcvdColl, len(collIDs))
assertExpectations()
})
}
// TestProcessBackgroundCalls tests that updateLastFullBlockReceivedIndex and checkMissingCollections
// function calls keep the FullBlockIndex up-to-date and request collections if blocks with missing
// collections exceed the threshold.
func (s *Suite) TestProcessBackgroundCalls() {
irrecoverableCtx := irrecoverable.NewMockSignalerContext(s.T(), s.ctx)
eng := s.initIngestionEngine(irrecoverableCtx)
blkCnt := 3
collPerBlk := 10
startHeight := uint64(1000)
blocks := make([]flow.Block, blkCnt)
collMap := make(map[flow.Identifier]*flow.LightCollection, blkCnt*collPerBlk)
// prepare cluster committee members
clusterCommittee := unittest.IdentityListFixture(32 * 4).Filter(filter.HasRole[flow.Identity](flow.RoleCollection)).ToSkeleton()
refBlockID := unittest.IdentifierFixture()
// generate the test blocks, cgs and collections
for i := 0; i < blkCnt; i++ {
guarantees := make([]*flow.CollectionGuarantee, collPerBlk)
for j := 0; j < collPerBlk; j++ {
coll := unittest.CollectionFixture(2).Light()
collMap[coll.ID()] = &coll
cg := unittest.CollectionGuaranteeFixture(func(cg *flow.CollectionGuarantee) {
cg.CollectionID = coll.ID()
cg.ReferenceBlockID = refBlockID
})
// guarantee signers must be cluster committee members, so that access will fetch collection from
// the signers that are specified by guarantee.SignerIndices
indices, err := signature.EncodeSignersToIndices(clusterCommittee.NodeIDs(), clusterCommittee.NodeIDs())
require.NoError(s.T(), err)
cg.SignerIndices = indices
guarantees[j] = cg
}
block := unittest.BlockFixture()
block.SetPayload(unittest.PayloadFixture(unittest.WithGuarantees(guarantees...)))
// set the height
height := startHeight + uint64(i)
block.Header.Height = height
s.blockMap[block.Header.Height] = &block
blocks[i] = block
s.finalizedBlock = block.Header
}
finalizedHeight := s.finalizedBlock.Height
cluster := new(protocol.Cluster)
cluster.On("Members").Return(clusterCommittee, nil)
epoch := new(protocol.Epoch)
epoch.On("ClusterByChainID", mock.Anything).Return(cluster, nil)
epochs := new(protocol.EpochQuery)
epochs.On("Current").Return(epoch)
snap := new(protocol.Snapshot)
snap.On("Epochs").Return(epochs)
s.proto.state.On("AtBlockID", refBlockID).Return(snap)
// blkMissingColl controls which collections are reported as missing by the collections storage mock
blkMissingColl := make([]bool, blkCnt)
for i := 0; i < blkCnt; i++ {
blkMissingColl[i] = false
for _, cg := range blocks[i].Payload.Guarantees {
j := i
s.collections.On("LightByID", cg.CollectionID).Return(
func(cID flow.Identifier) *flow.LightCollection {
return collMap[cID]
},
func(cID flow.Identifier) error {
if blkMissingColl[j] {
return storerr.ErrNotFound
}
return nil
})
}
}
rootBlk := blocks[0]
// root block is the last complete block
err := s.lastFullBlockHeight.Set(rootBlk.Header.Height)
s.Require().NoError(err)
s.Run("missing collections are requested when count exceeds defaultMissingCollsForBlkThreshold", func() {
// lower the block threshold to request missing collections
defaultMissingCollsForBlkThreshold = 2
// mark all blocks beyond the root block as incomplete
for i := 1; i < blkCnt; i++ {
blkMissingColl[i] = true
// setup receive engine expectations
for _, cg := range blocks[i].Payload.Guarantees {
s.request.On("EntityByID", cg.CollectionID, mock.Anything).Return().Once()
}
}
err := eng.checkMissingCollections()
s.Require().NoError(err)
// assert that missing collections are requested
s.request.AssertExpectations(s.T())
// last full blk index is not advanced
s.blocks.AssertExpectations(s.T()) // no new call to UpdateLastFullBlockHeight should be made
})
s.Run("missing collections are requested when count exceeds defaultMissingCollsForAgeThreshold", func() {
// lower the height threshold to request missing collections
defaultMissingCollsForAgeThreshold = 1
// raise the block threshold to ensure it does not trigger missing collection request
defaultMissingCollsForBlkThreshold = blkCnt + 1
// mark all blocks beyond the root block as incomplete
for i := 1; i < blkCnt; i++ {
blkMissingColl[i] = true
// setup receive engine expectations
for _, cg := range blocks[i].Payload.Guarantees {
s.request.On("EntityByID", cg.CollectionID, mock.Anything).Return().Once()
}
}
err := eng.checkMissingCollections()
s.Require().NoError(err)
// assert that missing collections are requested
s.request.AssertExpectations(s.T())
// last full blk index is not advanced
s.blocks.AssertExpectations(s.T()) // not new call to UpdateLastFullBlockHeight should be made
})
s.Run("missing collections are not requested if defaultMissingCollsForBlkThreshold not reached", func() {
// raise the thresholds to avoid requesting missing collections
defaultMissingCollsForAgeThreshold = 3
defaultMissingCollsForBlkThreshold = 3
// mark all blocks beyond the root block as incomplete
for i := 1; i < blkCnt; i++ {
blkMissingColl[i] = true
}
err := eng.checkMissingCollections()
s.Require().NoError(err)
// assert that missing collections are not requested even though there are collections missing
s.request.AssertExpectations(s.T())
// last full blk index is not advanced
s.blocks.AssertExpectations(s.T()) // not new call to UpdateLastFullBlockHeight should be made
})
// create new block
finalizedBlk := unittest.BlockFixture()
height := blocks[blkCnt-1].Header.Height + 1
finalizedBlk.Header.Height = height
s.blockMap[height] = &finalizedBlk
finalizedHeight = finalizedBlk.Header.Height
s.finalizedBlock = finalizedBlk.Header
blockBeforeFinalized := blocks[blkCnt-1].Header
s.Run("full block height index is advanced if newer full blocks are discovered", func() {
// set lastFullBlockHeight to block
err = s.lastFullBlockHeight.Set(blockBeforeFinalized.Height)
s.Require().NoError(err)
err = eng.updateLastFullBlockReceivedIndex()
s.Require().NoError(err)
s.Require().Equal(finalizedHeight, s.lastFullBlockHeight.Value())
s.Require().NoError(err)
s.blocks.AssertExpectations(s.T())
})
s.Run("full block height index is not advanced beyond finalized blocks", func() {
err = eng.updateLastFullBlockReceivedIndex()
s.Require().NoError(err)
s.Require().Equal(finalizedHeight, s.lastFullBlockHeight.Value())
s.blocks.AssertExpectations(s.T())
})
}
func (s *Suite) TestComponentShutdown() {
irrecoverableCtx := irrecoverable.NewMockSignalerContext(s.T(), s.ctx)
eng := s.initIngestionEngine(irrecoverableCtx)
// start then shut down the engine
unittest.AssertClosesBefore(s.T(), eng.Ready(), 10*time.Millisecond)
s.cancel()
unittest.AssertClosesBefore(s.T(), eng.Done(), 10*time.Millisecond)
err := eng.Process(channels.ReceiveReceipts, unittest.IdentifierFixture(), &flow.ExecutionReceipt{})
s.Assert().ErrorIs(err, component.ErrComponentShutdown)
}