-
Notifications
You must be signed in to change notification settings - Fork 4
/
quorum.go
54 lines (47 loc) · 1.23 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
package sbft
import (
"github.com/ibalajiarun/go-consensus/peer/peerpb"
pb "github.com/ibalajiarun/go-consensus/protocols/sbft/sbftpb"
)
type MessageWithSign struct {
*pb.NormalMessage
sign []byte
}
type quorum struct {
s *sbft
msgs map[pb.View]map[peerpb.PeerID]MessageWithSign
}
func newQuorum(s *sbft) *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) <= 3*q.s.f+q.s.c {
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 > 3*q.s.f+q.s.c
}
return nil, false
}