-
Notifications
You must be signed in to change notification settings - Fork 178
/
broker.go
376 lines (328 loc) · 13.2 KB
/
broker.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package dkg
import (
"bytes"
"context"
"fmt"
"sync"
"time"
"github.com/onflow/flow-go/engine"
"github.com/sethvargo/go-retry"
"github.com/rs/zerolog"
"github.com/onflow/flow-go/crypto"
"github.com/onflow/flow-go/model/fingerprint"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/model/messages"
"github.com/onflow/flow-go/module"
)
// retryMaxPublish is the maximum number of times the broker will attempt to broadcast
// a message or publish a result
const retryMaxPublish = 8
// retryMaxRead is the max number of times the broker will attempt to read messages
const retryMaxRead = 3
// retryMilliseconds is the number of milliseconds to wait between retries
const retryMilliseconds = 1000 * time.Millisecond
// Broker is an implementation of the DKGBroker interface which is intended to
// be used in conjuction with the DKG MessagingEngine for private messages, and
// with the DKG smart-contract for broadcast messages.
type Broker struct {
sync.Mutex
log zerolog.Logger
unit *engine.Unit
dkgInstanceID string // unique identifier of the current dkg run (prevent replay attacks)
committee flow.IdentityList // IDs of DKG members
me module.Local // used for signing bcast messages
myIndex int // index of this instance in the committee
dkgContractClients []module.DKGContractClient // array of clients to communicate with the DKG smart contract in priority order for fallbacks during retries
activeDKGContractClient int // index of the dkg contract client that is currently in use
tunnel *BrokerTunnel // channels through which the broker communicates with the network engine
privateMsgCh chan messages.DKGMessage // channel to forward incoming private messages to consumers
broadcastMsgCh chan messages.DKGMessage // channel to forward incoming broadcast messages to consumers
messageOffset uint // offset for next broadcast messages to fetch
shutdownCh chan struct{} // channel to stop the broker from listening
broadcasts uint // broadcasts counts the number of successful broadcasts
}
// NewBroker instantiates a new epoch-specific broker capable of communicating
// with other nodes via a network engine and dkg smart-contract.
func NewBroker(
log zerolog.Logger,
dkgInstanceID string,
committee flow.IdentityList,
me module.Local,
myIndex int,
dkgContractClients []module.DKGContractClient,
tunnel *BrokerTunnel) *Broker {
b := &Broker{
log: log.With().Str("component", "broker").Str("dkg_instance_id", dkgInstanceID).Logger(),
unit: engine.NewUnit(),
dkgInstanceID: dkgInstanceID,
committee: committee,
me: me,
myIndex: myIndex,
dkgContractClients: dkgContractClients,
tunnel: tunnel,
privateMsgCh: make(chan messages.DKGMessage),
broadcastMsgCh: make(chan messages.DKGMessage),
shutdownCh: make(chan struct{}),
}
go b.listen()
return b
}
// dkgContractClient returns active dkg contract client
func (b *Broker) dkgContractClient() module.DKGContractClient {
return b.dkgContractClients[b.activeDKGContractClient]
}
func (b *Broker) updateActiveDKGContractClient() {
// if we have reached the end of our array start from beginning
if b.activeDKGContractClient == len(b.dkgContractClients)-1 {
b.activeDKGContractClient = 0
return
}
b.activeDKGContractClient++
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Implement DKGBroker
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
// GetIndex returns the index of this node in the committee list.
func (b *Broker) GetIndex() int {
return b.myIndex
}
// PrivateSend sends a DKGMessage to a destination over a private channel. It
// appends the current DKG instance ID to the message.
func (b *Broker) PrivateSend(dest int, data []byte) {
if dest >= len(b.committee) || dest < 0 {
b.log.Error().Msgf("destination id out of range: %d", dest)
return
}
dkgMessageOut := messages.PrivDKGMessageOut{
DKGMessage: messages.NewDKGMessage(b.myIndex, data, b.dkgInstanceID),
DestID: b.committee[dest].NodeID,
}
b.tunnel.SendOut(dkgMessageOut)
}
// Broadcast signs and broadcasts a message to all participants.
func (b *Broker) Broadcast(data []byte) {
b.unit.Launch(func() {
// NOTE: We're counting the number of times the underlying DKG
// requested a broadcast so we can detect an unhappy path. Thus incrementing
// broadcasts before we perform the broadcasts is okay.
b.unit.Lock()
if b.broadcasts > 0 {
// The Warn log is used by the integration tests to check if this method
// is called more than once within one epoch.
b.log.Warn().Msgf("DKG broadcast number %d with header %d", b.broadcasts+1, data[0])
} else {
b.log.Info().Msgf("DKG message broadcast with header %d", data[0])
}
b.broadcasts++
b.unit.Unlock()
bcastMsg, err := b.prepareBroadcastMessage(data)
if err != nil {
b.log.Fatal().Err(err).Msg("failed to create broadcast message")
}
expRetry, err := retry.NewExponential(retryMilliseconds)
if err != nil {
b.log.Fatal().Err(err).Msg("create retry mechanism")
}
maxedExpRetry := retry.WithMaxRetries(retryMaxPublish, expRetry)
attempts := 1
err = retry.Do(context.Background(), maxedExpRetry, func(ctx context.Context) error {
err := b.dkgContractClient().Broadcast(bcastMsg)
if err != nil {
b.log.Error().Err(err).Msgf("error broadcasting, retrying (%d)", attempts)
// retry with next fallback client after 2 failed attempts
if attempts%2 == 0 {
b.updateActiveDKGContractClient()
b.log.Warn().Msgf("broadcast: retrying on attempt (%d) with fallback access node at index (%d)", attempts, b.activeDKGContractClient)
}
}
attempts++
return retry.RetryableError(err)
})
// Various network can conditions can result in errors while broadcasting DKG messages,
// because failure to send an individual DKG message doesn't necessarily result in local or global DKG failure
// it is acceptable to log the error and move on.
if err != nil {
b.log.Error().Err(err).Msg("failed to broadcast message")
}
})
}
// Disqualify flags that a node is misbehaving and got disqualified
func (b *Broker) Disqualify(node int, log string) {
// The Warn log is used by the integration tests to check if this method is
// called.
b.log.Warn().Msgf("participant %d is disqualifying participant %d because: %s", b.myIndex, node, log)
}
// FlagMisbehavior warns that a node is misbehaving.
func (b *Broker) FlagMisbehavior(node int, log string) {
// The Warn log is used by the integration tests to check if this method is
// called.
b.log.Warn().Msgf("participant %d is flagging participant %d because: %s", b.myIndex, node, log)
}
// GetPrivateMsgCh returns the channel through which consumers can receive
// incoming private DKG messages.
func (b *Broker) GetPrivateMsgCh() <-chan messages.DKGMessage {
return b.privateMsgCh
}
// GetBroadcastMsgCh returns the channel through which consumers can receive
// incoming broadcast DKG messages.
func (b *Broker) GetBroadcastMsgCh() <-chan messages.DKGMessage {
return b.broadcastMsgCh
}
// Poll calls the DKG smart contract to get missing DKG messages for the current
// epoch, and forwards them to the msgCh. It should be called with the ID of a
// block whose seal is finalized. The function doesn't return until the received
// messages are processed by the consumer because b.msgCh is not buffered.
func (b *Broker) Poll(referenceBlock flow.Identifier) error {
b.Lock()
defer b.Unlock()
expRetry, err := retry.NewExponential(retryMilliseconds)
if err != nil {
b.log.Fatal().Err(err).Msg("failed to create retry mechanism")
}
maxedExpRetry := retry.WithMaxRetries(retryMaxRead, expRetry)
var msgs []messages.BroadcastDKGMessage
attempts := 0
err = retry.Do(b.unit.Ctx(), maxedExpRetry, func(ctx context.Context) error {
attempts++
// retry with next fallback client after 2 failed attempts
if attempts%2 == 0 {
b.updateActiveDKGContractClient()
b.log.Warn().Msgf("poll: retrying on attempt (%d) with fallback access node at index (%d)", attempts, b.activeDKGContractClient)
}
msgs, err = b.dkgContractClient().ReadBroadcast(b.messageOffset, referenceBlock)
if err != nil {
err = fmt.Errorf("could not read broadcast messages(offset: %d, ref: %v): %w", b.messageOffset, referenceBlock, err)
return retry.RetryableError(err)
}
return nil
})
// Various network conditions can result in errors while reading DKG messages
// We will read any messages during the next poll because messageOffset is not increased
if err != nil {
b.log.Error().Err(err).Msg("failed to read messages")
return nil
}
for _, msg := range msgs {
ok, err := b.verifyBroadcastMessage(msg)
if err != nil {
b.log.Error().Err(err).Msg("bad broadcast message")
continue
}
if !ok {
b.log.Debug().Msg("invalid signature on broadcast dkg message")
continue
}
b.log.Debug().Msgf("forwarding broadcast message to controller")
b.broadcastMsgCh <- msg.DKGMessage
}
b.messageOffset += uint(len(msgs))
return nil
}
// SubmitResult publishes the result of the DKG protocol to the smart contract.
func (b *Broker) SubmitResult(pubKey crypto.PublicKey, groupKeys []crypto.PublicKey) error {
expRetry, err := retry.NewExponential(retryMilliseconds)
if err != nil {
b.log.Fatal().Err(err).Msg("failed to create retry mechanism")
}
maxedExpRetry := retry.WithMaxRetries(retryMaxPublish, expRetry)
attempts := 0
err = retry.Do(context.Background(), maxedExpRetry, func(ctx context.Context) error {
attempts++
// retry with next fallback client after 2 failed attempts
if attempts%2 == 0 {
b.updateActiveDKGContractClient()
b.log.Warn().Msgf("submit result: retrying on attempt (%d) with fallback access node at index (%d)", attempts, b.activeDKGContractClient)
}
err := b.dkgContractClient().SubmitResult(pubKey, groupKeys)
if err != nil {
b.log.Error().Err(err).Msg("error submitting DKG result, retrying")
}
return retry.RetryableError(err)
})
if err != nil {
b.log.Fatal().Err(err).Msg("failed to submit dkg result")
}
b.log.Debug().Msg("dkg result submitted")
return nil
}
// Shutdown stop the goroutine that listens to incoming private messages.
func (b *Broker) Shutdown() {
close(b.shutdownCh)
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
// listen is a blocking call that processes incoming messages from the network
// engine.
func (b *Broker) listen() {
for {
select {
case msg := <-b.tunnel.MsgChIn:
b.onPrivateMessage(msg.OriginID, msg.DKGMessage)
case <-b.shutdownCh:
return
}
}
}
// onPrivateMessage verifies the integrity of an incoming message and forwards
// it to consumers via the msgCh.
func (b *Broker) onPrivateMessage(originID flow.Identifier, msg messages.DKGMessage) {
err := b.checkMessageInstanceAndOrigin(msg)
if err != nil {
b.log.Err(err).Msg("bad message")
return
}
// check that the message's origin matches the sender's flow identifier
nodeID := b.committee[msg.Orig].NodeID
if !bytes.Equal(nodeID[:], originID[:]) {
b.log.Error().Msgf("bad message: OriginID (%v) does not match committee member %d (%v)", originID, msg.Orig, nodeID)
return
}
b.privateMsgCh <- msg
}
// checkMessageInstanceAndOrigin returns an error if the message's dkgInstanceID
// does not correspond to the current instance, or if the message's origin index
// is out of range with respect to the committee list.
func (b *Broker) checkMessageInstanceAndOrigin(msg messages.DKGMessage) error {
// check that the message corresponds to the current epoch
if b.dkgInstanceID != msg.DKGInstanceID {
return fmt.Errorf("wrong DKG instance. Got %s, want %s", msg.DKGInstanceID, b.dkgInstanceID)
}
// check that the message's origin is not out of range
if msg.Orig >= uint64(len(b.committee)) {
return fmt.Errorf("origin id out of range: %d", msg.Orig)
}
return nil
}
// prepareBroadcastMessage creates BroadcastDKGMessage with a signature from the
// node's staking key.
func (b *Broker) prepareBroadcastMessage(data []byte) (messages.BroadcastDKGMessage, error) {
dkgMessage := messages.NewDKGMessage(
b.myIndex,
data,
b.dkgInstanceID,
)
sigData := fingerprint.Fingerprint(dkgMessage)
signature, err := b.me.Sign(sigData[:], NewDKGMessageHasher())
if err != nil {
return messages.BroadcastDKGMessage{}, err
}
bcastMsg := messages.BroadcastDKGMessage{
DKGMessage: dkgMessage,
Signature: signature,
}
return bcastMsg, nil
}
// verifyBroadcastMessage checks the DKG instance and Origin of a broadcast
// message, as well as the signature against the staking key of the sender.
func (b *Broker) verifyBroadcastMessage(bcastMsg messages.BroadcastDKGMessage) (bool, error) {
err := b.checkMessageInstanceAndOrigin(bcastMsg.DKGMessage)
if err != nil {
return false, err
}
origin := b.committee[bcastMsg.Orig]
signData := fingerprint.Fingerprint(bcastMsg.DKGMessage)
return origin.StakingPubKey.Verify(
bcastMsg.Signature,
signData[:],
NewDKGMessageHasher(),
)
}