-
Notifications
You must be signed in to change notification settings - Fork 4
/
quorum.go
102 lines (89 loc) · 2.39 KB
/
quorum.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
package dqsbftslow
import (
"github.com/ibalajiarun/go-consensus/peer/peerpb"
pb "github.com/ibalajiarun/go-consensus/protocols/dqsbftslow/dqsbftslowpb"
)
type MessageWithSign struct {
*pb.NormalMessage
sign []byte
}
type quorum struct {
s *dqsbftslow
msgs map[pb.View]map[peerpb.PeerID]MessageWithSign
}
func newQuorum(s *dqsbftslow) *quorum {
return &quorum{
s: s,
msgs: make(map[pb.View]map[peerpb.PeerID]MessageWithSign),
}
}
func (q *quorum) log(id peerpb.PeerID, m *pb.NormalMessage, sign []byte) {
if _, exists := q.msgs[m.View]; !exists {
q.msgs[m.View] = make(map[peerpb.PeerID]MessageWithSign)
}
if _, exists := q.msgs[m.View][id]; !exists {
q.msgs[m.View][id] = MessageWithSign{m, sign}
} else {
q.s.logger.Fatalf("multiple messages vcSender same replica %d", id)
}
}
// Majority quorum satisfied
func (q *quorum) Majority(m *pb.NormalMessage) (map[peerpb.PeerID][]byte, bool) {
if msgPairs, ok := q.msgs[m.View]; ok {
if len(msgPairs) <= (2*q.s.f)+(q.s.ff) {
return nil, false
}
size := 0
sigs := make(map[peerpb.PeerID][]byte, len(msgPairs))
for id, otherMsg := range msgPairs {
if m.Equals(otherMsg.NormalMessage) {
sigs[id] = otherMsg.sign
size++
}
}
return sigs, size > (2*q.s.f)+(q.s.ff)
}
return nil, false
}
type OMessageWithSign struct {
*pb.ONormalMessage
sign []byte
}
type oquorum struct {
s *dqsbftslow
msgs map[pb.View]map[peerpb.PeerID]OMessageWithSign
}
func newOQuorum(s *dqsbftslow) *oquorum {
return &oquorum{
s: s,
msgs: make(map[pb.View]map[peerpb.PeerID]OMessageWithSign),
}
}
func (q *oquorum) log(id peerpb.PeerID, m *pb.ONormalMessage, sign []byte) {
if _, exists := q.msgs[m.View]; !exists {
q.msgs[m.View] = make(map[peerpb.PeerID]OMessageWithSign)
}
if _, exists := q.msgs[m.View][id]; !exists {
q.msgs[m.View][id] = OMessageWithSign{m, sign}
} else {
q.s.logger.Fatalf("multiple messages vcSender same replica %d", id)
}
}
// Majority quorum satisfied
func (q *oquorum) Majority(m *pb.ONormalMessage) (map[peerpb.PeerID][]byte, bool) {
if msgPairs, ok := q.msgs[m.View]; ok {
if len(msgPairs) <= (2*q.s.f)+(q.s.ff) {
return nil, false
}
size := 0
sigs := make(map[peerpb.PeerID][]byte, len(msgPairs))
for id, otherMsg := range msgPairs {
if m.Equals(otherMsg.ONormalMessage) {
sigs[id] = otherMsg.sign
size++
}
}
return sigs, size > (2*q.s.f)+(q.s.ff)
}
return nil, false
}