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

import (
4
	"context"
Jan Winkelmann's avatar
Jan Winkelmann committed
5

6 7
	bsnet "github.com/ipfs/go-ipfs/exchange/bitswap/network"
	mockrouting "github.com/ipfs/go-ipfs/routing/mock"
Steven Allen's avatar
Steven Allen committed
8 9 10 11
	mockpeernet "gx/ipfs/QmNRN4eZGmY89CRC4T5PC4xDYRx6GkDKEfRnvrT65fVeio/go-libp2p/p2p/net/mock"
	ds "gx/ipfs/QmPpegoMqhAEqjncrzArm7KVWAkCm78rqL2DPuNjhPrshg/go-datastore"
	peer "gx/ipfs/Qma7H6RW8wRrfZpNSXwxYGcd1E149s42FpWNpDNieSVrnU/go-libp2p-peer"
	testutil "gx/ipfs/QmfB65MYJqaKzBiMvW47fquCRhmEeXW6AhrJSGM7TeY5eG/go-testutil"
12 13 14 15 16 17 18
)

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

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

23
func (pn *peernet) Adapter(p testutil.Identity) bsnet.BitSwapNetwork {
24 25 26 27
	client, err := pn.Mocknet.AddPeer(p.PrivateKey(), p.Address())
	if err != nil {
		panic(err.Error())
	}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
28
	routing := pn.routingserver.ClientWithDatastore(context.TODO(), p, ds.NewMapDatastore())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
29
	return bsnet.NewFromIpfsHost(client, routing)
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
}

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