Skip to content
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

fix: make getPeerId return the target peer id from relay addrs #325

Merged
merged 3 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 12 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -655,14 +655,21 @@ class DefaultMultiaddr implements Multiaddr {

getPeerId (): string | null {
try {
const tuples = this.stringTuples().filter((tuple) => {
if (tuple[0] === names.ipfs.code) {
return true
let tuples: Array<[number, string | undefined]> = []

this.stringTuples().forEach(([code, name]) => {
if (code === names.p2p.code) {
tuples.push([code, name])
}

// if this is a p2p-circuit address, return the target peer id if present
// not the peer id of the relay
if (code === names['p2p-circuit'].code) {
tuples = []
}
Comment on lines +641 to 649

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i find it confusing that we push to tuples and then empty tuples if p2p-circuit is found.

Could this result in (eventually longer and currently unknown) multiaddr tuples being removed that shouldn't? Instead, should we just not call tuples.push if it's the p2p-circuit case for the current code?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, because circuit relay addresses are not supposed to be relayed themselves so whatever is after p2p-circuit should be what we are interested in.

We reset the tuples because whatever was before the p2p-circuit tuple is the relay address and we're trying to extract the PeerId of the target, not the relay.

It's trying to handle the case of:

/ip4/foo/p2p/QmRelay/p2p-circuit -> should return null not QmRelay

return false
})

// Get the last ipfs tuple ['ipfs', 'peerid string']
// Get the last ipfs tuple ['p2p', 'peerid string']
const tuple = tuples.pop()
if (tuple?.[1] != null) {
const peerIdStr = tuple[1]
Expand Down
5 changes: 5 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,11 @@ describe('helpers', () => {
multiaddr('/p2p-circuit/p2p/12D3KooWNvSZnPi3RrhrTwEY4LuuBeB6K6facKUCJcyWG1aoDd2p').getPeerId()
).to.equal('12D3KooWNvSZnPi3RrhrTwEY4LuuBeB6K6facKUCJcyWG1aoDd2p')
})
it('does not extract a peer Id from a circuit relay multiaddr where only the relay peer id is present', () => {
expect(
multiaddr('/ip4/127.0.0.1/tcp/123/p2p/bafzbeigweq4zr4x4ky2dvv7nanbkw6egutvrrvzw6g3h2rftp7gidyhtt4/p2p-circuit').getPeerId()
).to.be.null()
})
Comment on lines +1000 to +1004

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the positive test case (peerId of relayed peer exists) already exists in the tests from what I can see. However, do we need to check a case where we have something like

/ip4/127.0.0.1/tcp/123/p2p/bafzbeigweq4zr4x4ky2dvv7nanbkw6egutvrrvzw6g3h2rftp7gidyhtt4/p2p-circuit/p2p/bafzbeidt255unskpefjmqb2rc27vjuyxopkxgaylxij6pw35hhys4vnyp4

to make sure the correct peer ID is returned when there are two?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we have this in the 'extracts the correct peer Id from a circuit multiaddr' test?

})

describe('.getPeerId should return null on missing peer id in multiaddr', () => {
Expand Down