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

chore: peer-discovery not using peer-info #103

Merged
merged 3 commits into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@
"devDependencies": {
"aegir": "^21.2.0",
"chai": "^4.2.0",
"dirty-chai": "^2.0.1"
"dirty-chai": "^2.0.1",
"libp2p-interfaces": "^0.3.0"
},
"dependencies": {
"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
20 changes: 11 additions & 9 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,11 @@ class Bootstrap extends EventEmitter {
* Emit each address in the list as a PeerInfo.
*/
_discoverBootstrapPeers () {
this._list.forEach(async (candidate) => {
if (!this._timer) {
return
}

this._list.forEach((candidate) => {
if (!mafmt.P2P.matches(candidate)) {
return log.error('Invalid multiaddr')
}
Expand All @@ -60,9 +63,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 All @@ -73,10 +77,8 @@ class Bootstrap extends EventEmitter {
* Stop emitting events.
*/
stop () {
if (this._timer) {
clearInterval(this._timer)
this._timer = null
}
clearInterval(this._timer)
this._timer = null
}
}

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
21 changes: 21 additions & 0 deletions test/compliance.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict'

/* eslint-env mocha */

const tests = require('libp2p-interfaces/src/peer-discovery/tests')

const Bootstrap = require('../src')
const peerList = require('./default-peers')

describe('compliance tests', () => {
tests({
setup () {
const bootstrap = new Bootstrap({
list: peerList,
interval: 2000
})

return bootstrap
}
})
})