Skip to content

Commit b7e8706

Browse files
authoredJan 21, 2022
fix: make tests more reliable (#1139)
Try to use only public functions and properties to verify test behaviour
1 parent 4c3bf01 commit b7e8706

File tree

3 files changed

+100
-189
lines changed

3 files changed

+100
-189
lines changed
 

‎doc/CONFIGURATION.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ const node = await Libp2p.create({
592592
593593
#### Configuring Transport Manager
594594
595-
The Transport Manager is responsible for managing the libp2p transports life cycle. This includes starting listeners for the provided listen addresses, closing these listeners and dialing using the provided transports. By default, if a libp2p node has a list of multiaddrs for listenning on and there are no valid transports for those multiaddrs, libp2p will throw an error on startup and shutdown. However, for some applications it is perfectly acceptable for libp2p nodes to start in dial only mode if all the listen multiaddrs failed. This error tolerance can be enabled as follows:
595+
The Transport Manager is responsible for managing the libp2p transports life cycle. This includes starting listeners for the provided listen addresses, closing these listeners and dialing using the provided transports. By default, if a libp2p node has a list of multiaddrs for listening on and there are no valid transports for those multiaddrs, libp2p will throw an error on startup and shutdown. However, for some applications it is perfectly acceptable for libp2p nodes to start in dial only mode if all the listen multiaddrs failed. This error tolerance can be enabled as follows:
596596
597597
```js
598598
const Libp2p = require('libp2p')

‎src/circuit/auto-relay.js

+24-17
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const log = Object.assign(debug('libp2p:auto-relay'), {
88
const { fromString: uint8ArrayFromString } = require('uint8arrays/from-string')
99
const { toString: uint8ArrayToString } = require('uint8arrays/to-string')
1010
const { Multiaddr } = require('multiaddr')
11+
const all = require('it-all')
1112

1213
const { relay: multicodec } = require('./multicodec')
1314
const { canHop } = require('./circuit/hop')
@@ -149,26 +150,27 @@ class AutoRelay {
149150
* @returns {Promise<void>}
150151
*/
151152
async _addListenRelay (connection, id) {
152-
// Check if already listening on enough relays
153-
if (this._listenRelays.size >= this.maxListeners) {
154-
return
155-
}
153+
try {
154+
// Check if already listening on enough relays
155+
if (this._listenRelays.size >= this.maxListeners) {
156+
return
157+
}
156158

157-
// Get peer known addresses and sort them per public addresses first
158-
const remoteAddrs = await this._peerStore.addressBook.getMultiaddrsForPeer(
159-
connection.remotePeer, this._addressSorter
160-
)
159+
// Get peer known addresses and sort them per public addresses first
160+
const remoteAddrs = await this._peerStore.addressBook.getMultiaddrsForPeer(
161+
connection.remotePeer, this._addressSorter
162+
)
161163

162-
if (!remoteAddrs || !remoteAddrs.length) {
163-
return
164-
}
164+
if (!remoteAddrs || !remoteAddrs.length) {
165+
return
166+
}
165167

166-
const listenAddr = `${remoteAddrs[0].toString()}/p2p-circuit`
167-
this._listenRelays.add(id)
168+
const listenAddr = `${remoteAddrs[0].toString()}/p2p-circuit`
169+
this._listenRelays.add(id)
168170

169-
// Attempt to listen on relay
170-
try {
171+
// Attempt to listen on relay
171172
await this._transportManager.listen([new Multiaddr(listenAddr)])
173+
172174
// Announce multiaddrs will update on listen success by TransportManager event being triggered
173175
} catch (/** @type {any} */ err) {
174176
this._onError(err)
@@ -206,13 +208,18 @@ class AutoRelay {
206208
}
207209

208210
const knownHopsToDial = []
211+
const peers = await all(this._peerStore.getPeers())
209212

210213
// Check if we have known hop peers to use and attempt to listen on the already connected
211-
for await (const { id, metadata } of this._peerStore.getPeers()) {
214+
for await (const { id, metadata } of peers) {
212215
const idStr = id.toB58String()
213216

214217
// Continue to next if listening on this or peer to ignore
215-
if (this._listenRelays.has(idStr) || peersToIgnore.includes(idStr)) {
218+
if (this._listenRelays.has(idStr)) {
219+
continue
220+
}
221+
222+
if (peersToIgnore.includes(idStr)) {
216223
continue
217224
}
218225

0 commit comments

Comments
 (0)
Please sign in to comment.