mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-30 01:11:34 +00:00
chore: store self protocols in protobook (#760)
This commit is contained in:
@ -51,9 +51,8 @@ class IdentifyService {
|
||||
* @class
|
||||
* @param {object} options
|
||||
* @param {Libp2p} options.libp2p
|
||||
* @param {Map<string, handler>} options.protocols - A reference to the protocols we support
|
||||
*/
|
||||
constructor ({ libp2p, protocols }) {
|
||||
constructor ({ libp2p }) {
|
||||
/**
|
||||
* @property {PeerStore}
|
||||
*/
|
||||
@ -74,10 +73,9 @@ class IdentifyService {
|
||||
*/
|
||||
this._libp2p = libp2p
|
||||
|
||||
this._protocols = protocols
|
||||
|
||||
this.handleMessage = this.handleMessage.bind(this)
|
||||
|
||||
// When a new connection happens, trigger identify
|
||||
this.connectionManager.on('peer:connect', (connection) => {
|
||||
const peerId = connection.remotePeer
|
||||
|
||||
@ -90,6 +88,13 @@ class IdentifyService {
|
||||
this.pushToPeerStore()
|
||||
}
|
||||
})
|
||||
|
||||
// When self protocols change, trigger identify-push
|
||||
this.peerStore.on('change:protocols', ({ peerId }) => {
|
||||
if (peerId.toString() === this.peerId.toString()) {
|
||||
this.pushToPeerStore()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
@ -101,7 +106,7 @@ class IdentifyService {
|
||||
async push (connections) {
|
||||
const signedPeerRecord = await this.peerStore.addressBook.getRawEnvelope(this.peerId)
|
||||
const listenAddrs = this._libp2p.multiaddrs.map((ma) => ma.bytes)
|
||||
const protocols = Array.from(this._protocols.keys())
|
||||
const protocols = this.peerStore.protoBook.get(this.peerId) || []
|
||||
|
||||
const pushes = connections.map(async connection => {
|
||||
try {
|
||||
@ -132,6 +137,11 @@ class IdentifyService {
|
||||
* @returns {void}
|
||||
*/
|
||||
pushToPeerStore () {
|
||||
// Do not try to push if libp2p node is not running
|
||||
if (!this._libp2p.isStarted()) {
|
||||
return
|
||||
}
|
||||
|
||||
const connections = []
|
||||
let connection
|
||||
for (const peer of this.peerStore.peers.values()) {
|
||||
@ -251,6 +261,7 @@ class IdentifyService {
|
||||
}
|
||||
|
||||
const signedPeerRecord = await this.peerStore.addressBook.getRawEnvelope(this.peerId)
|
||||
const protocols = this.peerStore.protoBook.get(this.peerId) || []
|
||||
|
||||
const message = Message.encode({
|
||||
protocolVersion: PROTOCOL_VERSION,
|
||||
@ -259,7 +270,7 @@ class IdentifyService {
|
||||
listenAddrs: this._libp2p.multiaddrs.map((ma) => ma.bytes),
|
||||
signedPeerRecord,
|
||||
observedAddr: connection.remoteAddr.bytes,
|
||||
protocols: Array.from(this._protocols.keys())
|
||||
protocols
|
||||
})
|
||||
|
||||
try {
|
||||
|
Reference in New Issue
Block a user