-
Notifications
You must be signed in to change notification settings - Fork 178
/
distributor.go
164 lines (142 loc) · 4.07 KB
/
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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"
)
// Distributor distributes notifications to a list of subscribers (event consumers).
//
// It allows thread-safe subscription of multiple consumers to events.
type Distributor struct {
subscribers []hotstuff.Consumer
lock sync.RWMutex
}
func (p *Distributor) OnEventProcessed() {
p.lock.RLock()
defer p.lock.RUnlock()
for _, subscriber := range p.subscribers {
subscriber.OnEventProcessed()
}
}
func NewDistributor() *Distributor {
return &Distributor{}
}
// AddConsumer adds an a event consumer to the Distributor
func (p *Distributor) AddConsumer(consumer hotstuff.Consumer) {
p.lock.Lock()
defer p.lock.Unlock()
p.subscribers = append(p.subscribers, consumer)
}
func (p *Distributor) OnReceiveVote(currentView uint64, vote *model.Vote) {
p.lock.RLock()
defer p.lock.RUnlock()
for _, subscriber := range p.subscribers {
subscriber.OnReceiveVote(currentView, vote)
}
}
func (p *Distributor) OnReceiveProposal(currentView uint64, proposal *model.Proposal) {
p.lock.RLock()
defer p.lock.RUnlock()
for _, subscriber := range p.subscribers {
subscriber.OnReceiveProposal(currentView, proposal)
}
}
func (p *Distributor) OnEnteringView(view uint64, leader flow.Identifier) {
p.lock.RLock()
defer p.lock.RUnlock()
for _, subscriber := range p.subscribers {
subscriber.OnEnteringView(view, leader)
}
}
func (p *Distributor) OnQcTriggeredViewChange(qc *flow.QuorumCertificate, newView uint64) {
p.lock.RLock()
defer p.lock.RUnlock()
for _, subscriber := range p.subscribers {
subscriber.OnQcTriggeredViewChange(qc, newView)
}
}
func (p *Distributor) OnProposingBlock(proposal *model.Proposal) {
p.lock.RLock()
defer p.lock.RUnlock()
for _, subscriber := range p.subscribers {
subscriber.OnProposingBlock(proposal)
}
}
func (p *Distributor) OnVoting(vote *model.Vote) {
p.lock.RLock()
defer p.lock.RUnlock()
for _, subscriber := range p.subscribers {
subscriber.OnVoting(vote)
}
}
func (p *Distributor) OnQcConstructedFromVotes(qc *flow.QuorumCertificate) {
p.lock.RLock()
defer p.lock.RUnlock()
for _, subscriber := range p.subscribers {
subscriber.OnQcConstructedFromVotes(qc)
}
}
func (p *Distributor) OnStartingTimeout(timerInfo *model.TimerInfo) {
p.lock.RLock()
defer p.lock.RUnlock()
for _, subscriber := range p.subscribers {
subscriber.OnStartingTimeout(timerInfo)
}
}
func (p *Distributor) OnReachedTimeout(timeout *model.TimerInfo) {
p.lock.RLock()
defer p.lock.RUnlock()
for _, subscriber := range p.subscribers {
subscriber.OnReachedTimeout(timeout)
}
}
func (p *Distributor) OnQcIncorporated(qc *flow.QuorumCertificate) {
p.lock.RLock()
defer p.lock.RUnlock()
for _, subscriber := range p.subscribers {
subscriber.OnQcIncorporated(qc)
}
}
func (p *Distributor) OnForkChoiceGenerated(curView uint64, selectedQC *flow.QuorumCertificate) {
p.lock.RLock()
defer p.lock.RUnlock()
for _, subscriber := range p.subscribers {
subscriber.OnForkChoiceGenerated(curView, selectedQC)
}
}
func (p *Distributor) OnBlockIncorporated(block *model.Block) {
p.lock.RLock()
defer p.lock.RUnlock()
for _, subscriber := range p.subscribers {
subscriber.OnBlockIncorporated(block)
}
}
func (p *Distributor) OnFinalizedBlock(block *model.Block) {
p.lock.RLock()
defer p.lock.RUnlock()
for _, subscriber := range p.subscribers {
subscriber.OnFinalizedBlock(block)
}
}
func (p *Distributor) OnDoubleProposeDetected(block1, block2 *model.Block) {
p.lock.RLock()
defer p.lock.RUnlock()
for _, subscriber := range p.subscribers {
subscriber.OnDoubleProposeDetected(block1, block2)
}
}
func (p *Distributor) OnDoubleVotingDetected(vote1, vote2 *model.Vote) {
p.lock.RLock()
defer p.lock.RUnlock()
for _, subscriber := range p.subscribers {
subscriber.OnDoubleVotingDetected(vote1, vote2)
}
}
func (p *Distributor) OnInvalidVoteDetected(vote *model.Vote) {
p.lock.RLock()
defer p.lock.RUnlock()
for _, subscriber := range p.subscribers {
subscriber.OnInvalidVoteDetected(vote)
}
}