-
Notifications
You must be signed in to change notification settings - Fork 177
/
communicator_distributor.go
56 lines (47 loc) · 1.67 KB
/
communicator_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
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"
)
// CommunicatorDistributor ingests outbound consensus messages from HotStuff's core logic and
// distributes them to consumers. This logic only runs inside active consensus participants proposing
// blocks, voting, collecting + aggregating votes to QCs, and participating in the pacemaker (sending
// timeouts, collecting + aggregating timeouts to TCs).
// Concurrently safe.
type CommunicatorDistributor struct {
consumers []hotstuff.CommunicatorConsumer
lock sync.RWMutex
}
var _ hotstuff.CommunicatorConsumer = (*CommunicatorDistributor)(nil)
func NewCommunicatorDistributor() *CommunicatorDistributor {
return &CommunicatorDistributor{}
}
func (d *CommunicatorDistributor) AddCommunicatorConsumer(consumer hotstuff.CommunicatorConsumer) {
d.lock.Lock()
defer d.lock.Unlock()
d.consumers = append(d.consumers, consumer)
}
func (d *CommunicatorDistributor) OnOwnVote(blockID flow.Identifier, view uint64, sigData []byte, recipientID flow.Identifier) {
d.lock.RLock()
defer d.lock.RUnlock()
for _, s := range d.consumers {
s.OnOwnVote(blockID, view, sigData, recipientID)
}
}
func (d *CommunicatorDistributor) OnOwnTimeout(timeout *model.TimeoutObject) {
d.lock.RLock()
defer d.lock.RUnlock()
for _, s := range d.consumers {
s.OnOwnTimeout(timeout)
}
}
func (d *CommunicatorDistributor) OnOwnProposal(proposal *flow.Header, targetPublicationTime time.Time) {
d.lock.RLock()
defer d.lock.RUnlock()
for _, s := range d.consumers {
s.OnOwnProposal(proposal, targetPublicationTime)
}
}