Skip to content

Commit

Permalink
fix!: remove @libp2p/components (#1427)
Browse files Browse the repository at this point in the history
`@libp2p/components` is a choke-point for our dependency graph as it depends on every interface, meaning when one interface revs a major `@libp2p/components` major has to change too which means every module depending on it also needs a major.

Switch instead to constructor injection of simple objects that let modules declare their dependencies on interfaces directly instead of indirectly via `@libp2p/components`

Fixes libp2p/js-libp2p-components#6

BREAKING CHANGE: modules no longer implement `Initializable` instead switching to constructor injection
  • Loading branch information
achingbrain committed Oct 14, 2022
1 parent b717beb commit a3847f2
Show file tree
Hide file tree
Showing 103 changed files with 1,998 additions and 1,312 deletions.
14 changes: 7 additions & 7 deletions .aegir.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { WebSockets } from '@libp2p/websockets'
import { Mplex } from '@libp2p/mplex'
import { webSockets } from '@libp2p/websockets'
import { mplex } from '@libp2p/mplex'
import { Noise } from '@chainsafe/libp2p-noise'
import { pipe } from 'it-pipe'
import { createFromJSON } from '@libp2p/peer-id-factory'
Expand All @@ -14,7 +14,7 @@ export default {
// use dynamic import because we only want to reference these files during the test run, e.g. after building
const { createLibp2p } = await import('./dist/src/index.js')
const { MULTIADDRS_WEBSOCKETS } = await import('./dist/test/fixtures/browser.js')
const { Plaintext } = await import('./dist/src/insecure/index.js')
const { plaintext } = await import('./dist/src/insecure/index.js')
const { default: Peers } = await import('./dist/test/fixtures/peers.js')

// Use the last peer
Expand All @@ -28,14 +28,14 @@ export default {
},
peerId,
transports: [
new WebSockets()
webSockets()
],
streamMuxers: [
new Mplex()
mplex()
],
connectionEncryption: [
new Noise(),
new Plaintext()
() => () => new Noise(),
plaintext()
],
relay: {
enabled: true,
Expand Down
141 changes: 141 additions & 0 deletions doc/migrations/v0.37-v0.40.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# Migrating to libp2p@40 <!-- omit in toc -->

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

## Table of Contents <!-- omit in toc -->

- [Modules](#modules)
- [Autodial](#autodial)

## 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**

```js
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
],
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**

```js
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](https://github.com/libp2p/js-libp2p/blob/master/doc/LIMITS.md#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](https://docs.libp2p.io/concepts/protocols/#identify) is run, during which we find out which protocols the remote peer supports. This in turn affects the nodes added to [topologies](https://github.com/libp2p/js-libp2p-topology#readme) 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.
10 changes: 5 additions & 5 deletions examples/auto-relay/dialer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createLibp2p } from 'libp2p'
import { WebSockets } from '@libp2p/websockets'
import { webSockets } from '@libp2p/websockets'
import { Noise } from '@chainsafe/libp2p-noise'
import { Mplex } from '@libp2p/mplex'
import { mplex } from '@libp2p/mplex'

async function main () {
const autoRelayNodeAddr = process.argv[2]
Expand All @@ -11,13 +11,13 @@ async function main () {

const node = await createLibp2p({
transports: [
new WebSockets()
webSockets()
],
connectionEncryption: [
new Noise()
() => new Noise()
],
streamMuxers: [
new Mplex()
mplex()
]
})

Expand Down
10 changes: 5 additions & 5 deletions examples/auto-relay/listener.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createLibp2p } from 'libp2p'
import { WebSockets } from '@libp2p/websockets'
import { webSockets } from '@libp2p/websockets'
import { Noise } from '@chainsafe/libp2p-noise'
import { Mplex } from '@libp2p/mplex'
import { mplex } from '@libp2p/mplex'

async function main () {
const relayAddr = process.argv[2]
Expand All @@ -11,13 +11,13 @@ async function main () {

const node = await createLibp2p({
transports: [
new WebSockets()
webSockets()
],
connectionEncryption: [
new Noise()
() => new Noise()
],
streamMuxers: [
new Mplex()
mplex()
],
relay: {
enabled: true,
Expand Down
10 changes: 5 additions & 5 deletions examples/auto-relay/relay.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createLibp2p } from 'libp2p'
import { WebSockets } from '@libp2p/websockets'
import { webSockets } from '@libp2p/websockets'
import { Noise } from '@chainsafe/libp2p-noise'
import { Mplex } from '@libp2p/mplex'
import { mplex } from '@libp2p/mplex'

async function main () {
const node = await createLibp2p({
Expand All @@ -11,13 +11,13 @@ async function main () {
// announce: ['/dns4/auto-relay.libp2p.io/tcp/443/wss/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3']
},
transports: [
new WebSockets()
webSockets()
],
connectionEncryption: [
new Noise()
() => new Noise()
],
streamMuxers: [
new Mplex()
mplex()
],
relay: {
enabled: true,
Expand Down
14 changes: 7 additions & 7 deletions examples/chat/src/libp2p.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { TCP } from '@libp2p/tcp'
import { WebSockets } from '@libp2p/websockets'
import { Mplex } from '@libp2p/mplex'
import { tcp } from '@libp2p/tcp'
import { webSockets } from '@libp2p/websockets'
import { mplex } from '@libp2p/mplex'
import { Noise } from '@chainsafe/libp2p-noise'
import defaultsDeep from '@nodeutils/defaults-deep'
import { createLibp2p as create } from 'libp2p'

export async function createLibp2p(_options) {
const defaults = {
transports: [
new TCP(),
new WebSockets()
tcp(),
webSockets()
],
streamMuxers: [
new Mplex()
mplex()
],
connectionEncryption: [
new Noise()
() => new Noise()
]
}

Expand Down
10 changes: 5 additions & 5 deletions examples/connection-encryption/1.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createLibp2p } from '../../dist/src/index.js'
import { TCP } from '@libp2p/tcp'
import { Mplex } from '@libp2p/mplex'
import { tcp } from '@libp2p/tcp'
import { mplex } from '@libp2p/mplex'
import { Noise } from '@chainsafe/libp2p-noise'
import { pipe } from 'it-pipe'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
Expand All @@ -11,9 +11,9 @@ const createNode = async () => {
addresses: {
listen: ['/ip4/0.0.0.0/tcp/0']
},
transports: [new TCP()],
streamMuxers: [new Mplex()],
connectionEncryption: [new Noise()]
transports: [tcp()],
streamMuxers: [mplex()],
connectionEncryption: [() => new Noise()]
})

await node.start()
Expand Down
13 changes: 7 additions & 6 deletions examples/delegated-routing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
"dependencies": {
"@chainsafe/libp2p-noise": "^9.0.0",
"ipfs-core": "^0.15.4",
"ipfs-http-client": "^58.0.1",
"libp2p": "../../",
"@libp2p/delegated-content-routing": "^2.0.2",
"@libp2p/delegated-peer-routing": "^2.0.2",
"@libp2p/kad-dht": "^4.0.0",
"@libp2p/mplex": "^6.0.2",
"@libp2p/webrtc-star": "^4.0.1",
"@libp2p/websockets": "^4.0.0",
"@libp2p/delegated-content-routing": "^3.0.0",
"@libp2p/delegated-peer-routing": "^3.0.0",
"@libp2p/kad-dht": "^5.0.1",
"@libp2p/mplex": "^7.0.0",
"@libp2p/webrtc-star": "^5.0.2",
"@libp2p/websockets": "^5.0.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "5.0.0"
Expand Down
32 changes: 18 additions & 14 deletions examples/delegated-routing/src/libp2p-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@
'use strict'

import { createLibp2p } from 'libp2p'
import { WebSockets } from '@libp2p/websockets'
import { WebRTCStar } from '@libp2p/webrtc-star'
import { Mplex } from '@libp2p/mplex'
import { webSockets } from '@libp2p/websockets'
import { webRTCStar } from '@libp2p/webrtc-star'
import { mplex } from '@libp2p/mplex'
import { Noise } from '@chainsafe/libp2p-noise'
import { DelegatedPeerRouting } from '@libp2p/delegated-peer-routing'
import { DelegatedContentRouting } from '@libp2p/delegated-content-routing'
import { delegatedPeerRouting } from '@libp2p/delegated-peer-routing'
import { delegatedContentRouting } from '@libp2p/delegated-content-routing'
import { create as createIpfsHttpClient } from 'ipfs-http-client'

export default function Libp2pBundle ({peerInfo, peerBook}) {
const wrtcstar = new WebRTCStar()
const delegatedApiOptions = {
const wrtcstar = new webRTCStar()
const client = createIpfsHttpClient({
host: '0.0.0.0',
protocol: 'http',
port: '8080'
}
})

return createLibp2p({
peerInfo,
Expand All @@ -26,20 +27,23 @@ export default function Libp2pBundle ({peerInfo, peerBook}) {
pollInterval: 5000
},
contentRouting: [
new DelegatedPeerRouting(peerInfo.id, delegatedApiOptions)
delegatedPeerRouting(client)
],
peerRouting: [
new DelegatedContentRouting(delegatedApiOptions)
delegatedContentRouting(client)
],
transports: [
wrtcstar,
new WebSockets()
wrtcstar.transport,
webSockets()
],
streamMuxers: [
new Mplex()
mplex()
],
peerDiscovery: [
wrtcstar.discovery
],
connectionEncryption: [
new Noise()
() => new Noise()
],
connectionManager: {
autoDial: false
Expand Down

0 comments on commit a3847f2

Please sign in to comment.