|
| 1 | +# Migrating to libp2p@45 <!-- omit in toc --> |
| 2 | + |
| 3 | +A migration guide for refactoring your application code from libp2p v0.44.x to v0.45.0. |
| 4 | + |
| 5 | +## Table of Contents <!-- omit in toc --> |
| 6 | + |
| 7 | +- [Events](#events) |
| 8 | + - [Emitters](#emitters) |
| 9 | + - [Event changes](#event-changes) |
| 10 | + - [`peer:connect`](#peerconnect) |
| 11 | + - [`peer:disconnect`](#peerdisconnect) |
| 12 | + - [`peer:update`](#peerupdate) |
| 13 | + - [`self:peer:update`](#selfpeerupdate) |
| 14 | +- [Atomic peer store methods](#atomic-peer-store-methods) |
| 15 | + |
| 16 | +## Events |
| 17 | + |
| 18 | +The events emitted by libp2p have been refactored to be more consistent and to give more insight into the inner workings of libp2p. |
| 19 | + |
| 20 | +> 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. |
| 21 | +
|
| 22 | +### Emitters |
| 23 | + |
| 24 | +The primary interaction point for events is now the libp2p node itself, no need to access internal properties to set up listeners. |
| 25 | + |
| 26 | +**Before** |
| 27 | + |
| 28 | +```js |
| 29 | +import { createLibp2p } from 'libp2p' |
| 30 | + |
| 31 | +const node = await createLibp2p({ /* ... */ }) |
| 32 | +node.connectionManager.addEventListener('peer:connect', () => {}) |
| 33 | +``` |
| 34 | + |
| 35 | +**After** |
| 36 | + |
| 37 | +```js |
| 38 | +import { createLibp2p } from 'libp2p' |
| 39 | + |
| 40 | +const node = await createLibp2p({ /* ... */ }) |
| 41 | +node.addEventListener('peer:connect', () => {}) |
| 42 | +``` |
| 43 | + |
| 44 | +### Event changes |
| 45 | + |
| 46 | +Some types have changed. |
| 47 | + |
| 48 | +> 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. |
| 49 | +
|
| 50 | +#### `peer:connect` |
| 51 | + |
| 52 | +The detail field for this event was a [Connection] now it is a [PeerId] |
| 53 | + |
| 54 | +It is emitted when a new peer opens it's first connection. |
| 55 | + |
| 56 | +To receive notifications of the opening of individual connections, listen for the `connection:open` event instead. |
| 57 | + |
| 58 | +#### `peer:disconnect` |
| 59 | + |
| 60 | +The detail field for this event was a [Connection] now it is a [PeerId] |
| 61 | + |
| 62 | +It is emitted when all connections for the peer have been closed. |
| 63 | + |
| 64 | +To receive notifications of the closing of individual connections, listen for the `connection:close` event instead. |
| 65 | + |
| 66 | +#### `peer:update` |
| 67 | + |
| 68 | +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. |
| 69 | + |
| 70 | +#### `self:peer:update` |
| 71 | + |
| 72 | +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. |
| 73 | + |
| 74 | +## Atomic peer store methods |
| 75 | + |
| 76 | +The libp2p peer store has been refactored to reduce the number of methods it exposes. |
| 77 | + |
| 78 | +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. |
| 79 | + |
| 80 | +The updated peer store has simple `save`, `patch` and `merge` methods which update all fields in a peer's stored data at once. |
| 81 | + |
| 82 | +**Before** |
| 83 | + |
| 84 | +```js |
| 85 | +import { createLibp2p } from 'libp2p' |
| 86 | + |
| 87 | +const node = await createLibp2p({ /* ... */ }) |
| 88 | + |
| 89 | +// add addresses |
| 90 | +await node.peerStore.addressBook.add(peerId, [ |
| 91 | + multiaddr('/ip4/43.14.67.21/tcp/3847') |
| 92 | +]) |
| 93 | + |
| 94 | +// set protocols |
| 95 | +await node.peerStore.protoBook.set(peerId, [ |
| 96 | + '/a-proto/1.0.0', |
| 97 | + '/another-proto/1.0.0' |
| 98 | +]) |
| 99 | + |
| 100 | +// add metadata |
| 101 | +await node.peerStore.metadataBook.set(peerId, 'key', Uint8Array.from([0, 1, 2, 3])) |
| 102 | + |
| 103 | +// add tags |
| 104 | +await node.peerStore.tagPeer(peerId, 'tag-name', { value: 10 }) |
| 105 | +``` |
| 106 | + |
| 107 | +**After** |
| 108 | + |
| 109 | +```js |
| 110 | +import { createLibp2p } from 'libp2p' |
| 111 | + |
| 112 | +const node = await createLibp2p({ /* ... */ }) |
| 113 | + |
| 114 | +// `save` replaces all data for the peer. Use with caution - any fields not passed |
| 115 | +// will be deleted |
| 116 | +await node.peerStore.save(peerId, { |
| 117 | + multiaddrs: [ |
| 118 | + multiaddr('/ip4/43.14.67.21/tcp/3847') |
| 119 | + ], |
| 120 | + protocols: [ |
| 121 | + '/a-proto/1.0.0', |
| 122 | + '/another-proto/1.0.0' |
| 123 | + ], |
| 124 | + metadata: { |
| 125 | + key: Uint8Array.from([0, 1, 2, 3]) |
| 126 | + }, |
| 127 | + tags: { |
| 128 | + 'tag-name': { value: 10 } |
| 129 | + } |
| 130 | +}) |
| 131 | +``` |
| 132 | + |
| 133 | +Other ways to update peers are available which are more concise and allow you to just update specific fields: |
| 134 | + |
| 135 | +```js |
| 136 | +// `patch` replaces only the passed fields and retains all other data |
| 137 | +await node.peerStore.patch(peerId, { |
| 138 | + multiaddrs: [ |
| 139 | + multiaddr('/ip4/43.14.67.21/tcp/3847') |
| 140 | + ] |
| 141 | +}) |
| 142 | + |
| 143 | +// `merge` behaves like `patch` but deeply merges multiaddrs, protocols, metadata, |
| 144 | +// and tags, removing duplicates. any existing metadata/tags with the same |
| 145 | +// keys/tag names will be overwritten. |
| 146 | +await node.peerStore.merge(peerId, { |
| 147 | + multiaddrs: [ |
| 148 | + multiaddr('/ip4/43.14.67.21/tcp/3847') |
| 149 | + ] |
| 150 | +}) |
| 151 | +``` |
| 152 | + |
| 153 | +You can also remove fields quickly: |
| 154 | + |
| 155 | +```js |
| 156 | +// passing `undefined` to `merge` is a quick way of deleting metadata/tags |
| 157 | +await node.peerStore.merge(peerId, { |
| 158 | + metadata: { |
| 159 | + key: undefined |
| 160 | + }, |
| 161 | + tags: { |
| 162 | + 'tag-name': undefined |
| 163 | + } |
| 164 | +}) |
| 165 | +``` |
| 166 | + |
| 167 | +[Connection]: https://libp2p.github.io/js-libp2p-interfaces/interfaces/_libp2p_interface_connection.Connection.html |
| 168 | +[PeerId]: https://libp2p.github.io/js-libp2p-interfaces/types/_libp2p_interface_peer_id.PeerId.html |
| 169 | +[Identify]: https://github.com/libp2p/specs/blob/master/identify/README.md |
| 170 | +[AutoNAT]: https://github.com/libp2p/specs/blob/master/autonat/README.md |
0 commit comments