Skip to content

Commit

Permalink
revert: reapply "fix: throw if no conn encryption module provided (#665
Browse files Browse the repository at this point in the history
…)"

This reapplies commit b621fbd.
  • Loading branch information
vasco-santos committed Jul 15, 2020
1 parent 798d7b7 commit de3a302
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 22 deletions.
14 changes: 7 additions & 7 deletions doc/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 to use |
| options.modules | [`Array<object>`](./CONFIGURATION.md#modules) | libp2p [modules](./CONFIGURATION.md#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 |
| [options.transportManager] | [`object`](./CONFIGURATION.md#configuring-transport-manager) | libp2p transport manager 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.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
| [options.keychain] | [`object`](./CONFIGURATION.md#setup-with-keychain) | keychain configuration |
| [options.metrics] | [`object`](./CONFIGURATION.md#configuring-metrics) | libp2p Metrics configuration
| [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.peerId] | [`PeerId`][peer-id] | peerId instance (it will be created if not provided) |
| [options.peerStore] | [`object`](./CONFIGURATION.md#configuring-peerstore) | libp2p PeerStore configuration |
| [options.peerStore] | [`object`](./CONFIGURATION.md#configuring-peerstore) | libp2p PeerStore [configuration]((./CONFIGURATION.md#configuring-peerstore)) |

For Libp2p configurations and modules details read the [Configuration Document](./CONFIGURATION.md).

Expand Down
2 changes: 1 addition & 1 deletion doc/CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 only a **transport** and **connection encryption** are required, while all the other subsystems are optional.
Bear in mind that a **transport** and **connection encryption** are **required**, while all the other subsystems are optional.

### Transport

Expand Down
2 changes: 1 addition & 1 deletion examples/discovery-mechanisms/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const createNode = async () => {
},
config: {
peerDiscovery: {
mdns: {
[MulticastDNS.tag]: {
interval: 20e3,
enabled: true
}
Expand Down
4 changes: 3 additions & 1 deletion src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

exports.messages = {
NOT_STARTED_YET: 'The libp2p node is not started yet',
DHT_DISABLED: 'DHT is not available'
DHT_DISABLED: 'DHT is not available',
CONN_ENCRYPTION_REQUIRED: 'At least one connection encryption module is required'
}

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',
Expand Down
14 changes: 8 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ 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')
const contentRouting = require('./content-routing')
const pubsub = require('./pubsub')
const getPeer = require('./get-peer')
const { validate: validateConfig } = require('./config')
const { codes } = require('./errors')
const { codes, messages } = require('./errors')

const AddressManager = require('./address-manager')
const ConnectionManager = require('./connection-manager')
Expand Down Expand Up @@ -121,12 +122,13 @@ class Libp2p extends EventEmitter {
this.registrar.handle = this.handle

// Attach crypto channels
if (this._modules.connEncryption) {
const cryptos = this._modules.connEncryption
cryptos.forEach((crypto) => {
this.upgrader.cryptos.set(crypto.protocol, crypto)
})
if (!this._modules.connEncryption || !this._modules.connEncryption.length) {
throw errCode(new Error(messages.CONN_ENCRYPTION_REQUIRED), codes.CONN_ENCRYPTION_REQUIRED)
}
const cryptos = this._modules.connEncryption
cryptos.forEach((crypto) => {
this.upgrader.cryptos.set(crypto.protocol, crypto)
})

this.dialer = new Dialer({
transportManager: this.transportManager,
Expand Down
57 changes: 57 additions & 0 deletions test/core/encryption.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'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)
})
})
4 changes: 3 additions & 1 deletion test/core/listening.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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')
Expand All @@ -31,7 +32,8 @@ describe('Listening', () => {
listen: [listenAddr]
},
modules: {
transport: [Transport]
transport: [Transport],
connEncryption: [Crypto]
}
})

Expand Down
16 changes: 11 additions & 5 deletions test/transports/transport-manager.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ 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')
Expand Down Expand Up @@ -110,7 +111,8 @@ describe('libp2p.transportManager', () => {
libp2p = new Libp2p({
peerId,
modules: {
transport: [Transport]
transport: [Transport],
connEncryption: [Crypto]
}
})

Expand All @@ -128,7 +130,8 @@ describe('libp2p.transportManager', () => {
libp2p = new Libp2p({
peerId,
modules: {
transport: [spy]
transport: [spy],
connEncryption: [Crypto]
},
config: {
transport: {
Expand All @@ -152,7 +155,8 @@ describe('libp2p.transportManager', () => {
libp2p = new Libp2p({
peerId,
modules: {
transport: [Transport]
transport: [Transport],
connEncryption: [Crypto]
}
})

Expand Down Expand Up @@ -188,7 +192,8 @@ describe('libp2p.transportManager (dial only)', () => {
listen: [multiaddr('/ip4/127.0.0.1/tcp/0')]
},
modules: {
transport: [Transport]
transport: [Transport],
connEncryption: [Crypto]
}
})

Expand All @@ -212,7 +217,8 @@ describe('libp2p.transportManager (dial only)', () => {
faultTolerance: FaultTolerance.NO_FATAL
},
modules: {
transport: [Transport]
transport: [Transport],
connEncryption: [Crypto]
}
})

Expand Down

0 comments on commit de3a302

Please sign in to comment.