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

     1  package pubsub
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/onflow/flow-go/consensus/hotstuff"
     7  	"github.com/onflow/flow-go/consensus/hotstuff/model"
     8  	"github.com/onflow/flow-go/model/flow"
     9  )
    10  
    11  // VoteCollectorDistributor ingests notifications about vote aggregation and
    12  // distributes them to consumers. Such notifications are produced by the vote aggregation logic.
    13  // Concurrently safe.
    14  type VoteCollectorDistributor struct {
    15  	consumers []hotstuff.VoteCollectorConsumer
    16  	lock      sync.RWMutex
    17  }
    18  
    19  var _ hotstuff.VoteCollectorConsumer = (*VoteCollectorDistributor)(nil)
    20  
    21  func NewQCCreatedDistributor() *VoteCollectorDistributor {
    22  	return &VoteCollectorDistributor{}
    23  }
    24  
    25  func (d *VoteCollectorDistributor) AddVoteCollectorConsumer(consumer hotstuff.VoteCollectorConsumer) {
    26  	d.lock.Lock()
    27  	defer d.lock.Unlock()
    28  	d.consumers = append(d.consumers, consumer)
    29  }
    30  
    31  func (d *VoteCollectorDistributor) OnQcConstructedFromVotes(qc *flow.QuorumCertificate) {
    32  	d.lock.RLock()
    33  	defer d.lock.RUnlock()
    34  	for _, consumer := range d.consumers {
    35  		consumer.OnQcConstructedFromVotes(qc)
    36  	}
    37  }
    38  
    39  func (d *VoteCollectorDistributor) OnVoteProcessed(vote *model.Vote) {
    40  	d.lock.RLock()
    41  	defer d.lock.RUnlock()
    42  	for _, subscriber := range d.consumers {
    43  		subscriber.OnVoteProcessed(vote)
    44  	}
    45  }