-
Notifications
You must be signed in to change notification settings - Fork 541
feat(utils): add multiaddr sort comparators #3488
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e537433
deps(utils): add @multiformats/multiaddr-matcher
tabcat 5fa1bc0
feat(utils): add multiaddr sort comparators
tabcat 71a4b3d
refactor(libp2p): use multiaddr sorters from @libp2p/utils
tabcat 31a0b4b
test(utils): unit tests for multiaddr sort comparators
tabcat c83d22b
Merge branch 'main' into feat/address-sorter-utils
tabcat 423752c
feat(utils): add defaultMultiaddrSorter
tabcat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| import { Circuit, WebSockets, WebSocketsSecure, WebRTC, WebRTCDirect, WebTransport, TCP } from '@multiformats/multiaddr-matcher' | ||
| import { isLoopback } from './is-loopback.ts' | ||
| import { isPrivate } from './is-private.ts' | ||
| import type { Multiaddr } from '@multiformats/multiaddr' | ||
|
|
||
| /** | ||
| * Sorts addresses by order of reliability, where they have presented the fewest | ||
| * problems: | ||
| * | ||
| * TCP -> WebSockets/Secure -> WebRTC -> WebRTCDirect -> WebTransport | ||
| */ | ||
| // eslint-disable-next-line complexity | ||
| export function reliableTransportsFirst (a: Multiaddr, b: Multiaddr): -1 | 0 | 1 { | ||
| const isATcp = TCP.exactMatch(a) | ||
| const isBTcp = TCP.exactMatch(b) | ||
|
|
||
| if (isATcp && !isBTcp) { | ||
| return -1 | ||
| } | ||
|
|
||
| if (!isATcp && isBTcp) { | ||
| return 1 | ||
| } | ||
|
|
||
| const isAWebSocketSecure = WebSocketsSecure.exactMatch(a) | ||
| const isBWebSocketSecure = WebSocketsSecure.exactMatch(b) | ||
|
|
||
| if (isAWebSocketSecure && !isBWebSocketSecure) { | ||
| return -1 | ||
| } | ||
|
|
||
| if (!isAWebSocketSecure && isBWebSocketSecure) { | ||
| return 1 | ||
| } | ||
|
|
||
| const isAWebSocket = WebSockets.exactMatch(a) | ||
| const isBWebSocket = WebSockets.exactMatch(b) | ||
|
|
||
| if (isAWebSocket && !isBWebSocket) { | ||
| return -1 | ||
| } | ||
|
|
||
| if (!isAWebSocket && isBWebSocket) { | ||
| return 1 | ||
| } | ||
|
|
||
| const isAWebRTC = WebRTC.exactMatch(a) | ||
| const isBWebRTC = WebRTC.exactMatch(b) | ||
|
|
||
| if (isAWebRTC && !isBWebRTC) { | ||
| return -1 | ||
| } | ||
|
|
||
| if (!isAWebRTC && isBWebRTC) { | ||
| return 1 | ||
| } | ||
|
|
||
| const isAWebRTCDirect = WebRTCDirect.exactMatch(a) | ||
| const isBWebRTCDirect = WebRTCDirect.exactMatch(b) | ||
|
|
||
| if (isAWebRTCDirect && !isBWebRTCDirect) { | ||
| return -1 | ||
| } | ||
|
|
||
| if (!isAWebRTCDirect && isBWebRTCDirect) { | ||
| return 1 | ||
| } | ||
|
|
||
| const isAWebTransport = WebTransport.exactMatch(a) | ||
| const isBWebTransport = WebTransport.exactMatch(b) | ||
|
|
||
| if (isAWebTransport && !isBWebTransport) { | ||
| return -1 | ||
| } | ||
|
|
||
| if (!isAWebTransport && isBWebTransport) { | ||
| return 1 | ||
| } | ||
|
|
||
| // ... everything else | ||
| return 0 | ||
| } | ||
|
|
||
| /** | ||
| * Compare function for array.sort() that moves loopback addresses to the end | ||
| * of the array. | ||
| */ | ||
| export function loopbackAddressLast (a: Multiaddr, b: Multiaddr): -1 | 0 | 1 { | ||
| const isALoopback = isLoopback(a) | ||
| const isBLoopback = isLoopback(b) | ||
|
|
||
| if (isALoopback && !isBLoopback) { | ||
| return 1 | ||
| } else if (!isALoopback && isBLoopback) { | ||
| return -1 | ||
| } | ||
|
|
||
| return 0 | ||
| } | ||
|
|
||
| /** | ||
| * Compare function for array.sort() that moves public addresses to the start | ||
| * of the array. | ||
| */ | ||
| export function publicAddressesFirst (a: Multiaddr, b: Multiaddr): -1 | 0 | 1 { | ||
| const isAPrivate = isPrivate(a) | ||
| const isBPrivate = isPrivate(b) | ||
|
|
||
| if (isAPrivate && !isBPrivate) { | ||
| return 1 | ||
| } else if (!isAPrivate && isBPrivate) { | ||
| return -1 | ||
| } | ||
|
|
||
| return 0 | ||
| } | ||
|
|
||
| /** | ||
| * Compare function for array.sort() that moves circuit relay addresses to the | ||
| * end of the array. | ||
| */ | ||
| export function circuitRelayAddressesLast (a: Multiaddr, b: Multiaddr): -1 | 0 | 1 { | ||
| const isACircuit = Circuit.exactMatch(a) | ||
| const isBCircuit = Circuit.exactMatch(b) | ||
|
|
||
| if (isACircuit && !isBCircuit) { | ||
| return 1 | ||
| } else if (!isACircuit && isBCircuit) { | ||
| return -1 | ||
| } | ||
|
|
||
| return 0 | ||
| } | ||
|
|
||
| /** | ||
| * Sort multiaddrs by the default multiaddr-only ordering: loopback addresses | ||
| * last, public addresses first, circuit-relay addresses last, with reliable | ||
| * transports as the innermost tiebreaker. | ||
| */ | ||
| export function defaultMultiaddrSorter (multiaddrs: Multiaddr[]): Multiaddr[] { | ||
| return multiaddrs.sort((a, b) => | ||
| loopbackAddressLast(a, b) || | ||
| publicAddressesFirst(a, b) || | ||
| circuitRelayAddressesLast(a, b) || | ||
| reliableTransportsFirst(a, b) | ||
|
Comment on lines
+141
to
+145
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| ) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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