mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-25 10:32:14 +00:00
chore: deprecate old peer store api (#598)
* chore: deprecate old peer-store api BREAKING CHANGE: the peer-store api changed. Check the API docs for the new specification. * chore: apply suggestions from code review Co-Authored-By: Jacob Heun <jacobheun@gmail.com> * chore: apply suggestions from code review Co-Authored-By: Jacob Heun <jacobheun@gmail.com> Co-authored-by: Jacob Heun <jacobheun@gmail.com>
This commit is contained in:
parent
e9d225c9dc
commit
ed6d5bb4b4
28
doc/API.md
28
doc/API.md
@ -41,6 +41,8 @@
|
|||||||
* [`metrics.forPeer`](#metricsforpeer)
|
* [`metrics.forPeer`](#metricsforpeer)
|
||||||
* [`metrics.forProtocol`](#metricsforprotocol)
|
* [`metrics.forProtocol`](#metricsforprotocol)
|
||||||
* [Events](#events)
|
* [Events](#events)
|
||||||
|
* [`libp2p`](#libp2p)
|
||||||
|
* [`libp2p.peerStore`](#libp2ppeerStore)
|
||||||
* [Types](#types)
|
* [Types](#types)
|
||||||
* [`Stats`](#stats)
|
* [`Stats`](#stats)
|
||||||
|
|
||||||
@ -1099,7 +1101,9 @@ console.log(peerStats.toJSON())
|
|||||||
|
|
||||||
## Events
|
## Events
|
||||||
|
|
||||||
Once you have a libp2p instance, you can listen to several events it emits, so that you can be notified of relevant network events.
|
Once you have a libp2p instance, you can listen to several events it emits, so that you can be notified of relevant network events.
|
||||||
|
|
||||||
|
### libp2p
|
||||||
|
|
||||||
#### An error has occurred
|
#### An error has occurred
|
||||||
|
|
||||||
@ -1132,6 +1136,28 @@ This event will be triggered anytime we are disconnected from another peer, rega
|
|||||||
|
|
||||||
- `peer`: instance of [`PeerInfo`][peer-info]
|
- `peer`: instance of [`PeerInfo`][peer-info]
|
||||||
|
|
||||||
|
### libp2p.peerStore
|
||||||
|
|
||||||
|
#### A new peer is added to the peerStore
|
||||||
|
|
||||||
|
`libp2p.peerStore.on('peer', (peerId) => {})`
|
||||||
|
|
||||||
|
- `peerId`: instance of [`PeerId`][peer-id]
|
||||||
|
|
||||||
|
#### Known multiaddrs for a peer change
|
||||||
|
|
||||||
|
`libp2p.peerStore.on('change:multiaddrs', ({ peerId, multiaddrs}) => {})`
|
||||||
|
|
||||||
|
- `peerId`: instance of [`PeerId`][peer-id]
|
||||||
|
- `multiaddrs`: array of known [`multiaddr`][multiaddr] for the peer
|
||||||
|
|
||||||
|
#### Known protocols for a peer change
|
||||||
|
|
||||||
|
`libp2p.peerStore.on('change:protocols', ({ peerId, protocols}) => {})`
|
||||||
|
|
||||||
|
- `peerId`: instance of [`PeerId`][peer-id]
|
||||||
|
- `protocols`: array of known, supported protocols for the peer (string identifiers)
|
||||||
|
|
||||||
## Types
|
## Types
|
||||||
|
|
||||||
### Stats
|
### Stats
|
||||||
|
@ -86,7 +86,7 @@
|
|||||||
"cids": "^0.8.0",
|
"cids": "^0.8.0",
|
||||||
"delay": "^4.3.0",
|
"delay": "^4.3.0",
|
||||||
"dirty-chai": "^2.0.1",
|
"dirty-chai": "^2.0.1",
|
||||||
"interop-libp2p": "~0.0.1",
|
"interop-libp2p": "libp2p/interop#chore/update-libp2p-daemon-with-peerstore",
|
||||||
"it-concat": "^1.0.0",
|
"it-concat": "^1.0.0",
|
||||||
"it-pair": "^1.0.0",
|
"it-pair": "^1.0.0",
|
||||||
"it-pushable": "^1.4.0",
|
"it-pushable": "^1.4.0",
|
||||||
@ -94,8 +94,8 @@
|
|||||||
"libp2p-delegated-content-routing": "^0.4.5",
|
"libp2p-delegated-content-routing": "^0.4.5",
|
||||||
"libp2p-delegated-peer-routing": "^0.4.3",
|
"libp2p-delegated-peer-routing": "^0.4.3",
|
||||||
"libp2p-floodsub": "^0.20.0",
|
"libp2p-floodsub": "^0.20.0",
|
||||||
"libp2p-gossipsub": "^0.3.0",
|
"libp2p-gossipsub": "^0.2.6",
|
||||||
"libp2p-kad-dht": "^0.18.6",
|
"libp2p-kad-dht": "^0.19.0-pre.0",
|
||||||
"libp2p-mdns": "^0.13.0",
|
"libp2p-mdns": "^0.13.0",
|
||||||
"libp2p-mplex": "^0.9.5",
|
"libp2p-mplex": "^0.9.5",
|
||||||
"libp2p-secio": "^0.12.4",
|
"libp2p-secio": "^0.12.4",
|
||||||
|
@ -28,7 +28,7 @@ class AddressBook extends Book {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @constructor
|
* @constructor
|
||||||
* @param {EventEmitter} peerStore
|
* @param {PeerStore} peerStore
|
||||||
*/
|
*/
|
||||||
constructor (peerStore) {
|
constructor (peerStore) {
|
||||||
/**
|
/**
|
||||||
@ -80,6 +80,7 @@ class AddressBook extends Book {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.data.set(id, multiaddrInfos)
|
this.data.set(id, multiaddrInfos)
|
||||||
|
this._setPeerId(peerId)
|
||||||
log(`stored provided multiaddrs for ${id}`)
|
log(`stored provided multiaddrs for ${id}`)
|
||||||
|
|
||||||
// TODO: Remove peerInfo and its usage on peer-info deprecate
|
// TODO: Remove peerInfo and its usage on peer-info deprecate
|
||||||
@ -133,6 +134,7 @@ class AddressBook extends Book {
|
|||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this._setPeerId(peerId)
|
||||||
this.data.set(id, multiaddrInfos)
|
this.data.set(id, multiaddrInfos)
|
||||||
|
|
||||||
log(`added provided multiaddrs for ${id}`)
|
log(`added provided multiaddrs for ${id}`)
|
||||||
|
@ -82,6 +82,12 @@ class Book {
|
|||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_setPeerId (peerId) {
|
||||||
|
if (!this._ps.peerIds.get(peerId)) {
|
||||||
|
this._ps.peerIds.set(peerId.toB58String(), peerId)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Book
|
module.exports = Book
|
||||||
|
@ -43,97 +43,13 @@ class PeerStore extends EventEmitter {
|
|||||||
* ProtoBook containing a map of peerIdStr to supported protocols.
|
* ProtoBook containing a map of peerIdStr to supported protocols.
|
||||||
*/
|
*/
|
||||||
this.protoBook = new ProtoBook(this)
|
this.protoBook = new ProtoBook(this)
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Temporary adapter for modules using PeerStore
|
/**
|
||||||
// This should be removed under a breaking change
|
* TODO: this should only exist until we have the key-book
|
||||||
/**
|
* Map known peers to their peer-id.
|
||||||
* Stores the peerInfo of a new peer on each book.
|
* @type {Map<string, Array<PeerId>}
|
||||||
* @param {PeerInfo} peerInfo
|
*/
|
||||||
* @param {object} [options]
|
this.peerIds = new Map()
|
||||||
* @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<Multiaddr>}
|
|
||||||
*/
|
|
||||||
multiaddrsForPeer (peerInfo) {
|
|
||||||
return this.addressBook.getMultiaddrsForPeer(peerInfo.id)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -195,15 +111,16 @@ class PeerStore extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find the stored information of a given peer.
|
* Get the stored information of a given peer.
|
||||||
* @param {PeerId} peerId
|
* @param {PeerId} peerId
|
||||||
* @returns {peerInfo}
|
* @returns {peerInfo}
|
||||||
*/
|
*/
|
||||||
find (peerId) {
|
get (peerId) {
|
||||||
if (!PeerId.isPeerId(peerId)) {
|
if (!PeerId.isPeerId(peerId)) {
|
||||||
throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
|
throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const id = this.peerIds.get(peerId.toB58String())
|
||||||
const multiaddrInfos = this.addressBook.get(peerId)
|
const multiaddrInfos = this.addressBook.get(peerId)
|
||||||
const protocols = this.protoBook.get(peerId)
|
const protocols = this.protoBook.get(peerId)
|
||||||
|
|
||||||
@ -212,6 +129,7 @@ class PeerStore extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
id: id || peerId,
|
||||||
multiaddrInfos: multiaddrInfos || [],
|
multiaddrInfos: multiaddrInfos || [],
|
||||||
protocols: protocols || []
|
protocols: protocols || []
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,7 @@ const {
|
|||||||
class ProtoBook extends Book {
|
class ProtoBook extends Book {
|
||||||
/**
|
/**
|
||||||
* @constructor
|
* @constructor
|
||||||
* @param {EventEmitter} peerStore
|
* @param {PeerStore} peerStore
|
||||||
*/
|
*/
|
||||||
constructor (peerStore) {
|
constructor (peerStore) {
|
||||||
/**
|
/**
|
||||||
@ -71,6 +71,7 @@ class ProtoBook extends Book {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.data.set(id, newSet)
|
this.data.set(id, newSet)
|
||||||
|
this._setPeerId(peerId)
|
||||||
log(`stored provided protocols for ${id}`)
|
log(`stored provided protocols for ${id}`)
|
||||||
|
|
||||||
// TODO: Remove peerInfo and its usage on peer-info deprecate
|
// TODO: Remove peerInfo and its usage on peer-info deprecate
|
||||||
@ -118,6 +119,7 @@ class ProtoBook extends Book {
|
|||||||
protocols = [...newSet]
|
protocols = [...newSet]
|
||||||
|
|
||||||
this.data.set(id, newSet)
|
this.data.set(id, newSet)
|
||||||
|
this._setPeerId(peerId)
|
||||||
log(`added provided protocols for ${id}`)
|
log(`added provided protocols for ${id}`)
|
||||||
|
|
||||||
// TODO: Remove peerInfo and its usage on peer-info deprecate
|
// TODO: Remove peerInfo and its usage on peer-info deprecate
|
||||||
|
@ -75,8 +75,8 @@ describe('DHT subsystem operates correctly', () => {
|
|||||||
])
|
])
|
||||||
|
|
||||||
await libp2p.contentRouting.put(key, value)
|
await libp2p.contentRouting.put(key, value)
|
||||||
|
|
||||||
const fetchedValue = await remoteLibp2p.contentRouting.get(key)
|
const fetchedValue = await remoteLibp2p.contentRouting.get(key)
|
||||||
|
|
||||||
expect(fetchedValue).to.eql(value)
|
expect(fetchedValue).to.eql(value)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -5,11 +5,10 @@ const chai = require('chai')
|
|||||||
chai.use(require('dirty-chai'))
|
chai.use(require('dirty-chai'))
|
||||||
const { expect } = chai
|
const { expect } = chai
|
||||||
|
|
||||||
const { EventEmitter } = require('events')
|
|
||||||
const pDefer = require('p-defer')
|
const pDefer = require('p-defer')
|
||||||
const multiaddr = require('multiaddr')
|
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 peerUtils = require('../utils/creators/peer')
|
||||||
const {
|
const {
|
||||||
@ -30,15 +29,15 @@ describe('addressBook', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('addressBook.set', () => {
|
describe('addressBook.set', () => {
|
||||||
let ee, ab
|
let peerStore, ab
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
ee = new EventEmitter()
|
peerStore = new PeerStore()
|
||||||
ab = new AddressBook(ee)
|
ab = peerStore.addressBook
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
ee.removeAllListeners()
|
peerStore.removeAllListeners()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('throwns invalid parameters error if invalid PeerId is provided', () => {
|
it('throwns invalid parameters error if invalid PeerId is provided', () => {
|
||||||
@ -63,7 +62,7 @@ describe('addressBook', () => {
|
|||||||
const defer = pDefer()
|
const defer = pDefer()
|
||||||
const supportedMultiaddrs = [addr1, addr2]
|
const supportedMultiaddrs = [addr1, addr2]
|
||||||
|
|
||||||
ee.once('change:multiaddrs', ({ peerId, multiaddrs }) => {
|
peerStore.once('change:multiaddrs', ({ peerId, multiaddrs }) => {
|
||||||
expect(peerId).to.exist()
|
expect(peerId).to.exist()
|
||||||
expect(multiaddrs).to.eql(supportedMultiaddrs)
|
expect(multiaddrs).to.eql(supportedMultiaddrs)
|
||||||
defer.resolve()
|
defer.resolve()
|
||||||
@ -84,7 +83,7 @@ describe('addressBook', () => {
|
|||||||
const supportedMultiaddrsB = [addr2]
|
const supportedMultiaddrsB = [addr2]
|
||||||
|
|
||||||
let changeCounter = 0
|
let changeCounter = 0
|
||||||
ee.on('change:multiaddrs', () => {
|
peerStore.on('change:multiaddrs', () => {
|
||||||
changeCounter++
|
changeCounter++
|
||||||
if (changeCounter > 1) {
|
if (changeCounter > 1) {
|
||||||
defer.resolve()
|
defer.resolve()
|
||||||
@ -109,7 +108,7 @@ describe('addressBook', () => {
|
|||||||
const supportedMultiaddrs = [addr1, addr2]
|
const supportedMultiaddrs = [addr1, addr2]
|
||||||
|
|
||||||
let changeCounter = 0
|
let changeCounter = 0
|
||||||
ee.on('change:multiaddrs', () => {
|
peerStore.on('change:multiaddrs', () => {
|
||||||
changeCounter++
|
changeCounter++
|
||||||
if (changeCounter > 1) {
|
if (changeCounter > 1) {
|
||||||
defer.reject()
|
defer.reject()
|
||||||
@ -132,15 +131,15 @@ describe('addressBook', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('addressBook.add', () => {
|
describe('addressBook.add', () => {
|
||||||
let ee, ab
|
let peerStore, ab
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
ee = new EventEmitter()
|
peerStore = new PeerStore()
|
||||||
ab = new AddressBook(ee)
|
ab = peerStore.addressBook
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
ee.removeAllListeners()
|
peerStore.removeAllListeners()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('throwns invalid parameters error if invalid PeerId is provided', () => {
|
it('throwns invalid parameters error if invalid PeerId is provided', () => {
|
||||||
@ -169,7 +168,7 @@ describe('addressBook', () => {
|
|||||||
const finalMultiaddrs = supportedMultiaddrsA.concat(supportedMultiaddrsB)
|
const finalMultiaddrs = supportedMultiaddrsA.concat(supportedMultiaddrsB)
|
||||||
|
|
||||||
let changeTrigger = 2
|
let changeTrigger = 2
|
||||||
ee.on('change:multiaddrs', ({ multiaddrs }) => {
|
peerStore.on('change:multiaddrs', ({ multiaddrs }) => {
|
||||||
changeTrigger--
|
changeTrigger--
|
||||||
if (changeTrigger === 0 && arraysAreEqual(multiaddrs, finalMultiaddrs)) {
|
if (changeTrigger === 0 && arraysAreEqual(multiaddrs, finalMultiaddrs)) {
|
||||||
defer.resolve()
|
defer.resolve()
|
||||||
@ -199,7 +198,7 @@ describe('addressBook', () => {
|
|||||||
const finalMultiaddrs = supportedMultiaddrsA.concat(supportedMultiaddrsB)
|
const finalMultiaddrs = supportedMultiaddrsA.concat(supportedMultiaddrsB)
|
||||||
|
|
||||||
let changeCounter = 0
|
let changeCounter = 0
|
||||||
ee.on('change:multiaddrs', () => {
|
peerStore.on('change:multiaddrs', () => {
|
||||||
changeCounter++
|
changeCounter++
|
||||||
if (changeCounter > 1) {
|
if (changeCounter > 1) {
|
||||||
defer.resolve()
|
defer.resolve()
|
||||||
@ -225,7 +224,7 @@ describe('addressBook', () => {
|
|||||||
const supportedMultiaddrsB = [addr2]
|
const supportedMultiaddrsB = [addr2]
|
||||||
|
|
||||||
let changeCounter = 0
|
let changeCounter = 0
|
||||||
ee.on('change:multiaddrs', () => {
|
peerStore.on('change:multiaddrs', () => {
|
||||||
changeCounter++
|
changeCounter++
|
||||||
if (changeCounter > 1) {
|
if (changeCounter > 1) {
|
||||||
defer.reject()
|
defer.reject()
|
||||||
@ -248,11 +247,11 @@ describe('addressBook', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('addressBook.get', () => {
|
describe('addressBook.get', () => {
|
||||||
let ee, ab
|
let peerStore, ab
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
ee = new EventEmitter()
|
peerStore = new PeerStore()
|
||||||
ab = new AddressBook(ee)
|
ab = peerStore.addressBook
|
||||||
})
|
})
|
||||||
|
|
||||||
it('throwns invalid parameters error if invalid PeerId is provided', () => {
|
it('throwns invalid parameters error if invalid PeerId is provided', () => {
|
||||||
@ -279,11 +278,11 @@ describe('addressBook', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('addressBook.getMultiaddrsForPeer', () => {
|
describe('addressBook.getMultiaddrsForPeer', () => {
|
||||||
let ee, ab
|
let peerStore, ab
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
ee = new EventEmitter()
|
peerStore = new PeerStore()
|
||||||
ab = new AddressBook(ee)
|
ab = peerStore.addressBook
|
||||||
})
|
})
|
||||||
|
|
||||||
it('throwns invalid parameters error if invalid PeerId is provided', () => {
|
it('throwns invalid parameters error if invalid PeerId is provided', () => {
|
||||||
@ -311,11 +310,11 @@ describe('addressBook', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('addressBook.delete', () => {
|
describe('addressBook.delete', () => {
|
||||||
let ee, ab
|
let peerStore, ab
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
ee = new EventEmitter()
|
peerStore = new PeerStore()
|
||||||
ab = new AddressBook(ee)
|
ab = peerStore.addressBook
|
||||||
})
|
})
|
||||||
|
|
||||||
it('throwns invalid parameters error if invalid PeerId is provided', () => {
|
it('throwns invalid parameters error if invalid PeerId is provided', () => {
|
||||||
@ -327,7 +326,7 @@ describe('addressBook', () => {
|
|||||||
it('returns false if no records exist for the peer and no event is emitted', () => {
|
it('returns false if no records exist for the peer and no event is emitted', () => {
|
||||||
const defer = pDefer()
|
const defer = pDefer()
|
||||||
|
|
||||||
ee.on('change:multiaddrs', () => {
|
peerStore.on('change:multiaddrs', () => {
|
||||||
defer.reject()
|
defer.reject()
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -350,7 +349,7 @@ describe('addressBook', () => {
|
|||||||
ab.set(peerId, supportedMultiaddrs)
|
ab.set(peerId, supportedMultiaddrs)
|
||||||
|
|
||||||
// Listen after set
|
// Listen after set
|
||||||
ee.on('change:multiaddrs', ({ multiaddrs }) => {
|
peerStore.on('change:multiaddrs', ({ multiaddrs }) => {
|
||||||
expect(multiaddrs.length).to.eql(0)
|
expect(multiaddrs.length).to.eql(0)
|
||||||
defer.resolve()
|
defer.resolve()
|
||||||
})
|
})
|
||||||
|
@ -45,7 +45,7 @@ describe('peer-store', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('returns undefined on trying to find a non existant peerId', () => {
|
it('returns undefined on trying to find a non existant peerId', () => {
|
||||||
const peerInfo = peerStore.find(peerIds[0])
|
const peerInfo = peerStore.get(peerIds[0])
|
||||||
expect(peerInfo).to.not.exist()
|
expect(peerInfo).to.not.exist()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@ -101,8 +101,8 @@ describe('peer-store', () => {
|
|||||||
expect(peers.size).to.equal(3)
|
expect(peers.size).to.equal(3)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('finds the stored information of a peer in all its books', () => {
|
it('gets the stored information of a peer in all its books', () => {
|
||||||
const peerInfo = peerStore.find(peerIds[0])
|
const peerInfo = peerStore.get(peerIds[0])
|
||||||
expect(peerInfo).to.exist()
|
expect(peerInfo).to.exist()
|
||||||
expect(peerInfo.protocols).to.have.members([proto1])
|
expect(peerInfo.protocols).to.have.members([proto1])
|
||||||
|
|
||||||
@ -110,8 +110,8 @@ describe('peer-store', () => {
|
|||||||
expect(peerMultiaddrs).to.have.members([addr1, addr2])
|
expect(peerMultiaddrs).to.have.members([addr1, addr2])
|
||||||
})
|
})
|
||||||
|
|
||||||
it('finds the stored information of a peer that is not present in all its books', () => {
|
it('gets the stored information of a peer that is not present in all its books', () => {
|
||||||
const peerInfo = peerStore.find(peerIds[2])
|
const peerInfo = peerStore.get(peerIds[2])
|
||||||
expect(peerInfo).to.exist()
|
expect(peerInfo).to.exist()
|
||||||
expect(peerInfo.protocols.length).to.eql(0)
|
expect(peerInfo.protocols.length).to.eql(0)
|
||||||
|
|
||||||
|
@ -5,10 +5,9 @@ const chai = require('chai')
|
|||||||
chai.use(require('dirty-chai'))
|
chai.use(require('dirty-chai'))
|
||||||
const { expect } = chai
|
const { expect } = chai
|
||||||
|
|
||||||
const { EventEmitter } = require('events')
|
|
||||||
const pDefer = require('p-defer')
|
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 peerUtils = require('../utils/creators/peer')
|
||||||
const {
|
const {
|
||||||
@ -25,15 +24,15 @@ describe('protoBook', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('protoBook.set', () => {
|
describe('protoBook.set', () => {
|
||||||
let ee, pb
|
let peerStore, pb
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
ee = new EventEmitter()
|
peerStore = new PeerStore()
|
||||||
pb = new ProtoBook(ee)
|
pb = peerStore.protoBook
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
ee.removeAllListeners()
|
peerStore.removeAllListeners()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('throwns invalid parameters error if invalid PeerId is provided', () => {
|
it('throwns invalid parameters error if invalid PeerId is provided', () => {
|
||||||
@ -52,7 +51,7 @@ describe('protoBook', () => {
|
|||||||
const defer = pDefer()
|
const defer = pDefer()
|
||||||
const supportedProtocols = ['protocol1', 'protocol2']
|
const supportedProtocols = ['protocol1', 'protocol2']
|
||||||
|
|
||||||
ee.once('change:protocols', ({ peerId, protocols }) => {
|
peerStore.once('change:protocols', ({ peerId, protocols }) => {
|
||||||
expect(peerId).to.exist()
|
expect(peerId).to.exist()
|
||||||
expect(protocols).to.have.deep.members(supportedProtocols)
|
expect(protocols).to.have.deep.members(supportedProtocols)
|
||||||
defer.resolve()
|
defer.resolve()
|
||||||
@ -72,7 +71,7 @@ describe('protoBook', () => {
|
|||||||
const supportedProtocolsB = ['protocol2']
|
const supportedProtocolsB = ['protocol2']
|
||||||
|
|
||||||
let changeCounter = 0
|
let changeCounter = 0
|
||||||
ee.on('change:protocols', () => {
|
peerStore.on('change:protocols', () => {
|
||||||
changeCounter++
|
changeCounter++
|
||||||
if (changeCounter > 1) {
|
if (changeCounter > 1) {
|
||||||
defer.resolve()
|
defer.resolve()
|
||||||
@ -96,7 +95,7 @@ describe('protoBook', () => {
|
|||||||
const supportedProtocols = ['protocol1', 'protocol2']
|
const supportedProtocols = ['protocol1', 'protocol2']
|
||||||
|
|
||||||
let changeCounter = 0
|
let changeCounter = 0
|
||||||
ee.on('change:protocols', () => {
|
peerStore.on('change:protocols', () => {
|
||||||
changeCounter++
|
changeCounter++
|
||||||
if (changeCounter > 1) {
|
if (changeCounter > 1) {
|
||||||
defer.reject()
|
defer.reject()
|
||||||
@ -119,15 +118,15 @@ describe('protoBook', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('protoBook.add', () => {
|
describe('protoBook.add', () => {
|
||||||
let ee, pb
|
let peerStore, pb
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
ee = new EventEmitter()
|
peerStore = new PeerStore()
|
||||||
pb = new ProtoBook(ee)
|
pb = peerStore.protoBook
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
ee.removeAllListeners()
|
peerStore.removeAllListeners()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('throwns invalid parameters error if invalid PeerId is provided', () => {
|
it('throwns invalid parameters error if invalid PeerId is provided', () => {
|
||||||
@ -150,7 +149,7 @@ describe('protoBook', () => {
|
|||||||
const finalProtocols = supportedProtocolsA.concat(supportedProtocolsB)
|
const finalProtocols = supportedProtocolsA.concat(supportedProtocolsB)
|
||||||
|
|
||||||
let changeTrigger = 2
|
let changeTrigger = 2
|
||||||
ee.on('change:protocols', ({ protocols }) => {
|
peerStore.on('change:protocols', ({ protocols }) => {
|
||||||
changeTrigger--
|
changeTrigger--
|
||||||
if (changeTrigger === 0 && arraysAreEqual(protocols, finalProtocols)) {
|
if (changeTrigger === 0 && arraysAreEqual(protocols, finalProtocols)) {
|
||||||
defer.resolve()
|
defer.resolve()
|
||||||
@ -178,7 +177,7 @@ describe('protoBook', () => {
|
|||||||
const finalProtocols = supportedProtocolsA.concat(supportedProtocolsB)
|
const finalProtocols = supportedProtocolsA.concat(supportedProtocolsB)
|
||||||
|
|
||||||
let changeCounter = 0
|
let changeCounter = 0
|
||||||
ee.on('change:protocols', () => {
|
peerStore.on('change:protocols', () => {
|
||||||
changeCounter++
|
changeCounter++
|
||||||
if (changeCounter > 1) {
|
if (changeCounter > 1) {
|
||||||
defer.resolve()
|
defer.resolve()
|
||||||
@ -203,7 +202,7 @@ describe('protoBook', () => {
|
|||||||
const supportedProtocolsB = ['protocol2']
|
const supportedProtocolsB = ['protocol2']
|
||||||
|
|
||||||
let changeCounter = 0
|
let changeCounter = 0
|
||||||
ee.on('change:protocols', () => {
|
peerStore.on('change:protocols', () => {
|
||||||
changeCounter++
|
changeCounter++
|
||||||
if (changeCounter > 1) {
|
if (changeCounter > 1) {
|
||||||
defer.reject()
|
defer.reject()
|
||||||
@ -226,11 +225,11 @@ describe('protoBook', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('protoBook.get', () => {
|
describe('protoBook.get', () => {
|
||||||
let ee, pb
|
let peerStore, pb
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
ee = new EventEmitter()
|
peerStore = new PeerStore()
|
||||||
pb = new ProtoBook(ee)
|
pb = peerStore.protoBook
|
||||||
})
|
})
|
||||||
|
|
||||||
it('throwns invalid parameters error if invalid PeerId is provided', () => {
|
it('throwns invalid parameters error if invalid PeerId is provided', () => {
|
||||||
@ -256,11 +255,11 @@ describe('protoBook', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('protoBook.delete', () => {
|
describe('protoBook.delete', () => {
|
||||||
let ee, pb
|
let peerStore, pb
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
ee = new EventEmitter()
|
peerStore = new PeerStore()
|
||||||
pb = new ProtoBook(ee)
|
pb = peerStore.protoBook
|
||||||
})
|
})
|
||||||
|
|
||||||
it('throwns invalid parameters error if invalid PeerId is provided', () => {
|
it('throwns invalid parameters error if invalid PeerId is provided', () => {
|
||||||
@ -272,7 +271,7 @@ describe('protoBook', () => {
|
|||||||
it('returns false if no records exist for the peer and no event is emitted', () => {
|
it('returns false if no records exist for the peer and no event is emitted', () => {
|
||||||
const defer = pDefer()
|
const defer = pDefer()
|
||||||
|
|
||||||
ee.on('change:protocols', () => {
|
peerStore.on('change:protocols', () => {
|
||||||
defer.reject()
|
defer.reject()
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -295,7 +294,7 @@ describe('protoBook', () => {
|
|||||||
pb.set(peerId, supportedProtocols)
|
pb.set(peerId, supportedProtocols)
|
||||||
|
|
||||||
// Listen after set
|
// Listen after set
|
||||||
ee.on('change:protocols', ({ protocols }) => {
|
peerStore.on('change:protocols', ({ protocols }) => {
|
||||||
expect(protocols.length).to.eql(0)
|
expect(protocols.length).to.eql(0)
|
||||||
defer.resolve()
|
defer.resolve()
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user