From fb73ed463f4b9c9bc417c623c946bfc79c2af085 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 17 Apr 2025 10:48:16 +0100 Subject: [PATCH] fix: do not dial bootstrap nodes Updates the bootstrap module to only discover the bootstrap nodes but not dial them. Invidiual protocols that require bootstrapping (e.g. KAD-DHT) can then react to the disovery and choose to dial the peers. This cuts down on unecessary dials for lightweight nodes and brings the bootstrap module in line with other peer discovery mechanisms. --- .../integration-tests/test/bootstrap.spec.ts | 48 ------------------- .../peer-discovery-bootstrap/package.json | 2 +- .../peer-discovery-bootstrap/src/index.ts | 6 --- .../test/bootstrap.spec.ts | 21 -------- 4 files changed, 1 insertion(+), 76 deletions(-) diff --git a/packages/integration-tests/test/bootstrap.spec.ts b/packages/integration-tests/test/bootstrap.spec.ts index ae83d9dba2..a57d189c86 100644 --- a/packages/integration-tests/test/bootstrap.spec.ts +++ b/packages/integration-tests/test/bootstrap.spec.ts @@ -3,9 +3,7 @@ import { bootstrap } from '@libp2p/bootstrap' import { generateKeyPair } from '@libp2p/crypto/keys' import { TypedEventEmitter, peerDiscoverySymbol } from '@libp2p/interface' -import { mplex } from '@libp2p/mplex' import { peerIdFromPrivateKey } from '@libp2p/peer-id' -import { plaintext } from '@libp2p/plaintext' import { webSockets } from '@libp2p/websockets' import { multiaddr } from '@multiformats/multiaddr' import { expect } from 'aegir/chai' @@ -104,50 +102,4 @@ describe('bootstrap', () => { return deferred.promise }) - - it('bootstrap should dial all peers in the list', async () => { - const deferred = defer() - - const list = [ - `${process.env.RELAY_MULTIADDR}` - ] - - libp2p = await createLibp2p({ - connectionEncrypters: [ - plaintext() - ], - transports: [ - webSockets() - ], - streamMuxers: [ - mplex() - ], - peerDiscovery: [ - bootstrap({ - list - }) - ], - connectionGater: { - denyDialMultiaddr: () => false - } - }) - - const expectedPeers = new Set( - list.map(ma => multiaddr(ma).getPeerId()) - ) - - libp2p.addEventListener('connection:open', (evt) => { - const { remotePeer } = evt.detail - - expectedPeers.delete(remotePeer.toString()) - if (expectedPeers.size === 0) { - libp2p.removeEventListener('connection:open') - deferred.resolve() - } - }) - - await libp2p.start() - - return deferred.promise - }) }) diff --git a/packages/peer-discovery-bootstrap/package.json b/packages/peer-discovery-bootstrap/package.json index c3def49181..0459010117 100644 --- a/packages/peer-discovery-bootstrap/package.json +++ b/packages/peer-discovery-bootstrap/package.json @@ -55,13 +55,13 @@ }, "dependencies": { "@libp2p/interface": "^2.9.0", - "@libp2p/interface-internal": "^2.3.11", "@libp2p/peer-id": "^5.1.2", "@multiformats/mafmt": "^12.1.6", "@multiformats/multiaddr": "^12.3.3" }, "devDependencies": { "@libp2p/interface-compliance-tests": "^6.4.5", + "@libp2p/interface-internal": "^2.3.11", "@libp2p/logger": "^5.1.15", "aegir": "^45.1.1", "sinon-ts": "^2.0.0" diff --git a/packages/peer-discovery-bootstrap/src/index.ts b/packages/peer-discovery-bootstrap/src/index.ts index f7527c24c0..437b7d5502 100644 --- a/packages/peer-discovery-bootstrap/src/index.ts +++ b/packages/peer-discovery-bootstrap/src/index.ts @@ -37,7 +37,6 @@ import { peerIdFromString } from '@libp2p/peer-id' import { P2P } from '@multiformats/mafmt' import { multiaddr } from '@multiformats/multiaddr' import type { ComponentLogger, Logger, PeerDiscovery, PeerDiscoveryEvents, PeerInfo, PeerStore, Startable } from '@libp2p/interface' -import type { ConnectionManager } from '@libp2p/interface-internal' const DEFAULT_BOOTSTRAP_TAG_NAME = 'bootstrap' const DEFAULT_BOOTSTRAP_TAG_VALUE = 50 @@ -77,7 +76,6 @@ export interface BootstrapInit { export interface BootstrapComponents { peerStore: PeerStore logger: ComponentLogger - connectionManager: ConnectionManager } /** @@ -183,10 +181,6 @@ class Bootstrap extends TypedEventEmitter implements PeerDi } this.safeDispatchEvent('peer', { detail: peerData }) - this.components.connectionManager.openConnection(peerData.id) - .catch(err => { - this.log.error('could not dial bootstrap peer %p', peerData.id, err) - }) } } diff --git a/packages/peer-discovery-bootstrap/test/bootstrap.spec.ts b/packages/peer-discovery-bootstrap/test/bootstrap.spec.ts index 7429053fb4..57601a6a55 100644 --- a/packages/peer-discovery-bootstrap/test/bootstrap.spec.ts +++ b/packages/peer-discovery-bootstrap/test/bootstrap.spec.ts @@ -57,27 +57,6 @@ describe('bootstrap', () => { await stop(r) }) - it('should dial bootstrap peers', async function () { - this.timeout(5 * 1000) - const r = bootstrap({ - list: peerList, - timeout: 100 - })(components) - - await start(r) - - await new Promise(resolve => { - const interval = setInterval(() => { - if (components.connectionManager.openConnection.callCount === 1) { - clearInterval(interval) - resolve() - } - }, 100) - }) - - await stop(r) - }) - it('should tag bootstrap peers', async function () { this.timeout(5 * 1000)