/
messaging.go
42 lines (36 loc) · 1019 Bytes
/
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
package chainduobft
import (
"github.com/gogo/protobuf/proto"
"github.com/ibalajiarun/go-consensus/peer/peerpb"
pb "github.com/ibalajiarun/go-consensus/protocols/multichainduobft/chainduobft/chainduobftpb"
)
func (d *ChainDuoBFT) enqueueForSending(to peerpb.PeerID, mm []byte, certificate []byte) {
cm := peerpb.Message{
Content: mm,
Certificate: certificate,
To: to,
From: d.id,
}
d.msgs = append(d.msgs, cm)
}
func (s *ChainDuoBFT) marshall(m proto.Message) []byte {
hsMsg := pb.WrapChainDuoBFTMessage(m)
mBytes, err := proto.Marshal(hsMsg)
if err != nil {
panic("unable to marshall")
}
return mBytes
}
func (s *ChainDuoBFT) sendTo(to peerpb.PeerID, mBytes []byte, sign []byte) {
s.enqueueForSending(to, mBytes, sign)
}
func (d *ChainDuoBFT) broadcast(mBytes []byte, sendToSelf bool, sign []byte) {
for _, peer := range d.peers {
if sendToSelf || peer != d.id {
d.enqueueForSending(peer, mBytes, sign)
}
}
}
func (d *ChainDuoBFT) ClearMsgs() {
d.msgs = nil
}