mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-25 10:32:14 +00:00
fix: protocols change event
I was spelunking last night and found that pubsub was constantly having it's `onConnect` handler fired from the multicodec topology. It was because bitswap was calling `dialProtocol` to connect to a peer. `dialProtocol` makes a `PeerInfo` out of the multiaddr it's given but it has no `protocols` i.e. `[]`. This is passed to `peerStore.update()` where we compare `[]` with an array of populated protocols. Obviously there is no intersection here so `change:protocols` was being emitted, even though no protocols were added or removed. This logic needs to be improved and tested properly but I just wanted to send a PR to document my findings.
This commit is contained in:
parent
8bed8f39ff
commit
10d7212373
@ -133,18 +133,15 @@ class PeerStore extends EventEmitter {
|
||||
})
|
||||
}
|
||||
|
||||
// Update protocols
|
||||
// TODO: better track added and removed protocols
|
||||
const protocolsIntersection = new Set(
|
||||
[...recorded.protocols].filter((p) => peerInfo.protocols.has(p))
|
||||
)
|
||||
|
||||
if (protocolsIntersection.size !== peerInfo.protocols.size ||
|
||||
protocolsIntersection.size !== recorded.protocols.size) {
|
||||
let isProtocolsChanged = false
|
||||
for (const protocol of peerInfo.protocols) {
|
||||
if (!recorded.protocols.has(protocol)) {
|
||||
isProtocolsChanged = true
|
||||
recorded.protocols.add(protocol)
|
||||
}
|
||||
}
|
||||
|
||||
if (isProtocolsChanged) {
|
||||
this.emit('change:protocols', {
|
||||
peerInfo: recorded,
|
||||
protocols: Array.from(recorded.protocols)
|
||||
|
Loading…
x
Reference in New Issue
Block a user