-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnomination_protocol.go
154 lines (125 loc) · 3.22 KB
/
nomination_protocol.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package scp
type Validator interface {
ValidateValue(Value) bool
}
type Combiner interface {
CombineValues(...Value) Value
}
type nominationProtocol struct {
slotIndex uint64
id string
validator Validator
combiner Combiner
quorumSlices
nominates
proposals suspendableValueCh
candidates chan Value
inputMessages chan *Message
outputMessages chan *Message
ballotProtocol chan protocolMessage
lastCandidate Value
}
func (n *nominationProtocol) run() {
for {
select {
case p := <-n.proposals.out:
n.considerProposal(p)
case m := <-n.ballotProtocol:
n.reinit(m)
case m := <-n.inputMessages:
n.receive(m)
}
}
}
func (n *nominationProtocol) considerProposal(v Value) {
if !n.validator.ValidateValue(v) {
return
}
if nominate := n.nominates.find(v); nominate == nil {
nominate = n.nominates.create(v, n.quorumSlices)
n.voteNominate(nominate)
}
}
func (n *nominationProtocol) receive(m *Message) {
if m.SlotIndex != n.slotIndex {
return
}
switch m.Type {
case VoteNominate:
n.nominateVoted(m)
case AcceptNominate:
n.nominateAccepted(m)
}
}
func (n *nominationProtocol) broadcast(m *Message) {
if m.SlotIndex != n.slotIndex {
return
}
m.NodeID = n.id
n.outputMessages <- m
}
func (n *nominationProtocol) init(slotIndex uint64, candidatesCh chan Value) {
n.slotIndex = slotIndex
n.nominates = newNominates()
n.candidates = candidatesCh
}
func (n *nominationProtocol) reinit(m protocolMessage) {
n.init(m.slotIndex, m.candidates)
n.lastCandidate = nil
n.proposals.resume()
}
func (n *nominationProtocol) nominateVoted(m *Message) {
nominate := n.nominates.find(m.Value)
if nominate == nil {
if !n.validator.ValidateValue(m.Value) {
return
}
nominate = n.nominates.create(m.Value, n.quorumSlices)
n.voteNominate(nominate)
}
nominate.votedBy(m.NodeID)
if !nominate.accepted && nominate.votes.reachedQuorumThreshold(n.quorumSlices) {
n.acceptNominate(nominate)
}
}
func (n *nominationProtocol) nominateAccepted(m *Message) {
nominate := n.nominates.findOrCreate(m.Value, n.quorumSlices)
nominate.acceptedBy(m.NodeID)
if !nominate.accepted && nominate.accepts.reachedBlockingThreshold(n.quorumSlices) {
n.acceptNominate(nominate)
}
if !nominate.confirmed && nominate.accepts.reachedQuorumThreshold(n.quorumSlices) {
n.confirmNominate(nominate)
}
}
func (n *nominationProtocol) voteNominate(nominate *nominate) {
n.broadcast(&Message{
Type: VoteNominate,
SlotIndex: n.slotIndex,
Value: nominate.value,
})
nominate.selfVote()
if !nominate.accepted && nominate.votes.reachedQuorumThreshold(n.quorumSlices) {
n.acceptNominate(nominate)
}
}
func (n *nominationProtocol) acceptNominate(nominate *nominate) {
n.broadcast(&Message{
Type: AcceptNominate,
SlotIndex: n.slotIndex,
Value: nominate.value,
})
nominate.selfAccept()
if !nominate.confirmed && nominate.accepts.reachedQuorumThreshold(n.quorumSlices) {
n.confirmNominate(nominate)
}
}
func (n *nominationProtocol) confirmNominate(nominate *nominate) {
n.proposals.suspend()
nominate.selfConfirm()
n.newCandidate(nominate.value)
}
func (n *nominationProtocol) newCandidate(c Value) {
n.lastCandidate = n.combiner.CombineValues(n.lastCandidate, c)
n.candidates <- n.lastCandidate
}