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: pass addr sorting to connection manager to always dial in a predeterministic way #351

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions packages/network/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"@libp2p/websockets": "^9.1.1",
"@libp2p/webtransport": "^5.0.9",
"@multiformats/multiaddr": "^12.3.1",
"@multiformats/multiaddr-matcher": "^1.6.0",
"@ts-drp/logger": "^0.5.2",
"it-length-prefixed": "^9.1.0",
"it-map": "^3.1.1",
Expand Down
42 changes: 22 additions & 20 deletions packages/network/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { webSockets } from "@libp2p/websockets";
import * as filters from "@libp2p/websockets/filters";
import { webTransport } from "@libp2p/webtransport";
import { multiaddr } from "@multiformats/multiaddr";
import { WebRTC } from "@multiformats/multiaddr-matcher";
import { Logger, type LoggerOptions } from "@ts-drp/logger";
import { type Libp2p, createLibp2p } from "libp2p";
import { fromString as uint8ArrayFromString } from "uint8arrays/from-string";
Expand Down Expand Up @@ -136,6 +137,21 @@ export class DRPNetworkNode {
return false;
},
},
connectionManager: {
addressSorter: (a, b) => {
const localRegex =
/(^\/ip4\/127\.)|(^\/ip4\/10\.)|(^\/ip4\/172\.1[6-9]\.)|(^\/ip4\/172\.2[0-9]\.)|(^\/ip4\/172\.3[0-1]\.)|(^\/ip4\/192\.168\.)/;
const aLocal = localRegex.test(a.toString());
const bLocal = localRegex.test(b.toString());
const aWebrtc = WebRTC.matches(a.multiaddr);
const bWebrtc = WebRTC.matches(b.multiaddr);
if (aLocal && !bLocal) return 1;
if (!aLocal && bLocal) return -1;
if (aWebrtc && !bWebrtc) return -1;
if (!aWebrtc && bWebrtc) return 1;
return 0;
},
},
metrics: this._config?.browser_metrics ? devToolsMetrics() : undefined,
peerDiscovery: _peerDiscovery,
services: this._config?.bootstrap ? _bootstrap_services : _node_services,
Expand Down Expand Up @@ -175,26 +191,12 @@ export class DRPNetworkNode {
);
this._node.addEventListener("peer:discovery", async (e) => {
// current bug in v11.0.0 requires manual dial (https://github.com/libp2p/js-libp2p-pubsub-peer-discovery/issues/149)
const sortedAddrs = e.detail.multiaddrs.sort((a, b) => {
const localRegex =
/(^\/ip4\/127\.)|(^\/ip4\/10\.)|(^\/ip4\/172\.1[6-9]\.)|(^\/ip4\/172\.2[0-9]\.)|(^\/ip4\/172\.3[0-1]\.)|(^\/ip4\/192\.168\.)/;
const aLocal = localRegex.test(a.toString());
const bLocal = localRegex.test(b.toString());
const aWebrtc = a.toString().includes("/webrtc/");
const bWebrtc = b.toString().includes("/webrtc/");
if (aLocal && !bLocal) return 1;
if (!aLocal && bLocal) return -1;
if (aWebrtc && !bWebrtc) return -1;
if (!aWebrtc && bWebrtc) return 1;
return 0;
});

// Dial non-local multiaddrs, then WebRTC multiaddrs
for (const address of sortedAddrs) {
try {
await this._node?.dial(address);
} catch (e) {
log.error("::start::peer::dial::error", e);
if (e.detail.multiaddrs.length > 0) {
const alreadyConnected = this._node
?.getPeers()
.some((p) => p === e.detail.id);
if (!alreadyConnected) {
await this._node?.dial(e.detail.multiaddrs);
}
}

Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading