github.com/onflow/flow-go@v0.33.17/consensus/hotstuff/notifications/pubsub/communicator_distributor.go (about)

     1  package pubsub
     2  
     3  import (
     4  	"sync"
     5  	"time"
     6  
     7  	"github.com/onflow/flow-go/consensus/hotstuff"
     8  	"github.com/onflow/flow-go/consensus/hotstuff/model"
     9  	"github.com/onflow/flow-go/model/flow"
    10  )
    11  
    12  // CommunicatorDistributor ingests outbound consensus messages from HotStuff's core logic and
    13  // distributes them to consumers. This logic only runs inside active consensus participants proposing
    14  // blocks, voting, collecting + aggregating votes to QCs, and participating in the pacemaker (sending
    15  // timeouts, collecting + aggregating timeouts to TCs).
    16  // Concurrently safe.
    17  type CommunicatorDistributor struct {
    18  	consumers []hotstuff.CommunicatorConsumer
    19  	lock      sync.RWMutex
    20  }
    21  
    22  var _ hotstuff.CommunicatorConsumer = (*CommunicatorDistributor)(nil)
    23  
    24  func NewCommunicatorDistributor() *CommunicatorDistributor {
    25  	return &CommunicatorDistributor{}
    26  }
    27  
    28  func (d *CommunicatorDistributor) AddCommunicatorConsumer(consumer hotstuff.CommunicatorConsumer) {
    29  	d.lock.Lock()
    30  	defer d.lock.Unlock()
    31  	d.consumers = append(d.consumers, consumer)
    32  }
    33  
    34  func (d *CommunicatorDistributor) OnOwnVote(blockID flow.Identifier, view uint64, sigData []byte, recipientID flow.Identifier) {
    35  	d.lock.RLock()
    36  	defer d.lock.RUnlock()
    37  	for _, s := range d.consumers {
    38  		s.OnOwnVote(blockID, view, sigData, recipientID)
    39  	}
    40  }
    41  
    42  func (d *CommunicatorDistributor) OnOwnTimeout(timeout *model.TimeoutObject) {
    43  	d.lock.RLock()
    44  	defer d.lock.RUnlock()
    45  	for _, s := range d.consumers {
    46  		s.OnOwnTimeout(timeout)
    47  	}
    48  }
    49  
    50  func (d *CommunicatorDistributor) OnOwnProposal(proposal *flow.Header, targetPublicationTime time.Time) {
    51  	d.lock.RLock()
    52  	defer d.lock.RUnlock()
    53  	for _, s := range d.consumers {
    54  		s.OnOwnProposal(proposal, targetPublicationTime)
    55  	}
    56  }