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: limit observed addresses in address manager #2869

Merged
merged 1 commit into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
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
15 changes: 15 additions & 0 deletions packages/libp2p/src/address-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import type { ComponentLogger, Libp2pEvents, Logger, TypedEventTarget, PeerId, P
import type { AddressManager as AddressManagerInterface, TransportManager } from '@libp2p/interface-internal'
import type { Multiaddr } from '@multiformats/multiaddr'

export const defaultValues = {
maxObservedAddresses: 10
}

export interface AddressManagerInit {
/**
* Pass an function in this field to override the list of addresses
Expand All @@ -32,6 +36,11 @@ export interface AddressManagerInit {
* A list of string multiaddrs to add to the list of announced addresses
*/
appendAnnounce?: string[]

/**
* Limits the number of observed addresses we will store
*/
maxObservedAddresses?: number
}

export interface AddressManagerComponents {
Expand Down Expand Up @@ -103,6 +112,7 @@ export class AddressManager implements AddressManagerInterface {
private readonly announceFilter: AddressFilter
private readonly ipDomainMappings: Map<string, DNSMapping>
private readonly publicAddressMappings: Map<string, PublicAddressMapping[]>
private readonly maxObservedAddresses: number

/**
* Responsible for managing the peer addresses.
Expand All @@ -122,6 +132,7 @@ export class AddressManager implements AddressManagerInterface {
this.ipDomainMappings = new Map()
this.publicAddressMappings = new Map()
this.announceFilter = init.announceFilter ?? defaultAddressFilter
this.maxObservedAddresses = init.maxObservedAddresses ?? defaultValues.maxObservedAddresses

// this method gets called repeatedly on startup when transports start listening so
// debounce it so we don't cause multiple self:peer:update events to be emitted
Expand Down Expand Up @@ -192,6 +203,10 @@ export class AddressManager implements AddressManagerInterface {
* Add peer observed addresses
*/
addObservedAddr (addr: Multiaddr): void {
if (this.observed.size === this.maxObservedAddresses) {
return
}

addr = stripPeerId(addr, this.components.peerId)
const addrString = addr.toString()

Expand Down
20 changes: 20 additions & 0 deletions packages/libp2p/test/addresses/address-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,26 @@ describe('Address Manager', () => {
expect(am.getObservedAddrs()).to.have.lengthOf(1)
})

it('should limit observed addresses', () => {
const am = new AddressManager({
peerId,
transportManager: stubInterface<TransportManager>(),
peerStore,
events,
logger: defaultLogger()
}, {
announceFilter: stubInterface<AddressFilter>()
})

expect(am.getObservedAddrs()).to.be.empty()

for (let i = 0; i < 100; i++) {
am.addObservedAddr(multiaddr(`/ip4/123.123.123.123/tcp/392${i}`))
}

expect(am.getObservedAddrs()).to.have.lengthOf(10)
})

it('should allow duplicate listen addresses', () => {
const ma = multiaddr('/ip4/0.0.0.0/tcp/0')
const am = new AddressManager({
Expand Down
Loading