-
Notifications
You must be signed in to change notification settings - Fork 175
/
event_handler.go
551 lines (465 loc) · 18.1 KB
/
event_handler.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
package eventhandler
import (
"errors"
"fmt"
"time"
"github.com/rs/zerolog"
"github.com/onflow/flow-go/consensus/hotstuff"
"github.com/onflow/flow-go/consensus/hotstuff/model"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/utils/logging"
)
// EventHandler is the main handler for individual events that trigger state transition.
// It exposes API to handle one event at a time synchronously. The caller is
// responsible for running the event loop to ensure that.
type EventHandler struct {
log zerolog.Logger
paceMaker hotstuff.PaceMaker
blockProducer hotstuff.BlockProducer
forks hotstuff.Forks
persist hotstuff.Persister
communicator hotstuff.Communicator
committee hotstuff.Committee
voteAggregator hotstuff.VoteAggregator
voter hotstuff.Voter
validator hotstuff.Validator
notifier hotstuff.Consumer
ownProposal flow.Identifier
}
// New creates an EventHandler instance with initial components.
func New(
log zerolog.Logger,
paceMaker hotstuff.PaceMaker,
blockProducer hotstuff.BlockProducer,
forks hotstuff.Forks,
persist hotstuff.Persister,
communicator hotstuff.Communicator,
committee hotstuff.Committee,
voteAggregator hotstuff.VoteAggregator,
voter hotstuff.Voter,
validator hotstuff.Validator,
notifier hotstuff.Consumer,
) (*EventHandler, error) {
e := &EventHandler{
log: log.With().Str("hotstuff", "participant").Logger(),
paceMaker: paceMaker,
blockProducer: blockProducer,
forks: forks,
persist: persist,
communicator: communicator,
voteAggregator: voteAggregator,
voter: voter,
validator: validator,
committee: committee,
notifier: notifier,
ownProposal: flow.ZeroID,
}
return e, nil
}
// OnReceiveVote processes the vote when a vote is received.
// It is assumed that the voting block is not a missing block
func (e *EventHandler) OnReceiveVote(vote *model.Vote) error {
curView := e.paceMaker.CurView()
log := e.log.With().
Uint64("cur_view", curView).
Uint64("block_view", vote.View).
Hex("block_id", vote.BlockID[:]).
Hex("signer", vote.SignerID[:]).
Logger()
e.notifier.OnReceiveVote(curView, vote)
defer e.notifier.OnEventProcessed()
log.Debug().Msg("vote forwarded from compliance engine")
// votes for finalized view or older should be dropped:
if vote.View <= e.forks.FinalizedView() {
log.Debug().Msg("skipping vote view equal or below finalized view")
return nil
}
err := e.processVote(vote)
if err != nil {
return fmt.Errorf("failed processing vote: %w", err)
}
log.Debug().Msg("block vote processed")
return nil
}
// OnReceiveProposal processes the block when a block proposal is received.
// It is assumed that the block proposal is incorporated. (its parent can be found
// in the forks)
func (e *EventHandler) OnReceiveProposal(proposal *model.Proposal) error {
block := proposal.Block
curView := e.paceMaker.CurView()
log := e.log.With().
Uint64("cur_view", curView).
Uint64("block_view", block.View).
Hex("block_id", block.BlockID[:]).
Uint64("qc_view", block.QC.View).
Hex("proposer_id", block.ProposerID[:]).
Logger()
e.notifier.OnReceiveProposal(curView, proposal)
defer e.notifier.OnEventProcessed()
log.Debug().Msg("proposal forwarded from compliance engine")
// ignore stale proposals
if block.View < e.forks.FinalizedView() {
log.Debug().Msg("stale proposal")
return nil
}
// we skip validation for our last own proposal
if proposal.Block.BlockID != e.ownProposal {
// validate the block. exit if the proposal is invalid
err := e.validator.ValidateProposal(proposal)
if model.IsInvalidBlockError(err) {
log.Warn().Err(err).Msg("invalid block proposal")
return nil
}
if errors.Is(err, model.ErrUnverifiableBlock) {
log.Warn().Err(err).Msg("unverifiable block proposal")
// even if the block is unverifiable because the QC has been
// pruned, it still needs to be added to the forks, otherwise,
// a new block with a QC to this block will fail to be added
// to forks and crash the event loop.
} else if err != nil {
return fmt.Errorf("cannot validate proposal (%x): %w", block.BlockID, err)
}
}
// store the block. the block will also be validated there
err := e.forks.AddBlock(block)
if err != nil {
return fmt.Errorf("cannot add block to fork (%x): %w", block.BlockID, err)
}
// store the proposer's vote in voteAggregator
stored := e.voteAggregator.StoreProposerVote(proposal.ProposerVote())
// if the block is for the current view, then process the current block
if block.View == curView {
err = e.processBlockForCurrentView(block)
if err != nil {
return fmt.Errorf("failed processing current block: %w", err)
}
newView := e.paceMaker.CurView() // in case view has changed
log.Debug().Uint64("new_view", newView).Msg("block proposal for current view processed")
return nil
}
err = e.tryBuildQCForBlock(block)
if err != nil {
return fmt.Errorf("failed processing qc from block: %w", err)
}
newView := e.paceMaker.CurView() // in case we skipped ahead
log.Debug().Uint64("new_view", newView).Bool("proposer_vote_stored", stored).Msg("block proposal for non-current view processed")
return nil
}
// TimeoutChannel returns the channel for subscribing the waiting timeout on receiving
// block or votes for the current view.
func (e *EventHandler) TimeoutChannel() <-chan time.Time {
return e.paceMaker.TimeoutChannel()
}
// OnLocalTimeout is called when the timeout event created by pacemaker looped through the
// event loop.
func (e *EventHandler) OnLocalTimeout() error {
curView := e.paceMaker.CurView()
newView := e.paceMaker.OnTimeout()
defer e.notifier.OnEventProcessed()
log := e.log.With().
Uint64("cur_view", curView).
Uint64("new_view", newView.View).
Logger()
// notifications about time-outs and view-changes are generated by PaceMaker; no need to send a notification here
log.Debug().Msg("timeout received from event loop")
if curView == newView.View {
return fmt.Errorf("OnLocalTimeout should guarantee that the pacemaker should go to next view, but didn't: (curView: %v, newView: %v)", curView, newView.View)
}
// current view has changed, go to new view
err := e.startNewView()
if err != nil {
return fmt.Errorf("could not start new view: %w", err)
}
log.Debug().Msg("local timeout processed")
return nil
}
// Start will start the pacemaker's timer and start the new view
func (e *EventHandler) Start() error {
e.paceMaker.Start()
return e.startNewView()
}
// startNewView will only be called when there is a view change from pacemaker.
// It reads the current view, and check if it needs to propose or vote in this view.
func (e *EventHandler) startNewView() error {
// track the start time
start := time.Now()
curView := e.paceMaker.CurView()
err := e.persist.PutStarted(curView)
if err != nil {
return fmt.Errorf("could not persist current view: %w", err)
}
currentLeader, err := e.committee.LeaderForView(curView)
if err != nil {
return fmt.Errorf("failed to determine primary for new view %d: %w", curView, err)
}
log := e.log.With().
Uint64("cur_view", curView).
Hex("leader_id", currentLeader[:]).Logger()
log.Debug().
Uint64("finalized_view", e.forks.FinalizedView()).
Msg("entering new view")
e.notifier.OnEnteringView(curView, currentLeader)
e.pruneSubcomponents()
if e.committee.Self() == currentLeader {
log.Debug().Msg("generating block proposal as leader")
// as the leader of the current view,
// build the block proposal for the current view
qc, _, err := e.forks.MakeForkChoice(curView)
if err != nil {
return fmt.Errorf("can not make fork choice for view %v: %w", curView, err)
}
proposal, err := e.blockProducer.MakeBlockProposal(qc, curView)
if err != nil {
return fmt.Errorf("can not make block proposal for curView %v: %w", curView, err)
}
e.notifier.OnProposingBlock(proposal)
block := proposal.Block
log.Debug().
Uint64("block_view", block.View).
Hex("block_id", block.BlockID[:]).
Uint64("parent_view", qc.View).
Hex("parent_id", qc.BlockID[:]).
Hex("signer", block.ProposerID[:]).
Msg("forwarding proposal to communicator for broadcasting")
// broadcast the proposal
header := model.ProposalToFlow(proposal)
delay := e.paceMaker.BlockRateDelay()
elapsed := time.Since(start)
if elapsed > delay {
delay = 0
} else {
delay = delay - elapsed
}
err = e.communicator.BroadcastProposalWithDelay(header, delay)
if err != nil {
log.Warn().Err(err).Msg("could not forward proposal")
}
// mark our own proposals to avoid double validation
e.ownProposal = proposal.Block.BlockID
// We return here to correspond to the HotStuff state machine.
return nil
// Algorithmically, this return statement is optional:
// * If this replica is the leader for the current view, there can be no valid proposal from any
// other node. This replica's proposal is the only valid proposal.
// * This replica's proposal got just sent out above. It will enter the HotStuff logic from the
// EventLoop. In other words, the own proposal is not yet stored in Forks.
// Hence, Forks cannot contain _any_ valid proposal for the current view.
// Therefore, if this replica is the leader, the following code is a no-op.
}
// as a replica of the current view, find and process the block for the current view
blocks := e.forks.GetBlocksForView(curView)
if len(blocks) == 0 {
// if there is no block stored before for the current view, then exit and keep waiting
log.Debug().Msg("waiting for proposal from leader")
return nil
}
// when there are multiple block proposals, we will just pick the first one.
// forks is responsible for slashing double proposal behavior, and
// event handler is aware of double proposals, but picking any should work and
// won't hurt safeness
block := blocks[0]
log.Debug().
Uint64("block_view", block.View).
Hex("block_id", block.BlockID[:]).
Uint64("parent_view", block.QC.View).
Hex("parent_id", block.QC.BlockID[:]).
Hex("signer", block.ProposerID[:]).
Msg("processing cached proposal from leader")
return e.processBlockForCurrentView(block)
}
// pruneSubcomponents prunes EventHandler's sub-components
// Currently, the implementation follows the simplest design to prune once when we enter a
// new view instead of immediately when a block is finalized.
//
// Technically, EventHandler (and all other components) could consume Forks' OnBlockFinalized
// notifications and prune immediately. However, we have followed the design paradigm that all
// events are only for HotStuff-External components. The interaction of the HotStuff-internal
// components is directly handled by the EventHandler.
func (e *EventHandler) pruneSubcomponents() {
e.voteAggregator.PruneByView(e.forks.FinalizedView())
}
// processBlockForCurrentView processes the block for the current view.
// It is called AFTER the block has been stored or found in Forks
// It checks whether to vote for this block.
// It might trigger a view change to go to a different view, which might re-enter this function.
func (e *EventHandler) processBlockForCurrentView(block *model.Block) error {
// this is a sanity check to see if the block is really for the current view.
curView := e.paceMaker.CurView()
if block.View != curView {
return fmt.Errorf("sanity check fails: block proposal's view does not match with curView, (blockView: %v, curView: %v)",
block.View, curView)
}
// checking if I'm the next leader
nextView := curView + 1
nextLeader, err := e.committee.LeaderForView(nextView)
if err != nil {
return fmt.Errorf("failed to determine primary for next view %d: %w", nextView, err)
}
if e.committee.Self() == nextLeader {
return e.processBlockForCurrentViewIfIsNextLeader(block)
}
// if I'm not the next leader
return e.processBlockForCurrentViewIfIsNotNextLeader(block, nextLeader)
}
func (e *EventHandler) processBlockForCurrentViewIfIsNextLeader(block *model.Block) error {
log := e.log.With().
Uint64("block_view", block.View).
Hex("block_id", block.BlockID[:]).
Uint64("parent_view", block.QC.View).
Hex("parent_id", block.QC.BlockID[:]).
Hex("signer", block.ProposerID[:]).
Logger()
// voter performs all the checks to decide whether to vote for this block or not.
// note this call has to make before calling pacemaker, because calling to pace maker first might
// cause the `curView` here to be stale
isNextLeader := true
curView := block.View
shouldVote := true
ownVote, err := e.voter.ProduceVoteIfVotable(block, curView)
if err != nil {
if !model.IsNoVoteError(err) {
// unknown error, exit the event loop
return fmt.Errorf("could not produce vote: %w", err)
}
log.Debug().Err(err).Msg("should not vote for this block")
shouldVote = false
}
// if I'm the next leader, then stay at the current view to collect votes for the block of the current view.
nv, viewChanged := e.paceMaker.UpdateCurViewWithBlock(block, isNextLeader)
if viewChanged {
// this is a sanity check.
// when I've processed the block for the current view and I'm the next leader,
// the pacemaker should not trigger a view change.
return fmt.Errorf("pacemaker should not trigger a view change, but it changed to view. (newview: %v)", nv.View)
}
if !shouldVote {
// even if we are not voting for the block, we will still check if a QC can be built for this block.
err := e.tryBuildQCForBlock(block)
if err != nil {
return fmt.Errorf("failed to process QC for block when not voting: %w", err)
}
return nil
}
// since I'm the next leader, instead of sending the vote through the network and receiving it
// back, it can be processed locally right away.
e.notifier.OnVoting(ownVote)
e.notifier.OnEventProcessed()
e.notifier.OnReceiveVote(curView, ownVote)
log.Debug().Msg("processing own vote as next leader")
return e.processVote(ownVote)
}
func (e *EventHandler) processBlockForCurrentViewIfIsNotNextLeader(block *model.Block, nextLeader flow.Identifier) error {
log := e.log.With().
Uint64("block_view", block.View).
Hex("block_id", block.BlockID[:]).
Uint64("parent_view", block.QC.View).
Hex("parent_id", block.QC.BlockID[:]).
Hex("signer", block.ProposerID[:]).
Logger()
// voter performs all the checks to decide whether to vote for this block or not.
isNextLeader := false
curView := block.View
shouldVote := true
ownVote, err := e.voter.ProduceVoteIfVotable(block, curView)
if err != nil {
if !model.IsNoVoteError(err) {
// unknown error, exit the event loop
return fmt.Errorf("could not produce vote: %w", err)
}
log.Debug().Err(err).Msg("should not vote for this block")
shouldVote = false
}
// send my vote if I should vote and I'm not the leader
if shouldVote {
e.notifier.OnVoting(ownVote)
log.Debug().Msg("forwarding vote to compliance engine")
err := e.communicator.SendVote(ownVote.BlockID, ownVote.View, ownVote.SigData, nextLeader)
if err != nil {
log.Warn().Err(err).Msg("could not forward vote")
}
}
// inform pacemaker that we've done the work for the current view, it should increment the current view
_, viewChanged := e.paceMaker.UpdateCurViewWithBlock(block, isNextLeader)
if !viewChanged {
// this is a sanity check
// when I've processed the block for the current view and I'm not the next leader,
// the pacemaker should trigger a view change
nextView := curView + 1
return fmt.Errorf("pacemaker should trigger a view change to (view %v), but didn't", nextView)
}
// current view has changed, go to new view
return e.startNewView()
}
// tryBuildQCForBlock checks whether there are enough votes to build a QC for the given block,
// and process the QC if a QC was built.
func (e *EventHandler) tryBuildQCForBlock(block *model.Block) error {
// if the block is not for the current view, try to build QC from votes for this block
qc, built, err := e.voteAggregator.BuildQCOnReceivedBlock(block)
if err != nil {
return fmt.Errorf("building qc for block (%x) failed: %w", block.BlockID, err)
}
if !built {
// if we don't have enough votes to build QC for this block, proceed with block.qc instead
qc = block.QC
}
// process the QC
return e.processQC(qc)
}
// processVote stores the vote and check whether a QC can be built.
// If a QC is built, then process the QC.
// It assumes the voting block can be found in forks
func (e *EventHandler) processVote(vote *model.Vote) error {
log := e.log.With().
Uint64("block_view", vote.View).
Hex("block_id", logging.ID(vote.BlockID)).
Hex("signer", vote.SignerID[:]).
Logger()
// read the voting block
block, found := e.forks.GetBlock(vote.BlockID)
if !found {
// store the pending vote if voting block is not found.
// We don't need to proactively fetch the missing voting block, because the chain compliance layer has acknowledged
// the missing block and requested it already.
_, err := e.voteAggregator.StorePendingVote(vote)
if err != nil {
return fmt.Errorf("can not store pending vote: %w", err)
}
log.Debug().Msg("block for vote not found, caching for later")
return nil
}
// if the voting block can be found, we should be able to validate the vote
// and check if we can build a QC with it.
qc, built, err := e.voteAggregator.StoreVoteAndBuildQC(vote, block)
if err != nil {
return fmt.Errorf("building qc for block failed: %w", err)
}
// if we don't have enough votes to build QC for this block:
// nothing more to do for processing vote
if !built {
log.Debug().Msg("insufficient votes for QC, waiting for more")
return nil
}
log.Debug().Msg("enough votes for QC collected")
return e.processQC(qc)
}
// processQC stores the QC and check whether the QC will trigger view change.
// If triggered, then go to the new view.
func (e *EventHandler) processQC(qc *flow.QuorumCertificate) error {
log := e.log.With().
Uint64("block_view", qc.View).
Hex("block_id", qc.BlockID[:]).
Int("signers", len(qc.SignerIDs)).
Logger()
err := e.forks.AddQC(qc)
if err != nil {
return fmt.Errorf("cannot add QC to forks: %w", err)
}
_, viewChanged := e.paceMaker.UpdateCurViewWithQC(qc)
if !viewChanged {
log.Debug().Msg("QC didn't trigger view change, nothing to do")
return nil
}
log.Debug().Msg("QC triggered view change, starting new view now")
// current view has changed, go to new view
return e.startNewView()
}