From a08a72512300c46dd332aa3f8df663ade662cba5 Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Thu, 9 Apr 2020 16:07:18 +0200 Subject: [PATCH] feat: address and proto books (#590) * feat: address and proto books * chore: apply suggestions from code review Co-Authored-By: Jacob Heun * chore: minor fixes and initial tests added * chore: integrate new peer-store with code using adapters for other modules * chore: do not use peerstore.put on get-peer-info * chore: apply suggestions from code review Co-Authored-By: Jacob Heun * chore: add new peer store tests * chore: apply suggestions from code review Co-Authored-By: Jacob Heun Co-authored-by: Jacob Heun --- doc/API.md | 409 +++++++++++++++++++-- src/dialer/index.js | 36 +- src/get-peer-info.js | 7 +- src/identify/index.js | 55 +-- src/index.js | 15 +- src/peer-store/README.md | 90 ++++- src/peer-store/address-book.js | 214 +++++++++++ src/peer-store/book.js | 87 +++++ src/peer-store/index.js | 322 ++++++++-------- src/peer-store/proto-book.js | 137 +++++++ src/ping/index.js | 4 +- src/registrar.js | 26 +- src/upgrader.js | 4 +- test/content-routing/dht/operation.node.js | 7 +- test/dialing/direct.node.js | 17 +- test/dialing/direct.spec.js | 46 ++- test/dialing/relay.node.js | 7 +- test/identify/index.spec.js | 57 ++- test/peer-discovery/index.spec.js | 8 +- test/peer-store/address-book.spec.js | 365 ++++++++++++++++++ test/peer-store/peer-store.spec.js | 244 ++++++------ test/peer-store/proto-book.spec.js | 310 ++++++++++++++++ test/registrar/registrar.spec.js | 18 +- 23 files changed, 2012 insertions(+), 473 deletions(-) create mode 100644 src/peer-store/address-book.js create mode 100644 src/peer-store/book.js create mode 100644 src/peer-store/proto-book.js create mode 100644 test/peer-store/address-book.spec.js create mode 100644 test/peer-store/proto-book.spec.js diff --git a/doc/API.md b/doc/API.md index 3f12f22dfb..bfaa9f778b 100644 --- a/doc/API.md +++ b/doc/API.md @@ -17,6 +17,18 @@ * [`contentRouting.put`](#contentroutingput) * [`contentRouting.get`](#contentroutingget) * [`contentRouting.getMany`](#contentroutinggetmany) + * [`peerStore.addressBook.add`](#peerstoreaddressbookadd) + * [`peerStore.addressBook.delete`](#peerstoreaddressbookdelete) + * [`peerStore.addressBook.get`](#peerstoreaddressbookget) + * [`peerStore.addressBook.getMultiaddrsForPeer`](#peerstoreaddressbookgetmultiaddrsforpeer) + * [`peerStore.addressBook.set`](#peerstoreaddressbookset) + * [`peerStore.protoBook.add`](#peerstoreprotobookadd) + * [`peerStore.protoBook.delete`](#peerstoreprotobookdelete) + * [`peerStore.protoBook.get`](#peerstoreprotobookget) + * [`peerStore.protoBook.set`](#peerstoreprotobookset) + * [`peerStore.delete`](#peerstoredelete) + * [`peerStore.get`](#peerstoreget) + * [`peerStore.peers`](#peerstorepeers) * [`pubsub.getSubscribers`](#pubsubgetsubscribers) * [`pubsub.getTopics`](#pubsubgettopics) * [`pubsub.publish`](#pubsubpublish) @@ -44,13 +56,13 @@ Creates an instance of Libp2p. | Name | Type | Description | |------|------|-------------| -| options | `Object` | libp2p options | -| options.modules | `Array` | libp2p modules to use | -| [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 | `object` | libp2p options | +| options.modules | `Array` | libp2p modules to use | +| [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) | For Libp2p configurations and modules details read the [Configuration Document](./CONFIGURATION.md). @@ -181,7 +193,7 @@ 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 | -| [options] | `Object` | dial options | +| [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 | #### Returns @@ -216,8 +228,8 @@ 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 | -| protocols | `String|Array` | 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 | +| protocols | `string|Array` | 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 | #### Returns @@ -275,7 +287,7 @@ In the event of a new handler for the same protocol being added, the first one i | Name | Type | Description | |------|------|-------------| -| protocols | `Array|String` | protocols to register | +| protocols | `Array|string` | protocols to register | | handler | `function({ connection:*, stream:*, protocol:string })` | handler to call | @@ -300,7 +312,7 @@ Unregisters all handlers with the given protocols | Name | Type | Description | |------|------|-------------| -| protocols | `Array|String` | protocols to unregister | +| protocols | `Array|string` | protocols to unregister | #### Example @@ -345,7 +357,7 @@ Iterates over all peer routers in series to find the given peer. If the DHT is e | Name | Type | Description | |------|------|-------------| | peerId | [`PeerId`][peer-id] | ID of the peer to find | -| options | `Object` | operation options | +| options | `object` | operation options | | options.timeout | `number` | maximum time the query should run | #### Returns @@ -373,7 +385,7 @@ Once a content router succeeds, the iteration will stop. If the DHT is enabled, | Name | Type | Description | |------|------|-------------| | cid | [`CID`][cid] | cid to find | -| options | `Object` | operation options | +| options | `object` | operation options | | options.timeout | `number` | maximum time the query should run | | options.maxNumProviders | `number` | maximum number of providers to find | @@ -427,9 +439,9 @@ Writes a value to a key in the DHT. | Name | Type | Description | |------|------|-------------| -| key | `String` | key to add to the dht | +| key | `string` | key to add to the dht | | value | `Buffer` | value to add to the dht | -| [options] | `Object` | put options | +| [options] | `object` | put options | | [options.minPeers] | `number` | minimum number of peers required to successfully put (default: closestPeers.length) | #### Returns @@ -458,8 +470,8 @@ Queries the DHT for a value stored for a given key. | Name | Type | Description | |------|------|-------------| -| key | `String` | key to get from the dht | -| [options] | `Object` | get options | +| key | `string` | key to get from the dht | +| [options] | `object` | get options | | [options.timeout] | `number` | maximum time the query should run | #### Returns @@ -487,9 +499,9 @@ Queries the DHT for the n values stored for the given key (without sorting). | Name | Type | Description | |------|------|-------------| -| key | `String` | key to get from the dht | +| key | `string` | key to get from the dht | | nvals | `number` | number of values aimed | -| [options] | `Object` | get options | +| [options] | `object` | get options | | [options.timeout] | `number` | maximum time the query should run | #### Returns @@ -507,6 +519,348 @@ const key = '/key' const { from, val } = await libp2p.contentRouting.get(key) ``` +### peerStore.addressBook.add + +Adds known `multiaddrs` of a given peer. If the peer is not known, it will be set with the provided multiaddrs. + +`peerStore.addressBook.add(peerId, multiaddrs)` + +#### Parameters + +| Name | Type | Description | +|------|------|-------------| +| peerId | [`PeerId`][peer-id] | peerId to set | +| multiaddrs | |`Array` | [`Multiaddrs`][multiaddr] to add | + +#### Returns + +| Type | Description | +|------|-------------| +| `AddressBook` | Returns the Address Book component | + +#### Example + +```js +peerStore.addressBook.add(peerId, multiaddr) +``` + +### peerStore.addressBook.delete + +Delete the provided peer from the book. + +`peerStore.addressBook.delete(peerId)` + +#### Parameters + +| Name | Type | Description | +|------|------|-------------| +| peerId | [`PeerId`][peer-id] | peerId to remove | + +#### Returns + +| Type | Description | +|------|-------------| +| `boolean` | true if found and removed | + +#### Example + +```js +peerStore.addressBook.delete(peerId) +// false +peerStore.addressBook.set(peerId, multiaddr) +peerStore.addressBook.delete(peerId) +// true +``` + +### peerStore.addressBook.get + +Get the known [`MultiaddrInfos`][multiaddr-info] of a provided peer. + +`peerStore.addressBook.get(peerId)` + +#### Parameters + +| Name | Type | Description | +|------|------|-------------| +| peerId | [`PeerId`][peer-id] | peerId to get | + +#### Returns + +| Type | Description | +|------|-------------| +| `Array` | Array of peer's multiaddr with their relevant information [`MultiaddrInfo`][multiaddr-info] | + +#### Example + +```js +peerStore.addressBook.get(peerId) +// undefined +peerStore.addressBook.set(peerId, multiaddr) +peerStore.addressBook.get(peerId) +// [ +// { +// multiaddr: /ip4/140.10.2.1/tcp/8000, +// ... +// }, +// { +// multiaddr: /ip4/140.10.2.1/ws/8001 +// ... +// }, +// ] +``` + +## peerStore.addressBook.getMultiaddrsForPeer + +Get the known `Multiaddr` of a provided peer. All returned multiaddrs will include the encapsulated `PeerId` of the peer. + +`peerStore.addressBook.getMultiaddrsForPeer(peerId)` + +#### Parameters + +| Name | Type | Description | +|------|------|-------------| +| peerId | [`PeerId`][peer-id] | peerId to get | + +#### Returns + +| Type | Description | +|------|-------------| +| `Array` | Array of peer's multiaddr | + +#### Example + +```js +peerStore.addressBook.getMultiaddrsForPeer(peerId) +// undefined +peerStore.addressBook.set(peerId, multiaddr) +peerStore.addressBook.getMultiaddrsForPeer(peerId) +// [ +// /ip4/140.10.2.1/tcp/8000/p2p/QmW8rAgaaA6sRydK1k6vonShQME47aDxaFidbtMevWs73t +// /ip4/140.10.2.1/ws/8001/p2p/QmW8rAgaaA6sRydK1k6vonShQME47aDxaFidbtMevWs73t +// ] +``` + +### peerStore.addressBook.set + +Set known `multiaddrs` of a given peer. + +`peerStore.addressBook.set(peerId, multiaddrs)` + +#### Parameters + +| Name | Type | Description | +|------|------|-------------| +| peerId | [`PeerId`][peer-id] | peerId to set | +| multiaddrs | |`Array` | [`Multiaddrs`][multiaddr] to store | + +#### Returns + +| Type | Description | +|------|-------------| +| `AddressBook` | Returns the Address Book component | + +#### Example + +```js +peerStore.addressBook.add(peerId, multiaddr) +``` + +### peerStore.protoBook.add + +Add known `protocols` of a given peer. + +`peerStore.protoBook.add(peerId, protocols)` + +#### Parameters + +| Name | Type | Description | +|------|------|-------------| +| peerId | [`PeerId`][peer-id] | peerId to set | +| protocols | `Array` | protocols to add | + +#### Returns + +| Type | Description | +|------|-------------| +| `ProtoBook` | Returns the Proto Book component | + +#### Example + +```js +peerStore.protoBook.add(peerId, protocols) +``` + +### peerStore.protoBook.delete + +Delete the provided peer from the book. + +`peerStore.protoBook.delete(peerId)` + +#### Parameters + +| Name | Type | Description | +|------|------|-------------| +| peerId | [`PeerId`][peer-id] | peerId to remove | + +#### Returns + +| Type | Description | +|------|-------------| +| `boolean` | true if found and removed | + +#### Example + +```js +peerStore.protoBook.delete(peerId) +// false +peerStore.protoBook.set(peerId, protocols) +peerStore.protoBook.delete(peerId) +// true +``` + +### peerStore.protoBook.get + +Get the known `protocols` of a provided peer. + +`peerStore.protoBook.get(peerId)` + +#### Parameters + +| Name | Type | Description | +|------|------|-------------| +| peerId | [`PeerId`][peer-id] | peerId to get | + +#### Returns + +| Type | Description | +|------|-------------| +| `Array` | Array of peer's supported protocols | + +#### Example + +```js +peerStore.protoBook.get(peerId) +// undefined +peerStore.protoBook.set(peerId, [ '/proto/1.0.0', '/proto/1.1.0' ]) +peerStore.protoBook.get(peerId) +// [ '/proto/1.0.0', '/proto/1.1.0' ] +``` + +### peerStore.protoBook.set + +Set known `protocols` of a given peer. + +`peerStore.protoBook.set(peerId, protocols)` + +#### Parameters + +| Name | Type | Description | +|------|------|-------------| +| peerId | [`PeerId`][peer-id] | peerId to set | +| protocols | `Array` | protocols to store | + +#### Returns + +| Type | Description | +|------|-------------| +| `ProtoBook` | Returns the Proto Book component | + +#### Example + +```js +peerStore.protoBook.set(peerId, protocols) +``` + +### peerStore.delete + +Delete the provided peer from every book. + +`peerStore.delete(peerId)` + +#### Parameters + +| Name | Type | Description | +|------|------|-------------| +| peerId | [`PeerId`][peer-id] | peerId to remove | + +#### Returns + +| Type | Description | +|------|-------------| +| `boolean` | true if found and removed | + +#### Example + +```js +peerStore.delete(peerId) +// false +peerStore.addressBook.set(peerId, multiaddrs) +peerStore.protoBook.set(peerId, protocols) +peerStore.delete(peerId) +// true +peerStore.delete(peerId2) +// false +peerStore.addressBook.set(peerId2, multiaddrs) +peerStore.delete(peerId2) +// true +``` + +### peerStore.get + +Get the stored information of a given peer. + +`peerStore.get(peerId)` + +#### Parameters + +| Name | Type | Description | +|------|------|-------------| +| peerId | [`PeerId`][peer-id] | peerId to get | + +#### Returns + +| Type | Description | +|------|-------------| +| [`PeerInfo`][peer-info] | Peer information of the provided peer | + +TODO: change when `peer-info` is deprecated to new pointer + +#### Example + +```js +peerStore.get(peerId) +// false +peerStore.addressBook.set(peerId, multiaddrs) +peerStore.protoBook.set(peerId, protocols) +peerStore.get(peerId) +// { +// MultiaddrInfos: [...], +// protocols: [...] +// } +``` + +### peerStore.peers + +Get all the stored information of every peer. + +`peerStore.peers` + +#### Returns + +| Type | Description | +|------|-------------| +| `Map` | Peer information of every peer | + +TODO: change when `peer-info` is deprecated to new pointer (breaking change) + +#### Example + +```js +for (let [peerIdString, peerInfo] of peerStore.peers.entries()) { + // peerInfo instance +} +``` + ### pubsub.getSubscribers Gets a list of the peer-ids that are subscribed to one topic. @@ -523,7 +877,7 @@ Gets a list of the peer-ids that are subscribed to one topic. | Type | Description | |------|-------------| -| `Array` | peer-id subscribed to the topic | +| `Array` | peer-id subscribed to the topic | #### Example @@ -541,7 +895,7 @@ Gets a list of topics the node is subscribed to. | Type | Description | |------|-------------| -| `Array` | topics the node is subscribed to | +| `Array` | topics the node is subscribed to | #### Example @@ -588,7 +942,7 @@ Subscribes the given handler to a pubsub topic. | Name | Type | Description | |------|------|-------------| | topic | `string` | topic to subscribe | -| handler | `function({ from: String, data: Buffer, seqno: Buffer, topicIDs: Array, signature: Buffer, key: Buffer })` | handler for new data on topic | +| handler | `function({ from: string, data: Buffer, seqno: Buffer, topicIDs: Array, signature: Buffer, key: Buffer })` | handler for new data on topic | #### Returns @@ -618,7 +972,7 @@ Unsubscribes the given handler from a pubsub topic. If no handler is provided, a | Name | Type | Description | |------|------|-------------| | topic | `string` | topic to unsubscribe | -| handler | `function()` | handler subscribed | +| handler | `function()` | handler subscribed | #### Returns @@ -787,9 +1141,9 @@ This event will be triggered anytime we are disconnected from another peer, rega - `dataReceived`: The stringified value of total incoming data for this stat. - `dataSent`: The stringified value of total outgoing data for this stat. - `movingAverages`: The properties are dependent on the configuration of the moving averages interval. Defaults are listed here. - - `['60000']`: The calculated moving average at a 1 minute interval. - - `['300000']`: The calculated moving average at a 5 minute interval. - - `['900000']`: The calculated moving average at a 15 minute interval. + - `['60000']`: The calculated moving average at a 1 minute interval. + - `['300000']`: The calculated moving average at a 5 minute interval. + - `['900000']`: The calculated moving average at a 15 minute interval. - `snapshot`: A getter that returns a clone of the raw stats. - `dataReceived`: A [`BigNumber`](https://github.com/MikeMcl/bignumber.js/) of the amount of incoming data - `dataSent`: A [`BigNumber`](https://github.com/MikeMcl/bignumber.js/) of the amount of outgoing data @@ -798,6 +1152,7 @@ This event will be triggered anytime we are disconnected from another peer, rega - `['300000']`: The [MovingAverage](https://www.npmjs.com/package/moving-averages) at a 5 minute interval. - `['900000']`: The [MovingAverage](https://www.npmjs.com/package/moving-averages) at a 15 minute interval. +[multiaddr-info]: https://github.com/libp2p/js-libp2p/tree/master/src/peer-store/address-book.js [cid]: https://github.com/multiformats/js-cid [connection]: https://github.com/libp2p/js-interfaces/tree/master/src/connection [multiaddr]: https://github.com/multiformats/js-multiaddr diff --git a/src/dialer/index.js b/src/dialer/index.js index 7638ffe572..de9a394394 100644 --- a/src/dialer/index.js +++ b/src/dialer/index.js @@ -5,7 +5,6 @@ const errCode = require('err-code') const TimeoutController = require('timeout-abort-controller') const anySignal = require('any-signal') const PeerId = require('peer-id') -const PeerInfo = require('peer-info') const debug = require('debug') const log = debug('libp2p:dialer') log.error = debug('libp2p:dialer:error') @@ -62,13 +61,13 @@ class Dialer { * The dial to the first address that is successfully able to upgrade a connection * will be used. * - * @param {PeerInfo|Multiaddr} peer The peer to dial + * @param {PeerId|Multiaddr} peerId The peer to dial * @param {object} [options] * @param {AbortSignal} [options.signal] An AbortController signal * @returns {Promise} */ - async connectToPeer (peer, options = {}) { - const dialTarget = this._createDialTarget(peer) + async connectToPeer (peerId, options = {}) { + const dialTarget = this._createDialTarget(peerId) if (dialTarget.addrs.length === 0) { throw errCode(new Error('The dial request has no addresses'), codes.ERR_NO_VALID_ADDRESSES) } @@ -100,7 +99,7 @@ class Dialer { * Creates a DialTarget. The DialTarget is used to create and track * the DialRequest to a given peer. * @private - * @param {PeerInfo|Multiaddr} peer A PeerId or Multiaddr + * @param {PeerId|Multiaddr} peer A PeerId or Multiaddr * @returns {DialTarget} */ _createDialTarget (peer) { @@ -111,7 +110,10 @@ class Dialer { addrs: [dialable] } } - const addrs = this.peerStore.multiaddrsForPeer(dialable) + + dialable.multiaddrs && this.peerStore.addressBook.add(dialable.id, Array.from(dialable.multiaddrs)) + const addrs = this.peerStore.addressBook.getMultiaddrsForPeer(dialable.id) + return { id: dialable.id.toB58String(), addrs @@ -179,21 +181,27 @@ class Dialer { this.tokens.push(token) } + /** + * PeerInfo object + * @typedef {Object} peerInfo + * @property {Multiaddr} multiaddr peer multiaddr. + * @property {PeerId} id peer id. + */ + /** * Converts the given `peer` into a `PeerInfo` or `Multiaddr`. * @static - * @param {PeerInfo|PeerId|Multiaddr|string} peer - * @returns {PeerInfo|Multiaddr} + * @param {PeerId|Multiaddr|string} peer + * @returns {peerInfo|Multiaddr} */ static getDialable (peer) { - if (PeerInfo.isPeerInfo(peer)) return peer if (typeof peer === 'string') { peer = multiaddr(peer) } - let addr + let addrs if (multiaddr.isMultiaddr(peer)) { - addr = peer + addrs = new Set([peer]) // TODO: after peer-info removal, a Set should not be needed try { peer = PeerId.createFromCID(peer.getPeerId()) } catch (err) { @@ -202,10 +210,12 @@ class Dialer { } if (PeerId.isPeerId(peer)) { - peer = new PeerInfo(peer) + peer = { + id: peer, + multiaddrs: addrs + } } - addr && peer.multiaddrs.add(addr) return peer } } diff --git a/src/get-peer-info.js b/src/get-peer-info.js index 01a6bc49f2..5b0748ea8e 100644 --- a/src/get-peer-info.js +++ b/src/get-peer-info.js @@ -38,7 +38,12 @@ function getPeerInfo (peer, peerStore) { addr && peer.multiaddrs.add(addr) - return peerStore ? peerStore.put(peer) : peer + if (peerStore) { + peerStore.addressBook.add(peer.id, peer.multiaddrs.toArray()) + peerStore.protoBook.add(peer.id, Array.from(peer.protocols)) + } + + return peer } /** diff --git a/src/identify/index.js b/src/identify/index.js index a7351ecbaf..c51400720c 100644 --- a/src/identify/index.js +++ b/src/identify/index.js @@ -7,7 +7,6 @@ const lp = require('it-length-prefixed') const pipe = require('it-pipe') const { collect, take, consume } = require('streaming-iterables') -const PeerInfo = require('peer-info') const PeerId = require('peer-id') const multiaddr = require('multiaddr') const { toBuffer } = require('it-buffer') @@ -28,39 +27,6 @@ const errCode = require('err-code') const { codes } = require('../errors') class IdentifyService { - /** - * Replaces the multiaddrs on the given `peerInfo`, - * with the provided `multiaddrs` - * @param {PeerInfo} peerInfo - * @param {Array|Array} multiaddrs - */ - static updatePeerAddresses (peerInfo, multiaddrs) { - if (multiaddrs && multiaddrs.length > 0) { - peerInfo.multiaddrs.clear() - multiaddrs.forEach(ma => { - try { - peerInfo.multiaddrs.add(ma) - } catch (err) { - log.error('could not add multiaddr', err) - } - }) - } - } - - /** - * Replaces the protocols on the given `peerInfo`, - * with the provided `protocols` - * @static - * @param {PeerInfo} peerInfo - * @param {Array} protocols - */ - static updatePeerProtocols (peerInfo, protocols) { - if (protocols && protocols.length > 0) { - peerInfo.protocols.clear() - protocols.forEach(proto => peerInfo.protocols.add(proto)) - } - } - /** * Takes the `addr` and converts it to a Multiaddr if possible * @param {Buffer|String} addr @@ -182,7 +148,7 @@ class IdentifyService { } = message const id = await PeerId.createFromPubKey(publicKey) - const peerInfo = new PeerInfo(id) + if (connection.remotePeer.toB58String() !== id.toB58String()) { throw errCode(new Error('identified peer does not match the expected peer'), codes.ERR_INVALID_PEER) } @@ -190,11 +156,10 @@ class IdentifyService { // Get the observedAddr if there is one observedAddr = IdentifyService.getCleanMultiaddr(observedAddr) - // Copy the listenAddrs and protocols - IdentifyService.updatePeerAddresses(peerInfo, listenAddrs) - IdentifyService.updatePeerProtocols(peerInfo, protocols) + // Update peers data in PeerStore + this.registrar.peerStore.addressBook.set(id, listenAddrs.map((addr) => multiaddr(addr))) + this.registrar.peerStore.protoBook.set(id, protocols) - this.registrar.peerStore.replace(peerInfo) // TODO: Track our observed address so that we can score it log('received observed address of %s', observedAddr) } @@ -274,20 +239,16 @@ class IdentifyService { return log.error('received invalid message', err) } - // Update the listen addresses - const peerInfo = new PeerInfo(connection.remotePeer) - + // Update peers data in PeerStore + const id = connection.remotePeer try { - IdentifyService.updatePeerAddresses(peerInfo, message.listenAddrs) + this.registrar.peerStore.addressBook.set(id, message.listenAddrs.map((addr) => multiaddr(addr))) } catch (err) { return log.error('received invalid listen addrs', err) } // Update the protocols - IdentifyService.updatePeerProtocols(peerInfo, message.protocols) - - // Update the peer in the PeerStore - this.registrar.peerStore.replace(peerInfo) + this.registrar.peerStore.protoBook.set(id, message.protocols) } } diff --git a/src/index.js b/src/index.js index 1cc15a366b..96a78644e5 100644 --- a/src/index.js +++ b/src/index.js @@ -60,7 +60,7 @@ class Libp2p extends EventEmitter { localPeer: this.peerInfo.id, metrics: this.metrics, onConnection: (connection) => { - const peerInfo = this.peerStore.put(new PeerInfo(connection.remotePeer), { silent: true }) + const peerInfo = new PeerInfo(connection.remotePeer) this.registrar.onConnect(peerInfo, connection) this.connectionManager.onConnect(connection) this.emit('peer:connect', peerInfo) @@ -292,7 +292,11 @@ class Libp2p extends EventEmitter { const dialable = Dialer.getDialable(peer) let connection if (PeerInfo.isPeerInfo(dialable)) { - this.peerStore.put(dialable, { silent: true }) + // TODO Inconsistency from: getDialable adds a set, while regular peerInfo uses a Multiaddr set + // This should be handled on `peer-info` removal + const multiaddrs = dialable.multiaddrs.toArray ? dialable.multiaddrs.toArray() : Array.from(dialable.multiaddrs) + this.peerStore.addressBook.add(dialable.id, multiaddrs) + connection = this.registrar.getConnection(dialable) } @@ -338,7 +342,7 @@ class Libp2p extends EventEmitter { async ping (peer) { const peerInfo = await getPeerInfo(peer, this.peerStore) - return ping(this, peerInfo) + return ping(this, peerInfo.id) } /** @@ -440,7 +444,10 @@ class Libp2p extends EventEmitter { log.error(new Error(codes.ERR_DISCOVERED_SELF)) return } - this.peerStore.put(peerInfo) + + // TODO: once we deprecate peer-info, we should only set if we have data + this.peerStore.addressBook.add(peerInfo.id, peerInfo.multiaddrs.toArray()) + this.peerStore.protoBook.set(peerInfo.id, Array.from(peerInfo.protocols)) } /** diff --git a/src/peer-store/README.md b/src/peer-store/README.md index d9d79fe56a..bb309a0ddf 100644 --- a/src/peer-store/README.md +++ b/src/peer-store/README.md @@ -1,3 +1,89 @@ -# Peerstore +# PeerStore -WIP \ No newline at end of file +Libp2p's PeerStore is responsible for keeping an updated register with the relevant information of the known peers. It should be the single source of truth for all peer data, where a subsystem can learn about peers' data and where someone can listen for updates. The PeerStore comprises four main components: `addressBook`, `keyBook`, `protocolBook` and `metadataBook`. + +The PeerStore manages the high level operations on its inner books. Moreover, the PeerStore should be responsible for notifying interested parties of relevant events, through its Event Emitter. + +## Data gathering + +Several libp2p subsystems will perform operations, which will gather relevant information about peers. Some operations might not have this as an end goal, but can also gather important data. + +In a libp2p node's life, it will discover peers through its discovery protocols. In a typical discovery protocol, addresses of the peer are discovered along with its peer id. Once this happens, the PeerStore should collect this information for future (or immediate) usage by other subsystems. When the information is stored, the PeerStore should inform interested parties of the peer discovered (`peer` event). + +Taking into account a different scenario, a peer might perform/receive a dial request to/from a unkwown peer. In such a scenario, the PeerStore must store the peer's multiaddr once a connection is established. + +After a connection is established with a peer, the Identify protocol will run automatically. A stream is created and peers exchange their information (Multiaddrs, running protocols and their public key). Once this information is obtained, it should be added to the PeerStore. In this specific case, as we are speaking to the source of truth, we should ensure the PeerStore is prioritizing these records. If the recorded `multiaddrs` or `protocols` have changed, interested parties must be informed via the `change:multiaddrs` or `change:protocols` events respectively. + +In the background, the Identify Service is also waiting for protocol change notifications of peers via the IdentifyPush protocol. Peers may leverage the `identify-push` message to communicate protocol changes to all connected peers, so that their PeerStore can be updated with the updated protocols. As the `identify-push` also sends complete and updated information, the data in the PeerStore can be replaced. + +(To consider: Should we not replace until we get to multiaddr confidence? we might loose true information as we will talk with older nodes on the network.) + +While it is currently not supported in js-libp2p, future iterations may also support the [IdentifyDelta protocol](https://github.com/libp2p/specs/pull/176). + +It is also possible to gather relevant information for peers from other protocols / subsystems. For instance, in `DHT` operations, nodes can exchange peer data as part of the `DHT` operation. In this case, we can learn additional information about a peer we already know. In this scenario the PeerStore should not replace the existing data it has, just add it. + +## Data Consumption + +When the PeerStore data is updated, this information might be important for different parties. + +Every time a peer needs to dial another peer, it is essential that it knows the multiaddrs used by the peer, in order to perform a successful dial to it. The same is true for pinging a peer. While the `AddressBook` is going to keep its data updated, it will also emit `change:multiaddrs` events so that subsystems/users interested in knowing these changes can be notified instead of polling the `AddressBook`. + +Everytime a peer starts/stops supporting a protocol, libp2p subsystems or users might need to act accordingly. `js-libp2p` registrar orchestrates known peers, established connections and protocol topologies. This way, once a protocol is supported for a peer, the topology of that protocol should be informed that a new peer may be used and the subsystem can decide if it should open a new stream with that peer or not. For these situations, the `ProtoBook` will emit `change:protocols` events whenever supported protocols of a peer change. + +## PeerStore implementation + +The PeerStore wraps four main components: `addressBook`, `keyBook`, `protocolBook` and `metadataBook`. Moreover, it provides a high level API for those components, as well as data events. + +### Components + +#### Address Book + +The `addressBook` keeps the known multiaddrs of a peer. The multiaddrs of each peer may change over time and the Address Book must account for this. + +`Map` + +A `peerId.toString()` identifier mapping to a `multiaddrInfo` object, which should have the following structure: + +```js +{ + multiaddr: +} +``` + +#### Key Book + +The `keyBook` tracks the keys of the peers. + +**Not Yet Implemented** + +#### Protocol Book + +The `protoBook` holds the identifiers of the protocols supported by each peer. The protocols supported by each peer are dynamic and will change over time. + +`Map>` + +A `peerId.toString()` identifier mapping to a `Set` of protocol identifier strings. + +#### Metadata Book + +**Not Yet Implemented** + +### API + +For the complete API documentation, you should check the [API.md](../../doc/API.md). + +Access to its underlying books: + +- `peerStore.protoBook.*` +- `peerStore.addressBook.*` + +### Events + +- `peer` - emitted when a new peer is added. +- `change:multiaadrs` - emitted when a known peer has a different set of multiaddrs. +- `change:protocols` - emitted when a known peer supports a different set of protocols. + +## Future Considerations + +- If multiaddr TTLs are added, the PeerStore may schedule jobs to delete all addresses that exceed the TTL to prevent AddressBook bloating +- Further API methods will probably need to be added in the context of multiaddr validity and confidence. diff --git a/src/peer-store/address-book.js b/src/peer-store/address-book.js new file mode 100644 index 0000000000..445d731e1e --- /dev/null +++ b/src/peer-store/address-book.js @@ -0,0 +1,214 @@ +'use strict' + +const errcode = require('err-code') +const debug = require('debug') +const log = debug('libp2p:peer-store:address-book') +log.error = debug('libp2p:peer-store:address-book:error') + +const multiaddr = require('multiaddr') +const PeerId = require('peer-id') +const PeerInfo = require('peer-info') + +const Book = require('./book') + +const { + ERR_INVALID_PARAMETERS +} = require('../errors') + +/** + * The AddressBook is responsible for keeping the known multiaddrs + * of a peer. + */ +class AddressBook extends Book { + /** + * MultiaddrInfo object + * @typedef {Object} MultiaddrInfo + * @property {Multiaddr} multiaddr peer multiaddr. + */ + + /** + * @constructor + * @param {EventEmitter} peerStore + */ + constructor (peerStore) { + /** + * PeerStore Event emitter, used by the AddressBook to emit: + * "peer" - emitted when a peer is discovered by the node. + * "change:multiaddrs" - emitted when the known multiaddrs of a peer change. + */ + super(peerStore, 'change:multiaddrs', 'multiaddrs') + + /** + * Map known peers to their known multiaddrs. + * @type {Map>} + */ + this.data = new Map() + } + + /** + * Set known addresses of a provided peer. + * @override + * @param {PeerId} peerId + * @param {Array} addresses + * @returns {AddressBook} + */ + set (peerId, addresses) { + if (!PeerId.isPeerId(peerId)) { + log.error('peerId must be an instance of peer-id to store data') + throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS) + } + + const multiaddrInfos = this._toMultiaddrInfos(addresses) + const id = peerId.toB58String() + const rec = this.data.get(id) + + // Not replace multiaddrs + if (!multiaddrInfos.length) { + return this + } + + // Already knows the peer + if (rec && rec.length === multiaddrInfos.length) { + const intersection = rec.filter((mi) => multiaddrInfos.some((newMi) => mi.multiaddr.equals(newMi.multiaddr))) + + // Are new addresses equal to the old ones? + // If yes, no changes needed! + if (intersection.length === rec.length) { + log(`the addresses provided to store are equal to the already stored for ${id}`) + return this + } + } + + this.data.set(id, multiaddrInfos) + log(`stored provided multiaddrs for ${id}`) + + // TODO: Remove peerInfo and its usage on peer-info deprecate + const peerInfo = new PeerInfo(peerId) + multiaddrInfos.forEach((mi) => peerInfo.multiaddrs.add(mi.multiaddr)) + + // Notify the existance of a new peer + if (!rec) { + // this._ps.emit('peer', peerId) + this._ps.emit('peer', peerInfo) + } + + this._ps.emit('change:multiaddrs', { + peerId, + peerInfo, + multiaddrs: multiaddrInfos.map((mi) => mi.multiaddr) + }) + + return this + } + + /** + * Add known addresses of a provided peer. + * If the peer is not known, it is set with the given addresses. + * @override + * @param {PeerId} peerId + * @param {Array} addresses + * @returns {AddressBook} + */ + add (peerId, addresses) { + if (!PeerId.isPeerId(peerId)) { + log.error('peerId must be an instance of peer-id to store data') + throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS) + } + + const multiaddrInfos = this._toMultiaddrInfos(addresses) + const id = peerId.toB58String() + const rec = this.data.get(id) + + // Add recorded uniquely to the new array (Union) + rec && rec.forEach((mi) => { + if (!multiaddrInfos.find(r => r.multiaddr.equals(mi.multiaddr))) { + multiaddrInfos.push(mi) + } + }) + + // If the recorded length is equal to the new after the unique union + // The content is the same, no need to update. + if (rec && rec.length === multiaddrInfos.length) { + log(`the addresses provided to store are already stored for ${id}`) + return this + } + + this.data.set(id, multiaddrInfos) + + log(`added provided multiaddrs for ${id}`) + + // TODO: Remove peerInfo and its usage on peer-info deprecate + const peerInfo = new PeerInfo(peerId) + multiaddrInfos.forEach((mi) => peerInfo.multiaddrs.add(mi.multiaddr)) + + this._ps.emit('change:multiaddrs', { + peerId, + peerInfo, + multiaddrs: multiaddrInfos.map((mi) => mi.multiaddr) + }) + + // Notify the existance of a new peer + if (!rec) { + // this._ps.emit('peer', peerId) + this._ps.emit('peer', peerInfo) + } + + return this + } + + /** + * Transforms received multiaddrs into MultiaddrInfo. + * @param {Array} addresses + * @returns {Array} + */ + _toMultiaddrInfos (addresses) { + if (!addresses) { + log.error('addresses must be provided to store data') + throw errcode(new Error('addresses must be provided'), ERR_INVALID_PARAMETERS) + } + + // create MultiaddrInfo for each address + const multiaddrInfos = [] + addresses.forEach((addr) => { + if (!multiaddr.isMultiaddr(addr)) { + log.error(`multiaddr ${addr} must be an instance of multiaddr`) + throw errcode(new Error(`multiaddr ${addr} must be an instance of multiaddr`), ERR_INVALID_PARAMETERS) + } + + multiaddrInfos.push({ + multiaddr: addr + }) + }) + + return multiaddrInfos + } + + /** + * Get the known multiaddrs for a given peer. All returned multiaddrs + * will include the encapsulated `PeerId` of the peer. + * @param {PeerId} peerId + * @returns {Array} + */ + getMultiaddrsForPeer (peerId) { + if (!PeerId.isPeerId(peerId)) { + throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS) + } + + const record = this.data.get(peerId.toB58String()) + + if (!record) { + return undefined + } + + return record.map((multiaddrInfo) => { + const addr = multiaddrInfo.multiaddr + + const idString = addr.getPeerId() + if (idString && idString === peerId.toB58String()) return addr + + return addr.encapsulate(`/p2p/${peerId.toB58String()}`) + }) + } +} + +module.exports = AddressBook diff --git a/src/peer-store/book.js b/src/peer-store/book.js new file mode 100644 index 0000000000..32456b7c13 --- /dev/null +++ b/src/peer-store/book.js @@ -0,0 +1,87 @@ +'use strict' + +const errcode = require('err-code') +const PeerId = require('peer-id') +const PeerInfo = require('peer-info') + +const { + ERR_INVALID_PARAMETERS +} = require('../errors') + +/** + * The Book is the skeleton for the PeerStore books. + */ +class Book { + constructor (peerStore, eventName, eventProperty) { + this._ps = peerStore + this.eventName = eventName + this.eventProperty = eventProperty + + /** + * Map known peers to their data. + * @type {Map} + */ + this.data = new Map() + } + + /** + * Set known data of a provided peer. + * @param {PeerId} peerId + * @param {Array|Data} data + */ + set (peerId, data) { + throw errcode(new Error('set must be implemented by the subclass'), 'ERR_NOT_IMPLEMENTED') + } + + /** + * Add known data of a provided peer. + * @param {PeerId} peerId + * @param {Array|Data} data + */ + add (peerId, data) { + throw errcode(new Error('set must be implemented by the subclass'), 'ERR_NOT_IMPLEMENTED') + } + + /** + * Get the known data of a provided peer. + * @param {PeerId} peerId + * @returns {Array} + */ + get (peerId) { + if (!PeerId.isPeerId(peerId)) { + throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS) + } + + const rec = this.data.get(peerId.toB58String()) + + return rec ? [...rec] : undefined + } + + /** + * Deletes the provided peer from the book. + * @param {PeerId} peerId + * @returns {boolean} + */ + delete (peerId) { + if (!PeerId.isPeerId(peerId)) { + throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS) + } + + if (!this.data.delete(peerId.toB58String())) { + return false + } + + // TODO: Remove peerInfo and its usage on peer-info deprecate + const peerInfo = new PeerInfo(peerId) + + this._ps.emit(this.eventName, { + peerId, + peerInfo, + [this.eventProperty]: [] + }) + + return true + } +} + +module.exports = Book diff --git a/src/peer-store/index.js b/src/peer-store/index.js index 494d613d13..597b69e178 100644 --- a/src/peer-store/index.js +++ b/src/peer-store/index.js @@ -9,244 +9,212 @@ const { EventEmitter } = require('events') const PeerId = require('peer-id') const PeerInfo = require('peer-info') + +const AddressBook = require('./address-book') +const ProtoBook = require('./proto-book') + const { ERR_INVALID_PARAMETERS } = require('../errors') /** - * Responsible for managing known peers, as well as their addresses and metadata - * @fires PeerStore#peer Emitted when a peer is connected to this node - * @fires PeerStore#change:protocols - * @fires PeerStore#change:multiaddrs + * Responsible for managing known peers, as well as their addresses, protocols and metadata. + * @fires PeerStore#peer Emitted when a new peer is added. + * @fires PeerStore#change:protocols Emitted when a known peer supports a different set of protocols. + * @fires PeerStore#change:multiaddrs Emitted when a known peer has a different set of multiaddrs. */ class PeerStore extends EventEmitter { + /** + * PeerInfo object + * @typedef {Object} peerInfo + * @property {Array} multiaddrsInfos peer's information of the multiaddrs. + * @property {Array} protocols peer's supported protocols. + */ + constructor () { super() /** - * Map of peers - * - * @type {Map} + * AddressBook containing a map of peerIdStr to multiaddrsInfo */ - this.peers = new Map() + this.addressBook = new AddressBook(this) - // TODO: Track ourselves. We should split `peerInfo` up into its pieces so we get better - // control and observability. This will be the initial step for removing PeerInfo - // https://github.com/libp2p/go-libp2p-core/blob/master/peerstore/peerstore.go - // this.addressBook = new Map() - // this.protoBook = new Map() + /** + * ProtoBook containing a map of peerIdStr to supported protocols. + */ + this.protoBook = new ProtoBook(this) } + // TODO: Temporary adapter for modules using PeerStore + // This should be removed under a breaking change /** - * Stores the peerInfo of a new peer. - * If already exist, its info is updated. If `silent` is set to - * true, no 'peer' event will be emitted. This can be useful if you - * are already in the process of dialing the peer. The peer is technically - * known, but may not have been added to the PeerStore yet. + * Stores the peerInfo of a new peer on each book. * @param {PeerInfo} peerInfo * @param {object} [options] - * @param {boolean} [options.silent] (Default=false) + * @param {boolean} [options.replace = true] * @return {PeerInfo} */ - put (peerInfo, options = { silent: false }) { - if (!PeerInfo.isPeerInfo(peerInfo)) { - throw errcode(new Error('peerInfo must be an instance of peer-info'), ERR_INVALID_PARAMETERS) - } + put (peerInfo, options) { + const multiaddrs = peerInfo.multiaddrs.toArray() + const protocols = Array.from(peerInfo.protocols || new Set()) - let peer - // Already know the peer? - if (this.has(peerInfo.id)) { - peer = this.update(peerInfo) - } else { - peer = this.add(peerInfo) + this.addressBook.set(peerInfo.id, multiaddrs, options) + this.protoBook.set(peerInfo.id, protocols, options) - // Emit the peer if silent = false - !options.silent && this.emit('peer', peerInfo) - } - return peer - } + const peer = this.find(peerInfo.id) + const pInfo = new PeerInfo(peerInfo.id) - /** - * Add a new peer to the store. - * @param {PeerInfo} peerInfo - * @return {PeerInfo} - */ - add (peerInfo) { - if (!PeerInfo.isPeerInfo(peerInfo)) { - throw errcode(new Error('peerInfo must be an instance of peer-info'), ERR_INVALID_PARAMETERS) + if (!peer) { + return pInfo } - // Create new instance and add values to it - const newPeerInfo = new PeerInfo(peerInfo.id) - - peerInfo.multiaddrs.forEach((ma) => newPeerInfo.multiaddrs.add(ma)) - peerInfo.protocols.forEach((p) => newPeerInfo.protocols.add(p)) - - const connectedMa = peerInfo.isConnected() - connectedMa && newPeerInfo.connect(connectedMa) - - const peerProxy = new Proxy(newPeerInfo, { - set: (obj, prop, value) => { - if (prop === 'multiaddrs') { - this.emit('change:multiaddrs', { - peerInfo: obj, - multiaddrs: value.toArray() - }) - } else if (prop === 'protocols') { - this.emit('change:protocols', { - peerInfo: obj, - protocols: Array.from(value) - }) - } - return Reflect.set(...arguments) - } - }) + peer.protocols.forEach((p) => pInfo.protocols.add(p)) + peer.multiaddrInfos.forEach((mi) => pInfo.multiaddrs.add(mi.multiaddr)) - this.peers.set(peerInfo.id.toB58String(), peerProxy) - return peerProxy + return pInfo } + // TODO: Temporary adapter for modules using PeerStore + // This should be removed under a breaking change /** - * Updates an already known peer. - * @param {PeerInfo} peerInfo - * @return {PeerInfo} - */ - update (peerInfo) { - if (!PeerInfo.isPeerInfo(peerInfo)) { - throw errcode(new Error('peerInfo must be an instance of peer-info'), ERR_INVALID_PARAMETERS) - } - - const id = peerInfo.id.toB58String() - const recorded = this.peers.get(id) - - // pass active connection state - const ma = peerInfo.isConnected() - if (ma) { - recorded.connect(ma) - } - - // Verify new multiaddrs - // TODO: better track added and removed multiaddrs - const multiaddrsIntersection = [ - ...recorded.multiaddrs.toArray() - ].filter((m) => peerInfo.multiaddrs.has(m)) - - if (multiaddrsIntersection.length !== peerInfo.multiaddrs.size || - multiaddrsIntersection.length !== recorded.multiaddrs.size) { - for (const ma of peerInfo.multiaddrs.toArray()) { - recorded.multiaddrs.add(ma) - } - - this.emit('change:multiaddrs', { - peerInfo: recorded, - multiaddrs: recorded.multiaddrs.toArray() - }) - } - - // Update protocols - // TODO: better track added and removed protocols - const protocolsIntersection = new Set( - [...recorded.protocols].filter((p) => peerInfo.protocols.has(p)) - ) - - if (protocolsIntersection.size !== peerInfo.protocols.size || - protocolsIntersection.size !== recorded.protocols.size) { - for (const protocol of peerInfo.protocols) { - recorded.protocols.add(protocol) - } - - this.emit('change:protocols', { - peerInfo: recorded, - protocols: Array.from(recorded.protocols) - }) - } - - // Add the public key if missing - if (!recorded.id.pubKey && peerInfo.id.pubKey) { - recorded.id.pubKey = peerInfo.id.pubKey - } - - return recorded - } - - /** - * Get the info to the given id. - * @param {PeerId|string} peerId b58str id + * Get the info of the given id. + * @param {peerId} peerId * @returns {PeerInfo} */ get (peerId) { - // TODO: deprecate this and just accept `PeerId` instances - if (PeerId.isPeerId(peerId)) { - peerId = peerId.toB58String() - } + const peer = this.find(peerId) - return this.peers.get(peerId) + const pInfo = new PeerInfo(peerId) + peer.protocols.forEach((p) => pInfo.protocols.add(p)) + peer.multiaddrInfos.forEach((mi) => pInfo.multiaddrs.add(mi.multiaddr)) + + return pInfo } + // TODO: Temporary adapter for modules using PeerStore + // This should be removed under a breaking change /** * Has the info to the given id. - * @param {PeerId|string} peerId b58str id + * @param {PeerId} peerId * @returns {boolean} */ has (peerId) { - // TODO: deprecate this and just accept `PeerId` instances - if (PeerId.isPeerId(peerId)) { - peerId = peerId.toB58String() - } - - return this.peers.has(peerId) + return Boolean(this.find(peerId)) } + // TODO: Temporary adapter for modules using PeerStore + // This should be removed under a breaking change /** - * Removes the Peer with the matching `peerId` from the PeerStore - * @param {PeerId|string} peerId b58str id + * Removes the peer provided. + * @param {PeerId} peerId * @returns {boolean} true if found and removed */ remove (peerId) { - // TODO: deprecate this and just accept `PeerId` instances - if (PeerId.isPeerId(peerId)) { - peerId = peerId.toB58String() - } - - return this.peers.delete(peerId) + return this.delete(peerId) } + // TODO: Temporary adapter for modules using PeerStore + // This should be removed under a breaking change /** * Completely replaces the existing peers metadata with the given `peerInfo` * @param {PeerInfo} peerInfo * @returns {void} */ replace (peerInfo) { - if (!PeerInfo.isPeerInfo(peerInfo)) { - throw errcode(new Error('peerInfo must be an instance of peer-info'), ERR_INVALID_PARAMETERS) - } - - this.remove(peerInfo.id.toB58String()) - this.add(peerInfo) - - // This should be cleaned up in PeerStore v2 - this.emit('change:multiaddrs', { - peerInfo, - multiaddrs: peerInfo.multiaddrs.toArray() - }) - this.emit('change:protocols', { - peerInfo, - protocols: Array.from(peerInfo.protocols) - }) + this.put(peerInfo) } + // TODO: Temporary adapter for modules using PeerStore + // This should be removed under a breaking change /** * Returns the known multiaddrs for a given `PeerInfo`. All returned multiaddrs * will include the encapsulated `PeerId` of the peer. - * @param {PeerInfo} peer + * @param {PeerInfo} peerInfo * @returns {Array} */ - multiaddrsForPeer (peer) { - return this.put(peer, true).multiaddrs.toArray().map(addr => { - const idString = addr.getPeerId() - if (idString && idString === peer.id.toB58String()) return addr - return addr.encapsulate(`/p2p/${peer.id.toB58String()}`) - }) + multiaddrsForPeer (peerInfo) { + return this.addressBook.getMultiaddrsForPeer(peerInfo.id) + } + + /** + * Get all the stored information of every peer. + * @returns {Map} + */ + get peers () { + const peerInfos = new Map() + + // AddressBook + for (const [idStr, multiaddrInfos] of this.addressBook.data.entries()) { + // TODO: Remove peerInfo and its usage on peer-info deprecate + const peerInfo = new PeerInfo(PeerId.createFromCID(idStr)) + + multiaddrInfos.forEach((mi) => peerInfo.multiaddrs.add((mi.multiaddr))) + + const protocols = this.protoBook.data.get(idStr) || [] + protocols.forEach((p) => peerInfo.protocols.add(p)) + + peerInfos.set(idStr, peerInfo) + // TODO + // peerInfos.set(idStr, { + // id: PeerId.createFromCID(idStr), + // multiaddrInfos, + // protocols: this.protoBook.data.get(idStr) || [] + // }) + } + + // ProtoBook + for (const [idStr, protocols] of this.protoBook.data.entries()) { + // TODO: Remove peerInfo and its usage on peer-info deprecate + const peerInfo = peerInfos.get(idStr) + + if (!peerInfo) { + const peerInfo = new PeerInfo(PeerId.createFromCID(idStr)) + + protocols.forEach((p) => peerInfo.protocols.add(p)) + peerInfos.set(idStr, peerInfo) + // peerInfos.set(idStr, { + // id: PeerId.createFromCID(idStr), + // multiaddrInfos: [], + // protocols: protocols + // }) + } + } + + return peerInfos + } + + /** + * Delete the information of the given peer in every book. + * @param {PeerId} peerId + * @returns {boolean} true if found and removed + */ + delete (peerId) { + const addressesDeleted = this.addressBook.delete(peerId) + const protocolsDeleted = this.protoBook.delete(peerId) + return addressesDeleted || protocolsDeleted + } + + /** + * Find the stored information of a given peer. + * @param {PeerId} peerId + * @returns {peerInfo} + */ + find (peerId) { + if (!PeerId.isPeerId(peerId)) { + throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS) + } + + const multiaddrInfos = this.addressBook.get(peerId) + const protocols = this.protoBook.get(peerId) + + if (!multiaddrInfos && !protocols) { + return undefined + } + + return { + multiaddrInfos: multiaddrInfos || [], + protocols: protocols || [] + } } } diff --git a/src/peer-store/proto-book.js b/src/peer-store/proto-book.js new file mode 100644 index 0000000000..a5c5867ee8 --- /dev/null +++ b/src/peer-store/proto-book.js @@ -0,0 +1,137 @@ +'use strict' + +const errcode = require('err-code') +const debug = require('debug') +const log = debug('libp2p:peer-store:proto-book') +log.error = debug('libp2p:peer-store:proto-book:error') + +const PeerId = require('peer-id') +const PeerInfo = require('peer-info') + +const Book = require('./book') + +const { + ERR_INVALID_PARAMETERS +} = require('../errors') + +/** + * The ProtoBook is responsible for keeping the known supported + * protocols of a peer. + * @fires ProtoBook#change:protocols + */ +class ProtoBook extends Book { + /** + * @constructor + * @param {EventEmitter} peerStore + */ + constructor (peerStore) { + /** + * PeerStore Event emitter, used by the ProtoBook to emit: + * "change:protocols" - emitted when the known protocols of a peer change. + */ + super(peerStore, 'change:protocols', 'protocols') + + /** + * Map known peers to their known protocols. + * @type {Map>} + */ + this.data = new Map() + } + + /** + * Set known protocols of a provided peer. + * If the peer was not known before, it will be added. + * @override + * @param {PeerId} peerId + * @param {Array} protocols + * @returns {ProtoBook} + */ + set (peerId, protocols) { + if (!PeerId.isPeerId(peerId)) { + log.error('peerId must be an instance of peer-id to store data') + throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS) + } + + if (!protocols) { + log.error('protocols must be provided to store data') + throw errcode(new Error('protocols must be provided'), ERR_INVALID_PARAMETERS) + } + + const id = peerId.toB58String() + const recSet = this.data.get(id) + const newSet = new Set(protocols) + + const isSetEqual = (a, b) => a.size === b.size && [...a].every(value => b.has(value)) + + // Already knows the peer and the recorded protocols are the same? + // If yes, no changes needed! + if (recSet && isSetEqual(recSet, newSet)) { + log(`the protocols provided to store are equal to the already stored for ${id}`) + return this + } + + this.data.set(id, newSet) + log(`stored provided protocols for ${id}`) + + // TODO: Remove peerInfo and its usage on peer-info deprecate + const peerInfo = new PeerInfo(peerId) + protocols.forEach((p) => peerInfo.protocols.add(p)) + + this._ps.emit('change:protocols', { + peerId, + peerInfo, + protocols + }) + + return this + } + + /** + * Adds known protocols of a provided peer. + * If the peer was not known before, it will be added. + * @override + * @param {PeerId} peerId + * @param {Array} protocols + * @returns {ProtoBook} + */ + add (peerId, protocols) { + if (!PeerId.isPeerId(peerId)) { + log.error('peerId must be an instance of peer-id to store data') + throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS) + } + + if (!protocols) { + log.error('protocols must be provided to store data') + throw errcode(new Error('protocols must be provided'), ERR_INVALID_PARAMETERS) + } + + const id = peerId.toB58String() + const recSet = this.data.get(id) || new Set() + const newSet = new Set([...recSet, ...protocols]) // Set Union + + // Any new protocol added? + if (recSet.size === newSet.size) { + log(`the protocols provided to store are already stored for ${id}`) + return this + } + + protocols = [...newSet] + + this.data.set(id, newSet) + log(`added provided protocols for ${id}`) + + // TODO: Remove peerInfo and its usage on peer-info deprecate + const peerInfo = new PeerInfo(peerId) + protocols.forEach((p) => peerInfo.protocols.add(p)) + + this._ps.emit('change:protocols', { + peerId, + peerInfo, + protocols + }) + + return this + } +} + +module.exports = ProtoBook diff --git a/src/ping/index.js b/src/ping/index.js index d679a5c102..8590323056 100644 --- a/src/ping/index.js +++ b/src/ping/index.js @@ -15,11 +15,11 @@ const { PROTOCOL, PING_LENGTH } = require('./constants') /** * Ping a given peer and wait for its response, getting the operation latency. * @param {Libp2p} node - * @param {PeerInfo} peer + * @param {PeerId} peer * @returns {Promise} */ async function ping (node, peer) { - log('dialing %s to %s', PROTOCOL, peer.id.toB58String()) + log('dialing %s to %s', PROTOCOL, peer.toB58String()) const { stream } = await node.dialProtocol(peer, PROTOCOL) diff --git a/src/registrar.js b/src/registrar.js index cc24548516..2aa6bcf85e 100644 --- a/src/registrar.js +++ b/src/registrar.js @@ -10,7 +10,6 @@ const { } = require('./errors') const Topology = require('libp2p-interfaces/src/topology') const { Connection } = require('libp2p-interfaces/src/connection') -const PeerInfo = require('peer-info') /** * Responsible for notifying registered protocols of events in the network. @@ -22,6 +21,7 @@ class Registrar { * @constructor */ constructor ({ peerStore }) { + // Used on topology to listen for protocol changes this.peerStore = peerStore /** @@ -74,9 +74,11 @@ class Registrar { * @returns {void} */ onConnect (peerInfo, conn) { - if (!PeerInfo.isPeerInfo(peerInfo)) { - throw errcode(new Error('peerInfo must be an instance of peer-info'), ERR_INVALID_PARAMETERS) - } + // TODO: This is not a `peer-info` instance anymore, but an object with the data. + // This can be modified to `peer-id` though, once `peer-info` is deprecated. + // if (!PeerInfo.isPeerInfo(peerInfo)) { + // throw errcode(new Error('peerInfo must be an instance of peer-info'), ERR_INVALID_PARAMETERS) + // } if (!Connection.isConnection(conn)) { throw errcode(new Error('conn must be an instance of interface-connection'), ERR_INVALID_PARAMETERS) @@ -101,9 +103,11 @@ class Registrar { * @returns {void} */ onDisconnect (peerInfo, connection, error) { - if (!PeerInfo.isPeerInfo(peerInfo)) { - throw errcode(new Error('peerInfo must be an instance of peer-info'), ERR_INVALID_PARAMETERS) - } + // TODO: This is not a `peer-info` instance anymore, but an object with the data. + // This can be modified to `peer-id` though, once `peer-info` is deprecated. + // if (!PeerInfo.isPeerInfo(peerInfo)) { + // throw errcode(new Error('peerInfo must be an instance of peer-info'), ERR_INVALID_PARAMETERS) + // } const id = peerInfo.id.toB58String() let storedConn = this.connections.get(id) @@ -126,9 +130,11 @@ class Registrar { * @returns {Connection} */ getConnection (peerInfo) { - if (!PeerInfo.isPeerInfo(peerInfo)) { - throw errcode(new Error('peerInfo must be an instance of peer-info'), ERR_INVALID_PARAMETERS) - } + // TODO: This is not a `peer-info` instance anymore, but an object with the data. + // This can be modified to `peer-id` though, once `peer-info` is deprecated. + // if (!PeerInfo.isPeerInfo(peerInfo)) { + // throw errcode(new Error('peerInfo must be an instance of peer-info'), ERR_INVALID_PARAMETERS) + // } const connections = this.connections.get(peerInfo.id.toB58String()) // Return the first, open connection diff --git a/src/upgrader.js b/src/upgrader.js index 25cb6d3f11..ad2391971f 100644 --- a/src/upgrader.js +++ b/src/upgrader.js @@ -317,7 +317,7 @@ class Upgrader { * Attempts to encrypt the incoming `connection` with the provided `cryptos`. * @private * @async - * @param {PeerId} localPeer The initiators PeerInfo + * @param {PeerId} localPeer The initiators PeerId * @param {*} connection * @param {Map} cryptos * @returns {CryptoResult} An encrypted connection, remote peer `PeerId` and the protocol of the `Crypto` used @@ -346,7 +346,7 @@ class Upgrader { * The first `Crypto` module to succeed will be used * @private * @async - * @param {PeerId} localPeer The initiators PeerInfo + * @param {PeerId} localPeer The initiators PeerId * @param {*} connection * @param {PeerId} remotePeerId * @param {Map} cryptos diff --git a/test/content-routing/dht/operation.node.js b/test/content-routing/dht/operation.node.js index 10ae5504be..3ce8f411d5 100644 --- a/test/content-routing/dht/operation.node.js +++ b/test/content-routing/dht/operation.node.js @@ -44,7 +44,8 @@ describe('DHT subsystem operates correctly', () => { remoteLibp2p.start() ]) - remAddr = libp2p.peerStore.multiaddrsForPeer(remotePeerInfo)[0] + libp2p.peerStore.addressBook.set(remotePeerInfo.id, [remoteListenAddr]) + remAddr = libp2p.peerStore.addressBook.getMultiaddrsForPeer(remotePeerInfo.id)[0] }) afterEach(() => Promise.all([ @@ -68,7 +69,6 @@ describe('DHT subsystem operates correctly', () => { const value = Buffer.from('world') await libp2p.dialProtocol(remAddr, subsystemMulticodecs) - await Promise.all([ pWaitFor(() => libp2p._dht.routingTable.size === 1), pWaitFor(() => remoteLibp2p._dht.routingTable.size === 1) @@ -99,7 +99,8 @@ describe('DHT subsystem operates correctly', () => { await libp2p.start() await remoteLibp2p.start() - remAddr = libp2p.peerStore.multiaddrsForPeer(remotePeerInfo)[0] + libp2p.peerStore.addressBook.set(remotePeerInfo.id, [remoteListenAddr]) + remAddr = libp2p.peerStore.addressBook.getMultiaddrsForPeer(remotePeerInfo.id)[0] }) afterEach(() => Promise.all([ diff --git a/test/dialing/direct.node.js b/test/dialing/direct.node.js index d7b3499d95..01bb2360e8 100644 --- a/test/dialing/direct.node.js +++ b/test/dialing/direct.node.js @@ -100,7 +100,10 @@ describe('Dialing (direct, TCP)', () => { const dialer = new Dialer({ transportManager: localTM, peerStore: { - multiaddrsForPeer: () => [remoteAddr] + addressBook: { + add: () => {}, + getMultiaddrsForPeer: () => [remoteAddr] + } } }) const peerId = await PeerId.createFromJSON(Peers[0]) @@ -121,7 +124,7 @@ describe('Dialing (direct, TCP)', () => { const peerId = await PeerId.createFromJSON(Peers[0]) const peerInfo = new PeerInfo(peerId) peerInfo.multiaddrs.add(remoteAddr) - peerStore.put(peerInfo) + peerStore.addressBook.set(peerInfo.id, peerInfo.multiaddrs.toArray()) const connection = await dialer.connectToPeer(peerInfo) expect(connection).to.exist() @@ -132,7 +135,10 @@ describe('Dialing (direct, TCP)', () => { const dialer = new Dialer({ transportManager: localTM, peerStore: { - multiaddrsForPeer: () => [unsupportedAddr] + addressBook: { + add: () => {}, + getMultiaddrsForPeer: () => [unsupportedAddr] + } } }) const peerId = await PeerId.createFromJSON(Peers[0]) @@ -173,7 +179,10 @@ describe('Dialing (direct, TCP)', () => { transportManager: localTM, concurrency: 2, peerStore: { - multiaddrsForPeer: () => addrs + addressBook: { + add: () => {}, + getMultiaddrsForPeer: () => addrs + } } }) diff --git a/test/dialing/direct.spec.js b/test/dialing/direct.spec.js index cbf47fc132..df24983ce7 100644 --- a/test/dialing/direct.spec.js +++ b/test/dialing/direct.spec.js @@ -87,7 +87,10 @@ describe('Dialing (direct, WebSockets)', () => { const dialer = new Dialer({ transportManager: localTM, peerStore: { - multiaddrsForPeer: () => [remoteAddr] + addressBook: { + add: () => {}, + getMultiaddrsForPeer: () => [remoteAddr] + } } }) @@ -100,7 +103,10 @@ describe('Dialing (direct, WebSockets)', () => { const dialer = new Dialer({ transportManager: localTM, peerStore: { - multiaddrsForPeer: () => [remoteAddr] + addressBook: { + add: () => {}, + getMultiaddrsForPeer: () => [remoteAddr] + } } }) @@ -121,7 +127,10 @@ describe('Dialing (direct, WebSockets)', () => { const dialer = new Dialer({ transportManager: localTM, peerStore: { - multiaddrsForPeer: () => [remoteAddr] + addressBook: { + add: () => {}, + getMultiaddrsForPeer: () => [remoteAddr] + } } }) const peerId = await PeerId.createFromJSON(Peers[0]) @@ -135,7 +144,10 @@ describe('Dialing (direct, WebSockets)', () => { const dialer = new Dialer({ transportManager: localTM, peerStore: { - multiaddrsForPeer: () => [unsupportedAddr] + addressBook: { + set: () => {}, + getMultiaddrsForPeer: () => [unsupportedAddr] + } } }) const peerId = await PeerId.createFromJSON(Peers[0]) @@ -150,7 +162,10 @@ describe('Dialing (direct, WebSockets)', () => { transportManager: localTM, timeout: 50, peerStore: { - multiaddrsForPeer: () => [remoteAddr] + addressBook: { + add: () => {}, + getMultiaddrsForPeer: () => [remoteAddr] + } } }) sinon.stub(localTM, 'dial').callsFake(async (addr, options) => { @@ -172,7 +187,10 @@ describe('Dialing (direct, WebSockets)', () => { transportManager: localTM, concurrency: 2, peerStore: { - multiaddrsForPeer: () => [remoteAddr, remoteAddr, remoteAddr] + addressBook: { + set: () => {}, + getMultiaddrsForPeer: () => [remoteAddr, remoteAddr, remoteAddr] + } } }) @@ -208,7 +226,10 @@ describe('Dialing (direct, WebSockets)', () => { transportManager: localTM, concurrency: 2, peerStore: { - multiaddrsForPeer: () => [remoteAddr, remoteAddr, remoteAddr] + addressBook: { + set: () => {}, + getMultiaddrsForPeer: () => [remoteAddr, remoteAddr, remoteAddr] + } } }) @@ -316,7 +337,7 @@ describe('Dialing (direct, WebSockets)', () => { }) sinon.spy(libp2p.dialer, 'connectToPeer') - sinon.spy(libp2p.peerStore, 'put') + sinon.spy(libp2p.peerStore.addressBook, 'add') const connection = await libp2p.dial(remoteAddr) expect(connection).to.exist() @@ -325,7 +346,7 @@ describe('Dialing (direct, WebSockets)', () => { expect(protocol).to.equal('/echo/1.0.0') await connection.close() expect(libp2p.dialer.connectToPeer.callCount).to.equal(1) - expect(libp2p.peerStore.put.callCount).to.be.at.least(1) + expect(libp2p.peerStore.addressBook.add.callCount).to.be.at.least(1) }) it('should run identify automatically after connecting', async () => { @@ -339,19 +360,22 @@ describe('Dialing (direct, WebSockets)', () => { }) sinon.spy(libp2p.identifyService, 'identify') - sinon.spy(libp2p.peerStore, 'replace') sinon.spy(libp2p.upgrader, 'onConnection') const connection = await libp2p.dial(remoteAddr) expect(connection).to.exist() + sinon.spy(libp2p.peerStore.addressBook, 'set') + sinon.spy(libp2p.peerStore.protoBook, 'set') + // Wait for onConnection to be called await pWaitFor(() => libp2p.upgrader.onConnection.callCount === 1) expect(libp2p.identifyService.identify.callCount).to.equal(1) await libp2p.identifyService.identify.firstCall.returnValue - expect(libp2p.peerStore.replace.callCount).to.equal(1) + expect(libp2p.peerStore.addressBook.set.callCount).to.equal(1) + expect(libp2p.peerStore.protoBook.set.callCount).to.equal(1) }) it('should be able to use hangup to close connections', async () => { diff --git a/test/dialing/relay.node.js b/test/dialing/relay.node.js index f9c23e7252..33fa6a52c1 100644 --- a/test/dialing/relay.node.js +++ b/test/dialing/relay.node.js @@ -12,6 +12,8 @@ const multiaddr = require('multiaddr') const { collect } = require('streaming-iterables') const pipe = require('it-pipe') const AggregateError = require('aggregate-error') +const PeerId = require('peer-id') + const { createPeerInfo } = require('../utils/creators/peer') const baseOptions = require('../utils/base-options') const Libp2p = require('../../src') @@ -52,8 +54,9 @@ describe('Dialing (via relay, TCP)', () => { return Promise.all([srcLibp2p, relayLibp2p, dstLibp2p].map(async libp2p => { await libp2p.stop() // Clear the peer stores - for (const peerId of libp2p.peerStore.peers.keys()) { - libp2p.peerStore.remove(peerId) + for (const peerIdStr of libp2p.peerStore.peers.keys()) { + const peerId = PeerId.createFromCID(peerIdStr) + libp2p.peerStore.delete(peerId) } })) }) diff --git a/test/identify/index.spec.js b/test/identify/index.spec.js index ca32e023f2..7af2f67c84 100644 --- a/test/identify/index.spec.js +++ b/test/identify/index.spec.js @@ -48,7 +48,12 @@ describe('Identify', () => { protocols, registrar: { peerStore: { - replace: () => {} + addressBook: { + set: () => { } + }, + protoBook: { + set: () => { } + } } } }) @@ -64,7 +69,8 @@ describe('Identify', () => { const [local, remote] = duplexPair() sinon.stub(localConnectionMock, 'newStream').returns({ stream: local, protocol: multicodecs.IDENTIFY }) - sinon.spy(localIdentify.registrar.peerStore, 'replace') + sinon.spy(localIdentify.registrar.peerStore.addressBook, 'set') + sinon.spy(localIdentify.registrar.peerStore.protoBook, 'set') // Run identify await Promise.all([ @@ -76,9 +82,10 @@ describe('Identify', () => { }) ]) - expect(localIdentify.registrar.peerStore.replace.callCount).to.equal(1) + expect(localIdentify.registrar.peerStore.addressBook.set.callCount).to.equal(1) + expect(localIdentify.registrar.peerStore.protoBook.set.callCount).to.equal(1) // Validate the remote peer gets updated in the peer store - const call = localIdentify.registrar.peerStore.replace.firstCall + const call = localIdentify.registrar.peerStore.addressBook.set.firstCall expect(call.args[0].id.bytes).to.equal(remotePeer.id.bytes) }) @@ -88,7 +95,12 @@ describe('Identify', () => { protocols, registrar: { peerStore: { - replace: () => {} + addressBook: { + set: () => { } + }, + protoBook: { + set: () => { } + } } } }) @@ -134,7 +146,12 @@ describe('Identify', () => { peerInfo: remotePeer, registrar: { peerStore: { - replace: () => {} + addressBook: { + set: () => {} + }, + protoBook: { + set: () => { } + } } } }) @@ -152,9 +169,8 @@ describe('Identify', () => { const [local, remote] = duplexPair() sinon.stub(localConnectionMock, 'newStream').returns({ stream: local, protocol: multicodecs.IDENTIFY_PUSH }) - sinon.spy(IdentifyService, 'updatePeerAddresses') - sinon.spy(IdentifyService, 'updatePeerProtocols') - sinon.spy(remoteIdentify.registrar.peerStore, 'replace') + sinon.spy(remoteIdentify.registrar.peerStore.addressBook, 'set') + sinon.spy(remoteIdentify.registrar.peerStore.protoBook, 'set') // Run identify await Promise.all([ @@ -166,14 +182,14 @@ describe('Identify', () => { }) ]) - expect(IdentifyService.updatePeerAddresses.callCount).to.equal(1) - expect(IdentifyService.updatePeerProtocols.callCount).to.equal(1) - - expect(remoteIdentify.registrar.peerStore.replace.callCount).to.equal(1) - const [peerInfo] = remoteIdentify.registrar.peerStore.replace.firstCall.args - expect(peerInfo.id.bytes).to.eql(localPeer.id.bytes) - expect(peerInfo.multiaddrs.toArray()).to.eql([listeningAddr]) - expect(peerInfo.protocols).to.eql(localProtocols) + expect(remoteIdentify.registrar.peerStore.addressBook.set.callCount).to.equal(1) + expect(remoteIdentify.registrar.peerStore.protoBook.set.callCount).to.equal(1) + const [peerId, multiaddrs] = remoteIdentify.registrar.peerStore.addressBook.set.firstCall.args + expect(peerId.bytes).to.eql(localPeer.id.bytes) + expect(multiaddrs).to.eql([listeningAddr]) + const [peerId2, protocols] = remoteIdentify.registrar.peerStore.protoBook.set.firstCall.args + expect(peerId2.bytes).to.eql(localPeer.id.bytes) + expect(protocols).to.eql(Array.from(localProtocols)) }) }) @@ -204,13 +220,15 @@ describe('Identify', () => { }) sinon.spy(libp2p.identifyService, 'identify') - const peerStoreSpy = sinon.spy(libp2p.peerStore, 'replace') + const peerStoreSpySet = sinon.spy(libp2p.peerStore.addressBook, 'set') + const peerStoreSpyAdd = sinon.spy(libp2p.peerStore.addressBook, 'add') const connection = await libp2p.dialer.connectToPeer(remoteAddr) expect(connection).to.exist() // Wait for peer store to be updated - await pWaitFor(() => peerStoreSpy.callCount === 1) + // Dialer._createDialTarget (add), Identify (replace) + await pWaitFor(() => peerStoreSpySet.callCount === 1 && peerStoreSpyAdd.callCount === 1) expect(libp2p.identifyService.identify.callCount).to.equal(1) // The connection should have no open streams @@ -226,7 +244,6 @@ describe('Identify', () => { sinon.spy(libp2p.identifyService, 'identify') sinon.spy(libp2p.identifyService, 'push') - sinon.spy(libp2p.peerStore, 'update') const connection = await libp2p.dialer.connectToPeer(remoteAddr) expect(connection).to.exist() diff --git a/test/peer-discovery/index.spec.js b/test/peer-discovery/index.spec.js index 2d514cb5b2..733130950a 100644 --- a/test/peer-discovery/index.spec.js +++ b/test/peer-discovery/index.spec.js @@ -35,7 +35,9 @@ describe('peer discovery', () => { ...baseOptions, peerInfo }) - libp2p.peerStore.add(remotePeerInfo) + libp2p.peerStore.addressBook.set(remotePeerInfo.id, remotePeerInfo.multiaddrs.toArray()) + libp2p.peerStore.protoBook.set(remotePeerInfo.id, Array.from(remotePeerInfo.protocols)) + const deferred = defer() sinon.stub(libp2p.dialer, 'connectToPeer').callsFake((remotePeerInfo) => { expect(remotePeerInfo).to.equal(remotePeerInfo) @@ -46,7 +48,9 @@ describe('peer discovery', () => { libp2p.start() await deferred.promise - expect(spy.getCall(0).args).to.eql([remotePeerInfo]) + + expect(spy.calledOnce).to.eql(true) + expect(spy.getCall(0).args[0].id.toString()).to.eql(remotePeerInfo.id.toString()) }) it('should stop discovery on libp2p start/stop', async () => { diff --git a/test/peer-store/address-book.spec.js b/test/peer-store/address-book.spec.js new file mode 100644 index 0000000000..1ec11eb742 --- /dev/null +++ b/test/peer-store/address-book.spec.js @@ -0,0 +1,365 @@ +'use strict' +/* eslint-env mocha */ + +const chai = require('chai') +chai.use(require('dirty-chai')) +const { expect } = chai + +const { EventEmitter } = require('events') +const pDefer = require('p-defer') +const multiaddr = require('multiaddr') + +const AddressBook = require('../../src/peer-store/address-book') + +const peerUtils = require('../utils/creators/peer') +const { + ERR_INVALID_PARAMETERS +} = require('../../src/errors') + +const addr1 = multiaddr('/ip4/127.0.0.1/tcp/8000') +const addr2 = multiaddr('/ip4/127.0.0.1/tcp/8001') +const addr3 = multiaddr('/ip4/127.0.0.1/tcp/8002') + +const arraysAreEqual = (a, b) => a.length === b.length && a.sort().every((item, index) => b[index] === item) + +describe('addressBook', () => { + let peerId + + before(async () => { + [peerId] = await peerUtils.createPeerId() + }) + + describe('addressBook.set', () => { + let ee, ab + + beforeEach(() => { + ee = new EventEmitter() + ab = new AddressBook(ee) + }) + + afterEach(() => { + ee.removeAllListeners() + }) + + it('throwns invalid parameters error if invalid PeerId is provided', () => { + expect(() => { + ab.set('invalid peerId') + }).to.throw(ERR_INVALID_PARAMETERS) + }) + + it('throwns invalid parameters error if no addresses provided', () => { + expect(() => { + ab.set(peerId) + }).to.throw(ERR_INVALID_PARAMETERS) + }) + + it('throwns invalid parameters error if invalid multiaddrs are provided', () => { + expect(() => { + ab.set(peerId, 'invalid multiaddr') + }).to.throw(ERR_INVALID_PARAMETERS) + }) + + it('replaces the stored content by default and emit change event', () => { + const defer = pDefer() + const supportedMultiaddrs = [addr1, addr2] + + ee.once('change:multiaddrs', ({ peerId, multiaddrs }) => { + expect(peerId).to.exist() + expect(multiaddrs).to.eql(supportedMultiaddrs) + defer.resolve() + }) + + ab.set(peerId, supportedMultiaddrs) + const multiaddrInfos = ab.get(peerId) + const multiaddrs = multiaddrInfos.map((mi) => mi.multiaddr) + expect(multiaddrs).to.have.deep.members(supportedMultiaddrs) + + return defer.promise + }) + + it('emits on set if not storing the exact same content', async () => { + const defer = pDefer() + + const supportedMultiaddrsA = [addr1, addr2] + const supportedMultiaddrsB = [addr2] + + let changeCounter = 0 + ee.on('change:multiaddrs', () => { + changeCounter++ + if (changeCounter > 1) { + defer.resolve() + } + }) + + // set 1 + ab.set(peerId, supportedMultiaddrsA) + + // set 2 (same content) + ab.set(peerId, supportedMultiaddrsB) + const multiaddrInfos = ab.get(peerId) + const multiaddrs = multiaddrInfos.map((mi) => mi.multiaddr) + expect(multiaddrs).to.have.deep.members(supportedMultiaddrsB) + + await defer.promise + }) + + it('does not emit on set if it is storing the exact same content', async () => { + const defer = pDefer() + + const supportedMultiaddrs = [addr1, addr2] + + let changeCounter = 0 + ee.on('change:multiaddrs', () => { + changeCounter++ + if (changeCounter > 1) { + defer.reject() + } + }) + + // set 1 + ab.set(peerId, supportedMultiaddrs) + + // set 2 (same content) + ab.set(peerId, supportedMultiaddrs) + + // Wait 50ms for incorrect second event + setTimeout(() => { + defer.resolve() + }, 50) + + await defer.promise + }) + }) + + describe('addressBook.add', () => { + let ee, ab + + beforeEach(() => { + ee = new EventEmitter() + ab = new AddressBook(ee) + }) + + afterEach(() => { + ee.removeAllListeners() + }) + + it('throwns invalid parameters error if invalid PeerId is provided', () => { + expect(() => { + ab.add('invalid peerId') + }).to.throw(ERR_INVALID_PARAMETERS) + }) + + it('throwns invalid parameters error if no addresses provided', () => { + expect(() => { + ab.add(peerId) + }).to.throw(ERR_INVALID_PARAMETERS) + }) + + it('throwns invalid parameters error if invalid multiaddrs are provided', () => { + expect(() => { + ab.add(peerId, 'invalid multiaddr') + }).to.throw(ERR_INVALID_PARAMETERS) + }) + + it('adds the new content and emits change event', () => { + const defer = pDefer() + + const supportedMultiaddrsA = [addr1, addr2] + const supportedMultiaddrsB = [addr3] + const finalMultiaddrs = supportedMultiaddrsA.concat(supportedMultiaddrsB) + + let changeTrigger = 2 + ee.on('change:multiaddrs', ({ multiaddrs }) => { + changeTrigger-- + if (changeTrigger === 0 && arraysAreEqual(multiaddrs, finalMultiaddrs)) { + defer.resolve() + } + }) + + // Replace + ab.set(peerId, supportedMultiaddrsA) + let multiaddrInfos = ab.get(peerId) + let multiaddrs = multiaddrInfos.map((mi) => mi.multiaddr) + expect(multiaddrs).to.have.deep.members(supportedMultiaddrsA) + + // Add + ab.add(peerId, supportedMultiaddrsB) + multiaddrInfos = ab.get(peerId) + multiaddrs = multiaddrInfos.map((mi) => mi.multiaddr) + expect(multiaddrs).to.have.deep.members(finalMultiaddrs) + + return defer.promise + }) + + it('emits on add if the content to add not exists', async () => { + const defer = pDefer() + + const supportedMultiaddrsA = [addr1] + const supportedMultiaddrsB = [addr2] + const finalMultiaddrs = supportedMultiaddrsA.concat(supportedMultiaddrsB) + + let changeCounter = 0 + ee.on('change:multiaddrs', () => { + changeCounter++ + if (changeCounter > 1) { + defer.resolve() + } + }) + + // set 1 + ab.set(peerId, supportedMultiaddrsA) + + // set 2 (content already existing) + ab.add(peerId, supportedMultiaddrsB) + const multiaddrInfos = ab.get(peerId) + const multiaddrs = multiaddrInfos.map((mi) => mi.multiaddr) + expect(multiaddrs).to.have.deep.members(finalMultiaddrs) + + await defer.promise + }) + + it('does not emit on add if the content to add already exists', async () => { + const defer = pDefer() + + const supportedMultiaddrsA = [addr1, addr2] + const supportedMultiaddrsB = [addr2] + + let changeCounter = 0 + ee.on('change:multiaddrs', () => { + changeCounter++ + if (changeCounter > 1) { + defer.reject() + } + }) + + // set 1 + ab.set(peerId, supportedMultiaddrsA) + + // set 2 (content already existing) + ab.add(peerId, supportedMultiaddrsB) + + // Wait 50ms for incorrect second event + setTimeout(() => { + defer.resolve() + }, 50) + + await defer.promise + }) + }) + + describe('addressBook.get', () => { + let ee, ab + + beforeEach(() => { + ee = new EventEmitter() + ab = new AddressBook(ee) + }) + + it('throwns invalid parameters error if invalid PeerId is provided', () => { + expect(() => { + ab.get('invalid peerId') + }).to.throw(ERR_INVALID_PARAMETERS) + }) + + it('returns undefined if no multiaddrs are known for the provided peer', () => { + const multiaddrInfos = ab.get(peerId) + + expect(multiaddrInfos).to.not.exist() + }) + + it('returns the multiaddrs stored', () => { + const supportedMultiaddrs = [addr1, addr2] + + ab.set(peerId, supportedMultiaddrs) + + const multiaddrInfos = ab.get(peerId) + const multiaddrs = multiaddrInfos.map((mi) => mi.multiaddr) + expect(multiaddrs).to.have.deep.members(supportedMultiaddrs) + }) + }) + + describe('addressBook.getMultiaddrsForPeer', () => { + let ee, ab + + beforeEach(() => { + ee = new EventEmitter() + ab = new AddressBook(ee) + }) + + it('throwns invalid parameters error if invalid PeerId is provided', () => { + expect(() => { + ab.getMultiaddrsForPeer('invalid peerId') + }).to.throw(ERR_INVALID_PARAMETERS) + }) + + it('returns undefined if no multiaddrs are known for the provided peer', () => { + const multiaddrInfos = ab.getMultiaddrsForPeer(peerId) + + expect(multiaddrInfos).to.not.exist() + }) + + it('returns the multiaddrs stored', () => { + const supportedMultiaddrs = [addr1, addr2] + + ab.set(peerId, supportedMultiaddrs) + + const multiaddrs = ab.getMultiaddrsForPeer(peerId) + multiaddrs.forEach((m) => { + expect(m.getPeerId()).to.equal(peerId.toB58String()) + }) + }) + }) + + describe('addressBook.delete', () => { + let ee, ab + + beforeEach(() => { + ee = new EventEmitter() + ab = new AddressBook(ee) + }) + + it('throwns invalid parameters error if invalid PeerId is provided', () => { + expect(() => { + ab.delete('invalid peerId') + }).to.throw(ERR_INVALID_PARAMETERS) + }) + + it('returns false if no records exist for the peer and no event is emitted', () => { + const defer = pDefer() + + ee.on('change:multiaddrs', () => { + defer.reject() + }) + + const deleted = ab.delete(peerId) + + expect(deleted).to.equal(false) + + // Wait 50ms for incorrect invalid event + setTimeout(() => { + defer.resolve() + }, 50) + + return defer.promise + }) + + it('returns true if the record exists and an event is emitted', () => { + const defer = pDefer() + + const supportedMultiaddrs = [addr1, addr2] + ab.set(peerId, supportedMultiaddrs) + + // Listen after set + ee.on('change:multiaddrs', ({ multiaddrs }) => { + expect(multiaddrs.length).to.eql(0) + defer.resolve() + }) + + const deleted = ab.delete(peerId) + + expect(deleted).to.equal(true) + + return defer.promise + }) + }) +}) diff --git a/test/peer-store/peer-store.spec.js b/test/peer-store/peer-store.spec.js index 91628432cc..411977f86d 100644 --- a/test/peer-store/peer-store.spec.js +++ b/test/peer-store/peer-store.spec.js @@ -4,185 +4,147 @@ const chai = require('chai') chai.use(require('dirty-chai')) const { expect } = chai -const sinon = require('sinon') - -const pDefer = require('p-defer') const PeerStore = require('../../src/peer-store') const multiaddr = require('multiaddr') + const peerUtils = require('../utils/creators/peer') -const addr = multiaddr('/ip4/127.0.0.1/tcp/8000') +const addr1 = multiaddr('/ip4/127.0.0.1/tcp/8000') +const addr2 = multiaddr('/ip4/127.0.0.1/tcp/8001') +const addr3 = multiaddr('/ip4/127.0.0.1/tcp/8002') +const addr4 = multiaddr('/ip4/127.0.0.1/tcp/8003') -describe('peer-store', () => { - let peerStore +const proto1 = '/protocol1' +const proto2 = '/protocol2' +const proto3 = '/protocol3' - beforeEach(() => { - peerStore = new PeerStore() +describe('peer-store', () => { + let peerIds + before(async () => { + peerIds = await peerUtils.createPeerId({ + number: 4 + }) }) - it('should add a new peer and emit it when it does not exist', async () => { - const defer = pDefer() + describe('empty books', () => { + let peerStore - sinon.spy(peerStore, 'put') - sinon.spy(peerStore, 'add') - sinon.spy(peerStore, 'update') - - const [peerInfo] = await peerUtils.createPeerInfo() + beforeEach(() => { + peerStore = new PeerStore() + }) - peerStore.on('peer', (peer) => { - expect(peer).to.exist() - defer.resolve() + it('has an empty map of peers', () => { + const peers = peerStore.peers + expect(peers.size).to.equal(0) }) - peerStore.put(peerInfo) - // Wait for peerStore to emit the peer - await defer.promise + it('returns false on trying to delete a non existant peerId', () => { + const deleted = peerStore.delete(peerIds[0]) + expect(deleted).to.equal(false) + }) - expect(peerStore.put.callCount).to.equal(1) - expect(peerStore.add.callCount).to.equal(1) - expect(peerStore.update.callCount).to.equal(0) + it('returns undefined on trying to find a non existant peerId', () => { + const peerInfo = peerStore.find(peerIds[0]) + expect(peerInfo).to.not.exist() + }) }) - it('should update peer when it is already in the store', async () => { - const [peerInfo] = await peerUtils.createPeerInfo() + describe('previously populated books', () => { + let peerStore - // Put the peer in the store - peerStore.put(peerInfo) + beforeEach(() => { + peerStore = new PeerStore() - sinon.spy(peerStore, 'add') - sinon.spy(peerStore, 'update') + // Add peer0 with { addr1, addr2 } and { proto1 } + peerStore.addressBook.set(peerIds[0], [addr1, addr2]) + peerStore.protoBook.set(peerIds[0], [proto1]) - // When updating, peer event must not be emitted - peerStore.on('peer', () => { - throw new Error('should not emit twice') - }) - // If no multiaddrs change, the event should not be emitted - peerStore.on('change:multiaddrs', () => { - throw new Error('should not emit change:multiaddrs') - }) - // If no protocols change, the event should not be emitted - peerStore.on('change:protocols', () => { - throw new Error('should not emit change:protocols') - }) - - peerStore.put(peerInfo) + // Add peer1 with { addr3 } and { proto2, proto3 } + peerStore.addressBook.set(peerIds[1], [addr3]) + peerStore.protoBook.set(peerIds[1], [proto2, proto3]) - expect(peerStore.add.callCount).to.equal(0) - expect(peerStore.update.callCount).to.equal(1) - }) + // Add peer2 with { addr4 } + peerStore.addressBook.set(peerIds[2], [addr4]) - it('should emit the "change:multiaddrs" event when a peer has new multiaddrs', async () => { - const defer = pDefer() - const [createdPeerInfo] = await peerUtils.createPeerInfo() + // Add peer3 with { addr4 } and { proto2 } + peerStore.addressBook.set(peerIds[3], [addr4]) + peerStore.protoBook.set(peerIds[3], [proto2]) + }) - // Put the peer in the store - peerStore.put(createdPeerInfo) + it('has peers', () => { + const peers = peerStore.peers - // When updating, "change:multiaddrs" event must not be emitted - peerStore.on('change:multiaddrs', ({ peerInfo, multiaddrs }) => { - expect(peerInfo).to.exist() - expect(peerInfo.id).to.eql(createdPeerInfo.id) - expect(peerInfo.protocols).to.eql(createdPeerInfo.protocols) - expect(multiaddrs).to.exist() - expect(multiaddrs).to.eql(createdPeerInfo.multiaddrs.toArray()) - defer.resolve() - }) - // If no protocols change, the event should not be emitted - peerStore.on('change:protocols', () => { - throw new Error('should not emit change:protocols') + expect(peers.size).to.equal(4) + expect(Array.from(peers.keys())).to.have.members([ + peerIds[0].toB58String(), + peerIds[1].toB58String(), + peerIds[2].toB58String(), + peerIds[3].toB58String() + ]) }) - createdPeerInfo.multiaddrs.add(addr) - peerStore.put(createdPeerInfo) + it('returns true on deleting a stored peer', () => { + const deleted = peerStore.delete(peerIds[0]) + expect(deleted).to.equal(true) - // Wait for peerStore to emit the event - await defer.promise - }) - - it('should emit the "change:protocols" event when a peer has new protocols', async () => { - const defer = pDefer() - const [createdPeerInfo] = await peerUtils.createPeerInfo() + const peers = peerStore.peers + expect(peers.size).to.equal(3) + expect(Array.from(peers.keys())).to.not.have.members([peerIds[0].toB58String()]) + }) - // Put the peer in the store - peerStore.put(createdPeerInfo) + it('returns true on deleting a stored peer which is only on one book', () => { + const deleted = peerStore.delete(peerIds[2]) + expect(deleted).to.equal(true) - // If no multiaddrs change, the event should not be emitted - peerStore.on('change:multiaddrs', () => { - throw new Error('should not emit change:multiaddrs') - }) - // When updating, "change:protocols" event must be emitted - peerStore.on('change:protocols', ({ peerInfo, protocols }) => { - expect(peerInfo).to.exist() - expect(peerInfo.id).to.eql(createdPeerInfo.id) - expect(peerInfo.multiaddrs).to.eql(createdPeerInfo.multiaddrs) - expect(protocols).to.exist() - expect(protocols).to.eql(Array.from(createdPeerInfo.protocols)) - defer.resolve() + const peers = peerStore.peers + expect(peers.size).to.equal(3) }) - createdPeerInfo.protocols.add('/new-protocol/1.0.0') - peerStore.put(createdPeerInfo) - - // Wait for peerStore to emit the event - await defer.promise - }) + it('finds the stored information of a peer in all its books', () => { + const peerInfo = peerStore.find(peerIds[0]) + expect(peerInfo).to.exist() + expect(peerInfo.protocols).to.have.members([proto1]) - it('should be able to retrieve a peer from store through its b58str id', async () => { - const [peerInfo] = await peerUtils.createPeerInfo() - const id = peerInfo.id + const peerMultiaddrs = peerInfo.multiaddrInfos.map((mi) => mi.multiaddr) + expect(peerMultiaddrs).to.have.members([addr1, addr2]) + }) - let retrievedPeer = peerStore.get(id) - expect(retrievedPeer).to.not.exist() + it('finds the stored information of a peer that is not present in all its books', () => { + const peerInfo = peerStore.find(peerIds[2]) + expect(peerInfo).to.exist() + expect(peerInfo.protocols.length).to.eql(0) - // Put the peer in the store - peerStore.put(peerInfo) + const peerMultiaddrs = peerInfo.multiaddrInfos.map((mi) => mi.multiaddr) + expect(peerMultiaddrs).to.have.members([addr4]) + }) - retrievedPeer = peerStore.get(id) - expect(retrievedPeer).to.exist() - expect(retrievedPeer.id).to.equal(peerInfo.id) - expect(retrievedPeer.multiaddrs).to.eql(peerInfo.multiaddrs) - expect(retrievedPeer.protocols).to.eql(peerInfo.protocols) - }) + it('can find all the peers supporting a protocol', () => { + const peerSupporting2 = [] - it('should be able to remove a peer from store through its b58str id', async () => { - const [peerInfo] = await peerUtils.createPeerInfo() - const id = peerInfo.id + for (const [, peerInfo] of peerStore.peers.entries()) { + if (peerInfo.protocols.has(proto2)) { + peerSupporting2.push(peerInfo) + } + } - let removed = peerStore.remove(id) - expect(removed).to.eql(false) + expect(peerSupporting2.length).to.eql(2) + expect(peerSupporting2[0].id.toB58String()).to.eql(peerIds[1].toB58String()) + expect(peerSupporting2[1].id.toB58String()).to.eql(peerIds[3].toB58String()) + }) - // Put the peer in the store - peerStore.put(peerInfo) - expect(peerStore.peers.size).to.equal(1) + it('can find all the peers listening on a given address', () => { + const peerListenint4 = [] - removed = peerStore.remove(id) - expect(removed).to.eql(true) - expect(peerStore.peers.size).to.equal(0) - }) + for (const [, peerInfo] of peerStore.peers.entries()) { + if (peerInfo.multiaddrs.has(addr4)) { + peerListenint4.push(peerInfo) + } + } - it('should be able to get the multiaddrs for a peer', async () => { - const [peerInfo, relayInfo] = await peerUtils.createPeerInfo({ number: 2 }) - const id = peerInfo.id - const ma1 = multiaddr('/ip4/127.0.0.1/tcp/4001') - const ma2 = multiaddr('/ip4/127.0.0.1/tcp/4002/ws') - const ma3 = multiaddr(`/ip4/127.0.0.1/tcp/4003/ws/p2p/${relayInfo.id.toB58String()}/p2p-circuit`) - - peerInfo.multiaddrs.add(ma1) - peerInfo.multiaddrs.add(ma2) - peerInfo.multiaddrs.add(ma3) - - const multiaddrs = peerStore.multiaddrsForPeer(peerInfo) - const expectedAddrs = [ - ma1.encapsulate(`/p2p/${id.toB58String()}`), - ma2.encapsulate(`/p2p/${id.toB58String()}`), - ma3.encapsulate(`/p2p/${id.toB58String()}`) - ] - - expect(multiaddrs).to.eql(expectedAddrs) + expect(peerListenint4.length).to.eql(2) + expect(peerListenint4[0].id.toB58String()).to.eql(peerIds[2].toB58String()) + expect(peerListenint4[1].id.toB58String()).to.eql(peerIds[3].toB58String()) + }) }) }) - -describe('peer-store on discovery', () => { - // TODO: implement with discovery -}) diff --git a/test/peer-store/proto-book.spec.js b/test/peer-store/proto-book.spec.js new file mode 100644 index 0000000000..7985dfe1dc --- /dev/null +++ b/test/peer-store/proto-book.spec.js @@ -0,0 +1,310 @@ +'use strict' +/* eslint-env mocha */ + +const chai = require('chai') +chai.use(require('dirty-chai')) +const { expect } = chai + +const { EventEmitter } = require('events') +const pDefer = require('p-defer') + +const ProtoBook = require('../../src/peer-store/proto-book') + +const peerUtils = require('../utils/creators/peer') +const { + ERR_INVALID_PARAMETERS +} = require('../../src/errors') + +const arraysAreEqual = (a, b) => a.length === b.length && a.sort().every((item, index) => b[index] === item) + +describe('protoBook', () => { + let peerId + + before(async () => { + [peerId] = await peerUtils.createPeerId() + }) + + describe('protoBook.set', () => { + let ee, pb + + beforeEach(() => { + ee = new EventEmitter() + pb = new ProtoBook(ee) + }) + + afterEach(() => { + ee.removeAllListeners() + }) + + it('throwns invalid parameters error if invalid PeerId is provided', () => { + expect(() => { + pb.set('invalid peerId') + }).to.throw(ERR_INVALID_PARAMETERS) + }) + + it('throwns invalid parameters error if no protocols provided', () => { + expect(() => { + pb.set(peerId) + }).to.throw(ERR_INVALID_PARAMETERS) + }) + + it('replaces the stored content by default and emit change event', () => { + const defer = pDefer() + const supportedProtocols = ['protocol1', 'protocol2'] + + ee.once('change:protocols', ({ peerId, protocols }) => { + expect(peerId).to.exist() + expect(protocols).to.have.deep.members(supportedProtocols) + defer.resolve() + }) + + pb.set(peerId, supportedProtocols) + const protocols = pb.get(peerId) + expect(protocols).to.have.deep.members(supportedProtocols) + + return defer.promise + }) + + it('emits on set if not storing the exact same content', () => { + const defer = pDefer() + + const supportedProtocolsA = ['protocol1', 'protocol2'] + const supportedProtocolsB = ['protocol2'] + + let changeCounter = 0 + ee.on('change:protocols', () => { + changeCounter++ + if (changeCounter > 1) { + defer.resolve() + } + }) + + // set 1 + pb.set(peerId, supportedProtocolsA) + + // set 2 (same content) + pb.set(peerId, supportedProtocolsB) + const protocols = pb.get(peerId) + expect(protocols).to.have.deep.members(supportedProtocolsB) + + return defer.promise + }) + + it('does not emit on set if it is storing the exact same content', () => { + const defer = pDefer() + + const supportedProtocols = ['protocol1', 'protocol2'] + + let changeCounter = 0 + ee.on('change:protocols', () => { + changeCounter++ + if (changeCounter > 1) { + defer.reject() + } + }) + + // set 1 + pb.set(peerId, supportedProtocols) + + // set 2 (same content) + pb.set(peerId, supportedProtocols) + + // Wait 50ms for incorrect second event + setTimeout(() => { + defer.resolve() + }, 50) + + return defer.promise + }) + }) + + describe('protoBook.add', () => { + let ee, pb + + beforeEach(() => { + ee = new EventEmitter() + pb = new ProtoBook(ee) + }) + + afterEach(() => { + ee.removeAllListeners() + }) + + it('throwns invalid parameters error if invalid PeerId is provided', () => { + expect(() => { + pb.add('invalid peerId') + }).to.throw(ERR_INVALID_PARAMETERS) + }) + + it('throwns invalid parameters error if no protocols provided', () => { + expect(() => { + pb.add(peerId) + }).to.throw(ERR_INVALID_PARAMETERS) + }) + + it('adds the new content and emits change event', () => { + const defer = pDefer() + + const supportedProtocolsA = ['protocol1', 'protocol2'] + const supportedProtocolsB = ['protocol3'] + const finalProtocols = supportedProtocolsA.concat(supportedProtocolsB) + + let changeTrigger = 2 + ee.on('change:protocols', ({ protocols }) => { + changeTrigger-- + if (changeTrigger === 0 && arraysAreEqual(protocols, finalProtocols)) { + defer.resolve() + } + }) + + // Replace + pb.set(peerId, supportedProtocolsA) + let protocols = pb.get(peerId) + expect(protocols).to.have.deep.members(supportedProtocolsA) + + // Add + pb.add(peerId, supportedProtocolsB) + protocols = pb.get(peerId) + expect(protocols).to.have.deep.members(finalProtocols) + + return defer.promise + }) + + it('emits on add if the content to add not exists', () => { + const defer = pDefer() + + const supportedProtocolsA = ['protocol1'] + const supportedProtocolsB = ['protocol2'] + const finalProtocols = supportedProtocolsA.concat(supportedProtocolsB) + + let changeCounter = 0 + ee.on('change:protocols', () => { + changeCounter++ + if (changeCounter > 1) { + defer.resolve() + } + }) + + // set 1 + pb.set(peerId, supportedProtocolsA) + + // set 2 (content already existing) + pb.add(peerId, supportedProtocolsB) + const protocols = pb.get(peerId) + expect(protocols).to.have.deep.members(finalProtocols) + + return defer.promise + }) + + it('does not emit on add if the content to add already exists', () => { + const defer = pDefer() + + const supportedProtocolsA = ['protocol1', 'protocol2'] + const supportedProtocolsB = ['protocol2'] + + let changeCounter = 0 + ee.on('change:protocols', () => { + changeCounter++ + if (changeCounter > 1) { + defer.reject() + } + }) + + // set 1 + pb.set(peerId, supportedProtocolsA) + + // set 2 (content already existing) + pb.add(peerId, supportedProtocolsB) + + // Wait 50ms for incorrect second event + setTimeout(() => { + defer.resolve() + }, 50) + + return defer.promise + }) + }) + + describe('protoBook.get', () => { + let ee, pb + + beforeEach(() => { + ee = new EventEmitter() + pb = new ProtoBook(ee) + }) + + it('throwns invalid parameters error if invalid PeerId is provided', () => { + expect(() => { + pb.get('invalid peerId') + }).to.throw(ERR_INVALID_PARAMETERS) + }) + + it('returns undefined if no protocols are known for the provided peer', () => { + const protocols = pb.get(peerId) + + expect(protocols).to.not.exist() + }) + + it('returns the protocols stored', () => { + const supportedProtocols = ['protocol1', 'protocol2'] + + pb.set(peerId, supportedProtocols) + + const protocols = pb.get(peerId) + expect(protocols).to.have.deep.members(supportedProtocols) + }) + }) + + describe('protoBook.delete', () => { + let ee, pb + + beforeEach(() => { + ee = new EventEmitter() + pb = new ProtoBook(ee) + }) + + it('throwns invalid parameters error if invalid PeerId is provided', () => { + expect(() => { + pb.delete('invalid peerId') + }).to.throw(ERR_INVALID_PARAMETERS) + }) + + it('returns false if no records exist for the peer and no event is emitted', () => { + const defer = pDefer() + + ee.on('change:protocols', () => { + defer.reject() + }) + + const deleted = pb.delete(peerId) + + expect(deleted).to.equal(false) + + // Wait 50ms for incorrect invalid event + setTimeout(() => { + defer.resolve() + }, 50) + + return defer.promise + }) + + it('returns true if the record exists and an event is emitted', () => { + const defer = pDefer() + + const supportedProtocols = ['protocol1', 'protocol2'] + pb.set(peerId, supportedProtocols) + + // Listen after set + ee.on('change:protocols', ({ protocols }) => { + expect(protocols.length).to.eql(0) + defer.resolve() + }) + + const deleted = pb.delete(peerId) + + expect(deleted).to.equal(true) + + return defer.promise + }) + }) +}) diff --git a/test/registrar/registrar.spec.js b/test/registrar/registrar.spec.js index 056c4b49ab..3e2744901e 100644 --- a/test/registrar/registrar.spec.js +++ b/test/registrar/registrar.spec.js @@ -89,7 +89,9 @@ describe('registrar', () => { remotePeerInfo.protocols.add(multicodec) // Add connected peer to peerStore and registrar - peerStore.put(remotePeerInfo) + peerStore.addressBook.set(remotePeerInfo.id, remotePeerInfo.multiaddrs.toArray()) + peerStore.protoBook.set(remotePeerInfo.id, Array.from(remotePeerInfo.protocols)) + registrar.onConnect(remotePeerInfo, conn) expect(registrar.connections.size).to.eql(1) @@ -156,18 +158,23 @@ describe('registrar', () => { const peerInfo = await PeerInfo.create(conn.remotePeer) // Add connected peer to peerStore and registrar - peerStore.put(peerInfo) + peerStore.addressBook.set(peerInfo.id, peerInfo.multiaddrs.toArray()) + peerStore.protoBook.set(peerInfo.id, Array.from(peerInfo.protocols)) + registrar.onConnect(peerInfo, conn) // Add protocol to peer and update it peerInfo.protocols.add(multicodec) - peerStore.put(peerInfo) + peerStore.addressBook.add(peerInfo.id, peerInfo.multiaddrs.toArray()) + peerStore.protoBook.add(peerInfo.id, Array.from(peerInfo.protocols)) await onConnectDefer.promise // Remove protocol to peer and update it peerInfo.protocols.delete(multicodec) - peerStore.replace(peerInfo) + + peerStore.addressBook.set(peerInfo.id, peerInfo.multiaddrs.toArray()) + peerStore.protoBook.set(peerInfo.id, Array.from(peerInfo.protocols)) await onDisconnectDefer.promise }) @@ -197,7 +204,8 @@ describe('registrar', () => { const id = peerInfo.id.toB58String() // Add connection to registrar - peerStore.put(peerInfo) + peerStore.addressBook.set(peerInfo.id, peerInfo.multiaddrs.toArray()) + peerStore.protoBook.set(peerInfo.id, Array.from(peerInfo.protocols)) registrar.onConnect(peerInfo, conn1) registrar.onConnect(peerInfo, conn2)