Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 41a7e55

Browse files
achingbrainAlan Shaw
authored and
Alan Shaw
committed
fix: report correct swarm addresses after listening on new addrs (#2749)
The user may start the node with no swarm addresses to speed up startup times - if they then use libp2p to listen on new transports we should return the addresses currently being listened on instead of those configured at startup. refs #2508
1 parent 2bc2d6f commit 41a7e55

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-4
lines changed

src/core/components/id.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@
33
const pkgversion = require('../../../package.json').version
44
const multiaddr = require('multiaddr')
55

6-
module.exports = ({ peerInfo }) => {
6+
module.exports = ({ peerInfo, libp2p }) => {
77
return async function id () { // eslint-disable-line require-await
88
const id = peerInfo.id.toB58String()
9+
let addresses = []
10+
11+
if (libp2p) {
12+
// only available while the node is running
13+
addresses = libp2p.transportManager.getAddrs()
14+
}
915

1016
return {
1117
id,
1218
publicKey: peerInfo.id.pubKey.bytes.toString('base64'),
13-
addresses: peerInfo.multiaddrs
14-
.toArray()
19+
addresses: addresses
1520
.map(ma => {
1621
const str = ma.toString()
1722

src/core/components/start.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ function createApi ({
208208
dns,
209209
files,
210210
get: Components.get({ ipld, preload }),
211-
id: Components.id({ peerInfo }),
211+
id: Components.id({ peerInfo, libp2p }),
212212
init: async () => { throw new AlreadyInitializedError() }, // eslint-disable-line require-await
213213
isOnline,
214214
key: {

test/core/id.spec.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const { expect } = require('interface-ipfs-core/src/utils/mocha')
5+
const multiaddr = require('multiaddr')
6+
const { isBrowser, isWebWorker } = require('ipfs-utils/src/env')
7+
const factory = require('../utils/factory')
8+
9+
describe('id', function () {
10+
this.timeout(60 * 1000)
11+
const df = factory()
12+
let node
13+
14+
before(async () => {
15+
node = (await df.spawn({
16+
type: 'proc',
17+
ipfsOptions: {
18+
config: {
19+
Addresses: {
20+
Swarm: []
21+
}
22+
}
23+
}
24+
})).api
25+
})
26+
27+
after(async () => {
28+
await node.stop()
29+
})
30+
31+
it('should return swarm ports opened after startup', async function () {
32+
if (isWebWorker) {
33+
// TODO: webworkers are not currently dialable
34+
return this.skip()
35+
}
36+
37+
await expect(node.id()).to.eventually.have.property('addresses').that.is.empty()
38+
39+
let servers = [
40+
multiaddr('/ip4/127.0.0.1/tcp/0')
41+
]
42+
43+
if (isBrowser) {
44+
servers = [
45+
multiaddr('/ip4/127.0.0.1/tcp/14579/wss/p2p-webrtc-star')
46+
]
47+
}
48+
49+
await node.libp2p.transportManager.listen(servers)
50+
await expect(node.id()).to.eventually.have.property('addresses').that.is.not.empty()
51+
})
52+
})

0 commit comments

Comments
 (0)