Skip to content

Commit fb73ed4

Browse files
committed
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.
1 parent 7b4fa53 commit fb73ed4

File tree

4 files changed

+1
-76
lines changed

4 files changed

+1
-76
lines changed

packages/integration-tests/test/bootstrap.spec.ts

-48
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
import { bootstrap } from '@libp2p/bootstrap'
44
import { generateKeyPair } from '@libp2p/crypto/keys'
55
import { TypedEventEmitter, peerDiscoverySymbol } from '@libp2p/interface'
6-
import { mplex } from '@libp2p/mplex'
76
import { peerIdFromPrivateKey } from '@libp2p/peer-id'
8-
import { plaintext } from '@libp2p/plaintext'
97
import { webSockets } from '@libp2p/websockets'
108
import { multiaddr } from '@multiformats/multiaddr'
119
import { expect } from 'aegir/chai'
@@ -104,50 +102,4 @@ describe('bootstrap', () => {
104102

105103
return deferred.promise
106104
})
107-
108-
it('bootstrap should dial all peers in the list', async () => {
109-
const deferred = defer()
110-
111-
const list = [
112-
`${process.env.RELAY_MULTIADDR}`
113-
]
114-
115-
libp2p = await createLibp2p({
116-
connectionEncrypters: [
117-
plaintext()
118-
],
119-
transports: [
120-
webSockets()
121-
],
122-
streamMuxers: [
123-
mplex()
124-
],
125-
peerDiscovery: [
126-
bootstrap({
127-
list
128-
})
129-
],
130-
connectionGater: {
131-
denyDialMultiaddr: () => false
132-
}
133-
})
134-
135-
const expectedPeers = new Set(
136-
list.map(ma => multiaddr(ma).getPeerId())
137-
)
138-
139-
libp2p.addEventListener('connection:open', (evt) => {
140-
const { remotePeer } = evt.detail
141-
142-
expectedPeers.delete(remotePeer.toString())
143-
if (expectedPeers.size === 0) {
144-
libp2p.removeEventListener('connection:open')
145-
deferred.resolve()
146-
}
147-
})
148-
149-
await libp2p.start()
150-
151-
return deferred.promise
152-
})
153105
})

packages/peer-discovery-bootstrap/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@
5555
},
5656
"dependencies": {
5757
"@libp2p/interface": "^2.9.0",
58-
"@libp2p/interface-internal": "^2.3.11",
5958
"@libp2p/peer-id": "^5.1.2",
6059
"@multiformats/mafmt": "^12.1.6",
6160
"@multiformats/multiaddr": "^12.3.3"
6261
},
6362
"devDependencies": {
6463
"@libp2p/interface-compliance-tests": "^6.4.5",
64+
"@libp2p/interface-internal": "^2.3.11",
6565
"@libp2p/logger": "^5.1.15",
6666
"aegir": "^45.1.1",
6767
"sinon-ts": "^2.0.0"

packages/peer-discovery-bootstrap/src/index.ts

-6
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import { peerIdFromString } from '@libp2p/peer-id'
3737
import { P2P } from '@multiformats/mafmt'
3838
import { multiaddr } from '@multiformats/multiaddr'
3939
import type { ComponentLogger, Logger, PeerDiscovery, PeerDiscoveryEvents, PeerInfo, PeerStore, Startable } from '@libp2p/interface'
40-
import type { ConnectionManager } from '@libp2p/interface-internal'
4140

4241
const DEFAULT_BOOTSTRAP_TAG_NAME = 'bootstrap'
4342
const DEFAULT_BOOTSTRAP_TAG_VALUE = 50
@@ -77,7 +76,6 @@ export interface BootstrapInit {
7776
export interface BootstrapComponents {
7877
peerStore: PeerStore
7978
logger: ComponentLogger
80-
connectionManager: ConnectionManager
8179
}
8280

8381
/**
@@ -183,10 +181,6 @@ class Bootstrap extends TypedEventEmitter<PeerDiscoveryEvents> implements PeerDi
183181
}
184182

185183
this.safeDispatchEvent('peer', { detail: peerData })
186-
this.components.connectionManager.openConnection(peerData.id)
187-
.catch(err => {
188-
this.log.error('could not dial bootstrap peer %p', peerData.id, err)
189-
})
190184
}
191185
}
192186

packages/peer-discovery-bootstrap/test/bootstrap.spec.ts

-21
Original file line numberDiff line numberDiff line change
@@ -57,27 +57,6 @@ describe('bootstrap', () => {
5757
await stop(r)
5858
})
5959

60-
it('should dial bootstrap peers', async function () {
61-
this.timeout(5 * 1000)
62-
const r = bootstrap({
63-
list: peerList,
64-
timeout: 100
65-
})(components)
66-
67-
await start(r)
68-
69-
await new Promise<void>(resolve => {
70-
const interval = setInterval(() => {
71-
if (components.connectionManager.openConnection.callCount === 1) {
72-
clearInterval(interval)
73-
resolve()
74-
}
75-
}, 100)
76-
})
77-
78-
await stop(r)
79-
})
80-
8160
it('should tag bootstrap peers', async function () {
8261
this.timeout(5 * 1000)
8362

0 commit comments

Comments
 (0)