Skip to content

feat(utils): add multiaddr sort comparators - #3488

Merged
tabcat merged 6 commits into
libp2p:mainfrom
tabcat:feat/address-sorter-utils
May 7, 2026
Merged

feat(utils): add multiaddr sort comparators#3488
tabcat merged 6 commits into
libp2p:mainfrom
tabcat:feat/address-sorter-utils

Conversation

@tabcat

@tabcat tabcat commented May 5, 2026

Copy link
Copy Markdown
Member

Description

Move the four multiaddr-only sort comparators (reliableTransportsFirst, loopbackAddressLast, publicAddressesFirst, circuitRelayAddressesLast) from packages/libp2p/src/connection-manager/address-sorter.ts into @libp2p/utils, retyped to take Multiaddr directly. Also adds a defaultMultiaddrSorter(multiaddrs: Multiaddr[]) convenience that chains the four in the standard priority order (loopback last, public first, relay last, reliable transports as innermost tiebreaker).

The Address-aware composition (certifiedAddressesFirst, defaultAddressSorter) stays in libp2p and now feeds the utils comparators through a small asMultiaddr adapter. Sort order is preserved verbatim — same five-key priority chain in defaultAddressSorter. dial-queue.ts import path is unchanged. The AddressSorter interface in @libp2p/interface is unchanged.

@libp2p/utils gains a new dependency on @multiformats/multiaddr-matcher.

This unblocks #3423, which puts the addresses we send in identify messages in priority order.

Notes & open questions

  • I couldn't think of a solution that would keep the logic simple and allow defaultAddressSorter to consume defaultMultiaddrSorter. certifiedAddressesFirst needs to slot at priority 4 inside the chain, which doesn't fit cleanly into a no-options defaultMultiaddrSorter.

Change checklist

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation if necessary (this includes comments as well)
  • I have added tests that prove my fix is effective or that my feature works

tabcat added 6 commits May 5, 2026 18:07
Required for the multiaddr-only sort comparators being relocated from
libp2p to @libp2p/utils.
Move the four multiaddr-only sort comparators from libp2p
into @libp2p/utils, retyped to take Multiaddr directly. The
Address-aware composition stays in libp2p (next commit).
Drops the inline implementations of the four multiaddr-only
comparators. The Address-aware composition (defaultAddressSorter,
certifiedAddressesFirst) stays here, feeding utils comparators
through a small asMultiaddr adapter. Sort order is preserved.
One focused test per comparator validating direction and
no-op-on-tie behavior, with bare Multiaddr inputs.
Convenience wrapper that chains the four multiaddr-only comparators
in the standard priority order. Lets bare-Multiaddr callers sort
without composing their own chain.
Comment on lines +6 to +10
/**
* Sorts addresses by order of reliability, where they have presented the fewest
* problems:
*
* TCP -> WebSockets/Secure -> WebRTC -> WebRTCDirect -> WebTransport

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wondering if this needs to be updated.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

think this is still the best order

@tabcat
tabcat marked this pull request as ready for review May 5, 2026 12:06
@tabcat
tabcat requested a review from a team as a code owner May 5, 2026 12:06
Comment on lines +141 to +145
return multiaddrs.sort((a, b) =>
loopbackAddressLast(a, b) ||
publicAddressesFirst(a, b) ||
circuitRelayAddressesLast(a, b) ||
reliableTransportsFirst(a, b)

@tabcat tabcat May 5, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

equivalent to .sort chain (although primary key order changes). could make this change to defaultAddressSorter.

@tabcat
tabcat merged commit 7a35523 into libp2p:main May 7, 2026
47 of 48 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants