Skip to content

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 17, 2020
1 parent 4ed124b commit 26d4cb4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@
"aegir": "^21.4.5",
"chai": "^4.2.0",
"dirty-chai": "^2.0.1",
"libp2p-interfaces": "^0.2.7",
"libp2p-interfaces": "libp2p/js-interfaces#v0.3.x",
"p-defer": "^3.0.0",
"p-wait-for": "^3.1.0",
"peer-info": "^0.17.5",
"sinon": "^9.0.1"
},
"dependencies": {
"debug": "^4.1.1",
"emittery": "^0.6.0",
"multiaddr": "^7.4.3",
"peer-id": "^0.13.11",
"peer-info": "^0.17.5",
"protons": "^1.0.2"
},
"contributors": [
Expand Down
19 changes: 15 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const Emittery = require('emittery')
const debug = require('debug')

const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const multiaddr = require('multiaddr')

const PB = require('./peer.proto')
Expand Down Expand Up @@ -54,6 +53,7 @@ class PubsubPeerDiscovery extends Emittery {
}

this.removeListener = this.off.bind(this)
this.removeAllListeners = this.clearListeners.bind(this)
}

/**
Expand Down Expand Up @@ -122,14 +122,25 @@ class PubsubPeerDiscovery extends Emittery {
// Ignore if we received our own response
if (peerId.equals(this.libp2p.peerInfo.id)) return

const peerInfo = new PeerInfo(peerId)
peer.addrs.forEach(buffer => peerInfo.multiaddrs.add(multiaddr(buffer)))
log('discovered peer %j on %j', peerId, message.topicIDs)
this.emit('peer', peerInfo)
this._onNewPeer(peerId, peer.addrs)
// this.emit('peer', {
// id: peerId,
// multiaddrs: peer.addrs.map(b => multiaddr(b))
// })
} catch (err) {
log.error(err)
}
}

_onNewPeer (peerId, addrs) {
if (!this._intervalId) return

this.emit('peer', {
id: peerId,
multiaddrs: addrs.map(b => multiaddr(b))
})
}
}

PubsubPeerDiscovery.TOPIC = TOPIC
Expand Down
11 changes: 10 additions & 1 deletion test/compliance.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ const PeerID = require('peer-id')
const PeerInfo = require('peer-info')

describe('compliance tests', () => {
let intervalId
tests({
async setup () {
const peerId = await PeerID.create({ bits: 512 })
const peerInfo = new PeerInfo(peerId)
await new Promise(resolve => setTimeout(resolve, 10))
return new PubsubPeerDiscovery({

const pubsubDiscovery = new PubsubPeerDiscovery({
libp2p: {
peerInfo,
pubsub: {
Expand All @@ -23,9 +25,16 @@ describe('compliance tests', () => {
}
}
})

intervalId = setInterval(() => {
pubsubDiscovery._onNewPeer(peerId, ['/ip4/166.10.1.2/tcp/80'])
}, 1000)

return pubsubDiscovery
},
async teardown () {
await new Promise(resolve => setTimeout(resolve, 10))
clearInterval(intervalId)
}
})
})
22 changes: 15 additions & 7 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const sinon = require('sinon')
const defer = require('p-defer')
const pWaitFor = require('p-wait-for')

const multiaddr = require('multiaddr')
const PeerID = require('peer-id')
const PeerInfo = require('peer-info')

Expand Down Expand Up @@ -61,13 +62,19 @@ describe('Pubsub Peer Discovery', () => {

it('should be able to encode/decode a message', async () => {
discovery = new PubsubPeerDiscovery({ libp2p: mockLibp2p })
discovery.start()

const peerId = await PeerID.create({ bits: 512 })
const expectedPeerInfo = new PeerInfo(peerId)
expectedPeerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/8080/ws')
expectedPeerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/8081/ws')
const expectedPeerData = {
id: peerId,
multiaddrs: [
'/ip4/0.0.0.0/tcp/8080/ws',
'/ip4/0.0.0.0/tcp/8081/ws'
]
}
const peer = {
publicKey: peerId.pubKey.bytes,
addrs: expectedPeerInfo.multiaddrs.toArray().map(ma => ma.buffer)
addrs: expectedPeerData.multiaddrs.map(ma => multiaddr(ma).buffer)
}

const deferred = defer()
Expand All @@ -80,9 +87,10 @@ describe('Pubsub Peer Discovery', () => {
await discovery._onMessage({ data: encodedPeer, topicIDs: [PubsubPeerDiscovery.TOPIC] })

const discoveredPeer = await deferred.promise
expect(discoveredPeer.id.equals(expectedPeerInfo.id)).to.equal(true)
expectedPeerInfo.multiaddrs.forEach(addr => {
expect(discoveredPeer.multiaddrs.has(addr)).to.equal(true)
expect(discoveredPeer.id.equals(expectedPeerData.id)).to.equal(true)

discoveredPeer.multiaddrs.forEach(addr => {
expect(expectedPeerData.multiaddrs.includes(addr.toString())).to.equal(true)
})
})

Expand Down

0 comments on commit 26d4cb4

Please sign in to comment.