-
Notifications
You must be signed in to change notification settings - Fork 207
Expand file tree
/
Copy pathengine_test.go
More file actions
653 lines (543 loc) · 23.6 KB
/
engine_test.go
File metadata and controls
653 lines (543 loc) · 23.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
package ingestion
import (
"context"
"os"
"sync"
"testing"
"time"
"github.com/jordanschalm/lockctx"
"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/consensus/hotstuff/notifications/pubsub"
"github.com/onflow/flow-go/engine/access/ingestion/collections"
"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/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"
mocknetwork "github.com/onflow/flow-go/network/mock"
protocolmock "github.com/onflow/flow-go/state/protocol/mock"
"github.com/onflow/flow-go/storage"
storagemock "github.com/onflow/flow-go/storage/mock"
"github.com/onflow/flow-go/storage/operation/pebbleimpl"
"github.com/onflow/flow-go/storage/store"
"github.com/onflow/flow-go/utils/unittest"
"github.com/onflow/flow-go/utils/unittest/fixtures"
"github.com/onflow/flow-go/utils/unittest/mocks"
)
type Suite struct {
suite.Suite
// protocol state
proto struct {
state *protocolmock.FollowerState
snapshot *protocolmock.Snapshot
params *protocolmock.Params
}
me *modulemock.Local
net *mocknetwork.EngineRegistry
request *modulemock.Requester
obsIdentity *flow.Identity
provider *mocknetwork.Engine
blocks *storagemock.Blocks
headers *storagemock.Headers
collections *storagemock.Collections
transactions *storagemock.Transactions
receipts *storagemock.ExecutionReceipts
results *storagemock.ExecutionResults
seals *storagemock.Seals
conduit *mocknetwork.Conduit
downloader *downloadermock.Downloader
sealedBlock *flow.Header
finalizedBlock *flow.Header
log zerolog.Logger
blockMap map[uint64]*flow.Block
distributor *pubsub.FollowerDistributor
rootBlock *flow.Block
collectionExecutedMetric *indexer.CollectionExecutedMetricImpl
ctx context.Context
cancel context.CancelFunc
db storage.DB
dbDir string
lastFullBlockHeight *counters.PersistentStrictMonotonicCounter
lockManager lockctx.Manager
}
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 = unittest.Logger()
s.ctx, s.cancel = context.WithCancel(context.Background())
db, dbDir := unittest.TempPebbleDB(s.T())
s.db = pebbleimpl.ToDB(db)
s.dbDir = dbDir
s.lockManager = storage.NewTestingLockManager()
s.obsIdentity = unittest.IdentityFixture(unittest.WithRole(flow.RoleAccess))
s.blocks = storagemock.NewBlocks(s.T())
// mock out protocol state
s.proto.state = new(protocolmock.FollowerState)
s.proto.snapshot = new(protocolmock.Snapshot)
s.proto.params = new(protocolmock.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.me = modulemock.NewLocal(s.T())
s.me.On("NodeID").Return(s.obsIdentity.NodeID).Maybe()
s.net = mocknetwork.NewEngineRegistry(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 = storagemock.NewBlocks(s.T())
s.headers = storagemock.NewHeaders(s.T())
s.collections = new(storagemock.Collections)
s.receipts = new(storagemock.ExecutionReceipts)
s.transactions = new(storagemock.Transactions)
s.results = new(storagemock.ExecutionResults)
s.results.On("BatchIndex", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil).Maybe()
collectionsToMarkFinalized := stdmap.NewTimes(100)
collectionsToMarkExecuted := stdmap.NewTimes(100)
blocksToMarkExecuted := stdmap.NewTimes(100)
blockTransactions := stdmap.NewIdentifierMap(100)
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.Block.Genesis(flow.Emulator)
parent := s.rootBlock.ToHeader()
for range blockCount {
block := unittest.BlockWithParentFixture(parent)
s.blockMap[block.Height] = block
// update for next iteration
parent = block.ToHeader()
}
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()
// Mock the finalized root block header with height 0.
header := unittest.BlockHeaderFixture(unittest.WithHeaderHeight(0))
s.proto.params.On("FinalizedRoot").Return(header, nil)
var err error
s.collectionExecutedMetric, err = indexer.NewCollectionExecutedMetricImpl(
s.log,
metrics.NewNoopCollector(),
collectionsToMarkFinalized,
collectionsToMarkExecuted,
blocksToMarkExecuted,
s.collections,
s.blocks,
blockTransactions,
)
require.NoError(s.T(), err)
}
// initEngineAndSyncer create new instance of ingestion engine and collection syncer.
// It waits until the ingestion engine starts.
func (s *Suite) initEngineAndSyncer() (*Engine, *collections.Syncer, *collections.Indexer) {
processedHeight, err := store.NewConsumerProgress(s.db, module.ConsumeProgressIngestionEngineBlockHeight).Initialize(s.finalizedBlock.Height)
require.NoError(s.T(), err)
lastFullBlockHeight, err := store.NewConsumerProgress(s.db, module.ConsumeProgressLastFullBlockHeight).Initialize(s.finalizedBlock.Height)
require.NoError(s.T(), err)
s.lastFullBlockHeight, err = counters.NewPersistentStrictMonotonicCounter(lastFullBlockHeight)
require.NoError(s.T(), err)
indexer, err := collections.NewIndexer(
s.log,
s.db,
s.collectionExecutedMetric,
s.proto.state,
s.blocks,
s.collections,
s.lastFullBlockHeight,
s.lockManager,
)
require.NoError(s.T(), err)
syncer := collections.NewSyncer(
s.log,
s.request,
s.proto.state,
s.collections,
s.lastFullBlockHeight,
indexer,
nil,
)
s.distributor = pubsub.NewFollowerDistributor()
eng, err := New(
s.log,
s.net,
s.proto.state,
s.me,
s.lockManager,
s.db,
s.blocks,
s.results,
s.receipts,
processedHeight,
syncer,
indexer,
s.collectionExecutedMetric,
metrics.NewNoopCollector(),
nil,
s.distributor,
)
require.NoError(s.T(), err)
return eng, syncer, indexer
}
// 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 *protocolmock.Snapshot) *flow.Block {
block := unittest.BlockFixture(
unittest.Block.WithPayload(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(protocolmock.Cluster)
epoch := new(protocolmock.CommittedEpoch)
epochs := new(protocolmock.EpochQuery)
snap := new(protocolmock.Snapshot)
finalSnapshot := protocolmock.NewSnapshot(s.T())
finalSnapshot.On("Head").Return(s.finalizedBlock, nil).Once()
s.proto.state.On("Final").Return(finalSnapshot, nil).Once()
epoch.On("ClusterByChainID", mock.Anything).Return(cluster, nil)
epochs.On("Current").Return(epoch, nil)
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)
eng, _, _ := s.initEngineAndSyncer()
irrecoverableCtx, cancel := irrecoverable.NewMockSignalerContextWithCancel(s.T(), s.ctx)
eng.ComponentManager.Start(irrecoverableCtx)
unittest.RequireCloseBefore(s.T(), eng.Ready(), 100*time.Millisecond, "could not start worker")
defer func() {
cancel()
unittest.RequireCloseBefore(s.T(), eng.Done(), 100*time.Millisecond, "could not stop worker")
}()
block := s.generateBlock(clusterCommittee, snap)
block.Height = s.finalizedBlock.Height + 1
s.blockMap[block.Height] = block
s.mockCollectionsForBlock(block)
s.finalizedBlock = block.ToHeader()
hotstuffBlock := hotmodel.Block{
BlockID: block.ID(),
}
// expect that the block storage is indexed with each of the collection guarantee
s.blocks.On("BatchIndexBlockContainingCollectionGuarantees", mock.Anything, mock.Anything, block.ID(), []flow.Identifier(flow.GetIDs(block.Payload.Guarantees))).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()
}
// force should be called once
s.request.On("Force").Return().Once()
// process the block through the finalized callback
s.distributor.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))
}
// TestOnFinalizedBlockSeveralBlocksAhead checks OnFinalizedBlock with a block several blocks newer than the last block processed
func (s *Suite) TestOnFinalizedBlockSeveralBlocksAhead() {
cluster := new(protocolmock.Cluster)
epoch := new(protocolmock.CommittedEpoch)
epochs := new(protocolmock.EpochQuery)
snap := new(protocolmock.Snapshot)
finalSnapshot := protocolmock.NewSnapshot(s.T())
finalSnapshot.On("Head").Return(s.finalizedBlock, nil).Once()
s.proto.state.On("Final").Return(finalSnapshot, nil).Once()
epoch.On("ClusterByChainID", mock.Anything).Return(cluster, nil)
epochs.On("Current").Return(epoch, nil)
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)
eng, _, _ := s.initEngineAndSyncer()
irrecoverableCtx, cancel := irrecoverable.NewMockSignalerContextWithCancel(s.T(), s.ctx)
eng.ComponentManager.Start(irrecoverableCtx)
unittest.RequireCloseBefore(s.T(), eng.Ready(), 100*time.Millisecond, "could not start worker")
defer func() {
cancel()
unittest.RequireCloseBefore(s.T(), eng.Done(), 100*time.Millisecond, "could not stop worker")
}()
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.Height = startHeight + uint64(i)
s.blockMap[block.Height] = block
blocks[i] = block
s.mockCollectionsForBlock(block)
s.finalizedBlock = block.ToHeader()
}
// 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("BatchIndexBlockContainingCollectionGuarantees", mock.Anything, mock.Anything, 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()
}
// force should be called once
s.request.On("Force").Return().Once()
}
s.distributor.OnFinalizedBlock(&hotstuffBlock)
unittest.RequireReturnsBefore(s.T(), wg.Wait, 100*time.Millisecond, "expect to process all blocks before timeout")
expectedEntityByIDCalls := 0
for _, block := range blocks {
expectedEntityByIDCalls += len(block.Payload.Guarantees)
}
s.headers.AssertExpectations(s.T())
s.blocks.AssertNumberOfCalls(s.T(), "BatchIndexBlockContainingCollectionGuarantees", newBlocksCount)
s.request.AssertNumberOfCalls(s.T(), "EntityByID", expectedEntityByIDCalls)
}
// TestExecutionReceiptsAreIndexed checks that execution receipts are properly indexed
func (s *Suite) TestExecutionReceiptsAreIndexed() {
eng, _, _ := s.initEngineAndSyncer()
originID := unittest.IdentifierFixture()
collection := unittest.CollectionFixture(5)
light := collection.Light()
// we should store the collection and index its transactions
s.collections.On("StoreAndIndexByTransaction", &collection).Return(light, nil).Once()
block := unittest.BlockFixture(
unittest.Block.WithHeight(0),
unittest.Block.WithPayload(
unittest.PayloadFixture(unittest.WithGuarantees([]*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, storage.ErrNotFound)
s.receipts.On("Store", mock.Anything).Return(nil)
s.blocks.On("ByID", er2.ExecutionResult.BlockID).Return(nil, storage.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())
}
// TestCollectionSyncing tests the happy path of syncing collections for finalized blocks.
// It performs syncs for a single block passed via the OnFinalizedBlock callback, and verifies that
// the finalized block processing logic submits the request for each collection in the finalized block,
// the indexer indexes all collections received from the network, and the last full block height is
// updated after the collections are indexed.
func (s *Suite) TestCollectionSyncing() {
g := fixtures.NewGeneratorSuite()
guarantors := g.Identities().List(3, fixtures.Identity.WithRole(flow.RoleCollection))
signerIndices, err := signature.EncodeSignersToIndices(guarantors.NodeIDs(), guarantors.NodeIDs())
require.NoError(s.T(), err)
collections := g.Collections().List(10)
guarantees := make([]*flow.CollectionGuarantee, len(collections))
for i, collection := range collections {
guarantee := g.Guarantees().Fixture(
fixtures.Guarantee.WithCollectionID(collection.ID()),
fixtures.Guarantee.WithSignerIndices(signerIndices),
)
guarantees[i] = guarantee
}
payload := g.Payloads().Fixture(
fixtures.Payload.WithGuarantees(guarantees...),
fixtures.Payload.WithSeals(g.Seals().List(10)...),
)
block := g.Blocks().Fixture(
fixtures.Block.WithPayload(payload),
fixtures.Block.WithParentHeader(s.finalizedBlock), // replace the finalized block
)
s.blockMap[block.Height] = block
hotstuffBlock := hotmodel.Block{
BlockID: block.ID(),
}
for _, guarantee := range block.Payload.Guarantees {
// initially, all collections should be missing from storage
s.collections.On("LightByID", guarantee.CollectionID).Return(nil, storage.ErrNotFound)
// setup requester engine requests
s.mockGuarantorsForCollection(guarantee, guarantors.ToSkeleton())
s.request.On("EntityByID", guarantee.CollectionID, mock.Anything).Once()
}
s.request.On("Force").Once()
// setup finalized block indexer mocks
guaranteeIDs := []flow.Identifier(flow.GetIDs(block.Payload.Guarantees))
s.blocks.On("BatchIndexBlockContainingCollectionGuarantees", mock.Anything, mock.Anything, block.ID(), guaranteeIDs).Return(nil).Once()
for _, seal := range payload.Seals {
s.results.On("BatchIndex", mock.Anything, mock.Anything, seal.BlockID, seal.ResultID).Return(nil).Once()
}
// initialize the engine using the initial finalized block
initialFinalSnapshot := protocolmock.NewSnapshot(s.T())
initialFinalSnapshot.On("Head").Return(s.finalizedBlock, nil)
s.proto.state.On("Final").Return(initialFinalSnapshot, nil)
eng, syncer, _ := s.initEngineAndSyncer()
irrecoverableCtx, cancel := irrecoverable.NewMockSignalerContextWithCancel(s.T(), s.ctx)
eng.ComponentManager.Start(irrecoverableCtx)
unittest.RequireCloseBefore(s.T(), eng.Ready(), 1*time.Second, "could not start worker")
defer func() {
cancel()
unittest.RequireCloseBefore(s.T(), eng.Done(), 100*time.Millisecond, "could not stop worker")
}()
// progress the finalized block, and submit the new block to the engine
newFinalSnapshot := protocolmock.NewSnapshot(s.T())
newFinalSnapshot.On("Head").Return(block.ToHeader(), nil)
s.proto.state.On("Final").Unset()
s.proto.state.On("Final").Return(newFinalSnapshot, nil)
s.distributor.OnFinalizedBlock(&hotstuffBlock)
// wait until the finalized block jobqueue completes processing the block
require.Eventually(s.T(), func() bool {
return eng.finalizedBlockConsumer.LastProcessedIndex() == block.Height
}, 2*time.Second, 10*time.Millisecond, "finalized block processor never processed block")
// all requests should be sent after the finalized block processor completes processing the block.
// The requester engine calls the syncer's OnCollectionDownloaded callback for each response.
// simulate receiving the collection responses from the network.
for _, collection := range collections {
collectionID := collection.ID()
light := collection.Light()
s.collections.On("StoreAndIndexByTransaction", mock.Anything, collection).Return(light, nil).Once()
// the collections are now available in storage.
s.collections.On("LightByID", collectionID).Unset()
s.collections.On("LightByID", collectionID).Return(light, nil)
syncer.OnCollectionDownloaded(g.Identifiers().Fixture(), collection)
}
// make sure that the collection indexer updates the last full block height
require.Eventually(s.T(), func() bool {
return s.lastFullBlockHeight.Value() == block.Height
}, 2*time.Second, 100*time.Millisecond, "last full block height never updated")
}
// TestOnFinalizedBlockAlreadyIndexed checks that when a block has already been indexed
// (storage.ErrAlreadyExists), the engine logs a warning and continues processing by
// requesting collections and updating metrics. This can happen when the job queue processed
// index hasn't been updated yet after a previous indexing operation.
func (s *Suite) TestOnFinalizedBlockAlreadyIndexed() {
cluster := new(protocolmock.Cluster)
epoch := new(protocolmock.CommittedEpoch)
epochs := new(protocolmock.EpochQuery)
snap := new(protocolmock.Snapshot)
finalSnapshot := protocolmock.NewSnapshot(s.T())
finalSnapshot.On("Head").Return(s.finalizedBlock, nil).Once()
s.proto.state.On("Final").Return(finalSnapshot, nil).Once()
epoch.On("ClusterByChainID", mock.Anything).Return(cluster, nil)
epochs.On("Current").Return(epoch, nil)
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)
eng, _, _ := s.initEngineAndSyncer()
irrecoverableCtx, cancel := irrecoverable.NewMockSignalerContextWithCancel(s.T(), s.ctx)
eng.ComponentManager.Start(irrecoverableCtx)
unittest.RequireCloseBefore(s.T(), eng.Ready(), 100*time.Millisecond, "could not start worker")
defer func() {
cancel()
unittest.RequireCloseBefore(s.T(), eng.Done(), 100*time.Millisecond, "could not stop worker")
}()
block := s.generateBlock(clusterCommittee, snap)
block.Height = s.finalizedBlock.Height + 1
s.blockMap[block.Height] = block
s.mockCollectionsForBlock(block)
s.finalizedBlock = block.ToHeader()
hotstuffBlock := hotmodel.Block{
BlockID: block.ID(),
}
// simulate that the block has already been indexed (e.g., by a previous job queue run)
// by returning storage.ErrAlreadyExists
s.blocks.On("BatchIndexBlockContainingCollectionGuarantees", mock.Anything, mock.Anything, block.ID(), []flow.Identifier(flow.GetIDs(block.Payload.Guarantees))).
Return(storage.ErrAlreadyExists).Once()
missingCollectionCount := 4
wg := sync.WaitGroup{}
wg.Add(missingCollectionCount)
// even though block is already indexed, collections should still be requested
for _, cg := range block.Payload.Guarantees {
s.request.On("EntityByID", cg.CollectionID, mock.Anything).Return().Run(func(args mock.Arguments) {
wg.Done()
}).Once()
}
// force should be called once
s.request.On("Force").Return().Once()
// process the block through the finalized callback
s.distributor.OnFinalizedBlock(&hotstuffBlock)
unittest.RequireReturnsBefore(s.T(), wg.Wait, 100*time.Millisecond, "expect to process new block before timeout")
// assert that collections were still requested despite the block being already indexed
s.headers.AssertExpectations(s.T())
s.request.AssertNumberOfCalls(s.T(), "EntityByID", len(block.Payload.Guarantees))
}
func (s *Suite) mockGuarantorsForCollection(guarantee *flow.CollectionGuarantee, members flow.IdentitySkeletonList) {
cluster := protocolmock.NewCluster(s.T())
cluster.On("Members").Return(members, nil).Once()
epoch := protocolmock.NewCommittedEpoch(s.T())
epoch.On("ClusterByChainID", guarantee.ClusterChainID).Return(cluster, nil).Once()
query := protocolmock.NewEpochQuery(s.T())
query.On("Current").Return(epoch, nil).Once()
snapshot := protocolmock.NewSnapshot(s.T())
snapshot.On("Epochs").Return(query).Once()
s.proto.state.On("AtBlockID", guarantee.ReferenceBlockID).Return(snapshot).Once()
}