mock_net.go 7.02 KB
Newer Older
1
2
3
4
5
6
7
package mocknet

import (
	"fmt"
	"sync"

	ic "github.com/jbenet/go-ipfs/p2p/crypto"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
8
9
	host "github.com/jbenet/go-ipfs/p2p/host"
	bhost "github.com/jbenet/go-ipfs/p2p/host/basic"
10
11
12
13
14
15
16
17
18
19
20
	inet "github.com/jbenet/go-ipfs/p2p/net2"
	peer "github.com/jbenet/go-ipfs/p2p/peer"
	testutil "github.com/jbenet/go-ipfs/util/testutil"

	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
	ctxgroup "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-ctxgroup"
	ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
)

// mocknet implements mocknet.Mocknet
type mocknet struct {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
21
22
	nets  map[peer.ID]*peernet
	hosts map[peer.ID]*bhost.BasicHost
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

	// links make it possible to connect two peers.
	// think of links as the physical medium.
	// usually only one, but there could be multiple
	// **links are shared between peers**
	links map[peer.ID]map[peer.ID]map[*link]struct{}

	linkDefaults LinkOptions

	cg ctxgroup.ContextGroup // for Context closing
	sync.RWMutex
}

func New(ctx context.Context) Mocknet {
	return &mocknet{
		nets:  map[peer.ID]*peernet{},
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
39
		hosts: map[peer.ID]*bhost.BasicHost{},
40
41
42
43
44
		links: map[peer.ID]map[peer.ID]map[*link]struct{}{},
		cg:    ctxgroup.WithContext(ctx),
	}
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
45
func (mn *mocknet) GenPeer() (host.Host, error) {
46
47
48
49
50
51
52
	sk, _, err := testutil.RandKeyPair(512)
	if err != nil {
		return nil, err
	}

	a := testutil.RandLocalTCPAddress()

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
53
	h, err := mn.AddPeer(sk, a)
54
55
56
57
	if err != nil {
		return nil, err
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
58
	return h, nil
59
60
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
61
func (mn *mocknet) AddPeer(k ic.PrivKey, a ma.Multiaddr) (host.Host, error) {
62
63
64
65
66
	n, err := newPeernet(mn.cg.Context(), mn, k, a)
	if err != nil {
		return nil, err
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
67
68
	h := bhost.New(n)

69
70
71
72
73
74
75
76
77
78
	// make sure to add listening address!
	// this makes debugging things simpler as remembering to register
	// an address may cause unexpected failure.
	n.Peerstore().AddAddress(n.LocalPeer(), a)
	log.Debugf("mocknet added listen addr for peer: %s -- %s", n.LocalPeer(), a)

	mn.cg.AddChildGroup(n.cg)

	mn.Lock()
	mn.nets[n.peer] = n
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
79
	mn.hosts[n.peer] = h
80
	mn.Unlock()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
81
	return h, nil
82
83
84
85
86
87
88
89
90
91
92
93
94
}

func (mn *mocknet) Peers() []peer.ID {
	mn.RLock()
	defer mn.RUnlock()

	cp := make([]peer.ID, 0, len(mn.nets))
	for _, n := range mn.nets {
		cp = append(cp, n.peer)
	}
	return cp
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
95
96
97
98
99
100
101
func (mn *mocknet) Host(pid peer.ID) host.Host {
	mn.RLock()
	host := mn.hosts[pid]
	mn.RUnlock()
	return host
}

102
func (mn *mocknet) Net(pid peer.ID) inet.Network {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
103
104
105
106
107
108
109
	mn.RLock()
	n := mn.nets[pid]
	mn.RUnlock()
	return n
}

func (mn *mocknet) Hosts() []host.Host {
110
111
112
	mn.RLock()
	defer mn.RUnlock()

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
113
114
115
	cp := make([]host.Host, 0, len(mn.hosts))
	for _, h := range mn.hosts {
		cp = append(cp, h)
116
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
117
	return cp
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
}

func (mn *mocknet) Nets() []inet.Network {
	mn.RLock()
	defer mn.RUnlock()

	cp := make([]inet.Network, 0, len(mn.nets))
	for _, n := range mn.nets {
		cp = append(cp, n)
	}
	return cp
}

// Links returns a copy of the internal link state map.
// (wow, much map. so data structure. how compose. ahhh pointer)
func (mn *mocknet) Links() LinkMap {
	mn.RLock()
	defer mn.RUnlock()

	links := map[string]map[string]map[Link]struct{}{}
	for p1, lm := range mn.links {
		sp1 := string(p1)
		links[sp1] = map[string]map[Link]struct{}{}
		for p2, ls := range lm {
			sp2 := string(p2)
			links[sp1][sp2] = map[Link]struct{}{}
			for l := range ls {
				links[sp1][sp2][l] = struct{}{}
			}
		}
	}
	return links
}

func (mn *mocknet) LinkAll() error {
	nets := mn.Nets()
	for _, n1 := range nets {
		for _, n2 := range nets {
			if _, err := mn.LinkNets(n1, n2); err != nil {
				return err
			}
		}
	}
	return nil
}

func (mn *mocknet) LinkPeers(p1, p2 peer.ID) (Link, error) {
	mn.RLock()
	n1 := mn.nets[p1]
	n2 := mn.nets[p2]
	mn.RUnlock()

	if n1 == nil {
		return nil, fmt.Errorf("network for p1 not in mocknet")
	}

	if n2 == nil {
		return nil, fmt.Errorf("network for p2 not in mocknet")
	}

	return mn.LinkNets(n1, n2)
}

func (mn *mocknet) validate(n inet.Network) (*peernet, error) {
	// WARNING: assumes locks acquired

	nr, ok := n.(*peernet)
	if !ok {
		return nil, fmt.Errorf("Network not supported (use mock package nets only)")
	}

	if _, found := mn.nets[nr.peer]; !found {
		return nil, fmt.Errorf("Network not on mocknet. is it from another mocknet?")
	}

	return nr, nil
}

func (mn *mocknet) LinkNets(n1, n2 inet.Network) (Link, error) {
	mn.RLock()
	n1r, err1 := mn.validate(n1)
	n2r, err2 := mn.validate(n2)
	ld := mn.linkDefaults
	mn.RUnlock()

	if err1 != nil {
		return nil, err1
	}
	if err2 != nil {
		return nil, err2
	}

	l := newLink(mn, ld)
	l.nets = append(l.nets, n1r, n2r)
	mn.addLink(l)
	return l, nil
}

func (mn *mocknet) Unlink(l2 Link) error {

	l, ok := l2.(*link)
	if !ok {
		return fmt.Errorf("only links from mocknet are supported")
	}

	mn.removeLink(l)
	return nil
}

func (mn *mocknet) UnlinkPeers(p1, p2 peer.ID) error {
	ls := mn.LinksBetweenPeers(p1, p2)
	if ls == nil {
		return fmt.Errorf("no link between p1 and p2")
	}

	for _, l := range ls {
		if err := mn.Unlink(l); err != nil {
			return err
		}
	}
	return nil
}

func (mn *mocknet) UnlinkNets(n1, n2 inet.Network) error {
	return mn.UnlinkPeers(n1.LocalPeer(), n2.LocalPeer())
}

// get from the links map. and lazily contruct.
func (mn *mocknet) linksMapGet(p1, p2 peer.ID) *map[*link]struct{} {

	l1, found := mn.links[p1]
	if !found {
		mn.links[p1] = map[peer.ID]map[*link]struct{}{}
		l1 = mn.links[p1] // so we make sure it's there.
	}

	l2, found := l1[p2]
	if !found {
		m := map[*link]struct{}{}
		l1[p2] = m
		l2 = l1[p2]
	}

	return &l2
}

func (mn *mocknet) addLink(l *link) {
	mn.Lock()
	defer mn.Unlock()

	n1, n2 := l.nets[0], l.nets[1]
	(*mn.linksMapGet(n1.peer, n2.peer))[l] = struct{}{}
	(*mn.linksMapGet(n2.peer, n1.peer))[l] = struct{}{}
}

func (mn *mocknet) removeLink(l *link) {
	mn.Lock()
	defer mn.Unlock()

	n1, n2 := l.nets[0], l.nets[1]
	delete(*mn.linksMapGet(n1.peer, n2.peer), l)
	delete(*mn.linksMapGet(n2.peer, n1.peer), l)
}

func (mn *mocknet) ConnectAll() error {
	nets := mn.Nets()
	for _, n1 := range nets {
		for _, n2 := range nets {
			if n1 == n2 {
				continue
			}

			if _, err := mn.ConnectNets(n1, n2); err != nil {
				return err
			}
		}
	}
	return nil
}

func (mn *mocknet) ConnectPeers(a, b peer.ID) (inet.Conn, error) {
	return mn.Net(a).DialPeer(mn.cg.Context(), b)
}

func (mn *mocknet) ConnectNets(a, b inet.Network) (inet.Conn, error) {
	return a.DialPeer(mn.cg.Context(), b.LocalPeer())
}

func (mn *mocknet) DisconnectPeers(p1, p2 peer.ID) error {
	return mn.Net(p1).ClosePeer(p2)
}

func (mn *mocknet) DisconnectNets(n1, n2 inet.Network) error {
	return n1.ClosePeer(n2.LocalPeer())
}

func (mn *mocknet) LinksBetweenPeers(p1, p2 peer.ID) []Link {
	mn.RLock()
	defer mn.RUnlock()

	ls2 := *mn.linksMapGet(p1, p2)
	cp := make([]Link, 0, len(ls2))
	for l := range ls2 {
		cp = append(cp, l)
	}
	return cp
}

func (mn *mocknet) LinksBetweenNets(n1, n2 inet.Network) []Link {
	return mn.LinksBetweenPeers(n1.LocalPeer(), n2.LocalPeer())
}

func (mn *mocknet) SetLinkDefaults(o LinkOptions) {
	mn.Lock()
	mn.linkDefaults = o
	mn.Unlock()
}

func (mn *mocknet) LinkDefaults() LinkOptions {
	mn.RLock()
	defer mn.RUnlock()
	return mn.linkDefaults
}