Skip to content

Commit

Permalink
chore: deprecate old peer-store api
Browse files Browse the repository at this point in the history
BREAKING CHANGE: the peer-store api changed. Check the API docs for the new specification.
  • Loading branch information
vasco-santos committed Apr 10, 2020
1 parent c99a7a2 commit af6454f
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 152 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"cids": "^0.8.0",
"delay": "^4.3.0",
"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-pair": "^1.0.0",
"it-pushable": "^1.4.0",
Expand All @@ -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",
Expand Down
10 changes: 9 additions & 1 deletion src/peer-store/address-book.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class AddressBook extends Book {

/**
* @constructor
* @param {EventEmitter} peerStore
* @param {PeerStore} peerStore
*/
constructor (peerStore) {
/**
Expand Down Expand Up @@ -80,6 +80,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
Expand Down Expand Up @@ -133,6 +134,7 @@ class AddressBook extends Book {
return this
}

this._setPeerId(peerId)
this.data.set(id, multiaddrInfos)

log(`added provided multiaddrs for ${id}`)
Expand Down Expand Up @@ -183,6 +185,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.
Expand Down
102 changes: 10 additions & 92 deletions src/peer-store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<Multiaddr>}
*/
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<string, Array<PeerId>}
*/
this.peerIds = new Map()
}

/**
Expand Down Expand Up @@ -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
* @returns {peerInfo}
*/
find (peerId) {
get (peerId) {
if (!PeerId.isPeerId(peerId)) {
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)

Expand All @@ -212,6 +129,7 @@ class PeerStore extends EventEmitter {
}

return {
id: id || peerId,
multiaddrInfos: multiaddrInfos || [],
protocols: protocols || []
}
Expand Down
10 changes: 9 additions & 1 deletion src/peer-store/proto-book.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const {
class ProtoBook extends Book {
/**
* @constructor
* @param {EventEmitter} peerStore
* @param {PeerStore} peerStore
*/
constructor (peerStore) {
/**
Expand Down Expand Up @@ -71,6 +71,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
Expand Down Expand Up @@ -118,6 +119,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
Expand All @@ -132,6 +134,12 @@ class ProtoBook extends Book {

return this
}

_setPeerId (peerId) {
if (!this._ps.peerIds.get(peerId)) {
this._ps.peerIds.set(peerId.toString(), peerId)
}
}
}

module.exports = ProtoBook
53 changes: 26 additions & 27 deletions test/peer-store/address-book.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ 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 PeerStore = require('../../src/peer-store')

const peerUtils = require('../utils/creators/peer')
const {
Expand All @@ -30,15 +29,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', () => {
Expand All @@ -63,7 +62,7 @@ describe('addressBook', () => {
const defer = pDefer()
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()
Expand All @@ -84,7 +83,7 @@ describe('addressBook', () => {
const supportedMultiaddrsB = [addr2]

let changeCounter = 0
ee.on('change:multiaddrs', () => {
peerStore.on('change:multiaddrs', () => {
changeCounter++
if (changeCounter > 1) {
defer.resolve()
Expand All @@ -109,7 +108,7 @@ describe('addressBook', () => {
const supportedMultiaddrs = [addr1, addr2]

let changeCounter = 0
ee.on('change:multiaddrs', () => {
peerStore.on('change:multiaddrs', () => {
changeCounter++
if (changeCounter > 1) {
defer.reject()
Expand All @@ -132,15 +131,15 @@ describe('addressBook', () => {
})

describe('addressBook.add', () => {
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', () => {
Expand Down Expand Up @@ -169,7 +168,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()
Expand Down Expand Up @@ -199,7 +198,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()
Expand All @@ -225,7 +224,7 @@ describe('addressBook', () => {
const supportedMultiaddrsB = [addr2]

let changeCounter = 0
ee.on('change:multiaddrs', () => {
peerStore.on('change:multiaddrs', () => {
changeCounter++
if (changeCounter > 1) {
defer.reject()
Expand All @@ -248,11 +247,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', () => {
Expand All @@ -279,11 +278,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', () => {
Expand Down Expand Up @@ -311,11 +310,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', () => {
Expand All @@ -327,7 +326,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()
})

Expand All @@ -350,7 +349,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()
})
Expand Down
Loading

0 comments on commit af6454f

Please sign in to comment.