@serfab/cadre-core 0.12.0 → 1.0.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.
- package/README.md +23 -8
- package/dist/bootstrap-peer-store-file.js +2 -2
- package/dist/bootstrap-peer-store.d.ts +32 -12
- package/dist/bootstrap-peer-store.js +41 -15
- package/dist/bootstrap-peer-store.js.map +1 -1
- package/dist/cadre-node.d.ts +883 -102
- package/dist/cadre-node.js +1800 -209
- package/dist/cadre-node.js.map +1 -1
- package/dist/control-authorization.d.ts +2 -2
- package/dist/control-authorization.js +1 -0
- package/dist/control-authorization.js.map +1 -1
- package/dist/control-cohort.d.ts +13 -26
- package/dist/control-cohort.js +0 -26
- package/dist/control-cohort.js.map +1 -1
- package/dist/control-database.d.ts +318 -44
- package/dist/control-database.js +516 -74
- package/dist/control-database.js.map +1 -1
- package/dist/control-read-retry.d.ts +40 -11
- package/dist/control-read-retry.js +48 -15
- package/dist/control-read-retry.js.map +1 -1
- package/dist/control-retry.d.ts +58 -3
- package/dist/control-retry.js +58 -3
- package/dist/control-retry.js.map +1 -1
- package/dist/control-schema.d.ts +1 -1
- package/dist/control-schema.js +133 -21
- package/dist/control-schema.js.map +1 -1
- package/dist/control-write-retry.d.ts +80 -11
- package/dist/control-write-retry.js +168 -29
- package/dist/control-write-retry.js.map +1 -1
- package/dist/enrolled-machine-store-file.d.ts +13 -0
- package/dist/enrolled-machine-store-file.js +41 -0
- package/dist/enrolled-machine-store-file.js.map +1 -0
- package/dist/enrolled-machine-store.d.ts +81 -0
- package/dist/enrolled-machine-store.js +225 -0
- package/dist/enrolled-machine-store.js.map +1 -0
- package/dist/file-durable-slot.js +5 -3
- package/dist/file-durable-slot.js.map +1 -1
- package/dist/index.d.ts +17 -8
- package/dist/index.js +54 -9
- package/dist/index.js.map +1 -1
- package/dist/membership-connection-gater.d.ts +15 -1
- package/dist/membership-connection-gater.js +9 -3
- package/dist/membership-connection-gater.js.map +1 -1
- package/dist/node-local-snapshot.d.ts +21 -7
- package/dist/node-local-snapshot.js +30 -9
- package/dist/node-local-snapshot.js.map +1 -1
- package/dist/peer-dial.d.ts +143 -0
- package/dist/peer-dial.js +176 -0
- package/dist/peer-dial.js.map +1 -0
- package/dist/{strand-backfill.d.ts → peer-join-backfill.d.ts} +49 -20
- package/dist/{strand-backfill.js → peer-join-backfill.js} +133 -52
- package/dist/peer-join-backfill.js.map +1 -0
- package/dist/relay-addrs.d.ts +160 -49
- package/dist/relay-addrs.js +287 -60
- package/dist/relay-addrs.js.map +1 -1
- package/dist/relay-reservation.d.ts +71 -3
- package/dist/relay-reservation.js +116 -8
- package/dist/relay-reservation.js.map +1 -1
- package/dist/seed-bootstrap.d.ts +30 -1
- package/dist/seed-bootstrap.js +62 -17
- package/dist/seed-bootstrap.js.map +1 -1
- package/dist/storage-scope.d.ts +62 -0
- package/dist/storage-scope.js +75 -0
- package/dist/storage-scope.js.map +1 -0
- package/dist/strand-database.d.ts +41 -11
- package/dist/strand-database.js +48 -18
- package/dist/strand-database.js.map +1 -1
- package/dist/strand-first-sync-gate.d.ts +152 -0
- package/dist/strand-first-sync-gate.js +208 -0
- package/dist/strand-first-sync-gate.js.map +1 -0
- package/dist/strand-formation-manager.d.ts +99 -5
- package/dist/strand-formation-manager.js +119 -8
- package/dist/strand-formation-manager.js.map +1 -1
- package/dist/strand-formation-protocol.d.ts +97 -3
- package/dist/strand-formation-protocol.js +108 -2
- package/dist/strand-formation-protocol.js.map +1 -1
- package/dist/strand-instance-manager.d.ts +389 -19
- package/dist/strand-instance-manager.js +678 -65
- package/dist/strand-instance-manager.js.map +1 -1
- package/dist/strand-member-key.d.ts +20 -12
- package/dist/strand-member-key.js +20 -12
- package/dist/strand-member-key.js.map +1 -1
- package/dist/strand-member-registry.js +3 -0
- package/dist/strand-member-registry.js.map +1 -1
- package/dist/strand-membership-reconciler.d.ts +361 -0
- package/dist/strand-membership-reconciler.js +548 -0
- package/dist/strand-membership-reconciler.js.map +1 -0
- package/dist/strand-membership-writer.d.ts +143 -11
- package/dist/strand-membership-writer.js +419 -135
- package/dist/strand-membership-writer.js.map +1 -1
- package/dist/strand-network-config.d.ts +116 -0
- package/dist/strand-network-config.js +192 -0
- package/dist/strand-network-config.js.map +1 -0
- package/dist/strand-revocation-enforcer.d.ts +392 -0
- package/dist/strand-revocation-enforcer.js +533 -0
- package/dist/strand-revocation-enforcer.js.map +1 -0
- package/dist/strand-solicitation.d.ts +17 -1
- package/dist/strand-solicitation.js +13 -2
- package/dist/strand-solicitation.js.map +1 -1
- package/dist/strand-wake-protocol.d.ts +11 -7
- package/dist/strand-wake-protocol.js +20 -52
- package/dist/strand-wake-protocol.js.map +1 -1
- package/dist/strand-watcher.js +12 -1
- package/dist/strand-watcher.js.map +1 -1
- package/dist/timed-step.d.ts +12 -0
- package/dist/timed-step.js +28 -0
- package/dist/timed-step.js.map +1 -0
- package/dist/timeout-scheduler.d.ts +19 -0
- package/dist/timeout-scheduler.js +22 -0
- package/dist/timeout-scheduler.js.map +1 -0
- package/dist/types.d.ts +472 -46
- package/dist/types.js +7 -1
- package/dist/types.js.map +1 -1
- package/package.json +16 -8
- package/src/bootstrap-peer-store-file.ts +2 -2
- package/src/bootstrap-peer-store.ts +69 -27
- package/src/cadre-node.ts +1926 -228
- package/src/control-authorization.ts +2 -1
- package/src/control-cohort.ts +122 -130
- package/src/control-database.ts +586 -74
- package/src/control-read-retry.ts +202 -167
- package/src/control-retry.ts +297 -190
- package/src/control-schema.ts +133 -21
- package/src/control-write-retry.ts +525 -383
- package/src/enrolled-machine-store-file.ts +43 -0
- package/src/enrolled-machine-store.ts +280 -0
- package/src/file-durable-slot.ts +5 -3
- package/src/index.ts +134 -15
- package/src/membership-connection-gater.ts +9 -3
- package/src/node-local-snapshot.ts +38 -9
- package/src/peer-addr-book.ts +251 -250
- package/src/peer-dial.ts +215 -0
- package/src/{strand-backfill.ts → peer-join-backfill.ts} +478 -389
- package/src/relay-addrs.ts +458 -218
- package/src/relay-reservation.ts +951 -828
- package/src/seed-bootstrap.ts +76 -18
- package/src/storage-scope.ts +78 -0
- package/src/strand-database.ts +61 -21
- package/src/strand-first-sync-gate.ts +258 -0
- package/src/strand-formation-manager.ts +162 -8
- package/src/strand-formation-protocol.ts +850 -692
- package/src/strand-instance-manager.ts +877 -74
- package/src/strand-member-key.ts +20 -12
- package/src/strand-member-registry.ts +3 -0
- package/src/strand-membership-reconciler.ts +680 -0
- package/src/strand-membership-writer.ts +550 -190
- package/src/strand-network-config.ts +250 -0
- package/src/strand-revocation-enforcer.ts +653 -0
- package/src/strand-solicitation.ts +499 -471
- package/src/strand-wake-protocol.ts +21 -66
- package/src/strand-watcher.ts +325 -314
- package/src/timed-step.ts +28 -0
- package/src/timeout-scheduler.ts +28 -0
- package/src/types.ts +487 -52
- package/dist/strand-backfill.js.map +0 -1
package/src/relay-addrs.ts
CHANGED
|
@@ -1,218 +1,458 @@
|
|
|
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
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* |
|
|
11
|
-
* |
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
* reservation
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
* The
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
*
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
-
*
|
|
151
|
-
*
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
*
|
|
185
|
-
*
|
|
186
|
-
*
|
|
187
|
-
*
|
|
188
|
-
*
|
|
189
|
-
*
|
|
190
|
-
*
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
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
|
+
*
|
|
8
|
+
* | listen entry | libp2p behaviour |
|
|
9
|
+
* | ----------------------------------- | --------------------------------------------------------------- |
|
|
10
|
+
* | `<relayAddr>/p2p-circuit` | CONFIGURED: dial that relay from inside `listen()` and reserve, or throw |
|
|
11
|
+
* | bare `/p2p-circuit` | SEARCH: register a pending reservation; open no connection |
|
|
12
|
+
*
|
|
13
|
+
* Every cadre-built node takes the SEARCH shape, and something explicit drives the
|
|
14
|
+
* reservation afterwards (`relay-reservation.ts`):
|
|
15
|
+
*
|
|
16
|
+
* - The CONTROL node resolves `relayAddrs` here to ONE bare entry (whichever relay
|
|
17
|
+
* answers first fills it) and `CadreNode.start()` drives the reservation once the
|
|
18
|
+
* control database is up. That ordering is the point: a configured circuit
|
|
19
|
+
* listener dials the relay from inside `libp2p.start()`, so the control database
|
|
20
|
+
* was being built while a sibling relay was already in this node's cohort — and a
|
|
21
|
+
* sibling that has not yet replicated this node's `CadrePeer` row correctly refuses
|
|
22
|
+
* its control-DB streams, which killed `start()` outright. See `cadre-node.ts` →
|
|
23
|
+
* `start()`.
|
|
24
|
+
* - Each STRAND node resolves the same `relayAddrs` to one bare entry PER relay and
|
|
25
|
+
* runs one reservation supervisor per relay (`strand-network-config.ts`,
|
|
26
|
+
* `strand-instance-manager.ts`).
|
|
27
|
+
*
|
|
28
|
+
* The CONFIGURED shape is deliberately not producible from a `NetworkConfig` any
|
|
29
|
+
* more: it loses its address and never recovers when the relay restarts, when either
|
|
30
|
+
* side hangs up, and — with no network event at all — on libp2p's own reservation
|
|
31
|
+
* refresh, which removes the reservation before re-creating it while a configured
|
|
32
|
+
* listener republishes only from inside its own `listen()`. A hand-written
|
|
33
|
+
* `<relay>/p2p-circuit` entry in `network.listenAddrs` is rejected on the control
|
|
34
|
+
* node ({@link resolveListenAddrs}) and folded into the supervised relay set on a
|
|
35
|
+
* strand node.
|
|
36
|
+
*
|
|
37
|
+
* `network.relayAddrs` is FAIL-FAST for the operator by default: a malformed entry
|
|
38
|
+
* throws here at config resolution regardless of posture, and on the control node a
|
|
39
|
+
* first reservation attempt that does not land throws out of `start()`
|
|
40
|
+
* (`RelayReservationFailedError`). Naming a relay that is down still means the control
|
|
41
|
+
* node does not come up. `network.requireRelay: false` softens the second half only —
|
|
42
|
+
* for a node that must still boot with no network (a phone, a browser tab) — logging
|
|
43
|
+
* the failed attempt instead of throwing and leaving the retry supervisor running in
|
|
44
|
+
* the background; the malformed-entry check is unaffected. A strand node is
|
|
45
|
+
* fail-SOFT regardless of `requireRelay` (its supervisor keeps trying while the
|
|
46
|
+
* strand serves).
|
|
47
|
+
*
|
|
48
|
+
* The bare `/p2p-circuit` search listener cannot open a connection on its own:
|
|
49
|
+
* libp2p fills a pending reservation from `RelayDiscovery`, which nominates a peer
|
|
50
|
+
* only once the relay-hop protocol id is in that peer's peer-store protocol list,
|
|
51
|
+
* and that list is written exclusively by IDENTIFY — which `@optimystic/db-p2p`
|
|
52
|
+
* namespaces per network, so a cadre node and a stock relay never identify each
|
|
53
|
+
* other. See `relay-reservation.ts`'s module doc and `docs/architecture.md`.
|
|
54
|
+
*/
|
|
55
|
+
|
|
56
|
+
import { multiaddr, type Component } from '@multiformats/multiaddr';
|
|
57
|
+
import { circuitRelayTargetOrThrow } from './delegate-admission.js';
|
|
58
|
+
import type { NetworkConfig } from './types.js';
|
|
59
|
+
import type { RelayReservationState } from './relay-reservation.js';
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The direct listener a node keeps when it configures `relayAddrs` but no
|
|
63
|
+
* `listenAddrs`. Without this, naming a relay would silently REPLACE the node's
|
|
64
|
+
* direct TCP listener with a circuit-only one. Shared with the strand-node
|
|
65
|
+
* derivation (`strand-network-config.ts`), which applies the same rule.
|
|
66
|
+
*
|
|
67
|
+
* NOTE: mirrors `createLibp2pNode`'s own default listen addr in
|
|
68
|
+
* `../optimystic/packages/db-p2p/src/libp2p-node.ts` (`/ip4/0.0.0.0/tcp/<port>`,
|
|
69
|
+
* and `CadreNode` passes `port: 0`) — that file is the source of truth. If it
|
|
70
|
+
* ever changes its default, this constant has to follow.
|
|
71
|
+
*/
|
|
72
|
+
export const DEFAULT_DIRECT_LISTEN_ADDR = '/ip4/0.0.0.0/tcp/0';
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* libp2p's relay SEARCH listener: registers one pending reservation and dials
|
|
76
|
+
* nothing. The pending reservation is per-LISTENER, not per-relay: the control
|
|
77
|
+
* node binds one entry for its whole relay list (`relay-reservation.ts` fills it
|
|
78
|
+
* with whichever relay answers first), a strand node binds one entry per relay so
|
|
79
|
+
* each relay's supervisor has a slot of its own to fill.
|
|
80
|
+
*/
|
|
81
|
+
export const RELAY_SEARCH_LISTEN_ADDR = '/p2p-circuit';
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* The circuit-listen multiaddr for each configured relay: `<relayAddr>/p2p-circuit`.
|
|
85
|
+
* An entry that already carries `/p2p-circuit` is passed through unchanged.
|
|
86
|
+
* Throws on an entry that is unparsable or names no relay peerId — this is
|
|
87
|
+
* operator configuration, so a typo must fail loudly at start rather than
|
|
88
|
+
* silently costing the node its reachability.
|
|
89
|
+
*
|
|
90
|
+
* No node LISTENS on these any more (see the module doc); they are the validated
|
|
91
|
+
* form of `relayAddrs`, read by `CadreNode.circuitRelayTargets` for the delegate
|
|
92
|
+
* announce and by `strand-network-config.ts` for the per-relay supervisor list.
|
|
93
|
+
*
|
|
94
|
+
* Deliberately unlike `extractCircuitRelayTargets` (`delegate-admission.ts`), which
|
|
95
|
+
* SKIPS a bad entry: that one reads addresses discovered at runtime from peers, where
|
|
96
|
+
* one bad entry must not be fatal.
|
|
97
|
+
*/
|
|
98
|
+
export function relayCircuitAddrs(relayAddrs: readonly string[]): string[] {
|
|
99
|
+
return dedupe(relayAddrs.map(circuitListenAddr));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The listen multiaddrs the CONTROL node binds: configured `listenAddrs` plus ONE
|
|
104
|
+
* bare `/p2p-circuit` search entry when `relayAddrs` names any relay, deduplicated,
|
|
105
|
+
* order-stable (configured entries first). Returns `undefined` when neither field
|
|
106
|
+
* is set, so callers keep omitting `listenAddrs` and inherit db-p2p's default.
|
|
107
|
+
*
|
|
108
|
+
* Every `relayAddrs` entry is validated even though the search entry discards the
|
|
109
|
+
* resolved circuit addrs — an operator typo must fail at config resolution. A
|
|
110
|
+
* hand-written `<relay>/p2p-circuit` entry in `listenAddrs` is rejected outright
|
|
111
|
+
* (see {@link rejectConfiguredCircuitListenAddrs}).
|
|
112
|
+
*
|
|
113
|
+
* Strand nodes derive their own listen set from the same config in
|
|
114
|
+
* `strand-network-config.ts` (`strandNodeAddrs`) — one search entry per relay,
|
|
115
|
+
* fixed direct ports made ephemeral.
|
|
116
|
+
*/
|
|
117
|
+
export function resolveListenAddrs(network: NetworkConfig | undefined): string[] | undefined {
|
|
118
|
+
const relayEntries = relayCircuitAddrs(network?.relayAddrs ?? []).length > 0
|
|
119
|
+
? [RELAY_SEARCH_LISTEN_ADDR]
|
|
120
|
+
: [];
|
|
121
|
+
const listenAddrs = network?.listenAddrs;
|
|
122
|
+
rejectConfiguredCircuitListenAddrs(listenAddrs ?? []);
|
|
123
|
+
if (!listenAddrs && relayEntries.length === 0) {
|
|
124
|
+
return undefined;
|
|
125
|
+
}
|
|
126
|
+
// An explicitly empty `listenAddrs` (the React Native "cannot listen" case) stays
|
|
127
|
+
// empty — but a relay named alongside it still gets its listen entry, since
|
|
128
|
+
// acquiring a slot is the whole point of configuring a relay.
|
|
129
|
+
return dedupe([...(listenAddrs ?? [DEFAULT_DIRECT_LISTEN_ADDR]), ...relayEntries]);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* The transport-derived `createLibp2pNode` options a resolved listen set implies —
|
|
134
|
+
* see {@link resolveTransportOptions}.
|
|
135
|
+
*/
|
|
136
|
+
export interface ListenTransportOptions {
|
|
137
|
+
/**
|
|
138
|
+
* Present when some listen entry names WebSocket. Its VALUE is never bound; it is
|
|
139
|
+
* the switch that makes `@optimystic/db-p2p` add `webSockets()` — see
|
|
140
|
+
* {@link WS_TRANSPORT_SWITCH_PORT}.
|
|
141
|
+
*/
|
|
142
|
+
wsPort?: number;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* The `wsPort` value passed purely to turn `@optimystic/db-p2p`'s WebSocket transport
|
|
147
|
+
* ON. It is never bound, so it is deliberately `0` rather than a port scraped from a
|
|
148
|
+
* listen entry.
|
|
149
|
+
*
|
|
150
|
+
* WHY it is a switch and not a port: `createLibp2pNode`
|
|
151
|
+
* (`../optimystic/packages/db-p2p/src/libp2p-node.ts`) feeds `wsPort` into BOTH its
|
|
152
|
+
* default transports and its default listen addrs, and `createLibp2pNodeBase`
|
|
153
|
+
* (`libp2p-node-base.ts:490-491`) falls back to those two defaults INDEPENDENTLY:
|
|
154
|
+
*
|
|
155
|
+
* ```ts
|
|
156
|
+
* const listenAddrs = options.listenAddrs ?? defaults.listenAddrs;
|
|
157
|
+
* const transports = options.transports ?? defaults.transports;
|
|
158
|
+
* ```
|
|
159
|
+
*
|
|
160
|
+
* `cadre-core` always supplies explicit `listenAddrs` alongside it, so the synthesized
|
|
161
|
+
* `/ip4/<wsHost>/tcp/<wsPort>/ws` default ADDRESS is discarded while the `webSockets()`
|
|
162
|
+
* transport it added survives. A scraped port would also be a lie whenever a config
|
|
163
|
+
* names several WebSocket addresses on different ports, and the strand path rewrites
|
|
164
|
+
* fixed ports to `0` regardless (`strand-network-config.ts`).
|
|
165
|
+
*
|
|
166
|
+
* NOTE: db-p2p's own doc comment on `wsPort` (`libp2p-node-base.ts:170-172`) says it is
|
|
167
|
+
* "Ignored when `transports`/`listenAddrs` are explicitly provided" — true of the
|
|
168
|
+
* address half, NOT of the transport half, which is the half this depends on. Re-verify
|
|
169
|
+
* empirically rather than trusting that comment: `test/listen-transport-options.spec.ts`
|
|
170
|
+
* boots a real node and asserts a `/ws` multiaddr appears. If db-p2p ever makes the
|
|
171
|
+
* comment true of both halves, it needs an explicit `enableWebSockets` switch and this
|
|
172
|
+
* constant becomes unusable.
|
|
173
|
+
*/
|
|
174
|
+
const WS_TRANSPORT_SWITCH_PORT = 0;
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* The transport-derived libp2p options `listenAddrs` implies, and the gate that stops a
|
|
178
|
+
* listen address the node has no transport for from being SILENTLY dropped.
|
|
179
|
+
*
|
|
180
|
+
* libp2p's transport manager sorts configured listen addresses by which transport claims
|
|
181
|
+
* them, discards the unclaimed ones, and raises `UnsupportedListenAddressesError` only
|
|
182
|
+
* when EVERY address was discarded. Pair a TCP address with a WebSocket one — which is
|
|
183
|
+
* exactly what the shipped configs do — and the TCP address carries the start while the
|
|
184
|
+
* missing WebSocket listener is never reported. Two halves of `NetworkConfig` that have
|
|
185
|
+
* to agree (`listenAddrs`, and the transports the node will actually have) were never
|
|
186
|
+
* checked against each other; this is that check, and it covers both node kinds because
|
|
187
|
+
* both derive their listen set from the same config here ({@link resolveListenAddrs}) or
|
|
188
|
+
* in `strand-network-config.ts`, and both call this on the result.
|
|
189
|
+
*
|
|
190
|
+
* The two transport classes are handled differently, on purpose:
|
|
191
|
+
*
|
|
192
|
+
* - **WebSocket is DERIVED.** It is the one non-TCP transport the shipped configs need
|
|
193
|
+
* (the React Native reference app's companion drone; `cadre start --ws-port`), and
|
|
194
|
+
* `@optimystic/db-p2p` already knows how to add it. `cadre-core` grows no dependency
|
|
195
|
+
* and decides no transport policy — it flips db-p2p's own switch
|
|
196
|
+
* ({@link WS_TRANSPORT_SWITCH_PORT}).
|
|
197
|
+
* - **Everything else is REFUSED.** Deriving `/quic-v1`, `/webrtc` or `/webtransport`
|
|
198
|
+
* would mean importing transport packages into every `cadre-core` consumer including
|
|
199
|
+
* the React Native and browser bundles (against the cross-platform rule in
|
|
200
|
+
* `AGENTS.md`) and duplicating policy `libp2p-node.ts` owns. So name the address, name
|
|
201
|
+
* the transport it needs, and refuse to start — matching how `network.relayAddrs` and
|
|
202
|
+
* `network.announceAddrs` already treat an operator typo.
|
|
203
|
+
*
|
|
204
|
+
* Returns `{}` unconditionally when `network.transports` is set: a programmatic embedder
|
|
205
|
+
* supplying transport factories owns the policy, and the factories are opaque — nothing
|
|
206
|
+
* can be inferred from them. That is what keeps the RN phone, the web app, and the
|
|
207
|
+
* integration-test harness unaffected.
|
|
208
|
+
*
|
|
209
|
+
* NOTE: pairing this with {@link resolveListenAddrs} is a CONVENTION, not a structure —
|
|
210
|
+
* a third libp2p-node build site could resolve listen addrs and forget to call this,
|
|
211
|
+
* putting the original bug back on that path. Both existing sites are covered
|
|
212
|
+
* (`cadre-node.ts` → `buildControlNodeOptions`, `strand-network-config.ts` →
|
|
213
|
+
* `strandNodeAddrs`), and they are the only two `createLibp2pNode` callers in this repo
|
|
214
|
+
* that derive their listen set from a `NetworkConfig` — the other callers
|
|
215
|
+
* (`integration-tests/src/harness/test-party.ts`, `quereus-plugin-sereus`'s `connect.ts`
|
|
216
|
+
* and `connect-browser.ts`) pass no `listenAddrs` at all, or pass their own transports
|
|
217
|
+
* with it. If a third CONFIG-derived site appears, fold the two functions into one that
|
|
218
|
+
* returns listen addrs and transport options together, so forgetting becomes impossible
|
|
219
|
+
* rather than merely unlikely.
|
|
220
|
+
*
|
|
221
|
+
* @param listenAddrs the ALREADY-resolved listen set — the output of
|
|
222
|
+
* {@link resolveListenAddrs}, or the strand derivation, after any per-node rewriting —
|
|
223
|
+
* so the check reads what this node will actually bind.
|
|
224
|
+
* @throws {UnbindableListenAddressError} when a listen entry names a transport outside
|
|
225
|
+
* {tcp, ws/wss, p2p-circuit}.
|
|
226
|
+
*/
|
|
227
|
+
export function resolveTransportOptions(
|
|
228
|
+
network: NetworkConfig | undefined,
|
|
229
|
+
listenAddrs: readonly string[] | undefined
|
|
230
|
+
): ListenTransportOptions {
|
|
231
|
+
if (network?.transports) {
|
|
232
|
+
return {};
|
|
233
|
+
}
|
|
234
|
+
const kinds = (listenAddrs ?? []).map((addr) => [addr, listenTransportKind(addr)] as const);
|
|
235
|
+
const unsupported = kinds.filter(([, kind]) => kind === 'unsupported').map(([addr]) => addr);
|
|
236
|
+
if (unsupported.length > 0) {
|
|
237
|
+
throw new UnbindableListenAddressError(unsupported);
|
|
238
|
+
}
|
|
239
|
+
return kinds.some(([, kind]) => kind === 'websockets') ? { wsPort: WS_TRANSPORT_SWITCH_PORT } : {};
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Thrown at config resolution when `network.listenAddrs` names an address none of the
|
|
244
|
+
* default transports can bind. Names each offending address alongside the libp2p
|
|
245
|
+
* transport package it would need — what the operator has without this is a node that
|
|
246
|
+
* starts and silently never listens there.
|
|
247
|
+
*/
|
|
248
|
+
export class UnbindableListenAddressError extends Error {
|
|
249
|
+
constructor(readonly listenAddrs: readonly string[]) {
|
|
250
|
+
super(
|
|
251
|
+
'network.listenAddrs names an address this node has no transport for: ' +
|
|
252
|
+
`${listenAddrs.map((a) => `${a} — ${unbindableReason(a)}`).join('; ')}. ` +
|
|
253
|
+
'Default transports bind TCP (including /unix/<path>), WebSocket (/ws, /wss) and circuit-relay addresses only. ' +
|
|
254
|
+
'Either drop the address, or supply the transport programmatically via network.transports.'
|
|
255
|
+
);
|
|
256
|
+
this.name = 'UnbindableListenAddressError';
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Which default transport claims `listenAddr`, or `'unsupported'` when none does.
|
|
262
|
+
*
|
|
263
|
+
* Classification is a multiaddr COMPONENT question, not a string question, so this
|
|
264
|
+
* parses and reads the transport stack — the component names left once the addressing
|
|
265
|
+
* and security layers are dropped. That stack is outermost-LAST
|
|
266
|
+
* (`/ip4/…/tcp/443/tls/ws` → `['tcp', 'ws']`), which is why its terminal entry decides.
|
|
267
|
+
*
|
|
268
|
+
* An unparsable entry is `'ignored'` and passes through untouched, following
|
|
269
|
+
* `isConfiguredCircuitListenAddr` below and `ephemeralPortListenAddr`
|
|
270
|
+
* (`strand-network-config.ts`): libp2p reports a bad listen addr itself, and this check
|
|
271
|
+
* must only ever ADD a denial.
|
|
272
|
+
*/
|
|
273
|
+
function listenTransportKind(listenAddr: string): 'tcp' | 'websockets' | 'circuit' | 'ignored' | 'unsupported' {
|
|
274
|
+
let stack: readonly string[];
|
|
275
|
+
try {
|
|
276
|
+
stack = transportStack(listenAddr);
|
|
277
|
+
} catch {
|
|
278
|
+
return 'ignored';
|
|
279
|
+
}
|
|
280
|
+
const outermost = stack.at(-1);
|
|
281
|
+
if (outermost === undefined) {
|
|
282
|
+
// Nothing but addressing components — a bare `/ip4/1.2.3.4`. No transport claims
|
|
283
|
+
// it, so it is dropped by exactly the same silent path this check exists to close.
|
|
284
|
+
return 'unsupported';
|
|
285
|
+
}
|
|
286
|
+
if (outermost === 'p2p-circuit') {
|
|
287
|
+
return 'circuit';
|
|
288
|
+
}
|
|
289
|
+
if (outermost === 'ws' || outermost === 'wss') {
|
|
290
|
+
return 'websockets';
|
|
291
|
+
}
|
|
292
|
+
// Only a BARE tcp or unix stack is TCP. `@libp2p/tcp` binds both — its `listenFilter`
|
|
293
|
+
// accepts an exact TCP match OR a `/unix/<path>` address (a named pipe on Windows) —
|
|
294
|
+
// and nothing layered on top of either, which is why `/tcp/<port>/<anything else>` is
|
|
295
|
+
// NOT waved through merely because a `tcp` component happens to appear.
|
|
296
|
+
return stack.length === 1 && (outermost === 'tcp' || outermost === 'unix') ? 'tcp' : 'unsupported';
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Component names of `listenAddr` with the addressing and security layers removed, so
|
|
301
|
+
* what remains names transports. Host components (`ip4`, `dns4`, …) and the layers that
|
|
302
|
+
* ride inside a transport without being one (`tls`, `sni`, `certhash`, `p2p`) say
|
|
303
|
+
* nothing about which transport has to be configured.
|
|
304
|
+
*/
|
|
305
|
+
function transportStack(listenAddr: string): string[] {
|
|
306
|
+
return multiaddr(listenAddr).getComponents()
|
|
307
|
+
.map((component) => component.name)
|
|
308
|
+
.filter((name) => !NON_TRANSPORT_COMPONENTS.has(name));
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** Components that address or secure a connection rather than name its transport. */
|
|
312
|
+
const NON_TRANSPORT_COMPONENTS = new Set([
|
|
313
|
+
'ip4', 'ip6', 'ip6zone', 'dns', 'dns4', 'dns6', 'dnsaddr',
|
|
314
|
+
'tls', 'sni', 'certhash', 'p2p', 'noise'
|
|
315
|
+
]);
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Why `listenAddr` cannot be bound, phrased for the operator: the libp2p package that
|
|
319
|
+
* would make it bindable where one is known, and otherwise what is wrong with it.
|
|
320
|
+
*
|
|
321
|
+
* Read OUTERMOST-first (`/udp/…/quic-v1/webtransport` needs `@libp2p/webtransport`,
|
|
322
|
+
* not the `@libp2p/quic` it rides on), matching how `listenTransportKind` decides.
|
|
323
|
+
*/
|
|
324
|
+
function unbindableReason(listenAddr: string): string {
|
|
325
|
+
let stack: readonly string[];
|
|
326
|
+
try {
|
|
327
|
+
stack = transportStack(listenAddr);
|
|
328
|
+
} catch {
|
|
329
|
+
return 'not a parsable multiaddr';
|
|
330
|
+
}
|
|
331
|
+
if (stack.length === 0) {
|
|
332
|
+
return 'it names no transport component';
|
|
333
|
+
}
|
|
334
|
+
const pkg = [...stack].reverse().map((name) => TRANSPORT_PACKAGES[name]).find((p) => p !== undefined);
|
|
335
|
+
return pkg === undefined ? 'needs an unrecognized transport' : `needs ${pkg}`;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* The libp2p package each unbindable transport component comes from. Naming the package
|
|
340
|
+
* is the actionable half of the refusal — the operator either drops the address or wires
|
|
341
|
+
* that package in through `network.transports`.
|
|
342
|
+
*/
|
|
343
|
+
const TRANSPORT_PACKAGES: Record<string, string> = {
|
|
344
|
+
'quic': '@libp2p/quic',
|
|
345
|
+
'quic-v1': '@libp2p/quic',
|
|
346
|
+
'webtransport': '@libp2p/webtransport',
|
|
347
|
+
'webrtc': '@libp2p/webrtc',
|
|
348
|
+
'webrtc-direct': '@libp2p/webrtc'
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Thrown out of `CadreNode.start()` when the boot-path reservation drive for
|
|
353
|
+
* `network.relayAddrs` produces no `/p2p-circuit` address on its FIRST attempt,
|
|
354
|
+
* UNLESS `network.requireRelay` is explicitly `false` (see `driveControlRelayReservation`,
|
|
355
|
+
* which logs and returns instead on that posture).
|
|
356
|
+
*
|
|
357
|
+
* This is what keeps `network.relayAddrs` fail-fast by default now that the control node
|
|
358
|
+
* listens on the bare search entry: libp2p's own `UnsupportedListenAddressesError` used
|
|
359
|
+
* to abort start from inside `listen()`, and an operator who names a relay is
|
|
360
|
+
* telling the node it has no other reachability — coming up undialable is worse
|
|
361
|
+
* than not coming up. Names the relays and the reservation's own reason, both of
|
|
362
|
+
* which the libp2p error omitted.
|
|
363
|
+
*/
|
|
364
|
+
export class RelayReservationFailedError extends Error {
|
|
365
|
+
constructor(
|
|
366
|
+
readonly relayAddrs: readonly string[],
|
|
367
|
+
readonly state: RelayReservationState
|
|
368
|
+
) {
|
|
369
|
+
super(
|
|
370
|
+
`network.relayAddrs reservation failed (status: ${state.status}): ` +
|
|
371
|
+
`${state.error ?? 'no /p2p-circuit address appeared'} — relays: ${relayAddrs.join(', ')}`
|
|
372
|
+
);
|
|
373
|
+
this.name = 'RelayReservationFailedError';
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* A hand-written `<relay>/p2p-circuit` entry in `network.listenAddrs` is the
|
|
379
|
+
* CONFIGURED listener shape, and it is unusable on the control node: libp2p
|
|
380
|
+
* dials that relay from inside `listen()`, the bring-up quiet period
|
|
381
|
+
* (`membership-connection-gater.ts`) denies exactly that dial, `listen()` fails, and
|
|
382
|
+
* the transport manager's default `FATAL_ALL` aborts `libp2p.start()`. The operator
|
|
383
|
+
* would see `UnsupportedListenAddressesError` from deep inside bring-up with nothing
|
|
384
|
+
* naming the cause, so name it here instead.
|
|
385
|
+
*
|
|
386
|
+
* `network.relayAddrs` is the exact replacement — same relay, and the reservation is
|
|
387
|
+
* driven after bring-up, which is the whole point of the search shape. (A strand node
|
|
388
|
+
* built directly from such a config folds the entry into its supervised relay set
|
|
389
|
+
* instead — `strand-network-config.ts` — but the control node's resolution runs first
|
|
390
|
+
* on every machine, so in production the entry never reaches a strand node.)
|
|
391
|
+
*/
|
|
392
|
+
function rejectConfiguredCircuitListenAddrs(listenAddrs: readonly string[]): void {
|
|
393
|
+
const configured = listenAddrs.filter(isConfiguredCircuitListenAddr);
|
|
394
|
+
if (configured.length > 0) {
|
|
395
|
+
throw new Error(
|
|
396
|
+
'network.listenAddrs names a relay directly ' +
|
|
397
|
+
`(${configured.join(', ')}), which a control node cannot listen on: libp2p would ` +
|
|
398
|
+
'dial that relay during control-database bring-up, when the node accepts no ' +
|
|
399
|
+
'connections. Move the relay to network.relayAddrs, which reserves after bring-up.'
|
|
400
|
+
);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* `<something>/p2p-circuit` — the configured shape, as opposed to the bare search
|
|
406
|
+
* addr. An unparsable entry is `false`: libp2p reports a bad listen addr itself,
|
|
407
|
+
* and this helper only ever ADDS a denial (or, on the strand path, a supervisor).
|
|
408
|
+
*/
|
|
409
|
+
export function isConfiguredCircuitListenAddr(listenAddr: string): boolean {
|
|
410
|
+
if (listenAddr === RELAY_SEARCH_LISTEN_ADDR) {
|
|
411
|
+
return false;
|
|
412
|
+
}
|
|
413
|
+
try {
|
|
414
|
+
return multiaddr(listenAddr).getComponents().some((c) => c.name === 'p2p-circuit');
|
|
415
|
+
} catch {
|
|
416
|
+
// Not our error to raise: libp2p reports an unparsable listen addr itself, and
|
|
417
|
+
// this helper only ever ADDS a denial.
|
|
418
|
+
return false;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* One entry of {@link relayCircuitAddrs}: this relay's addr in the configured shape.
|
|
424
|
+
*
|
|
425
|
+
* NOTE: an entry carrying components AFTER `/p2p-circuit` — a full relayed DIAL addr,
|
|
426
|
+
* `…/p2p/<relay>/p2p-circuit/p2p/<someone else>` — is passed through as written. It
|
|
427
|
+
* validates (the relay it names is real), and every consumer reads only the relay
|
|
428
|
+
* peer id and the dial prefix before `/p2p-circuit`, so the tail is ignored rather
|
|
429
|
+
* than harmful; if that paste turns out to be common, truncate at the `/p2p-circuit`
|
|
430
|
+
* component here so the resolved list is canonical.
|
|
431
|
+
*/
|
|
432
|
+
function circuitListenAddr(relayAddr: string): string {
|
|
433
|
+
const alreadyCircuit = parseComponents(relayAddr).some((c) => c.name === 'p2p-circuit');
|
|
434
|
+
const listenAddr = alreadyCircuit ? relayAddr : `${relayAddr}/p2p-circuit`;
|
|
435
|
+
try {
|
|
436
|
+
circuitRelayTargetOrThrow(listenAddr); // validates the relay peerId
|
|
437
|
+
} catch (err) {
|
|
438
|
+
throw new Error(
|
|
439
|
+
`network.relayAddrs entry names no relay peerId (expected <dial addr>/p2p/<relayPeerId>): ${relayAddr}`,
|
|
440
|
+
{ cause: err }
|
|
441
|
+
);
|
|
442
|
+
}
|
|
443
|
+
return listenAddr;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/** Multiaddr components of `relayAddr`, with the config field named on a parse failure. */
|
|
447
|
+
function parseComponents(relayAddr: string): Component[] {
|
|
448
|
+
try {
|
|
449
|
+
return multiaddr(relayAddr).getComponents();
|
|
450
|
+
} catch (err) {
|
|
451
|
+
throw new Error(`network.relayAddrs entry is not a valid multiaddr: ${relayAddr}`, { cause: err });
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/** Exact-string dedupe, first occurrence wins, so the list is stable across restarts. */
|
|
456
|
+
function dedupe(addrs: readonly string[]): string[] {
|
|
457
|
+
return [...new Set(addrs)];
|
|
458
|
+
}
|