/
broadcast.go
554 lines (492 loc) 路 15.5 KB
/
broadcast.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
package dkg
import (
"bytes"
"context"
"errors"
"fmt"
"math/rand"
"sync"
commonutils "github.com/drand/drand/v2/common"
"github.com/drand/drand/v2/common/log"
"github.com/drand/drand/v2/common/tracer"
"github.com/drand/drand/v2/crypto"
"github.com/drand/drand/v2/internal/net"
"github.com/drand/drand/v2/internal/util"
pdkg "github.com/drand/drand/v2/protobuf/dkg"
"github.com/drand/drand/v2/protobuf/drand"
"github.com/drand/kyber"
"github.com/drand/kyber/share/dkg"
)
// Broadcast is an interface that represents the minimum functionality required
// by drand to both (1) be the interface between drand and the dkg logic and (2)
// implement the broadcasting mechanism.
type Broadcast interface {
dkg.Board
BroadcastDKG(ctx context.Context, p *pdkg.DKGPacket) error
Stop()
}
// echoBroadcast implements a very simple broadcasting mechanism: for each new
// packet seen, rebroadcast it once. While this protocol is simple to implement,
// it does not guarantees anything about the timing of which nodes is going to
// accept packets, with Byzantine adversaries. However, an attacker that wants
// to split the nodes into two groups such that they accept different deals need
// to be able to reliably know the network topology and be able to send the
// deals close enough to the next phase to each node such that they won't be
// able to send it to their other nodes in time.
//
// There are other broadcast protocols that are resilient against Byzantine
// behaviors but these require a higher threshold and they still do not protect
// against these kinds of "epoch boundary" attack. For example
// https://eprint.iacr.org/2011/535.pdf suggests a protocol where each
// rebroadcast until a certain threshold happens. That protocol is secure
// against byzantine behavior if number of malicious actors is less than 1/3 of
// the total number of participants. As well, and the most problematic point
// here, it does not protect against epoch boundary attacks since a group of
// nodes can "accept" a packet right before the next phase starts and the rest
// of the node don't accept it because it's too late. Note that even though the
// DKG library allows to use fast sync the fast sync mode.
type echoBroadcast struct {
ctx context.Context
sync.Mutex
l log.Logger
version commonutils.Version
beaconID string
// responsible for sending out the messages
dispatcher *dispatcher
// list of messages already retransmitted comparison by hash
hashes set
dealCh chan dkg.DealBundle
respCh chan dkg.ResponseBundle
justCh chan dkg.JustificationBundle
scheme *crypto.Scheme
config dkg.Config
isStopped bool
}
type packet = dkg.Packet
var _ Broadcast = (*echoBroadcast)(nil)
func newEchoBroadcast(
ctx context.Context,
client net.DKGClient,
l log.Logger,
version commonutils.Version,
beaconID string,
own string,
to []*pdkg.Participant,
scheme *crypto.Scheme,
config *dkg.Config,
) (*echoBroadcast, error) {
if len(to) == 0 {
return nil, errors.New("cannot create a broadcaster with no participants")
}
// copy the config to avoid races
c := *config
return &echoBroadcast{
ctx: ctx,
l: l.Named("echoBroadcast"),
version: version,
beaconID: beaconID,
dispatcher: newDispatcher(ctx, client, l, to, own),
dealCh: make(chan dkg.DealBundle, len(to)),
respCh: make(chan dkg.ResponseBundle, len(to)),
justCh: make(chan dkg.JustificationBundle, len(to)),
hashes: new(arraySet),
scheme: scheme,
config: c,
isStopped: false,
}, nil
}
func (b *echoBroadcast) PushDeals(bundle *dkg.DealBundle) {
ctx, span := tracer.NewSpan(b.ctx, "b.PushDeals")
defer span.End()
b.dealCh <- *bundle
b.Lock()
defer b.Unlock()
h := hash(bundle.Hash())
b.l.Infow("push broadcast", "deal", fmt.Sprintf("%x", h[:5]))
b.sendout(ctx, h, bundle, true, b.beaconID)
}
func (b *echoBroadcast) PushResponses(bundle *dkg.ResponseBundle) {
ctx, span := tracer.NewSpan(b.ctx, "b.PushResponses")
defer span.End()
b.respCh <- *bundle
b.Lock()
defer b.Unlock()
h := hash(bundle.Hash())
b.l.Debugw("push", "response", bundle.String())
b.sendout(ctx, h, bundle, true, b.beaconID)
}
func (b *echoBroadcast) PushJustifications(bundle *dkg.JustificationBundle) {
ctx, span := tracer.NewSpan(b.ctx, "b.PushJustifications")
defer span.End()
b.justCh <- *bundle
b.Lock()
defer b.Unlock()
h := hash(bundle.Hash())
b.l.Debugw("push", "justification", fmt.Sprintf("%x", h[:5]))
b.sendout(ctx, h, bundle, true, b.beaconID)
}
func (b *echoBroadcast) BroadcastDKG(ctx context.Context, p *pdkg.DKGPacket) error {
ctx, span := tracer.NewSpan(ctx, "b.BroadcastDKG")
defer span.End()
b.Lock()
defer b.Unlock()
addr := net.RemoteAddress(ctx)
dkgPacket, err := protoToDKGPacket(p.GetDkg(), b.scheme)
if err != nil {
b.l.Errorw("received invalid packet DKGPacket", "from", addr, "err", err)
err := errors.New("invalid DKGPacket")
span.RecordError(err)
return err
}
hash := hash(dkgPacket.Hash())
if b.hashes.exists(hash) {
// if we've already seen this one, no need to verify even because that
// means we already broadcasted it
b.l.Debugw("ignoring duplicate packet", "index", dkgPacket.Index(), "from", addr, "type", fmt.Sprintf("%T", dkgPacket))
return nil
}
dkgConfig := b.config
if err := dkg.VerifyPacketSignature(&dkgConfig, dkgPacket); err != nil {
b.l.Errorw("received invalid signature", "from", addr, "signature", dkgPacket.Sig(), "scheme", b.scheme, "err", err)
err := errors.New("invalid DKGPacket")
span.RecordError(err)
return err
}
b.l.Debugw("received new packet to echoBroadcast", "from", addr, "packet index", dkgPacket.Index(), "type", fmt.Sprintf("%T", dkgPacket))
b.sendout(ctx, hash, dkgPacket, false, b.beaconID) // we're using the rate limiting
b.passToApplication(dkgPacket)
return nil
}
func (b *echoBroadcast) passToApplication(p packet) {
switch pp := p.(type) {
case *dkg.DealBundle:
b.dealCh <- *pp
case *dkg.ResponseBundle:
b.respCh <- *pp
case *dkg.JustificationBundle:
b.justCh <- *pp
default:
b.l.Errorw("application channel full")
}
}
// sendout converts the packet to protobuf and pass the packet to the dispatcher
// so it is broadcasted out to all nodes. sendout requires the echoBroadcast
// lock. If bypass is true, the message is directly sent to the peers, bypassing
// the rate limiting in place.
func (b *echoBroadcast) sendout(ctx context.Context, h []byte, p packet, bypass bool, beaconID string) {
ctx, span := tracer.NewSpan(ctx, "b.sendout")
defer span.End()
if b.isStopped {
return
}
dkgproto, err := dkgPacketToProto(p, beaconID)
if err != nil {
b.l.Errorw("can't send packet", "err", err)
return
}
// we register we saw that packet and we broadcast it
b.hashes.put(h)
proto := &pdkg.DKGPacket{Dkg: dkgproto}
if bypass {
// in a routine cause we don't want to block the processing of the DKG
// as well - that's ok since we are only expecting to send 3 packets out
// at most.
go b.dispatcher.broadcastDirect(ctx, proto)
} else {
b.dispatcher.broadcast(ctx, proto)
}
}
func (b *echoBroadcast) IncomingDeal() <-chan dkg.DealBundle {
return b.dealCh
}
func (b *echoBroadcast) IncomingResponse() <-chan dkg.ResponseBundle {
return b.respCh
}
func (b *echoBroadcast) IncomingJustification() <-chan dkg.JustificationBundle {
return b.justCh
}
func (b *echoBroadcast) Stop() {
b.Lock()
b.isStopped = true
b.Unlock()
b.dispatcher.stop()
}
type hash []byte
// set is a simple interface to keep tracks of all the packet hashes that we
// have rebroadcast already
// TODO: check if having a map makes more sense.
type set interface {
put(hash)
exists(hash) bool
}
type arraySet struct {
hashes [][]byte
}
func (a *arraySet) put(hash hash) {
for _, h := range a.hashes {
if bytes.Equal(h, hash) {
return
}
}
a.hashes = append(a.hashes, hash)
}
func (a *arraySet) exists(hash hash) bool {
for _, h := range a.hashes {
if bytes.Equal(h, hash) {
return true
}
}
return false
}
type broadcastPacket = *pdkg.DKGPacket
// maxQueueSize is the maximum queue size we reserve for each destination of
// broadcast.
const maxQueueSize = 1000
// senderQueueSize returns a dynamic queue size depending on the number of nodes
// to contact.
func senderQueueSize(nodes int) int {
if nodes > maxQueueSize {
return maxQueueSize
}
// we have 3 steps
return nodes * 3 //nolint:gomnd
}
// dispatcher maintains a list of worker assigned one destination and pushes the
// message to send to the right worker
type dispatcher struct {
sync.Mutex
senders []*sender
}
func newDispatcher(ctx context.Context, dkgClient net.DKGClient, l log.Logger, to []*pdkg.Participant, us string) *dispatcher {
ctx, span := tracer.NewSpan(ctx, "newDispatcher")
defer span.End()
var senders = make([]*sender, 0, len(to)-1)
queue := senderQueueSize(len(to))
for _, node := range to {
if node.Address == us {
continue
}
sender := newSender(dkgClient, node, l, queue)
go sender.run(ctx)
senders = append(senders, sender)
}
return &dispatcher{
senders: senders,
}
}
// broadcast uses the regular channel limitation for messages coming from other
// nodes.
func (d *dispatcher) broadcast(ctx context.Context, p broadcastPacket) {
ctx, span := tracer.NewSpanFromContext(context.Background(), ctx, "d.broadcast")
defer span.End()
for _, i := range rand.Perm(len(d.senders)) {
d.senders[i].sendPacket(ctx, p)
}
}
// broadcastDirect directly send to the other peers - it is used only for our
// own packets so we're not bound to congestion events.
func (d *dispatcher) broadcastDirect(ctx context.Context, p broadcastPacket) {
ctx, span := tracer.NewSpan(ctx, "d.broadcastDirect")
defer span.End()
for _, i := range rand.Perm(len(d.senders)) {
d.senders[i].sendDirect(ctx, p)
}
}
func (d *dispatcher) stop() {
for _, sender := range d.senders {
sender.stop()
}
}
type sender struct {
l log.Logger
client net.DKGClient
to *pdkg.Participant
newCh chan broadcastPacket
}
func newSender(client net.DKGClient, to *pdkg.Participant, l log.Logger, queueSize int) *sender {
return &sender{
l: l.Named("Sender"),
client: client,
to: to,
newCh: make(chan broadcastPacket, queueSize),
}
}
func (s *sender) sendPacket(ctx context.Context, p broadcastPacket) {
_, span := tracer.NewSpan(ctx, "s.sendPacket")
defer span.End()
select {
case s.newCh <- p:
default:
s.l.Errorw("sender queue full", "endpoint", s.to.Address)
}
}
func (s *sender) run(ctx context.Context) {
ctx, span := tracer.NewSpanFromContext(context.Background(), ctx, "s.run")
defer span.End()
for newPacket := range s.newCh {
s.sendDirect(ctx, newPacket)
}
}
func (s *sender) sendDirect(ctx context.Context, newPacket broadcastPacket) {
ctx, span := tracer.NewSpanFromContext(context.Background(), ctx, "s.sendDirect")
defer span.End()
node := util.ToPeer(s.to)
_, err := s.client.BroadcastDKG(ctx, node, newPacket)
if err != nil {
s.l.Errorw("error while sending out", "to", s.to.Address, "err:", err)
} else {
s.l.Debugw("sending out", "to", s.to.Address)
}
}
func (s *sender) stop() {
close(s.newCh)
}
func protoToDKGPacket(d *pdkg.Packet, sch *crypto.Scheme) (dkg.Packet, error) {
switch packet := d.GetBundle().(type) {
case *pdkg.Packet_Deal:
return protoToDeal(packet.Deal, sch)
case *pdkg.Packet_Response:
return protoToResp(packet.Response), nil
case *pdkg.Packet_Justification:
return protoToJustif(packet.Justification, sch)
default:
return nil, errors.New("unknown packet")
}
}
func dkgPacketToProto(p dkg.Packet, beaconID string) (*pdkg.Packet, error) {
switch inner := p.(type) {
case *dkg.DealBundle:
return dealToProto(inner, beaconID), nil
case *dkg.ResponseBundle:
return respToProto(inner, beaconID), nil
case *dkg.JustificationBundle:
return justifToProto(inner, beaconID), nil
default:
return nil, errors.New("invalid dkg packet")
}
}
func protoToDeal(d *pdkg.DealBundle, sch *crypto.Scheme) (*dkg.DealBundle, error) {
bundle := new(dkg.DealBundle)
bundle.DealerIndex = d.DealerIndex
publics := make([]kyber.Point, 0, len(d.Commits))
for _, c := range d.Commits {
coeff := sch.KeyGroup.Point()
if err := coeff.UnmarshalBinary(c); err != nil {
return nil, fmt.Errorf("invalid public coeff:%w", err)
}
publics = append(publics, coeff)
}
bundle.Public = publics
deals := make([]dkg.Deal, 0, len(d.Deals))
for _, dd := range d.Deals {
deal := dkg.Deal{
EncryptedShare: dd.EncryptedShare,
ShareIndex: dd.ShareIndex,
}
deals = append(deals, deal)
}
bundle.Deals = deals
bundle.SessionID = d.SessionId
bundle.Signature = d.Signature
return bundle, nil
}
func protoToResp(r *pdkg.ResponseBundle) *dkg.ResponseBundle {
resp := new(dkg.ResponseBundle)
resp.ShareIndex = r.ShareIndex
resp.Responses = make([]dkg.Response, 0, len(r.Responses))
for _, rr := range r.Responses {
response := dkg.Response{
DealerIndex: rr.DealerIndex,
Status: rr.Status,
}
resp.Responses = append(resp.Responses, response)
}
resp.SessionID = r.SessionId
resp.Signature = r.Signature
return resp
}
func protoToJustif(j *pdkg.JustificationBundle, sch *crypto.Scheme) (*dkg.JustificationBundle, error) {
just := new(dkg.JustificationBundle)
just.DealerIndex = j.DealerIndex
just.Justifications = make([]dkg.Justification, len(j.Justifications))
for i, j := range j.Justifications {
share := sch.KeyGroup.Scalar()
if err := share.UnmarshalBinary(j.Share); err != nil {
return nil, fmt.Errorf("invalid share: %w", err)
}
justif := dkg.Justification{
ShareIndex: j.ShareIndex,
Share: share,
}
just.Justifications[i] = justif
}
just.SessionID = j.SessionId
just.Signature = j.Signature
return just, nil
}
func dealToProto(d *dkg.DealBundle, beaconID string) *pdkg.Packet {
packet := new(pdkg.Packet)
bundle := new(pdkg.DealBundle)
bundle.DealerIndex = d.DealerIndex
bundle.Deals = make([]*pdkg.Deal, len(d.Deals))
for i, deal := range d.Deals {
pdeal := &pdkg.Deal{
ShareIndex: deal.ShareIndex,
EncryptedShare: deal.EncryptedShare,
}
bundle.Deals[i] = pdeal
}
bundle.Commits = make([][]byte, len(d.Public))
for i, coeff := range d.Public {
cbuff, _ := coeff.MarshalBinary()
bundle.Commits[i] = cbuff
}
bundle.Signature = d.Signature
bundle.SessionId = d.SessionID
packet.Bundle = &pdkg.Packet_Deal{Deal: bundle}
packet.Metadata = &drand.Metadata{
BeaconID: beaconID,
}
return packet
}
func respToProto(r *dkg.ResponseBundle, beaconID string) *pdkg.Packet {
packet := new(pdkg.Packet)
bundle := new(pdkg.ResponseBundle)
bundle.ShareIndex = r.ShareIndex
bundle.Responses = make([]*pdkg.Response, len(r.Responses))
for i, resp := range r.Responses {
presp := &pdkg.Response{
DealerIndex: resp.DealerIndex,
Status: resp.Status,
}
bundle.Responses[i] = presp
}
bundle.SessionId = r.SessionID
bundle.Signature = r.Signature
packet.Bundle = &pdkg.Packet_Response{Response: bundle}
packet.Metadata = &drand.Metadata{
BeaconID: beaconID,
}
return packet
}
func justifToProto(j *dkg.JustificationBundle, beaconID string) *pdkg.Packet {
packet := new(pdkg.Packet)
bundle := new(pdkg.JustificationBundle)
bundle.DealerIndex = j.DealerIndex
bundle.Justifications = make([]*pdkg.Justification, len(j.Justifications))
for i, just := range j.Justifications {
shareBuff, _ := just.Share.MarshalBinary()
pjust := &pdkg.Justification{
ShareIndex: just.ShareIndex,
Share: shareBuff,
}
bundle.Justifications[i] = pjust
}
bundle.SessionId = j.SessionID
bundle.Signature = j.Signature
packet.Bundle = &pdkg.Packet_Justification{Justification: bundle}
packet.Metadata = &drand.Metadata{
BeaconID: beaconID,
}
return packet
}