-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
wan_lan_dht_test.go
280 lines (252 loc) · 8.05 KB
/
wan_lan_dht_test.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
package integrationtest
import (
"context"
"encoding/binary"
"fmt"
"math"
"math/rand"
"net"
"testing"
"time"
"github.com/ipfs/go-cid"
"github.com/ipfs/kubo/core"
mock "github.com/ipfs/kubo/core/mock"
libp2p2 "github.com/ipfs/kubo/core/node/libp2p"
testutil "github.com/libp2p/go-libp2p-testing/net"
corenet "github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peerstore"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
ma "github.com/multiformats/go-multiaddr"
)
func TestDHTConnectivityFast(t *testing.T) {
conf := testutil.LatencyConfig{
NetworkLatency: 0,
RoutingLatency: 0,
BlockstoreLatency: 0,
}
if err := RunDHTConnectivity(conf, 5); err != nil {
t.Fatal(err)
}
}
func TestDHTConnectivitySlowNetwork(t *testing.T) {
SkipUnlessEpic(t)
conf := testutil.LatencyConfig{NetworkLatency: 400 * time.Millisecond}
if err := RunDHTConnectivity(conf, 5); err != nil {
t.Fatal(err)
}
}
func TestDHTConnectivitySlowRouting(t *testing.T) {
SkipUnlessEpic(t)
conf := testutil.LatencyConfig{RoutingLatency: 400 * time.Millisecond}
if err := RunDHTConnectivity(conf, 5); err != nil {
t.Fatal(err)
}
}
// wan prefix must have a real corresponding ASN for the peer diversity filter to work.
var (
wanPrefix = net.ParseIP("2001:218:3004::")
lanPrefix = net.ParseIP("fe80::")
)
func makeAddr(n uint32, wan bool) ma.Multiaddr {
var ip net.IP
if wan {
ip = append(net.IP{}, wanPrefix...)
} else {
ip = append(net.IP{}, lanPrefix...)
}
binary.LittleEndian.PutUint32(ip[12:], n)
addr, _ := ma.NewMultiaddr(fmt.Sprintf("/ip6/%s/tcp/4242", ip))
return addr
}
func RunDHTConnectivity(conf testutil.LatencyConfig, numPeers int) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// create network
mn := mocknet.New()
mn.SetLinkDefaults(mocknet.LinkOptions{
Latency: conf.NetworkLatency,
Bandwidth: math.MaxInt32,
})
testPeer, err := core.NewNode(ctx, &core.BuildCfg{
Online: true,
Host: mock.MockHostOption(mn),
})
if err != nil {
return err
}
defer testPeer.Close()
wanPeers := []*core.IpfsNode{}
lanPeers := []*core.IpfsNode{}
connectionContext, connCtxCancel := context.WithTimeout(ctx, 15*time.Second)
defer connCtxCancel()
for i := 0; i < numPeers; i++ {
wanPeer, err := core.NewNode(ctx, &core.BuildCfg{
Online: true,
Routing: libp2p2.DHTServerOption,
Host: mock.MockHostOption(mn),
})
if err != nil {
return err
}
defer wanPeer.Close()
wanAddr := makeAddr(uint32(i), true)
wanPeer.Peerstore.AddAddr(wanPeer.Identity, wanAddr, peerstore.PermanentAddrTTL)
for _, p := range wanPeers {
_, _ = mn.LinkPeers(p.Identity, wanPeer.Identity)
_ = wanPeer.PeerHost.Connect(connectionContext, p.Peerstore.PeerInfo(p.Identity))
}
wanPeers = append(wanPeers, wanPeer)
lanPeer, err := core.NewNode(ctx, &core.BuildCfg{
Online: true,
Host: mock.MockHostOption(mn),
})
if err != nil {
return err
}
defer lanPeer.Close()
lanAddr := makeAddr(uint32(i), false)
lanPeer.Peerstore.AddAddr(lanPeer.Identity, lanAddr, peerstore.PermanentAddrTTL)
for _, p := range lanPeers {
_, _ = mn.LinkPeers(p.Identity, lanPeer.Identity)
_ = lanPeer.PeerHost.Connect(connectionContext, p.Peerstore.PeerInfo(p.Identity))
}
lanPeers = append(lanPeers, lanPeer)
}
connCtxCancel()
// Add interfaces / addresses to test peer.
wanAddr := makeAddr(0, true)
testPeer.Peerstore.AddAddr(testPeer.Identity, wanAddr, peerstore.PermanentAddrTTL)
lanAddr := makeAddr(0, false)
testPeer.Peerstore.AddAddr(testPeer.Identity, lanAddr, peerstore.PermanentAddrTTL)
// The test peer is connected to one lan peer.
for _, p := range lanPeers {
if _, err := mn.LinkPeers(testPeer.Identity, p.Identity); err != nil {
return err
}
}
err = testPeer.PeerHost.Connect(ctx, lanPeers[0].Peerstore.PeerInfo(lanPeers[0].Identity))
if err != nil {
return err
}
startupCtx, startupCancel := context.WithTimeout(ctx, time.Second*60)
StartupWait:
for {
select {
case err := <-testPeer.DHT.LAN.RefreshRoutingTable():
if err != nil {
fmt.Printf("Error refreshing routing table: %v\n", err)
}
if testPeer.DHT.LAN.RoutingTable() == nil ||
testPeer.DHT.LAN.RoutingTable().Size() == 0 ||
err != nil {
time.Sleep(100 * time.Millisecond)
continue
}
break StartupWait
case <-startupCtx.Done():
startupCancel()
return fmt.Errorf("expected faster dht bootstrap")
}
}
startupCancel()
// choose a lan peer and validate lan DHT is functioning.
i := rand.Intn(len(lanPeers))
if testPeer.PeerHost.Network().Connectedness(lanPeers[i].Identity) == corenet.Connected {
i = (i + 1) % len(lanPeers)
if testPeer.PeerHost.Network().Connectedness(lanPeers[i].Identity) == corenet.Connected {
_ = testPeer.PeerHost.Network().ClosePeer(lanPeers[i].Identity)
testPeer.PeerHost.Peerstore().ClearAddrs(lanPeers[i].Identity)
}
}
// That peer will provide a new CID, and we'll validate the test node can find it.
provideCid := cid.NewCidV1(cid.Raw, []byte("Lan Provide Record"))
provideCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
if err := lanPeers[i].DHT.Provide(provideCtx, provideCid, true); err != nil {
return err
}
provChan := testPeer.DHT.FindProvidersAsync(provideCtx, provideCid, 0)
prov, ok := <-provChan
if !ok || prov.ID == "" {
return fmt.Errorf("Expected provider. stream closed early")
}
if prov.ID != lanPeers[i].Identity {
return fmt.Errorf("Unexpected lan peer provided record")
}
// Now, connect with a wan peer.
for _, p := range wanPeers {
if _, err := mn.LinkPeers(testPeer.Identity, p.Identity); err != nil {
return err
}
}
err = testPeer.PeerHost.Connect(ctx, wanPeers[0].Peerstore.PeerInfo(wanPeers[0].Identity))
if err != nil {
return err
}
startupCtx, startupCancel = context.WithTimeout(ctx, time.Second*60)
WanStartupWait:
for {
select {
case err := <-testPeer.DHT.WAN.RefreshRoutingTable():
// if err != nil {
// fmt.Printf("Error refreshing routing table: %v\n", err)
// }
if testPeer.DHT.WAN.RoutingTable() == nil ||
testPeer.DHT.WAN.RoutingTable().Size() == 0 ||
err != nil {
time.Sleep(100 * time.Millisecond)
continue
}
break WanStartupWait
case <-startupCtx.Done():
startupCancel()
return fmt.Errorf("expected faster wan dht bootstrap")
}
}
startupCancel()
// choose a wan peer and validate wan DHT is functioning.
i = rand.Intn(len(wanPeers))
if testPeer.PeerHost.Network().Connectedness(wanPeers[i].Identity) == corenet.Connected {
i = (i + 1) % len(wanPeers)
if testPeer.PeerHost.Network().Connectedness(wanPeers[i].Identity) == corenet.Connected {
_ = testPeer.PeerHost.Network().ClosePeer(wanPeers[i].Identity)
testPeer.PeerHost.Peerstore().ClearAddrs(wanPeers[i].Identity)
}
}
// That peer will provide a new CID, and we'll validate the test node can find it.
wanCid := cid.NewCidV1(cid.Raw, []byte("Wan Provide Record"))
wanProvideCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
if err := wanPeers[i].DHT.Provide(wanProvideCtx, wanCid, true); err != nil {
return err
}
provChan = testPeer.DHT.FindProvidersAsync(wanProvideCtx, wanCid, 0)
prov, ok = <-provChan
if !ok || prov.ID == "" {
return fmt.Errorf("Expected one provider, closed early")
}
if prov.ID != wanPeers[i].Identity {
return fmt.Errorf("Unexpected lan peer provided record")
}
// Finally, re-share the lan provided cid from a wan peer and expect a merged result.
i = rand.Intn(len(wanPeers))
if testPeer.PeerHost.Network().Connectedness(wanPeers[i].Identity) == corenet.Connected {
_ = testPeer.PeerHost.Network().ClosePeer(wanPeers[i].Identity)
testPeer.PeerHost.Peerstore().ClearAddrs(wanPeers[i].Identity)
}
provideCtx, cancel = context.WithTimeout(ctx, time.Second)
defer cancel()
if err := wanPeers[i].DHT.Provide(provideCtx, provideCid, true); err != nil {
return err
}
provChan = testPeer.DHT.FindProvidersAsync(provideCtx, provideCid, 0)
prov, ok = <-provChan
if !ok {
return fmt.Errorf("Expected two providers, got 0")
}
prov, ok = <-provChan
if !ok {
return fmt.Errorf("Expected two providers, got 1")
}
return nil
}