Skip to content

Commit 1a344be

Browse files
committed
chore: remove peer-info usage
BREAKING CHANGE: all API methods with peer-info parameters or return values were changed. You can check the API.md document, in order to check the new values to use
1 parent 54212cb commit 1a344be

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+613
-700
lines changed

.aegir.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const Libp2p = require('./src')
44
const { MULTIADDRS_WEBSOCKETS } = require('./test/fixtures/browser')
55
const Peers = require('./test/fixtures/peers')
66
const PeerId = require('peer-id')
7-
const PeerInfo = require('peer-info')
87
const WebSockets = require('libp2p-websockets')
98
const Muxer = require('libp2p-mplex')
109
const Crypto = require('libp2p-secio')
@@ -14,11 +13,12 @@ let libp2p
1413
const before = async () => {
1514
// Use the last peer
1615
const peerId = await PeerId.createFromJSON(Peers[Peers.length - 1])
17-
const peerInfo = new PeerInfo(peerId)
18-
peerInfo.multiaddrs.add(MULTIADDRS_WEBSOCKETS[0])
1916

2017
libp2p = new Libp2p({
21-
peerInfo,
18+
addresses: {
19+
listen: [MULTIADDRS_WEBSOCKETS[0]]
20+
},
21+
peerId,
2222
modules: {
2323
transport: [WebSockets],
2424
streamMuxer: [Muxer],

doc/API.md

+33-28
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,13 @@ Creates an instance of Libp2p.
6060
|------|------|-------------|
6161
| options | `object` | libp2p options |
6262
| options.modules | `Array<object>` | libp2p modules to use |
63+
| [options.addresses] | `{ listen: Array<Multiaddr> }` | Addresses to use for transport listening and to announce to the network |
6364
| [options.config] | `object` | libp2p modules configuration and core configuration |
6465
| [options.connectionManager] | `object` | libp2p Connection Manager configuration |
6566
| [options.datastore] | `object` | must implement [ipfs/interface-datastore](https://github.com/ipfs/interface-datastore) (in memory datastore will be used if not provided) |
6667
| [options.dialer] | `object` | libp2p Dialer configuration
6768
| [options.metrics] | `object` | libp2p Metrics configuration
68-
| [options.peerInfo] | [`PeerInfo`][peer-info] | peerInfo instance (it will be created if not provided) |
69+
| [options.peerId] | [`PeerId`][peer-id] | peerId instance (it will be created if not provided) |
6970

7071
For Libp2p configurations and modules details read the [Configuration Document](./CONFIGURATION.md).
7172

@@ -87,7 +88,7 @@ const options = {}
8788
const libp2p = await Libp2p.create(options)
8889
```
8990

90-
Note: The [`PeerInfo`][peer-info] option is not required and will be generated if it is not provided.
91+
Note: The [`PeerId`][peer-id] option is not required and will be generated if it is not provided.
9192

9293
<details><summary>Alternative</summary>
9394
As an alternative, it is possible to create a Libp2p instance with the constructor:
@@ -106,7 +107,7 @@ const libp2p = new Libp2p(options)
106107

107108
Required keys in the `options` object:
108109

109-
- `peerInfo`: instance of [`PeerInfo`][peer-info] that contains the [`PeerId`][peer-id], Keys and [multiaddrs][multiaddr] of the libp2p Node (optional when using `.create`).
110+
- `peerId`: instance of [`PeerId`][peer-id] that contains the peer Keys (optional when using `.create`).
110111
- `modules.transport`: An array that must include at least 1 compliant transport. See [modules that implement the transport interface](https://github.com/libp2p/js-interfaces/tree/master/src/transport#modules-that-implement-the-interface).
111112

112113
</details>
@@ -163,6 +164,10 @@ const libp2p = await Libp2p.create(options)
163164
await libp2p.stop()
164165
```
165166

167+
### addresses
168+
169+
TODO with `address-manager`.
170+
166171
### connections
167172

168173
A Getter that returns a Map of the current Connections libp2p has to other peers.
@@ -194,10 +199,12 @@ for (const [peerId, connections] of libp2p.connections) {
194199

195200
| Name | Type | Description |
196201
|------|------|-------------|
197-
| peer | [`PeerInfo`][peer-info]\|[`PeerId`][peer-id]\|[`Multiaddr`][multiaddr]\|`string` | The peer to dial. If a [`Multiaddr`][multiaddr] or its string is provided, it **must** include the peer id |
202+
| peer | [`PeerId`][peer-id]\|[`Multiaddr`][multiaddr]\|`string` | The peer to dial. |
198203
| [options] | `object` | dial options |
199204
| [options.signal] | [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) | An `AbortSignal` instance obtained from an [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) that can be used to abort the connection before it completes |
200205

206+
**Note:** If a [`Multiaddr`][multiaddr] or its string is provided, it **must** include the peer id. Moreover, if a [`PeerId`][peer-id] is given, the peer will need to have known multiaddrs for it in the PeerStore.
207+
201208
#### Returns
202209

203210
| Type | Description |
@@ -208,7 +215,7 @@ for (const [peerId, connections] of libp2p.connections) {
208215

209216
```js
210217
// ...
211-
const conn = await libp2p.dial(remotePeerInfo)
218+
const conn = await libp2p.dial(remotePeerId)
212219

213220
// create a new stream within the connection
214221
const { stream, protocol } = await conn.newStream(['/echo/1.1.0', '/echo/1.0.0'])
@@ -229,11 +236,13 @@ Dials to another peer in the network and selects a protocol to communicate with
229236

230237
| Name | Type | Description |
231238
|------|------|-------------|
232-
| peer | [`PeerInfo`][peer-info]\|[`PeerId`][peer-id]\|[`Multiaddr`][multiaddr]\|`string` | The peer to dial. If a [`Multiaddr`][multiaddr] or its string is provided, it **must** include the peer id |
239+
| peer | [`PeerId`][peer-id]\|[`Multiaddr`][multiaddr]\|`string` | The peer to dial. |
233240
| protocols | `string|Array<string>` | A list of protocols (or single protocol) to negotiate with. Protocols are attempted in order until a match is made. (e.g '/ipfs/bitswap/1.1.0') |
234241
| [options] | `object` | dial options |
235242
| [options.signal] | [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) | An `AbortSignal` instance obtained from an [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) that can be used to abort the connection before it completes |
236243

244+
**Note:** If a [`Multiaddr`][multiaddr] or its string is provided, it **must** include the peer id. Moreover, if a [`PeerId`][peer-id] is given, the peer will need to have known multiaddrs for it in the PeerStore.
245+
237246
#### Returns
238247

239248
| Type | Description |
@@ -246,7 +255,7 @@ Dials to another peer in the network and selects a protocol to communicate with
246255
// ...
247256
const pipe = require('it-pipe')
248257

249-
const { stream, protocol } = await libp2p.dialProtocol(remotePeerInfo, protocols)
258+
const { stream, protocol } = await libp2p.dialProtocol(remotePeerId, protocols)
250259

251260
// Use this new stream like any other duplex stream
252261
pipe([1, 2, 3], stream, consume)
@@ -262,7 +271,7 @@ Attempts to gracefully close an open connection to the given peer. If the connec
262271

263272
| Name | Type | Description |
264273
|------|------|-------------|
265-
| peer | [`PeerInfo`][peer-info]\|[`PeerId`][peer-id]\|[`Multiaddr`][multiaddr]\|`string` | peer to hang up |
274+
| peer | [`PeerId`][peer-id]\|[`Multiaddr`][multiaddr]\|`string` | peer to hang up |
266275

267276
#### Returns
268277

@@ -274,7 +283,7 @@ Attempts to gracefully close an open connection to the given peer. If the connec
274283

275284
```js
276285
// ...
277-
await libp2p.hangUp(remotePeerInfo)
286+
await libp2p.hangUp(remotePeerId)
278287
```
279288

280289
### handle
@@ -333,7 +342,7 @@ Pings a given peer and get the operation's latency.
333342

334343
| Name | Type | Description |
335344
|------|------|-------------|
336-
| peer | [`PeerInfo`][peer-info]\|[`PeerId`][peer-id]\|[`Multiaddr`][multiaddr]\|`string` | peer to ping |
345+
| peer | [`PeerId`][peer-id]\|[`Multiaddr`][multiaddr]\|`string` | peer to ping |
337346

338347
#### Returns
339348

@@ -366,13 +375,13 @@ Iterates over all peer routers in series to find the given peer. If the DHT is e
366375

367376
| Type | Description |
368377
|------|-------------|
369-
| `Promise<PeerInfo>` | Peer info of a known peer |
378+
| `Promise<{ id: PeerId, multiaddrs: Multiaddr[] }>` | Peer data of a known peer |
370379

371380
#### Example
372381

373382
```js
374383
// ...
375-
const peerInfo = await libp2p.peerRouting.findPeer(peerId, options)
384+
const peerData = await libp2p.peerRouting.findPeer(peerId, options)
376385
```
377386

378387
### contentRouting.findProviders
@@ -395,14 +404,14 @@ Once a content router succeeds, the iteration will stop. If the DHT is enabled,
395404

396405
| Type | Description |
397406
|------|-------------|
398-
| `AsyncIterator<PeerInfo>` | Async iterator for [`PeerInfo`][peer-info] |
407+
| `AsyncIterable<{ id: PeerId, multiaddrs: Multiaddr[] }` | Async iterator for peer data |
399408

400409
#### Example
401410

402411
```js
403412
// Iterate over the providers found for the given cid
404413
for await (const provider of libp2p.contentRouting.findProviders(cid)) {
405-
console.log(provider)
414+
console.log(provider.id, provider.addrs)
406415
}
407416
```
408417

@@ -809,7 +818,7 @@ peerStore.delete(peerId2)
809818

810819
### peerStore.get
811820

812-
Get the stored information of a given peer.
821+
Get the stored information of a given peer, namely its [`PeerId`][peer-id], known [`MultiaddrInfos`][multiaddr-info] and supported protocols.
813822

814823
`peerStore.get(peerId)`
815824

@@ -823,9 +832,7 @@ Get the stored information of a given peer.
823832

824833
| Type | Description |
825834
|------|-------------|
826-
| [`PeerInfo`][peer-info] | Peer information of the provided peer |
827-
828-
TODO: change when `peer-info` is deprecated to new pointer
835+
| `{ id: PeerId, multiaddrInfos: Array<MultiaddrInfo>, protocols: Array<string> }` | Peer information of the provided peer |
829836

830837
#### Example
831838

@@ -836,6 +843,7 @@ peerStore.addressBook.set(peerId, multiaddrs)
836843
peerStore.protoBook.set(peerId, protocols)
837844
peerStore.get(peerId)
838845
// {
846+
// id: {},
839847
// MultiaddrInfos: [...],
840848
// protocols: [...]
841849
// }
@@ -851,15 +859,13 @@ Get all the stored information of every peer.
851859

852860
| Type | Description |
853861
|------|-------------|
854-
| `Map<string, PeerInfo>` | Peer information of every peer |
855-
856-
TODO: change when `peer-info` is deprecated to new pointer (breaking change)
862+
| `Map<string, { id: PeerId, multiaddrInfos: Array<MultiaddrInfo>, protocols: Array<string> }>` | Peer data of every peer known |
857863

858864
#### Example
859865

860866
```js
861-
for (let [peerIdString, peerInfo] of peerStore.peers.entries()) {
862-
// peerInfo instance
867+
for (let [peerIdString, peerData] of peerStore.peers.entries()) {
868+
// peerData
863869
}
864870
```
865871

@@ -1070,7 +1076,7 @@ Returns the [`Stats`](#stats) object for a given [`PeerId`][peer-id] if it is be
10701076
#### Example
10711077

10721078
```js
1073-
const peerStats = libp2p.metrics.forPeer(peerInfo)
1079+
const peerStats = libp2p.metrics.forPeer(peerId)
10741080
console.log(peerStats.toJSON())
10751081
```
10761082

@@ -1118,23 +1124,23 @@ Once you have a libp2p instance, you can listen to several events it emits, so t
11181124
If `autoDial` option is `true`, applications should **not** attempt to connect to the peer
11191125
unless they are performing a specific action. See [peer discovery and auto dial](./PEER_DISCOVERY.md) for more information.
11201126

1121-
- `peer`: instance of [`PeerInfo`][peer-info]
1127+
- `peer`: instance of [`PeerId`][peer-id]
11221128

11231129
#### A new connection to a peer has been opened
11241130

11251131
This event will be triggered anytime a new Connection is established to another peer.
11261132

11271133
`libp2p.on('peer:connect', (peer) => {})`
11281134

1129-
- `peer`: instance of [`PeerInfo`][peer-info]
1135+
- `peer`: instance of [`PeerId`][peer-id]
11301136

11311137
#### An existing connection to a peer has been closed
11321138

11331139
This event will be triggered anytime we are disconnected from another peer, regardless of the circumstances of that disconnection. If we happen to have multiple connections to a peer, this event will **only** be triggered when the last connection is closed.
11341140

11351141
`libp2p.on('peer:disconnect', (peer) => {})`
11361142

1137-
- `peer`: instance of [`PeerInfo`][peer-info]
1143+
- `peer`: instance of [`PeerId`][peer-id]
11381144

11391145
### libp2p.peerStore
11401146

@@ -1183,4 +1189,3 @@ This event will be triggered anytime we are disconnected from another peer, rega
11831189
[connection]: https://github.com/libp2p/js-interfaces/tree/master/src/connection
11841190
[multiaddr]: https://github.com/multiformats/js-multiaddr
11851191
[peer-id]: https://github.com/libp2p/js-peer-id
1186-
[peer-info]: https://github.com/libp2p/js-peer-info

package.json

+9-10
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"it-protocol-buffers": "^0.2.0",
5858
"latency-monitor": "~0.2.1",
5959
"libp2p-crypto": "^0.17.1",
60-
"libp2p-interfaces": "^0.2.3",
60+
"libp2p-interfaces": "^0.3.0",
6161
"libp2p-utils": "^0.1.2",
6262
"mafmt": "^7.0.0",
6363
"merge-options": "^2.0.0",
@@ -69,7 +69,6 @@
6969
"p-fifo": "^1.0.0",
7070
"p-settle": "^4.0.0",
7171
"peer-id": "^0.13.4",
72-
"peer-info": "^0.17.0",
7372
"protons": "^1.0.1",
7473
"retimer": "^2.0.0",
7574
"timeout-abort-controller": "^1.0.0",
@@ -88,17 +87,17 @@
8887
"it-concat": "^1.0.0",
8988
"it-pair": "^1.0.0",
9089
"it-pushable": "^1.4.0",
91-
"libp2p-bootstrap": "^0.10.3",
92-
"libp2p-delegated-content-routing": "^0.4.1",
93-
"libp2p-delegated-peer-routing": "^0.4.0",
94-
"libp2p-floodsub": "^0.20.0",
95-
"libp2p-gossipsub": "^0.2.0",
96-
"libp2p-kad-dht": "^0.19.0-pre.0",
97-
"libp2p-mdns": "^0.13.0",
90+
"libp2p-bootstrap": "^0.11.0",
91+
"libp2p-delegated-content-routing": "libp2p/js-libp2p-delegated-content-routing#master",
92+
"libp2p-delegated-peer-routing": "libp2p/js-libp2p-delegated-peer-routing#master",
93+
"libp2p-floodsub": "^0.21.0",
94+
"libp2p-gossipsub": "ChainSafe/gossipsub-js#chore/remove-peer-info-usage",
95+
"libp2p-kad-dht": "libp2p/js-libp2p-kad-dht#chore/use-new-content-and-peer-routing-apis",
96+
"libp2p-mdns": "^0.14.0",
9897
"libp2p-mplex": "^0.9.1",
9998
"libp2p-secio": "^0.12.1",
10099
"libp2p-tcp": "^0.14.1",
101-
"libp2p-webrtc-star": "^0.17.0",
100+
"libp2p-webrtc-star": "^0.18.0",
102101
"libp2p-websockets": "^0.13.1",
103102
"nock": "^12.0.0",
104103
"p-defer": "^3.0.0",

src/circuit/circuit/hop.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
'use strict'
22

33
const debug = require('debug')
4-
const PeerInfo = require('peer-info')
4+
const log = debug('libp2p:circuit:hop')
5+
log.error = debug('libp2p:circuit:hop:error')
6+
57
const PeerId = require('peer-id')
68
const { validateAddrs } = require('./utils')
79
const StreamHandler = require('./stream-handler')
@@ -14,9 +16,6 @@ const { stop } = require('./stop')
1416

1517
const multicodec = require('./../multicodec')
1618

17-
const log = debug('libp2p:circuit:hop')
18-
log.error = debug('libp2p:circuit:hop:error')
19-
2019
module.exports.handleHop = async function handleHop ({
2120
connection,
2221
request,
@@ -42,7 +41,7 @@ module.exports.handleHop = async function handleHop ({
4241
// Get the connection to the destination (stop) peer
4342
const destinationPeer = new PeerId(request.dstPeer.id)
4443

45-
const destinationConnection = circuit._registrar.getConnection(new PeerInfo(destinationPeer))
44+
const destinationConnection = circuit._registrar.getConnection(destinationPeer)
4645
if (!destinationConnection && !circuit._options.hop.active) {
4746
log('HOP request received but we are not connected to the destination peer')
4847
return streamHandler.end({

src/circuit/index.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
const mafmt = require('mafmt')
44
const multiaddr = require('multiaddr')
55
const PeerId = require('peer-id')
6-
const PeerInfo = require('peer-info')
76
const withIs = require('class-is')
87
const { CircuitRelay: CircuitPB } = require('./protocol')
98

@@ -32,7 +31,8 @@ class Circuit {
3231
this._registrar = libp2p.registrar
3332
this._upgrader = upgrader
3433
this._options = libp2p._config.relay
35-
this.peerInfo = libp2p.peerInfo
34+
this.addresses = libp2p.addresses
35+
this.peerId = libp2p.peerId
3636
this._registrar.handle(multicodec, this._onProtocol.bind(this))
3737
}
3838

@@ -107,7 +107,7 @@ class Circuit {
107107
const destinationPeer = PeerId.createFromCID(destinationAddr.getPeerId())
108108

109109
let disconnectOnFailure = false
110-
let relayConnection = this._registrar.getConnection(new PeerInfo(relayPeer))
110+
let relayConnection = this._registrar.getConnection(relayPeer)
111111
if (!relayConnection) {
112112
relayConnection = await this._dialer.connectToPeer(relayAddr, options)
113113
disconnectOnFailure = true
@@ -120,8 +120,8 @@ class Circuit {
120120
request: {
121121
type: CircuitPB.Type.HOP,
122122
srcPeer: {
123-
id: this.peerInfo.id.toBytes(),
124-
addrs: this.peerInfo.multiaddrs.toArray().map(addr => addr.buffer)
123+
id: this.peerId.toBytes(),
124+
addrs: this.addresses.listen.map(addr => addr.buffer)
125125
},
126126
dstPeer: {
127127
id: destinationPeer.toBytes(),
@@ -130,7 +130,7 @@ class Circuit {
130130
}
131131
})
132132

133-
const localAddr = relayAddr.encapsulate(`/p2p-circuit/p2p/${this.peerInfo.id.toB58String()}`)
133+
const localAddr = relayAddr.encapsulate(`/p2p-circuit/p2p/${this.peerId.toB58String()}`)
134134
const maConn = toConnection({
135135
stream: virtualConnection,
136136
remoteAddr: ma,

src/config.js

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ const mergeOptions = require('merge-options')
44
const Constants = require('./constants')
55

66
const DefaultConfig = {
7+
addresses: {
8+
listen: []
9+
},
710
connectionManager: {
811
minPeers: 25
912
},

0 commit comments

Comments
 (0)