Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

fix: if client mode is specifed, do not auto-switch to server mode #475

Merged
merged 1 commit into from
May 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 27 additions & 23 deletions src/dual-kad-dht.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,29 +164,33 @@ export class DefaultDualKadDHT extends EventEmitter<PeerDiscoveryEvents> impleme
}))
})

components.events.addEventListener('self:peer:update', (evt) => {
log('received update of self-peer info')
const hasPublicAddress = evt.detail.peer.addresses
.some(({ multiaddr }) => {
const isPublic = multiaddrIsPublic(multiaddr)

log('%m is public %s', multiaddr, isPublic)

return isPublic
})

this.getMode()
.then(async mode => {
if (hasPublicAddress && mode === 'client') {
await this.setMode('server')
} else if (mode === 'server' && !hasPublicAddress) {
await this.setMode('client')
}
})
.catch(err => {
log.error('error setting dht server mode', err)
})
})
// if client mode has not been explicitly specified, auto-switch to server
// mode when the node's peer data is updated with publicly dialable addresses
if (init.clientMode == null) {
components.events.addEventListener('self:peer:update', (evt) => {
log('received update of self-peer info')
const hasPublicAddress = evt.detail.peer.addresses
.some(({ multiaddr }) => {
const isPublic = multiaddrIsPublic(multiaddr)

log('%m is public %s', multiaddr, isPublic)

return isPublic
})

this.getMode()
.then(async mode => {
if (hasPublicAddress && mode === 'client') {
await this.setMode('server')
} else if (mode === 'server' && !hasPublicAddress) {
await this.setMode('client')
}
})
.catch(err => {
log.error('error setting dht server mode', err)
})
})
}
}

readonly [Symbol.toStringTag] = '@libp2p/dual-kad-dht'
Expand Down