github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/consensus/hotstuff/notifications/pubsub/timeout_aggregation_violation_consumer.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  )
     9  
    10  // TimeoutAggregationViolationDistributor ingests notifications about timeout aggregation violations and
    11  // distributes them to consumers. Such notifications are produced by the timeout aggregation logic.
    12  // Concurrently safe.
    13  type TimeoutAggregationViolationDistributor struct {
    14  	consumers []hotstuff.TimeoutAggregationViolationConsumer
    15  	lock      sync.RWMutex
    16  }
    17  
    18  var _ hotstuff.TimeoutAggregationViolationConsumer = (*TimeoutAggregationViolationDistributor)(nil)
    19  
    20  func NewTimeoutAggregationViolationDistributor() *TimeoutAggregationViolationDistributor {
    21  	return &TimeoutAggregationViolationDistributor{}
    22  }
    23  
    24  func (d *TimeoutAggregationViolationDistributor) AddTimeoutAggregationViolationConsumer(consumer hotstuff.TimeoutAggregationViolationConsumer) {
    25  	d.lock.Lock()
    26  	defer d.lock.Unlock()
    27  	d.consumers = append(d.consumers, consumer)
    28  }
    29  
    30  func (d *TimeoutAggregationViolationDistributor) OnDoubleTimeoutDetected(timeout *model.TimeoutObject, altTimeout *model.TimeoutObject) {
    31  	d.lock.RLock()
    32  	defer d.lock.RUnlock()
    33  	for _, subscriber := range d.consumers {
    34  		subscriber.OnDoubleTimeoutDetected(timeout, altTimeout)
    35  	}
    36  }
    37  
    38  func (d *TimeoutAggregationViolationDistributor) OnInvalidTimeoutDetected(err model.InvalidTimeoutError) {
    39  	d.lock.RLock()
    40  	defer d.lock.RUnlock()
    41  	for _, subscriber := range d.consumers {
    42  		subscriber.OnInvalidTimeoutDetected(err)
    43  	}
    44  }