github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/state/protocol/events/distributor.go (about) 1 package events 2 3 import ( 4 "sync" 5 6 "github.com/onflow/flow-go/model/flow" 7 "github.com/onflow/flow-go/state/protocol" 8 ) 9 10 // Distributor implements the `protocol.Consumer` interface for ingesting notifications emitted 11 // by the protocol state. It distributes the notifications to all registered consumers. 12 type Distributor struct { 13 subscribers []protocol.Consumer 14 mu sync.RWMutex 15 } 16 17 var _ protocol.Consumer = (*Distributor)(nil) 18 19 // NewDistributor returns a new events distributor. 20 func NewDistributor() *Distributor { 21 return &Distributor{} 22 } 23 24 func (d *Distributor) AddConsumer(consumer protocol.Consumer) { 25 d.mu.Lock() 26 defer d.mu.Unlock() 27 d.subscribers = append(d.subscribers, consumer) 28 } 29 30 func (d *Distributor) BlockFinalized(block *flow.Header) { 31 d.mu.RLock() 32 defer d.mu.RUnlock() 33 for _, sub := range d.subscribers { 34 sub.BlockFinalized(block) 35 } 36 } 37 38 func (d *Distributor) BlockProcessable(block *flow.Header, certifyingQC *flow.QuorumCertificate) { 39 d.mu.RLock() 40 defer d.mu.RUnlock() 41 for _, sub := range d.subscribers { 42 sub.BlockProcessable(block, certifyingQC) 43 } 44 } 45 46 func (d *Distributor) EpochTransition(newEpoch uint64, first *flow.Header) { 47 d.mu.RLock() 48 defer d.mu.RUnlock() 49 for _, sub := range d.subscribers { 50 sub.EpochTransition(newEpoch, first) 51 } 52 } 53 54 func (d *Distributor) EpochSetupPhaseStarted(epoch uint64, first *flow.Header) { 55 d.mu.RLock() 56 defer d.mu.RUnlock() 57 for _, sub := range d.subscribers { 58 sub.EpochSetupPhaseStarted(epoch, first) 59 } 60 } 61 62 func (d *Distributor) EpochCommittedPhaseStarted(epoch uint64, first *flow.Header) { 63 d.mu.RLock() 64 defer d.mu.RUnlock() 65 for _, sub := range d.subscribers { 66 sub.EpochCommittedPhaseStarted(epoch, first) 67 } 68 } 69 70 func (d *Distributor) EpochEmergencyFallbackTriggered() { 71 d.mu.RLock() 72 defer d.mu.RUnlock() 73 for _, sub := range d.subscribers { 74 sub.EpochEmergencyFallbackTriggered() 75 } 76 }