-
Notifications
You must be signed in to change notification settings - Fork 47
/
broadcast.go
110 lines (95 loc) · 2.99 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
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT License was not distributed with this
// file, you can obtain one at https://opensource.org/licenses/MIT.
//
// Copyright (c) DUSK NETWORK. All rights reserved.
package writer
import (
"bytes"
"context"
"github.com/dusk-network/dusk-blockchain/pkg/config"
"github.com/dusk-network/dusk-blockchain/pkg/p2p/wire/message"
"github.com/dusk-network/dusk-blockchain/pkg/p2p/wire/protocol"
"github.com/dusk-network/dusk-blockchain/pkg/p2p/wire/topics"
"github.com/dusk-network/dusk-blockchain/pkg/util/container/ring"
"github.com/dusk-network/dusk-blockchain/pkg/util/nativeutils/eventbus"
"github.com/dusk-network/dusk-protobuf/autogen/go/rusk"
)
// Broadcast is a proxy between EventBus and Kadcast service. It subscribes for
// both topics.Kadcast and topics.KadcastPoint, compiles a valid wire frame and
// propagates the message to Kadcast service.
type Broadcast struct {
Base
}
// NewBroadcast ...
func NewBroadcast(ctx context.Context, s eventbus.Subscriber, g *protocol.Gossip, rusk rusk.NetworkClient) ring.Writer {
b := &Broadcast{
Base: Base{
subscriber: s,
gossip: g,
client: rusk,
ctx: ctx,
topic: topics.Kadcast,
},
}
b.Subscribe()
return b
}
// Subscribe subscribes to eventbus Kadcast messages.
func (w *Broadcast) Subscribe() {
mapper := func(t topics.Topic) byte {
const (
High = byte(1)
Low = byte(0)
)
switch t {
case topics.NewBlock, topics.Candidate, topics.GetCandidate,
topics.Reduction, topics.AggrAgreement, topics.Agreement:
return High
default:
return Low
}
}
// Kadcast subs
l := eventbus.NewStreamListenerWithParams(w, MaxWriterQueueSize, mapper)
w.subscriptionID = w.subscriber.Subscribe(w.topic, l)
}
// Write implements. ring.Writer.
func (w *Broadcast) Write(data []byte, metadata *message.Metadata, priority byte) (int, error) {
if err := w.broadcast(data, metadata, priority); err != nil {
// A returned error here is treated as unrecoverable err.
log.WithError(err).WithField("handler", w.topic.String()).Warn("write failed")
}
return 0, nil
}
// broadcast broadcasts message to the entire network.
// The kadcast height is read from message metadata.
func (w *Broadcast) broadcast(data []byte, metadata *message.Metadata, _ byte) error {
h := config.KadcastInitialHeight
// extract kadcast height
if metadata != nil {
if metadata.KadcastHeight == 0 {
// Apparently, this node is the last peer in a bucket of height 0. We
// should not repropagate.
return nil
}
// Decrement kadcast height
h = metadata.KadcastHeight - 1
}
// create the message
b := bytes.NewBuffer(data)
if err := w.gossip.Process(b); err != nil {
return err
}
// prepare message
m := &rusk.BroadcastMessage{
KadcastHeight: uint32(h),
Message: b.Bytes(),
}
// broadcast message
if _, err := w.client.Broadcast(w.ctx, m); err != nil {
log.WithError(err).Warn("failed to broadcast message")
return err
}
return nil
}