Skip to content

Commit 51474c3

Browse files
chore: rename address functions and apply suggestions from code review
Co-Authored-By: Jacob Heun <jacobheun@gmail.com>
1 parent d75cc97 commit 51474c3

19 files changed

+78
-119
lines changed

doc/API.md

+16-16
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
* [`handle`](#handle)
1212
* [`unhandle`](#unhandle)
1313
* [`ping`](#ping)
14-
* [`getAdvertisingMultiaddrs`](#getadvertisingmultiaddrs)
15-
* [`addressManager.getListenMultiaddrs`](#addressmanagergetlistenmultiaddrs)
16-
* [`addressmger.getAnnounceMultiaddrs`](#addressmanagergetannouncemultiaddrs)
17-
* [`addressManager.getNoAnnounceMultiaddrs`](#addressmanagergetnoannouncemultiaddrs)
14+
* [`multiaddrs`](#multiaddrs)
15+
* [`addressManager.getListenAddrs`](#addressmanagergetlistenaddrs)
16+
* [`addressmger.getAnnounceAddrs`](#addressmanagergetannounceaddrs)
17+
* [`addressManager.getNoAnnounceAddrs`](#addressmanagergetnoannounceaddrs)
1818
* [`contentRouting.findProviders`](#contentroutingfindproviders)
1919
* [`contentRouting.provide`](#contentroutingprovide)
2020
* [`contentRouting.put`](#contentroutingput)
@@ -364,14 +364,14 @@ Pings a given peer and get the operation's latency.
364364
const latency = await libp2p.ping(otherPeerId)
365365
```
366366

367-
## getAdvertisingMultiaddrs
367+
## multiaddrs
368368

369369
Get peer advertising multiaddrs. This computes the advertising multiaddrs of the peer by
370370
joining the multiaddrs that libp2p transports are listening on with the announce multiaddrs
371371
provided in hte libp2p config. No announce multiaddrs will be filtered out, even when
372372
using random ports in the provided multiaddrs.
373373

374-
`libp2p.getAdvertisingMultiaddrs()`
374+
`libp2p.multiaddrs`
375375

376376
#### Returns
377377

@@ -383,15 +383,15 @@ using random ports in the provided multiaddrs.
383383

384384
```js
385385
// ...
386-
const listenMa = libp2p.getAdvertisingMultiaddrs()
386+
const listenMa = libp2p.multiaddrs
387387
// [ <Multiaddr 047f00000106f9ba - /ip4/127.0.0.1/tcp/63930> ]
388388
```
389389

390-
### addressManager.getListenMultiaddrs
390+
### addressManager.getListenAddrs
391391

392392
Get the multiaddrs that were provided for listening on libp2p transports.
393393

394-
`libp2p.addressManager.getListenMultiaddrs()`
394+
`libp2p.addressManager.getListenAddrs()`
395395

396396
#### Returns
397397

@@ -403,15 +403,15 @@ Get the multiaddrs that were provided for listening on libp2p transports.
403403

404404
```js
405405
// ...
406-
const listenMa = libp2p.addressManager.getListenMultiaddrs()
406+
const listenMa = libp2p.addressManager.getListenAddrs()
407407
// [ <Multiaddr 047f00000106f9ba - /ip4/127.0.0.1/tcp/63930> ]
408408
```
409409

410-
### addressManager.getAnnounceMultiaddrs
410+
### addressManager.getAnnounceAddrs
411411

412412
Get the multiaddrs that were provided to announce to the network.
413413

414-
`libp2p.addressManager.getAnnounceMultiaddrs()`
414+
`libp2p.addressManager.getAnnounceAddrs()`
415415

416416
#### Returns
417417

@@ -423,15 +423,15 @@ Get the multiaddrs that were provided to announce to the network.
423423

424424
```js
425425
// ...
426-
const announceMa = libp2p.addressManager.getAnnounceMultiaddrs()
426+
const announceMa = libp2p.addressManager.getAnnounceAddrs()
427427
// [ <Multiaddr 047f00000106f9ba - /dns4/peer.io/...> ]
428428
```
429429

430-
### addressManager.getNoAnnounceMultiaddrs
430+
### addressManager.getNoAnnounceAddrs
431431

432432
Get the multiaddrs that were provided to not announce to the network.
433433

434-
`libp2p.addressManager.getNoAnnounceMultiaddrs()`
434+
`libp2p.addressManager.getNoAnnounceAddrs()`
435435

436436
#### Returns
437437

@@ -443,7 +443,7 @@ Get the multiaddrs that were provided to not announce to the network.
443443

444444
```js
445445
// ...
446-
const noAnnounceMa = libp2p.addressManager.getNoAnnounceMultiaddrs()
446+
const noAnnounceMa = libp2p.addressManager.getNoAnnounceAddrs()
447447
// [ <Multiaddr 047f00000106f9ba - /ip4/127.0.0.1/tcp/63930> ]
448448
```
449449

doc/CONFIGURATION.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ Moreover, the majority of the modules can be customized via option parameters. T
204204
Besides the `modules` and `config`, libp2p allows other internal options and configurations:
205205
- `datastore`: an instance of [ipfs/interface-datastore](https://github.com/ipfs/interface-datastore/) modules.
206206
- This is used in modules such as the DHT. If it is not provided, `js-libp2p` will use an in memory datastore.
207-
- `peerId`: a previously computed instance of [libp2p/js-peer-id](https://github.com/libp2p/js-peer-id).
207+
- `peerId`: the identity of the node, an instance of [libp2p/js-peer-id](https://github.com/libp2p/js-peer-id).
208208
- This is particularly useful if you want to reuse the same `peer-id`, as well as for modules like `libp2p-delegated-content-routing`, which need a `peer-id` in their instantiation.
209209
- `addresses`: an object containing `listen`, `announce` and `noAnnounce` properties with `Array<string>`:
210210
- `listen` addresses will be provided to the libp2p underlying transports for listening on them.
@@ -215,8 +215,6 @@ Besides the `modules` and `config`, libp2p allows other internal options and con
215215

216216
#### Basic setup
217217

218-
TODO: should we add to the basic setup the configuration of listen addresses? we should probably make it a required option?
219-
220218
```js
221219
// Creating a libp2p node with:
222220
// transport: websockets + tcp

doc/GETTING_STARTED.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,16 @@ If you want to know more about libp2p stream multiplexing, you should read the f
136136

137137
Now that you have configured a [**Transport**][transport], [**Crypto**][crypto] and [**Stream Multiplexer**](streamMuxer) module, you can start your libp2p node. We can start and stop libp2p using the [`libp2p.start()`](./API.md#start) and [`libp2p.stop()`](./API.md#stop) methods.
138138

139-
TODO: add listen addresses here?
140-
141139
```js
142140
const Libp2p = require('libp2p')
143141
const WebSockets = require('libp2p-websockets')
144142
const SECIO = require('libp2p-secio')
145143
const MPLEX = require('libp2p-mplex')
146144

147145
const node = await Libp2p.create({
146+
addresses: {
147+
listen: ['/ip4/127.0.0.1/tcp/8000/ws']
148+
},
148149
modules: {
149150
transport: [WebSockets],
150151
connEncryption: [SECIO],
@@ -156,6 +157,12 @@ const node = await Libp2p.create({
156157
await node.start()
157158
console.log('libp2p has started')
158159

160+
const listenAddrs = node.transportManager.getAddrs()
161+
console.log('libp2p is listening on the following addresses: ', listenAddrs)
162+
163+
const advertiseAddrs = node.multiaddrs
164+
console.log('libp2p is advertising the following addresses: ', advertiseAddrs)
165+
159166
// stop libp2p
160167
await node.stop()
161168
console.log('libp2p has stopped')

src/address-manager/README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ These Addresses should be specified in your libp2p [configuration](../../doc/CON
88

99
A libp2p node should have a set of listen addresses, which will be used by libp2p underlying transports to listen for dials from other nodes in the network.
1010

11-
Before a libp2p node starts, a set of listen addresses should be provided to the AddressManager, so that when the node is started, the libp2p transports can use them to listen for connections. Accordingly, listen addresses should be specified through the libp2p configuration, in order to have the `AddressManager` created with them.
11+
Before a libp2p node starts, its configured listen addresses will be passed to the AddressManager, so that during startup the libp2p transports can use them to listen for connections. Accordingly, listen addresses should be specified through the libp2p configuration, in order to have the `AddressManager` created with them.
1212

13-
It is important pointing out that libp2p accepts to listen on addresses that intend to rely on any available local port. In this context, the provided listen addresses might not be exactly the same as the ones used by the transports. For example tcp may replace `/ip4/0.0.0.0/tcp/0` with something like `/ip4/0.0.0.0/tcp/8989`. As a consequence, libp2p should take into account this when advertising its addresses.
13+
It is important pointing out that libp2p accepts ephemeral listening addresses. In this context, the provided listen addresses might not be exactly the same as the ones used by the transports. For example TCP may replace `/ip4/0.0.0.0/tcp/0` with something like `/ip4/127.0.0.1/tcp/8989`. As a consequence, libp2p should take into account this when determining its advertised addresses.
1414

1515
## Announce Addresses
1616

@@ -22,13 +22,13 @@ Scenarios for Announce Addresses include:
2222

2323
## No Announce Addresses
2424

25-
While we need to add Announce Addresses to enable peers' connectivity, we can also not announce addresses that will not be reachable. This way, No Announce Addresses should be specified so that they are not announced by the peer as addresses that other peers can use to dial it.
25+
While we need to add Announce Addresses to enable peers' connectivity, we should also avoid announcing addresses that will not be reachable. No Announce Addresses should be specified so that they are filtered from the advertised multiaddrs.
2626

27-
As stated into the Listen Addresses section, Listen Addresses might get modified after libp2p transports get in action and use them to listen for new connections. This way, libp2p should also take into account these changes so that they can be matched when No Announce Addresses are being filtered out for advertising addresses.
27+
As stated in the Listen Addresses section, Listen Addresses might be modified by libp2p transports after the successfully bind to those addresses. Libp2p should also take these changes into account so that they can be matched when No Announce Addresses are being filtered out of the advertised multiaddrs.
2828

2929
## Implementation
3030

31-
When a libp2p node is created, the Address Manager will be populated from the provided addresses through the libp2p configuration. Once the node is started, the Transport Manager component will gather the listen addresses from the Address Manager, so that the libp2p transports use them to listen on.
31+
When a libp2p node is created, the Address Manager will be populated from the provided addresses through the libp2p configuration. Once the node is started, the Transport Manager component will gather the listen addresses from the Address Manager, so that the libp2p transports can attempt to bind to them.
3232

3333
Libp2p will use the the Address Manager as the source of truth when advertising the peers addresses. After all transports are ready, other libp2p components/subsystems will kickoff, namely the Identify Service and the DHT. Both of them will announce the node addresses to the other peers in the network. The announce and noAnnounce addresses will have an important role here and will be gathered by libp2p to compute its current addresses to advertise everytime it is needed.
3434

@@ -40,7 +40,7 @@ In a future iteration, we can enable these addresses to be modified in runtime.
4040

4141
#### Modify Listen Addresses
4242

43-
While adding new addresses to listen on runtime is a feasible operation, removing one listen address might have bad implications for the node, since all the connections using that listen address will be closed. With this in mind and taking also into consideration the lack of good use cases for removing listen addresses, the Address Manager API only allows libp2p users to add new Listen Addresses on runtime.
43+
While adding new addresses to listen on runtime should be trivial, removing a listen address might have bad implications for the node, since all the connections using that listen address will be closed. However, libp2p should provide a mechanism for both adding and removing listen addresses in the future.
4444

4545
Every time a new listen address is added, the Address Manager should emit an event with the new multiaddrs to listen. The Transport Manager should listen to this events and act accordingly.
4646

src/address-manager/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ log.error = debug('libp2p:addresses:error')
77
const multiaddr = require('multiaddr')
88

99
/**
10-
* Responsible for managing the peer addresses.
10+
* Responsible for managing this peers addresses.
1111
* Peers can specify their listen, announce and noAnnounce addresses.
1212
* The listen addresses will be used by the libp2p transports to listen for new connections,
1313
* while the announce an noAnnounce addresses will be combined with the listen addresses for
@@ -31,23 +31,23 @@ class AddressManager {
3131
* Get peer listen multiaddrs.
3232
* @return {Array<Multiaddr>}
3333
*/
34-
getListenMultiaddrs () {
34+
getListenAddrs () {
3535
return Array.from(this.listen).map((a) => multiaddr(a))
3636
}
3737

3838
/**
3939
* Get peer announcing multiaddrs.
4040
* @return {Array<Multiaddr>}
4141
*/
42-
getAnnounceMultiaddrs () {
42+
getAnnounceAddrs () {
4343
return Array.from(this.announce).map((a) => multiaddr(a))
4444
}
4545

4646
/**
4747
* Get peer noAnnouncing multiaddrs.
4848
* @return {Array<Multiaddr>}
4949
*/
50-
getNoAnnounceMultiaddrs () {
50+
getNoAnnounceAddrs () {
5151
return Array.from(this.noAnnounce).map((a) => multiaddr(a))
5252
}
5353
}

src/circuit/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class Circuit {
122122
type: CircuitPB.Type.HOP,
123123
srcPeer: {
124124
id: this.peerId.toBytes(),
125-
addrs: this.addressManager.getListenMultiaddrs().map(addr => addr.buffer)
125+
addrs: this.addressManager.getListenAddrs().map(addr => addr.buffer)
126126
},
127127
dstPeer: {
128128
id: destinationPeer.toBytes(),

src/identify/index.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,16 @@ class IdentifyService {
4949
* @param {Libp2p} options.libp2p
5050
* @param {Map<string, handler>} options.protocols A reference to the protocols we support
5151
*/
52-
constructor (options) {
52+
constructor ({ libp2p, protocols }) {
5353
/**
5454
* @property {PeerStore}
5555
*/
56-
this.peerStore = options.libp2p.peerStore
56+
this.peerStore = libp2p.peerStore
5757

5858
/**
5959
* @property {ConnectionManager}
6060
*/
61-
this.connectionManager = options.libp2p.connectionManager
61+
this.connectionManager = libp2p.connectionManager
6262

6363
this.connectionManager.on('peer:connect', (connection) => {
6464
const peerId = connection.remotePeer
@@ -69,14 +69,14 @@ class IdentifyService {
6969
/**
7070
* @property {PeerId}
7171
*/
72-
this.peerId = options.libp2p.peerId
72+
this.peerId = libp2p.peerId
7373

7474
/**
7575
* @property {AddressManager}
7676
*/
77-
this._libp2p = options.libp2p
77+
this._libp2p = libp2p
7878

79-
this._protocols = options.protocols
79+
this._protocols = protocols
8080

8181
this.handleMessage = this.handleMessage.bind(this)
8282
}
@@ -93,7 +93,7 @@ class IdentifyService {
9393

9494
await pipe(
9595
[{
96-
listenAddrs: this._libp2p.getAdvertisingMultiaddrs().map((ma) => ma.buffer),
96+
listenAddrs: this._libp2p.multiaddrs.map((ma) => ma.buffer),
9797
protocols: Array.from(this._protocols.keys())
9898
}],
9999
pb.encode(Message),
@@ -218,7 +218,7 @@ class IdentifyService {
218218
protocolVersion: PROTOCOL_VERSION,
219219
agentVersion: AGENT_VERSION,
220220
publicKey,
221-
listenAddrs: this._libp2p.getAdvertisingMultiaddrs().map((ma) => ma.buffer),
221+
listenAddrs: this._libp2p.multiaddrs.map((ma) => ma.buffer),
222222
observedAddr: connection.remoteAddr.buffer,
223223
protocols: Array.from(this._protocols.keys())
224224
})

src/index.js

+3-23
Original file line numberDiff line numberDiff line change
@@ -300,25 +300,15 @@ class Libp2p extends EventEmitter {
300300
* Get peer advertising multiaddrs by concating the addresses used
301301
* by transports to listen with the announce addresses.
302302
* Duplicated addresses and noAnnounce addresses are filtered out.
303-
* This takes into account random ports on matching noAnnounce addresses.
304303
* @return {Array<Multiaddr>}
305304
*/
306-
getAdvertisingMultiaddrs () {
305+
get multiaddrs () {
307306
// Filter noAnnounce multiaddrs
308-
const filterMa = this.addressManager.getNoAnnounceMultiaddrs()
309-
310-
// Special filter for noAnnounce addresses using a random port
311-
// eg /ip4/0.0.0.0/tcp/0 => /ip4/192.168.1.0/tcp/58751
312-
const filterSpecial = filterMa
313-
.map((ma) => ({
314-
protos: ma.protos(),
315-
...ma.toOptions()
316-
}))
317-
.filter((op) => op.port === 0)
307+
const filterMa = this.addressManager.getNoAnnounceAddrs()
318308

319309
// Create advertising list
320310
return this.transportManager.getAddrs()
321-
.concat(this.addressManager.getAnnounceMultiaddrs())
311+
.concat(this.addressManager.getAnnounceAddrs())
322312
.filter((ma, index, array) => {
323313
// Filter out if repeated
324314
if (array.findIndex((otherMa) => otherMa.equals(ma)) !== index) {
@@ -330,16 +320,6 @@ class Libp2p extends EventEmitter {
330320
return false
331321
}
332322

333-
// Filter out if in the special filter
334-
const options = ma.toOptions()
335-
if (filterSpecial.find((op) =>
336-
op.family === options.family &&
337-
op.host === options.host &&
338-
op.transport === options.transport &&
339-
op.protos.length === ma.protos().length
340-
)) {
341-
return false
342-
}
343323
return true
344324
})
345325
}

src/transport-manager.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class TransportManager {
131131
* @async
132132
*/
133133
async listen () {
134-
const addrs = this.libp2p.addressManager.getListenMultiaddrs()
134+
const addrs = this.libp2p.addressManager.getListenAddrs()
135135

136136
if (addrs.length === 0) {
137137
log('no addresses were provided for listening, this node is dial only')

test/addresses/address-manager.spec.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('Address Manager', () => {
3232
expect(am.announce.size).to.equal(0)
3333
expect(am.noAnnounce.size).to.equal(0)
3434

35-
const listenMultiaddrs = am.getListenMultiaddrs()
35+
const listenMultiaddrs = am.getListenAddrs()
3636
expect(listenMultiaddrs.length).to.equal(2)
3737
expect(listenMultiaddrs[0].equals(multiaddr(listenAddresses[0]))).to.equal(true)
3838
expect(listenMultiaddrs[1].equals(multiaddr(listenAddresses[1]))).to.equal(true)
@@ -48,7 +48,7 @@ describe('Address Manager', () => {
4848
expect(am.announce.size).to.equal(announceAddreses.length)
4949
expect(am.noAnnounce.size).to.equal(0)
5050

51-
const announceMultiaddrs = am.getAnnounceMultiaddrs()
51+
const announceMultiaddrs = am.getAnnounceAddrs()
5252
expect(announceMultiaddrs.length).to.equal(1)
5353
expect(announceMultiaddrs[0].equals(multiaddr(announceAddreses[0]))).to.equal(true)
5454
})
@@ -63,7 +63,7 @@ describe('Address Manager', () => {
6363
expect(am.announce.size).to.equal(0)
6464
expect(am.noAnnounce.size).to.equal(listenAddresses.length)
6565

66-
const noAnnounceMultiaddrs = am.getNoAnnounceMultiaddrs()
66+
const noAnnounceMultiaddrs = am.getNoAnnounceAddrs()
6767
expect(noAnnounceMultiaddrs.length).to.equal(2)
6868
expect(noAnnounceMultiaddrs[0].equals(multiaddr(listenAddresses[0]))).to.equal(true)
6969
expect(noAnnounceMultiaddrs[1].equals(multiaddr(listenAddresses[1]))).to.equal(true)

0 commit comments

Comments
 (0)