-
Notifications
You must be signed in to change notification settings - Fork 1
/
mock_peernet.go
394 lines (332 loc) · 9.32 KB
/
mock_peernet.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
package mocknet
import (
"bytes"
"context"
"fmt"
"math/rand"
"sync"
"github.com/libp2p/go-libp2p/core/event"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/peerstore"
ma "github.com/multiformats/go-multiaddr"
)
// peernet implements network.Network
type peernet struct {
mocknet *mocknet // parent
peer peer.ID
ps peerstore.Peerstore
emitter event.Emitter
// conns are actual live connections between peers.
// many conns could run over each link.
// **conns are NOT shared between peers**
connsByPeer map[peer.ID]map[*conn]struct{}
connsByLink map[*link]map[*conn]struct{}
// implement network.Network
streamHandler network.StreamHandler
notifmu sync.Mutex
notifs map[network.Notifiee]struct{}
sync.RWMutex
}
// newPeernet constructs a new peernet
func newPeernet(m *mocknet, p peer.ID, ps peerstore.Peerstore, bus event.Bus) (*peernet, error) {
emitter, err := bus.Emitter(&event.EvtPeerConnectednessChanged{})
if err != nil {
return nil, err
}
n := &peernet{
mocknet: m,
peer: p,
ps: ps,
emitter: emitter,
connsByPeer: map[peer.ID]map[*conn]struct{}{},
connsByLink: map[*link]map[*conn]struct{}{},
notifs: make(map[network.Notifiee]struct{}),
}
return n, nil
}
func (pn *peernet) Close() error {
// close the connections
for _, c := range pn.allConns() {
c.Close()
}
pn.emitter.Close()
return pn.ps.Close()
}
// allConns returns all the connections between this peer and others
func (pn *peernet) allConns() []*conn {
pn.RLock()
var cs []*conn
for _, csl := range pn.connsByPeer {
for c := range csl {
cs = append(cs, c)
}
}
pn.RUnlock()
return cs
}
func (pn *peernet) Peerstore() peerstore.Peerstore {
return pn.ps
}
func (pn *peernet) String() string {
return fmt.Sprintf("<mock.peernet %s - %d conns>", pn.peer, len(pn.allConns()))
}
// handleNewStream is an internal function to trigger the client's handler
func (pn *peernet) handleNewStream(s network.Stream) {
pn.RLock()
handler := pn.streamHandler
pn.RUnlock()
if handler != nil {
go handler(s)
}
}
// DialPeer attempts to establish a connection to a given peer.
// Respects the context.
func (pn *peernet) DialPeer(ctx context.Context, p peer.ID) (network.Conn, error) {
return pn.connect(p)
}
func (pn *peernet) connect(p peer.ID) (*conn, error) {
if p == pn.peer {
return nil, fmt.Errorf("attempted to dial self %s", p)
}
// first, check if we already have live connections
pn.RLock()
cs, found := pn.connsByPeer[p]
if found && len(cs) > 0 {
var chosen *conn
for c := range cs { // because cs is a map
chosen = c // select first
break
}
pn.RUnlock()
return chosen, nil
}
pn.RUnlock()
log.Debugf("%s (newly) dialing %s", pn.peer, p)
// ok, must create a new connection. we need a link
links := pn.mocknet.LinksBetweenPeers(pn.peer, p)
if len(links) < 1 {
return nil, fmt.Errorf("%s cannot connect to %s", pn.peer, p)
}
// if many links found, how do we select? for now, randomly...
// this would be an interesting place to test logic that can measure
// links (network interfaces) and select properly
l := links[rand.Intn(len(links))]
log.Debugf("%s dialing %s openingConn", pn.peer, p)
// create a new connection with link
c := pn.openConn(p, l.(*link))
return c, nil
}
func (pn *peernet) openConn(r peer.ID, l *link) *conn {
lc, rc := l.newConnPair(pn)
log.Debugf("%s opening connection to %s", pn.LocalPeer(), lc.RemotePeer())
addConnPair(pn, rc.net, lc, rc)
go rc.net.remoteOpenedConn(rc)
pn.addConn(lc)
return lc
}
// addConnPair adds connection to both peernets at the same time
// must be followerd by pn1.addConn(c1) and pn2.addConn(c2)
func addConnPair(pn1, pn2 *peernet, c1, c2 *conn) {
var l1, l2 = pn1, pn2 // peernets in lock order
// bytes compare as string compare is lexicographical
if bytes.Compare([]byte(l1.LocalPeer()), []byte(l2.LocalPeer())) > 0 {
l1, l2 = l2, l1
}
l1.Lock()
l2.Lock()
add := func(pn *peernet, c *conn) {
_, found := pn.connsByPeer[c.RemotePeer()]
if !found {
pn.connsByPeer[c.RemotePeer()] = map[*conn]struct{}{}
}
pn.connsByPeer[c.RemotePeer()][c] = struct{}{}
_, found = pn.connsByLink[c.link]
if !found {
pn.connsByLink[c.link] = map[*conn]struct{}{}
}
pn.connsByLink[c.link][c] = struct{}{}
}
add(pn1, c1)
add(pn2, c2)
c1.notifLk.Lock()
c2.notifLk.Lock()
l2.Unlock()
l1.Unlock()
}
func (pn *peernet) remoteOpenedConn(c *conn) {
log.Debugf("%s accepting connection from %s", pn.LocalPeer(), c.RemotePeer())
pn.addConn(c)
}
// addConn constructs and adds a connection
// to given remote peer over given link
func (pn *peernet) addConn(c *conn) {
defer c.notifLk.Unlock()
pn.notifyAll(func(n network.Notifiee) {
n.Connected(pn, c)
})
pn.emitter.Emit(event.EvtPeerConnectednessChanged{
Peer: c.remote,
Connectedness: network.Connected,
})
}
// removeConn removes a given conn
func (pn *peernet) removeConn(c *conn) {
pn.Lock()
cs, found := pn.connsByLink[c.link]
if !found || len(cs) < 1 {
panic(fmt.Sprintf("attempting to remove a conn that doesnt exist %p", c.link))
}
delete(cs, c)
cs, found = pn.connsByPeer[c.remote]
if !found {
panic(fmt.Sprintf("attempting to remove a conn that doesnt exist %v", c.remote))
}
delete(cs, c)
pn.Unlock()
// notify asynchronously to mimic Swarm
// FIXME: IIRC, we wanted to make notify for Close synchronous
go func() {
c.notifLk.Lock()
defer c.notifLk.Unlock()
pn.notifyAll(func(n network.Notifiee) {
n.Disconnected(c.net, c)
})
}()
c.net.emitter.Emit(event.EvtPeerConnectednessChanged{
Peer: c.remote,
Connectedness: network.NotConnected,
})
}
// LocalPeer the network's LocalPeer
func (pn *peernet) LocalPeer() peer.ID {
return pn.peer
}
// Peers returns the connected peers
func (pn *peernet) Peers() []peer.ID {
pn.RLock()
defer pn.RUnlock()
peers := make([]peer.ID, 0, len(pn.connsByPeer))
for _, cs := range pn.connsByPeer {
for c := range cs {
peers = append(peers, c.remote)
break
}
}
return peers
}
// Conns returns all the connections of this peer
func (pn *peernet) Conns() []network.Conn {
pn.RLock()
defer pn.RUnlock()
out := make([]network.Conn, 0, len(pn.connsByPeer))
for _, cs := range pn.connsByPeer {
for c := range cs {
out = append(out, c)
}
}
return out
}
func (pn *peernet) ConnsToPeer(p peer.ID) []network.Conn {
pn.RLock()
defer pn.RUnlock()
cs, found := pn.connsByPeer[p]
if !found || len(cs) == 0 {
return nil
}
cs2 := make([]network.Conn, 0, len(cs))
for c := range cs {
cs2 = append(cs2, c)
}
return cs2
}
// ClosePeer connections to peer
func (pn *peernet) ClosePeer(p peer.ID) error {
pn.RLock()
cs, found := pn.connsByPeer[p]
if !found {
pn.RUnlock()
return nil
}
conns := make([]*conn, 0, len(cs))
for c := range cs {
conns = append(conns, c)
}
pn.RUnlock()
for _, c := range conns {
c.Close()
}
return nil
}
// BandwidthTotals returns the total amount of bandwidth transferred
func (pn *peernet) BandwidthTotals() (in uint64, out uint64) {
// need to implement this. probably best to do it in swarm this time.
// need a "metrics" object
return 0, 0
}
// Listen tells the network to start listening on given multiaddrs.
func (pn *peernet) Listen(addrs ...ma.Multiaddr) error {
pn.Peerstore().AddAddrs(pn.LocalPeer(), addrs, peerstore.PermanentAddrTTL)
return nil
}
// ListenAddresses returns a list of addresses at which this network listens.
func (pn *peernet) ListenAddresses() []ma.Multiaddr {
return pn.Peerstore().Addrs(pn.LocalPeer())
}
// InterfaceListenAddresses returns a list of addresses at which this network
// listens. It expands "any interface" addresses (/ip4/0.0.0.0, /ip6/::) to
// use the known local interfaces.
func (pn *peernet) InterfaceListenAddresses() ([]ma.Multiaddr, error) {
return pn.ListenAddresses(), nil
}
// Connectedness returns a state signaling connection capabilities
// For now only returns Connecter || NotConnected. Expand into more later.
func (pn *peernet) Connectedness(p peer.ID) network.Connectedness {
pn.Lock()
defer pn.Unlock()
cs, found := pn.connsByPeer[p]
if found && len(cs) > 0 {
return network.Connected
}
return network.NotConnected
}
// NewStream returns a new stream to given peer p.
// If there is no connection to p, attempts to create one.
func (pn *peernet) NewStream(ctx context.Context, p peer.ID) (network.Stream, error) {
c, err := pn.DialPeer(ctx, p)
if err != nil {
return nil, err
}
return c.NewStream(ctx)
}
// SetStreamHandler sets the new stream handler on the Network.
// This operation is threadsafe.
func (pn *peernet) SetStreamHandler(h network.StreamHandler) {
pn.Lock()
pn.streamHandler = h
pn.Unlock()
}
// Notify signs up Notifiee to receive signals when events happen
func (pn *peernet) Notify(f network.Notifiee) {
pn.notifmu.Lock()
pn.notifs[f] = struct{}{}
pn.notifmu.Unlock()
}
// StopNotify unregisters Notifiee from receiving signals
func (pn *peernet) StopNotify(f network.Notifiee) {
pn.notifmu.Lock()
delete(pn.notifs, f)
pn.notifmu.Unlock()
}
// notifyAll runs the notification function on all Notifiees
func (pn *peernet) notifyAll(notification func(f network.Notifiee)) {
pn.notifmu.Lock()
// notify synchronously to mimic Swarm
for n := range pn.notifs {
notification(n)
}
pn.notifmu.Unlock()
}
func (pn *peernet) ResourceManager() network.ResourceManager {
return &network.NullResourceManager{}
}