-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix!: remove @libp2p/components (#1427)
`@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
1 parent
b717beb
commit a3847f2
Showing
103 changed files
with
1,998 additions
and
1,312 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.