Skip to content

Commit afb8e09

Browse files
committed
custom dial logic
1 parent 9555c61 commit afb8e09

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

src/components/P2P/index.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,22 @@ export class OceanP2P extends EventEmitter {
162162
try {
163163
const peerInfo = details.detail
164164
P2P_LOGGER.debug('Discovered new peer:' + peerInfo.id.toString())
165+
166+
// v2/v3: autodialer was removed - we implement custom dial logic
167+
const currentConnections = this._libp2p.getConnections().length
168+
const { minConnections, maxConnections } = this._config.p2pConfig
169+
170+
// Only dial if we're below minConnections or have room for more
171+
if (currentConnections < minConnections || currentConnections < maxConnections) {
172+
const existingConnections = this._libp2p.getConnections(peerInfo.id)
173+
if (existingConnections.length === 0) {
174+
this._libp2p.dial(peerInfo.id).catch((err: Error) => {
175+
P2P_LOGGER.debug(
176+
`Failed to dial discovered peer ${peerInfo.id}: ${err.message}`
177+
)
178+
})
179+
}
180+
}
165181
} catch (e) {
166182
// no panic if it failed
167183
// console.error(e)
@@ -337,27 +353,24 @@ export class OceanP2P extends EventEmitter {
337353

338354
let transports = []
339355
P2P_LOGGER.info('Enabling P2P Transports: websockets, tcp, circuitRelay')
356+
// relay discovery is now automatic through the network's RandomWalk component
340357
transports = [webSockets(), tcp(), circuitRelayTransport()]
341358

342359
let options = {
343360
addresses,
344361
privateKey: config.keys.privateKey,
345362
transports,
346363
streamMuxers: [yamux()],
347-
connectionEncryption: [
364+
connectionEncrypters: [
348365
noise()
349366
// plaintext()
350367
],
351368
services: servicesConfig,
352369
connectionManager: {
353-
maxParallelDials: config.p2pConfig.connectionsMaxParallelDials, // 150 total parallel multiaddr dials
354-
dialTimeout: config.p2pConfig.connectionsDialTimeout, // 10 second dial timeout per peer dial
355-
minConnections: config.p2pConfig.minConnections,
370+
maxParallelDials: config.p2pConfig.connectionsMaxParallelDials,
371+
dialTimeout: config.p2pConfig.connectionsDialTimeout,
356372
maxConnections: config.p2pConfig.maxConnections,
357-
autoDialPeerRetryThreshold: config.p2pConfig.autoDialPeerRetryThreshold,
358-
autoDialConcurrency: config.p2pConfig.autoDialConcurrency,
359-
maxPeerAddrsToDial: config.p2pConfig.maxPeerAddrsToDial,
360-
autoDialInterval: config.p2pConfig.autoDialInterval
373+
maxPeerAddrsToDial: config.p2pConfig.maxPeerAddrsToDial
361374
}
362375
}
363376
if (config.p2pConfig.bootstrapNodes && config.p2pConfig.bootstrapNodes.length > 0) {
@@ -654,7 +667,7 @@ export class OceanP2P extends EventEmitter {
654667
const options = {
655668
signal: AbortSignal.timeout(10000),
656669
priority: 100,
657-
runOnTransientConnection: true
670+
runOnLimitedConnection: true
658671
}
659672
const connection = await this._libp2p.dial(multiaddrs, options)
660673
if (connection.remotePeer.toString() !== peerId.toString()) {

0 commit comments

Comments
 (0)