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:
Alan Shaw 2020-02-06 09:23:30 +00:00 committed by GitHub
parent 8bed8f39ff
commit 10d7212373
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -133,18 +133,15 @@ class PeerStore extends EventEmitter {
}) })
} }
// Update protocols let isProtocolsChanged = false
// 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) {
for (const protocol of peerInfo.protocols) { for (const protocol of peerInfo.protocols) {
if (!recorded.protocols.has(protocol)) {
isProtocolsChanged = true
recorded.protocols.add(protocol) recorded.protocols.add(protocol)
} }
}
if (isProtocolsChanged) {
this.emit('change:protocols', { this.emit('change:protocols', {
peerInfo: recorded, peerInfo: recorded,
protocols: Array.from(recorded.protocols) protocols: Array.from(recorded.protocols)