-
Notifications
You must be signed in to change notification settings - Fork 448
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feat/add-event-bus' into arbitrary-modules
- Loading branch information
Showing
86 changed files
with
1,341 additions
and
1,008 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,170 @@ | ||
# Migrating to libp2p@45 <!-- omit in toc --> | ||
|
||
A migration guide for refactoring your application code from libp2p v0.44.x to v0.45.0. | ||
|
||
## Table of Contents <!-- omit in toc --> | ||
|
||
- [Events](#events) | ||
- [Emitters](#emitters) | ||
- [Event changes](#event-changes) | ||
- [`peer:connect`](#peerconnect) | ||
- [`peer:disconnect`](#peerdisconnect) | ||
- [`peer:update`](#peerupdate) | ||
- [`self:peer:update`](#selfpeerupdate) | ||
- [Atomic peer store methods](#atomic-peer-store-methods) | ||
|
||
## Events | ||
|
||
The events emitted by libp2p have been refactored to be more consistent and to give more insight into the inner workings of libp2p. | ||
|
||
> Please see the [API docs](https://libp2p.github.io/js-libp2p-interfaces/interfaces/_libp2p_interface_libp2p.Libp2pEvents.html) for an exhaustive list of events emitted by Libp2p. | ||
### Emitters | ||
|
||
The primary interaction point for events is now the libp2p node itself, no need to access internal properties to set up listeners. | ||
|
||
**Before** | ||
|
||
```js | ||
import { createLibp2p } from 'libp2p' | ||
|
||
const node = await createLibp2p({ /* ... */ }) | ||
node.connectionManager.addEventListener('peer:connect', () => {}) | ||
``` | ||
|
||
**After** | ||
|
||
```js | ||
import { createLibp2p } from 'libp2p' | ||
|
||
const node = await createLibp2p({ /* ... */ }) | ||
node.addEventListener('peer:connect', () => {}) | ||
``` | ||
|
||
### Event changes | ||
|
||
Some types have changed. | ||
|
||
> Please see the [API docs](https://libp2p.github.io/js-libp2p-interfaces/interfaces/_libp2p_interface_libp2p.Libp2pEvents.html) for an exhaustive list of events emitted by Libp2p. | ||
#### `peer:connect` | ||
|
||
The detail field for this event was a [Connection] now it is a [PeerId] | ||
|
||
It is emitted when a new peer opens it's first connection. | ||
|
||
To receive notifications of the opening of individual connections, listen for the `connection:open` event instead. | ||
|
||
#### `peer:disconnect` | ||
|
||
The detail field for this event was a [Connection] now it is a [PeerId] | ||
|
||
It is emitted when all connections for the peer have been closed. | ||
|
||
To receive notifications of the closing of individual connections, listen for the `connection:close` event instead. | ||
|
||
#### `peer:update` | ||
|
||
This event is emitted when a peer's data has been changed in the peer store. This can be in response to a user manually updating the peer, or after the [Identify] protocol has completed. | ||
|
||
#### `self:peer:update` | ||
|
||
This event occurs when the data of the running node has changed. It may have started listening on a new multiaddr, [AutoNAT] may have given us new confidence in an external address or a user may have manually updated the information. | ||
|
||
## Atomic peer store methods | ||
|
||
The libp2p peer store has been refactored to reduce the number of methods it exposes. | ||
|
||
Previously it had separate components for managing addresses, protocols, metadata, etc, all of which exposed async methods which meant updating the data for a peer could involve multiple async calls which required complicated internal locking mechanisms which introduced a lot of latency into libp2p nodes performing many peer operations. | ||
|
||
The updated peer store has simple `save`, `patch` and `merge` methods which update all fields in a peer's stored data at once. | ||
|
||
**Before** | ||
|
||
```js | ||
import { createLibp2p } from 'libp2p' | ||
|
||
const node = await createLibp2p({ /* ... */ }) | ||
|
||
// add addresses | ||
await node.peerStore.addressBook.add(peerId, [ | ||
multiaddr('/ip4/43.14.67.21/tcp/3847') | ||
]) | ||
|
||
// set protocols | ||
await node.peerStore.protoBook.set(peerId, [ | ||
'/a-proto/1.0.0', | ||
'/another-proto/1.0.0' | ||
]) | ||
|
||
// add metadata | ||
await node.peerStore.metadataBook.set(peerId, 'key', Uint8Array.from([0, 1, 2, 3])) | ||
|
||
// add tags | ||
await node.peerStore.tagPeer(peerId, 'tag-name', { value: 10 }) | ||
``` | ||
|
||
**After** | ||
|
||
```js | ||
import { createLibp2p } from 'libp2p' | ||
|
||
const node = await createLibp2p({ /* ... */ }) | ||
|
||
// `save` replaces all data for the peer. Use with caution - any fields not passed | ||
// will be deleted | ||
await node.peerStore.save(peerId, { | ||
multiaddrs: [ | ||
multiaddr('/ip4/43.14.67.21/tcp/3847') | ||
], | ||
protocols: [ | ||
'/a-proto/1.0.0', | ||
'/another-proto/1.0.0' | ||
], | ||
metadata: { | ||
key: Uint8Array.from([0, 1, 2, 3]) | ||
}, | ||
tags: { | ||
'tag-name': { value: 10 } | ||
} | ||
}) | ||
``` | ||
|
||
Other ways to update peers are available which are more concise and allow you to just update specific fields: | ||
|
||
```js | ||
// `patch` replaces only the passed fields and retains all other data | ||
await node.peerStore.patch(peerId, { | ||
multiaddrs: [ | ||
multiaddr('/ip4/43.14.67.21/tcp/3847') | ||
] | ||
}) | ||
|
||
// `merge` behaves like `patch` but deeply merges multiaddrs, protocols, metadata, | ||
// and tags, removing duplicates. any existing metadata/tags with the same | ||
// keys/tag names will be overwritten. | ||
await node.peerStore.merge(peerId, { | ||
multiaddrs: [ | ||
multiaddr('/ip4/43.14.67.21/tcp/3847') | ||
] | ||
}) | ||
``` | ||
|
||
You can also remove fields quickly: | ||
|
||
```js | ||
// passing `undefined` to `merge` is a quick way of deleting metadata/tags | ||
await node.peerStore.merge(peerId, { | ||
metadata: { | ||
key: undefined | ||
}, | ||
tags: { | ||
'tag-name': undefined | ||
} | ||
}) | ||
``` | ||
|
||
[Connection]: https://libp2p.github.io/js-libp2p-interfaces/interfaces/_libp2p_interface_connection.Connection.html | ||
[PeerId]: https://libp2p.github.io/js-libp2p-interfaces/types/_libp2p_interface_peer_id.PeerId.html | ||
[Identify]: https://github.com/libp2p/specs/blob/master/identify/README.md | ||
[AutoNAT]: https://github.com/libp2p/specs/blob/master/autonat/README.md |
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
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
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.