From 5fe719e885e23dd89139cd66f908fc39ffa5d03e Mon Sep 17 00:00:00 2001 From: Marco Munizaga Date: Mon, 3 Jun 2024 11:40:12 -0700 Subject: [PATCH] Remove out-dated code in example readme (#2818) --- examples/libp2p-host/README.md | 80 +--------------------------------- examples/libp2p-host/host.go | 7 +-- 2 files changed, 3 insertions(+), 84 deletions(-) diff --git a/examples/libp2p-host/README.md b/examples/libp2p-host/README.md index 966ee0e1fd..6de534cff6 100644 --- a/examples/libp2p-host/README.md +++ b/examples/libp2p-host/README.md @@ -4,87 +4,11 @@ For most applications, the host is the basic building block you'll need to get s The host is an abstraction that manages services on top of a swarm. It provides a clean interface to connect to a service on a given remote peer. -If you want to create a host with a default configuration, you can do the following: - -```go -import ( - "crypto/rand" - "fmt" - - "github.com/libp2p/go-libp2p" - "github.com/libp2p/go-libp2p-core/crypto" -) - -// To construct a simple host with all the default settings, just use `New` -h, err := libp2p.New() -if err != nil { - panic(err) -} -defer h.Close() - -fmt.Printf("Hello World, my hosts ID is %s\n", h.ID()) -``` +If you want to create a host with a default configuration refer to the example in `./host.go` If you want more control over the configuration, you can specify some options to the constructor. For a full list of all the configuration supported by the constructor [see the different options in the docs](https://godoc.org/github.com/libp2p/go-libp2p). -In this snippet we set a number of useful options like a custom ID and enable routing. This will improve discoverability and reachability of the peer on NAT'ed environments: - -```go -// Set your own keypair -priv, _, err := crypto.GenerateKeyPair( - crypto.Ed25519, // Select your key type. Ed25519 are nice short - -1, // Select key length when possible (i.e. RSA). -) -if err != nil { - panic(err) -} - -var idht *dht.IpfsDHT - -h2, err := libp2p.New( - // Use the keypair we generated - libp2p.Identity(priv), - // Multiple listen addresses - libp2p.ListenAddrStrings( - "/ip4/0.0.0.0/tcp/9000", // regular tcp connections - "/ip4/0.0.0.0/udp/9000/quic", // a UDP endpoint for the QUIC transport - ), - // support TLS connections - libp2p.Security(libp2ptls.ID, libp2ptls.New), - // support Noise connections - libp2p.Security(noise.ID, noise.New), - // support QUIC - libp2p.Transport(libp2pquic.NewTransport), - // support any other default transports (TCP) - libp2p.DefaultTransports, - // Let's prevent our peer from having too many - // connections by attaching a connection manager. - libp2p.ConnectionManager(connmgr.NewConnManager( - 100, // Lowwater - 400, // HighWater, - time.Minute, // GracePeriod - )), - // Attempt to open ports using uPNP for NATed hosts. - libp2p.NATPortMap(), - // Let this host use the DHT to find other hosts - libp2p.Routing(func(h host.Host) (routing.PeerRouting, error) { - idht, err = dht.New(ctx, h) - return idht, err - }), - // Let this host use relays and advertise itself on relays if - // it finds it is behind NAT. Use libp2p.Relay(options...) to - // enable active relays and more. - libp2p.EnableAutoRelay(), -) -if err != nil { - panic(err) -} -defer h2.Close() - -fmt.Printf("Hello World, my second hosts ID is %s\n", h2.ID()) -``` - -And that's it, you have a libp2p host and you're ready to start doing some awesome p2p networking! +In `./host.go` we set a number of useful options like a custom ID and enable routing. This will improve discoverability and reachability of the peer on NAT'ed environments. In future guides we will go over ways to use hosts, configure them differently (hint: there are a huge number of ways to set these up), and interesting ways to apply this technology to various applications you might want to build. diff --git a/examples/libp2p-host/host.go b/examples/libp2p-host/host.go index b1b6790aa1..0d90370376 100644 --- a/examples/libp2p-host/host.go +++ b/examples/libp2p-host/host.go @@ -21,11 +21,6 @@ func main() { } func run() { - // The context governs the lifetime of the libp2p node. - // Cancelling it will stop the host. - 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() if err != nil { @@ -79,7 +74,7 @@ func run() { libp2p.NATPortMap(), // Let this host use the DHT to find other hosts libp2p.Routing(func(h host.Host) (routing.PeerRouting, error) { - idht, err = dht.New(ctx, h) + idht, err = dht.New(context.Background(), h) return idht, err }), // If you want to help other peers to figure out if they are behind