func GenSwarmNetwork(t *testing.T, ctx context.Context) *swarm.Network { p := tu.RandPeerNetParamsOrFatal(t) ps := peer.NewPeerstore() ps.AddPubKey(p.ID, p.PubKey) ps.AddPrivKey(p.ID, p.PrivKey) n, err := swarm.NewNetwork(ctx, []ma.Multiaddr{p.Addr}, p.ID, ps, metrics.NewBandwidthCounter()) if err != nil { t.Fatal(err) } ps.AddAddrs(p.ID, n.ListenAddresses(), peer.PermanentAddrTTL) return n }
// create a 'Host' with a random peer to listen on the given address func makeDummyHost(listen string) (host.Host, error) { addr, err := ma.NewMultiaddr(listen) if err != nil { return nil, err } pid, err := testutil.RandPeerID() if err != nil { return nil, err } // bandwidth counter, should be optional in the future bwc := metrics.NewBandwidthCounter() // create a new swarm to be used by the service host netw, err := swarm.NewNetwork(context.Background(), []ma.Multiaddr{addr}, pid, pstore.NewPeerstore(), bwc) if err != nil { return nil, err } log.Printf("I am %s/ipfs/%s\n", addr, pid.Pretty()) return bhost.New(netw), nil }