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 18, 2021
1 parent 29da01a commit b7bee38
Show file tree
Hide file tree
Showing 37 changed files with 128 additions and 234 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
2 changes: 1 addition & 1 deletion examples/chat-with-mdns/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 0 additions & 2 deletions examples/chat-with-mdns/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 1 addition & 3 deletions examples/chat-with-rendezvous/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
11 changes: 4 additions & 7 deletions examples/chat-with-rendezvous/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions examples/chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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),
)
Expand Down
4 changes: 2 additions & 2 deletions examples/chat/chat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/echo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion examples/http-proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
7 changes: 1 addition & 6 deletions examples/ipfs-camp-2019/01-Transports/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
Expand Down
9 changes: 2 additions & 7 deletions examples/ipfs-camp-2019/02-Multiaddrs/main.go
Original file line number Diff line number Diff line change
@@ -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),
Expand All @@ -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)
}
Expand Down
13 changes: 6 additions & 7 deletions examples/ipfs-camp-2019/03-Muxing-Encryption/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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)
Expand All @@ -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 change: 0 additions & 1 deletion examples/ipfs-camp-2019/05-Discovery/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion examples/ipfs-camp-2019/06-Pubsub/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ func main() {
routing := libp2p.Routing(newDHT)

host, err := libp2p.New(
ctx,
transports,
listenAddrs,
muxers,
Expand Down
1 change: 0 additions & 1 deletion examples/ipfs-camp-2019/07-Messaging/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ func main() {
routing := libp2p.Routing(newDHT)

host, err := libp2p.New(
ctx,
transports,
listenAddrs,
muxers,
Expand Down
1 change: 0 additions & 1 deletion examples/ipfs-camp-2019/08-End/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ func main() {
routing := libp2p.Routing(newDHT)

host, err := libp2p.New(
ctx,
transports,
listenAddrs,
muxers,
Expand Down
12 changes: 4 additions & 8 deletions examples/libp2p-host/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,19 @@ If you want to create a host with a default configuration, you can do the follow

```go
import (
"context"
"crypto/rand"
"fmt"

"github.com/libp2p/go-libp2p"
"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())
```
Expand All @@ -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
Expand Down Expand Up @@ -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())
```
Expand Down
6 changes: 4 additions & 2 deletions examples/libp2p-host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit b7bee38

Please sign in to comment.