Compare commits

..

1 Commits

Author SHA1 Message Date
Vasco Santos
932e10dd9b chore: bench persisted peerStore 2020-06-02 14:32:45 +02:00
17 changed files with 364 additions and 185 deletions

View File

@@ -1,23 +1,3 @@
<a name="0.28.1"></a>
## [0.28.1](https://github.com/libp2p/js-libp2p/compare/v0.28.0...v0.28.1) (2020-06-12)
### Bug Fixes
* throw if no conn encryption module provided ([#665](https://github.com/libp2p/js-libp2p/issues/665)) ([c038550](https://github.com/libp2p/js-libp2p/commit/c038550))
### Features
* add ConnectionManager#getAll ([8f680e2](https://github.com/libp2p/js-libp2p/commit/8f680e2))
<a name="0.28.0"></a>
# [0.28.0](https://github.com/libp2p/js-libp2p/compare/v0.28.0-rc.0...v0.28.0) (2020-06-05)
<a name="0.28.0-rc.0"></a>
# [0.28.0-rc.0](https://github.com/libp2p/js-libp2p/compare/v0.27.8...v0.28.0-rc.0) (2020-05-28)

View File

@@ -155,7 +155,7 @@ List of packages currently in existence for libp2p
| [`libp2p-kad-dht`](//github.com/libp2p/js-libp2p-kad-dht) | [![npm](https://img.shields.io/npm/v/libp2p-kad-dht.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-kad-dht/releases) | [![Deps](https://david-dm.org/libp2p/js-libp2p-kad-dht.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-kad-dht) | [![Travis CI](https://flat.badgen.net/travis/libp2p/js-libp2p-kad-dht/master)](https://travis-ci.com/libp2p/js-libp2p-kad-dht) | [![codecov](https://codecov.io/gh/libp2p/js-libp2p-kad-dht/branch/master/graph/badge.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p-kad-dht) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
| [`libp2p-mdns`](//github.com/libp2p/js-libp2p-mdns) | [![npm](https://img.shields.io/npm/v/libp2p-mdns.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-mdns/releases) | [![Deps](https://david-dm.org/libp2p/js-libp2p-mdns.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-mdns) | [![Travis CI](https://flat.badgen.net/travis/libp2p/js-libp2p-mdns/master)](https://travis-ci.com/libp2p/js-libp2p-mdns) | [![codecov](https://codecov.io/gh/libp2p/js-libp2p-mdns/branch/master/graph/badge.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p-mdns) | [Jacob Heun](mailto:jacobheun@gmail.com) |
| [`libp2p-webrtc-star`](//github.com/libp2p/js-libp2p-webrtc-star) | [![npm](https://img.shields.io/npm/v/libp2p-webrtc-star.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-webrtc-star/releases) | [![Deps](https://david-dm.org/libp2p/js-libp2p-webrtc-star.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-webrtc-star) | [![Travis CI](https://flat.badgen.net/travis/libp2p/js-libp2p-webrtc-star/master)](https://travis-ci.com/libp2p/js-libp2p-webrtc-star) | [![codecov](https://codecov.io/gh/libp2p/js-libp2p-webrtc-star/branch/master/graph/badge.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p-webrtc-star) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |
| [`@chainsafe/discv5`](//github.com/ChainSafe/discv5) | [![npm](https://img.shields.io/npm/v/@chainsafe/discv5.svg?maxAge=86400&style=flat-square)](//github.com/ChainSafe/discv5/releases) | [![Deps](https://david-dm.org/ChainSafe/discv5.svg?style=flat-square)](https://david-dm.org/ChainSafe/discv5) | [![Travis CI](https://flat.badgen.net/travis/ChainSafe/discv5/master)](https://travis-ci.com/ChainSafe/discv5) | [![codecov](https://codecov.io/gh/ChainSafe/discv5/branch/master/graph/badge.svg?style=flat-square)](https://codecov.io/gh/ChainSafe/discv5) | [Cayman Nava](mailto:caymannava@gmail.com) |
| [`discv5`](//github.com/ChainSafe/discv5) | [![npm](https://img.shields.io/npm/v/discv5.svg?maxAge=86400&style=flat-square)](//github.com/ChainSafe/discv5/releases) | [![Deps](https://david-dm.org/ChainSafe/discv5.svg?style=flat-square)](https://david-dm.org/ChainSafe/discv5) | [![Travis CI](https://flat.badgen.net/travis/ChainSafe/discv5/master)](https://travis-ci.com/ChainSafe/discv5) | [![codecov](https://codecov.io/gh/ChainSafe/discv5/branch/master/graph/badge.svg?style=flat-square)](https://codecov.io/gh/ChainSafe/discv5) | [Cayman Nava](mailto:caymannava@gmail.com) |
| **content routing** |
| [`libp2p-delegated-content-routing`](//github.com/libp2p/js-libp2p-delegated-content-routing) | [![npm](https://img.shields.io/npm/v/libp2p-delegated-content-routing.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-delegated-content-routing/releases) | [![Deps](https://david-dm.org/libp2p/js-libp2p-delegated-content-routing.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-delegated-content-routing) | [![Travis CI](https://flat.badgen.net/travis/libp2p/js-libp2p-delegated-content-routing/master)](https://travis-ci.com/libp2p/js-libp2p-delegated-content-routing) | [![codecov](https://codecov.io/gh/libp2p/js-libp2p-delegated-content-routing/branch/master/graph/badge.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p-delegated-content-routing) | [Jacob Heun](mailto:jacobheun@gmail.com) |
| [`libp2p-kad-dht`](//github.com/libp2p/js-libp2p-kad-dht) | [![npm](https://img.shields.io/npm/v/libp2p-kad-dht.svg?maxAge=86400&style=flat-square)](//github.com/libp2p/js-libp2p-kad-dht/releases) | [![Deps](https://david-dm.org/libp2p/js-libp2p-kad-dht.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-kad-dht) | [![Travis CI](https://flat.badgen.net/travis/libp2p/js-libp2p-kad-dht/master)](https://travis-ci.com/libp2p/js-libp2p-kad-dht) | [![codecov](https://codecov.io/gh/libp2p/js-libp2p-kad-dht/branch/master/graph/badge.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p-kad-dht) | [Vasco Santos](mailto:vasco.santos@moxy.studio) |

View File

@@ -85,17 +85,17 @@ Creates an instance of Libp2p.
| Name | Type | Description |
|------|------|-------------|
| options | `object` | libp2p options |
| options.modules | [`Array<object>`](./CONFIGURATION.md#modules) | libp2p [modules](./CONFIGURATION.md#modules) to use |
| options.modules | [`Array<object>`](./CONFIGURATION.md#modules) | libp2p modules to use |
| [options.addresses] | `{ listen: Array<string>, announce: Array<string>, noAnnounce: Array<string> }` | Addresses for transport listening and to advertise to the network |
| [options.config] | `object` | libp2p modules configuration and core configuration |
| [options.connectionManager] | [`object`](./CONFIGURATION.md#configuring-connection-manager) | libp2p Connection Manager [configuration](./CONFIGURATION.md#configuring-connection-manager) |
| [options.transportManager] | [`object`](./CONFIGURATION.md#configuring-transport-manager) | libp2p transport manager [configuration]((./CONFIGURATION.md#configuring-transport-manager)) |
| [options.connectionManager] | [`object`](./CONFIGURATION.md#configuring-connection-manager) | libp2p Connection Manager configuration |
| [options.transportManager] | [`object`](./CONFIGURATION.md#configuring-transport-manager) | libp2p transport manager configuration |
| [options.datastore] | `object` | must implement [ipfs/interface-datastore](https://github.com/ipfs/interface-datastore) (in memory datastore will be used if not provided) |
| [options.dialer] | [`object`](./CONFIGURATION.md#configuring-dialing) | libp2p Dialer [configuration]((./CONFIGURATION.md#configuring-dialing))
| [options.keychain] | [`object`](./CONFIGURATION.md#setup-with-keychain) | keychain [configuration]((./CONFIGURATION.md#setup-with-keychain)) |
| [options.metrics] | [`object`](./CONFIGURATION.md#configuring-metrics) | libp2p Metrics [configuration]((./CONFIGURATION.md#configuring-metrics)) |
| [options.dialer] | [`object`](./CONFIGURATION.md#configuring-dialing) | libp2p Dialer configuration
| [options.keychain] | [`object`](./CONFIGURATION.md#setup-with-keychain) | keychain configuration |
| [options.metrics] | [`object`](./CONFIGURATION.md#configuring-metrics) | libp2p Metrics configuration
| [options.peerId] | [`PeerId`][peer-id] | peerId instance (it will be created if not provided) |
| [options.peerStore] | [`object`](./CONFIGURATION.md#configuring-peerstore) | libp2p PeerStore [configuration]((./CONFIGURATION.md#configuring-peerstore)) |
| [options.peerStore] | [`object`](./CONFIGURATION.md#configuring-peerstore) | libp2p PeerStore configuration |
For Libp2p configurations and modules details read the [Configuration Document](./CONFIGURATION.md).

View File

@@ -52,7 +52,7 @@ The libp2p ecosystem contains at least one module for each of these subsystems.
After selecting the modules to use, it is also possible to configure each one according to your needs.
Bear in mind that a **transport** and **connection encryption** are **required**, while all the other subsystems are optional.
Bear in mind that only a **transport** and **connection encryption** are required, while all the other subsystems are optional.
### Transport

View File

@@ -21,7 +21,7 @@ const createNode = async () => {
},
config: {
peerDiscovery: {
[MulticastDNS.tag]: {
mdns: {
interval: 20e3,
enabled: true
}

View File

@@ -33,7 +33,7 @@
["libp2p/js-libp2p-kad-dht", "libp2p-kad-dht"],
["libp2p/js-libp2p-mdns", "libp2p-mdns"],
["libp2p/js-libp2p-webrtc-star", "libp2p-webrtc-star"],
["ChainSafe/discv5", "@chainsafe/discv5"],
["ChainSafe/discv5", "discv5"],
"content routing",
["libp2p/js-libp2p-delegated-content-routing", "libp2p-delegated-content-routing"],

View File

@@ -1,6 +1,6 @@
{
"name": "libp2p",
"version": "0.28.1",
"version": "0.28.0-rc.0",
"description": "JavaScript implementation of libp2p, a modular peer to peer network stack",
"leadMaintainer": "Jacob Heun <jacobheun@gmail.com>",
"main": "src/index.js",
@@ -50,7 +50,7 @@
"err-code": "^2.0.0",
"events": "^3.1.0",
"hashlru": "^2.3.0",
"interface-datastore": "^1.0.4",
"interface-datastore": "^0.8.3",
"ipfs-utils": "^2.2.0",
"it-all": "^1.0.1",
"it-buffer": "^0.1.2",
@@ -92,7 +92,7 @@
"datastore-level": "^1.1.0",
"delay": "^4.3.0",
"dirty-chai": "^2.0.1",
"interop-libp2p": "^0.1.0",
"interop-libp2p": "libp2p/interop#chore/update-libp2p-daemon-with-peerstore",
"ipfs-http-client": "^44.0.0",
"it-concat": "^1.0.0",
"it-pair": "^1.0.0",
@@ -125,14 +125,14 @@
"Jacob Heun <jacobheun@gmail.com>",
"Vasco Santos <vasco.santos@moxy.studio>",
"Alan Shaw <alan@tableflip.io>",
"Cayman <caymannava@gmail.com>",
"Pedro Teixeira <i@pgte.me>",
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
"Alex Potsides <alex@achingbrain.net>",
"Maciej Krüger <mkg20001@gmail.com>",
"Hugo Dias <mail@hugodias.me>",
"Volker Mische <volker.mische@gmail.com>",
"Cayman <caymannava@gmail.com>",
"dirkmc <dirkmdev@gmail.com>",
"Volker Mische <volker.mische@gmail.com>",
"Richard Littauer <richard.littauer@gmail.com>",
"Elven <mon.samuel@qq.com>",
"Andrew Nesbitt <andrewnez@gmail.com>",

View File

@@ -215,19 +215,6 @@ class ConnectionManager extends EventEmitter {
* @returns {Connection}
*/
get (peerId) {
const connections = this.getAll(peerId)
if (connections.length) {
return connections[0]
}
return null
}
/**
* Get all open connections with a peer.
* @param {PeerId} peerId
* @returns {Array<Connection>}
*/
getAll (peerId) {
if (!PeerId.isPeerId(peerId)) {
throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
}
@@ -235,11 +222,11 @@ class ConnectionManager extends EventEmitter {
const id = peerId.toB58String()
const connections = this.connections.get(id)
// Return all open connections
// Return the first, open connection
if (connections) {
return connections.filter(connection => connection.stat.status === 'open')
return connections.find(connection => connection.stat.status === 'open')
}
return []
return null
}
/**

View File

@@ -2,15 +2,13 @@
exports.messages = {
NOT_STARTED_YET: 'The libp2p node is not started yet',
DHT_DISABLED: 'DHT is not available',
CONN_ENCRYPTION_REQUIRED: 'At least one connection encryption module is required'
DHT_DISABLED: 'DHT is not available'
}
exports.codes = {
DHT_DISABLED: 'ERR_DHT_DISABLED',
PUBSUB_NOT_STARTED: 'ERR_PUBSUB_NOT_STARTED',
DHT_NOT_STARTED: 'ERR_DHT_NOT_STARTED',
CONN_ENCRYPTION_REQUIRED: 'ERR_CONN_ENCRYPTION_REQUIRED',
ERR_CONNECTION_ENDED: 'ERR_CONNECTION_ENDED',
ERR_CONNECTION_FAILED: 'ERR_CONNECTION_FAILED',
ERR_NODE_NOT_STARTED: 'ERR_NODE_NOT_STARTED',

View File

@@ -6,7 +6,6 @@ const globalThis = require('ipfs-utils/src/globalthis')
const log = debug('libp2p')
log.error = debug('libp2p:error')
const errCode = require('err-code')
const PeerId = require('peer-id')
const peerRouting = require('./peer-routing')
@@ -14,7 +13,7 @@ const contentRouting = require('./content-routing')
const pubsub = require('./pubsub')
const getPeer = require('./get-peer')
const { validate: validateConfig } = require('./config')
const { codes, messages } = require('./errors')
const { codes } = require('./errors')
const AddressManager = require('./address-manager')
const ConnectionManager = require('./connection-manager')
@@ -116,13 +115,12 @@ class Libp2p extends EventEmitter {
this.registrar.handle = this.handle
// Attach crypto channels
if (!this._modules.connEncryption || !this._modules.connEncryption.length) {
throw errCode(new Error(messages.CONN_ENCRYPTION_REQUIRED), codes.CONN_ENCRYPTION_REQUIRED)
if (this._modules.connEncryption) {
const cryptos = this._modules.connEncryption
cryptos.forEach((crypto) => {
this.upgrader.cryptos.set(crypto.protocol, crypto)
})
}
const cryptos = this._modules.connEncryption
cryptos.forEach((crypto) => {
this.upgrader.cryptos.set(crypto.protocol, crypto)
})
this.dialer = new Dialer({
transportManager: this.transportManager,

View File

@@ -96,9 +96,7 @@ class PersistentPeerStore extends PeerStore {
if (this._dirtyPeers.size >= this.threshold) {
// Commit current data
this._commitData().catch(err => {
log.error('error committing data', err)
})
this._commitData()
}
}
@@ -122,9 +120,7 @@ class PersistentPeerStore extends PeerStore {
if (this._dirtyPeers.size >= this.threshold) {
// Commit current data
this._commitData().catch(err => {
log.error('error committing data', err)
})
this._commitData()
}
}

167
test/benchmark.node.js Normal file
View File

@@ -0,0 +1,167 @@
'use strict'
/* eslint-env mocha */
const chai = require('chai')
chai.use(require('dirty-chai'))
const pWaitFor = require('p-wait-for')
const { createPeerId } = require('./utils/creators/peer')
const Libp2p = require('../src')
const Transport = require('libp2p-tcp')
const Muxer = require('libp2p-mplex')
const Crypto = require('libp2p-secio')
const MulticastDNS = require('libp2p-mdns')
const multiaddr = require('multiaddr')
const { MemoryDatastore } = require('interface-datastore')
describe.only('benchmark', function () {
this.timeout(30e3)
let nodes
const create = async (config = {}) => {
const peerIds = await createPeerId({ number: 5 })
nodes = await Promise.all(peerIds.map((peerId) => Libp2p.create({
peerId,
config: {
relay: {
enabled: true,
hop: {
enabled: false
}
}
},
modules: {
transport: [Transport],
streamMuxer: [Muxer],
connEncryption: [Crypto],
peerDiscovery: [MulticastDNS]
},
config: {
peerDiscovery: {
autoDial: true, // Auto connect to discovered peers (limited by ConnectionManager minPeers)
// The `tag` property will be searched when creating the instance of your Peer Discovery service.
// The associated object, will be passed to the service when it is instantiated.
[MulticastDNS.tag]: {
interval: 1000,
enabled: true
}
// .. other discovery module options.
}
},
addresses: {
listen: [multiaddr('/ip4/127.0.0.1/tcp/0')]
},
...config
})))
nodes[1].connectionManager.on('peer:connected', () => {
console.log('connected')
})
await Promise.all(nodes.map((node) => node.start()))
}
const clearStore = (node) => {
node.peerStore.addressBook.data.clear()
node.peerStore.protoBook.data.clear()
node.peerStore.keyBook.data.clear()
node.peerStore.metadataBook.data.clear()
}
const test = async () => {
await nodes[4].start()
const start = Date.now()
await pWaitFor(() =>
nodes[0].connectionManager.connections.size >= nodes.length - 1 &&
nodes[1].connectionManager.connections.size >= nodes.length - 1 &&
nodes[2].connectionManager.connections.size >= nodes.length - 1 &&
nodes[3].connectionManager.connections.size >= nodes.length - 1 &&
nodes[4].connectionManager.connections.size >= nodes.length - 1
)
return Date.now() - start
}
afterEach(() => Promise.all(nodes.map((node) => node.stop())))
it('run without persistence', async () => {
await create({
datastore: new MemoryDatastore(),
peerStore: {
persistence: false,
threshold: 5
}
})
// wait all peers connected
await pWaitFor(() =>
nodes[0].connectionManager.connections.size >= nodes.length - 1 &&
nodes[1].connectionManager.connections.size >= nodes.length - 1 &&
nodes[2].connectionManager.connections.size >= nodes.length - 1 &&
nodes[3].connectionManager.connections.size >= nodes.length - 1 &&
nodes[4].connectionManager.connections.size >= nodes.length - 1
)
console.log('all peers connected')
await nodes[4].stop()
await pWaitFor(() =>
nodes[0].connectionManager.connections.size >= nodes.length - 2 &&
nodes[1].connectionManager.connections.size >= nodes.length - 2 &&
nodes[2].connectionManager.connections.size >= nodes.length - 2 &&
nodes[3].connectionManager.connections.size >= nodes.length - 2
)
clearStore(nodes[4])
console.log('peers disconnected 1')
const dur = await test()
console.log('all peers connected', dur)
})
it('run with persistence', async () => {
await create({
datastore: new MemoryDatastore(),
peerStore: {
persistence: true,
threshold: 5
}
})
// wait all peers connected
await pWaitFor(() =>
nodes[0].connectionManager.connections.size >= 4 &&
nodes[1].connectionManager.connections.size >= 4 &&
nodes[2].connectionManager.connections.size >= 4 &&
nodes[3].connectionManager.connections.size >= 4 &&
nodes[4].connectionManager.connections.size >= 4
)
console.log('all peers connected')
await nodes[4].stop()
await pWaitFor(() =>
nodes[0].connectionManager.connections.size >= 3 &&
nodes[1].connectionManager.connections.size >= 3 &&
nodes[2].connectionManager.connections.size >= 3 &&
nodes[3].connectionManager.connections.size >= 3
)
clearStore(nodes[4])
console.log('peers disconnected 1')
const dur = await test()
console.log('all peers connected', dur)
})
})

162
test/benchmark.spec.js Normal file
View File

@@ -0,0 +1,162 @@
'use strict'
/* eslint-env mocha */
const chai = require('chai')
chai.use(require('dirty-chai'))
const pWaitFor = require('p-wait-for')
const { isNode } = require('ipfs-utils/src/env')
const { createPeerId } = require('./utils/creators/peer')
const Libp2p = require('../src')
const Transport = require('libp2p-webrtc-star')
const Muxer = require('libp2p-mplex')
const Crypto = require('libp2p-secio')
const multiaddr = require('multiaddr')
const { MemoryDatastore } = require('interface-datastore')
const signallerAddr = '/ip4/127.0.0.1/tcp/15555/ws/p2p-webrtc-star'
describe.only('benchmark', function () {
this.timeout(30e3)
let nodes
if (isNode) {
return
}
const create = async (config = {}) => {
const peerIds = await createPeerId({ number: 5 })
nodes = await Promise.all(peerIds.map((peerId) => Libp2p.create({
peerId,
config: {
relay: {
enabled: true,
hop: {
enabled: false
}
}
},
modules: {
transport: [Transport],
streamMuxer: [Muxer],
connEncryption: [Crypto]
},
addresses: {
listen: [multiaddr(`${signallerAddr}/p2p/${peerId.toB58String()}`)]
},
...config
})))
nodes[1].connectionManager.on('peer:connected', () => {
console.log('connected')
})
await Promise.all(nodes.map((node) => node.start()))
}
const clearStore = (node) => {
node.peerStore.addressBook.data.clear()
node.peerStore.protoBook.data.clear()
node.peerStore.keyBook.data.clear()
node.peerStore.metadataBook.data.clear()
}
const test = async () => {
const start = new Date()
await nodes[4].start()
console.log('node start time', Date.now() - start)
await pWaitFor(() =>
nodes[0].connectionManager.connections.size >= nodes.length - 1 &&
nodes[1].connectionManager.connections.size >= nodes.length - 1 &&
nodes[2].connectionManager.connections.size >= nodes.length - 1 &&
nodes[3].connectionManager.connections.size >= nodes.length - 1 &&
nodes[4].connectionManager.connections.size >= nodes.length - 1
)
}
afterEach(() => Promise.all(nodes.map((node) => node.stop())))
it('run without persistence', async () => {
const start = Date.now()
await create({
datastore: new MemoryDatastore(),
peerStore: {
persistence: false,
threshold: 5
}
})
// wait all peers connected
await pWaitFor(() =>
nodes[0].connectionManager.connections.size >= nodes.length - 1 &&
nodes[1].connectionManager.connections.size >= nodes.length - 1 &&
nodes[2].connectionManager.connections.size >= nodes.length - 1 &&
nodes[3].connectionManager.connections.size >= nodes.length - 1 &&
nodes[4].connectionManager.connections.size >= nodes.length - 1
)
console.log('all peers connected')
await nodes[4].stop()
await pWaitFor(() =>
nodes[0].connectionManager.connections.size >= nodes.length - 2 &&
nodes[1].connectionManager.connections.size >= nodes.length - 2 &&
nodes[2].connectionManager.connections.size >= nodes.length - 2 &&
nodes[3].connectionManager.connections.size >= nodes.length - 2
)
clearStore(nodes[4])
console.log('peers disconnected 1')
await test()
console.log('all peers connected', Date.now() - start)
})
it.only('run with persistence', async () => {
const start = Date.now()
await create({
datastore: new MemoryDatastore(),
peerStore: {
persistence: true,
threshold: 5
}
})
// wait all peers connected
await pWaitFor(() =>
nodes[0].connectionManager.connections.size >= 4 &&
nodes[1].connectionManager.connections.size >= 4 &&
nodes[2].connectionManager.connections.size >= 4 &&
nodes[3].connectionManager.connections.size >= 4 &&
nodes[4].connectionManager.connections.size >= 4
)
console.log('all peers connected')
await nodes[4].stop()
await pWaitFor(() =>
nodes[0].connectionManager.connections.size >= 3 &&
nodes[1].connectionManager.connections.size >= 3 &&
nodes[2].connectionManager.connections.size >= 3 &&
nodes[3].connectionManager.connections.size >= 3
)
clearStore(nodes[4])
console.log('peers disconnected 1')
await test()
console.log('all peers connected', Date.now() - start)
})
})

View File

@@ -1,57 +0,0 @@
'use strict'
/* eslint-env mocha */
const chai = require('chai')
chai.use(require('dirty-chai'))
chai.use(require('chai-as-promised'))
const { expect } = chai
const Transport = require('libp2p-websockets')
const { NOISE: Crypto } = require('libp2p-noise')
const Libp2p = require('../../src')
const { codes: ErrorCodes } = require('../../src/errors')
const { createPeerId } = require('../utils/creators/peer')
describe('Connection encryption configuration', () => {
let peerId
before(async () => {
[peerId] = await createPeerId()
})
it('is required', async () => {
const config = {
peerId,
modules: {
transport: [Transport]
}
}
await expect(Libp2p.create(config)).to.eventually.be.rejected()
.and.to.have.property('code', ErrorCodes.CONN_ENCRYPTION_REQUIRED)
})
it('is required and needs at least one module', async () => {
const config = {
peerId,
modules: {
transport: [Transport],
connEncryption: []
}
}
await expect(Libp2p.create(config)).to.eventually.be.rejected()
.and.to.have.property('code', ErrorCodes.CONN_ENCRYPTION_REQUIRED)
})
it('can be created', async () => {
const config = {
peerId,
modules: {
transport: [Transport],
connEncryption: [Crypto]
}
}
await Libp2p.create(config)
})
})

View File

@@ -6,7 +6,6 @@ chai.use(require('dirty-chai'))
const { expect } = chai
const Transport = require('libp2p-tcp')
const { NOISE: Crypto } = require('libp2p-noise')
const { create } = require('../../src')
const peerUtils = require('../utils/creators/peer')
@@ -32,8 +31,7 @@ describe('Listening', () => {
listen: [listenAddr]
},
modules: {
transport: [Transport],
connEncryption: [Crypto]
transport: [Transport]
}
})

View File

@@ -5,6 +5,7 @@ const chai = require('chai')
chai.use(require('dirty-chai'))
const { expect } = chai
const sinon = require('sinon')
const PeerStore = require('../../src/peer-store/persistent')
const multiaddr = require('multiaddr')
const { MemoryDatastore } = require('interface-datastore')
@@ -61,7 +62,6 @@ describe('Persisted PeerStore', () => {
const protocols = ['/ping/1.0.0']
const spyDirty = sinon.spy(peerStore, '_addDirtyPeer')
const spyDs = sinon.spy(datastore, 'batch')
const commitSpy = sinon.spy(peerStore, '_commitData')
await peerStore.start()
@@ -71,18 +71,12 @@ describe('Persisted PeerStore', () => {
expect(spyDirty).to.have.property('callCount', 1) // Address
expect(spyDs).to.have.property('callCount', 1)
// let batch commit complete
await Promise.all(commitSpy.returnValues)
// ProtoBook
peerStore.protoBook.set(peer, protocols)
expect(spyDirty).to.have.property('callCount', 2) // Protocol
expect(spyDs).to.have.property('callCount', 2)
// let batch commit complete
await Promise.all(commitSpy.returnValues)
// Should have three peer records stored in the datastore
const queryParams = {
prefix: '/peers/'
@@ -104,7 +98,6 @@ describe('Persisted PeerStore', () => {
it('should load content to the peerStore when restart but not put in datastore again', async () => {
const spyDs = sinon.spy(datastore, 'batch')
const peers = await peerUtils.createPeerId({ number: 2 })
const commitSpy = sinon.spy(peerStore, '_commitData')
const multiaddrs = [
multiaddr('/ip4/156.10.1.22/tcp/1000'),
multiaddr('/ip4/156.10.1.23/tcp/1000')
@@ -117,29 +110,17 @@ describe('Persisted PeerStore', () => {
peerStore.addressBook.set(peers[0], [multiaddrs[0]])
peerStore.addressBook.set(peers[1], [multiaddrs[1]])
// let batch commit complete
await Promise.all(commitSpy.returnValues)
// KeyBook
peerStore.keyBook.set(peers[0], peers[0].pubKey)
peerStore.keyBook.set(peers[1], peers[1].pubKey)
// let batch commit complete
await Promise.all(commitSpy.returnValues)
// ProtoBook
peerStore.protoBook.set(peers[0], protocols)
peerStore.protoBook.set(peers[1], protocols)
// let batch commit complete
await Promise.all(commitSpy.returnValues)
// MetadataBook
peerStore.metadataBook.set(peers[0], 'location', Buffer.from('earth'))
// let batch commit complete
await Promise.all(commitSpy.returnValues)
expect(spyDs).to.have.property('callCount', 7) // 2 Address + 2 Key + 2 Proto + 1 Metadata
expect(peerStore.peers.size).to.equal(2)
@@ -167,7 +148,6 @@ describe('Persisted PeerStore', () => {
const [peer] = await peerUtils.createPeerId()
const multiaddrs = [multiaddr('/ip4/156.10.1.22/tcp/1000')]
const protocols = ['/ping/1.0.0']
const commitSpy = sinon.spy(peerStore, '_commitData')
await peerStore.start()
@@ -178,9 +158,6 @@ describe('Persisted PeerStore', () => {
// MetadataBook
peerStore.metadataBook.set(peer, 'location', Buffer.from('earth'))
// let batch commit complete
await Promise.all(commitSpy.returnValues)
const spyDs = sinon.spy(datastore, 'batch')
const spyAddressBook = sinon.spy(peerStore.addressBook, 'delete')
const spyKeyBook = sinon.spy(peerStore.keyBook, 'delete')
@@ -189,10 +166,6 @@ describe('Persisted PeerStore', () => {
// Delete from PeerStore
peerStore.delete(peer)
// let batch commit complete
await Promise.all(commitSpy.returnValues)
await peerStore.stop()
expect(spyAddressBook).to.have.property('callCount', 1)
@@ -224,7 +197,6 @@ describe('Persisted PeerStore', () => {
const spyDirty = sinon.spy(peerStore, '_addDirtyPeer')
const spyDirtyMetadata = sinon.spy(peerStore, '_addDirtyPeerMetadata')
const spyDs = sinon.spy(datastore, 'batch')
const commitSpy = sinon.spy(peerStore, '_commitData')
const peers = await peerUtils.createPeerId({ number: 2 })
@@ -241,15 +213,9 @@ describe('Persisted PeerStore', () => {
peerStore.protoBook.set(peers[0], protocols)
peerStore.metadataBook.set(peers[0], 'location', Buffer.from('earth'))
// let batch commit complete
await Promise.all(commitSpy.returnValues)
// Remove data from the same Peer
peerStore.addressBook.delete(peers[0])
// let batch commit complete
await Promise.all(commitSpy.returnValues)
expect(spyDirty).to.have.property('callCount', 3) // 2 AddrBook ops, 1 ProtoBook op
expect(spyDirtyMetadata).to.have.property('callCount', 1) // 1 MetadataBook op
expect(peerStore._dirtyPeers.size).to.equal(1)
@@ -265,9 +231,6 @@ describe('Persisted PeerStore', () => {
// Add data for second book
peerStore.addressBook.set(peers[1], multiaddrs)
// let batch commit complete
await Promise.all(commitSpy.returnValues)
expect(spyDirty).to.have.property('callCount', 4)
expect(spyDirtyMetadata).to.have.property('callCount', 1)
expect(spyDs).to.have.property('callCount', 1)
@@ -370,7 +333,6 @@ describe('libp2p.peerStore (Persisted)', () => {
})
it('should load content to the peerStore when a new node is started with the same datastore', async () => {
const commitSpy = sinon.spy(libp2p.peerStore, '_commitData')
const peers = await peerUtils.createPeerId({ number: 2 })
const multiaddrs = [
multiaddr('/ip4/156.10.1.22/tcp/1000'),
@@ -384,16 +346,10 @@ describe('libp2p.peerStore (Persisted)', () => {
libp2p.peerStore.addressBook.set(peers[0], [multiaddrs[0]])
libp2p.peerStore.addressBook.set(peers[1], [multiaddrs[1]])
// let batch commit complete
await Promise.all(commitSpy.returnValues)
// ProtoBook
libp2p.peerStore.protoBook.set(peers[0], protocols)
libp2p.peerStore.protoBook.set(peers[1], protocols)
// let batch commit complete
await Promise.all(commitSpy.returnValues)
expect(libp2p.peerStore.peers.size).to.equal(2)
await libp2p.stop()

View File

@@ -9,7 +9,6 @@ const sinon = require('sinon')
const multiaddr = require('multiaddr')
const Transport = require('libp2p-websockets')
const { NOISE: Crypto } = require('libp2p-noise')
const AddressManager = require('../../src/address-manager')
const TransportManager = require('../../src/transport-manager')
const mockUpgrader = require('../utils/mockUpgrader')
@@ -111,8 +110,7 @@ describe('libp2p.transportManager', () => {
libp2p = new Libp2p({
peerId,
modules: {
transport: [Transport],
connEncryption: [Crypto]
transport: [Transport]
}
})
@@ -130,8 +128,7 @@ describe('libp2p.transportManager', () => {
libp2p = new Libp2p({
peerId,
modules: {
transport: [spy],
connEncryption: [Crypto]
transport: [spy]
},
config: {
transport: {
@@ -155,8 +152,7 @@ describe('libp2p.transportManager', () => {
libp2p = new Libp2p({
peerId,
modules: {
transport: [Transport],
connEncryption: [Crypto]
transport: [Transport]
}
})
@@ -192,8 +188,7 @@ describe('libp2p.transportManager (dial only)', () => {
listen: [multiaddr('/ip4/127.0.0.1/tcp/0')]
},
modules: {
transport: [Transport],
connEncryption: [Crypto]
transport: [Transport]
}
})
@@ -217,8 +212,7 @@ describe('libp2p.transportManager (dial only)', () => {
faultTolerance: FaultTolerance.NO_FATAL
},
modules: {
transport: [Transport],
connEncryption: [Crypto]
transport: [Transport]
}
})