github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/consensus/hotstuff/notifications/pubsub/timeout_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  // TimeoutCollectorDistributor ingests notifications about timeout aggregation and
    12  // distributes them to consumers. Such notifications are produced by the timeout aggregation logic.
    13  // Concurrently safe.
    14  type TimeoutCollectorDistributor struct {
    15  	lock      sync.RWMutex
    16  	consumers []hotstuff.TimeoutCollectorConsumer
    17  }
    18  
    19  var _ hotstuff.TimeoutCollectorConsumer = (*TimeoutCollectorDistributor)(nil)
    20  
    21  func NewTimeoutCollectorDistributor() *TimeoutCollectorDistributor {
    22  	return &TimeoutCollectorDistributor{}
    23  }
    24  
    25  func (d *TimeoutCollectorDistributor) AddTimeoutCollectorConsumer(consumer hotstuff.TimeoutCollectorConsumer) {
    26  	d.lock.Lock()
    27  	defer d.lock.Unlock()
    28  	d.consumers = append(d.consumers, consumer)
    29  }
    30  
    31  func (d *TimeoutCollectorDistributor) OnTcConstructedFromTimeouts(tc *flow.TimeoutCertificate) {
    32  	d.lock.RLock()
    33  	defer d.lock.RUnlock()
    34  	for _, consumer := range d.consumers {
    35  		consumer.OnTcConstructedFromTimeouts(tc)
    36  	}
    37  }
    38  
    39  func (d *TimeoutCollectorDistributor) OnPartialTcCreated(view uint64, newestQC *flow.QuorumCertificate, lastViewTC *flow.TimeoutCertificate) {
    40  	d.lock.RLock()
    41  	defer d.lock.RUnlock()
    42  	for _, consumer := range d.consumers {
    43  		consumer.OnPartialTcCreated(view, newestQC, lastViewTC)
    44  	}
    45  }
    46  
    47  func (d *TimeoutCollectorDistributor) OnNewQcDiscovered(qc *flow.QuorumCertificate) {
    48  	d.lock.RLock()
    49  	defer d.lock.RUnlock()
    50  	for _, consumer := range d.consumers {
    51  		consumer.OnNewQcDiscovered(qc)
    52  	}
    53  }
    54  
    55  func (d *TimeoutCollectorDistributor) OnNewTcDiscovered(tc *flow.TimeoutCertificate) {
    56  	d.lock.RLock()
    57  	defer d.lock.RUnlock()
    58  	for _, consumer := range d.consumers {
    59  		consumer.OnNewTcDiscovered(tc)
    60  	}
    61  }
    62  
    63  func (d *TimeoutCollectorDistributor) OnTimeoutProcessed(timeout *model.TimeoutObject) {
    64  	d.lock.RLock()
    65  	defer d.lock.RUnlock()
    66  	for _, subscriber := range d.consumers {
    67  		subscriber.OnTimeoutProcessed(timeout)
    68  	}
    69  }