From a5a48672c2c4807977203ff501c623dd312301f5 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Wed, 15 Mar 2023 11:42:13 +0100 Subject: [PATCH 1/3] fix!: remove connection manager autodial option The connection manager's `autoDial` option originally meant "dial every peer we discover". Then it changed to "if we have fewer than `minConnections`, dial peers from the peer store until we have more than `minConnections`". This is confusing and also redundant, since if we don't want to dial peers to ensure we have more than `minConnections` we can just set `minConnections` to `0`. Also fixes a bug where we actually configured two autodialer components. --- doc/CONFIGURATION.md | 7 +---- doc/GETTING_STARTED.md | 7 +---- doc/PEER_DISCOVERY.md | 1 - doc/migrations/v0.42-v0.43.md | 38 ++++++++++++++++++++++++++ examples/discovery-mechanisms/1.js | 2 +- examples/transports/4.js | 7 +---- src/config.ts | 1 - src/connection-manager/auto-dialler.ts | 16 ----------- src/connection-manager/index.ts | 5 ---- src/libp2p.ts | 7 ----- test/circuit/utils.ts | 3 -- test/dialing/resolver.spec.ts | 2 -- test/peer-discovery/index.node.ts | 11 +------- 13 files changed, 43 insertions(+), 64 deletions(-) diff --git a/doc/CONFIGURATION.md b/doc/CONFIGURATION.md index 817bf618f0..89e9a858f2 100644 --- a/doc/CONFIGURATION.md +++ b/doc/CONFIGURATION.md @@ -284,12 +284,7 @@ const node = await createLibp2p({ ], interval: 2000 ) - ], - connectionManager: { - autoDial: true // Auto connect to discovered peers (limited by ConnectionManager minConnections) - // 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. - } + ] }) ``` diff --git a/doc/GETTING_STARTED.md b/doc/GETTING_STARTED.md index cf5986aaf7..476d0c71cb 100644 --- a/doc/GETTING_STARTED.md +++ b/doc/GETTING_STARTED.md @@ -206,12 +206,7 @@ const node = await createLibp2p({ bootstrap({ list: bootstrapMultiaddrs, // provide array of multiaddrs }) - ], - connectionManager: { - autoDial: true, // Auto connect to discovered peers (limited by ConnectionManager minConnections) - // 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. - } + ] }) node.addEventListener('peer:discovery', (evt) => { diff --git a/doc/PEER_DISCOVERY.md b/doc/PEER_DISCOVERY.md index 9d7e4b5f26..d7ad2791aa 100644 --- a/doc/PEER_DISCOVERY.md +++ b/doc/PEER_DISCOVERY.md @@ -22,7 +22,6 @@ * To ensure reasonable resource usage, discovered peers are not connected to automatically * Applications should lisen for the `peer:connect` event if they wish to take a specific action when new connections are established * Libp2p functions best with a good number of network connections to a diverse set of peers. When the number of connected peers a node has falls under the [ConnectionManager](./CONFIGURATION.md#configuring-connection-manager) `minConnections` setting, randomly selected peers from the peer store will be dialed until the node's number of connections rises above this number. - * Applications can disable this behaviour via the `connectionManager.autoDial` config property, and handle increasing the current number of connections themselves ## Scenarios diff --git a/doc/migrations/v0.42-v0.43.md b/doc/migrations/v0.42-v0.43.md index c9e156dfd1..c8955870f1 100644 --- a/doc/migrations/v0.42-v0.43.md +++ b/doc/migrations/v0.42-v0.43.md @@ -5,6 +5,7 @@ A migration guide for refactoring your application code from libp2p v0.42.x to v ## Table of Contents - [Circuit Relay v2](#circuit-relay-v2) +- [Connection manager autodial](#connection-manager-autodial) ## Circuit Relay v2 @@ -77,3 +78,40 @@ const node = await createLibp2p({ ``` Please see the [Setup with Relay](https://github.com/libp2p/js-libp2p/blob/master/doc/CONFIGURATION.md#setup-with-relay) section of the configuration for a full breakdown of all the options. + +## Connection manager autodial + +The `autoDial` configutation option has been removed from the connection manager. + +This setting used to control whether libp2p would automatically dial every discovered peer, then it was changed to control whether libp2p would try to dial peers from the peer store to keep the number of connections above `minConnections`. + +Instead, just set `minConnections` to `0` if you don't want to keep a minimum number of connections open. + +**Before** + +```js +import { createLibp2p } from 'libp2p' + +const node = await createLibp2p({ + // ... other options + connectionManager: { + autoDial: false, + minConnections: 10, + maxConnections: 100 + } +} +``` + +**After** + +```js +import { createLibp2p } from 'libp2p' + +const node = await createLibp2p({ + // ... other options + connectionManager: { + minConnections: 0, + maxConnections: 100 + } +} +``` diff --git a/examples/discovery-mechanisms/1.js b/examples/discovery-mechanisms/1.js index 00015dfd8d..9ce8fc2b98 100644 --- a/examples/discovery-mechanisms/1.js +++ b/examples/discovery-mechanisms/1.js @@ -30,7 +30,7 @@ import bootstrapers from './bootstrappers.js' node.addEventListener('peer:discovery', (evt) => { const peer = evt.detail - // No need to dial, autoDial is on + console.log('Discovered:', peer.id.toString()) }) })(); diff --git a/examples/transports/4.js b/examples/transports/4.js index 07496f70be..07fedaddf3 100644 --- a/examples/transports/4.js +++ b/examples/transports/4.js @@ -35,12 +35,7 @@ const createNode = async (addresses = []) => { }) ], connectionEncryption: [noise()], - streamMuxers: [mplex()], - connectionManager: { - // Disable autoDial as it would fail because we are using a self-signed cert. - // `dialProtocol` does not fail because we pass `rejectUnauthorized: false`. - autoDial: false - } + streamMuxers: [mplex()] }) return node diff --git a/src/config.ts b/src/config.ts index 767f04bea3..651d418294 100644 --- a/src/config.ts +++ b/src/config.ts @@ -21,7 +21,6 @@ const DefaultConfig: Partial = { connectionManager: { maxConnections: 300, minConnections: 50, - autoDial: true, autoDialInterval: 10000, maxParallelDials: Constants.MAX_PARALLEL_DIALS, maxDialsPerPeer: Constants.MAX_PER_PEER_DIALS, diff --git a/src/connection-manager/auto-dialler.ts b/src/connection-manager/auto-dialler.ts index f35337a65e..d9e0da94bc 100644 --- a/src/connection-manager/auto-dialler.ts +++ b/src/connection-manager/auto-dialler.ts @@ -10,11 +10,6 @@ import type { PeerStore } from '@libp2p/interface-peer-store' const log = logger('libp2p:connection-manager:auto-dialler') export interface AutoDiallerInit { - /** - * Should preemptively guarantee connections are above the low watermark - */ - enabled?: boolean - /** * The minimum number of connections to avoid pruning */ @@ -33,7 +28,6 @@ export interface AutoDiallerComponents { } const defaultOptions: Partial = { - enabled: true, minConnections: 0, autoDialInterval: 10000 } @@ -66,11 +60,6 @@ export class AutoDialler implements Startable { * Starts the auto dialer */ async start (): Promise { - if (!this.options.enabled) { - log('not enabled') - return - } - this.running = true void this._autoDial().catch(err => { @@ -84,11 +73,6 @@ export class AutoDialler implements Startable { * Stops the auto dialler */ async stop (): Promise { - if (!this.options.enabled) { - log('not enabled') - return - } - this.running = false if (this.autoDialTimeout != null) { diff --git a/src/connection-manager/index.ts b/src/connection-manager/index.ts index c09929ecb4..01712a6f83 100644 --- a/src/connection-manager/index.ts +++ b/src/connection-manager/index.ts @@ -44,11 +44,6 @@ export interface ConnectionManagerConfig { */ pollInterval?: number - /** - * If true, try to connect to all discovered peers up to the connection manager limit - */ - autoDial?: boolean - /** * How long to wait between attempting to keep our number of concurrent connections * above minConnections diff --git a/src/libp2p.ts b/src/libp2p.ts index 4ddcafec2a..957a6c1324 100644 --- a/src/libp2p.ts +++ b/src/libp2p.ts @@ -157,12 +157,6 @@ export class Libp2pNode extends EventEmitter implements Libp2p { // update our peer record when addresses change this.configureComponent(new PeerRecordUpdater(this.components)) - this.configureComponent(new AutoDialler(this.components, { - enabled: init.connectionManager.autoDial, - minConnections: init.connectionManager.minConnections, - autoDialInterval: init.connectionManager.autoDialInterval - })) - // Create keychain const keychainOpts = DefaultKeyChain.generateOptions() this.keychain = this.configureComponent(new DefaultKeyChain(this.components, { @@ -237,7 +231,6 @@ export class Libp2pNode extends EventEmitter implements Libp2p { })) this.configureComponent(new AutoDialler(this.components, { - enabled: init.connectionManager.autoDial, minConnections: init.connectionManager.minConnections, autoDialInterval: init.connectionManager.autoDialInterval })) diff --git a/test/circuit/utils.ts b/test/circuit/utils.ts index 1e03ff260b..7154d1abaa 100644 --- a/test/circuit/utils.ts +++ b/test/circuit/utils.ts @@ -20,9 +20,6 @@ export function createNodeOptions (...overrides: Libp2pOptions[]): Libp2pOptions return createBaseOptions({ addresses: { listen: [listenAddr] - }, - connectionManager: { - autoDial: false } }, ...overrides) } diff --git a/test/dialing/resolver.spec.ts b/test/dialing/resolver.spec.ts index 731ddf40b1..7fc89dc147 100644 --- a/test/dialing/resolver.spec.ts +++ b/test/dialing/resolver.spec.ts @@ -49,7 +49,6 @@ describe('Dialing (resolvable addresses)', () => { listen: [`${relayAddr.toString()}/p2p-circuit`] }, connectionManager: { - autoDial: false, resolvers: { dnsaddr: resolver } @@ -62,7 +61,6 @@ describe('Dialing (resolvable addresses)', () => { listen: [`${relayAddr.toString()}/p2p-circuit`] }, connectionManager: { - autoDial: false, resolvers: { dnsaddr: resolver } diff --git a/test/peer-discovery/index.node.ts b/test/peer-discovery/index.node.ts index bcfeefab1e..97f0d27a38 100644 --- a/test/peer-discovery/index.node.ts +++ b/test/peer-discovery/index.node.ts @@ -74,9 +74,6 @@ describe('peer discovery scenarios', () => { listenAddr.toString() ] }, - connectionManager: { - autoDial: false - }, peerDiscovery: [ bootstrap({ list: bootstrappers @@ -122,10 +119,7 @@ describe('peer discovery scenarios', () => { interval: 200, // discover quickly serviceTag }) - ], - connectionManager: { - autoDial: false - } + ] }) libp2p = await createLibp2pNode(getConfig(peerId)) @@ -170,9 +164,6 @@ describe('peer discovery scenarios', () => { listenAddr.toString() ] }, - connectionManager: { - autoDial: false - }, dht: kadDHT() }) From 9b52f7f3b8f16ebe1c1f3b60d0584e2663fc7d23 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Wed, 15 Mar 2023 11:47:18 +0100 Subject: [PATCH 2/3] chore: update docs --- doc/PEER_DISCOVERY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/PEER_DISCOVERY.md b/doc/PEER_DISCOVERY.md index d7ad2791aa..7814c5fecc 100644 --- a/doc/PEER_DISCOVERY.md +++ b/doc/PEER_DISCOVERY.md @@ -22,6 +22,7 @@ * To ensure reasonable resource usage, discovered peers are not connected to automatically * Applications should lisen for the `peer:connect` event if they wish to take a specific action when new connections are established * Libp2p functions best with a good number of network connections to a diverse set of peers. When the number of connected peers a node has falls under the [ConnectionManager](./CONFIGURATION.md#configuring-connection-manager) `minConnections` setting, randomly selected peers from the peer store will be dialed until the node's number of connections rises above this number. + * Applications can disable this behaviour by setting the `connectionManager.minConnections` config property to `0`, and handle increasing the current number of connections themselves ## Scenarios From 0bfc0f202fbacdd5844d5776881f560b25adcf7c Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Fri, 17 Mar 2023 08:16:38 +0100 Subject: [PATCH 3/3] chore: review comments Co-authored-by: Chad Nehemiah --- doc/PEER_DISCOVERY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/PEER_DISCOVERY.md b/doc/PEER_DISCOVERY.md index 7814c5fecc..2f03a3fdb1 100644 --- a/doc/PEER_DISCOVERY.md +++ b/doc/PEER_DISCOVERY.md @@ -22,7 +22,7 @@ * To ensure reasonable resource usage, discovered peers are not connected to automatically * Applications should lisen for the `peer:connect` event if they wish to take a specific action when new connections are established * Libp2p functions best with a good number of network connections to a diverse set of peers. When the number of connected peers a node has falls under the [ConnectionManager](./CONFIGURATION.md#configuring-connection-manager) `minConnections` setting, randomly selected peers from the peer store will be dialed until the node's number of connections rises above this number. - * Applications can disable this behaviour by setting the `connectionManager.minConnections` config property to `0`, and handle increasing the current number of connections themselves +* Applications can disable this behaviour by setting the `connectionManager.minConnections` config property to `0`, but they will have to manage increasing the current number of connections themselves. ## Scenarios