-
Notifications
You must be signed in to change notification settings - Fork 178
/
distributor.go
231 lines (200 loc) · 6.02 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package pubsub
import (
"sync"
"time"
"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
}
var _ hotstuff.Consumer = (*Distributor)(nil)
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 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) OnStart(currentView uint64) {
p.lock.RLock()
defer p.lock.RUnlock()
for _, subscriber := range p.subscribers {
subscriber.OnStart(currentView)
}
}
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) OnReceiveQc(currentView uint64, qc *flow.QuorumCertificate) {
p.lock.RLock()
defer p.lock.RUnlock()
for _, subscriber := range p.subscribers {
subscriber.OnReceiveQc(currentView, qc)
}
}
func (p *Distributor) OnReceiveTc(currentView uint64, tc *flow.TimeoutCertificate) {
p.lock.RLock()
defer p.lock.RUnlock()
for _, subscriber := range p.subscribers {
subscriber.OnReceiveTc(currentView, tc)
}
}
func (p *Distributor) OnPartialTc(currentView uint64, partialTc *hotstuff.PartialTcCreated) {
p.lock.RLock()
defer p.lock.RUnlock()
for _, subscriber := range p.subscribers {
subscriber.OnPartialTc(currentView, partialTc)
}
}
func (p *Distributor) OnLocalTimeout(currentView uint64) {
p.lock.RLock()
defer p.lock.RUnlock()
for _, subscriber := range p.subscribers {
subscriber.OnLocalTimeout(currentView)
}
}
func (p *Distributor) OnViewChange(oldView, newView uint64) {
p.lock.RLock()
defer p.lock.RUnlock()
for _, subscriber := range p.subscribers {
subscriber.OnViewChange(oldView, newView)
}
}
func (p *Distributor) OnQcTriggeredViewChange(oldView uint64, newView uint64, qc *flow.QuorumCertificate) {
p.lock.RLock()
defer p.lock.RUnlock()
for _, subscriber := range p.subscribers {
subscriber.OnQcTriggeredViewChange(oldView, newView, qc)
}
}
func (p *Distributor) OnTcTriggeredViewChange(oldView uint64, newView uint64, tc *flow.TimeoutCertificate) {
p.lock.RLock()
defer p.lock.RUnlock()
for _, subscriber := range p.subscribers {
subscriber.OnTcTriggeredViewChange(oldView, newView, tc)
}
}
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) OnVoteProcessed(vote *model.Vote) {
p.lock.RLock()
defer p.lock.RUnlock()
for _, subscriber := range p.subscribers {
subscriber.OnVoteProcessed(vote)
}
}
func (p *Distributor) OnTimeoutProcessed(timeout *model.TimeoutObject) {
p.lock.RLock()
defer p.lock.RUnlock()
for _, subscriber := range p.subscribers {
subscriber.OnTimeoutProcessed(timeout)
}
}
func (p *Distributor) OnCurrentViewDetails(currentView, finalizedView uint64, currentLeader flow.Identifier) {
p.lock.RLock()
defer p.lock.RUnlock()
for _, subscriber := range p.subscribers {
subscriber.OnCurrentViewDetails(currentView, finalizedView, currentLeader)
}
}
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(err model.InvalidVoteError) {
p.lock.RLock()
defer p.lock.RUnlock()
for _, subscriber := range p.subscribers {
subscriber.OnInvalidVoteDetected(err)
}
}
func (p *Distributor) OnVoteForInvalidBlockDetected(vote *model.Vote, invalidProposal *model.Proposal) {
p.lock.RLock()
defer p.lock.RUnlock()
for _, subscriber := range p.subscribers {
subscriber.OnVoteForInvalidBlockDetected(vote, invalidProposal)
}
}
func (p *Distributor) OnDoubleTimeoutDetected(timeout *model.TimeoutObject, altTimeout *model.TimeoutObject) {
p.lock.RLock()
defer p.lock.RUnlock()
for _, subscriber := range p.subscribers {
subscriber.OnDoubleTimeoutDetected(timeout, altTimeout)
}
}
func (p *Distributor) OnInvalidTimeoutDetected(err model.InvalidTimeoutError) {
p.lock.RLock()
defer p.lock.RUnlock()
for _, subscriber := range p.subscribers {
subscriber.OnInvalidTimeoutDetected(err)
}
}
func (p *Distributor) OnOwnVote(blockID flow.Identifier, view uint64, sigData []byte, recipientID flow.Identifier) {
p.lock.RLock()
defer p.lock.RUnlock()
for _, s := range p.subscribers {
s.OnOwnVote(blockID, view, sigData, recipientID)
}
}
func (p *Distributor) OnOwnTimeout(timeout *model.TimeoutObject) {
p.lock.RLock()
defer p.lock.RUnlock()
for _, s := range p.subscribers {
s.OnOwnTimeout(timeout)
}
}
func (p *Distributor) OnOwnProposal(proposal *flow.Header, targetPublicationTime time.Time) {
p.lock.RLock()
defer p.lock.RUnlock()
for _, s := range p.subscribers {
s.OnOwnProposal(proposal, targetPublicationTime)
}
}