2019-11-19 17:44:45 -06:00
|
|
|
'use strict'
|
|
|
|
/* eslint-env mocha */
|
|
|
|
|
2020-10-15 15:31:33 +01:00
|
|
|
const { expect } = require('aegir/utils/chai')
|
2019-11-19 17:44:45 -06:00
|
|
|
const sinon = require('sinon')
|
2019-11-28 22:50:14 +01:00
|
|
|
|
2019-11-19 17:44:45 -06:00
|
|
|
const defer = require('p-defer')
|
2019-11-28 22:50:14 +01:00
|
|
|
const mergeOptions = require('merge-options')
|
|
|
|
|
2021-04-15 09:40:02 +02:00
|
|
|
const { Multiaddr } = require('multiaddr')
|
2019-12-11 17:06:40 +01:00
|
|
|
const WebRTCStar = require('libp2p-webrtc-star')
|
2019-11-19 17:44:45 -06:00
|
|
|
|
|
|
|
const Libp2p = require('../../src')
|
|
|
|
const baseOptions = require('../utils/base-options.browser')
|
2020-04-14 14:05:30 +02:00
|
|
|
const { createPeerId } = require('../utils/creators/peer')
|
2019-11-19 17:44:45 -06:00
|
|
|
|
|
|
|
describe('peer discovery', () => {
|
2019-12-11 17:06:40 +01:00
|
|
|
describe('basic functions', () => {
|
2020-04-14 14:05:30 +02:00
|
|
|
let peerId
|
|
|
|
let remotePeerId
|
2019-12-11 17:06:40 +01:00
|
|
|
let libp2p
|
2019-11-19 17:44:45 -06:00
|
|
|
|
2019-12-11 17:06:40 +01:00
|
|
|
before(async () => {
|
2020-04-14 14:05:30 +02:00
|
|
|
[peerId, remotePeerId] = await createPeerId({ number: 2 })
|
2019-12-11 17:06:40 +01:00
|
|
|
})
|
2019-11-19 17:44:45 -06:00
|
|
|
|
2019-12-11 17:06:40 +01:00
|
|
|
afterEach(async () => {
|
|
|
|
libp2p && await libp2p.stop()
|
|
|
|
sinon.reset()
|
2019-11-19 17:44:45 -06:00
|
|
|
})
|
2019-12-11 17:06:40 +01:00
|
|
|
|
2020-07-14 16:05:26 +02:00
|
|
|
it('should dial know peers on startup below the minConnections watermark', async () => {
|
2019-12-11 17:06:40 +01:00
|
|
|
libp2p = new Libp2p({
|
|
|
|
...baseOptions,
|
2020-07-14 16:05:26 +02:00
|
|
|
peerId,
|
|
|
|
connectionManager: {
|
|
|
|
minConnections: 2
|
|
|
|
}
|
2019-12-11 17:06:40 +01:00
|
|
|
})
|
2020-04-14 14:05:30 +02:00
|
|
|
|
2021-04-15 09:40:02 +02:00
|
|
|
libp2p.peerStore.addressBook.set(remotePeerId, [new Multiaddr('/ip4/165.1.1.1/tcp/80')])
|
2020-04-09 16:07:18 +02:00
|
|
|
|
2019-12-11 17:06:40 +01:00
|
|
|
const deferred = defer()
|
2020-04-14 14:05:30 +02:00
|
|
|
sinon.stub(libp2p.dialer, 'connectToPeer').callsFake((remotePeerId) => {
|
|
|
|
expect(remotePeerId).to.equal(remotePeerId)
|
2019-12-11 17:06:40 +01:00
|
|
|
deferred.resolve()
|
|
|
|
})
|
|
|
|
const spy = sinon.spy()
|
|
|
|
libp2p.on('peer:discovery', spy)
|
|
|
|
|
|
|
|
libp2p.start()
|
|
|
|
await deferred.promise
|
2020-04-09 16:07:18 +02:00
|
|
|
|
|
|
|
expect(spy.calledOnce).to.eql(true)
|
2020-04-14 14:05:30 +02:00
|
|
|
expect(spy.getCall(0).args[0].toString()).to.eql(remotePeerId.toString())
|
2019-11-19 17:44:45 -06:00
|
|
|
})
|
|
|
|
|
2020-01-07 15:27:32 +00:00
|
|
|
it('should stop discovery on libp2p start/stop', async () => {
|
|
|
|
const mockDiscovery = {
|
|
|
|
tag: 'mock',
|
|
|
|
start: () => {},
|
|
|
|
stop: () => {},
|
|
|
|
on: () => {},
|
|
|
|
removeListener: () => {}
|
|
|
|
}
|
|
|
|
const startSpy = sinon.spy(mockDiscovery, 'start')
|
|
|
|
const stopSpy = sinon.spy(mockDiscovery, 'stop')
|
|
|
|
|
|
|
|
libp2p = new Libp2p(mergeOptions(baseOptions, {
|
2020-04-14 14:05:30 +02:00
|
|
|
peerId,
|
2020-01-07 15:27:32 +00:00
|
|
|
modules: {
|
|
|
|
peerDiscovery: [mockDiscovery]
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
await libp2p.start()
|
|
|
|
expect(startSpy).to.have.property('callCount', 1)
|
|
|
|
expect(stopSpy).to.have.property('callCount', 0)
|
|
|
|
await libp2p.stop()
|
|
|
|
expect(startSpy).to.have.property('callCount', 1)
|
|
|
|
expect(stopSpy).to.have.property('callCount', 1)
|
|
|
|
})
|
2019-11-19 17:44:45 -06:00
|
|
|
})
|
2019-11-28 22:50:14 +01:00
|
|
|
|
2019-12-11 17:06:40 +01:00
|
|
|
describe('discovery modules from transports', () => {
|
2020-04-14 14:05:30 +02:00
|
|
|
let peerId, libp2p
|
2019-11-28 22:50:14 +01:00
|
|
|
|
2019-12-11 17:06:40 +01:00
|
|
|
before(async () => {
|
2020-04-14 14:05:30 +02:00
|
|
|
[peerId] = await createPeerId()
|
2019-12-11 17:06:40 +01:00
|
|
|
})
|
|
|
|
|
2021-04-15 09:40:02 +02:00
|
|
|
afterEach(async () => {
|
|
|
|
libp2p && await libp2p.stop()
|
|
|
|
})
|
|
|
|
|
2019-12-11 17:06:40 +01:00
|
|
|
it('should add discovery module if present in transports and enabled', async () => {
|
|
|
|
libp2p = new Libp2p(mergeOptions(baseOptions, {
|
2020-04-14 14:05:30 +02:00
|
|
|
peerId,
|
2019-12-11 17:06:40 +01:00
|
|
|
modules: {
|
|
|
|
transport: [WebRTCStar]
|
|
|
|
},
|
|
|
|
config: {
|
|
|
|
peerDiscovery: {
|
|
|
|
webRTCStar: {
|
|
|
|
enabled: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
await libp2p.start()
|
|
|
|
|
|
|
|
expect(libp2p._discovery.size).to.eql(1)
|
|
|
|
expect(libp2p._discovery.has('webRTCStar')).to.eql(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not add discovery module if present in transports but disabled', async () => {
|
|
|
|
libp2p = new Libp2p(mergeOptions(baseOptions, {
|
2020-04-14 14:05:30 +02:00
|
|
|
peerId,
|
2019-12-11 17:06:40 +01:00
|
|
|
modules: {
|
|
|
|
transport: [WebRTCStar]
|
|
|
|
},
|
|
|
|
config: {
|
|
|
|
peerDiscovery: {
|
|
|
|
webRTCStar: {
|
|
|
|
enabled: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
2019-11-28 22:50:14 +01:00
|
|
|
|
2019-12-11 17:06:40 +01:00
|
|
|
await libp2p.start()
|
|
|
|
|
|
|
|
expect(libp2p._discovery.size).to.eql(0)
|
|
|
|
})
|
2019-11-28 22:50:14 +01:00
|
|
|
})
|
2019-11-19 17:44:45 -06:00
|
|
|
})
|