Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

Commit

Permalink
chore: peer-discovery not using peer-info
Browse files Browse the repository at this point in the history
BREAKING CHANGE: peer event emits an object with id and multiaddr instead of a peer-info
  • Loading branch information
vasco-santos committed Apr 6, 2020
1 parent a87c1f7 commit 8a99f1b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@
"debug": "^4.1.1",
"mafmt": "^7.0.0",
"multiaddr": "^7.2.1",
"peer-id": "^0.13.5",
"peer-info": "^0.17.0"
"peer-id": "^0.13.5"
},
"pre-push": [
"lint",
Expand Down
10 changes: 5 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const multiaddr = require('multiaddr')
const mafmt = require('mafmt')
const { EventEmitter } = require('events')
Expand Down Expand Up @@ -50,7 +49,7 @@ class Bootstrap extends EventEmitter {
* Emit each address in the list as a PeerInfo.
*/
_discoverBootstrapPeers () {
this._list.forEach(async (candidate) => {
this._list.forEach((candidate) => {
if (!mafmt.P2P.matches(candidate)) {
return log.error('Invalid multiaddr')
}
Expand All @@ -60,9 +59,10 @@ class Bootstrap extends EventEmitter {
const peerId = PeerId.createFromB58String(ma.getPeerId())

try {
const peerInfo = await PeerInfo.create(peerId)
peerInfo.multiaddrs.add(ma)
this.emit('peer', peerInfo)
this.emit('peer', {
id: peerId,
multiaddrs: [ma]
})
} catch (err) {
log.error('Invalid bootstrap peer id', err)
}
Expand Down
13 changes: 8 additions & 5 deletions test/bootstrap.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ const chai = require('chai')
chai.use(require('dirty-chai'))
const { expect } = chai

const mafmt = require('mafmt')
const PeerId = require('peer-id')

const Bootstrap = require('../src')
const peerList = require('./default-peers')
const partialValidPeerList = require('./some-invalid-peers')
const mafmt = require('mafmt')

describe('bootstrap', () => {
it('should throw if no peer list is provided', () => {
Expand Down Expand Up @@ -42,10 +44,11 @@ describe('bootstrap', () => {
})

const p = new Promise((resolve) => {
r.once('peer', (peer) => {
const peerList = peer.multiaddrs.toArray()
expect(peerList.length).to.eq(1)
expect(mafmt.IPFS.matches(peerList[0].toString())).equals(true)
r.once('peer', ({ id, multiaddrs }) => {
expect(id).to.exist()
expect(PeerId.isPeerId(id)).to.eql(true)
expect(multiaddrs.length).to.eq(1)
expect(mafmt.IPFS.matches(multiaddrs[0].toString())).equals(true)
resolve()
})
})
Expand Down

0 comments on commit 8a99f1b

Please sign in to comment.