js-libp2p/test/peer-discovery/index.spec.ts

102 lines
2.7 KiB
TypeScript
Raw Normal View History

feat: convert to typescript (#1172) Converts this module to typescript. - Ecosystem modules renamed from (e.g.) `libp2p-tcp` to `@libp2p/tcp` - Ecosystem module now have named exports - Configuration has been updated, now pass instances of modules instead of classes: - Some configuration keys have been renamed to make them more descriptive. `transport` -> `transports`, `connEncryption` -> `connectionEncryption`. In general where we pass multiple things, the key is now plural, e.g. `streamMuxer` -> `streamMuxers`, `contentRouting` -> `contentRouters`, etc. Where we are configuring a singleton the config key is singular, e.g. `connProtector` -> `connectionProtector` etc. - Properties of the `modules` config key have been moved to the root - Properties of the `config` config key have been moved to the root ```js // before import Libp2p from 'libp2p' import TCP from 'libp2p-tcp' await Libp2p.create({ modules: { transport: [ TCP ], } config: { transport: { [TCP.tag]: { foo: 'bar' } }, relay: { enabled: true, hop: { enabled: true, active: true } } } }) ``` ```js // after import { createLibp2p } from 'libp2p' import { TCP } from '@libp2p/tcp' await createLibp2p({ transports: [ new TCP({ foo: 'bar' }) ], relay: { enabled: true, hop: { enabled: true, active: true } } }) ``` - Use of `enabled` flag has been reduced - previously you could pass a module but disable it with config. Now if you don't want a feature, just don't pass an implementation. Eg: ```js // before await Libp2p.create({ modules: { transport: [ TCP ], pubsub: Gossipsub }, config: { pubsub: { enabled: false } } }) ``` ```js // after await createLibp2p({ transports: [ new TCP() ] }) ``` - `.multiaddrs` renamed to `.getMultiaddrs()` because it's not a property accessor, work is done by that method to calculate announce addresses, observed addresses, etc - `/p2p/${peerId}` is now appended to all addresses returned by `.getMultiaddrs()` so they can be used opaquely (every consumer has to append the peer ID to the address to actually use it otherwise). If you need low-level unadulterated addresses, call methods on the address manager. BREAKING CHANGE: types are no longer hand crafted, this module is now ESM only
2022-03-28 14:30:27 +01:00
/* eslint-env mocha */
import { expect } from 'aegir/utils/chai.js'
import sinon from 'sinon'
import defer from 'p-defer'
import { Multiaddr } from '@multiformats/multiaddr'
import { createBaseOptions } from '../utils/base-options.browser.js'
import { createPeerId } from '../utils/creators/peer.js'
import { isPeerId, PeerId } from '@libp2p/interfaces/peer-id'
import { createLibp2pNode, Libp2pNode } from '../../src/libp2p.js'
import { mockConnection, mockDuplex, mockMultiaddrConnection } from '@libp2p/interface-compliance-tests/mocks'
describe('peer discovery', () => {
describe('basic functions', () => {
let peerId: PeerId
let remotePeerId: PeerId
let libp2p: Libp2pNode
before(async () => {
[peerId, remotePeerId] = await Promise.all([
createPeerId(),
createPeerId()
])
})
afterEach(async () => {
if (libp2p != null) {
await libp2p.stop()
}
sinon.reset()
})
it('should dial known peers on startup below the minConnections watermark', async () => {
libp2p = await createLibp2pNode(createBaseOptions({
peerId,
connectionManager: {
minConnections: 2
}
}))
await libp2p.peerStore.addressBook.set(remotePeerId, [new Multiaddr('/ip4/165.1.1.1/tcp/80')])
const deferred = defer()
sinon.stub(libp2p.components.getDialer(), 'dial').callsFake(async (id) => {
if (!isPeerId(id)) {
throw new Error('Tried to dial something that was not a peer ID')
}
if (!remotePeerId.equals(id)) {
throw new Error('Tried to dial wrong peer ID')
}
deferred.resolve()
return mockConnection(mockMultiaddrConnection(mockDuplex(), id))
})
const spy = sinon.spy()
libp2p.addEventListener('peer:discovery', spy)
await libp2p.start()
await deferred.promise
expect(spy.calledOnce).to.equal(true)
expect(spy.getCall(0).args[0].detail.id.toString()).to.equal(remotePeerId.toString())
})
it('should stop discovery on libp2p start/stop', async () => {
let started = 0
let stopped = 0
class MockDiscovery {
static tag = 'mock'
start () {
started++
}
stop () {
stopped++
}
addEventListener () {}
removeEventListener () {}
}
libp2p = await createLibp2pNode(createBaseOptions({
peerId,
peerDiscovery: [
new MockDiscovery()
]
}))
await libp2p.start()
expect(started).to.equal(1)
expect(stopped).to.equal(0)
await libp2p.stop()
expect(started).to.equal(1)
expect(stopped).to.equal(1)
})
})
})