peernet.go 1.2 KB
Newer Older
1 2 3
package bitswap

import (
4 5
	bsnet "github.com/ipfs/go-ipfs/exchange/bitswap/network"
	mockrouting "github.com/ipfs/go-ipfs/routing/mock"
6
	testutil "github.com/ipfs/go-ipfs/thirdparty/testutil"
7
	peer "gx/ipfs/QmRBqJF7hb8ZSpRcMwUt8hNhydWcxGEhtk81HKq6oUwKvs/go-libp2p-peer"
Jeromy's avatar
Jeromy committed
8
	mockpeernet "gx/ipfs/QmVCe3SNMjkcPgnpFhZs719dheq6xE7gJwjzV7aWcUM4Ms/go-libp2p/p2p/net/mock"
Jeromy's avatar
Jeromy committed
9
	context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
Jeromy's avatar
Jeromy committed
10
	ds "gx/ipfs/QmfQzVugPq1w5shWRcLWSeiHF4a2meBX7yVD8Vw7GWJM9o/go-datastore"
11 12 13 14 15 16 17
)

type peernet struct {
	mockpeernet.Mocknet
	routingserver mockrouting.Server
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
18
func StreamNet(ctx context.Context, net mockpeernet.Mocknet, rs mockrouting.Server) (Network, error) {
19 20 21
	return &peernet{net, rs}, nil
}

22
func (pn *peernet) Adapter(p testutil.Identity) bsnet.BitSwapNetwork {
23 24 25 26
	client, err := pn.Mocknet.AddPeer(p.PrivateKey(), p.Address())
	if err != nil {
		panic(err.Error())
	}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
27
	routing := pn.routingserver.ClientWithDatastore(context.TODO(), p, ds.NewMapDatastore())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
28
	return bsnet.NewFromIpfsHost(client, routing)
29 30 31 32 33 34 35 36 37 38 39 40
}

func (pn *peernet) HasPeer(p peer.ID) bool {
	for _, member := range pn.Mocknet.Peers() {
		if p == member {
			return true
		}
	}
	return false
}

var _ Network = &peernet{}