github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/consensus/hotstuff/notifications/pubsub/vote_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 // VoteAggregationViolationDistributor ingests notifications about vote aggregation violations and 11 // distributes them to consumers. Such notifications are produced by the vote aggregation logic. 12 // Concurrently safe. 13 type VoteAggregationViolationDistributor struct { 14 consumers []hotstuff.VoteAggregationViolationConsumer 15 lock sync.RWMutex 16 } 17 18 var _ hotstuff.VoteAggregationViolationConsumer = (*VoteAggregationViolationDistributor)(nil) 19 20 func NewVoteAggregationViolationDistributor() *VoteAggregationViolationDistributor { 21 return &VoteAggregationViolationDistributor{} 22 } 23 24 func (d *VoteAggregationViolationDistributor) AddVoteAggregationViolationConsumer(consumer hotstuff.VoteAggregationViolationConsumer) { 25 d.lock.Lock() 26 defer d.lock.Unlock() 27 d.consumers = append(d.consumers, consumer) 28 } 29 30 func (d *VoteAggregationViolationDistributor) OnDoubleVotingDetected(vote1, vote2 *model.Vote) { 31 d.lock.RLock() 32 defer d.lock.RUnlock() 33 for _, subscriber := range d.consumers { 34 subscriber.OnDoubleVotingDetected(vote1, vote2) 35 } 36 } 37 38 func (d *VoteAggregationViolationDistributor) OnInvalidVoteDetected(err model.InvalidVoteError) { 39 d.lock.RLock() 40 defer d.lock.RUnlock() 41 for _, subscriber := range d.consumers { 42 subscriber.OnInvalidVoteDetected(err) 43 } 44 } 45 46 func (d *VoteAggregationViolationDistributor) OnVoteForInvalidBlockDetected(vote *model.Vote, invalidProposal *model.Proposal) { 47 d.lock.RLock() 48 defer d.lock.RUnlock() 49 for _, subscriber := range d.consumers { 50 subscriber.OnVoteForInvalidBlockDetected(vote, invalidProposal) 51 } 52 }