-
Notifications
You must be signed in to change notification settings - Fork 179
/
controller.go
327 lines (276 loc) · 7.46 KB
/
controller.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package dkg
import (
"fmt"
"sync"
"github.com/rs/zerolog"
"github.com/onflow/flow-go/crypto"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module"
)
// Controller implements the DKGController interface. It controls the execution
// of a Joint Feldman DKG instance. A new Controller must be instantiated for
// every epoch.
type Controller struct {
// The embedded state Manager is used to manage the controller's underlying
// state.
Manager
log zerolog.Logger
// DKGState is the object that actually executes the protocol steps.
dkg crypto.DKGState
// dkgLock protects access to dkg
dkgLock sync.Mutex
// seed is required by DKGState
seed []byte
// broker enables the controller to communicate with other nodes
broker module.DKGBroker
// Channels used internally to trigger state transitions
h1Ch chan struct{}
h2Ch chan struct{}
endCh chan struct{}
shutdownCh chan struct{}
// private fields that hold the DKG artifacts when the protocol runs to
// completion
privateShare crypto.PrivateKey
publicKeys []crypto.PublicKey
groupPublicKey crypto.PublicKey
// artifactsLock protects access to artifacts
artifactsLock sync.Mutex
}
// NewController instantiates a new Joint Feldman DKG controller.
func NewController(
log zerolog.Logger,
dkgInstanceID string,
dkg crypto.DKGState,
seed []byte,
broker module.DKGBroker,
) *Controller {
logger := log.With().
Str("component", "dkg_controller").
Str("dkg_instance_id", dkgInstanceID).
Logger()
return &Controller{
log: logger,
dkg: dkg,
seed: seed,
broker: broker,
h1Ch: make(chan struct{}),
h2Ch: make(chan struct{}),
endCh: make(chan struct{}),
shutdownCh: make(chan struct{}),
}
}
/*******************************************************************************
Implement DKGController
*******************************************************************************/
// Run starts the DKG controller and executes the DKG state-machine. It blocks
// until the controller is shutdown or until an error is encountered in one of
// the protocol phases.
func (c *Controller) Run() error {
// Start DKG and transition to phase 1
err := c.start()
if err != nil {
return err
}
// Start a background routine to listen for incoming private and broadcast
// messages from other nodes
go c.doBackgroundWork()
// Execute DKG State Machine
for {
state := c.GetState()
c.log.Debug().Msgf("DKG: %s", c.state)
switch state {
case Phase1:
err := c.phase1()
if err != nil {
return err
}
case Phase2:
err := c.phase2()
if err != nil {
return err
}
case Phase3:
err := c.phase3()
if err != nil {
return err
}
case End:
c.Shutdown()
case Shutdown:
return nil
}
}
}
// EndPhase1 notifies the controller to end phase 1, and start phase 2
func (c *Controller) EndPhase1() error {
state := c.GetState()
if state != Phase1 {
return NewInvalidStateTransitionError(state, Phase2)
}
c.SetState(Phase2)
close(c.h1Ch)
return nil
}
// EndPhase2 notifies the controller to end phase 2, and start phase 3
func (c *Controller) EndPhase2() error {
state := c.GetState()
if state != Phase2 {
return NewInvalidStateTransitionError(state, Phase3)
}
c.SetState(Phase3)
close(c.h2Ch)
return nil
}
// End terminates the DKG state machine and records the artifacts.
func (c *Controller) End() error {
state := c.GetState()
if state != Phase3 {
return NewInvalidStateTransitionError(state, End)
}
c.log.Debug().Msg("DKG engine end")
// end and retrieve products of the DKG protocol
c.dkgLock.Lock()
privateShare, groupPublicKey, publicKeys, err := c.dkg.End()
c.dkgLock.Unlock()
if err != nil {
return err
}
c.artifactsLock.Lock()
c.privateShare = privateShare
c.groupPublicKey = groupPublicKey
c.publicKeys = publicKeys
c.artifactsLock.Unlock()
c.SetState(End)
close(c.endCh)
return nil
}
// Shutdown stops the controller regardless of the current state.
func (c *Controller) Shutdown() {
c.broker.Shutdown()
c.SetState(Shutdown)
close(c.shutdownCh)
}
// Poll instructs the broker to read new broadcast messages, which will be
// relayed through the message channel. The function does not return until the
// received messages are processed.
func (c *Controller) Poll(blockReference flow.Identifier) error {
return c.broker.Poll(blockReference)
}
// GetArtifacts returns our node's private key share, the group public key,
// and the list of all nodes' public keys (including ours), as computed by
// the DKG.
func (c *Controller) GetArtifacts() (crypto.PrivateKey, crypto.PublicKey, []crypto.PublicKey) {
c.artifactsLock.Lock()
defer c.artifactsLock.Unlock()
return c.privateShare, c.groupPublicKey, c.publicKeys
}
// GetIndex returns the index of this node in the DKG committee list.
func (c *Controller) GetIndex() int {
return c.broker.GetIndex()
}
// SubmitResult instructs the broker to submit DKG results. It is up to the
// caller to ensure that this method is called after a succesfull run of the
// protocol.
func (c *Controller) SubmitResult() error {
_, pubKey, groupKeys := c.GetArtifacts()
return c.broker.SubmitResult(pubKey, groupKeys)
}
/*******************************************************************************
WORKERS
*******************************************************************************/
func (c *Controller) doBackgroundWork() {
privateMsgCh := c.broker.GetPrivateMsgCh()
broadcastMsgCh := c.broker.GetBroadcastMsgCh()
for {
select {
case msg := <-privateMsgCh:
c.dkgLock.Lock()
err := c.dkg.HandlePrivateMsg(int(msg.Orig), msg.Data)
c.dkgLock.Unlock()
if err != nil {
c.log.Err(err).Msg("error processing DKG private message")
}
case msg := <-broadcastMsgCh:
c.dkgLock.Lock()
err := c.dkg.HandleBroadcastMsg(int(msg.Orig), msg.Data)
c.dkgLock.Unlock()
if err != nil {
c.log.Err(err).Msg("error processing DKG broadcast message")
}
case <-c.shutdownCh:
return
}
}
}
func (c *Controller) start() error {
state := c.GetState()
if state != Init {
return fmt.Errorf("Cannot execute start routine in state %s", state)
}
c.dkgLock.Lock()
err := c.dkg.Start(c.seed)
c.dkgLock.Unlock()
if err != nil {
return fmt.Errorf("Error starting DKG: %w", err)
}
c.log.Debug().Msg("DKG engine started")
c.SetState(Phase1)
return nil
}
func (c *Controller) phase1() error {
state := c.GetState()
if state != Phase1 {
return fmt.Errorf("Cannot execute phase1 routine in state %s", state)
}
c.log.Debug().Msg("Waiting for end of phase 1")
for {
select {
case <-c.h1Ch:
return nil
case <-c.shutdownCh:
return nil
}
}
}
func (c *Controller) phase2() error {
state := c.GetState()
if state != Phase2 {
return fmt.Errorf("Cannot execute phase2 routine in state %s", state)
}
c.dkgLock.Lock()
err := c.dkg.NextTimeout()
c.dkgLock.Unlock()
if err != nil {
return fmt.Errorf("Error calling NextTimeout: %w", err)
}
c.log.Debug().Msg("Waiting for end of phase 2")
for {
select {
case <-c.h2Ch:
return nil
case <-c.shutdownCh:
return nil
}
}
}
func (c *Controller) phase3() error {
state := c.GetState()
if state != Phase3 {
return fmt.Errorf("Cannot execute phase3 routine in state %s", state)
}
c.dkgLock.Lock()
err := c.dkg.NextTimeout()
c.dkgLock.Unlock()
if err != nil {
return fmt.Errorf("Error calling NextTimeout: %w", err)
}
c.log.Debug().Msg("Waiting for end of phase 3")
for {
select {
case <-c.endCh:
return nil
case <-c.shutdownCh:
return nil
}
}
}