-
Notifications
You must be signed in to change notification settings - Fork 0
/
quorum.go
75 lines (58 loc) · 1.2 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
package scp
type quorumSlices []*QuorumSlice
type QuorumSlice struct {
Threshold uint32
Validators map[string]struct{}
InnerSlices []*QuorumSlice
}
func (q *QuorumSlice) blockingThreshold(a agreement) bool {
var agreed uint32
for node := range a {
if _, ok := q.Validators[node]; ok {
agreed++
}
}
for _, inner := range q.InnerSlices {
if inner.blockingThreshold(a) {
agreed++
}
}
if agreed > uint32(len(q.Validators)+len(q.InnerSlices))-q.Threshold {
return true
}
return false
}
func (q *QuorumSlice) quorumThreshold(a agreement) bool {
var agreed uint32
for node := range a {
if _, ok := q.Validators[node]; ok {
agreed++
}
}
for _, inner := range q.InnerSlices {
if inner.quorumThreshold(a) {
agreed++
}
}
if agreed >= q.Threshold {
return true
}
return false
}
func (q *QuorumSlice) blockingCounter(c uint32, counters map[string]uint32) bool {
blocking := uint32(0)
for _, counter := range counters {
if counter > c {
blocking++
}
}
for _, inner := range q.InnerSlices {
if inner.blockingCounter(c, counters) {
blocking++
}
}
if blocking > uint32(len(q.Validators)+len(q.InnerSlices))-q.Threshold {
return true
}
return false
}