mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-03-30 06:11:05 +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
188 lines
5.5 KiB
TypeScript
188 lines
5.5 KiB
TypeScript
/* eslint-env mocha */
|
|
|
|
import { expect } from 'aegir/utils/chai.js'
|
|
import sinon from 'sinon'
|
|
import { randomBytes } from '@libp2p/crypto'
|
|
import { pipe } from 'it-pipe'
|
|
import toBuffer from 'it-to-buffer'
|
|
import delay from 'delay'
|
|
import { createNode, populateAddressBooks } from '../utils/creators/peer.js'
|
|
import { createBaseOptions } from '../utils/base-options.js'
|
|
import type { Libp2pNode } from '../../src/libp2p.js'
|
|
import type { Libp2pOptions } from '../../src/index.js'
|
|
import type { DefaultMetrics } from '../../src/metrics/index.js'
|
|
|
|
describe('libp2p.metrics', () => {
|
|
let libp2p: Libp2pNode
|
|
|
|
afterEach(async () => {
|
|
if (libp2p != null) {
|
|
await libp2p.stop()
|
|
}
|
|
})
|
|
|
|
it('should disable metrics by default', async () => {
|
|
libp2p = await createNode({
|
|
config: createBaseOptions()
|
|
})
|
|
|
|
expect(libp2p.components.getMetrics()).to.be.undefined()
|
|
})
|
|
|
|
it('should start/stop metrics on startup/shutdown when enabled', async () => {
|
|
const config: Libp2pOptions = createBaseOptions({
|
|
metrics: {
|
|
enabled: true,
|
|
computeThrottleMaxQueueSize: 1, // compute after every message
|
|
movingAverageIntervals: [10]
|
|
}
|
|
})
|
|
libp2p = await createNode({ started: false, config })
|
|
|
|
const metrics = libp2p.components.getMetrics() as DefaultMetrics
|
|
|
|
if (metrics == null) {
|
|
throw new Error('Metrics not configured')
|
|
}
|
|
|
|
const metricsStartSpy = sinon.spy(metrics, 'start')
|
|
const metricsStopSpy = sinon.spy(metrics, 'stop')
|
|
|
|
await libp2p.start()
|
|
expect(metricsStartSpy).to.have.property('callCount', 1)
|
|
|
|
await libp2p.stop()
|
|
expect(metricsStopSpy).to.have.property('callCount', 1)
|
|
})
|
|
|
|
it('should record metrics on connections and streams when enabled', async () => {
|
|
let remoteLibp2p: Libp2pNode
|
|
;[libp2p, remoteLibp2p] = await Promise.all([
|
|
createNode({
|
|
config: createBaseOptions({
|
|
metrics: {
|
|
enabled: true,
|
|
computeThrottleMaxQueueSize: 1, // compute after every message
|
|
movingAverageIntervals: [10]
|
|
}
|
|
})
|
|
}),
|
|
createNode({
|
|
config: createBaseOptions({
|
|
metrics: {
|
|
enabled: true,
|
|
computeThrottleMaxQueueSize: 1, // compute after every message
|
|
movingAverageIntervals: [10]
|
|
}
|
|
})
|
|
})
|
|
])
|
|
|
|
await populateAddressBooks([libp2p, remoteLibp2p])
|
|
|
|
void remoteLibp2p.handle('/echo/1.0.0', ({ stream }) => {
|
|
void pipe(stream, stream)
|
|
})
|
|
|
|
const connection = await libp2p.dial(remoteLibp2p.peerId)
|
|
const { stream } = await connection.newStream('/echo/1.0.0')
|
|
|
|
const bytes = randomBytes(512)
|
|
const result = await pipe(
|
|
[bytes],
|
|
stream,
|
|
async (source) => await toBuffer(source)
|
|
)
|
|
|
|
// Flush the call stack
|
|
await delay(0)
|
|
|
|
expect(result).to.have.length(bytes.length)
|
|
|
|
const metrics = libp2p.components.getMetrics()
|
|
|
|
if (metrics == null) {
|
|
throw new Error('Metrics not configured')
|
|
}
|
|
|
|
// Protocol stats should equal the echo size
|
|
const protocolStats = metrics.forProtocol('/echo/1.0.0')?.getSnapshot()
|
|
expect(protocolStats?.dataReceived).to.equal(BigInt(bytes.length))
|
|
expect(protocolStats?.dataSent).to.equal(BigInt(bytes.length))
|
|
|
|
// A lot more traffic will be sent over the wire for the peer
|
|
const peerStats = metrics.forPeer(connection.remotePeer)?.getSnapshot()
|
|
expect(parseInt(peerStats?.dataReceived.toString() ?? '0')).to.be.at.least(bytes.length)
|
|
await remoteLibp2p.stop()
|
|
})
|
|
|
|
it('should move disconnected peers to the old peers list', async () => {
|
|
let remoteLibp2p
|
|
;[libp2p, remoteLibp2p] = await Promise.all([
|
|
createNode({
|
|
config: createBaseOptions({
|
|
metrics: {
|
|
enabled: true,
|
|
computeThrottleMaxQueueSize: 1, // compute after every message
|
|
movingAverageIntervals: [10]
|
|
},
|
|
connectionManager: {
|
|
autoDial: false
|
|
}
|
|
})
|
|
}),
|
|
createNode({
|
|
config: createBaseOptions({
|
|
metrics: {
|
|
enabled: true,
|
|
computeThrottleMaxQueueSize: 1, // compute after every message
|
|
movingAverageIntervals: [10]
|
|
},
|
|
connectionManager: {
|
|
autoDial: false
|
|
}
|
|
})
|
|
})
|
|
])
|
|
await populateAddressBooks([libp2p, remoteLibp2p])
|
|
|
|
void remoteLibp2p.handle('/echo/1.0.0', ({ stream }) => {
|
|
void pipe(stream, stream)
|
|
})
|
|
|
|
const connection = await libp2p.dial(remoteLibp2p.peerId)
|
|
const { stream } = await connection.newStream('/echo/1.0.0')
|
|
|
|
const bytes = randomBytes(512)
|
|
await pipe(
|
|
[bytes],
|
|
stream,
|
|
async (source) => await toBuffer(source)
|
|
)
|
|
|
|
const metrics = libp2p.components.getMetrics()
|
|
|
|
if (metrics == null) {
|
|
throw new Error('Metrics not configured')
|
|
}
|
|
|
|
const peerStats = metrics.forPeer(connection.remotePeer)?.getSnapshot()
|
|
expect(parseInt(peerStats?.dataReceived.toString() ?? '0')).to.be.at.least(bytes.length)
|
|
|
|
const metricsOnPeerDisconnectedSpy = sinon.spy(metrics, 'onPeerDisconnected')
|
|
await libp2p.hangUp(connection.remotePeer)
|
|
|
|
// Flush call stack
|
|
await delay(0)
|
|
|
|
expect(metricsOnPeerDisconnectedSpy).to.have.property('callCount', 1)
|
|
|
|
// forPeer should still give us the old peer stats,
|
|
// even though its not in the active peer list
|
|
const peerStatsAfterHangup = metrics.forPeer(connection.remotePeer)?.getSnapshot()
|
|
expect(parseInt(peerStatsAfterHangup?.dataReceived.toString() ?? '0')).to.be.at.least(bytes.length)
|
|
|
|
await remoteLibp2p.stop()
|
|
})
|
|
})
|