-
Notifications
You must be signed in to change notification settings - Fork 4
/
messaging.go
67 lines (56 loc) · 1.63 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package hybsterx
import (
"github.com/gogo/protobuf/proto"
"github.com/ibalajiarun/go-consensus/peer/peerpb"
pb "github.com/ibalajiarun/go-consensus/protocols/hybsterx/hybsterxpb"
)
func (p *hybsterx) wrapAndMarshal(m proto.Message) []byte {
hMsg := pb.WrapHybsterxMessage(m)
mBytes, err := proto.Marshal(hMsg)
if err != nil {
p.logger.Panic("unable to marshal")
}
return mBytes
}
func (p *hybsterx) sendTo(to peerpb.PeerID, mm, cert []byte) {
cm := peerpb.Message{
Content: mm,
Certificate: cert,
To: to,
From: p.id,
}
p.msgs = append(p.msgs, cm)
}
func (p *hybsterx) broadcastNormal(m *pb.NormalMessage) {
mBytes := p.wrapAndMarshal(m)
counter := uint64(uint64(m.View<<48)|uint64(m.InstanceID.Index)) + 1
ctrIdx := int(m.InstanceID.PeerID)
p.signDispatcher.Exec(func() []byte {
return p.certifier.CreateIndependentCounterCertificateVector(mBytes, counter, ctrIdx)
}, func(cert []byte) {
for _, node := range p.nodes {
if node != p.id {
idx := node * 32
p.sendTo(node, mBytes, cert[idx:idx+32])
}
}
}, int(m.InstanceID.PeerID)%p.signWorkerCount)
}
func (p *hybsterx) broadcastONormal(m *pb.ONormalMessage) {
mBytes := p.wrapAndMarshal(m)
counter := uint64(uint64(m.View<<48)|uint64(m.Index)) + 1
ctrIdx := len(p.nodes)
p.signDispatcher.Exec(func() []byte {
return p.certifier.CreateIndependentCounterCertificateVector(mBytes, counter, ctrIdx)
}, func(cert []byte) {
for _, node := range p.nodes {
if node != p.id {
idx := node * 32
p.sendTo(node, mBytes, cert[idx:idx+32])
}
}
}, len(p.nodes)%p.signWorkerCount)
}
func (p *hybsterx) ClearMsgs() {
p.msgs = nil
}