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 */
|
|
|
|
|
2022-04-09 09:26:25 +01:00
|
|
|
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 sinon from 'sinon'
|
|
|
|
import defer from 'p-defer'
|
|
|
|
import { Bootstrap } from '@libp2p/bootstrap'
|
|
|
|
import { randomBytes } from '@libp2p/crypto'
|
|
|
|
import { KadDHT } from '@libp2p/kad-dht'
|
|
|
|
import { MulticastDNS } from '@libp2p/mdns'
|
|
|
|
import { Multiaddr } from '@multiformats/multiaddr'
|
|
|
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
|
|
import { createBaseOptions } from '../utils/base-options.js'
|
|
|
|
import { createPeerId } from '../utils/creators/peer.js'
|
2022-06-15 18:30:39 +01:00
|
|
|
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 { createLibp2pNode, Libp2pNode } from '../../src/libp2p.js'
|
2022-05-04 16:03:43 +01:00
|
|
|
import { CustomEvent } from '@libp2p/interfaces/events'
|
2022-06-15 18:30:39 +01:00
|
|
|
import type { PeerInfo } from '@libp2p/interface-peer-info'
|
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 listenAddr = new Multiaddr('/ip4/127.0.0.1/tcp/0')
|
|
|
|
|
|
|
|
describe('peer discovery scenarios', () => {
|
|
|
|
let peerId: PeerId, remotePeerId1: PeerId, remotePeerId2: PeerId
|
|
|
|
let libp2p: Libp2pNode
|
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
[peerId, remotePeerId1, remotePeerId2] = await Promise.all([
|
|
|
|
createPeerId(),
|
|
|
|
createPeerId(),
|
|
|
|
createPeerId()
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
if (libp2p != null) {
|
|
|
|
await libp2p.stop()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should ignore self on discovery', async () => {
|
|
|
|
libp2p = await createLibp2pNode(createBaseOptions({
|
|
|
|
peerId,
|
|
|
|
peerDiscovery: [
|
|
|
|
new MulticastDNS()
|
|
|
|
]
|
|
|
|
}))
|
|
|
|
|
|
|
|
await libp2p.start()
|
|
|
|
const discoverySpy = sinon.spy()
|
|
|
|
libp2p.addEventListener('peer:discovery', discoverySpy)
|
|
|
|
libp2p.onDiscoveryPeer(new CustomEvent<PeerInfo>('peer', {
|
|
|
|
detail: {
|
|
|
|
id: libp2p.peerId,
|
|
|
|
multiaddrs: [],
|
|
|
|
protocols: []
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
expect(discoverySpy.called).to.eql(false)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('bootstrap should discover all peers in the list', async () => {
|
|
|
|
const deferred = defer()
|
|
|
|
|
|
|
|
const bootstrappers = [
|
|
|
|
`${listenAddr.toString()}/p2p/${remotePeerId1.toString()}`,
|
|
|
|
`${listenAddr.toString()}/p2p/${remotePeerId2.toString()}`
|
|
|
|
]
|
|
|
|
|
|
|
|
libp2p = await createLibp2pNode(createBaseOptions({
|
|
|
|
peerId,
|
|
|
|
addresses: {
|
|
|
|
listen: [
|
|
|
|
listenAddr.toString()
|
|
|
|
]
|
|
|
|
},
|
|
|
|
connectionManager: {
|
|
|
|
autoDial: false
|
|
|
|
},
|
|
|
|
peerDiscovery: [
|
|
|
|
new Bootstrap({
|
|
|
|
list: bootstrappers
|
|
|
|
})
|
|
|
|
]
|
|
|
|
}))
|
|
|
|
|
|
|
|
const expectedPeers = new Set([
|
|
|
|
remotePeerId1.toString(),
|
|
|
|
remotePeerId2.toString()
|
|
|
|
])
|
|
|
|
|
|
|
|
libp2p.addEventListener('peer:discovery', (evt) => {
|
|
|
|
const { id } = evt.detail
|
|
|
|
|
|
|
|
expectedPeers.delete(id.toString())
|
|
|
|
if (expectedPeers.size === 0) {
|
|
|
|
libp2p.removeEventListener('peer:discovery')
|
|
|
|
deferred.resolve()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
await libp2p.start()
|
|
|
|
|
|
|
|
return await deferred.promise
|
|
|
|
})
|
|
|
|
|
|
|
|
it('MulticastDNS should discover all peers on the local network', async () => {
|
|
|
|
const deferred = defer()
|
|
|
|
|
|
|
|
// use a random tag to prevent CI collision
|
|
|
|
const serviceTag = `libp2p-test-${uint8ArrayToString(randomBytes(4), 'base16')}.local`
|
|
|
|
|
|
|
|
const getConfig = (peerId: PeerId) => createBaseOptions({
|
|
|
|
peerId,
|
|
|
|
addresses: {
|
|
|
|
listen: [
|
|
|
|
listenAddr.toString()
|
|
|
|
]
|
|
|
|
},
|
|
|
|
peerDiscovery: [
|
|
|
|
new MulticastDNS({
|
|
|
|
interval: 200, // discover quickly
|
|
|
|
serviceTag
|
|
|
|
})
|
|
|
|
],
|
|
|
|
connectionManager: {
|
|
|
|
autoDial: false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
libp2p = await createLibp2pNode(getConfig(peerId))
|
|
|
|
const remoteLibp2p1 = await createLibp2pNode(getConfig(remotePeerId1))
|
|
|
|
const remoteLibp2p2 = await createLibp2pNode(getConfig(remotePeerId2))
|
|
|
|
|
|
|
|
const expectedPeers = new Set([
|
|
|
|
remotePeerId1.toString(),
|
|
|
|
remotePeerId2.toString()
|
|
|
|
])
|
|
|
|
|
|
|
|
libp2p.addEventListener('peer:discovery', (evt) => {
|
|
|
|
const { id } = evt.detail
|
|
|
|
|
|
|
|
expectedPeers.delete(id.toString())
|
|
|
|
|
|
|
|
if (expectedPeers.size === 0) {
|
|
|
|
libp2p.removeEventListener('peer:discovery')
|
|
|
|
deferred.resolve()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
remoteLibp2p1.start(),
|
|
|
|
remoteLibp2p2.start(),
|
|
|
|
libp2p.start()
|
|
|
|
])
|
|
|
|
|
|
|
|
await deferred.promise
|
|
|
|
|
|
|
|
await remoteLibp2p1.stop()
|
|
|
|
await remoteLibp2p2.stop()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('kad-dht should discover other peers', async () => {
|
|
|
|
const deferred = defer()
|
|
|
|
|
|
|
|
const getConfig = (peerId: PeerId) => createBaseOptions({
|
|
|
|
peerId,
|
|
|
|
addresses: {
|
|
|
|
listen: [
|
|
|
|
listenAddr.toString()
|
|
|
|
]
|
|
|
|
},
|
|
|
|
connectionManager: {
|
|
|
|
autoDial: false
|
|
|
|
},
|
|
|
|
dht: new KadDHT()
|
|
|
|
})
|
|
|
|
|
|
|
|
const localConfig = getConfig(peerId)
|
|
|
|
|
|
|
|
libp2p = await createLibp2pNode(localConfig)
|
|
|
|
|
|
|
|
const remoteLibp2p1 = await createLibp2pNode(getConfig(remotePeerId1))
|
|
|
|
const remoteLibp2p2 = await createLibp2pNode(getConfig(remotePeerId2))
|
|
|
|
|
|
|
|
libp2p.addEventListener('peer:discovery', (evt) => {
|
|
|
|
const { id } = evt.detail
|
|
|
|
|
|
|
|
if (id.equals(remotePeerId1)) {
|
|
|
|
libp2p.removeEventListener('peer:discovery')
|
|
|
|
deferred.resolve()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
libp2p.start(),
|
|
|
|
remoteLibp2p1.start(),
|
|
|
|
remoteLibp2p2.start()
|
|
|
|
])
|
|
|
|
|
|
|
|
await libp2p.peerStore.addressBook.set(remotePeerId1, remoteLibp2p1.getMultiaddrs())
|
|
|
|
await remoteLibp2p2.peerStore.addressBook.set(remotePeerId1, remoteLibp2p1.getMultiaddrs())
|
|
|
|
|
|
|
|
// Topology:
|
|
|
|
// A -> B
|
|
|
|
// C -> B
|
|
|
|
await Promise.all([
|
|
|
|
libp2p.dial(remotePeerId1),
|
|
|
|
remoteLibp2p2.dial(remotePeerId1)
|
|
|
|
])
|
|
|
|
|
|
|
|
await deferred.promise
|
|
|
|
return await Promise.all([
|
|
|
|
remoteLibp2p1.stop(),
|
|
|
|
remoteLibp2p2.stop()
|
|
|
|
])
|
|
|
|
})
|
|
|
|
})
|