-
Notifications
You must be signed in to change notification settings - Fork 10
/
floodsub.go
447 lines (407 loc) · 10.4 KB
/
floodsub.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
package floodsub
import (
"context"
"sync"
"time"
"github.com/aperturerobotics/bifrost/hash"
"github.com/aperturerobotics/bifrost/link"
"github.com/aperturerobotics/bifrost/peer"
"github.com/aperturerobotics/bifrost/protocol"
"github.com/aperturerobotics/bifrost/pubsub"
pubmessage "github.com/aperturerobotics/bifrost/pubsub/util/pubmessage"
stream_packet "github.com/aperturerobotics/bifrost/stream/packet"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/patrickmn/go-cache"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
// maxMessageSize constrains the message buffer allocation size.
// currently set to 2MB
const maxMessageSize = 2000000
const (
FloodSubID = protocol.ID("bifrost/floodsub")
)
// FloodSub implements the FloodSub router.
//
// TODO bind to a specific peer
type FloodSub struct {
// conf is the config
conf *Config
// le is the logger
le *logrus.Entry
// handler is the pubsub handler
handler pubsub.PubSubHandler
// wakeCh wakes the execute loop
wakeCh chan struct{}
// publishCh is for publishing messages
publishCh chan *publishChMsg
// seenMessages is a map with recently seen messages
seenMessages *cache.Cache
mtx sync.Mutex
// peers are the complete set of executing remote peer streams that we can
// use to contact next-hop peers. this is the "working set" of peers.
peers map[pubsub.PeerLinkTuple]*streamHandler
// channels are active local channel subscriptions.
// key: channel ID
channels map[string]map[*subscription]struct{}
// peerChannels tracks which topics each peer is subscribed to
peerChannels map[string]map[pubsub.PeerLinkTuple]struct{}
// incSessions are sessions that were just added and haven't been executed yet.
// if !streamHandler.initiator -> push to peers map when executing
incSessions []*streamHandler
}
// publishChMsg is a message queued for publishing
type publishChMsg struct {
msg *peer.SignedMsg
prevHopPeer peer.ID
channelID string
}
// NewFloodSub constructs a new FloodSub PubSub router.
func NewFloodSub(
ctx context.Context,
le *logrus.Entry,
handler pubsub.PubSubHandler,
cc *Config,
) (pubsub.PubSub, error) {
return &FloodSub{
le: le,
conf: cc,
handler: handler,
wakeCh: make(chan struct{}, 1),
peers: make(map[pubsub.PeerLinkTuple]*streamHandler),
channels: make(map[string]map[*subscription]struct{}),
publishCh: make(chan *publishChMsg, 16),
seenMessages: cache.New(120*time.Second, 30*time.Second),
peerChannels: make(map[string]map[pubsub.PeerLinkTuple]struct{}),
}, nil
}
// Execute executes the PubSub routines.
func (m *FloodSub) Execute(ctx context.Context) error {
m.le.Debug("floodsub starting")
// re-evaluate at most once every 100ms
evalTimer := time.NewTicker(time.Millisecond * 100)
defer evalTimer.Stop()
pubbedChannels := make(map[string]struct{})
for {
var initSet []*SubscriptionOpts
m.mtx.Lock()
for i := range m.incSessions {
s := m.incSessions[i]
nctx, nctxCancel := context.WithCancel(ctx)
s.ctx = nctx
s.ctxCancel = nctxCancel
// if !s.initiator {
if initSet == nil {
initSet = make([]*SubscriptionOpts, 0, len(m.channels))
for chid := range m.channels {
initSet = append(initSet, &SubscriptionOpts{
ChannelId: chid,
Subscribe: true,
})
}
}
s.packetCh <- &Packet{Subscriptions: initSet}
// }
go func() {
err := s.executeSession()
if err != nil && err != context.Canceled {
s.le.WithError(err).Warn("session exited with error")
}
// if s.initiator {
m.mtx.Lock()
if m.peers[s.tpl] == s {
delete(m.peers, s.tpl)
}
m.mtx.Unlock()
// }
}()
// if s.initiator {
m.peers[s.tpl] = s
// }
}
m.incSessions = nil
m.mtx.Unlock() // intentional mtx hold-break
initSet = nil
var xmitPeers []*streamHandler
var subChanges []*SubscriptionOpts
m.mtx.Lock()
// sweep empty channels
for chid, chm := range m.channels {
if len(chm) == 0 {
if _, ok := pubbedChannels[chid]; ok {
// cleanup no-ref subscription
// inform peers we no longer need the channel
subChanges = append(subChanges, &SubscriptionOpts{
ChannelId: chid,
Subscribe: false,
})
delete(pubbedChannels, chid)
}
delete(m.channels, chid)
m.le.WithField("channel-id", chid).Info("unsubscribed from channel")
} else if _, ok := pubbedChannels[chid]; !ok {
pubbedChannels[chid] = struct{}{}
subChanges = append(subChanges, &SubscriptionOpts{
ChannelId: chid,
Subscribe: true,
})
}
}
if len(subChanges) != 0 {
xmitPeers = make([]*streamHandler, 0, len(m.peers))
for _, p := range m.peers {
if p.ctx != nil {
xmitPeers = append(xmitPeers, p)
}
}
}
m.mtx.Unlock()
for _, p := range xmitPeers { // xmitPeers is usually nil
p.writePacket(&Packet{Subscriptions: subChanges})
}
var woken bool
for !woken {
select {
case <-ctx.Done():
return ctx.Err()
case pubMsg := <-m.publishCh:
m.execPublish(pubMsg.prevHopPeer, pubMsg)
case <-m.wakeCh:
woken = true
}
}
select {
case <-ctx.Done():
return ctx.Err()
case <-evalTimer.C:
}
}
}
// execPublish executes publishing a message
func (m *FloodSub) execPublish(prevHopPeerID peer.ID, pubMsg *publishChMsg) {
pkt := &Packet{
Publish: []*peer.SignedMsg{
pubMsg.msg,
},
}
chid := pubMsg.channelID
tosend := make(map[pubsub.PeerLinkTuple]struct{})
m.mtx.Lock()
if peerChannels, ok := m.peerChannels[chid]; ok {
for p := range peerChannels {
tosend[p] = struct{}{}
}
}
for pid := range tosend {
pidString := pid.PeerID.String()
if pidString == pubMsg.msg.GetFromPeerId() ||
pid.PeerID == prevHopPeerID {
continue
}
peer, ok := m.peers[pid]
if ok {
peer.writePacket(pkt)
}
}
m.mtx.Unlock()
}
// AddSubscription adds a channel subscription, returning a subscription handle.
func (m *FloodSub) AddSubscription(ctx context.Context, privKey crypto.PrivKey, channelID string) (pubsub.Subscription, error) {
if channelID == "" {
return nil, errors.New("channel id must be specified")
}
peerID, err := peer.IDFromPrivateKey(privKey)
if err != nil {
return nil, err
}
ns := &subscription{
ctx: ctx,
m: m,
channelID: channelID,
handlers: make(map[*subscriptionHandler]struct{}),
privKey: privKey,
peerID: peerID,
}
m.mtx.Lock()
subs := m.channels[channelID]
if subs == nil {
subs = make(map[*subscription]struct{})
defer m.wake()
m.channels[channelID] = subs
m.le.
WithField("channel-id", channelID).
WithField("sub-peer-id", peerID.String()).
Info("subscribed to channel")
}
subs[ns] = struct{}{}
m.mtx.Unlock()
return ns, nil
}
// AddPeerStream adds a negotiated peer stream.
// The pubsub should communicate over the stream.
func (m *FloodSub) AddPeerStream(
tpl pubsub.PeerLinkTuple,
initiator bool,
mstrm link.MountedStream,
) {
le := m.le.WithField("peer", tpl.PeerID.String())
if !mstrm.GetOpenOpts().Encrypted || !mstrm.GetOpenOpts().Reliable {
le.Warn("rejecting unencrypted or unreliable pubsub stream")
mstrm.GetStream().Close()
return
}
sh := &streamHandler{
m: m,
le: le,
tpl: tpl,
peerID: mstrm.GetPeerID(),
packetCh: make(chan *Packet, 32),
stream: stream_packet.NewSession(mstrm.GetStream(), maxMessageSize),
initiator: initiator,
}
m.mtx.Lock()
// if !initiator {
if e, ok := m.peers[tpl]; ok {
if e.ctxCancel != nil {
e.ctxCancel()
}
}
m.peers[tpl] = sh
// }
m.incSessions = append(m.incSessions, sh)
m.mtx.Unlock()
m.wake()
}
// Publish writes to the channel with a private key.
func (m *FloodSub) Publish(
ctx context.Context,
channelID string,
privKey crypto.PrivKey,
data []byte,
) error {
pht := m.conf.GetPublishHashType()
if pht == hash.HashType_HashType_UNKNOWN {
pht = hash.HashType_HashType_SHA256
}
msg, inner, err := pubmessage.NewPubMessage(channelID, privKey, pht, data)
if err != nil {
return err
}
pid, err := peer.IDFromPrivateKey(privKey)
if err != nil {
return err
}
m.handleValidMessage(ctx, pid, msg, inner)
return nil
}
// Close closes the pubsub.
func (m *FloodSub) Close() {
m.mtx.Lock()
for pid, s := range m.peers {
if s.ctxCancel != nil {
s.ctxCancel()
}
delete(m.peers, pid)
}
for _, s := range m.incSessions {
if s.stream != nil {
s.stream.Close()
}
}
m.incSessions = nil
m.mtx.Unlock()
}
// handleValidMessage handles a valid message, repeating it to other peers and handlers.
func (m *FloodSub) handleValidMessage(
ctx context.Context,
prevHopPeer peer.ID,
pkt *peer.SignedMsg,
pktInner *pubmessage.PubMessageInner,
) {
channelID := pktInner.GetChannel()
msgId := pkt.ComputeMessageID()
if _, ok := m.seenMessages.Get(msgId); ok {
return
}
m.seenMessages.Set(msgId, pkt, 0)
pid, err := peer.IDB58Decode(pkt.GetFromPeerId())
if err != nil {
return
}
msg := pubmessage.NewMessage(pid, pktInner)
m.mtx.Lock()
subs := m.channels[channelID]
for sub := range subs {
ss := sub
go func() {
ss.mtx.Lock()
for s := range ss.handlers {
s.cb(msg)
}
ss.mtx.Unlock()
}()
}
m.mtx.Unlock()
select {
case m.publishCh <- &publishChMsg{
msg: pkt,
channelID: channelID,
prevHopPeer: prevHopPeer,
}:
case <-ctx.Done():
}
}
// wake wakes the controller
func (m *FloodSub) wake() {
select {
case m.wakeCh <- struct{}{}:
default:
}
}
/*
func shufflePeers(peers []pubsub.PeerLinkTuple) {
for i := range peers {
j := rand.Intn(i + 1)
peers[i], peers[j] = peers[j], peers[i]
}
}
// getPeers returns peers for a channel
func (m *FloodSub) getPeers(
channel string,
count int,
filter func(pubsub.PeerLinkTuple) bool,
) []pubsub.PeerLinkTuple {
tmap, ok := m.peerChannels[channel]
if !ok {
return nil
}
peers := make([]pubsub.PeerLinkTuple, 0, len(tmap))
for p := range tmap {
if filter(p) {
peers = append(peers, p)
}
}
shufflePeers(peers)
if count > 0 && len(peers) > count {
peers = peers[:count]
}
return peers
}
// peerListToMap converts a slice to a map
func peerListToMap(peers []pubsub.PeerLinkTuple) map[pubsub.PeerLinkTuple]struct{} {
pmap := make(map[pubsub.PeerLinkTuple]struct{})
for _, p := range peers {
pmap[p] = struct{}{}
}
return pmap
}
func peerMapToList(peers map[pubsub.PeerLinkTuple]struct{}) []pubsub.PeerLinkTuple {
plst := make([]pubsub.PeerLinkTuple, 0, len(peers))
for p := range peers {
plst = append(plst, p)
}
return plst
}
*/
// _ is a type assertion
var _ pubsub.PubSub = ((*FloodSub)(nil))