-
Notifications
You must be signed in to change notification settings - Fork 4
/
quorum.go
60 lines (51 loc) · 1.3 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
package minbft
import (
"github.com/ibalajiarun/go-consensus/peer/peerpb"
pb "github.com/ibalajiarun/go-consensus/protocols/minbft/minbftpb"
)
type Quorum struct {
p *minbft
maxView pb.View
msgs map[pb.View]map[peerpb.PeerID]*pb.NormalMessage
}
// newQuorum returns a new Quorum
func newQuorum(p *minbft) *Quorum {
return &Quorum{
p: p,
msgs: make(map[pb.View]map[peerpb.PeerID]*pb.NormalMessage),
}
}
// log adds id to quorum log records
func (q *Quorum) log(id peerpb.PeerID, m *pb.NormalMessage) {
if q.maxView > m.View {
q.p.logger.Panicf("quorum view is greater %v > %v", q.maxView, m.View)
}
q.maxView = m.View
if _, exists := q.msgs[m.View]; !exists {
q.msgs[m.View] = make(map[peerpb.PeerID]*pb.NormalMessage)
}
if _, exists := q.msgs[m.View][id]; !exists {
q.msgs[m.View][id] = m
} else {
// q.p.logger.Fatalf("multiple messages vcSender same replica %d: %v", id, m)
}
}
func (q *Quorum) Reset() {
q.msgs = make(map[pb.View]map[peerpb.PeerID]*pb.NormalMessage)
}
// Majority quorum satisfied
func (q *Quorum) Majority(m *pb.NormalMessage) bool {
if msgPairs, ok := q.msgs[m.View]; ok {
if len(msgPairs) <= q.p.f {
return false
}
size := 0
for _, otherMsg := range msgPairs {
if m.Equals(otherMsg) {
size++
}
}
return size > q.p.f
}
return false
}