-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathreplica.go
268 lines (237 loc) · 7.69 KB
/
replica.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package replica
import (
"context"
"github.com/renproject/hyperdrive/mq"
"github.com/renproject/hyperdrive/process"
"github.com/renproject/hyperdrive/scheduler"
"github.com/renproject/hyperdrive/timer"
"github.com/renproject/id"
)
// DidHandleMessage is called by the Replica after it has finished handling an
// input message (i.e. Propose, Prevote, or Precommit), or timeout. The message
// could have been either accepted and inserted into the processing queue, or
// filtered out and dropped. The callback is also called when the context
// within which the Replica runs gets cancelled.
type DidHandleMessage func()
// A Replica represents a process in a replicated state machine that
// participates in the Hyperdrive Consensus Algorithm. It encapsulates a
// Hyperdrive Process and exposes an interface for the Hyperdrive user to
// insert messages (propose, prevote, precommit, timeouts). A Replica then
// handles these messages asynchronously, after sorting them in an increasing
// order of height and round. A Replica is instantiated by passing in the set
// of signatories participating in the consensus mechanism, and it filters out
// messages that have not been sent by one of the known set of allowed
// signatories.
type Replica struct {
opts Options
proc process.Process
procsAllowed map[id.Signatory]bool
mch chan interface{}
mq mq.MessageQueue
didHandleMessage DidHandleMessage
}
// New instantiates and returns a pointer to a new Hyperdrive replica machine
func New(
opts Options,
whoami id.Signatory,
signatories []id.Signatory,
linearTimer process.Timer,
propose process.Proposer,
validate process.Validator,
commit process.Committer,
catch process.Catcher,
broadcast process.Broadcaster,
didHandleMessage DidHandleMessage,
) *Replica {
f := len(signatories) / 3
scheduler := scheduler.NewRoundRobin(signatories)
proc := process.NewWithCurrentHeight(
whoami,
opts.StartingHeight,
f,
linearTimer,
scheduler,
propose,
validate,
broadcast,
commit,
catch,
)
procsAllowed := make(map[id.Signatory]bool)
for _, signatory := range signatories {
procsAllowed[signatory] = true
}
return &Replica{
opts: opts,
proc: proc,
procsAllowed: procsAllowed,
mch: make(chan interface{}, opts.MessageQueueOpts.MaxCapacity),
mq: mq.New(opts.MessageQueueOpts),
didHandleMessage: didHandleMessage,
}
}
// Run starts the Hyperdrive replica's process
func (replica *Replica) Run(ctx context.Context) {
replica.proc.Start()
isRunning := true
for isRunning {
func() {
defer func() {
if replica.didHandleMessage != nil {
replica.didHandleMessage()
}
}()
select {
case <-ctx.Done():
isRunning = false
return
case m := <-replica.mch:
switch m := m.(type) {
case timer.Timeout:
switch m.MessageType {
case process.MessageTypePropose:
replica.proc.OnTimeoutPropose(m.Height, m.Round)
case process.MessageTypePrevote:
replica.proc.OnTimeoutPrevote(m.Height, m.Round)
case process.MessageTypePrecommit:
replica.proc.OnTimeoutPrecommit(m.Height, m.Round)
default:
return
}
case process.Propose:
if !replica.filterHeight(m.Height) {
return
}
replica.mq.InsertPropose(m)
case process.Prevote:
if !replica.filterHeight(m.Height) {
return
}
replica.mq.InsertPrevote(m)
case process.Precommit:
if !replica.filterHeight(m.Height) {
return
}
replica.mq.InsertPrecommit(m)
case process.Height:
replica.proc.State = process.DefaultState().WithCurrentHeight(m)
replica.mq.DropMessagesBelowHeight(m)
case []id.Signatory:
procAllowed := map[id.Signatory]bool{}
for _, sig := range m {
procAllowed[sig] = true
}
replica.procsAllowed = procAllowed
scheduler := scheduler.NewRoundRobin(m)
f := len(procAllowed) / 3
replica.proc.ResetF(uint64(f), scheduler)
}
}
replica.flush()
}()
}
}
// Propose adds a propose message to the replica. This message will be
// asynchronously inserted into the replica's message queue asynchronously,
// and consumed when the replica does not have any immediate task to do
func (replica *Replica) Propose(ctx context.Context, propose process.Propose) {
select {
case <-ctx.Done():
case replica.mch <- propose:
}
}
// Prevote adds a prevote message to the replica. This message will be
// asynchronously inserted into the replica's message queue asynchronously,
// and consumed when the replica does not have any immediate task to do
func (replica *Replica) Prevote(ctx context.Context, prevote process.Prevote) {
select {
case <-ctx.Done():
case replica.mch <- prevote:
}
}
// Precommit adds a precommit message to the replica. This message will be
// asynchronously inserted into the replica's message queue asynchronously,
// and consumed when the replica does not have any immediate task to do
func (replica *Replica) Precommit(ctx context.Context, precommit process.Precommit) {
select {
case <-ctx.Done():
case replica.mch <- precommit:
}
}
// TimeoutPropose adds a propose timeout message to the replica. This message
// will be filtered based on the replica's consensus height, and inserted
// asynchronously into the replica's message queue. It will be consumed when
// the replica does not have any immediate task to do
func (replica *Replica) TimeoutPropose(ctx context.Context, timeout timer.Timeout) {
select {
case <-ctx.Done():
case replica.mch <- timeout:
}
}
// TimeoutPrevote adds a prevote timeout message to the replica. This message
// will be filtered based on the replica's consensus height, and inserted
// asynchronously into the replica's message queue. It will be consumed when
// the replica does not have any immediate task to do
func (replica *Replica) TimeoutPrevote(ctx context.Context, timeout timer.Timeout) {
select {
case <-ctx.Done():
case replica.mch <- timeout:
}
}
// TimeoutPrecommit adds a precommit timeout message to the replica. This message
// will be filtered based on the replica's consensus height, and inserted
// asynchronously into the replica's message queue. It will be consumed when
// the replica does not have any immediate task to do
func (replica *Replica) TimeoutPrecommit(ctx context.Context, timeout timer.Timeout) {
select {
case <-ctx.Done():
case replica.mch <- timeout:
}
}
// ResetHeight of the underlying process to a future height. This should only
// be used when resynchronising the chain. If the given height is less than or
// equal to the current height, nothing happens.
//
// NOTE: All messages that are currently in the message queue for heights less
// than the given height will be dropped.
func (replica *Replica) ResetHeight(ctx context.Context, newHeight process.Height, signatories []id.Signatory) {
if newHeight <= replica.proc.State.CurrentHeight {
return
}
select {
case <-ctx.Done():
case replica.mch <- newHeight:
}
if len(signatories) == 0 {
return
}
select {
case <-ctx.Done():
case replica.mch <- signatories:
}
}
// State returns the current height, round and step of the underlying process.
func (replica Replica) State() (process.Height, process.Round, process.Step) {
return replica.proc.CurrentHeight, replica.proc.CurrentRound, replica.proc.CurrentStep
}
// CurrentHeight returns the current height of the underlying process.
func (replica Replica) CurrentHeight() process.Height {
return replica.proc.CurrentHeight
}
func (replica *Replica) filterHeight(height process.Height) bool {
return height >= replica.proc.CurrentHeight
}
func (replica *Replica) flush() {
for {
n := replica.mq.Consume(
replica.proc.CurrentHeight,
replica.proc.Propose,
replica.proc.Prevote,
replica.proc.Precommit,
replica.procsAllowed,
)
if n == 0 {
return
}
}
}