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 tests more reliable #1139

Merged
merged 2 commits into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion doc/CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ const node = await Libp2p.create({

#### Configuring Transport Manager

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:
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:

```js
const Libp2p = require('libp2p')
Expand Down
41 changes: 24 additions & 17 deletions src/circuit/auto-relay.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const log = Object.assign(debug('libp2p:auto-relay'), {
const { fromString: uint8ArrayFromString } = require('uint8arrays/from-string')
const { toString: uint8ArrayToString } = require('uint8arrays/to-string')
const { Multiaddr } = require('multiaddr')
const all = require('it-all')

const { relay: multicodec } = require('./multicodec')
const { canHop } = require('./circuit/hop')
Expand Down Expand Up @@ -149,26 +150,27 @@ class AutoRelay {
* @returns {Promise<void>}
*/
async _addListenRelay (connection, id) {
// Check if already listening on enough relays
if (this._listenRelays.size >= this.maxListeners) {
return
}
try {
// Check if already listening on enough relays
if (this._listenRelays.size >= this.maxListeners) {
return
}

// Get peer known addresses and sort them per public addresses first
const remoteAddrs = await this._peerStore.addressBook.getMultiaddrsForPeer(
connection.remotePeer, this._addressSorter
)
// Get peer known addresses and sort them per public addresses first
const remoteAddrs = await this._peerStore.addressBook.getMultiaddrsForPeer(
connection.remotePeer, this._addressSorter
)

if (!remoteAddrs || !remoteAddrs.length) {
return
}
if (!remoteAddrs || !remoteAddrs.length) {
return
}

const listenAddr = `${remoteAddrs[0].toString()}/p2p-circuit`
this._listenRelays.add(id)
const listenAddr = `${remoteAddrs[0].toString()}/p2p-circuit`
this._listenRelays.add(id)

// Attempt to listen on relay
try {
// Attempt to listen on relay
await this._transportManager.listen([new Multiaddr(listenAddr)])

// Announce multiaddrs will update on listen success by TransportManager event being triggered
} catch (/** @type {any} */ err) {
this._onError(err)
Expand Down Expand Up @@ -206,13 +208,18 @@ class AutoRelay {
}

const knownHopsToDial = []
const peers = await all(this._peerStore.getPeers())

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

// Continue to next if listening on this or peer to ignore
if (this._listenRelays.has(idStr) || peersToIgnore.includes(idStr)) {
if (this._listenRelays.has(idStr)) {
continue
}

if (peersToIgnore.includes(idStr)) {
continue
}

Expand Down
Loading