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
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package libp2p_test
import (
"context"
"errors"
"sync"
"testing"
"time"
"github.com/ethersphere/bee/pkg/addressbook"
"github.com/ethersphere/bee/pkg/p2p"
"github.com/ethersphere/bee/pkg/p2p/libp2p"
"github.com/ethersphere/bee/pkg/p2p/libp2p/internal/handshake"
"github.com/ethersphere/bee/pkg/statestore/mock"
"github.com/ethersphere/bee/pkg/swarm"
libp2ppeer "github.com/libp2p/go-libp2p-core/peer"
ma "github.com/multiformats/go-multiaddr"
)
func TestAddresses(t *testing.T) {
s, _ := newService(t, 1, libp2pServiceOpts{})
addrs, err := s.Addresses()
if err != nil {
t.Fatal(err)
}
if l := len(addrs); l == 0 {
t.Fatal("no addresses")
}
}
func TestConnectDisconnect(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s1, overlay1 := newService(t, 1, libp2pServiceOpts{})
s2, overlay2 := newService(t, 1, libp2pServiceOpts{})
addr := serviceUnderlayAddress(t, s1)
bzzAddr, err := s2.Connect(ctx, addr)
if err != nil {
t.Fatal(err)
}
expectPeers(t, s2, overlay1)
expectPeersEventually(t, s1, overlay2)
if err := s2.Disconnect(bzzAddr.Overlay); err != nil {
t.Fatal(err)
}
expectPeers(t, s2)
expectPeersEventually(t, s1)
}
func TestDoubleConnect(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s1, overlay1 := newService(t, 1, libp2pServiceOpts{})
s2, overlay2 := newService(t, 1, libp2pServiceOpts{})
addr := serviceUnderlayAddress(t, s1)
if _, err := s2.Connect(ctx, addr); err != nil {
t.Fatal(err)
}
expectPeers(t, s2, overlay1)
expectPeersEventually(t, s1, overlay2)
if _, err := s2.Connect(ctx, addr); !errors.Is(err, p2p.ErrAlreadyConnected) {
t.Fatalf("expected %s error, got %s error", p2p.ErrAlreadyConnected, err)
}
expectPeers(t, s2, overlay1)
expectPeers(t, s1, overlay2)
}
func TestDoubleDisconnect(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s1, overlay1 := newService(t, 1, libp2pServiceOpts{})
s2, overlay2 := newService(t, 1, libp2pServiceOpts{})
addr := serviceUnderlayAddress(t, s1)
bzzAddr, err := s2.Connect(ctx, addr)
if err != nil {
t.Fatal(err)
}
expectPeers(t, s2, overlay1)
expectPeersEventually(t, s1, overlay2)
if err := s2.Disconnect(bzzAddr.Overlay); err != nil {
t.Fatal(err)
}
expectPeers(t, s2)
expectPeersEventually(t, s1)
if err := s2.Disconnect(bzzAddr.Overlay); !errors.Is(err, p2p.ErrPeerNotFound) {
t.Errorf("got error %v, want %v", err, p2p.ErrPeerNotFound)
}
expectPeers(t, s2)
expectPeersEventually(t, s1)
}
func TestMultipleConnectDisconnect(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s1, overlay1 := newService(t, 1, libp2pServiceOpts{})
s2, overlay2 := newService(t, 1, libp2pServiceOpts{})
addr := serviceUnderlayAddress(t, s1)
bzzAddr, err := s2.Connect(ctx, addr)
if err != nil {
t.Fatal(err)
}
expectPeers(t, s2, overlay1)
expectPeersEventually(t, s1, overlay2)
if err := s2.Disconnect(bzzAddr.Overlay); err != nil {
t.Fatal(err)
}
expectPeers(t, s2)
expectPeersEventually(t, s1)
bzzAddr, err = s2.Connect(ctx, addr)
if err != nil {
t.Fatal(err)
}
expectPeers(t, s2, overlay1)
expectPeersEventually(t, s1, overlay2)
if err := s2.Disconnect(bzzAddr.Overlay); err != nil {
t.Fatal(err)
}
expectPeers(t, s2)
expectPeersEventually(t, s1)
}
func TestConnectDisconnectOnAllAddresses(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s1, overlay1 := newService(t, 1, libp2pServiceOpts{})
s2, overlay2 := newService(t, 1, libp2pServiceOpts{})
addrs, err := s1.Addresses()
if err != nil {
t.Fatal(err)
}
for _, addr := range addrs {
bzzAddr, err := s2.Connect(ctx, addr)
if err != nil {
t.Fatal(err)
}
expectPeers(t, s2, overlay1)
expectPeersEventually(t, s1, overlay2)
if err := s2.Disconnect(bzzAddr.Overlay); err != nil {
t.Fatal(err)
}
expectPeers(t, s2)
expectPeersEventually(t, s1)
}
}
func TestDoubleConnectOnAllAddresses(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s1, overlay1 := newService(t, 1, libp2pServiceOpts{})
addrs, err := s1.Addresses()
if err != nil {
t.Fatal(err)
}
for _, addr := range addrs {
// creating new remote host for each address
s2, overlay2 := newService(t, 1, libp2pServiceOpts{})
if _, err := s2.Connect(ctx, addr); err != nil {
t.Fatal(err)
}
expectPeers(t, s2, overlay1)
expectPeersEventually(t, s1, overlay2)
if _, err := s2.Connect(ctx, addr); !errors.Is(err, p2p.ErrAlreadyConnected) {
t.Fatalf("expected %s error, got %s error", p2p.ErrAlreadyConnected, err)
}
expectPeers(t, s2, overlay1)
expectPeers(t, s1, overlay2)
if err := s2.Disconnect(overlay1); err != nil {
t.Fatal(err)
}
expectPeers(t, s2)
expectPeersEventually(t, s1)
s2.Close()
}
}
func TestDifferentNetworkIDs(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s1, _ := newService(t, 1, libp2pServiceOpts{})
s2, _ := newService(t, 2, libp2pServiceOpts{})
addr := serviceUnderlayAddress(t, s1)
if _, err := s2.Connect(ctx, addr); err == nil {
t.Fatal("connect attempt should result with an error")
}
expectPeers(t, s1)
expectPeers(t, s2)
}
func TestConnectWithEnabledQUICAndWSTransports(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s1, overlay1 := newService(t, 1, libp2pServiceOpts{
libp2pOpts: libp2p.Options{
EnableQUIC: true,
EnableWS: true,
},
})
s2, overlay2 := newService(t, 1, libp2pServiceOpts{
libp2pOpts: libp2p.Options{
EnableQUIC: true,
EnableWS: true,
},
})
addr := serviceUnderlayAddress(t, s1)
if _, err := s2.Connect(ctx, addr); err != nil {
t.Fatal(err)
}
expectPeers(t, s2, overlay1)
expectPeersEventually(t, s1, overlay2)
}
// TestConnectRepeatHandshake tests if handshake was attempted more then once by the same peer
func TestConnectRepeatHandshake(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s1, overlay1 := newService(t, 1, libp2pServiceOpts{})
s2, overlay2 := newService(t, 1, libp2pServiceOpts{})
addr := serviceUnderlayAddress(t, s1)
_, err := s2.Connect(ctx, addr)
if err != nil {
t.Fatal(err)
}
expectPeers(t, s2, overlay1)
expectPeersEventually(t, s1, overlay2)
info, err := libp2ppeer.AddrInfoFromP2pAddr(addr)
if err != nil {
t.Fatal(err)
}
stream, err := s2.NewStreamForPeerID(info.ID, handshake.ProtocolName, handshake.ProtocolVersion, handshake.StreamName)
if err != nil {
t.Fatal(err)
}
if _, err := s2.HandshakeService().Handshake(ctx, libp2p.NewStream(stream), info.Addrs[0], info.ID); err == nil {
t.Fatalf("expected stream error")
}
expectPeersEventually(t, s2)
expectPeersEventually(t, s1)
}
func TestBlocklisting(t *testing.T) {
s1, overlay1 := newService(t, 1, libp2pServiceOpts{})
s2, overlay2 := newService(t, 1, libp2pServiceOpts{})
addr1 := serviceUnderlayAddress(t, s1)
addr2 := serviceUnderlayAddress(t, s2)
// s2 connects to s1, thus the notifier on s1 should be called on Connect
_, err := s2.Connect(context.Background(), addr1)
if err != nil {
t.Fatal(err)
}
expectPeers(t, s2, overlay1)
expectPeersEventually(t, s1, overlay2)
if err := s2.Blocklist(overlay1, 0); err != nil {
t.Fatal(err)
}
expectPeers(t, s2)
expectPeersEventually(t, s1)
// s2 connects to s1, thus the notifier on s1 should be called on Connect
_, err = s2.Connect(context.Background(), addr1)
if err == nil {
t.Fatal("expected error during connection, got nil")
}
expectPeers(t, s2)
expectPeersEventually(t, s1)
// s2 connects to s1, thus the notifier on s1 should be called on Connect
_, err = s1.Connect(context.Background(), addr2)
if err == nil {
t.Fatal("expected error during connection, got nil")
}
expectPeers(t, s1)
expectPeersEventually(t, s2)
}
func TestTopologyNotifier(t *testing.T) {
var (
mtx sync.Mutex
ctx = context.Background()
ab1, ab2 = addressbook.New(mock.NewStateStore()), addressbook.New(mock.NewStateStore())
n1connectedPeer p2p.Peer
n1disconnectedPeer p2p.Peer
n2connectedPeer p2p.Peer
n2disconnectedPeer p2p.Peer
n1c = func(_ context.Context, p p2p.Peer) error {
mtx.Lock()
defer mtx.Unlock()
expectZeroAddress(t, n1connectedPeer.Address) // fail if set more than once
n1connectedPeer = p
return nil
}
n1d = func(p p2p.Peer) {
mtx.Lock()
defer mtx.Unlock()
n1disconnectedPeer = p
}
n2c = func(_ context.Context, p p2p.Peer) error {
mtx.Lock()
defer mtx.Unlock()
expectZeroAddress(t, n2connectedPeer.Address) // fail if set more than once
n2connectedPeer = p
return nil
}
n2d = func(p p2p.Peer) {
mtx.Lock()
defer mtx.Unlock()
n2disconnectedPeer = p
}
)
notifier1 := mockNotifier(n1c, n1d)
s1, overlay1 := newService(t, 1, libp2pServiceOpts{Addressbook: ab1})
s1.SetNotifier(notifier1)
notifier2 := mockNotifier(n2c, n2d)
s2, overlay2 := newService(t, 1, libp2pServiceOpts{Addressbook: ab2})
s2.SetNotifier(notifier2)
addr := serviceUnderlayAddress(t, s1)
// s2 connects to s1, thus the notifier on s1 should be called on Connect
bzzAddr, err := s2.Connect(ctx, addr)
if err != nil {
t.Fatal(err)
}
expectPeers(t, s2, overlay1)
expectPeersEventually(t, s1, overlay2)
// expect that n1 notifee called with s2 overlay
waitAddrSet(t, &n1connectedPeer.Address, &mtx, overlay2)
mtx.Lock()
expectZeroAddress(t, n1disconnectedPeer.Address, n2connectedPeer.Address, n2disconnectedPeer.Address)
mtx.Unlock()
// check address book entries are there
checkAddressbook(t, ab2, overlay1, addr)
// s2 disconnects from s1 so s1 disconnect notifiee should be called
if err := s2.Disconnect(bzzAddr.Overlay); err != nil {
t.Fatal(err)
}
expectPeers(t, s2)
expectPeersEventually(t, s1)
waitAddrSet(t, &n1disconnectedPeer.Address, &mtx, overlay2)
// note that both n1disconnect and n2disconnect callbacks are called after just
// one disconnect. this is due to the fact the when the libp2p abstraction is explicitly
// called to disconnect from a peer, it will also notify the topology notifiee, since
// peer disconnections can also result from components from outside the bound of the
// topology driver
mtx.Lock()
expectZeroAddress(t, n2connectedPeer.Address)
mtx.Unlock()
addr2 := serviceUnderlayAddress(t, s2)
// s1 connects to s2, thus the notifiee on s2 should be called on Connect
bzzAddr2, err := s1.Connect(ctx, addr2)
if err != nil {
t.Fatal(err)
}
expectPeers(t, s1, overlay2)
expectPeersEventually(t, s2, overlay1)
waitAddrSet(t, &n2connectedPeer.Address, &mtx, overlay1)
// s1 disconnects from s2 so s2 disconnect notifiee should be called
if err := s1.Disconnect(bzzAddr2.Overlay); err != nil {
t.Fatal(err)
}
expectPeers(t, s1)
expectPeersEventually(t, s2)
waitAddrSet(t, &n2disconnectedPeer.Address, &mtx, overlay1)
}
func expectZeroAddress(t *testing.T, addrs ...swarm.Address) {
t.Helper()
for i, a := range addrs {
if !a.Equal(swarm.ZeroAddress) {
t.Fatalf("address did not equal zero address. index %d", i)
}
}
}
func waitAddrSet(t *testing.T, addr *swarm.Address, mtx *sync.Mutex, exp swarm.Address) {
t.Helper()
for i := 0; i < 20; i++ {
mtx.Lock()
if addr.Equal(exp) {
mtx.Unlock()
return
}
mtx.Unlock()
time.Sleep(10 * time.Millisecond)
}
t.Fatal("timed out waiting for address to be set")
}
func checkAddressbook(t *testing.T, ab addressbook.Getter, overlay swarm.Address, underlay ma.Multiaddr) {
t.Helper()
addr, err := ab.Get(overlay)
if err != nil {
t.Fatal(err)
}
if !addr.Overlay.Equal(overlay) {
t.Fatalf("overlay mismatch. got %s want %s", addr.Overlay, overlay)
}
if !addr.Underlay.Equal(underlay) {
t.Fatalf("underlay mismatch. got %s, want %s", addr.Underlay, underlay)
}
}
type notifiee struct {
connected func(context.Context, p2p.Peer) error
disconnected func(p2p.Peer)
}
func (n *notifiee) Connected(c context.Context, p p2p.Peer) error {
return n.connected(c, p)
}
func (n *notifiee) Disconnected(p p2p.Peer) {
n.disconnected(p)
}
func mockNotifier(c cFunc, d dFunc) p2p.Notifier {
return ¬ifiee{connected: c, disconnected: d}
}
type cFunc func(context.Context, p2p.Peer) error
type dFunc func(p2p.Peer)