Skip to content

Commit

Permalink
chore: remove peer-info usage
Browse files Browse the repository at this point in the history
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
  • Loading branch information
vasco-santos committed Apr 23, 2020
1 parent 54212cb commit 1a344be
Show file tree
Hide file tree
Showing 45 changed files with 613 additions and 700 deletions.
8 changes: 4 additions & 4 deletions .aegir.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const Libp2p = require('./src')
const { MULTIADDRS_WEBSOCKETS } = require('./test/fixtures/browser')
const Peers = require('./test/fixtures/peers')
const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const WebSockets = require('libp2p-websockets')
const Muxer = require('libp2p-mplex')
const Crypto = require('libp2p-secio')
Expand All @@ -14,11 +13,12 @@ let libp2p
const before = async () => {
// Use the last peer
const peerId = await PeerId.createFromJSON(Peers[Peers.length - 1])
const peerInfo = new PeerInfo(peerId)
peerInfo.multiaddrs.add(MULTIADDRS_WEBSOCKETS[0])

libp2p = new Libp2p({
peerInfo,
addresses: {
listen: [MULTIADDRS_WEBSOCKETS[0]]
},
peerId,
modules: {
transport: [WebSockets],
streamMuxer: [Muxer],
Expand Down
61 changes: 33 additions & 28 deletions doc/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ Creates an instance of Libp2p.
|------|------|-------------|
| options | `object` | libp2p options |
| options.modules | `Array<object>` | libp2p modules to use |
| [options.addresses] | `{ listen: Array<Multiaddr> }` | Addresses to use for transport listening and to announce to the network |
| [options.config] | `object` | libp2p modules configuration and core configuration |
| [options.connectionManager] | `object` | libp2p Connection Manager configuration |
| [options.datastore] | `object` | must implement [ipfs/interface-datastore](https://github.com/ipfs/interface-datastore) (in memory datastore will be used if not provided) |
| [options.dialer] | `object` | libp2p Dialer configuration
| [options.metrics] | `object` | libp2p Metrics configuration
| [options.peerInfo] | [`PeerInfo`][peer-info] | peerInfo instance (it will be created if not provided) |
| [options.peerId] | [`PeerId`][peer-id] | peerId instance (it will be created if not provided) |

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

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

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

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

Required keys in the `options` object:

- `peerInfo`: instance of [`PeerInfo`][peer-info] that contains the [`PeerId`][peer-id], Keys and [multiaddrs][multiaddr] of the libp2p Node (optional when using `.create`).
- `peerId`: instance of [`PeerId`][peer-id] that contains the peer Keys (optional when using `.create`).
- `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).

</details>
Expand Down Expand Up @@ -163,6 +164,10 @@ const libp2p = await Libp2p.create(options)
await libp2p.stop()
```

### addresses

TODO with `address-manager`.

### connections

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

| Name | Type | Description |
|------|------|-------------|
| 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 |
| peer | [`PeerId`][peer-id]\|[`Multiaddr`][multiaddr]\|`string` | The peer to dial. |
| [options] | `object` | dial options |
| [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 |

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

#### Returns

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

```js
// ...
const conn = await libp2p.dial(remotePeerInfo)
const conn = await libp2p.dial(remotePeerId)

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

| Name | Type | Description |
|------|------|-------------|
| 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 |
| peer | [`PeerId`][peer-id]\|[`Multiaddr`][multiaddr]\|`string` | The peer to dial. |
| 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') |
| [options] | `object` | dial options |
| [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 |

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

#### Returns

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

const { stream, protocol } = await libp2p.dialProtocol(remotePeerInfo, protocols)
const { stream, protocol } = await libp2p.dialProtocol(remotePeerId, protocols)

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

| Name | Type | Description |
|------|------|-------------|
| peer | [`PeerInfo`][peer-info]\|[`PeerId`][peer-id]\|[`Multiaddr`][multiaddr]\|`string` | peer to hang up |
| peer | [`PeerId`][peer-id]\|[`Multiaddr`][multiaddr]\|`string` | peer to hang up |

#### Returns

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

```js
// ...
await libp2p.hangUp(remotePeerInfo)
await libp2p.hangUp(remotePeerId)
```

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

| Name | Type | Description |
|------|------|-------------|
| peer | [`PeerInfo`][peer-info]\|[`PeerId`][peer-id]\|[`Multiaddr`][multiaddr]\|`string` | peer to ping |
| peer | [`PeerId`][peer-id]\|[`Multiaddr`][multiaddr]\|`string` | peer to ping |

#### Returns

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

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

#### Example

```js
// ...
const peerInfo = await libp2p.peerRouting.findPeer(peerId, options)
const peerData = await libp2p.peerRouting.findPeer(peerId, options)
```

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

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

#### Example

```js
// Iterate over the providers found for the given cid
for await (const provider of libp2p.contentRouting.findProviders(cid)) {
console.log(provider)
console.log(provider.id, provider.addrs)
}
```

Expand Down Expand Up @@ -809,7 +818,7 @@ peerStore.delete(peerId2)

### peerStore.get

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

`peerStore.get(peerId)`

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

| Type | Description |
|------|-------------|
| [`PeerInfo`][peer-info] | Peer information of the provided peer |

TODO: change when `peer-info` is deprecated to new pointer
| `{ id: PeerId, multiaddrInfos: Array<MultiaddrInfo>, protocols: Array<string> }` | Peer information of the provided peer |

#### Example

Expand All @@ -836,6 +843,7 @@ peerStore.addressBook.set(peerId, multiaddrs)
peerStore.protoBook.set(peerId, protocols)
peerStore.get(peerId)
// {
// id: {},
// MultiaddrInfos: [...],
// protocols: [...]
// }
Expand All @@ -851,15 +859,13 @@ Get all the stored information of every peer.

| Type | Description |
|------|-------------|
| `Map<string, PeerInfo>` | Peer information of every peer |

TODO: change when `peer-info` is deprecated to new pointer (breaking change)
| `Map<string, { id: PeerId, multiaddrInfos: Array<MultiaddrInfo>, protocols: Array<string> }>` | Peer data of every peer known |

#### Example

```js
for (let [peerIdString, peerInfo] of peerStore.peers.entries()) {
// peerInfo instance
for (let [peerIdString, peerData] of peerStore.peers.entries()) {
// peerData
}
```

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

```js
const peerStats = libp2p.metrics.forPeer(peerInfo)
const peerStats = libp2p.metrics.forPeer(peerId)
console.log(peerStats.toJSON())
```

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

- `peer`: instance of [`PeerInfo`][peer-info]
- `peer`: instance of [`PeerId`][peer-id]

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

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

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

- `peer`: instance of [`PeerInfo`][peer-info]
- `peer`: instance of [`PeerId`][peer-id]

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

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.

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

- `peer`: instance of [`PeerInfo`][peer-info]
- `peer`: instance of [`PeerId`][peer-id]

### libp2p.peerStore

Expand Down Expand Up @@ -1183,4 +1189,3 @@ This event will be triggered anytime we are disconnected from another peer, rega
[connection]: https://github.com/libp2p/js-interfaces/tree/master/src/connection
[multiaddr]: https://github.com/multiformats/js-multiaddr
[peer-id]: https://github.com/libp2p/js-peer-id
[peer-info]: https://github.com/libp2p/js-peer-info
19 changes: 9 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"it-protocol-buffers": "^0.2.0",
"latency-monitor": "~0.2.1",
"libp2p-crypto": "^0.17.1",
"libp2p-interfaces": "^0.2.3",
"libp2p-interfaces": "^0.3.0",
"libp2p-utils": "^0.1.2",
"mafmt": "^7.0.0",
"merge-options": "^2.0.0",
Expand All @@ -69,7 +69,6 @@
"p-fifo": "^1.0.0",
"p-settle": "^4.0.0",
"peer-id": "^0.13.4",
"peer-info": "^0.17.0",
"protons": "^1.0.1",
"retimer": "^2.0.0",
"timeout-abort-controller": "^1.0.0",
Expand All @@ -88,17 +87,17 @@
"it-concat": "^1.0.0",
"it-pair": "^1.0.0",
"it-pushable": "^1.4.0",
"libp2p-bootstrap": "^0.10.3",
"libp2p-delegated-content-routing": "^0.4.1",
"libp2p-delegated-peer-routing": "^0.4.0",
"libp2p-floodsub": "^0.20.0",
"libp2p-gossipsub": "^0.2.0",
"libp2p-kad-dht": "^0.19.0-pre.0",
"libp2p-mdns": "^0.13.0",
"libp2p-bootstrap": "^0.11.0",
"libp2p-delegated-content-routing": "libp2p/js-libp2p-delegated-content-routing#master",
"libp2p-delegated-peer-routing": "libp2p/js-libp2p-delegated-peer-routing#master",
"libp2p-floodsub": "^0.21.0",
"libp2p-gossipsub": "ChainSafe/gossipsub-js#chore/remove-peer-info-usage",
"libp2p-kad-dht": "libp2p/js-libp2p-kad-dht#chore/use-new-content-and-peer-routing-apis",
"libp2p-mdns": "^0.14.0",
"libp2p-mplex": "^0.9.1",
"libp2p-secio": "^0.12.1",
"libp2p-tcp": "^0.14.1",
"libp2p-webrtc-star": "^0.17.0",
"libp2p-webrtc-star": "^0.18.0",
"libp2p-websockets": "^0.13.1",
"nock": "^12.0.0",
"p-defer": "^3.0.0",
Expand Down
9 changes: 4 additions & 5 deletions src/circuit/circuit/hop.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict'

const debug = require('debug')
const PeerInfo = require('peer-info')
const log = debug('libp2p:circuit:hop')
log.error = debug('libp2p:circuit:hop:error')

const PeerId = require('peer-id')
const { validateAddrs } = require('./utils')
const StreamHandler = require('./stream-handler')
Expand All @@ -14,9 +16,6 @@ const { stop } = require('./stop')

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

const log = debug('libp2p:circuit:hop')
log.error = debug('libp2p:circuit:hop:error')

module.exports.handleHop = async function handleHop ({
connection,
request,
Expand All @@ -42,7 +41,7 @@ module.exports.handleHop = async function handleHop ({
// Get the connection to the destination (stop) peer
const destinationPeer = new PeerId(request.dstPeer.id)

const destinationConnection = circuit._registrar.getConnection(new PeerInfo(destinationPeer))
const destinationConnection = circuit._registrar.getConnection(destinationPeer)
if (!destinationConnection && !circuit._options.hop.active) {
log('HOP request received but we are not connected to the destination peer')
return streamHandler.end({
Expand Down
12 changes: 6 additions & 6 deletions src/circuit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const mafmt = require('mafmt')
const multiaddr = require('multiaddr')
const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const withIs = require('class-is')
const { CircuitRelay: CircuitPB } = require('./protocol')

Expand Down Expand Up @@ -32,7 +31,8 @@ class Circuit {
this._registrar = libp2p.registrar
this._upgrader = upgrader
this._options = libp2p._config.relay
this.peerInfo = libp2p.peerInfo
this.addresses = libp2p.addresses
this.peerId = libp2p.peerId
this._registrar.handle(multicodec, this._onProtocol.bind(this))
}

Expand Down Expand Up @@ -107,7 +107,7 @@ class Circuit {
const destinationPeer = PeerId.createFromCID(destinationAddr.getPeerId())

let disconnectOnFailure = false
let relayConnection = this._registrar.getConnection(new PeerInfo(relayPeer))
let relayConnection = this._registrar.getConnection(relayPeer)
if (!relayConnection) {
relayConnection = await this._dialer.connectToPeer(relayAddr, options)
disconnectOnFailure = true
Expand All @@ -120,8 +120,8 @@ class Circuit {
request: {
type: CircuitPB.Type.HOP,
srcPeer: {
id: this.peerInfo.id.toBytes(),
addrs: this.peerInfo.multiaddrs.toArray().map(addr => addr.buffer)
id: this.peerId.toBytes(),
addrs: this.addresses.listen.map(addr => addr.buffer)
},
dstPeer: {
id: destinationPeer.toBytes(),
Expand All @@ -130,7 +130,7 @@ class Circuit {
}
})

const localAddr = relayAddr.encapsulate(`/p2p-circuit/p2p/${this.peerInfo.id.toB58String()}`)
const localAddr = relayAddr.encapsulate(`/p2p-circuit/p2p/${this.peerId.toB58String()}`)
const maConn = toConnection({
stream: virtualConnection,
remoteAddr: ma,
Expand Down
3 changes: 3 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const mergeOptions = require('merge-options')
const Constants = require('./constants')

const DefaultConfig = {
addresses: {
listen: []
},
connectionManager: {
minPeers: 25
},
Expand Down
Loading

0 comments on commit 1a344be

Please sign in to comment.