Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove change:multiaddrs listener #387

Merged
merged 1 commit into from
Dec 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 1 addition & 35 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ import { InboundStream, OutboundStream } from './stream.js'
import { Uint8ArrayList } from 'uint8arraylist'
import { decodeRpc, DecodeRPCLimits, defaultDecodeRpcLimits } from './message/decodeRpc.js'
import { ConnectionManager } from '@libp2p/interface-connection-manager'
import { PeerMultiaddrsChangeData, PeerStore } from '@libp2p/interface-peer-store'
import { PeerStore } from '@libp2p/interface-peer-store'
import { Multiaddr } from '@multiformats/multiaddr'
import { multiaddrToIPStr } from './utils/multiaddr.js'

Expand Down Expand Up @@ -590,8 +590,6 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements PubSub<G
const heartbeatTimeout = setTimeout(this.runHeartbeat, constants.GossipsubHeartbeatInitialDelay)
// Then, run heartbeat every `heartbeatInterval` offset by `GossipsubHeartbeatInitialDelay`

this.components.peerStore.addEventListener('change:multiaddrs', this.onPeerAddressChange)

this.status = {
code: GossipStatusCode.started,
registrarTopologyIds,
Expand Down Expand Up @@ -628,8 +626,6 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements PubSub<G
const { registrarTopologyIds } = this.status
this.status = { code: GossipStatusCode.stopped }

this.components.peerStore.removeEventListener('change:multiaddrs', this.onPeerAddressChange)

// unregister protocol and handlers
const registrar = this.components.registrar
registrarTopologyIds.forEach((id) => registrar.unregister(id))
Expand Down Expand Up @@ -879,36 +875,6 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements PubSub<G
this.acceptFromWhitelist.delete(id)
}

private onPeerAddressChange = (evt: CustomEvent<PeerMultiaddrsChangeData>): void => {
const { peerId, multiaddrs, oldMultiaddrs } = evt.detail
const newIps = new Set<string>()
const oldIps = new Set<string>()
for (const mu of multiaddrs) {
const ipStr = multiaddrToIPStr(mu)
if (ipStr) {
newIps.add(ipStr)
}
}
for (const mu of oldMultiaddrs) {
const ipStr = multiaddrToIPStr(mu)
if (ipStr) {
// Remove multiaddrs that aren't new
if (newIps.has(ipStr)) {
newIps.delete(ipStr)
} else {
oldIps.add(ipStr)
}
}
}
const id = peerId.toString()
for (const ipStr of oldIps) {
this.score.removeIP(id, ipStr)
}
for (const ipStr of newIps) {
this.score.addIP(id, ipStr)
}
}

// API METHODS

get started(): boolean {
Expand Down