From 98004ad1d3df635416a2fbcdc88fdd9d3b46657e Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Tue, 31 Mar 2020 16:58:07 +0200 Subject: [PATCH] chore: deprecate old peer-store api BREAKING CHANGE: the peer-store api changed. Check the API docs for the new specification. --- package.json | 2 +- src/peer-store/address-book.js | 10 ++- src/peer-store/index.js | 98 +++------------------------- src/peer-store/proto-book.js | 10 ++- test/peer-store/address-book.spec.js | 45 +++++++------ test/peer-store/proto-book.spec.js | 45 +++++++------ 6 files changed, 71 insertions(+), 139 deletions(-) diff --git a/package.json b/package.json index 9c49accc87..e6688dc935 100644 --- a/package.json +++ b/package.json @@ -93,7 +93,7 @@ "libp2p-delegated-peer-routing": "^0.4.0", "libp2p-floodsub": "^0.20.0", "libp2p-gossipsub": "^0.2.0", - "libp2p-kad-dht": "^0.18.2", + "libp2p-kad-dht": "libp2p/js-libp2p-kad-dht#chore/use-new-peer-store-api", "libp2p-mdns": "^0.13.0", "libp2p-mplex": "^0.9.1", "libp2p-secio": "^0.12.1", diff --git a/src/peer-store/address-book.js b/src/peer-store/address-book.js index c9a54bb493..556f1a2131 100644 --- a/src/peer-store/address-book.js +++ b/src/peer-store/address-book.js @@ -30,7 +30,7 @@ class AddressBook extends Book { /** * @constructor - * @param {EventEmitter} peerStore + * @param {PeerStore} peerStore */ constructor (peerStore) { super(peerStore, 'change:multiaddrs', 'multiaddrs') @@ -120,6 +120,7 @@ class AddressBook extends Book { } this.data.set(id, multiaddrInfos) + this._setPeerId(peerId) log(`stored provided multiaddrs for ${id}`) // TODO: Remove peerInfo and its usage on peer-info deprecate @@ -166,6 +167,7 @@ class AddressBook extends Book { return [...multiaddrInfos] } + this._setPeerId(peerId) this.data.set(id, multiaddrInfos) log(`added provided multiaddrs for ${id}`) @@ -189,6 +191,12 @@ class AddressBook extends Book { return [...multiaddrInfos] } + _setPeerId (peerId) { + if (!this._ps.peerIds.get(peerId)) { + this._ps.peerIds.set(peerId.toString(), peerId) + } + } + /** * Get the known multiaddrs for a given peer. All returned multiaddrs * will include the encapsulated `PeerId` of the peer. diff --git a/src/peer-store/index.js b/src/peer-store/index.js index 597b69e178..30ed7e94bb 100644 --- a/src/peer-store/index.js +++ b/src/peer-store/index.js @@ -43,97 +43,13 @@ class PeerStore extends EventEmitter { * 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 on each book. - * @param {PeerInfo} peerInfo - * @param {object} [options] - * @param {boolean} [options.replace = true] - * @return {PeerInfo} - */ - put (peerInfo, options) { - const multiaddrs = peerInfo.multiaddrs.toArray() - const protocols = Array.from(peerInfo.protocols || new Set()) - - this.addressBook.set(peerInfo.id, multiaddrs, options) - this.protoBook.set(peerInfo.id, protocols, options) - - const peer = this.find(peerInfo.id) - const pInfo = new PeerInfo(peerInfo.id) - - if (!peer) { - return pInfo - } - - 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 - /** - * Get the info of the given id. - * @param {peerId} peerId - * @returns {PeerInfo} - */ - get (peerId) { - const peer = this.find(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} peerId - * @returns {boolean} - */ - 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 provided. - * @param {PeerId} peerId - * @returns {boolean} true if found and removed - */ - remove (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) { - 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} peerInfo - * @returns {Array} - */ - multiaddrsForPeer (peerInfo) { - return this.addressBook.getMultiaddrsForPeer(peerInfo.id) + /** + * TODO: this should only exist until we have the key-book + * Map known peers to their peer-id. + * @type {Map} + */ + this.peerIds = new Map() } /** @@ -204,6 +120,7 @@ class PeerStore extends EventEmitter { throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS) } + const id = this.peerIds.get(peerId.toString()) const multiaddrInfos = this.addressBook.get(peerId) const protocols = this.protoBook.get(peerId) @@ -212,6 +129,7 @@ class PeerStore extends EventEmitter { } return { + id: id || peerId, multiaddrInfos: multiaddrInfos || [], protocols: protocols || [] } diff --git a/src/peer-store/proto-book.js b/src/peer-store/proto-book.js index be7762e4b0..eeef097e34 100644 --- a/src/peer-store/proto-book.js +++ b/src/peer-store/proto-book.js @@ -22,7 +22,7 @@ const { class ProtoBook extends Book { /** * @constructor - * @param {EventEmitter} peerStore + * @param {PeerStore} peerStore */ constructor (peerStore) { super(peerStore, 'change:protocols', 'protocols') @@ -94,6 +94,7 @@ class ProtoBook extends Book { } this.data.set(id, newSet) + this._setPeerId(peerId) log(`stored provided protocols for ${id}`) // TODO: Remove peerInfo and its usage on peer-info deprecate @@ -130,6 +131,7 @@ class ProtoBook extends Book { protocols = [...newSet] this.data.set(id, newSet) + this._setPeerId(peerId) log(`added provided protocols for ${id}`) // TODO: Remove peerInfo and its usage on peer-info deprecate @@ -145,6 +147,12 @@ class ProtoBook extends Book { return protocols } + _setPeerId (peerId) { + if (!this._ps.peerIds.get(peerId)) { + this._ps.peerIds.set(peerId.toString(), peerId) + } + } + /** * Verify if the provided peer supports the given protocols. * @param {PeerId} peerId diff --git a/test/peer-store/address-book.spec.js b/test/peer-store/address-book.spec.js index 140699eb05..3d049638e5 100644 --- a/test/peer-store/address-book.spec.js +++ b/test/peer-store/address-book.spec.js @@ -6,11 +6,10 @@ chai.use(require('dirty-chai')) const { expect } = chai const sinon = require('sinon') -const { EventEmitter } = require('events') const pDefer = require('p-defer') const multiaddr = require('multiaddr') -const AddressBook = require('../../src/peer-store/address-book') +const PeerStore = require('../../src/peer-store') const peerUtils = require('../utils/creators/peer') const { @@ -31,15 +30,15 @@ describe('addressBook', () => { }) describe('addressBook.set', () => { - let ee, ab + let peerStore, ab beforeEach(() => { - ee = new EventEmitter() - ab = new AddressBook(ee) + peerStore = new PeerStore() + ab = peerStore.addressBook }) afterEach(() => { - ee.removeAllListeners() + peerStore.removeAllListeners() }) it('throwns invalid parameters error if invalid PeerId is provided', () => { @@ -67,7 +66,7 @@ describe('addressBook', () => { const supportedMultiaddrs = [addr1, addr2] - ee.once('change:multiaddrs', ({ peerId, multiaddrs }) => { + peerStore.once('change:multiaddrs', ({ peerId, multiaddrs }) => { expect(peerId).to.exist() expect(multiaddrs).to.eql(supportedMultiaddrs) defer.resolve() @@ -93,7 +92,7 @@ describe('addressBook', () => { const finalMultiaddrs = supportedMultiaddrsA.concat(supportedMultiaddrsB) let changeTrigger = 2 - ee.on('change:multiaddrs', ({ multiaddrs }) => { + peerStore.on('change:multiaddrs', ({ multiaddrs }) => { changeTrigger-- if (changeTrigger === 0 && arraysAreEqual(multiaddrs, finalMultiaddrs)) { defer.resolve() @@ -124,7 +123,7 @@ describe('addressBook', () => { const supportedMultiaddrsB = [addr2] let changeCounter = 0 - ee.on('change:multiaddrs', () => { + peerStore.on('change:multiaddrs', () => { changeCounter++ if (changeCounter > 1) { defer.resolve() @@ -148,7 +147,7 @@ describe('addressBook', () => { const supportedMultiaddrs = [addr1, addr2] let changeCounter = 0 - ee.on('change:multiaddrs', () => { + peerStore.on('change:multiaddrs', () => { changeCounter++ if (changeCounter > 1) { defer.reject() @@ -177,7 +176,7 @@ describe('addressBook', () => { const finalMultiaddrs = supportedMultiaddrsA.concat(supportedMultiaddrsB) let changeCounter = 0 - ee.on('change:multiaddrs', () => { + peerStore.on('change:multiaddrs', () => { changeCounter++ if (changeCounter > 1) { defer.resolve() @@ -202,7 +201,7 @@ describe('addressBook', () => { const supportedMultiaddrsB = [addr2] let changeCounter = 0 - ee.on('change:multiaddrs', () => { + peerStore.on('change:multiaddrs', () => { changeCounter++ if (changeCounter > 1) { defer.reject() @@ -225,11 +224,11 @@ describe('addressBook', () => { }) describe('addressBook.get', () => { - let ee, ab + let peerStore, ab beforeEach(() => { - ee = new EventEmitter() - ab = new AddressBook(ee) + peerStore = new PeerStore() + ab = peerStore.addressBook }) it('throwns invalid parameters error if invalid PeerId is provided', () => { @@ -256,11 +255,11 @@ describe('addressBook', () => { }) describe('addressBook.getMultiaddrsForPeer', () => { - let ee, ab + let peerStore, ab beforeEach(() => { - ee = new EventEmitter() - ab = new AddressBook(ee) + peerStore = new PeerStore() + ab = peerStore.addressBook }) it('throwns invalid parameters error if invalid PeerId is provided', () => { @@ -288,11 +287,11 @@ describe('addressBook', () => { }) describe('addressBook.delete', () => { - let ee, ab + let peerStore, ab beforeEach(() => { - ee = new EventEmitter() - ab = new AddressBook(ee) + peerStore = new PeerStore() + ab = peerStore.addressBook }) it('throwns invalid parameters error if invalid PeerId is provided', () => { @@ -304,7 +303,7 @@ describe('addressBook', () => { it('returns false if no records exist for the peer and no event is emitted', () => { const defer = pDefer() - ee.on('change:multiaddrs', () => { + peerStore.on('change:multiaddrs', () => { defer.reject() }) @@ -327,7 +326,7 @@ describe('addressBook', () => { ab.set(peerId, supportedMultiaddrs) // Listen after set - ee.on('change:multiaddrs', ({ multiaddrs }) => { + peerStore.on('change:multiaddrs', ({ multiaddrs }) => { expect(multiaddrs.length).to.eql(0) defer.resolve() }) diff --git a/test/peer-store/proto-book.spec.js b/test/peer-store/proto-book.spec.js index 5c9fe5b983..e47b961b96 100644 --- a/test/peer-store/proto-book.spec.js +++ b/test/peer-store/proto-book.spec.js @@ -6,10 +6,9 @@ chai.use(require('dirty-chai')) const { expect } = chai const sinon = require('sinon') -const { EventEmitter } = require('events') const pDefer = require('p-defer') -const ProtoBook = require('../../src/peer-store/proto-book') +const PeerStore = require('../../src/peer-store') const peerUtils = require('../utils/creators/peer') const { @@ -26,15 +25,15 @@ describe('protoBook', () => { }) describe('protoBook.set', () => { - let ee, pb + let peerStore, pb beforeEach(() => { - ee = new EventEmitter() - pb = new ProtoBook(ee) + peerStore = new PeerStore() + pb = peerStore.protoBook }) afterEach(() => { - ee.removeAllListeners() + peerStore.removeAllListeners() }) it('throwns invalid parameters error if invalid PeerId is provided', () => { @@ -56,7 +55,7 @@ describe('protoBook', () => { const supportedProtocols = ['protocol1', 'protocol2'] - ee.once('change:protocols', ({ peerId, protocols }) => { + peerStore.once('change:protocols', ({ peerId, protocols }) => { expect(peerId).to.exist() expect(protocols).to.have.deep.members(supportedProtocols) defer.resolve() @@ -81,7 +80,7 @@ describe('protoBook', () => { const finalProtocols = supportedProtocolsA.concat(supportedProtocolsB) let changeTrigger = 2 - ee.on('change:protocols', ({ protocols }) => { + peerStore.on('change:protocols', ({ protocols }) => { changeTrigger-- if (changeTrigger === 0 && arraysAreEqual(protocols, finalProtocols)) { defer.resolve() @@ -110,7 +109,7 @@ describe('protoBook', () => { const supportedProtocolsB = ['protocol2'] let changeCounter = 0 - ee.on('change:protocols', () => { + peerStore.on('change:protocols', () => { changeCounter++ if (changeCounter > 1) { defer.resolve() @@ -133,7 +132,7 @@ describe('protoBook', () => { const supportedProtocols = ['protocol1', 'protocol2'] let changeCounter = 0 - ee.on('change:protocols', () => { + peerStore.on('change:protocols', () => { changeCounter++ if (changeCounter > 1) { defer.reject() @@ -162,7 +161,7 @@ describe('protoBook', () => { const finalProtocols = supportedProtocolsA.concat(supportedProtocolsB) let changeCounter = 0 - ee.on('change:protocols', () => { + peerStore.on('change:protocols', () => { changeCounter++ if (changeCounter > 1) { defer.resolve() @@ -186,7 +185,7 @@ describe('protoBook', () => { const supportedProtocolsB = ['protocol2'] let changeCounter = 0 - ee.on('change:protocols', () => { + peerStore.on('change:protocols', () => { changeCounter++ if (changeCounter > 1) { defer.reject() @@ -209,11 +208,11 @@ describe('protoBook', () => { }) describe('protoBook.get', () => { - let ee, pb + let peerStore, pb beforeEach(() => { - ee = new EventEmitter() - pb = new ProtoBook(ee) + peerStore = new PeerStore() + pb = peerStore.protoBook }) it('throwns invalid parameters error if invalid PeerId is provided', () => { @@ -239,11 +238,11 @@ describe('protoBook', () => { }) describe('protoBook.supports', () => { - let ee, pb + let peerStore, pb beforeEach(() => { - ee = new EventEmitter() - pb = new ProtoBook(ee) + peerStore = new PeerStore() + pb = peerStore.protoBook }) it('throwns invalid parameters error if invalid PeerId is provided', () => { @@ -286,11 +285,11 @@ describe('protoBook', () => { }) describe('protoBook.delete', () => { - let ee, pb + let peerStore, pb beforeEach(() => { - ee = new EventEmitter() - pb = new ProtoBook(ee) + peerStore = new PeerStore() + pb = peerStore.protoBook }) it('throwns invalid parameters error if invalid PeerId is provided', () => { @@ -302,7 +301,7 @@ describe('protoBook', () => { it('returns false if no records exist for the peer and no event is emitted', () => { const defer = pDefer() - ee.on('change:protocols', () => { + peerStore.on('change:protocols', () => { defer.reject() }) @@ -325,7 +324,7 @@ describe('protoBook', () => { pb.set(peerId, supportedProtocols) // Listen after set - ee.on('change:protocols', ({ protocols }) => { + peerStore.on('change:protocols', ({ protocols }) => { expect(protocols.length).to.eql(0) defer.resolve() })