refactor: circuit relay to async (#477)

* refactor: add dialing over relay support

* chore: fix lint

* fix: dont clear listeners on close

* fix: if dial errors already have codes, just rethrow them

* fix: clear the registrar when libp2p stops

* fix: improve connection maintenance with circuit

* chore: correct feedback

* test: use chai as promised

* test(fix): reset multiaddrs on dial test
This commit is contained in:
Jacob Heun
2019-11-29 16:41:08 +01:00
parent 18a062ed12
commit f77ce39484
30 changed files with 937 additions and 1169 deletions

View File

@ -16,6 +16,7 @@ const { getPeerInfo, getPeerInfoRemote } = require('./get-peer-info')
const { validate: validateConfig } = require('./config')
const { codes } = require('./errors')
const Circuit = require('./circuit')
const Dialer = require('./dialer')
const TransportManager = require('./transport-manager')
const Upgrader = require('./upgrader')
@ -78,9 +79,6 @@ class Libp2p extends EventEmitter {
libp2p: this,
upgrader: this.upgrader
})
this._modules.transport.forEach((Transport) => {
this.transportManager.add(Transport.prototype[Symbol.toStringTag], Transport)
})
// Attach crypto channels
if (this._modules.connEncryption) {
@ -95,6 +93,12 @@ class Libp2p extends EventEmitter {
peerStore: this.peerStore
})
this._modules.transport.forEach((Transport) => {
this.transportManager.add(Transport.prototype[Symbol.toStringTag], Transport)
})
// TODO: enable relay if enabled
this.transportManager.add(Circuit.prototype[Symbol.toStringTag], Circuit)
// Attach stream multiplexers
if (this._modules.streamMuxer) {
const muxers = this._modules.streamMuxer
@ -182,6 +186,7 @@ class Libp2p extends EventEmitter {
this.pubsub && await this.pubsub.stop()
this._dht && await this._dht.stop()
await this.transportManager.close()
await this.registrar.close()
} catch (err) {
if (err) {
log.error(err)
@ -282,7 +287,10 @@ class Libp2p extends EventEmitter {
this.upgrader.protocols.set(protocol, handler)
})
this.dialer.identifyService.pushToPeerStore(this.peerStore)
// Only push if libp2p is running
if (this.isStarted()) {
this.dialer.identifyService.pushToPeerStore(this.peerStore)
}
}
/**
@ -296,7 +304,10 @@ class Libp2p extends EventEmitter {
this.upgrader.protocols.delete(protocol)
})
this.dialer.identifyService.pushToPeerStore(this.peerStore)
// Only push if libp2p is running
if (this.isStarted()) {
this.dialer.identifyService.pushToPeerStore(this.peerStore)
}
}
async _onStarting () {