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

import (
4
	"context"
5 6
	bsnet "github.com/ipfs/go-ipfs/exchange/bitswap/network"
	mockrouting "github.com/ipfs/go-ipfs/routing/mock"
7
	ds "gx/ipfs/QmVSase1JP7cq9QkPT46oNwdp9pT6kBkG3oqS14y3QcZjG/go-datastore"
Jeromy's avatar
Jeromy committed
8
	testutil "gx/ipfs/QmWRCn8vruNAzHx8i6SAXinuheRitKEGu8c7m26stKvsYx/go-testutil"
9
	peer "gx/ipfs/QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB/go-libp2p-peer"
10
	mockpeernet "gx/ipfs/QmefgzMbKZYsmHFkLqxgaTBG9ypeEjrdWRD5WXH4j1cWDL/go-libp2p/p2p/net/mock"
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
}

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

40
var _ Network = (*peernet)(nil)