-
Notifications
You must be signed in to change notification settings - Fork 4
/
rquorum.go
57 lines (50 loc) · 1.19 KB
/
rquorum.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
package destiny
import (
"github.com/ibalajiarun/go-consensus/peer/peerpb"
pb "github.com/ibalajiarun/go-consensus/protocols/destiny/destinypb"
)
type rquorum struct {
s *destiny
msgs map[uint64]map[peerpb.PeerID]*pb.ResultMessage
exec map[uint64]struct{}
}
func newRQuorum(s *destiny) *rquorum {
return &rquorum{
s: s,
msgs: make(map[uint64]map[peerpb.PeerID]*pb.ResultMessage),
exec: make(map[uint64]struct{}),
}
}
func (q *rquorum) log(from peerpb.PeerID, m *pb.ResultMessage) {
if _, exists := q.msgs[m.Id]; !exists {
q.msgs[m.Id] = make(map[peerpb.PeerID]*pb.ResultMessage)
}
if _, exists := q.msgs[m.Id][from]; !exists {
q.msgs[m.Id][from] = m
} else {
q.s.logger.Fatalf("multiple messages vcSender same replica %d", from)
}
}
// Majority quorum satisfied
func (q *rquorum) Majority(m *pb.ResultMessage) bool {
if msgPairs, ok := q.msgs[m.Id]; ok {
if len(msgPairs) <= q.s.f {
return false
}
size := 0
for _, otherMsg := range msgPairs {
if m.Equals(otherMsg) {
size++
}
}
return size > q.s.f
}
return false
}
func (q *rquorum) GetAndSetExec(m *pb.ResultMessage) bool {
_, ok := q.exec[m.Id]
if !ok {
q.exec[m.Id] = struct{}{}
}
return ok
}