-
Notifications
You must be signed in to change notification settings - Fork 4
/
quorum.go
43 lines (37 loc) · 872 Bytes
/
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
package dispel
import (
"github.com/ibalajiarun/go-consensus/peer/peerpb"
pb "github.com/ibalajiarun/go-consensus/protocols/dispel/dispelpb"
)
type quorum struct {
d *Dispel
msgs map[peerpb.PeerID]*pb.RBMessage
}
// newQuorum returns a new Quorum
func newQuorum(d *Dispel) *quorum {
return &quorum{
d: d,
msgs: make(map[peerpb.PeerID]*pb.RBMessage),
}
}
// log adds id to quorum log records
func (q *quorum) log(id peerpb.PeerID, m *pb.RBMessage) {
if _, exists := q.msgs[id]; !exists {
q.msgs[id] = m
} else {
q.d.logger.Fatalf("multiple messages vcSender same replica %d", id)
}
}
// Majority quorum satisfied
func (q *quorum) Majority(m *pb.RBMessage) bool {
if len(q.msgs) <= 2*q.d.maxFailures {
return false
}
size := 0
for _, otherMsg := range q.msgs {
if m.Equals(otherMsg) {
size++
}
}
return size > 2*q.d.maxFailures
}