Skip to content

fix: encapsulate /p2p-circuit multiaddrs when dialing a known peerid #1680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions src/connection-manager/dial-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ export class DialQueue {
throw new CodeError('dial with more addresses than allowed', codes.ERR_TOO_MANY_ADDRESSES)
}

// append peer id to multiaddrs if it is not already present
// ensure the peer id is appended to the multiaddr
if (peerId != null) {
const peerIdMultiaddr = `/p2p/${peerId.toString()}`
dedupedMultiaddrs = dedupedMultiaddrs.map(addr => {
Expand All @@ -349,7 +349,8 @@ export class DialQueue {
return addr
}

if (addressPeerId == null) {
// append peer id to multiaddr if it is not already present
if (addressPeerId !== peerId.toString()) {
return {
multiaddr: addr.multiaddr.encapsulate(peerIdMultiaddr),
isCertified: addr.isCertified
Expand Down
20 changes: 20 additions & 0 deletions test/circuit-relay/relay.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type { Libp2p } from '@libp2p/interface-libp2p'
import { pbStream } from 'it-pb-stream'
import { HopMessage, Status } from '../../src/circuit-relay/pb/index.js'
import { Circuit } from '@multiformats/mafmt'
import { multiaddr } from '@multiformats/multiaddr'

describe('circuit-relay', () => {
describe('flows with 1 listener', () => {
Expand Down Expand Up @@ -392,6 +393,25 @@ describe('circuit-relay', () => {
await local.dial(ma)
})

it('should be able to dial a peer from its relayed address without peer id', async () => {
// discover relay and make reservation
await remote.dial(relay1.getMultiaddrs()[0])
await usingAsRelay(remote, relay1)

// get the relayed multiaddr without the remote's peer id
const ma = getRelayAddress(remote)
const maWithoutPeerId = multiaddr(`${ma.toString().split('/p2p-circuit')[0]}/p2p-circuit`)
expect(maWithoutPeerId.getPeerId()).to.not.equal(remote.peerId.toString())

// ensure this is the only address we have for the peer
await local.peerStore.addressBook.set(remote.peerId, [
maWithoutPeerId
])

// dial via peer id so we load the address from the address book
await local.dial(remote.peerId)
})

it('should not stay connected to a relay when not already connected and HOP fails', async () => {
// dial the remote through the relay
const relayedMultiaddr = relay1.getMultiaddrs()[0].encapsulate(`/p2p-circuit/p2p/${remote.peerId.toString()}`)
Expand Down