Skip to content

Latest commit

 

History

History
141 lines (121 loc) · 4.82 KB

v0.39-v0.40.md

File metadata and controls

141 lines (121 loc) · 4.82 KB

Migrating to libp2p@40

A migration guide for refactoring your application code from libp2p v0.39.x to v0.40.0.

Table of Contents

Modules

Modules in the libp2p ecosystem no longer export constructors. This is because internally libp2p has switched to using constructor dependency injection so needs to control the lifecycle of the various components.

These modules now export factory functions that take the same arguments as the older constructors so migration should be relatively straightforward.

The most affected place will be where you configure your libp2p node:

Before

import { createLibp2p } from 'libp2p'
import { TCP } from '@libp2p/tcp'
import { WebSockets } from '@libp2p/websockets'
import { WebRTCStar } from '@libp2p/webrtc-star'
import { WebRTCDirect } from '@libp2p/webrtc-direct'
import { KadDHT } from '@libp2p/kad-dht'
import Gossipsub from '@chainsafe/libp2p-gossipsub'
import { Bootstrap } from '@libp2p/bootstrap'
import { MulticastDNS } from '@libp2p/mdns'
import { Noise } from '@chainsafe/libp2p-noise'
import { Yamux } from '@chainsafe/libp2p-yamux'
import { Mplex } from '@libp2p/mplex'
import { DelegatedContentRouting } from '@libp2p/delegated-content-routing'
import { DelegatedPeerRouting } from '@libp2p/delegated-peer-routing'
import { create as createIpfsHttpClient } from 'ipfs-http-client'

  const star = new WebRTCStar()
  const ipfsHttpClinet = createIpfsHttpClient(new URL('https://node0.delegate.ipfs.io'))

const node = await createLibp2p({
  addresses: {
    listen: [
      '/ip4/0.0.0.0/tcp/0'
    ]
  },
  transports: [
    new TCP(),
    new WebSockets(),
    new WebRTCDirect(),
    star.transport
  ],
  peerDiscovery: [
    new Bootstrap({ list: [ ... ] }),
    new MulticastDNS(),
    star.discovery
  ],
  connectionEncryption: [
    new Noise()
  ],
  streamMuxers: [
    new Yamux(),
    new Mplex()
  ],
  contentRouting: [
    new DelegatedContentRouting(ipfsHttpClinet)
  ],
  peerRouting: [
    new DelegatedPeerRouting(ipfsHttpClinet)
  ],
  dht: new KadDHT(),
  pubsub: new Gossipsub()
})

await node.start()
// ...

After

import { createLibp2p } from 'libp2p'
import { tcp } from '@libp2p/tcp'
import { webSockets } from '@libp2p/websockets'
import { webRTCStar } from '@libp2p/webrtc-star'
import { webRTCDirect } from '@libp2p/webrtc-direct'
import { kadDHT } from '@libp2p/kad-dht'
import { gossipsub } from '@chainsafe/libp2p-gossipsub'
import { bootstrap } from '@libp2p/bootstrap'
import { mdns } from '@libp2p/mdns'
import { noise } from '@chainsafe/libp2p-noise'
import { yamux } from '@chainsafe/libp2p-yamux'
import { mplex } from '@libp2p/mplex'
import { delegatedContentRouting } from '@libp2p/delegated-content-routing'
import { delegatedPeerRouting } from '@libp2p/delegated-peer-routing'
import { create as createIpfsHttpClient } from 'ipfs-http-client'

const star = webRTCStar()
const ipfsHttpClinet = createIpfsHttpClient(new URL('https://node0.delegate.ipfs.io'))

const node = await createLibp2p({
  addresses: {
    listen: [
      '/ip4/0.0.0.0/tcp/0'
    ]
  },
  transports: [
    tcp(),
    webSockets(),
    webRTCDirect(),
    star
  ],
  peerDiscovery: [
    bootstrap({ list: [ ... ] }),
    mdns(),
    star.discovery
  ],
  connectionEncryption: [
    noise()
  ],
  streamMuxers: [
    yamux(),
    mplex()
  ],
  contentRouting: [
    delegatedContentRouting(ipfsHttpClinet)
  ],
  peerRouting: [
    delegatedPeerRouting(ipfsHttpClinet)
  ],
  dht: kadDHT(),
  pubsub: gossipsub()
})

await node.start()

Autodial

In versions of libp2p prior to 0.40.x, when the peer discovery subsystem announced a new peer, that peer would automatically be dialed and a connection held open, subject to the normal rules on closing connections. Prior to having a working DHT this was necessary to increase the chances that you would be connected to a peer that could service any network request you might make. The downside to this is that connections are expensive so nodes displayed high CPU usage even when idle. Now that the DHT is in place this is no longer necessary as we can discover nearby peers using that mechanism.

When dialing a remote peer the identify protocol is run, during which we find out which protocols the remote peer supports. This in turn affects the nodes added to topologies for a given protocol. Topologies may see fewer nodes added to them as we are now dialing and running identify on fewer peers, though the peers that do get added are likely to be higher value - their PeerIds will be KAD-closer to ours, they would have been dialed directly rather than being random peers on the network, etc.