js-libp2p/test/addresses/address-manager.spec.ts

189 lines
5.9 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/chai'
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
import { Multiaddr, protocols } from '@multiformats/multiaddr'
import { AddressFilter, DefaultAddressManager } from '../../src/address-manager/index.js'
import { createNode } from '../utils/creators/peer.js'
import { createFromJSON } from '@libp2p/peer-id-factory'
import Peers from '../fixtures/peers.js'
import { stubInterface } from 'ts-sinon'
import type { TransportManager } from '@libp2p/interface-transport'
import type { PeerId } from '@libp2p/interface-peer-id'
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
import type { Libp2p } from '../../src/index.js'
import { Components } from '@libp2p/components'
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
const listenAddresses = ['/ip4/127.0.0.1/tcp/15006/ws', '/ip4/127.0.0.1/tcp/15008/ws']
const announceAddreses = ['/dns4/peer.io']
describe('Address Manager', () => {
let peerId: PeerId
before(async () => {
peerId = await createFromJSON(Peers[0])
})
it('should not need any addresses', () => {
const am = new DefaultAddressManager(new Components({
peerId,
transportManager: stubInterface<TransportManager>()
}), {
announceFilter: stubInterface<AddressFilter>()
})
expect(am.getListenAddrs()).to.be.empty()
expect(am.getAnnounceAddrs()).to.be.empty()
})
it('should return listen multiaddrs on get', () => {
const am = new DefaultAddressManager(new Components({
peerId,
transportManager: stubInterface<TransportManager>()
}), {
announceFilter: stubInterface<AddressFilter>(),
listen: listenAddresses
})
expect(am.getListenAddrs()).to.have.lengthOf(listenAddresses.length)
expect(am.getAnnounceAddrs()).to.be.empty()
const listenMultiaddrs = am.getListenAddrs()
expect(listenMultiaddrs.length).to.equal(2)
expect(listenMultiaddrs[0].equals(new Multiaddr(listenAddresses[0]))).to.equal(true)
expect(listenMultiaddrs[1].equals(new Multiaddr(listenAddresses[1]))).to.equal(true)
})
it('should return announce multiaddrs on get', () => {
const am = new DefaultAddressManager(new Components({
peerId,
transportManager: stubInterface<TransportManager>()
}), {
announceFilter: stubInterface<AddressFilter>(),
listen: listenAddresses,
announce: announceAddreses
})
expect(am.getListenAddrs()).to.have.lengthOf(listenAddresses.length)
expect(am.getAnnounceAddrs()).to.have.lengthOf(announceAddreses.length)
const announceMultiaddrs = am.getAnnounceAddrs()
expect(announceMultiaddrs.length).to.equal(1)
expect(announceMultiaddrs[0].equals(new Multiaddr(announceAddreses[0]))).to.equal(true)
})
it('should add observed addresses', () => {
const am = new DefaultAddressManager(new Components({
peerId,
transportManager: stubInterface<TransportManager>()
}), {
announceFilter: stubInterface<AddressFilter>()
})
expect(am.getObservedAddrs()).to.be.empty()
am.addObservedAddr('/ip4/123.123.123.123/tcp/39201')
expect(am.getObservedAddrs()).to.have.lengthOf(1)
})
it('should dedupe added observed addresses', () => {
const ma = '/ip4/123.123.123.123/tcp/39201'
const am = new DefaultAddressManager(new Components({
peerId,
transportManager: stubInterface<TransportManager>()
}), {
announceFilter: stubInterface<AddressFilter>()
})
expect(am.getObservedAddrs()).to.be.empty()
am.addObservedAddr(ma)
am.addObservedAddr(ma)
am.addObservedAddr(ma)
expect(am.getObservedAddrs()).to.have.lengthOf(1)
expect(am.getObservedAddrs().map(ma => ma.toString())).to.include(ma)
})
it('should only emit one change:addresses event', () => {
const ma = '/ip4/123.123.123.123/tcp/39201'
const am = new DefaultAddressManager(new Components({
peerId,
transportManager: stubInterface<TransportManager>()
}), {
announceFilter: stubInterface<AddressFilter>()
})
let eventCount = 0
am.addEventListener('change:addresses', () => {
eventCount++
})
am.addObservedAddr(ma)
am.addObservedAddr(ma)
am.addObservedAddr(ma)
am.addObservedAddr(`${ma}/p2p/${peerId.toString()}`)
expect(eventCount).to.equal(1)
})
it('should strip our peer address from added observed addresses', () => {
const ma = '/ip4/123.123.123.123/tcp/39201'
const am = new DefaultAddressManager(new Components({
peerId,
transportManager: stubInterface<TransportManager>()
}), {
announceFilter: stubInterface<AddressFilter>()
})
expect(am.getObservedAddrs()).to.be.empty()
am.addObservedAddr(ma)
am.addObservedAddr(`${ma}/p2p/${peerId.toString()}`)
expect(am.getObservedAddrs()).to.have.lengthOf(1)
expect(am.getObservedAddrs().map(ma => ma.toString())).to.include(ma)
})
it('should strip our peer address from added observed addresses in difference formats', () => {
const ma = '/ip4/123.123.123.123/tcp/39201'
const am = new DefaultAddressManager(new Components({
peerId,
transportManager: stubInterface<TransportManager>()
}), {
announceFilter: stubInterface<AddressFilter>()
})
expect(am.getObservedAddrs()).to.be.empty()
am.addObservedAddr(ma)
am.addObservedAddr(`${ma}/p2p/${peerId.toString()}`)
expect(am.getObservedAddrs()).to.have.lengthOf(1)
expect(am.getObservedAddrs().map(ma => ma.toString())).to.include(ma)
})
})
describe('libp2p.addressManager', () => {
let libp2p: Libp2p
afterEach(async () => {
if (libp2p != null) {
await libp2p.stop()
}
})
it('should populate the AddressManager from the config', async () => {
libp2p = await createNode({
started: false,
config: {
addresses: {
listen: listenAddresses,
announce: announceAddreses
}
}
})
expect(libp2p.getMultiaddrs().map(ma => ma.decapsulateCode(protocols('p2p').code).toString())).to.have.members(announceAddreses)
expect(libp2p.getMultiaddrs().map(ma => ma.decapsulateCode(protocols('p2p').code).toString())).to.not.have.members(listenAddresses)
})
})