Skip to content

Commit

Permalink
remove the context from the libp2p and from the Host contructor
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Sep 17, 2021
1 parent 29da01a commit 1af3880
Show file tree
Hide file tree
Showing 15 changed files with 93 additions and 172 deletions.
11 changes: 5 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package config

import (
"context"
"crypto/rand"
"fmt"
"time"
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions config/muxer_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package config

import (
"context"
"testing"

"github.com/libp2p/go-libp2p-core/host"
Expand Down Expand Up @@ -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)
}
Expand Down
12 changes: 5 additions & 7 deletions libp2p.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down Expand Up @@ -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
Expand All @@ -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 changes: 23 additions & 56 deletions libp2p_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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")
Expand All @@ -42,27 +41,23 @@ func TestBadTransportConstructor(t *testing.T) {
}

func TestTransportConstructor(t *testing.T) {
ctx := context.Background()
ctor := func(
h host.Host,
_ connmgr.ConnectionGater,
upgrader *tptu.Upgrader,
) transport.Transport {
return tcp.NewTCPTransport(upgrader)
}
h, err := New(ctx, Transport(ctor))
h, err := New(Transport(ctor))
if err != nil {
t.Fatal(err)
}
h.Close()
}

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")
Expand All @@ -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{
Expand All @@ -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 {
Expand All @@ -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")
Expand All @@ -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) {
Expand Down Expand Up @@ -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()

Expand Down
3 changes: 1 addition & 2 deletions p2p/discovery/mdns/mdns_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package mdns

import (
"context"
"sync"
"testing"
"time"
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions p2p/discovery/mdns_legacy/mdns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions p2p/host/basic/basic_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
}
Expand Down
Loading

0 comments on commit 1af3880

Please sign in to comment.