mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-03-30 22:31:03 +00:00
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
144 lines
4.7 KiB
TypeScript
144 lines
4.7 KiB
TypeScript
/* eslint-env mocha */
|
|
|
|
import { expect } from 'aegir/utils/chai.js'
|
|
import sinon from 'sinon'
|
|
import { createNode } from '../utils/creators/peer.js'
|
|
import { createBaseOptions } from '../utils/base-options.browser.js'
|
|
import type { Libp2pNode } from '../../src/libp2p.js'
|
|
import type { DefaultConnectionManager } from '../../src/connection-manager/index.js'
|
|
import { mockConnection, mockDuplex, mockMultiaddrConnection } from '@libp2p/interface-compliance-tests/mocks'
|
|
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
|
import { CustomEvent } from '@libp2p/interfaces'
|
|
|
|
describe('Connection Manager', () => {
|
|
let libp2p: Libp2pNode
|
|
|
|
afterEach(async () => {
|
|
sinon.restore()
|
|
|
|
if (libp2p != null) {
|
|
await libp2p.stop()
|
|
}
|
|
})
|
|
|
|
it('should be able to create without metrics', async () => {
|
|
libp2p = await createNode({
|
|
config: createBaseOptions(),
|
|
started: false
|
|
})
|
|
|
|
const spy = sinon.spy(libp2p.components.getConnectionManager() as DefaultConnectionManager, 'start')
|
|
|
|
await libp2p.start()
|
|
expect(spy).to.have.property('callCount', 1)
|
|
expect(libp2p.components.getMetrics()).to.not.exist()
|
|
})
|
|
|
|
it('should be able to create with metrics', async () => {
|
|
libp2p = await createNode({
|
|
config: createBaseOptions({
|
|
metrics: {
|
|
enabled: true
|
|
}
|
|
}),
|
|
started: false
|
|
})
|
|
|
|
const spy = sinon.spy(libp2p.components.getConnectionManager() as DefaultConnectionManager, 'start')
|
|
|
|
await libp2p.start()
|
|
expect(spy).to.have.property('callCount', 1)
|
|
expect(libp2p.components.getMetrics()).to.exist()
|
|
})
|
|
|
|
it('should close lowest value peer connection when the maximum has been reached', async () => {
|
|
const max = 5
|
|
libp2p = await createNode({
|
|
config: createBaseOptions({
|
|
connectionManager: {
|
|
maxConnections: max,
|
|
minConnections: 2
|
|
}
|
|
}),
|
|
started: false
|
|
})
|
|
|
|
await libp2p.start()
|
|
|
|
const connectionManager = libp2p.components.getConnectionManager() as DefaultConnectionManager
|
|
const connectionManagerMaybeDisconnectOneSpy = sinon.spy(connectionManager, '_maybeDisconnectOne')
|
|
|
|
// Add 1 too many connections
|
|
const spies = new Map<number, sinon.SinonSpy<[], Promise<void>>>()
|
|
await Promise.all([...new Array(max + 1)].map(async (_, index) => {
|
|
const connection = mockConnection(mockMultiaddrConnection(mockDuplex(), await createEd25519PeerId()))
|
|
const spy = sinon.spy(connection, 'close')
|
|
// The connections have the same remote id, give them random ones
|
|
// so that we can verify the correct connection was closed
|
|
// sinon.stub(connection.remotePeer, 'toString').returns(index)
|
|
const value = Math.random()
|
|
spies.set(value, spy)
|
|
connectionManager.setPeerValue(connection.remotePeer, value)
|
|
await connectionManager.onConnect(new CustomEvent('connection', { detail: connection }))
|
|
}))
|
|
|
|
// get the lowest value
|
|
const lowest = Array.from(spies.keys()).sort((a, b) => {
|
|
if (a > b) {
|
|
return 1
|
|
}
|
|
|
|
if (a < b) {
|
|
return -1
|
|
}
|
|
|
|
return 0
|
|
})[0]
|
|
const lowestSpy = spies.get(lowest)
|
|
|
|
expect(connectionManagerMaybeDisconnectOneSpy.callCount).to.equal(1)
|
|
expect(lowestSpy).to.have.property('callCount', 1)
|
|
})
|
|
|
|
it('should close connection when the maximum has been reached even without peer values', async () => {
|
|
const max = 5
|
|
libp2p = await createNode({
|
|
config: createBaseOptions({
|
|
connectionManager: {
|
|
maxConnections: max,
|
|
minConnections: 0
|
|
}
|
|
}),
|
|
started: false
|
|
})
|
|
|
|
await libp2p.start()
|
|
|
|
const connectionManager = libp2p.components.getConnectionManager() as DefaultConnectionManager
|
|
const connectionManagerMaybeDisconnectOneSpy = sinon.spy(connectionManager, '_maybeDisconnectOne')
|
|
|
|
// Add 1 too many connections
|
|
const spy = sinon.spy()
|
|
await Promise.all([...new Array(max + 1)].map(async () => {
|
|
const connection = mockConnection(mockMultiaddrConnection(mockDuplex(), await createEd25519PeerId()))
|
|
sinon.stub(connection, 'close').callsFake(async () => spy()) // eslint-disable-line
|
|
await connectionManager.onConnect(new CustomEvent('connection', { detail: connection }))
|
|
}))
|
|
|
|
expect(connectionManagerMaybeDisconnectOneSpy.callCount).to.equal(1)
|
|
expect(spy).to.have.property('callCount', 1)
|
|
})
|
|
|
|
it('should fail if the connection manager has mismatched connection limit options', async () => {
|
|
await expect(createNode({
|
|
config: createBaseOptions({
|
|
connectionManager: {
|
|
maxConnections: 5,
|
|
minConnections: 6
|
|
}
|
|
}),
|
|
started: false
|
|
})).to.eventually.rejected('maxConnections must be greater')
|
|
})
|
|
})
|