github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/consensus/aggregators.go (about) 1 package consensus 2 3 import ( 4 "fmt" 5 6 "github.com/gammazero/workerpool" 7 "github.com/rs/zerolog" 8 9 "github.com/onflow/flow-go/consensus/hotstuff" 10 "github.com/onflow/flow-go/consensus/hotstuff/notifications/pubsub" 11 "github.com/onflow/flow-go/consensus/hotstuff/timeoutaggregator" 12 "github.com/onflow/flow-go/consensus/hotstuff/timeoutcollector" 13 "github.com/onflow/flow-go/consensus/hotstuff/voteaggregator" 14 "github.com/onflow/flow-go/consensus/hotstuff/votecollector" 15 "github.com/onflow/flow-go/module" 16 ) 17 18 // NewVoteAggregator creates new VoteAggregator and subscribes for finalization events. 19 // No error returns are expected during normal operations. 20 func NewVoteAggregator( 21 log zerolog.Logger, 22 hotstuffMetrics module.HotstuffMetrics, 23 engineMetrics module.EngineMetrics, 24 mempoolMetrics module.MempoolMetrics, 25 lowestRetainedView uint64, 26 notifier hotstuff.VoteAggregationConsumer, 27 voteProcessorFactory hotstuff.VoteProcessorFactory, 28 distributor *pubsub.FollowerDistributor, 29 ) (hotstuff.VoteAggregator, error) { 30 31 createCollectorFactoryMethod := votecollector.NewStateMachineFactory(log, notifier, voteProcessorFactory.Create) 32 voteCollectors := voteaggregator.NewVoteCollectors(log, lowestRetainedView, workerpool.New(4), createCollectorFactoryMethod) 33 34 // initialize the vote aggregator 35 aggregator, err := voteaggregator.NewVoteAggregator( 36 log, 37 hotstuffMetrics, 38 engineMetrics, 39 mempoolMetrics, 40 notifier, 41 lowestRetainedView, 42 voteCollectors, 43 ) 44 if err != nil { 45 return nil, fmt.Errorf("could not create vote aggregator: %w", err) 46 } 47 distributor.AddOnBlockFinalizedConsumer(aggregator.OnFinalizedBlock) 48 49 return aggregator, nil 50 } 51 52 // NewTimeoutAggregator creates new TimeoutAggregator and connects Hotstuff event source with event handler. 53 // No error returns are expected during normal operations. 54 func NewTimeoutAggregator(log zerolog.Logger, 55 hotstuffMetrics module.HotstuffMetrics, 56 engineMetrics module.EngineMetrics, 57 mempoolMetrics module.MempoolMetrics, 58 notifier *pubsub.Distributor, 59 timeoutProcessorFactory hotstuff.TimeoutProcessorFactory, 60 distributor *pubsub.TimeoutAggregationDistributor, 61 lowestRetainedView uint64, 62 ) (hotstuff.TimeoutAggregator, error) { 63 64 timeoutCollectorFactory := timeoutcollector.NewTimeoutCollectorFactory(log, distributor, timeoutProcessorFactory) 65 collectors := timeoutaggregator.NewTimeoutCollectors(log, hotstuffMetrics, lowestRetainedView, timeoutCollectorFactory) 66 67 // initialize the timeout aggregator 68 aggregator, err := timeoutaggregator.NewTimeoutAggregator( 69 log, 70 hotstuffMetrics, 71 engineMetrics, 72 mempoolMetrics, 73 lowestRetainedView, 74 collectors, 75 ) 76 if err != nil { 77 return nil, fmt.Errorf("could not create timeout aggregator: %w", err) 78 } 79 notifier.AddConsumer(aggregator) 80 81 return aggregator, nil 82 }