-
Notifications
You must be signed in to change notification settings - Fork 177
/
vote_aggregation_violation_consumer.go
52 lines (43 loc) · 1.64 KB
/
vote_aggregation_violation_consumer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package pubsub
import (
"sync"
"github.com/onflow/flow-go/consensus/hotstuff"
"github.com/onflow/flow-go/consensus/hotstuff/model"
)
// VoteAggregationViolationDistributor ingests notifications about vote aggregation violations and
// distributes them to consumers. Such notifications are produced by the vote aggregation logic.
// Concurrently safe.
type VoteAggregationViolationDistributor struct {
consumers []hotstuff.VoteAggregationViolationConsumer
lock sync.RWMutex
}
var _ hotstuff.VoteAggregationViolationConsumer = (*VoteAggregationViolationDistributor)(nil)
func NewVoteAggregationViolationDistributor() *VoteAggregationViolationDistributor {
return &VoteAggregationViolationDistributor{}
}
func (d *VoteAggregationViolationDistributor) AddVoteAggregationViolationConsumer(consumer hotstuff.VoteAggregationViolationConsumer) {
d.lock.Lock()
defer d.lock.Unlock()
d.consumers = append(d.consumers, consumer)
}
func (d *VoteAggregationViolationDistributor) OnDoubleVotingDetected(vote1, vote2 *model.Vote) {
d.lock.RLock()
defer d.lock.RUnlock()
for _, subscriber := range d.consumers {
subscriber.OnDoubleVotingDetected(vote1, vote2)
}
}
func (d *VoteAggregationViolationDistributor) OnInvalidVoteDetected(err model.InvalidVoteError) {
d.lock.RLock()
defer d.lock.RUnlock()
for _, subscriber := range d.consumers {
subscriber.OnInvalidVoteDetected(err)
}
}
func (d *VoteAggregationViolationDistributor) OnVoteForInvalidBlockDetected(vote *model.Vote, invalidProposal *model.Proposal) {
d.lock.RLock()
defer d.lock.RUnlock()
for _, subscriber := range d.consumers {
subscriber.OnVoteForInvalidBlockDetected(vote, invalidProposal)
}
}