@serfab/cadre-core 0.11.0 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. package/dist/announce-addrs.d.ts +49 -0
  2. package/dist/announce-addrs.js +78 -0
  3. package/dist/announce-addrs.js.map +1 -0
  4. package/dist/cadre-node.d.ts +281 -30
  5. package/dist/cadre-node.js +513 -103
  6. package/dist/cadre-node.js.map +1 -1
  7. package/dist/control-database.d.ts +155 -23
  8. package/dist/control-database.js +272 -104
  9. package/dist/control-database.js.map +1 -1
  10. package/dist/control-read-retry.d.ts +103 -0
  11. package/dist/control-read-retry.js +145 -0
  12. package/dist/control-read-retry.js.map +1 -0
  13. package/dist/control-retry.d.ts +101 -0
  14. package/dist/control-retry.js +108 -0
  15. package/dist/control-retry.js.map +1 -0
  16. package/dist/control-schema.d.ts +1 -1
  17. package/dist/control-schema.js +20 -4
  18. package/dist/control-schema.js.map +1 -1
  19. package/dist/control-write-retry.d.ts +59 -35
  20. package/dist/control-write-retry.js +21 -92
  21. package/dist/control-write-retry.js.map +1 -1
  22. package/dist/delegate-admission.d.ts +17 -15
  23. package/dist/delegate-admission.js +17 -15
  24. package/dist/delegate-admission.js.map +1 -1
  25. package/dist/index.d.ts +6 -4
  26. package/dist/index.js +17 -8
  27. package/dist/index.js.map +1 -1
  28. package/dist/membership-connection-gater.d.ts +211 -25
  29. package/dist/membership-connection-gater.js +309 -29
  30. package/dist/membership-connection-gater.js.map +1 -1
  31. package/dist/peer-record.d.ts +26 -0
  32. package/dist/peer-record.js +32 -0
  33. package/dist/peer-record.js.map +1 -1
  34. package/dist/relay-addrs.d.ts +76 -26
  35. package/dist/relay-addrs.js +115 -27
  36. package/dist/relay-addrs.js.map +1 -1
  37. package/dist/relay-reservation.d.ts +20 -15
  38. package/dist/relay-reservation.js +18 -14
  39. package/dist/relay-reservation.js.map +1 -1
  40. package/dist/strand-addr-protocol.js +16 -0
  41. package/dist/strand-addr-protocol.js.map +1 -1
  42. package/dist/strand-backfill.js +13 -4
  43. package/dist/strand-backfill.js.map +1 -1
  44. package/dist/strand-formation-manager.d.ts +10 -9
  45. package/dist/strand-formation-manager.js +10 -9
  46. package/dist/strand-formation-manager.js.map +1 -1
  47. package/dist/strand-formation-protocol.d.ts +7 -7
  48. package/dist/strand-formation-protocol.js +8 -8
  49. package/dist/strand-formation-protocol.js.map +1 -1
  50. package/dist/strand-instance-manager.d.ts +4 -3
  51. package/dist/strand-instance-manager.js +31 -6
  52. package/dist/strand-instance-manager.js.map +1 -1
  53. package/dist/strand-membership-writer.d.ts +106 -14
  54. package/dist/strand-membership-writer.js +180 -14
  55. package/dist/strand-membership-writer.js.map +1 -1
  56. package/dist/strand-solicitation.d.ts +3 -3
  57. package/dist/strand-solicitation.js +4 -5
  58. package/dist/strand-solicitation.js.map +1 -1
  59. package/dist/strand-wake-protocol.d.ts +47 -2
  60. package/dist/strand-wake-protocol.js +76 -6
  61. package/dist/strand-wake-protocol.js.map +1 -1
  62. package/dist/types.d.ts +74 -29
  63. package/dist/types.js.map +1 -1
  64. package/package.json +8 -8
  65. package/src/announce-addrs.ts +93 -0
  66. package/src/cadre-node.ts +5603 -5158
  67. package/src/control-database.ts +2584 -2406
  68. package/src/control-read-retry.ts +167 -0
  69. package/src/control-retry.ts +190 -0
  70. package/src/control-schema.ts +20 -4
  71. package/src/control-write-retry.ts +30 -130
  72. package/src/delegate-admission.ts +17 -15
  73. package/src/index.ts +435 -407
  74. package/src/membership-connection-gater.ts +533 -204
  75. package/src/peer-record.ts +33 -0
  76. package/src/relay-addrs.ts +218 -111
  77. package/src/relay-reservation.ts +828 -823
  78. package/src/strand-addr-protocol.ts +17 -0
  79. package/src/strand-backfill.ts +389 -366
  80. package/src/strand-formation-manager.ts +492 -491
  81. package/src/strand-formation-protocol.ts +8 -8
  82. package/src/strand-instance-manager.ts +636 -611
  83. package/src/strand-membership-writer.ts +209 -14
  84. package/src/strand-solicitation.ts +6 -7
  85. package/src/strand-wake-protocol.ts +94 -8
  86. package/src/types.ts +74 -29
@@ -135,6 +135,39 @@ export function trailingPeerId(addr: Multiaddr): string | null {
135
135
  return p2p.length > 0 ? (p2p[p2p.length - 1].value ?? null) : null;
136
136
  }
137
137
 
138
+ /**
139
+ * `addr` guaranteed to terminate in `/p2p/<peerId>` — the shape every dial path
140
+ * needs — or `null` when it cannot, because the address already terminates in a
141
+ * DIFFERENT peer id and therefore does not reach `peerId` at all.
142
+ *
143
+ * Three input shapes, matching what a `CadrePeer` record can legitimately carry:
144
+ *
145
+ * - no `/p2p/` component (`/ip4/1.2.3.4/tcp/4001`) — the suffix is appended;
146
+ * - a relay hop with the destination missing (`…/p2p/<relay>/p2p-circuit`) — the
147
+ * last component is the circuit, not a peer id, so the suffix is appended and
148
+ * the address becomes the full `…/p2p/<relay>/p2p-circuit/p2p/<peerId>`;
149
+ * - already terminating in `/p2p/<X>` — returned untouched when `X` is `peerId`,
150
+ * dropped otherwise.
151
+ *
152
+ * This is deliberately the same rule libp2p applies internally in
153
+ * `calculateMultiaddrs` (append when the LAST component is not `p2p`) followed by
154
+ * its wrong-peer-id filter, so a normalized list matches what libp2p would have
155
+ * built anyway. Normalizing before the dial is what keeps a list from mixing
156
+ * suffixed and unsuffixed entries, which `getPeerAddress` rejects outright
157
+ * ("Multiaddrs must all have the same peer id or have no peer id") — taking the
158
+ * whole peer down with it, not just the odd address.
159
+ *
160
+ * Never use before signature verification: verification runs against the
161
+ * original on-record strings.
162
+ */
163
+ export function withTrailingPeerId(addr: Multiaddr, peerId: string): Multiaddr | null {
164
+ const components = addr.getComponents();
165
+ if (components[components.length - 1]?.name !== 'p2p') {
166
+ return addr.encapsulate(`/p2p/${peerId}`);
167
+ }
168
+ return trailingPeerId(addr) === peerId ? addr : null;
169
+ }
170
+
138
171
  /**
139
172
  * Return `addrs` with signaling (`/p2p-circuit`) addrs first, otherwise stable.
140
173
  * Used to present the WebRTC dial input ahead of direct addrs. Does NOT mutate
@@ -1,111 +1,218 @@
1
- /**
2
- * Translate `network.relayAddrs` — the operator-facing "use these relay servers"
3
- * setting — into the mechanism this repo already has for reserving a circuit-relay
4
- * slot: a `…/p2p/<relayPeerId>/p2p-circuit` entry in the node's listen addrs.
5
- *
6
- * `@libp2p/circuit-relay-v2`'s transport is always present on the Node path
7
- * (`../optimystic/packages/db-p2p/src/libp2p-node.ts` pushes `circuitRelayTransport()`
8
- * unconditionally), and listening on a relay's circuit addr is what makes libp2p dial
9
- * that relay and hold a reservation. Everything downstream of the listen list —
10
- * the reservation itself, `CadreNode.circuitRelayTargets()` picking delegate-announce
11
- * targets, strand nodes inheriting the listen addrs, the `/p2p-circuit`-first address
12
- * publishing in `registerSelf` — then works unchanged.
13
- *
14
- * So `relayAddrs` is a second, friendlier door onto an existing mechanism:
15
- *
16
- * effective listen addrs = listenAddrs ++ relayAddrs.map(a => a + '/p2p-circuit')
17
- *
18
- * NOTE: this is the FAIL-FAST route to a reservation — libp2p's transport manager
19
- * throws when a configured circuit listener cannot listen, so naming a relay that
20
- * is down means the node does not start. The fail-soft route is the bare
21
- * `/p2p-circuit` search listener driven by `relay-reservation.ts`. The two are
22
- * ALTERNATIVES, not layers: configuring `relayAddrs` on a node that already
23
- * reserves via the search listener adds the fatal listener back and the node
24
- * stops booting when its relay is down.
25
- */
26
-
27
- import { multiaddr, type Component } from '@multiformats/multiaddr';
28
- import { circuitRelayTargetOrThrow } from './delegate-admission.js';
29
- import type { NetworkConfig } from './types.js';
30
-
31
- /**
32
- * The direct listener a node keeps when it configures `relayAddrs` but no
33
- * `listenAddrs`. Without this, naming a relay would silently REPLACE the node's
34
- * direct TCP listener with a circuit-only one.
35
- *
36
- * NOTE: mirrors `createLibp2pNode`'s own default listen addr in
37
- * `../optimystic/packages/db-p2p/src/libp2p-node.ts` (`/ip4/0.0.0.0/tcp/<port>`,
38
- * and `CadreNode` passes `port: 0`) — that file is the source of truth. If it
39
- * ever changes its default, this constant has to follow.
40
- */
41
- const DEFAULT_DIRECT_LISTEN_ADDR = '/ip4/0.0.0.0/tcp/0';
42
-
43
- /**
44
- * The circuit-listen multiaddr for each configured relay: `<relayAddr>/p2p-circuit`.
45
- * An entry that already carries `/p2p-circuit` is passed through unchanged.
46
- * Throws on an entry that is unparsable or names no relay peerId — this is
47
- * operator configuration, so a typo must fail loudly at start rather than
48
- * silently costing the node its reachability.
49
- *
50
- * Deliberately unlike `extractCircuitRelayTargets` (`delegate-admission.ts`), which
51
- * SKIPS a bad entry: that one reads addresses discovered at runtime from peers, where
52
- * one bad entry must not be fatal.
53
- */
54
- export function relayCircuitAddrs(relayAddrs: readonly string[]): string[] {
55
- return dedupe(relayAddrs.map(circuitListenAddr));
56
- }
57
-
58
- /**
59
- * The listen multiaddrs a node actually binds: configured `listenAddrs` plus the
60
- * circuit-listen addr of every configured relay, deduplicated, order-stable
61
- * (configured entries first). Returns `undefined` when neither field is set, so
62
- * callers keep omitting `listenAddrs` and inherit db-p2p's default.
63
- */
64
- export function resolveListenAddrs(network: NetworkConfig | undefined): string[] | undefined {
65
- const circuits = relayCircuitAddrs(network?.relayAddrs ?? []);
66
- const listenAddrs = network?.listenAddrs;
67
- if (!listenAddrs && circuits.length === 0) {
68
- return undefined;
69
- }
70
- // An explicitly empty `listenAddrs` (the React Native "cannot listen" case) stays
71
- // empty — but a relay named alongside it still gets its circuit listener, since
72
- // acquiring one is the whole point of configuring a relay.
73
- return dedupe([...(listenAddrs ?? [DEFAULT_DIRECT_LISTEN_ADDR]), ...circuits]);
74
- }
75
-
76
- /**
77
- * One entry of {@link relayCircuitAddrs}: the addr the node listens on for this relay.
78
- *
79
- * NOTE: an entry carrying components AFTER `/p2p-circuit` — a full relayed DIAL addr,
80
- * `…/p2p/<relay>/p2p-circuit/p2p/<someone else>` — is passed through as written and
81
- * would be listened on verbatim. It validates (the relay it names is real), so only an
82
- * operator who pastes a peer's relayed address into `relayAddrs` hits it; if that turns
83
- * out to be a common paste, truncate at the `/p2p-circuit` component here.
84
- */
85
- function circuitListenAddr(relayAddr: string): string {
86
- const alreadyCircuit = parseComponents(relayAddr).some((c) => c.name === 'p2p-circuit');
87
- const listenAddr = alreadyCircuit ? relayAddr : `${relayAddr}/p2p-circuit`;
88
- try {
89
- circuitRelayTargetOrThrow(listenAddr); // validates the relay peerId
90
- } catch (err) {
91
- throw new Error(
92
- `network.relayAddrs entry names no relay peerId (expected <dial addr>/p2p/<relayPeerId>): ${relayAddr}`,
93
- { cause: err }
94
- );
95
- }
96
- return listenAddr;
97
- }
98
-
99
- /** Multiaddr components of `relayAddr`, with the config field named on a parse failure. */
100
- function parseComponents(relayAddr: string): Component[] {
101
- try {
102
- return multiaddr(relayAddr).getComponents();
103
- } catch (err) {
104
- throw new Error(`network.relayAddrs entry is not a valid multiaddr: ${relayAddr}`, { cause: err });
105
- }
106
- }
107
-
108
- /** Exact-string dedupe, first occurrence wins, so the list is stable across restarts. */
109
- function dedupe(addrs: readonly string[]): string[] {
110
- return [...new Set(addrs)];
111
- }
1
+ /**
2
+ * Translate `network.relayAddrs` — the operator-facing "use these relay servers"
3
+ * setting — into the listen entry a node needs before it can hold a circuit-relay
4
+ * slot, and validate the operator's entries while doing it.
5
+ *
6
+ * `@libp2p/circuit-relay-v2`'s listener branches on the SHAPE of the listen addr,
7
+ * and the same `relayAddrs` list resolves to either shape depending on WHO is
8
+ * listening ({@link RelayListenRoute}):
9
+ *
10
+ * | route | listen entry | libp2p behaviour |
11
+ * | -------------- | ------------------------------------------- | --------------------------------------------------------- |
12
+ * | `'configured'` | `<relayAddr>/p2p-circuit`, one per relay | dial that relay from inside `listen()` and reserve, or throw |
13
+ * | `'search'` | a single bare `/p2p-circuit` | register a pending reservation; open no connection |
14
+ *
15
+ * The CONTROL node takes the `'search'` route, and `CadreNode.start()` drives the
16
+ * reservation explicitly (`relay-reservation.ts`) once the control database is up.
17
+ * That ordering is the point: a configured circuit listener dials the relay from
18
+ * inside `libp2p.start()`, so the control database was being built while a sibling
19
+ * relay was already in this node's cohort — and a sibling that has not yet
20
+ * replicated this node's `CadrePeer` row correctly refuses its control-DB streams,
21
+ * which killed `start()` outright. See `cadre-node.ts` → `start()`.
22
+ *
23
+ * STRAND nodes keep the `'configured'` route (`strand-instance-manager.ts`): they
24
+ * inherit the control node's `NetworkConfig`, nothing drives an explicit
25
+ * reservation for them, and their relay connection cannot disturb a control-DB
26
+ * bring-up (a strand node's protocol ids are namespaced `/optimystic/strand-<id>/…`,
27
+ * so the relay is never in its cohort).
28
+ *
29
+ * `network.relayAddrs` is FAIL-FAST for the operator on both routes, just from
30
+ * different places: a malformed entry throws here at config resolution, and on the
31
+ * control node a first reservation attempt that does not land throws out of
32
+ * `start()` (`RelayReservationFailedError`). Naming a relay that is down still
33
+ * means the node does not come up.
34
+ *
35
+ * The bare `/p2p-circuit` search listener cannot open a connection on its own:
36
+ * libp2p fills a pending reservation from `RelayDiscovery`, which nominates a peer
37
+ * only once the relay-hop protocol id is in that peer's peer-store protocol list,
38
+ * and that list is written exclusively by IDENTIFY — which `@optimystic/db-p2p`
39
+ * namespaces per network, so a cadre node and a stock relay never identify each
40
+ * other. See `relay-reservation.ts`'s module doc and `docs/architecture.md`.
41
+ */
42
+
43
+ import { multiaddr, type Component } from '@multiformats/multiaddr';
44
+ import { circuitRelayTargetOrThrow } from './delegate-admission.js';
45
+ import type { NetworkConfig } from './types.js';
46
+ import type { RelayReservationState } from './relay-reservation.js';
47
+
48
+ /**
49
+ * The direct listener a node keeps when it configures `relayAddrs` but no
50
+ * `listenAddrs`. Without this, naming a relay would silently REPLACE the node's
51
+ * direct TCP listener with a circuit-only one.
52
+ *
53
+ * NOTE: mirrors `createLibp2pNode`'s own default listen addr in
54
+ * `../optimystic/packages/db-p2p/src/libp2p-node.ts` (`/ip4/0.0.0.0/tcp/<port>`,
55
+ * and `CadreNode` passes `port: 0`) — that file is the source of truth. If it
56
+ * ever changes its default, this constant has to follow.
57
+ */
58
+ const DEFAULT_DIRECT_LISTEN_ADDR = '/ip4/0.0.0.0/tcp/0';
59
+
60
+ /**
61
+ * libp2p's relay SEARCH listener: registers one pending reservation and dials
62
+ * nothing. One entry covers every configured relay — the pending reservation is
63
+ * per-listener, not per-relay, and `relay-reservation.ts` fills it with whichever
64
+ * relay answers first.
65
+ */
66
+ export const RELAY_SEARCH_LISTEN_ADDR = '/p2p-circuit';
67
+
68
+ /**
69
+ * Which listener shape `network.relayAddrs` resolves to for this caller — see the
70
+ * table in the module doc. Defaults to `'configured'` everywhere except the
71
+ * control node's own libp2p options.
72
+ */
73
+ export type RelayListenRoute = 'configured' | 'search';
74
+
75
+ /**
76
+ * The circuit-listen multiaddr for each configured relay: `<relayAddr>/p2p-circuit`.
77
+ * An entry that already carries `/p2p-circuit` is passed through unchanged.
78
+ * Throws on an entry that is unparsable or names no relay peerId — this is
79
+ * operator configuration, so a typo must fail loudly at start rather than
80
+ * silently costing the node its reachability.
81
+ *
82
+ * Deliberately unlike `extractCircuitRelayTargets` (`delegate-admission.ts`), which
83
+ * SKIPS a bad entry: that one reads addresses discovered at runtime from peers, where
84
+ * one bad entry must not be fatal.
85
+ */
86
+ export function relayCircuitAddrs(relayAddrs: readonly string[]): string[] {
87
+ return dedupe(relayAddrs.map(circuitListenAddr));
88
+ }
89
+
90
+ /**
91
+ * The listen multiaddrs a node actually binds: configured `listenAddrs` plus the
92
+ * relay listen entry `route` calls for, deduplicated, order-stable (configured
93
+ * entries first). Returns `undefined` when neither field is set, so callers keep
94
+ * omitting `listenAddrs` and inherit db-p2p's default.
95
+ *
96
+ * Every `relayAddrs` entry is validated on BOTH routes — the `'search'` route
97
+ * discards the resolved circuit addrs, but an operator typo must fail at config
98
+ * resolution either way.
99
+ */
100
+ export function resolveListenAddrs(
101
+ network: NetworkConfig | undefined,
102
+ route: RelayListenRoute = 'configured'
103
+ ): string[] | undefined {
104
+ const configured = relayCircuitAddrs(network?.relayAddrs ?? []);
105
+ const relayEntries = route === 'search'
106
+ ? (configured.length > 0 ? [RELAY_SEARCH_LISTEN_ADDR] : [])
107
+ : configured;
108
+ const listenAddrs = network?.listenAddrs;
109
+ if (route === 'search') {
110
+ rejectConfiguredCircuitListenAddrs(listenAddrs ?? []);
111
+ }
112
+ if (!listenAddrs && relayEntries.length === 0) {
113
+ return undefined;
114
+ }
115
+ // An explicitly empty `listenAddrs` (the React Native "cannot listen" case) stays
116
+ // empty — but a relay named alongside it still gets its listen entry, since
117
+ // acquiring a slot is the whole point of configuring a relay.
118
+ return dedupe([...(listenAddrs ?? [DEFAULT_DIRECT_LISTEN_ADDR]), ...relayEntries]);
119
+ }
120
+
121
+ /**
122
+ * Thrown out of `CadreNode.start()` when the boot-path reservation drive for
123
+ * `network.relayAddrs` produces no `/p2p-circuit` address on its FIRST attempt.
124
+ *
125
+ * This is what keeps `network.relayAddrs` fail-fast now that the control node
126
+ * takes the `'search'` route: libp2p's own `UnsupportedListenAddressesError` used
127
+ * to abort start from inside `listen()`, and an operator who names a relay is
128
+ * telling the node it has no other reachability — coming up undialable is worse
129
+ * than not coming up. Names the relays and the reservation's own reason, both of
130
+ * which the libp2p error omitted.
131
+ */
132
+ export class RelayReservationFailedError extends Error {
133
+ constructor(
134
+ readonly relayAddrs: readonly string[],
135
+ readonly state: RelayReservationState
136
+ ) {
137
+ super(
138
+ `network.relayAddrs reservation failed (status: ${state.status}): ` +
139
+ `${state.error ?? 'no /p2p-circuit address appeared'} — relays: ${relayAddrs.join(', ')}`
140
+ );
141
+ this.name = 'RelayReservationFailedError';
142
+ }
143
+ }
144
+
145
+ /**
146
+ * A hand-written `<relay>/p2p-circuit` entry in `network.listenAddrs` is the
147
+ * CONFIGURED listener shape, and it is unusable on the `'search'` route: libp2p
148
+ * dials that relay from inside `listen()`, the bring-up quiet period
149
+ * (`membership-connection-gater.ts`) denies exactly that dial, `listen()` fails, and
150
+ * the transport manager's default `FATAL_ALL` aborts `libp2p.start()`. The operator
151
+ * would see `UnsupportedListenAddressesError` from deep inside bring-up with nothing
152
+ * naming the cause, so name it here instead.
153
+ *
154
+ * `network.relayAddrs` is the exact replacement — same relay, and the reservation is
155
+ * driven after bring-up, which is the whole point of the route.
156
+ */
157
+ function rejectConfiguredCircuitListenAddrs(listenAddrs: readonly string[]): void {
158
+ const configured = listenAddrs.filter(isConfiguredCircuitListenAddr);
159
+ if (configured.length > 0) {
160
+ throw new Error(
161
+ 'network.listenAddrs names a relay directly ' +
162
+ `(${configured.join(', ')}), which a control node cannot listen on: libp2p would ` +
163
+ 'dial that relay during control-database bring-up, when the node accepts no ' +
164
+ 'connections. Move the relay to network.relayAddrs, which reserves after bring-up.'
165
+ );
166
+ }
167
+ }
168
+
169
+ /** `<something>/p2p-circuit` — the configured shape, as opposed to the bare search addr. */
170
+ function isConfiguredCircuitListenAddr(listenAddr: string): boolean {
171
+ if (listenAddr === RELAY_SEARCH_LISTEN_ADDR) {
172
+ return false;
173
+ }
174
+ try {
175
+ return multiaddr(listenAddr).getComponents().some((c) => c.name === 'p2p-circuit');
176
+ } catch {
177
+ // Not our error to raise: libp2p reports an unparsable listen addr itself, and
178
+ // this helper only ever ADDS a denial.
179
+ return false;
180
+ }
181
+ }
182
+
183
+ /**
184
+ * One entry of {@link relayCircuitAddrs}: the addr the node listens on for this relay.
185
+ *
186
+ * NOTE: an entry carrying components AFTER `/p2p-circuit` — a full relayed DIAL addr,
187
+ * `…/p2p/<relay>/p2p-circuit/p2p/<someone else>` — is passed through as written and
188
+ * would be listened on verbatim. It validates (the relay it names is real), so only an
189
+ * operator who pastes a peer's relayed address into `relayAddrs` hits it; if that turns
190
+ * out to be a common paste, truncate at the `/p2p-circuit` component here.
191
+ */
192
+ function circuitListenAddr(relayAddr: string): string {
193
+ const alreadyCircuit = parseComponents(relayAddr).some((c) => c.name === 'p2p-circuit');
194
+ const listenAddr = alreadyCircuit ? relayAddr : `${relayAddr}/p2p-circuit`;
195
+ try {
196
+ circuitRelayTargetOrThrow(listenAddr); // validates the relay peerId
197
+ } catch (err) {
198
+ throw new Error(
199
+ `network.relayAddrs entry names no relay peerId (expected <dial addr>/p2p/<relayPeerId>): ${relayAddr}`,
200
+ { cause: err }
201
+ );
202
+ }
203
+ return listenAddr;
204
+ }
205
+
206
+ /** Multiaddr components of `relayAddr`, with the config field named on a parse failure. */
207
+ function parseComponents(relayAddr: string): Component[] {
208
+ try {
209
+ return multiaddr(relayAddr).getComponents();
210
+ } catch (err) {
211
+ throw new Error(`network.relayAddrs entry is not a valid multiaddr: ${relayAddr}`, { cause: err });
212
+ }
213
+ }
214
+
215
+ /** Exact-string dedupe, first occurrence wins, so the list is stable across restarts. */
216
+ function dedupe(addrs: readonly string[]): string[] {
217
+ return [...new Set(addrs)];
218
+ }