Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@
"devDependencies": {
"aegir": "^21.4.5",
"chai": "^4.2.0",
"chai-bytes": "^0.1.2",
"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",
"sinon": "^9.0.1"
Expand All @@ -52,7 +53,6 @@
"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
24 changes: 17 additions & 7 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 All @@ -31,17 +30,20 @@ class PubsubPeerDiscovery extends Emittery {
* @constructor
* @param {Libp2p} param0.libp2p Our libp2p node
* @param {number} [param0.interval = 10000] How often (ms) we should broadcast our infos
* @param {Array<Multiaddr>} param0.multiaddrs Multiaddrs used by the node to listen.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How are these intended to get passed in? I would recommend not doing this and instead we should just leverage the announce addrs. I don't believe that's done yet, so in the interim we may want to just do this.libp2p.transportManager.getAddrs(). This removes any need to add special options for the addresses, which is not friendly for new users.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, we can use the transportManager.getAddrs() and I will open an issue to use the addressManager in the future

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the param, using the transport manager for now and issue created #6

* @param {Array<string>} [param0.topics = PubsubPeerDiscovery.TOPIC] What topics to subscribe to. If set, the default will NOT be used.
* @param {boolean} [param0.listenOnly = false] If true, we will not broadcast our peer data
*/
constructor ({
libp2p,
interval = 10000,
topics,
multiaddrs = [],
listenOnly = false
}) {
super()
this.libp2p = libp2p
this.multiaddrs = multiaddrs
this.interval = interval
this._intervalId = null
this._listenOnly = listenOnly
Expand All @@ -54,6 +56,7 @@ class PubsubPeerDiscovery extends Emittery {
}

this.removeListener = this.off.bind(this)
this.removeAllListeners = this.clearListeners.bind(this)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's using this? If this is being used we likely need to get more explicit about what event api peer-discovery modules should be exposing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is being used fot the tests: https://github.com/libp2p/js-libp2p-interfaces/blob/v0.3.0/src/peer-discovery/tests/index.js#L25

I agree that we should be explicit about it.

}

/**
Expand Down Expand Up @@ -97,8 +100,8 @@ class PubsubPeerDiscovery extends Emittery {
*/
_broadcast () {
const peer = {
publicKey: this.libp2p.peerInfo.id.pubKey.bytes,
addrs: this.libp2p.peerInfo.multiaddrs.toArray().map(ma => ma.buffer)
publicKey: this.libp2p.peerId.pubKey.bytes,
addrs: this.multiaddrs.map(ma => ma.buffer)
}

const encodedPeer = PB.Peer.encode(peer)
Expand All @@ -120,16 +123,23 @@ class PubsubPeerDiscovery extends Emittery {
const peerId = await PeerId.createFromPubKey(peer.publicKey)

// Ignore if we received our own response
if (peerId.equals(this.libp2p.peerInfo.id)) return
if (peerId.equals(this.libp2p.peerId)) 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)
} catch (err) {
log.error(err)
}
}

_onNewPeer (peerId, addrs) {
jacobheun marked this conversation as resolved.
Show resolved Hide resolved
if (!this._intervalId) return

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

PubsubPeerDiscovery.TOPIC = TOPIC
Expand Down
15 changes: 11 additions & 4 deletions test/compliance.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,34 @@ const tests = require('libp2p-interfaces/src/peer-discovery/tests')
const PubsubPeerDiscovery = require('../src')

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,
peerId,
pubsub: {
subscribe: () => {},
unsubscribe: () => {},
publish: () => {}
}
}
})

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)
}
})
})
39 changes: 24 additions & 15 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,29 @@

const chai = require('chai')
chai.use(require('dirty-chai'))
chai.use(require('chai-bytes'))
const { expect } = chai
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')

const PubsubPeerDiscovery = require('../src')
const PB = require('../src/peer.proto')

const listeningMultiaddrs = multiaddr('/ip4/127.0.0.1/tcp/9000/ws')

describe('Pubsub Peer Discovery', () => {
let mockLibp2p
let discovery

before(async () => {
const peerInfo = await PeerInfo.create()
peerInfo.multiaddrs.add('/ip4/127.0.0.1/tcp/9000/ws')
const peerId = await PeerID.create()

mockLibp2p = {
peerInfo,
peerId,
pubsub: {
subscribe: () => {},
publish: () => {},
Expand All @@ -38,18 +40,18 @@ describe('Pubsub Peer Discovery', () => {
})

it('should not discover self', async () => {
discovery = new PubsubPeerDiscovery({ libp2p: mockLibp2p })
discovery = new PubsubPeerDiscovery({ libp2p: mockLibp2p, multiaddrs: [listeningMultiaddrs] })
sinon.spy(mockLibp2p.pubsub, 'publish')
discovery._broadcast()
expect(mockLibp2p.pubsub.publish.callCount).to.equal(1)

const [topic, encodedPeer] = mockLibp2p.pubsub.publish.getCall(0).args
const peer = PB.Peer.decode(encodedPeer)
const peerId = await PeerID.createFromPubKey(peer.publicKey)
expect(peerId.equals(mockLibp2p.peerInfo.id)).to.equal(true)
expect(peerId.equals(mockLibp2p.peerId)).to.equal(true)
expect(peer.addrs).to.have.length(1)
peer.addrs.forEach((addr) => {
expect(mockLibp2p.peerInfo.multiaddrs.has(addr)).to.equal(true)
expect(addr).to.equalBytes(listeningMultiaddrs.buffer)
})
expect(topic).to.equal(PubsubPeerDiscovery.TOPIC)

Expand All @@ -60,14 +62,20 @@ describe('Pubsub Peer Discovery', () => {
})

it('should be able to encode/decode a message', async () => {
discovery = new PubsubPeerDiscovery({ libp2p: mockLibp2p })
discovery = new PubsubPeerDiscovery({ libp2p: mockLibp2p, multiaddrs: [listeningMultiaddrs] })
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 +88,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