Browse Source

remove the context from the libp2p and from the Host contructor

pull/1190/head
Marten Seemann 3 years ago
parent
commit
b7bee3855c
  1. 11
      config/config.go
  2. 4
      config/muxer_test.go
  3. 2
      examples/chat-with-mdns/README.md
  4. 2
      examples/chat-with-mdns/main.go
  5. 4
      examples/chat-with-rendezvous/README.md
  6. 11
      examples/chat-with-rendezvous/chat.go
  7. 5
      examples/chat/chat.go
  8. 4
      examples/chat/chat_test.go
  9. 2
      examples/echo/main.go
  10. 2
      examples/http-proxy/proxy.go
  11. 7
      examples/ipfs-camp-2019/01-Transports/main.go
  12. 9
      examples/ipfs-camp-2019/02-Multiaddrs/main.go
  13. 13
      examples/ipfs-camp-2019/03-Muxing-Encryption/main.go
  14. 1
      examples/ipfs-camp-2019/05-Discovery/main.go
  15. 1
      examples/ipfs-camp-2019/06-Pubsub/main.go
  16. 1
      examples/ipfs-camp-2019/07-Messaging/main.go
  17. 1
      examples/ipfs-camp-2019/08-End/main.go
  18. 12
      examples/libp2p-host/README.md
  19. 6
      examples/libp2p-host/host.go
  20. 2
      examples/multipro/main.go
  21. 2
      examples/pubsub/chat/README.md
  22. 2
      examples/pubsub/chat/main.go
  23. 6
      examples/relay/main.go
  24. 2
      examples/routed-echo/main.go
  25. 12
      libp2p.go
  26. 79
      libp2p_test.go
  27. 3
      p2p/discovery/mdns/mdns_test.go
  28. 4
      p2p/discovery/mdns_legacy/mdns_test.go
  29. 4
      p2p/host/basic/basic_host.go
  30. 62
      p2p/host/basic/basic_host_test.go
  31. 26
      p2p/host/relay/autorelay_test.go
  32. 4
      p2p/net/mock/mock_net.go
  33. 18
      p2p/protocol/holepunch/coordination_test.go
  34. 22
      p2p/protocol/identify/id_test.go
  35. 4
      p2p/protocol/ping/ping_test.go
  36. 4
      p2p/test/backpressure/backpressure_test.go
  37. 8
      p2p/test/reconnects/reconnect_test.go

11
config/config.go

@ -1,7 +1,6 @@
package config
import (
"context"
"crypto/rand"
"fmt"
"time"
@ -100,7 +99,7 @@ type Config struct {
HolePunchingOptions []holepunch.Option
}
func (cfg *Config) makeSwarm(ctx context.Context) (*swarm.Swarm, error) {
func (cfg *Config) makeSwarm() (*swarm.Swarm, error) {
if cfg.Peerstore == nil {
return nil, fmt.Errorf("no peerstore specified")
}
@ -182,13 +181,13 @@ func (cfg *Config) addTransports(h host.Host) (err error) {
// NewNode constructs a new libp2p Host from the Config.
//
// This function consumes the config. Do not reuse it (really!).
func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) {
swrm, err := cfg.makeSwarm(ctx)
func (cfg *Config) NewNode() (host.Host, error) {
swrm, err := cfg.makeSwarm()
if err != nil {
return nil, err
}
h, err := bhost.NewHost(ctx, swrm, &bhost.HostOpts{
h, err := bhost.NewHost(swrm, &bhost.HostOpts{
ConnManager: cfg.ConnManager,
AddrsFactory: cfg.AddrsFactory,
NATManager: cfg.NATManager,
@ -296,7 +295,7 @@ func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) {
Peerstore: pstoremem.NewPeerstore(),
}
dialer, err := autoNatCfg.makeSwarm(ctx)
dialer, err := autoNatCfg.makeSwarm()
if err != nil {
h.Close()
return nil, err

4
config/muxer_test.go

@ -1,7 +1,6 @@
package config
import (
"context"
"testing"
"github.com/libp2p/go-libp2p-core/host"
@ -59,8 +58,7 @@ func TestMuxerBadTypes(t *testing.T) {
}
func TestCatchDuplicateTransportsMuxer(t *testing.T) {
ctx := context.Background()
h, err := bhost.NewHost(ctx, swarmt.GenSwarm(t), nil)
h, err := bhost.NewHost(swarmt.GenSwarm(t), nil)
if err != nil {
t.Fatal(err)
}

2
examples/chat-with-mdns/README.md

@ -28,7 +28,7 @@ ctx := context.Background()
// libp2p.New constructs a new libp2p Host.
// Other options can be added here.
host, err := libp2p.New(ctx)
host, err := libp2p.New()
```
[libp2p.New](https://godoc.org/github.com/libp2p/go-libp2p#New) is the constructor for libp2p node. It creates a host with given configuration.

2
examples/chat-with-mdns/main.go

@ -100,11 +100,9 @@ func main() {
// libp2p.New constructs a new libp2p Host.
// Other options can be added here.
host, err := libp2p.New(
ctx,
libp2p.ListenAddrs(sourceMultiAddr),
libp2p.Identity(prvKey),
)
if err != nil {
panic(err)
}

4
examples/chat-with-rendezvous/README.md

@ -23,11 +23,9 @@ Use two different terminal windows to run
1. **Configure a p2p host**
```go
ctx := context.Background()
// libp2p.New constructs a new libp2p Host.
// Other options can be added here.
host, err := libp2p.New(ctx)
host, err := libp2p.New()
```
[libp2p.New](https://godoc.org/github.com/libp2p/go-libp2p#New) is the constructor for a libp2p node. It creates a host with the given configuration. Right now, all the options are default, documented [here](https://godoc.org/github.com/libp2p/go-libp2p#New)

11
examples/chat-with-rendezvous/chat.go

@ -12,10 +12,10 @@ import (
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/protocol"
"github.com/libp2p/go-libp2p-discovery"
discovery "github.com/libp2p/go-libp2p-discovery"
dht "github.com/libp2p/go-libp2p-kad-dht"
multiaddr "github.com/multiformats/go-multiaddr"
"github.com/multiformats/go-multiaddr"
"github.com/ipfs/go-log/v2"
)
@ -95,13 +95,9 @@ func main() {
return
}
ctx := context.Background()
// libp2p.New constructs a new libp2p Host. Other options can be added
// here.
host, err := libp2p.New(ctx,
libp2p.ListenAddrs([]multiaddr.Multiaddr(config.ListenAddresses)...),
)
host, err := libp2p.New(libp2p.ListenAddrs([]multiaddr.Multiaddr(config.ListenAddresses)...))
if err != nil {
panic(err)
}
@ -116,6 +112,7 @@ func main() {
// client because we want each peer to maintain its own local copy of the
// DHT, so that the bootstrapping node of the DHT can go down without
// inhibiting future peer discovery.
ctx := context.Background()
kademliaDHT, err := dht.New(ctx, host)
if err != nil {
panic(err)

5
examples/chat/chat.go

@ -123,7 +123,7 @@ func main() {
r = rand.Reader
}
h, err := makeHost(ctx, *sourcePort, r)
h, err := makeHost(*sourcePort, r)
if err != nil {
log.Println(err)
return
@ -148,7 +148,7 @@ func main() {
select {}
}
func makeHost(ctx context.Context, port int, randomness io.Reader) (host.Host, error) {
func makeHost(port int, randomness io.Reader) (host.Host, error) {
// Creates a new RSA key pair for this host.
prvKey, _, err := crypto.GenerateKeyPairWithReader(crypto.RSA, 2048, randomness)
if err != nil {
@ -162,7 +162,6 @@ func makeHost(ctx context.Context, port int, randomness io.Reader) (host.Host, e
// libp2p.New constructs a new libp2p Host.
// Other options can be added here.
return libp2p.New(
ctx,
libp2p.ListenAddrs(sourceMultiAddr),
libp2p.Identity(prvKey),
)

4
examples/chat/chat_test.go

@ -35,7 +35,7 @@ func TestMain(t *testing.T) {
return
}
h1, err := makeHost(ctx, port1, rand.Reader)
h1, err := makeHost(port1, rand.Reader)
if err != nil {
log.Println(err)
return
@ -48,7 +48,7 @@ func TestMain(t *testing.T) {
dest := fmt.Sprintf("/ip4/127.0.0.1/tcp/%v/p2p/%s", port1, h1.ID().Pretty())
h2, err := makeHost(ctx, port2, rand.Reader)
h2, err := makeHost(port2, rand.Reader)
if err != nil {
log.Println(err)
return

2
examples/echo/main.go

@ -84,7 +84,7 @@ func makeBasicHost(listenPort int, insecure bool, randseed int64) (host.Host, er
opts = append(opts, libp2p.NoSecurity)
}
return libp2p.New(context.Background(), opts...)
return libp2p.New(opts...)
}
func getHostAddress(ha host.Host) string {

2
examples/http-proxy/proxy.go

@ -30,7 +30,7 @@ const Protocol = "/proxy-example/0.0.1"
// makeRandomHost creates a libp2p host with a randomly generated identity.
// This step is described in depth in other tutorials.
func makeRandomHost(port int) host.Host {
host, err := libp2p.New(context.Background(), libp2p.ListenAddrStrings(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", port)))
host, err := libp2p.New(libp2p.ListenAddrStrings(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", port)))
if err != nil {
log.Fatalln(err)
}

7
examples/ipfs-camp-2019/01-Transports/main.go

@ -1,19 +1,14 @@
package main
import (
"context"
"github.com/libp2p/go-libp2p"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// TODO: add some libp2p.Transport options to this chain!
transports := libp2p.ChainOptions()
host, err := libp2p.New(ctx, transports)
host, err := libp2p.New(transports)
if err != nil {
panic(err)
}

9
examples/ipfs-camp-2019/02-Multiaddrs/main.go

@ -1,17 +1,12 @@
package main
import (
"context"
"github.com/libp2p/go-libp2p"
tcp "github.com/libp2p/go-tcp-transport"
"github.com/libp2p/go-tcp-transport"
ws "github.com/libp2p/go-ws-transport"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
transports := libp2p.ChainOptions(
libp2p.Transport(tcp.NewTCPTransport),
libp2p.Transport(ws.New),
@ -20,7 +15,7 @@ func main() {
// TODO: add some listen addresses with the libp2p.ListenAddrs or
// libp2p.ListenAddrStrings configuration options.
host, err := libp2p.New(ctx, transports)
host, err := libp2p.New(transports)
if err != nil {
panic(err)
}

13
examples/ipfs-camp-2019/03-Muxing-Encryption/main.go

@ -3,18 +3,16 @@ package main
import (
"context"
"fmt"
"time"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p-core/peer"
tcp "github.com/libp2p/go-tcp-transport"
"github.com/libp2p/go-tcp-transport"
ws "github.com/libp2p/go-ws-transport"
"github.com/multiformats/go-multiaddr"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
transports := libp2p.ChainOptions(
libp2p.Transport(tcp.NewTCPTransport),
libp2p.Transport(ws.New),
@ -27,10 +25,11 @@ func main() {
"/ip4/0.0.0.0/tcp/0/ws",
)
host, err := libp2p.New(ctx, transports, listenAddrs)
host, err := libp2p.New(transports, listenAddrs)
if err != nil {
panic(err)
}
defer host.Close()
for _, addr := range host.Addrs() {
fmt.Println("Listening on", addr)
@ -46,12 +45,12 @@ func main() {
panic(err)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
err = host.Connect(ctx, *targetInfo)
if err != nil {
panic(err)
}
fmt.Println("Connected to", targetInfo.ID)
host.Close()
}

1
examples/ipfs-camp-2019/05-Discovery/main.go

@ -41,7 +41,6 @@ func main() {
// TODO: Configure libp2p to use a DHT with a libp2p.Routing option
host, err := libp2p.New(
ctx,
transports,
listenAddrs,
muxers,

1
examples/ipfs-camp-2019/06-Pubsub/main.go

@ -65,7 +65,6 @@ func main() {
routing := libp2p.Routing(newDHT)
host, err := libp2p.New(
ctx,
transports,
listenAddrs,
muxers,

1
examples/ipfs-camp-2019/07-Messaging/main.go

@ -61,7 +61,6 @@ func main() {
routing := libp2p.Routing(newDHT)
host, err := libp2p.New(
ctx,
transports,
listenAddrs,
muxers,

1
examples/ipfs-camp-2019/08-End/main.go

@ -61,7 +61,6 @@ func main() {
routing := libp2p.Routing(newDHT)
host, err := libp2p.New(
ctx,
transports,
listenAddrs,
muxers,

12
examples/libp2p-host/README.md

@ -8,7 +8,6 @@ If you want to create a host with a default configuration, you can do the follow
```go
import (
"context"
"crypto/rand"
"fmt"
@ -16,16 +15,12 @@ import (
"github.com/libp2p/go-libp2p-core/crypto"
)
// The context governs the lifetime of the libp2p node
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// To construct a simple host with all the default settings, just use `New`
h, err := libp2p.New(ctx)
h, err := libp2p.New()
if err != nil {
panic(err)
}
defer h.Close()
fmt.Printf("Hello World, my hosts ID is %s\n", h.ID())
```
@ -46,7 +41,7 @@ if err != nil {
var idht *dht.IpfsDHT
h2, err := libp2p.New(ctx,
h2, err := libp2p.New(
// Use the keypair we generated
libp2p.Identity(priv),
// Multiple listen addresses
@ -84,6 +79,7 @@ h2, err := libp2p.New(ctx,
if err != nil {
panic(err)
}
defer h2.Close()
fmt.Printf("Hello World, my second hosts ID is %s\n", h2.ID())
```

6
examples/libp2p-host/host.go

@ -26,10 +26,11 @@ func run() {
defer cancel()
// To construct a simple host with all the default settings, just use `New`
h, err := libp2p.New(ctx)
h, err := libp2p.New()
if err != nil {
panic(err)
}
defer h.Close()
log.Printf("Hello World, my hosts ID is %s\n", h.ID())
@ -48,7 +49,7 @@ func run() {
var idht *dht.IpfsDHT
h2, err := libp2p.New(ctx,
h2, err := libp2p.New(
// Use the keypair we generated
libp2p.Identity(priv),
// Multiple listen addresses
@ -91,6 +92,7 @@ func run() {
if err != nil {
panic(err)
}
defer h2.Close()
// The last step to get fully up and running would be to connect to
// bootstrap peers (or any other peers). We leave this commented as

2
examples/multipro/main.go

@ -1,7 +1,6 @@
package main
import (
"context"
"fmt"
"log"
"math/rand"
@ -37,7 +36,6 @@ func makeRandomNode(port int, done chan bool) *Node {
priv, _, _ := crypto.GenerateKeyPair(crypto.Secp256k1, 256)
listen, _ := ma.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", port))
host, _ := libp2p.New(
context.Background(),
libp2p.ListenAddrs(listen),
libp2p.Identity(priv),
)

2
examples/pubsub/chat/README.md

@ -56,7 +56,7 @@ func main() {
// (omitted) parse flags, etc...
// create a new libp2p Host that listens on a random TCP port
h, err := libp2p.New(ctx, libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/0"))
h, err := libp2p.New(libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/0"))
if err != nil {
panic(err)
}

2
examples/pubsub/chat/main.go

@ -29,7 +29,7 @@ func main() {
ctx := context.Background()
// create a new libp2p Host that listens on a random TCP port
h, err := libp2p.New(ctx, libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/0"))
h, err := libp2p.New(libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/0"))
if err != nil {
panic(err)
}

6
examples/relay/main.go

@ -22,7 +22,7 @@ func run() {
// of them.
// Tell the host use relays
h1, err := libp2p.New(context.Background(), libp2p.EnableRelay())
h1, err := libp2p.New(libp2p.EnableRelay())
if err != nil {
log.Printf("Failed to create h1: %v", err)
return
@ -30,7 +30,7 @@ func run() {
// Tell the host to relay connections for other peers (The ability to *use*
// a relay vs the ability to *be* a relay)
h2, err := libp2p.New(context.Background(), libp2p.DisableRelay())
h2, err := libp2p.New(libp2p.DisableRelay())
if err != nil {
log.Printf("Failed to create h2: %v", err)
return
@ -43,7 +43,7 @@ func run() {
// Zero out the listen addresses for the host, so it can only communicate
// via p2p-circuit for our example
h3, err := libp2p.New(context.Background(), libp2p.ListenAddrs(), libp2p.EnableRelay())
h3, err := libp2p.New(libp2p.ListenAddrs(), libp2p.EnableRelay())
if err != nil {
log.Printf("Failed to create h3: %v", err)
return

2
examples/routed-echo/main.go

@ -57,7 +57,7 @@ func makeRoutedHost(listenPort int, randseed int64, bootstrapPeers []peer.AddrIn
ctx := context.Background()
basicHost, err := libp2p.New(ctx, opts...)
basicHost, err := libp2p.New(opts...)
if err != nil {
return nil, err
}

12
libp2p.go

@ -1,9 +1,7 @@
package libp2p
import (
"context"
config "github.com/libp2p/go-libp2p/config"
"github.com/libp2p/go-libp2p/config"
"github.com/libp2p/go-libp2p-core/host"
)
@ -53,8 +51,8 @@ func ChainOptions(opts ...Option) Option {
// peerstore.
//
// To stop/shutdown the returned libp2p node, the user needs to cancel the passed context and call `Close` on the returned Host.
func New(ctx context.Context, opts ...Option) (host.Host, error) {
return NewWithoutDefaults(ctx, append(opts, FallbackDefaults)...)
func New(opts ...Option) (host.Host, error) {
return NewWithoutDefaults(append(opts, FallbackDefaults)...)
}
// NewWithoutDefaults constructs a new libp2p node with the given options but
@ -63,10 +61,10 @@ func New(ctx context.Context, opts ...Option) (host.Host, error) {
// Warning: This function should not be considered a stable interface. We may
// choose to add required services at any time and, by using this function, you
// opt-out of any defaults we may provide.
func NewWithoutDefaults(ctx context.Context, opts ...Option) (host.Host, error) {
func NewWithoutDefaults(opts ...Option) (host.Host, error) {
var cfg Config
if err := cfg.Apply(opts...); err != nil {
return nil, err
}
return cfg.NewNode(ctx)
return cfg.NewNode()
}

79
libp2p_test.go

@ -14,11 +14,11 @@ import (
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/transport"
noise "github.com/libp2p/go-libp2p-noise"
tptu "github.com/libp2p/go-libp2p-transport-upgrader"
"github.com/libp2p/go-tcp-transport"
ma "github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/require"
tptu "github.com/libp2p/go-libp2p-transport-upgrader"
"github.com/stretchr/testify/require"
)
func TestNewHost(t *testing.T) {
@ -30,8 +30,7 @@ func TestNewHost(t *testing.T) {
}
func TestBadTransportConstructor(t *testing.T) {
ctx := context.Background()
h, err := New(ctx, Transport(func() {}))
h, err := New(Transport(func() {}))
if err == nil {
h.Close()
t.Fatal("expected an error")
@ -42,7 +41,6 @@ func TestBadTransportConstructor(t *testing.T) {
}
func TestTransportConstructor(t *testing.T) {
ctx := context.Background()
ctor := func(
h host.Host,
_ connmgr.ConnectionGater,
@ -50,7 +48,7 @@ func TestTransportConstructor(t *testing.T) {
) transport.Transport {
return tcp.NewTCPTransport(upgrader)
}
h, err := New(ctx, Transport(ctor))
h, err := New(Transport(ctor))
if err != nil {
t.Fatal(err)
}
@ -58,11 +56,8 @@ func TestTransportConstructor(t *testing.T) {
}
func TestNoListenAddrs(t *testing.T) {
ctx := context.Background()
h, err := New(ctx, NoListenAddrs)
if err != nil {
t.Fatal(err)
}
h, err := New(NoListenAddrs)
require.NoError(t, err)
defer h.Close()
if len(h.Addrs()) != 0 {
t.Fatal("expected no addresses")
@ -71,16 +66,12 @@ func TestNoListenAddrs(t *testing.T) {
func TestNoTransports(t *testing.T) {
ctx := context.Background()
a, err := New(ctx, NoTransports)
if err != nil {
t.Fatal(err)
}
a, err := New(NoTransports)
require.NoError(t, err)
defer a.Close()
b, err := New(ctx, ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))
if err != nil {
t.Fatal(err)
}
b, err := New(ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))
require.NoError(t, err)
defer b.Close()
err = a.Connect(ctx, peer.AddrInfo{
@ -93,34 +84,24 @@ func TestNoTransports(t *testing.T) {
}
func TestInsecure(t *testing.T) {
ctx := context.Background()
h, err := New(ctx, NoSecurity)
if err != nil {
t.Fatal(err)
}
h, err := New(NoSecurity)
require.NoError(t, err)
h.Close()
}
func TestAutoNATService(t *testing.T) {
ctx := context.Background()
h, err := New(ctx, EnableNATService())
if err != nil {
t.Fatal(err)
}
h, err := New(EnableNATService())
require.NoError(t, err)
h.Close()
}
func TestDefaultListenAddrs(t *testing.T) {
ctx := context.Background()
re := regexp.MustCompile("/(ip)[4|6]/((0.0.0.0)|(::))/tcp/")
re2 := regexp.MustCompile("/p2p-circuit")
// Test 1: Setting the correct listen addresses if userDefined.Transport == nil && userDefined.ListenAddrs == nil
h, err := New(ctx)
if err != nil {
t.Fatal(err)
}
h, err := New()
require.NoError(t, err)
for _, addr := range h.Network().ListenAddresses() {
if re.FindStringSubmatchIndex(addr.String()) == nil &&
re2.FindStringSubmatchIndex(addr.String()) == nil {
@ -131,13 +112,8 @@ func TestDefaultListenAddrs(t *testing.T) {
h.Close()
// Test 2: Listen addr only include relay if user defined transport is passed.
h, err = New(
ctx,
Transport(tcp.NewTCPTransport),
)
if err != nil {
t.Fatal(err)
}
h, err = New(Transport(tcp.NewTCPTransport))
require.NoError(t, err)
if len(h.Network().ListenAddresses()) != 1 {
t.Error("expected one listen addr with user defined transport")
@ -149,22 +125,17 @@ func TestDefaultListenAddrs(t *testing.T) {
}
func makeRandomHost(t *testing.T, port int) (host.Host, error) {
ctx := context.Background()
priv, _, err := crypto.GenerateKeyPair(crypto.RSA, 2048)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
opts := []Option{
return New([]Option{
ListenAddrStrings(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", port)),
Identity(priv),
DefaultTransports,
DefaultMuxers,
DefaultSecurity,
NATPortMap(),
}
return New(ctx, opts...)
}...)
}
func TestChainOptions(t *testing.T) {
@ -198,17 +169,13 @@ func TestChainOptions(t *testing.T) {
}
func TestTcpSimultaneousConnect(t *testing.T) {
ctx := context.Background()
// Host1
h1, err := New(ctx, Transport(tcp.NewTCPTransport), Security(noise.ID, noise.New),
ListenAddrs(ma.StringCast("/ip4/0.0.0.0/tcp/0")))
h1, err := New(Transport(tcp.NewTCPTransport), Security(noise.ID, noise.New), ListenAddrs(ma.StringCast("/ip4/0.0.0.0/tcp/0")))
require.NoError(t, err)
defer h1.Close()
// Host2
h2, err := New(ctx, Transport(tcp.NewTCPTransport), Security(noise.ID, noise.New),
ListenAddrs(ma.StringCast("/ip4/0.0.0.0/tcp/0")))
h2, err := New(Transport(tcp.NewTCPTransport), Security(noise.ID, noise.New), ListenAddrs(ma.StringCast("/ip4/0.0.0.0/tcp/0")))
require.NoError(t, err)
defer h2.Close()

3
p2p/discovery/mdns/mdns_test.go

@ -1,7 +1,6 @@
package mdns
import (
"context"
"sync"
"testing"
"time"
@ -16,7 +15,7 @@ import (
func setupMDNS(t *testing.T, notifee Notifee) (host.Host, *mdnsService) {
t.Helper()
host, err := libp2p.New(context.Background(), libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))
host, err := libp2p.New(libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))
require.NoError(t, err)
s := NewMdnsService(host, "")
s.RegisterNotifee(notifee)

4
p2p/discovery/mdns_legacy/mdns_test.go

@ -28,9 +28,9 @@ func TestMdnsDiscovery(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
a, err := bhost.NewHost(ctx, swarmt.GenSwarm(t), nil)
a, err := bhost.NewHost(swarmt.GenSwarm(t), nil)
require.NoError(t, err)
b, err := bhost.NewHost(ctx, swarmt.GenSwarm(t), nil)
b, err := bhost.NewHost(swarmt.GenSwarm(t), nil)
require.NoError(t, err)
sa, err := NewMdnsService(ctx, a, time.Second, "someTag")

4
p2p/host/basic/basic_host.go

@ -146,8 +146,8 @@ type HostOpts struct {
}
// NewHost constructs a new *BasicHost and activates it by attaching its stream and connection handlers to the given inet.Network.
func NewHost(ctx context.Context, n network.Network, opts *HostOpts) (*BasicHost, error) {
hostCtx, cancel := context.WithCancel(ctx)
func NewHost(n network.Network, opts *HostOpts) (*BasicHost, error) {
hostCtx, cancel := context.WithCancel(context.Background())
if opts == nil {
opts = &HostOpts{}
}

62
p2p/host/basic/basic_host_test.go

@ -33,8 +33,7 @@ import (
)
func TestHostDoubleClose(t *testing.T) {
ctx := context.Background()
h1, err := NewHost(ctx, swarmt.GenSwarm(t), nil)
h1, err := NewHost(swarmt.GenSwarm(t), nil)
require.NoError(t, err)
h1.Close()
h1.Close()
@ -42,10 +41,10 @@ func TestHostDoubleClose(t *testing.T) {
func TestHostSimple(t *testing.T) {
ctx := context.Background()
h1, err := NewHost(ctx, swarmt.GenSwarm(t), nil)
h1, err := NewHost(swarmt.GenSwarm(t), nil)
require.NoError(t, err)
defer h1.Close()
h2, err := NewHost(ctx, swarmt.GenSwarm(t), nil)
h2, err := NewHost(swarmt.GenSwarm(t), nil)
require.NoError(t, err)
defer h2.Close()
@ -92,8 +91,7 @@ func TestHostSimple(t *testing.T) {
}
func TestMultipleClose(t *testing.T) {
ctx := context.Background()
h, err := NewHost(ctx, swarmt.GenSwarm(t), nil)
h, err := NewHost(swarmt.GenSwarm(t), nil)
require.NoError(t, err)
require.NoError(t, h.Close())
@ -102,9 +100,7 @@ func TestMultipleClose(t *testing.T) {
}
func TestSignedPeerRecordWithNoListenAddrs(t *testing.T) {
ctx := context.Background()
h, err := NewHost(ctx, swarmt.GenSwarm(t, swarmt.OptDialOnly), nil)
h, err := NewHost(swarmt.GenSwarm(t, swarmt.OptDialOnly), nil)
require.NoError(t, err)
h.Start()
@ -134,8 +130,7 @@ func TestSignedPeerRecordWithNoListenAddrs(t *testing.T) {
}
func TestProtocolHandlerEvents(t *testing.T) {
ctx := context.Background()
h, err := NewHost(ctx, swarmt.GenSwarm(t), nil)
h, err := NewHost(swarmt.GenSwarm(t), nil)
require.NoError(t, err)
defer h.Close()
@ -196,8 +191,7 @@ func TestHostAddrsFactory(t *testing.T) {
return []ma.Multiaddr{maddr}
}
ctx := context.Background()
h, err := NewHost(ctx, swarmt.GenSwarm(t), &HostOpts{AddrsFactory: addrsFactory})
h, err := NewHost(swarmt.GenSwarm(t), &HostOpts{AddrsFactory: addrsFactory})
require.NoError(t, err)
defer h.Close()
@ -221,10 +215,8 @@ func TestHostAddrsFactory(t *testing.T) {
}
func TestLocalIPChangesWhenListenAddrChanges(t *testing.T) {
ctx := context.Background()
// no listen addrs
h, err := NewHost(ctx, swarmt.GenSwarm(t, swarmt.OptDialOnly), nil)
h, err := NewHost(swarmt.GenSwarm(t, swarmt.OptDialOnly), nil)
require.NoError(t, err)
h.Start()
defer h.Close()
@ -246,10 +238,8 @@ func TestLocalIPChangesWhenListenAddrChanges(t *testing.T) {
}
func TestAllAddrs(t *testing.T) {
ctx := context.Background()
// no listen addrs
h, err := NewHost(ctx, swarmt.GenSwarm(t, swarmt.OptDialOnly), nil)
h, err := NewHost(swarmt.GenSwarm(t, swarmt.OptDialOnly), nil)
require.NoError(t, err)
defer h.Close()
require.Nil(t, h.AllAddrs())
@ -274,19 +264,20 @@ func TestAllAddrs(t *testing.T) {
t.Fatal("expected addrs to contain original addr")
}
func getHostPair(ctx context.Context, t *testing.T) (host.Host, host.Host) {
func getHostPair(t *testing.T) (host.Host, host.Host) {
t.Helper()
h1, err := NewHost(ctx, swarmt.GenSwarm(t), nil)
h1, err := NewHost(swarmt.GenSwarm(t), nil)
require.NoError(t, err)
h2, err := NewHost(ctx, swarmt.GenSwarm(t), nil)
h2, err := NewHost(swarmt.GenSwarm(t), nil)
require.NoError(t, err)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
h2pi := h2.Peerstore().PeerInfo(h2.ID())
if err := h1.Connect(ctx, h2pi); err != nil {
t.Fatal(err)
}
return h1, h2
}
@ -306,7 +297,7 @@ func TestHostProtoPreference(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
h1, h2 := getHostPair(ctx, t)
h1, h2 := getHostPair(t)
defer h1.Close()
defer h2.Close()
@ -383,7 +374,7 @@ func TestHostProtoMismatch(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
h1, h2 := getHostPair(ctx, t)
h1, h2 := getHostPair(t)
defer h1.Close()
defer h2.Close()
@ -402,9 +393,9 @@ func TestHostProtoPreknowledge(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
h1, err := NewHost(ctx, swarmt.GenSwarm(t), nil)
h1, err := NewHost(swarmt.GenSwarm(t), nil)
require.NoError(t, err)
h2, err := NewHost(ctx, swarmt.GenSwarm(t), nil)
h2, err := NewHost(swarmt.GenSwarm(t), nil)
require.NoError(t, err)
conn := make(chan protocol.ID)
@ -466,7 +457,7 @@ func TestNewDialOld(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
h1, h2 := getHostPair(ctx, t)
h1, h2 := getHostPair(t)
defer h1.Close()
defer h2.Close()
@ -500,7 +491,7 @@ func TestProtoDowngrade(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
h1, h2 := getHostPair(ctx, t)
h1, h2 := getHostPair(t)
defer h1.Close()
defer h2.Close()
@ -613,7 +604,7 @@ func TestAddrResolution(t *testing.T) {
t.Fatal(err)
}
h, err := NewHost(ctx, swarmt.GenSwarm(t), &HostOpts{MultiaddrResolver: resolver})
h, err := NewHost(swarmt.GenSwarm(t), &HostOpts{MultiaddrResolver: resolver})
require.NoError(t, err)
defer h.Close()
@ -671,7 +662,7 @@ func TestAddrResolutionRecursive(t *testing.T) {
t.Fatal(err)
}
h, err := NewHost(ctx, swarmt.GenSwarm(t), &HostOpts{MultiaddrResolver: resolver})
h, err := NewHost(swarmt.GenSwarm(t), &HostOpts{MultiaddrResolver: resolver})
require.NoError(t, err)
defer h.Close()
@ -706,7 +697,7 @@ func TestAddrChangeImmediatelyIfAddressNonEmpty(t *testing.T) {
taddrs := []ma.Multiaddr{ma.StringCast("/ip4/1.2.3.4/tcp/1234")}
starting := make(chan struct{})
h, err := NewHost(ctx, swarmt.GenSwarm(t), &HostOpts{AddrsFactory: func(addrs []ma.Multiaddr) []ma.Multiaddr {
h, err := NewHost(swarmt.GenSwarm(t), &HostOpts{AddrsFactory: func(addrs []ma.Multiaddr) []ma.Multiaddr {
<-starting
return taddrs
}})
@ -746,8 +737,7 @@ func TestAddrChangeImmediatelyIfAddressNonEmpty(t *testing.T) {
}
func TestStatefulAddrEvents(t *testing.T) {
ctx := context.Background()
h, err := NewHost(ctx, swarmt.GenSwarm(t), nil)
h, err := NewHost(swarmt.GenSwarm(t), nil)
require.NoError(t, err)
h.Start()
defer h.Close()
@ -816,7 +806,7 @@ func TestHostAddrChangeDetection(t *testing.T) {
}
ctx := context.Background()
h, err := NewHost(ctx, swarmt.GenSwarm(t), &HostOpts{AddrsFactory: addrsFactory})
h, err := NewHost(swarmt.GenSwarm(t), &HostOpts{AddrsFactory: addrsFactory})
require.NoError(t, err)
h.Start()
defer h.Close()
@ -860,7 +850,7 @@ func TestNegotiationCancel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
h1, h2 := getHostPair(ctx, t)
h1, h2 := getHostPair(t)
defer h1.Close()
defer h2.Close()

26
p2p/host/relay/autorelay_test.go

@ -133,18 +133,16 @@ func TestAutoRelay(t *testing.T) {
// this is the relay host
// announce dns addrs because filter out private addresses from relays,
// and we consider dns addresses "public".
relayHost, err := libp2p.New(ctx,
libp2p.DisableRelay(),
libp2p.AddrsFactory(func(addrs []ma.Multiaddr) []ma.Multiaddr {
for i, addr := range addrs {
saddr := addr.String()
if strings.HasPrefix(saddr, "/ip4/127.0.0.1/") {
addrNoIP := strings.TrimPrefix(saddr, "/ip4/127.0.0.1")
addrs[i] = ma.StringCast("/dns4/localhost" + addrNoIP)
}
relayHost, err := libp2p.New(libp2p.DisableRelay(), libp2p.AddrsFactory(func(addrs []ma.Multiaddr) []ma.Multiaddr {
for i, addr := range addrs {
saddr := addr.String()
if strings.HasPrefix(saddr, "/ip4/127.0.0.1/") {
addrNoIP := strings.TrimPrefix(saddr, "/ip4/127.0.0.1")
addrs[i] = ma.StringCast("/dns4/localhost" + addrNoIP)
}
return addrs
}))
}
return addrs
}))
if err != nil {
t.Fatal(err)
}
@ -164,16 +162,16 @@ func TestAutoRelay(t *testing.T) {
relay.Advertise(ctx, relayDiscovery)
// the client hosts
h1, err := libp2p.New(ctx, libp2p.EnableRelay())
h1, err := libp2p.New(libp2p.EnableRelay())
if err != nil {
t.Fatal(err)
}
h2, err := libp2p.New(ctx, libp2p.EnableRelay(), libp2p.EnableAutoRelay(), libp2p.Routing(makePeerRouting))
h2, err := libp2p.New(libp2p.EnableRelay(), libp2p.EnableAutoRelay(), libp2p.Routing(makePeerRouting))
if err != nil {
t.Fatal(err)
}
h3, err := libp2p.New(ctx, libp2p.EnableRelay())
h3, err := libp2p.New(libp2p.EnableRelay())
if err != nil {
t.Fatal(err)
}

4
p2p/net/mock/mock_net.go

@ -19,7 +19,7 @@ import (
goprocessctx "github.com/jbenet/goprocess/context"
p2putil "github.com/libp2p/go-libp2p-netutil"
pstoremem "github.com/libp2p/go-libp2p-peerstore/pstoremem"
"github.com/libp2p/go-libp2p-peerstore/pstoremem"
ma "github.com/multiformats/go-multiaddr"
)
@ -111,7 +111,7 @@ func (mn *mocknet) AddPeerWithPeerstore(p peer.ID, ps peerstore.Peerstore) (host
DisableSignedPeerRecord: true,
}
h, err := bhost.NewHost(mn.ctx, n, opts)
h, err := bhost.NewHost(n, opts)
if err != nil {
return nil, err
}

18
p2p/protocol/holepunch/coordination_test.go

@ -297,13 +297,7 @@ func mkHostWithStaticAutoRelay(t *testing.T, ctx context.Context, relay host.Hos
manet.Private4 = []*net.IPNet{}
defer func() { manet.Private4 = cpy }()
h, err := libp2p.New(ctx,
libp2p.ListenAddrs(ma.StringCast("/ip4/127.0.0.1/tcp/0"), ma.StringCast("/ip6/::1/tcp/0")),
libp2p.EnableRelay(),
libp2p.EnableAutoRelay(),
libp2p.ForceReachabilityPrivate(),
libp2p.StaticRelays([]peer.AddrInfo{pi}),
)
h, err := libp2p.New(libp2p.ListenAddrs(ma.StringCast("/ip4/127.0.0.1/tcp/0"), ma.StringCast("/ip6/::1/tcp/0")), libp2p.EnableRelay(), libp2p.EnableAutoRelay(), libp2p.ForceReachabilityPrivate(), libp2p.StaticRelays([]peer.AddrInfo{pi}))
require.NoError(t, err)
// wait till we have a relay addr
@ -322,10 +316,7 @@ func makeRelayedHosts(t *testing.T, h1Opt holepunch.Option, addHolePuncher bool)
t.Helper()
h1, _ = mkHostWithHolePunchSvc(t, h1Opt)
var err error
relay, err = libp2p.New(context.Background(),
libp2p.ListenAddrs(ma.StringCast("/ip4/127.0.0.1/tcp/0"), ma.StringCast("/ip6/::1/tcp/0")),
libp2p.DisableRelay(),
)
relay, err = libp2p.New(libp2p.ListenAddrs(ma.StringCast("/ip4/127.0.0.1/tcp/0"), ma.StringCast("/ip6/::1/tcp/0")), libp2p.DisableRelay())
require.NoError(t, err)
_, err = circuit.NewRelay(context.Background(), relay, nil, circuit.OptHop)
@ -364,10 +355,7 @@ func addHolePunchService(t *testing.T, h host.Host) *holepunch.Service {
func mkHostWithHolePunchSvc(t *testing.T, opts ...holepunch.Option) (host.Host, *holepunch.Service) {
t.Helper()
h, err := libp2p.New(
context.Background(),
libp2p.ListenAddrs(ma.StringCast("/ip4/127.0.0.1/tcp/0"), ma.StringCast("/ip6/::1/tcp/0")),
)
h, err := libp2p.New(libp2p.ListenAddrs(ma.StringCast("/ip4/127.0.0.1/tcp/0"), ma.StringCast("/ip6/::1/tcp/0")))
require.NoError(t, err)
ids, err := identify.NewIDService(h)
require.NoError(t, err)

22
p2p/protocol/identify/id_test.go

@ -677,21 +677,13 @@ func TestUserAgent(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
h1, err := libp2p.New(
ctx,
libp2p.UserAgent("foo"),
libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"),
)
h1, err := libp2p.New(libp2p.UserAgent("foo"), libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))
if err != nil {
t.Fatal(err)
}
defer h1.Close()
h2, err := libp2p.New(
ctx,
libp2p.UserAgent("bar"),
libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"),
)
h2, err := libp2p.New(libp2p.UserAgent("bar"), libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))
if err != nil {
t.Fatal(err)
}
@ -717,19 +709,13 @@ func TestNotListening(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
h1, err := libp2p.New(
ctx,
libp2p.NoListenAddrs,
)
h1, err := libp2p.New(libp2p.NoListenAddrs)
if err != nil {
t.Fatal(err)
}
defer h1.Close()
h2, err := libp2p.New(
ctx,
libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"),
)
h2, err := libp2p.New(libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))
if err != nil {
t.Fatal(err)
}

4
p2p/protocol/ping/ping_test.go

@ -16,9 +16,9 @@ import (
func TestPing(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
h1, err := bhost.NewHost(ctx, swarmt.GenSwarm(t), nil)
h1, err := bhost.NewHost(swarmt.GenSwarm(t), nil)
require.NoError(t, err)
h2, err := bhost.NewHost(ctx, swarmt.GenSwarm(t), nil)
h2, err := bhost.NewHost(swarmt.GenSwarm(t), nil)
require.NoError(t, err)
err = h1.Connect(ctx, peer.AddrInfo{

4
p2p/test/backpressure/backpressure_test.go

@ -23,9 +23,9 @@ func TestStBackpressureStreamWrite(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
h1, err := bhost.NewHost(ctx, swarmt.GenSwarm(t), nil)
h1, err := bhost.NewHost(swarmt.GenSwarm(t), nil)
require.NoError(t, err)
h2, err := bhost.NewHost(ctx, swarmt.GenSwarm(t), nil)
h2, err := bhost.NewHost(swarmt.GenSwarm(t), nil)
require.NoError(t, err)
// setup sender handler on 1

8
p2p/test/reconnects/reconnect_test.go

@ -102,10 +102,9 @@ func newSender() (chan sendChans, func(s network.Stream)) {
// TestReconnect tests whether hosts are able to disconnect and reconnect.
func TestReconnect2(t *testing.T) {
ctx := context.Background()
h1, err := bhost.NewHost(ctx, swarmt.GenSwarm(t), nil)
h1, err := bhost.NewHost(swarmt.GenSwarm(t), nil)
require.NoError(t, err)
h2, err := bhost.NewHost(ctx, swarmt.GenSwarm(t), nil)
h2, err := bhost.NewHost(swarmt.GenSwarm(t), nil)
require.NoError(t, err)
hosts := []host.Host{h1, h2}
@ -125,10 +124,9 @@ func TestReconnect2(t *testing.T) {
// TestReconnect tests whether hosts are able to disconnect and reconnect.
func TestReconnect5(t *testing.T) {
const num = 5
ctx := context.Background()
hosts := make([]host.Host, 0, num)
for i := 0; i < num; i++ {
h, err := bhost.NewHost(ctx, swarmt.GenSwarm(t), nil)
h, err := bhost.NewHost(swarmt.GenSwarm(t), nil)
require.NoError(t, err)
h.SetStreamHandler(protocol.TestingID, EchoStreamHandler)
hosts = append(hosts, h)

Loading…
Cancel
Save