mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-26 07:21:36 +00:00
chore: auto relay multiaddr update push
This commit is contained in:
committed by
Vasco Santos
parent
caf66ea143
commit
4d1fcdb3d2
@ -143,8 +143,8 @@ class AutoRelay {
|
||||
|
||||
try {
|
||||
await this._transportManager.listen([multiaddr(listenAddr)])
|
||||
// TODO: push announce multiaddrs update
|
||||
// await this._libp2p.identifyService.pushToPeerStore()
|
||||
// Announce multiaddrs update on listen success
|
||||
await this._libp2p.identifyService.pushToPeerStore()
|
||||
} catch (err) {
|
||||
log.error(err)
|
||||
this._listenRelays.delete(id)
|
||||
|
@ -20,8 +20,8 @@ module.exports = (libp2p) => {
|
||||
const deleted = listeningAddrs.delete(connection.remotePeer.toB58String())
|
||||
|
||||
if (deleted) {
|
||||
// TODO push announce multiaddrs update
|
||||
// libp2p.identifyService.pushToPeerStore()
|
||||
// Announce multiaddrs update on listen success
|
||||
libp2p.identifyService.pushToPeerStore()
|
||||
}
|
||||
})
|
||||
|
||||
|
@ -329,13 +329,30 @@ class IdentifyService {
|
||||
* @returns {Uint8Array}
|
||||
*/
|
||||
async _getSelfPeerRecord () {
|
||||
const selfSignedPeerRecord = this.peerStore.addressBook.getRawEnvelope(this.peerId)
|
||||
// Update self peer record if needed
|
||||
await this._createOrUpdateSelfPeerRecord()
|
||||
|
||||
// TODO: support invalidation when dynamic multiaddrs are supported
|
||||
if (selfSignedPeerRecord) {
|
||||
return selfSignedPeerRecord
|
||||
return this.peerStore.addressBook.getRawEnvelope(this.peerId)
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates or updates the self peer record if it exists and is outdated.
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
async _createOrUpdateSelfPeerRecord () {
|
||||
const selfPeerRecordEnvelope = await this.peerStore.addressBook.getPeerRecord(this.peerId)
|
||||
|
||||
if (selfPeerRecordEnvelope) {
|
||||
const peerRecord = PeerRecord.createFromProtobuf(selfPeerRecordEnvelope.payload)
|
||||
|
||||
const mIntersection = peerRecord.multiaddrs.filter((m) => this._libp2p.multiaddrs.some((newM) => m.equals(newM)))
|
||||
if (mIntersection.length === this._libp2p.multiaddrs.length) {
|
||||
// Same multiaddrs as already existing in the record, no need to proceed
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Create / Update Peer record
|
||||
try {
|
||||
const peerRecord = new PeerRecord({
|
||||
peerId: this.peerId,
|
||||
@ -343,12 +360,9 @@ class IdentifyService {
|
||||
})
|
||||
const envelope = await Envelope.seal(peerRecord, this.peerId)
|
||||
this.peerStore.addressBook.consumePeerRecord(envelope)
|
||||
|
||||
return this.peerStore.addressBook.getRawEnvelope(this.peerId)
|
||||
} catch (err) {
|
||||
log.error('failed to get self peer record')
|
||||
log.error('failed to create self peer record')
|
||||
}
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -259,6 +259,7 @@ class Libp2p extends EventEmitter {
|
||||
await this.peerStore.stop()
|
||||
await this.connectionManager.stop()
|
||||
|
||||
ping.unmount(this)
|
||||
await Promise.all([
|
||||
this.pubsub && this.pubsub.stop(),
|
||||
this._dht && this._dht.stop(),
|
||||
@ -267,7 +268,6 @@ class Libp2p extends EventEmitter {
|
||||
|
||||
await this.transportManager.close()
|
||||
|
||||
ping.unmount(this)
|
||||
this.dialer.destroy()
|
||||
} catch (err) {
|
||||
if (err) {
|
||||
|
Reference in New Issue
Block a user