github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/consensus/hotstuff/notifications/pubsub/distributor.go (about)

     1  package pubsub
     2  
     3  import (
     4  	"github.com/onflow/flow-go/consensus/hotstuff"
     5  )
     6  
     7  // Distributor distributes notifications to a list of consumers (event consumers).
     8  //
     9  // It allows thread-safe subscription of multiple consumers to events.
    10  type Distributor struct {
    11  	*FollowerDistributor
    12  	*CommunicatorDistributor
    13  	*ParticipantDistributor
    14  }
    15  
    16  var _ hotstuff.Consumer = (*Distributor)(nil)
    17  
    18  func NewDistributor() *Distributor {
    19  	return &Distributor{
    20  		FollowerDistributor:     NewFollowerDistributor(),
    21  		CommunicatorDistributor: NewCommunicatorDistributor(),
    22  		ParticipantDistributor:  NewParticipantDistributor(),
    23  	}
    24  }
    25  
    26  // AddConsumer adds an event consumer to the Distributor
    27  func (p *Distributor) AddConsumer(consumer hotstuff.Consumer) {
    28  	p.FollowerDistributor.AddFollowerConsumer(consumer)
    29  	p.CommunicatorDistributor.AddCommunicatorConsumer(consumer)
    30  	p.ParticipantDistributor.AddParticipantConsumer(consumer)
    31  }
    32  
    33  // FollowerDistributor ingests consensus follower events and distributes it to consumers.
    34  // It allows thread-safe subscription of multiple consumers to events.
    35  type FollowerDistributor struct {
    36  	*ProposalViolationDistributor
    37  	*FinalizationDistributor
    38  }
    39  
    40  var _ hotstuff.FollowerConsumer = (*FollowerDistributor)(nil)
    41  
    42  func NewFollowerDistributor() *FollowerDistributor {
    43  	return &FollowerDistributor{
    44  		ProposalViolationDistributor: NewProtocolViolationDistributor(),
    45  		FinalizationDistributor:      NewFinalizationDistributor(),
    46  	}
    47  }
    48  
    49  // AddFollowerConsumer registers the input `consumer` to be notified on `hotstuff.ConsensusFollowerConsumer` events.
    50  func (d *FollowerDistributor) AddFollowerConsumer(consumer hotstuff.FollowerConsumer) {
    51  	d.FinalizationDistributor.AddFinalizationConsumer(consumer)
    52  	d.ProposalViolationDistributor.AddProposalViolationConsumer(consumer)
    53  }
    54  
    55  // TimeoutAggregationDistributor ingests timeout aggregation events and distributes it to consumers.
    56  // It allows thread-safe subscription of multiple consumers to events.
    57  type TimeoutAggregationDistributor struct {
    58  	*TimeoutAggregationViolationDistributor
    59  	*TimeoutCollectorDistributor
    60  }
    61  
    62  var _ hotstuff.TimeoutAggregationConsumer = (*TimeoutAggregationDistributor)(nil)
    63  
    64  func NewTimeoutAggregationDistributor() *TimeoutAggregationDistributor {
    65  	return &TimeoutAggregationDistributor{
    66  		TimeoutAggregationViolationDistributor: NewTimeoutAggregationViolationDistributor(),
    67  		TimeoutCollectorDistributor:            NewTimeoutCollectorDistributor(),
    68  	}
    69  }
    70  
    71  func (d *TimeoutAggregationDistributor) AddTimeoutAggregationConsumer(consumer hotstuff.TimeoutAggregationConsumer) {
    72  	d.TimeoutAggregationViolationDistributor.AddTimeoutAggregationViolationConsumer(consumer)
    73  	d.TimeoutCollectorDistributor.AddTimeoutCollectorConsumer(consumer)
    74  }
    75  
    76  // VoteAggregationDistributor ingests vote aggregation events and distributes it to consumers.
    77  // It allows thread-safe subscription of multiple consumers to events.
    78  type VoteAggregationDistributor struct {
    79  	*VoteAggregationViolationDistributor
    80  	*VoteCollectorDistributor
    81  }
    82  
    83  var _ hotstuff.VoteAggregationConsumer = (*VoteAggregationDistributor)(nil)
    84  
    85  func NewVoteAggregationDistributor() *VoteAggregationDistributor {
    86  	return &VoteAggregationDistributor{
    87  		VoteAggregationViolationDistributor: NewVoteAggregationViolationDistributor(),
    88  		VoteCollectorDistributor:            NewQCCreatedDistributor(),
    89  	}
    90  }
    91  
    92  func (d *VoteAggregationDistributor) AddVoteAggregationConsumer(consumer hotstuff.VoteAggregationConsumer) {
    93  	d.VoteAggregationViolationDistributor.AddVoteAggregationViolationConsumer(consumer)
    94  	d.VoteCollectorDistributor.AddVoteCollectorConsumer(consumer)
    95  }