/
messaging.go
51 lines (44 loc) · 1.16 KB
/
messaging.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
package prime
import (
"github.com/gogo/protobuf/proto"
"github.com/ibalajiarun/go-consensus/peer/peerpb"
pb "github.com/ibalajiarun/go-consensus/protocols/prime/primepb"
)
func (p *prime) enqueueForSending(to peerpb.PeerID, mm []byte, certificate []byte) {
cm := peerpb.Message{
Content: mm,
Certificate: certificate,
To: to,
From: p.id,
}
p.msgs = append(p.msgs, cm)
}
func (p *prime) sendTo(to peerpb.PeerID, m proto.Message) {
pbftMsg := pb.WrapPBFTMessage(m)
mBytes, err := proto.Marshal(pbftMsg)
if err != nil {
panic("unable to marshall")
}
cert := p.certifier.CreateSignedCertificate(mBytes, 0)
p.enqueueForSending(to, mBytes, cert)
}
func (p *prime) broadcast(m proto.Message, sendToSelf bool) {
pbftMsg := pb.WrapPBFTMessage(m)
mBytes, err := proto.Marshal(pbftMsg)
if err != nil {
panic("unable to marshall")
}
for _, node := range p.nodes {
node := node
if sendToSelf || node != p.id {
p.signDispatcher.Exec(func() []byte {
return p.certifier.CreateSignedCertificate(mBytes, 0)
}, func(cert []byte) {
p.enqueueForSending(node, mBytes, cert)
})
}
}
}
func (p *prime) ClearMsgs() {
p.msgs = nil
}