mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-05-29 18:21:23 +00:00
fix: replace peerInfo addresses with listen addresses (#485)
* feat: replace peer info addresses with listen addresses * test: add listening test * chore: fix linting
This commit is contained in:
parent
995640ee2f
commit
acbbc0f84e
@ -301,10 +301,18 @@ class Libp2p extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async _onStarting () {
|
async _onStarting () {
|
||||||
|
// Listen on the addresses supplied in the peerInfo
|
||||||
const multiaddrs = this.peerInfo.multiaddrs.toArray()
|
const multiaddrs = this.peerInfo.multiaddrs.toArray()
|
||||||
|
|
||||||
await this.transportManager.listen(multiaddrs)
|
await this.transportManager.listen(multiaddrs)
|
||||||
|
|
||||||
|
// The addresses may change once the listener starts
|
||||||
|
// eg /ip4/0.0.0.0/tcp/0 => /ip4/192.168.1.0/tcp/58751
|
||||||
|
this.peerInfo.multiaddrs.clear()
|
||||||
|
for (const ma of this.transportManager.getAddrs()) {
|
||||||
|
this.peerInfo.multiaddrs.add(ma)
|
||||||
|
}
|
||||||
|
|
||||||
if (this._config.pubsub.enabled) {
|
if (this._config.pubsub.enabled) {
|
||||||
this.pubsub && this.pubsub.start()
|
this.pubsub && this.pubsub.start()
|
||||||
}
|
}
|
||||||
|
56
test/core/listening.node.js
Normal file
56
test/core/listening.node.js
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
'use strict'
|
||||||
|
/* eslint-env mocha */
|
||||||
|
|
||||||
|
const chai = require('chai')
|
||||||
|
chai.use(require('dirty-chai'))
|
||||||
|
const { expect } = chai
|
||||||
|
|
||||||
|
const multiaddr = require('multiaddr')
|
||||||
|
const Transport = require('libp2p-tcp')
|
||||||
|
|
||||||
|
const { create } = require('../../src')
|
||||||
|
const peerUtils = require('../utils/creators/peer')
|
||||||
|
|
||||||
|
const listenAddr = multiaddr('/ip4/0.0.0.0/tcp/0')
|
||||||
|
|
||||||
|
describe('Listening', () => {
|
||||||
|
let peerInfo
|
||||||
|
let libp2p
|
||||||
|
|
||||||
|
before(async () => {
|
||||||
|
[peerInfo] = await peerUtils.createPeerInfoFromFixture(1)
|
||||||
|
peerInfo.multiaddrs.add(listenAddr)
|
||||||
|
})
|
||||||
|
|
||||||
|
after(async () => {
|
||||||
|
await libp2p.stop()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should replace wildcard host and port with actual host and port on startup', async () => {
|
||||||
|
libp2p = await create({
|
||||||
|
peerInfo,
|
||||||
|
modules: {
|
||||||
|
transport: [Transport]
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
await libp2p.start()
|
||||||
|
|
||||||
|
const addrs = libp2p.peerInfo.multiaddrs.toArray()
|
||||||
|
|
||||||
|
// Should get something like:
|
||||||
|
// /ip4/127.0.0.1/tcp/50866
|
||||||
|
// /ip4/192.168.1.2/tcp/50866
|
||||||
|
expect(addrs.length).to.equal(2)
|
||||||
|
|
||||||
|
const opts = [addrs[0].toOptions(), addrs[1].toOptions()]
|
||||||
|
expect(opts[0].family).to.equal('ipv4')
|
||||||
|
expect(opts[1].family).to.equal('ipv4')
|
||||||
|
expect(opts[0].transport).to.equal('tcp')
|
||||||
|
expect(opts[1].transport).to.equal('tcp')
|
||||||
|
expect(opts[0].host).to.match(/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/)
|
||||||
|
expect(opts[1].host).to.match(/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/)
|
||||||
|
expect(opts[0].port).to.be.gt(0)
|
||||||
|
expect(opts[1].port).to.be.gt(0)
|
||||||
|
})
|
||||||
|
})
|
Loading…
x
Reference in New Issue
Block a user