Skip to content

refactor(libp2p): drop asMultiaddr, use composite sort#3492

Merged
tabcat merged 8 commits into
libp2p:mainfrom
tabcat:refactor/address-sorter-composite
May 7, 2026
Merged

refactor(libp2p): drop asMultiaddr, use composite sort#3492
tabcat merged 8 commits into
libp2p:mainfrom
tabcat:refactor/address-sorter-composite

Conversation

@tabcat
Copy link
Copy Markdown
Member

@tabcat tabcat commented May 7, 2026

Description

Replaces the five chained .sort() calls in defaultAddressSorter with a single .sort() using a ||-chained composite comparator. Drops the asMultiaddr helper.

Array.prototype.sort is stable, so a chain of stable sorts and a single lexicographic composite sort produce the same order for the same key list. Priority is preserved verbatim — loopback (primary) > public > circuit > certified > reliable (innermost tiebreaker).

Notes & open questions

Followup to #3488.

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 8 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.
defaultAddressSorter now uses a single .sort() with a ||-chained
composite comparator instead of five chained .sort() calls. Stable
sort makes the two forms equivalent; the priority order
(loopback > public > circuit > certified > reliable) is preserved.
@tabcat tabcat marked this pull request as ready for review May 7, 2026 08:51
@tabcat tabcat requested a review from a team as a code owner May 7, 2026 08:51
@tabcat tabcat merged commit 81e8dcc into libp2p:main May 7, 2026
48 of 49 checks passed
@tabcat tabcat mentioned this pull request May 9, 2026
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.

1 participant