-
Notifications
You must be signed in to change notification settings - Fork 178
/
vote_collector_distributor.go
45 lines (37 loc) · 1.22 KB
/
vote_collector_distributor.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
package pubsub
import (
"sync"
"github.com/onflow/flow-go/consensus/hotstuff"
"github.com/onflow/flow-go/consensus/hotstuff/model"
"github.com/onflow/flow-go/model/flow"
)
// VoteCollectorDistributor ingests notifications about vote aggregation and
// distributes them to consumers. Such notifications are produced by the vote aggregation logic.
// Concurrently safe.
type VoteCollectorDistributor struct {
consumers []hotstuff.VoteCollectorConsumer
lock sync.RWMutex
}
var _ hotstuff.VoteCollectorConsumer = (*VoteCollectorDistributor)(nil)
func NewQCCreatedDistributor() *VoteCollectorDistributor {
return &VoteCollectorDistributor{}
}
func (d *VoteCollectorDistributor) AddVoteCollectorConsumer(consumer hotstuff.VoteCollectorConsumer) {
d.lock.Lock()
defer d.lock.Unlock()
d.consumers = append(d.consumers, consumer)
}
func (d *VoteCollectorDistributor) OnQcConstructedFromVotes(qc *flow.QuorumCertificate) {
d.lock.RLock()
defer d.lock.RUnlock()
for _, consumer := range d.consumers {
consumer.OnQcConstructedFromVotes(qc)
}
}
func (d *VoteCollectorDistributor) OnVoteProcessed(vote *model.Vote) {
d.lock.RLock()
defer d.lock.RUnlock()
for _, subscriber := range d.consumers {
subscriber.OnVoteProcessed(vote)
}
}