forked from onflow/flow-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core_test.go
644 lines (554 loc) · 22.2 KB
/
core_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
package compliance
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
hotstuff "github.com/onflow/flow-go/consensus/hotstuff/mocks"
"github.com/onflow/flow-go/consensus/hotstuff/model"
consensus "github.com/onflow/flow-go/engine/consensus/mock"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/model/messages"
realModule "github.com/onflow/flow-go/module"
real "github.com/onflow/flow-go/module/buffer"
"github.com/onflow/flow-go/module/compliance"
"github.com/onflow/flow-go/module/metrics"
module "github.com/onflow/flow-go/module/mock"
"github.com/onflow/flow-go/module/trace"
netint "github.com/onflow/flow-go/network"
"github.com/onflow/flow-go/network/channels"
"github.com/onflow/flow-go/network/mocknetwork"
"github.com/onflow/flow-go/state"
protint "github.com/onflow/flow-go/state/protocol"
protocol "github.com/onflow/flow-go/state/protocol/mock"
storerr "github.com/onflow/flow-go/storage"
storage "github.com/onflow/flow-go/storage/mock"
"github.com/onflow/flow-go/utils/unittest"
)
func TestComplianceCore(t *testing.T) {
suite.Run(t, new(CoreSuite))
}
// CoreSuite tests the compliance core logic.
type CoreSuite struct {
CommonSuite
}
// CommonSuite is shared between compliance core and engine testing.
type CommonSuite struct {
suite.Suite
// engine parameters
participants flow.IdentityList
myID flow.Identifier
head *flow.Header
// storage data
headerDB map[flow.Identifier]*flow.Header
payloadDB map[flow.Identifier]*flow.Payload
pendingDB map[flow.Identifier]flow.Slashable[*flow.Block]
childrenDB map[flow.Identifier][]flow.Slashable[*flow.Block]
// mocked dependencies
me *module.Local
metrics *metrics.NoopCollector
tracer realModule.Tracer
headers *storage.Headers
payloads *storage.Payloads
state *protocol.ParticipantState
snapshot *protocol.Snapshot
con *mocknetwork.Conduit
net *mocknetwork.Network
prov *consensus.ProposalProvider
pending *module.PendingBlockBuffer
hotstuff *module.HotStuff
sync *module.BlockRequester
proposalViolationNotifier *hotstuff.ProposalViolationConsumer
validator *hotstuff.Validator
voteAggregator *hotstuff.VoteAggregator
timeoutAggregator *hotstuff.TimeoutAggregator
// engine under test
core *Core
}
func (cs *CommonSuite) SetupTest() {
// initialize the paramaters
cs.participants = unittest.IdentityListFixture(3,
unittest.WithRole(flow.RoleConsensus),
unittest.WithWeight(1000),
)
cs.myID = cs.participants[0].NodeID
block := unittest.BlockFixture()
cs.head = block.Header
// initialize the storage data
cs.headerDB = make(map[flow.Identifier]*flow.Header)
cs.payloadDB = make(map[flow.Identifier]*flow.Payload)
cs.pendingDB = make(map[flow.Identifier]flow.Slashable[*flow.Block])
cs.childrenDB = make(map[flow.Identifier][]flow.Slashable[*flow.Block])
// store the head header and payload
cs.headerDB[block.ID()] = block.Header
cs.payloadDB[block.ID()] = block.Payload
// set up local module mock
cs.me = &module.Local{}
cs.me.On("NodeID").Return(
func() flow.Identifier {
return cs.myID
},
)
// set up header storage mock
cs.headers = &storage.Headers{}
cs.headers.On("Store", mock.Anything).Return(
func(header *flow.Header) error {
cs.headerDB[header.ID()] = header
return nil
},
)
cs.headers.On("ByBlockID", mock.Anything).Return(
func(blockID flow.Identifier) *flow.Header {
return cs.headerDB[blockID]
},
func(blockID flow.Identifier) error {
_, exists := cs.headerDB[blockID]
if !exists {
return storerr.ErrNotFound
}
return nil
},
)
cs.headers.On("Exists", mock.Anything).Return(
func(blockID flow.Identifier) bool {
_, exists := cs.headerDB[blockID]
return exists
}, func(blockID flow.Identifier) error {
return nil
})
// set up payload storage mock
cs.payloads = &storage.Payloads{}
cs.payloads.On("Store", mock.Anything, mock.Anything).Return(
func(header *flow.Header, payload *flow.Payload) error {
cs.payloadDB[header.ID()] = payload
return nil
},
)
cs.payloads.On("ByBlockID", mock.Anything).Return(
func(blockID flow.Identifier) *flow.Payload {
return cs.payloadDB[blockID]
},
func(blockID flow.Identifier) error {
_, exists := cs.payloadDB[blockID]
if !exists {
return storerr.ErrNotFound
}
return nil
},
)
// set up protocol state mock
cs.state = &protocol.ParticipantState{}
cs.state.On("Final").Return(
func() protint.Snapshot {
return cs.snapshot
},
)
cs.state.On("AtBlockID", mock.Anything).Return(
func(blockID flow.Identifier) protint.Snapshot {
return cs.snapshot
},
)
cs.state.On("Extend", mock.Anything, mock.Anything).Return(nil)
// set up protocol snapshot mock
cs.snapshot = &protocol.Snapshot{}
cs.snapshot.On("Identities", mock.Anything).Return(
func(filter flow.IdentityFilter) flow.IdentityList {
return cs.participants.Filter(filter)
},
nil,
)
cs.snapshot.On("Head").Return(
func() *flow.Header {
return cs.head
},
nil,
)
// set up network conduit mock
cs.con = &mocknetwork.Conduit{}
cs.con.On("Publish", mock.Anything, mock.Anything).Return(nil)
cs.con.On("Publish", mock.Anything, mock.Anything, mock.Anything).Return(nil)
cs.con.On("Publish", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
cs.con.On("Unicast", mock.Anything, mock.Anything).Return(nil)
// set up network module mock
cs.net = &mocknetwork.Network{}
cs.net.On("Register", mock.Anything, mock.Anything).Return(
func(channel channels.Channel, engine netint.MessageProcessor) netint.Conduit {
return cs.con
},
nil,
)
// set up the provider engine
cs.prov = &consensus.ProposalProvider{}
cs.prov.On("ProvideProposal", mock.Anything).Return()
// set up pending module mock
cs.pending = &module.PendingBlockBuffer{}
cs.pending.On("Add", mock.Anything, mock.Anything).Return(true)
cs.pending.On("ByID", mock.Anything).Return(
func(blockID flow.Identifier) flow.Slashable[*flow.Block] {
return cs.pendingDB[blockID]
},
func(blockID flow.Identifier) bool {
_, ok := cs.pendingDB[blockID]
return ok
},
)
cs.pending.On("ByParentID", mock.Anything).Return(
func(blockID flow.Identifier) []flow.Slashable[*flow.Block] {
return cs.childrenDB[blockID]
},
func(blockID flow.Identifier) bool {
_, ok := cs.childrenDB[blockID]
return ok
},
)
cs.pending.On("DropForParent", mock.Anything).Return()
cs.pending.On("Size").Return(uint(0))
cs.pending.On("PruneByView", mock.Anything).Return()
// set up hotstuff module mock
cs.hotstuff = module.NewHotStuff(cs.T())
cs.validator = hotstuff.NewValidator(cs.T())
cs.voteAggregator = hotstuff.NewVoteAggregator(cs.T())
cs.timeoutAggregator = hotstuff.NewTimeoutAggregator(cs.T())
// set up synchronization module mock
cs.sync = &module.BlockRequester{}
cs.sync.On("RequestBlock", mock.Anything, mock.Anything).Return(nil)
cs.sync.On("Done", mock.Anything).Return(unittest.ClosedChannel())
// set up no-op metrics mock
cs.metrics = metrics.NewNoopCollector()
// set up no-op tracer
cs.tracer = trace.NewNoopTracer()
// set up notifier for reporting protocol violations
cs.proposalViolationNotifier = hotstuff.NewProposalViolationConsumer(cs.T())
// initialize the engine
e, err := NewCore(
unittest.Logger(),
cs.metrics,
cs.metrics,
cs.metrics,
cs.metrics,
cs.proposalViolationNotifier,
cs.tracer,
cs.headers,
cs.payloads,
cs.state,
cs.pending,
cs.sync,
cs.validator,
cs.hotstuff,
cs.voteAggregator,
cs.timeoutAggregator,
compliance.DefaultConfig(),
)
require.NoError(cs.T(), err, "engine initialization should pass")
cs.core = e
}
func (cs *CoreSuite) TestOnBlockProposalValidParent() {
// create a proposal that directly descends from the latest finalized header
originID := cs.participants[1].NodeID
block := unittest.BlockWithParentFixture(cs.head)
proposal := unittest.ProposalFromBlock(block)
// store the data for retrieval
cs.headerDB[block.Header.ParentID] = cs.head
hotstuffProposal := model.ProposalFromFlow(block.Header)
cs.validator.On("ValidateProposal", hotstuffProposal).Return(nil)
cs.voteAggregator.On("AddBlock", hotstuffProposal).Once()
cs.hotstuff.On("SubmitProposal", hotstuffProposal)
// it should be processed without error
err := cs.core.OnBlockProposal(flow.Slashable[*messages.BlockProposal]{
OriginID: originID,
Message: proposal,
})
require.NoError(cs.T(), err, "valid block proposal should pass")
// we should extend the state with the header
cs.state.AssertCalled(cs.T(), "Extend", mock.Anything, block)
}
func (cs *CoreSuite) TestOnBlockProposalValidAncestor() {
// create a proposal that has two ancestors in the cache
originID := cs.participants[1].NodeID
ancestor := unittest.BlockWithParentFixture(cs.head)
parent := unittest.BlockWithParentFixture(ancestor.Header)
block := unittest.BlockWithParentFixture(parent.Header)
proposal := unittest.ProposalFromBlock(block)
// store the data for retrieval
cs.headerDB[parent.ID()] = parent.Header
cs.headerDB[ancestor.ID()] = ancestor.Header
hotstuffProposal := model.ProposalFromFlow(block.Header)
cs.validator.On("ValidateProposal", hotstuffProposal).Return(nil)
cs.voteAggregator.On("AddBlock", hotstuffProposal).Once()
cs.hotstuff.On("SubmitProposal", hotstuffProposal)
// it should be processed without error
err := cs.core.OnBlockProposal(flow.Slashable[*messages.BlockProposal]{
OriginID: originID,
Message: proposal,
})
require.NoError(cs.T(), err, "valid block proposal should pass")
// we should extend the state with the header
cs.state.AssertCalled(cs.T(), "Extend", mock.Anything, block)
}
func (cs *CoreSuite) TestOnBlockProposalSkipProposalThreshold() {
// create a proposal which is far enough ahead to be dropped
originID := cs.participants[1].NodeID
block := unittest.BlockFixture()
block.Header.View = cs.head.View + compliance.DefaultConfig().SkipNewProposalsThreshold + 1
proposal := unittest.ProposalFromBlock(&block)
err := cs.core.OnBlockProposal(flow.Slashable[*messages.BlockProposal]{
OriginID: originID,
Message: proposal,
})
require.NoError(cs.T(), err)
// block should be dropped - not added to state or cache
cs.state.AssertNotCalled(cs.T(), "Extend", mock.Anything)
cs.validator.AssertNotCalled(cs.T(), "ValidateProposal", mock.Anything)
cs.pending.AssertNotCalled(cs.T(), "Add", originID, mock.Anything)
}
// TestOnBlockProposal_FailsHotStuffValidation tests that a proposal which fails HotStuff validation.
// - should not go through protocol state validation
// - should not be added to the state
// - we should not attempt to process its children
// - we should notify VoteAggregator, for known errors
func (cs *CoreSuite) TestOnBlockProposal_FailsHotStuffValidation() {
// create a proposal that has two ancestors in the cache
originID := cs.participants[1].NodeID
ancestor := unittest.BlockWithParentFixture(cs.head)
parent := unittest.BlockWithParentFixture(ancestor.Header)
block := unittest.BlockWithParentFixture(parent.Header)
proposal := unittest.ProposalFromBlock(block)
hotstuffProposal := model.ProposalFromFlow(block.Header)
// store the data for retrieval
cs.headerDB[parent.ID()] = parent.Header
cs.headerDB[ancestor.ID()] = ancestor.Header
cs.Run("invalid block error", func() {
// the block fails HotStuff validation
*cs.validator = *hotstuff.NewValidator(cs.T())
sentinelError := model.NewInvalidProposalErrorf(hotstuffProposal, "")
cs.validator.On("ValidateProposal", hotstuffProposal).Return(sentinelError)
cs.proposalViolationNotifier.On("OnInvalidBlockDetected", flow.Slashable[model.InvalidProposalError]{
OriginID: originID,
Message: sentinelError.(model.InvalidProposalError),
}).Return().Once()
// we should notify VoteAggregator about the invalid block
cs.voteAggregator.On("InvalidBlock", hotstuffProposal).Return(nil)
// the expected error should be handled within the Core
err := cs.core.OnBlockProposal(flow.Slashable[*messages.BlockProposal]{
OriginID: originID,
Message: proposal,
})
require.NoError(cs.T(), err, "proposal with invalid extension should fail")
// we should not extend the state with the header
cs.state.AssertNotCalled(cs.T(), "Extend", mock.Anything, block)
// we should not attempt to process the children
cs.pending.AssertNotCalled(cs.T(), "ByParentID", mock.Anything)
})
cs.Run("view for unknown epoch error", func() {
// the block fails HotStuff validation
*cs.validator = *hotstuff.NewValidator(cs.T())
cs.validator.On("ValidateProposal", hotstuffProposal).Return(model.ErrViewForUnknownEpoch)
// the expected error should be handled within the Core
err := cs.core.OnBlockProposal(flow.Slashable[*messages.BlockProposal]{
OriginID: originID,
Message: proposal,
})
require.NoError(cs.T(), err, "proposal with invalid extension should fail")
// we should not extend the state with the header
cs.state.AssertNotCalled(cs.T(), "Extend", mock.Anything, block)
// we should not attempt to process the children
cs.pending.AssertNotCalled(cs.T(), "ByParentID", mock.Anything)
})
cs.Run("unexpected error", func() {
// the block fails HotStuff validation
unexpectedErr := errors.New("generic unexpected error")
*cs.validator = *hotstuff.NewValidator(cs.T())
cs.validator.On("ValidateProposal", hotstuffProposal).Return(unexpectedErr)
// the error should be propagated
err := cs.core.OnBlockProposal(flow.Slashable[*messages.BlockProposal]{
OriginID: originID,
Message: proposal,
})
require.ErrorIs(cs.T(), err, unexpectedErr)
// we should not extend the state with the header
cs.state.AssertNotCalled(cs.T(), "Extend", mock.Anything, block)
// we should not attempt to process the children
cs.pending.AssertNotCalled(cs.T(), "ByParentID", mock.Anything)
})
}
// TestOnBlockProposal_FailsProtocolStateValidation tests processing a proposal which passes HotStuff validation,
// but fails protocol state validation
// - should not be added to the state
// - we should not attempt to process its children
// - we should notify VoteAggregator, for known errors
func (cs *CoreSuite) TestOnBlockProposal_FailsProtocolStateValidation() {
// create a proposal that has two ancestors in the cache
originID := cs.participants[1].NodeID
ancestor := unittest.BlockWithParentFixture(cs.head)
parent := unittest.BlockWithParentFixture(ancestor.Header)
block := unittest.BlockWithParentFixture(parent.Header)
proposal := unittest.ProposalFromBlock(block)
hotstuffProposal := model.ProposalFromFlow(block.Header)
// store the data for retrieval
cs.headerDB[parent.ID()] = parent.Header
cs.headerDB[ancestor.ID()] = ancestor.Header
// the block passes HotStuff validation
cs.validator.On("ValidateProposal", hotstuffProposal).Return(nil)
cs.Run("invalid block", func() {
// make sure we fail to extend the state
*cs.state = protocol.ParticipantState{}
cs.state.On("Final").Return(func() protint.Snapshot { return cs.snapshot })
sentinelErr := state.NewInvalidExtensionError("")
cs.state.On("Extend", mock.Anything, mock.Anything).Return(sentinelErr)
cs.proposalViolationNotifier.On("OnInvalidBlockDetected", mock.Anything).Run(func(args mock.Arguments) {
err := args.Get(0).(flow.Slashable[model.InvalidProposalError])
require.ErrorIs(cs.T(), err.Message, sentinelErr)
require.Equal(cs.T(), err.Message.InvalidProposal, hotstuffProposal)
require.Equal(cs.T(), err.OriginID, originID)
}).Return().Once()
// we should notify VoteAggregator about the invalid block
cs.voteAggregator.On("InvalidBlock", hotstuffProposal).Return(nil)
// the expected error should be handled within the Core
err := cs.core.OnBlockProposal(flow.Slashable[*messages.BlockProposal]{
OriginID: originID,
Message: proposal,
})
require.NoError(cs.T(), err, "proposal with invalid extension should fail")
// we should extend the state with the header
cs.state.AssertCalled(cs.T(), "Extend", mock.Anything, block)
// we should not pass the block to hotstuff
cs.hotstuff.AssertNotCalled(cs.T(), "SubmitProposal", mock.Anything)
// we should not attempt to process the children
cs.pending.AssertNotCalled(cs.T(), "ByParentID", mock.Anything)
})
cs.Run("outdated block", func() {
// make sure we fail to extend the state
*cs.state = protocol.ParticipantState{}
cs.state.On("Final").Return(func() protint.Snapshot { return cs.snapshot })
cs.state.On("Extend", mock.Anything, mock.Anything).Return(state.NewOutdatedExtensionError(""))
// the expected error should be handled within the Core
err := cs.core.OnBlockProposal(flow.Slashable[*messages.BlockProposal]{
OriginID: originID,
Message: proposal,
})
require.NoError(cs.T(), err, "proposal with invalid extension should fail")
// we should extend the state with the header
cs.state.AssertCalled(cs.T(), "Extend", mock.Anything, block)
// we should not pass the block to hotstuff
cs.hotstuff.AssertNotCalled(cs.T(), "SubmitProposal", mock.Anything)
// we should not attempt to process the children
cs.pending.AssertNotCalled(cs.T(), "ByParentID", mock.Anything)
})
cs.Run("unexpected error", func() {
// make sure we fail to extend the state
*cs.state = protocol.ParticipantState{}
cs.state.On("Final").Return(func() protint.Snapshot { return cs.snapshot })
unexpectedErr := errors.New("unexpected generic error")
cs.state.On("Extend", mock.Anything, mock.Anything).Return(unexpectedErr)
// it should be processed without error
err := cs.core.OnBlockProposal(flow.Slashable[*messages.BlockProposal]{
OriginID: originID,
Message: proposal,
})
require.ErrorIs(cs.T(), err, unexpectedErr)
// we should extend the state with the header
cs.state.AssertCalled(cs.T(), "Extend", mock.Anything, block)
// we should not pass the block to hotstuff
cs.hotstuff.AssertNotCalled(cs.T(), "SubmitProposal", mock.Anything)
// we should not attempt to process the children
cs.pending.AssertNotCalled(cs.T(), "ByParentID", mock.Anything)
})
}
func (cs *CoreSuite) TestProcessBlockAndDescendants() {
// create three children blocks
parent := unittest.BlockWithParentFixture(cs.head)
block1 := unittest.BlockWithParentFixture(parent.Header)
block2 := unittest.BlockWithParentFixture(parent.Header)
block3 := unittest.BlockWithParentFixture(parent.Header)
// create the pending blocks
pending1 := unittest.AsSlashable(block1)
pending2 := unittest.AsSlashable(block2)
pending3 := unittest.AsSlashable(block3)
// store the parent on disk
parentID := parent.ID()
cs.headerDB[parentID] = parent.Header
// store the pending children in the cache
cs.childrenDB[parentID] = append(cs.childrenDB[parentID], pending1)
cs.childrenDB[parentID] = append(cs.childrenDB[parentID], pending2)
cs.childrenDB[parentID] = append(cs.childrenDB[parentID], pending3)
for _, block := range []*flow.Block{parent, block1, block2, block3} {
hotstuffProposal := model.ProposalFromFlow(block.Header)
cs.validator.On("ValidateProposal", hotstuffProposal).Return(nil)
cs.voteAggregator.On("AddBlock", hotstuffProposal).Once()
cs.hotstuff.On("SubmitProposal", hotstuffProposal).Once()
}
// execute the connected children handling
err := cs.core.processBlockAndDescendants(flow.Slashable[*flow.Block]{
OriginID: unittest.IdentifierFixture(),
Message: parent,
})
require.NoError(cs.T(), err, "should pass handling children")
// make sure we drop the cache after trying to process
cs.pending.AssertCalled(cs.T(), "DropForParent", parent.Header.ID())
}
func (cs *CoreSuite) TestProposalBufferingOrder() {
// create a proposal that we will not submit until the end
originID := cs.participants[1].NodeID
missingBlock := unittest.BlockWithParentFixture(cs.head)
missingProposal := unittest.ProposalFromBlock(missingBlock)
// create a chain of descendants
var proposals []*messages.BlockProposal
parent := missingProposal
for i := 0; i < 3; i++ {
descendant := unittest.BlockWithParentFixture(&parent.Block.Header)
proposal := unittest.ProposalFromBlock(descendant)
proposals = append(proposals, proposal)
parent = proposal
}
// replace the engine buffer with the real one
cs.core.pending = real.NewPendingBlocks()
// check that we request the ancestor block each time
cs.sync.On("RequestBlock", missingBlock.Header.ID(), missingBlock.Header.Height).Times(len(proposals))
// process all the descendants
for _, proposal := range proposals {
// process and make sure no error occurs (as they are unverifiable)
err := cs.core.OnBlockProposal(flow.Slashable[*messages.BlockProposal]{
OriginID: originID,
Message: proposal,
})
require.NoError(cs.T(), err, "proposal buffering should pass")
// make sure no block is forwarded to hotstuff
cs.hotstuff.AssertNotCalled(cs.T(), "SubmitProposal", model.ProposalFromFlow(&proposal.Block.Header))
}
// check that we submit each proposal in a valid order
// - we must process the missingProposal parent first
// - we can process the children next, in any order
cs.validator.On("ValidateProposal", mock.Anything).Return(nil).Times(4)
calls := 0 // track # of calls to SubmitProposal
unprocessed := map[flow.Identifier]struct{}{ // track un-processed proposals
missingProposal.Block.Header.ID(): {},
proposals[0].Block.Header.ID(): {},
proposals[1].Block.Header.ID(): {},
proposals[2].Block.Header.ID(): {},
}
cs.hotstuff.On("SubmitProposal", mock.Anything).Times(4).Run(
func(args mock.Arguments) {
proposal := args.Get(0).(*model.Proposal)
header := proposal.Block
if calls == 0 {
// first header processed must be the common parent
assert.Equal(cs.T(), missingProposal.Block.Header.ID(), header.BlockID)
}
// mark the proposal as processed
delete(unprocessed, header.BlockID)
cs.headerDB[header.BlockID] = model.ProposalToFlow(proposal)
calls++
},
)
cs.voteAggregator.On("AddBlock", mock.Anything).Times(4)
// process the root proposal
err := cs.core.OnBlockProposal(flow.Slashable[*messages.BlockProposal]{
OriginID: originID,
Message: missingProposal,
})
require.NoError(cs.T(), err, "root proposal should pass")
// all proposals should be processed
assert.Len(cs.T(), unprocessed, 0)
}