@serfab/cadre-core 0.10.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.
- package/README.md +255 -255
- package/dist/announce-addrs.d.ts +49 -0
- package/dist/announce-addrs.js +78 -0
- package/dist/announce-addrs.js.map +1 -0
- package/dist/cadre-node.d.ts +484 -58
- package/dist/cadre-node.js +953 -182
- package/dist/cadre-node.js.map +1 -1
- package/dist/control-cohort.d.ts +26 -0
- package/dist/control-cohort.js +26 -0
- package/dist/control-cohort.js.map +1 -1
- package/dist/control-database.d.ts +222 -125
- package/dist/control-database.js +356 -266
- package/dist/control-database.js.map +1 -1
- package/dist/control-formation-recorder.d.ts +8 -7
- package/dist/control-formation-recorder.js +8 -7
- package/dist/control-formation-recorder.js.map +1 -1
- package/dist/control-read-retry.d.ts +103 -0
- package/dist/control-read-retry.js +145 -0
- package/dist/control-read-retry.js.map +1 -0
- package/dist/control-retry.d.ts +101 -0
- package/dist/control-retry.js +108 -0
- package/dist/control-retry.js.map +1 -0
- package/dist/control-schema.d.ts +1 -1
- package/dist/control-schema.js +805 -774
- package/dist/control-schema.js.map +1 -1
- package/dist/control-write-retry.d.ts +65 -31
- package/dist/control-write-retry.js +33 -103
- package/dist/control-write-retry.js.map +1 -1
- package/dist/delegate-admission.d.ts +17 -15
- package/dist/delegate-admission.js +17 -15
- package/dist/delegate-admission.js.map +1 -1
- package/dist/index.d.ts +7 -5
- package/dist/index.js +18 -9
- package/dist/index.js.map +1 -1
- package/dist/membership-connection-gater.d.ts +211 -25
- package/dist/membership-connection-gater.js +309 -29
- package/dist/membership-connection-gater.js.map +1 -1
- package/dist/peer-addr-book.d.ts +92 -0
- package/dist/peer-addr-book.js +220 -0
- package/dist/peer-addr-book.js.map +1 -0
- package/dist/peer-record.d.ts +26 -0
- package/dist/peer-record.js +32 -0
- package/dist/peer-record.js.map +1 -1
- package/dist/relay-addrs.d.ts +76 -26
- package/dist/relay-addrs.js +115 -27
- package/dist/relay-addrs.js.map +1 -1
- package/dist/relay-reservation.d.ts +20 -15
- package/dist/relay-reservation.js +18 -14
- package/dist/relay-reservation.js.map +1 -1
- package/dist/seed-bootstrap.d.ts +7 -0
- package/dist/seed-bootstrap.js +15 -13
- package/dist/seed-bootstrap.js.map +1 -1
- package/dist/strand-addr-protocol.js +16 -0
- package/dist/strand-addr-protocol.js.map +1 -1
- package/dist/strand-backfill.js +13 -4
- package/dist/strand-backfill.js.map +1 -1
- package/dist/strand-cohort.d.ts +4 -37
- package/dist/strand-cohort.js +4 -15
- package/dist/strand-cohort.js.map +1 -1
- package/dist/strand-database.d.ts +11 -16
- package/dist/strand-database.js +19 -7
- package/dist/strand-database.js.map +1 -1
- package/dist/strand-formation-manager.d.ts +18 -17
- package/dist/strand-formation-manager.js +25 -27
- package/dist/strand-formation-manager.js.map +1 -1
- package/dist/strand-formation-protocol.d.ts +7 -7
- package/dist/strand-formation-protocol.js +8 -8
- package/dist/strand-formation-protocol.js.map +1 -1
- package/dist/strand-instance-manager.d.ts +53 -21
- package/dist/strand-instance-manager.js +128 -37
- package/dist/strand-instance-manager.js.map +1 -1
- package/dist/strand-membership-writer.d.ts +113 -19
- package/dist/strand-membership-writer.js +219 -51
- package/dist/strand-membership-writer.js.map +1 -1
- package/dist/strand-solicitation.d.ts +3 -3
- package/dist/strand-solicitation.js +4 -5
- package/dist/strand-solicitation.js.map +1 -1
- package/dist/strand-wake-protocol.d.ts +47 -2
- package/dist/strand-wake-protocol.js +76 -6
- package/dist/strand-wake-protocol.js.map +1 -1
- package/dist/types.d.ts +107 -54
- package/dist/types.js.map +1 -1
- package/package.json +8 -7
- package/src/announce-addrs.ts +93 -0
- package/src/arachnode-stub.ts +159 -159
- package/src/bootstrap-peer-store-file.ts +44 -44
- package/src/bootstrap-peer-store.ts +172 -172
- package/src/cadre-node.ts +1026 -194
- package/src/canonical-json.ts +30 -30
- package/src/control-authorization.ts +114 -114
- package/src/control-cohort.ts +130 -103
- package/src/control-database.ts +376 -298
- package/src/control-formation-recorder.ts +305 -304
- package/src/control-read-retry.ts +167 -0
- package/src/control-retry.ts +190 -0
- package/src/control-schema.ts +817 -786
- package/src/control-stream.ts +205 -205
- package/src/control-write-retry.ts +383 -473
- package/src/delegate-admission.ts +304 -302
- package/src/device-token.ts +94 -94
- package/src/diagnostics/connection-path.ts +277 -277
- package/src/diagnostics/webrtc-turn-tracker.ts +222 -222
- package/src/ed25519-key.ts +134 -134
- package/src/enrollment.ts +199 -199
- package/src/file-durable-slot.ts +61 -61
- package/src/formation-approval.ts +604 -604
- package/src/fs-atomic.ts +107 -107
- package/src/hibernation-manager.ts +437 -437
- package/src/identity-key.ts +109 -109
- package/src/index.ts +435 -406
- package/src/key-store-file.ts +132 -132
- package/src/key-store.ts +99 -99
- package/src/membership-connection-gater.ts +368 -39
- package/src/node-local-snapshot.ts +260 -260
- package/src/peer-addr-book.ts +250 -0
- package/src/peer-authorization.ts +259 -259
- package/src/peer-record.ts +197 -164
- package/src/push-credentials.ts +84 -84
- package/src/push-node.ts +64 -64
- package/src/push-notifier-apns.ts +247 -247
- package/src/push-notifier-fcm.ts +220 -220
- package/src/push-notifier-shared.ts +28 -28
- package/src/push-notifier.ts +52 -52
- package/src/relay-addrs.ts +134 -27
- package/src/relay-reservation.ts +22 -17
- package/src/seed-bootstrap.ts +15 -15
- package/src/seed-trust-policy.ts +140 -140
- package/src/strand-addr-protocol.ts +458 -441
- package/src/strand-backfill.ts +28 -5
- package/src/strand-cohort.ts +38 -77
- package/src/strand-database.ts +202 -204
- package/src/strand-formation-manager.ts +26 -28
- package/src/strand-formation-protocol.ts +692 -692
- package/src/strand-instance-manager.ts +636 -553
- package/src/strand-member-key.ts +39 -39
- package/src/strand-member-registry.ts +182 -182
- package/src/strand-membership-writer.ts +1627 -1430
- package/src/strand-solicitation.ts +471 -472
- package/src/strand-transport-key.ts +58 -58
- package/src/strand-wake-payload.ts +32 -32
- package/src/strand-wake-protocol.ts +400 -314
- package/src/strand-watcher.ts +314 -314
- package/src/trusted-owner-store-file.ts +41 -41
- package/src/trusted-owner-store.ts +171 -171
- package/src/types.ts +1354 -1303
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Control-network inbound
|
|
2
|
+
* Control-network inbound admission gates (defense-in-depth): the
|
|
3
|
+
* encrypted-connection checkpoint and the circuit-relay reservation checkpoint.
|
|
3
4
|
*
|
|
4
5
|
* Layer 2 of the membership enforcement chain: the PRIMARY gates are per-stream
|
|
5
6
|
* — every sensitive sereus control protocol rejects a peer that fails the
|
|
@@ -10,7 +11,9 @@
|
|
|
10
11
|
* time. This module adds the opportunistic connection-level layer on top: a
|
|
11
12
|
* peer this node can positively determine is NOT authorized is refused at the
|
|
12
13
|
* encrypted-connection checkpoint, before any protocol negotiation, so a
|
|
13
|
-
* known-nothing outsider is never even in the conversation
|
|
14
|
+
* known-nothing outsider is never even in the conversation — EXCEPT on a node
|
|
15
|
+
* that runs the circuit-relay server, where the refusal moves to the
|
|
16
|
+
* relay-reservation checkpoint instead (see "The relay-reservation seam").
|
|
14
17
|
*
|
|
15
18
|
* ## The stranger allowlist (the ONE place it is defined)
|
|
16
19
|
*
|
|
@@ -45,14 +48,54 @@
|
|
|
45
48
|
*
|
|
46
49
|
* - **Announced delegate peers** (`delegate-admission.ts`). A member's strand
|
|
47
50
|
* node runs as a separate libp2p identity whose peerId no sibling can
|
|
48
|
-
* recompute
|
|
49
|
-
* reservation. Before starting a strand node, a member's control node
|
|
51
|
+
* recompute. Before starting a strand node, a member's control node
|
|
50
52
|
* announces that peerId over the already-authenticated strand-addr RPC, and
|
|
51
53
|
* the receiver holds a short-lived grant for it
|
|
52
|
-
* (`CadreNode.grantDelegateAdmission`). The grant admits the CONNECTION
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
* on every
|
|
54
|
+
* (`CadreNode.grantDelegateAdmission`). The grant admits the CONNECTION,
|
|
55
|
+
* admits the peer's RESERVATION at the seam below (without spending the
|
|
56
|
+
* unauthorized budget), and nothing else — it is deliberately invisible to
|
|
57
|
+
* the per-stream gate, so a delegate still gets refused on every
|
|
58
|
+
* members-only protocol.
|
|
59
|
+
*
|
|
60
|
+
* ## The relay-reservation seam
|
|
61
|
+
*
|
|
62
|
+
* A circuit-relay reservation is established by the reserving peer DIALING the
|
|
63
|
+
* relay, so at the relay it is an inbound connection — and killing that
|
|
64
|
+
* connection kills the reservation. For a genuine member whose `CadrePeer` row
|
|
65
|
+
* has not yet replicated to this node, a connection-level deny here is NOT
|
|
66
|
+
* self-healing the way a data connection's is: an outbound reconcile dial
|
|
67
|
+
* re-establishes a data link, but no outbound dial can grant the REMOTE peer a
|
|
68
|
+
* reservation, and a relay-only peer has no address of its own to dial back —
|
|
69
|
+
* the reservation IS its address. Boot ordering makes the window ordinary (a
|
|
70
|
+
* node's circuit listener runs inside `libp2p.start()`, before its own row
|
|
71
|
+
* could have replicated anywhere), and a stalled replication makes it
|
|
72
|
+
* unbounded. So the relay would be answering a RELAY question ("may this peer
|
|
73
|
+
* use my forwarding capacity?") with a MEMBERSHIP answer, at a checkpoint where
|
|
74
|
+
* a wrong answer is unrecoverable.
|
|
75
|
+
*
|
|
76
|
+
* On a node whose relay server is enabled the two questions are separated:
|
|
77
|
+
*
|
|
78
|
+
* - The policy returns `'admit-for-relay'` instead of `'deny'`, and the
|
|
79
|
+
* connection is ADMITTED. The fail-closed per-stream gates still refuse such
|
|
80
|
+
* a peer every members-only protocol, so it gains identify/ping and the
|
|
81
|
+
* relay hop protocol, nothing else.
|
|
82
|
+
* - The reservation itself is decided at libp2p's
|
|
83
|
+
* `denyInboundRelayReservation` hook (the circuit-relay server consults it
|
|
84
|
+
* per RESERVE request): the policy admits members, delegates and configured
|
|
85
|
+
* infra outright, and admits peers it cannot place only within a bounded
|
|
86
|
+
* budget ({@link UnauthorizedReservationBudget}) — a member whose row is in
|
|
87
|
+
* flight always finds a slot under any sane cap, while outsiders cannot
|
|
88
|
+
* annex the party's relay capacity.
|
|
89
|
+
* - An `'admit-for-relay'` connection that is NOT reserving is dropped: it has
|
|
90
|
+
* {@link RELAY_ADMISSION_RESERVE_DEADLINE_MS} to get a reservation ADMITTED
|
|
91
|
+
* at that hook, after which the underlying connection is aborted. The
|
|
92
|
+
* guarantee above thus weakens on relay-enabled nodes from "never in the
|
|
93
|
+
* conversation" to "in it briefly, and can speak nothing".
|
|
94
|
+
*
|
|
95
|
+
* The deadline clears on reservation ADMISSION, not on the reservation's own
|
|
96
|
+
* success: this gate cannot observe the server-side `reserve()` outcome, so a
|
|
97
|
+
* peer whose admitted reservation is then refused for server capacity keeps its
|
|
98
|
+
* connection until either side closes it — bounded and mute, so harmless.
|
|
56
99
|
*
|
|
57
100
|
* Everything else a control node handles — the Optimystic control-DB protocols
|
|
58
101
|
* (`/optimystic/control-<party>/{repo,cluster,sync,block-transfer}/…`), wake
|
|
@@ -70,12 +113,45 @@
|
|
|
70
113
|
* stranger carve-outs — an enrollment window admits a stranger's connection
|
|
71
114
|
* for seed delivery, yet its repo streams are still refused.
|
|
72
115
|
*
|
|
116
|
+
* ## The bring-up quiet period
|
|
117
|
+
*
|
|
118
|
+
* One state is decided BEFORE any of the above, and is not about the remote peer
|
|
119
|
+
* at all: while this node's control-database bring-up is in flight
|
|
120
|
+
* (`InboundAdmissionPolicy.bringUpInFlight`), the gate denies BOTH directions —
|
|
121
|
+
* `denyDialPeer` and `denyInboundEncryptedConnection` — and then opens.
|
|
122
|
+
*
|
|
123
|
+
* The invariant it protects is "`ControlDatabase.initialize()` runs while this
|
|
124
|
+
* node holds ZERO control connections". Building that database is a long chain
|
|
125
|
+
* of cohort-consulting block probes, and every connected same-party sibling is in
|
|
126
|
+
* the cohort those probes consult. A sibling that has not yet replicated this
|
|
127
|
+
* node's `CadrePeer` row correctly refuses them at its own fail-closed per-stream
|
|
128
|
+
* gate, so ONE connection in this window turns bring-up into
|
|
129
|
+
* `BlockUnavailableError` and `start()` rejects. Retrying cannot converge: the
|
|
130
|
+
* condition that would clear the refusal is this node's own row reaching the
|
|
131
|
+
* sibling, and writing that row needs the database the retry is building. So the
|
|
132
|
+
* only fix is to not be in the conversation yet — which is what this window is.
|
|
133
|
+
*
|
|
134
|
+
* The ordering is arranged so nothing SHOULD open a connection here anyway:
|
|
135
|
+
* `network.relayAddrs` resolves to a listener that dials nothing and reserves
|
|
136
|
+
* after bring-up (`relay-addrs.ts`), and the control-cohort reconcile pass is
|
|
137
|
+
* scheduled post-start. This window is what makes that a property rather than an
|
|
138
|
+
* accident of ordering — the live case it actually catches is
|
|
139
|
+
* `controlNetwork.bootstrapNodes`, where `@libp2p/bootstrap` emits its discovery
|
|
140
|
+
* events ~1 s after `libp2p.start()` and the connection manager auto-dials from
|
|
141
|
+
* there, which is a race bring-up wins only while raw-storage latency stays low.
|
|
142
|
+
*
|
|
143
|
+
* A denial here costs a retry, not a partition: libp2p's connection manager
|
|
144
|
+
* re-dials on its auto-dial cadence, the reservation supervisor re-drives, and a
|
|
145
|
+
* denied inbound peer reconnects. The window opens on bring-up FAILURE too (via
|
|
146
|
+
* `CadreNode.cleanup`), so teardown is never gated.
|
|
147
|
+
*
|
|
73
148
|
* ## Fail-open, deliberately
|
|
74
149
|
*
|
|
75
|
-
*
|
|
76
|
-
* outsider while no stranger path is open". Any error, missing
|
|
77
|
-
* ambiguous state admits the connection and
|
|
78
|
-
* gates — a DB hiccup must not partition a
|
|
150
|
+
* Outside that window this layer only ever denies on a POSITIVE determination of
|
|
151
|
+
* "unauthorized outsider while no stranger path is open". Any error, missing
|
|
152
|
+
* dependency, or ambiguous state admits the connection (and the reservation) and
|
|
153
|
+
* defers to the fail-closed stream gates — a DB hiccup must not partition a
|
|
154
|
+
* legitimate cadre.
|
|
79
155
|
*/
|
|
80
156
|
import debug from 'debug';
|
|
81
157
|
import { SEED_PROTOCOL } from './seed-bootstrap.js';
|
|
@@ -97,7 +173,8 @@ export const STRANGER_OPEN_PROTOCOLS = [SEED_PROTOCOL, FORMATION_PROTOCOL];
|
|
|
97
173
|
*/
|
|
98
174
|
export const DEFAULT_ENROLLMENT_WINDOW_MS = 30 * 60 * 1000;
|
|
99
175
|
/**
|
|
100
|
-
* Deadline for one admission decision, after which the
|
|
176
|
+
* Deadline for one admission decision, after which the fail-open outcome is
|
|
177
|
+
* used (connection admitted / reservation admitted).
|
|
101
178
|
*
|
|
102
179
|
* libp2p awaits `denyInboundEncryptedConnection` inside the inbound upgrade
|
|
103
180
|
* WITHOUT racing its inbound-upgrade timeout signal (unlike the pre-encryption
|
|
@@ -110,16 +187,118 @@ export const DEFAULT_ENROLLMENT_WINDOW_MS = 30 * 60 * 1000;
|
|
|
110
187
|
* failing closed (or not at all).
|
|
111
188
|
*/
|
|
112
189
|
export const ADMISSION_DECISION_TIMEOUT_MS = 2000;
|
|
190
|
+
/**
|
|
191
|
+
* How long an `'admit-for-relay'` connection may exist without a relay
|
|
192
|
+
* reservation being ADMITTED at the `denyInboundRelayReservation` hook, after
|
|
193
|
+
* which the gate aborts the underlying connection. A reserving client asks for
|
|
194
|
+
* its slot immediately after the connection upgrades (`relay-reservation.ts`
|
|
195
|
+
* dials and requests in one drive; a strand node's configured circuit listener
|
|
196
|
+
* does the same from inside its own `listen()`), so a connection idle past this
|
|
197
|
+
* deadline is not reserving.
|
|
198
|
+
*/
|
|
199
|
+
export const RELAY_ADMISSION_RESERVE_DEADLINE_MS = 5000;
|
|
200
|
+
/**
|
|
201
|
+
* Default cap on concurrent relay reservations held by peers the membership
|
|
202
|
+
* check could not place (see {@link UnauthorizedReservationBudget}). Small on
|
|
203
|
+
* purpose: it exists for the handful of genuine members whose rows are still
|
|
204
|
+
* in flight, not as public relay capacity. Overridable per node via
|
|
205
|
+
* `network.unauthorizedRelayReservationCap` (0 refuses every unauthorized
|
|
206
|
+
* reservation — the strict pre-seam posture).
|
|
207
|
+
*
|
|
208
|
+
* NOTE: this cap shares the relay server's own reservation store, whose default
|
|
209
|
+
* size is 15 (`@libp2p/circuit-relay-v2`'s `DEFAULT_MAX_RESERVATION_STORE_SIZE`);
|
|
210
|
+
* unplaced peers may therefore occupy up to this many of those slots, and the
|
|
211
|
+
* rest are what members and delegates compete for. This module cannot read the
|
|
212
|
+
* server's size, so the two are kept apart by hand — if either is raised, keep
|
|
213
|
+
* this one well under the store's, or a fleet of unplaceable peers can crowd
|
|
214
|
+
* genuine members out of the server's own store (which the gate cannot override).
|
|
215
|
+
*/
|
|
216
|
+
export const MAX_UNAUTHORIZED_RELAY_RESERVATIONS = 8;
|
|
217
|
+
/**
|
|
218
|
+
* How long one {@link UnauthorizedReservationBudget} entry occupies a slot
|
|
219
|
+
* without a refresh. Mirrors the relay server's own default reservation TTL
|
|
220
|
+
* (`@libp2p/circuit-relay-v2`'s `DEFAULT_MAX_RESERVATION_TTL`, 2 h): the server
|
|
221
|
+
* holds an unrefreshed reservation exactly that long, and a live reserver
|
|
222
|
+
* re-requests (re-hitting the admission hook, refreshing its entry) well before
|
|
223
|
+
* expiry — so the live entry count tracks the server's own occupancy without
|
|
224
|
+
* this module reaching into the server's reservation store.
|
|
225
|
+
*
|
|
226
|
+
* NOTE: a mirror, not a live coupling — if the relay server is ever configured
|
|
227
|
+
* with a non-default `reservationTtl` (db-p2p's `relayServerInit`), update this
|
|
228
|
+
* constant alongside it or the budget's occupancy drifts from the server's.
|
|
229
|
+
*/
|
|
230
|
+
export const UNAUTHORIZED_RESERVATION_TTL_MS = 2 * 60 * 60 * 1000;
|
|
231
|
+
/**
|
|
232
|
+
* Bounded budget of concurrent relay reservations for peers the membership
|
|
233
|
+
* check could not place. `tryAdmit` is the whole surface: an already-admitted
|
|
234
|
+
* peer refreshes its entry (a reservation refresh never double-counts), a new
|
|
235
|
+
* peer takes a free slot or is refused. Entries expire after `ttlMs` (see
|
|
236
|
+
* {@link UNAUTHORIZED_RESERVATION_TTL_MS} for why that mirrors the relay
|
|
237
|
+
* server's own hold). Injectable `now` keeps expiry testable without fake
|
|
238
|
+
* timers. Authorized members and delegates are never run through this — the
|
|
239
|
+
* policy admits them before consulting the budget, and `release`s the slot such
|
|
240
|
+
* a peer took while it was still unplaceable, so the boot-ordering window a
|
|
241
|
+
* member passes through costs the budget nothing once its row lands.
|
|
242
|
+
*/
|
|
243
|
+
export class UnauthorizedReservationBudget {
|
|
244
|
+
constructor(cap = MAX_UNAUTHORIZED_RELAY_RESERVATIONS, ttlMs = UNAUTHORIZED_RESERVATION_TTL_MS) {
|
|
245
|
+
this.cap = cap;
|
|
246
|
+
this.ttlMs = ttlMs;
|
|
247
|
+
this.admitted = new Map();
|
|
248
|
+
}
|
|
249
|
+
/** Number of live (unexpired at last prune) entries — test/diagnostic surface. */
|
|
250
|
+
get size() {
|
|
251
|
+
return this.admitted.size;
|
|
252
|
+
}
|
|
253
|
+
/** Admit (or refresh) `remotePeerId` if a slot is free; false when the cap is spent. */
|
|
254
|
+
tryAdmit(remotePeerId, now = Date.now()) {
|
|
255
|
+
this.prune(now);
|
|
256
|
+
if (!this.admitted.has(remotePeerId) && this.admitted.size >= this.cap) {
|
|
257
|
+
log('Unauthorized-reservation budget spent (%d/%d) — refusing %s', this.admitted.size, this.cap, remotePeerId);
|
|
258
|
+
return false;
|
|
259
|
+
}
|
|
260
|
+
this.admitted.set(remotePeerId, now + this.ttlMs);
|
|
261
|
+
return true;
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Give back the slot `remotePeerId` holds, if any — called when the peer has
|
|
265
|
+
* become admissible on its own merits (its membership row replicated, or a
|
|
266
|
+
* delegate grant landed), so the slot it took during the boot-ordering window
|
|
267
|
+
* does not stay spent for the remaining TTL on a peer that no longer needs it.
|
|
268
|
+
*/
|
|
269
|
+
release(remotePeerId) {
|
|
270
|
+
this.admitted.delete(remotePeerId);
|
|
271
|
+
}
|
|
272
|
+
prune(now) {
|
|
273
|
+
for (const [peerId, expiresAt] of this.admitted) {
|
|
274
|
+
if (expiresAt <= now) {
|
|
275
|
+
this.admitted.delete(peerId);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
113
280
|
/**
|
|
114
281
|
* Build the control node's connection gater: the caller-supplied gater (if any)
|
|
115
282
|
* with membership admission composed onto `denyInboundEncryptedConnection` —
|
|
116
|
-
* the earliest checkpoint where the remote's authenticated PeerId is known
|
|
283
|
+
* the earliest checkpoint where the remote's authenticated PeerId is known —
|
|
284
|
+
* and reservation admission composed onto `denyInboundRelayReservation` — the
|
|
285
|
+
* hook the circuit-relay server consults per RESERVE request (inert on a node
|
|
286
|
+
* whose relay server is off; libp2p never calls it there).
|
|
287
|
+
*
|
|
288
|
+
* `denyDialPeer` is composed too, but ONLY for the bring-up quiet period (see
|
|
289
|
+
* the module doc): outside that window outbound dials are never gated by
|
|
290
|
+
* membership — this node decides for itself who to talk to.
|
|
291
|
+
*
|
|
292
|
+
* Composition semantics: every hook of `base` is preserved as-is; on the three
|
|
293
|
+
* composed hooks a deny from EITHER the base gater or the admission policy
|
|
294
|
+
* denies. A policy error — or a decision slower than `decisionTimeoutMs` —
|
|
295
|
+
* takes the fail-open outcome (connection admitted / reservation admitted, see
|
|
296
|
+
* module doc); the base gater's verdict is still honored first.
|
|
117
297
|
*
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
* `
|
|
122
|
-
* verdict is still honored first.
|
|
298
|
+
* An `'admit-for-relay'` verdict admits the connection and arms a
|
|
299
|
+
* `reserveDeadlineMs` timer against it; the timer is disarmed when a
|
|
300
|
+
* reservation for that peer is ADMITTED at the reservation hook, and aborts the
|
|
301
|
+
* underlying `MultiaddrConnection` when it fires first.
|
|
123
302
|
*
|
|
124
303
|
* NOTE: `base` is spread, so a gater passed as a CLASS INSTANCE would lose its
|
|
125
304
|
* prototype methods; every caller in this repo (and libp2p's own default)
|
|
@@ -136,39 +315,80 @@ export const ADMISSION_DECISION_TIMEOUT_MS = 2000;
|
|
|
136
315
|
* Control node only: strand cohort nodes legitimately connect cross-party
|
|
137
316
|
* peers, so `CadreNode` threads the raw configured gater to them unchanged.
|
|
138
317
|
*/
|
|
139
|
-
export function createMembershipConnectionGater(policy, base, decisionTimeoutMs = ADMISSION_DECISION_TIMEOUT_MS) {
|
|
318
|
+
export function createMembershipConnectionGater(policy, base, decisionTimeoutMs = ADMISSION_DECISION_TIMEOUT_MS, reserveDeadlineMs = RELAY_ADMISSION_RESERVE_DEADLINE_MS) {
|
|
319
|
+
const pendingReservations = new PendingReserveDeadlines(reserveDeadlineMs);
|
|
320
|
+
const quiet = () => policy.bringUpInFlight?.() ?? false;
|
|
140
321
|
return {
|
|
141
322
|
...base,
|
|
323
|
+
denyDialPeer: async (peerId) => {
|
|
324
|
+
if (await base?.denyDialPeer?.(peerId)) {
|
|
325
|
+
return true;
|
|
326
|
+
}
|
|
327
|
+
if (quiet()) {
|
|
328
|
+
log('Control-database bring-up in flight — refusing the outbound dial to %s', peerId.toString());
|
|
329
|
+
return true;
|
|
330
|
+
}
|
|
331
|
+
return false;
|
|
332
|
+
},
|
|
142
333
|
denyInboundEncryptedConnection: async (peerId, maConn) => {
|
|
143
334
|
if (await base?.denyInboundEncryptedConnection?.(peerId, maConn)) {
|
|
144
335
|
return true;
|
|
145
336
|
}
|
|
337
|
+
if (quiet()) {
|
|
338
|
+
log('Control-database bring-up in flight — refusing the inbound connection from %s', peerId.toString());
|
|
339
|
+
return true;
|
|
340
|
+
}
|
|
146
341
|
const remotePeerId = peerId.toString();
|
|
342
|
+
let verdict;
|
|
147
343
|
try {
|
|
148
|
-
|
|
344
|
+
verdict = await decideWithinDeadline(() => policy.admitInbound(remotePeerId), 'admit', decisionTimeoutMs, `admitInbound(${remotePeerId})`);
|
|
149
345
|
}
|
|
150
346
|
catch (error) {
|
|
151
347
|
log('admitInbound threw for %s — admitting (fail-open; stream gates decide): %o', remotePeerId, error);
|
|
152
348
|
return false;
|
|
153
349
|
}
|
|
350
|
+
if (verdict === 'admit-for-relay') {
|
|
351
|
+
pendingReservations.arm(remotePeerId, maConn);
|
|
352
|
+
return false;
|
|
353
|
+
}
|
|
354
|
+
return verdict === 'deny';
|
|
355
|
+
},
|
|
356
|
+
denyInboundRelayReservation: async (peerId) => {
|
|
357
|
+
if (await base?.denyInboundRelayReservation?.(peerId)) {
|
|
358
|
+
return true;
|
|
359
|
+
}
|
|
360
|
+
const remotePeerId = peerId.toString();
|
|
361
|
+
let admitted;
|
|
362
|
+
try {
|
|
363
|
+
admitted = await decideWithinDeadline(() => policy.admitRelayReservation(remotePeerId), true, decisionTimeoutMs, `admitRelayReservation(${remotePeerId})`);
|
|
364
|
+
}
|
|
365
|
+
catch (error) {
|
|
366
|
+
log('admitRelayReservation threw for %s — admitting (fail-open): %o', remotePeerId, error);
|
|
367
|
+
admitted = true;
|
|
368
|
+
}
|
|
369
|
+
if (admitted) {
|
|
370
|
+
pendingReservations.disarm(remotePeerId);
|
|
371
|
+
}
|
|
372
|
+
return !admitted;
|
|
154
373
|
}
|
|
155
374
|
};
|
|
156
375
|
}
|
|
157
376
|
/**
|
|
158
|
-
* Run
|
|
159
|
-
*
|
|
160
|
-
* an unbounded await is not safe
|
|
161
|
-
* connection
|
|
377
|
+
* Run one admission decision under `timeoutMs`, resolving to `fallback` (the
|
|
378
|
+
* fail-open outcome) if it has not settled in time — see
|
|
379
|
+
* {@link ADMISSION_DECISION_TIMEOUT_MS} for why an unbounded await is not safe
|
|
380
|
+
* on the connection hook. The timer is always cleared so a decided call never
|
|
381
|
+
* holds the event loop open.
|
|
162
382
|
*/
|
|
163
|
-
async function
|
|
383
|
+
async function decideWithinDeadline(decide, fallback, timeoutMs, what) {
|
|
164
384
|
let timer;
|
|
165
385
|
try {
|
|
166
386
|
return await Promise.race([
|
|
167
|
-
Promise.resolve(
|
|
387
|
+
Promise.resolve().then(decide),
|
|
168
388
|
new Promise((resolve) => {
|
|
169
389
|
timer = setTimeout(() => {
|
|
170
|
-
log('
|
|
171
|
-
resolve(
|
|
390
|
+
log('%s exceeded %dms — taking the fail-open outcome (stream gates decide)', what, timeoutMs);
|
|
391
|
+
resolve(fallback);
|
|
172
392
|
}, timeoutMs);
|
|
173
393
|
})
|
|
174
394
|
]);
|
|
@@ -177,4 +397,64 @@ async function admitWithinDeadline(policy, remotePeerId, timeoutMs) {
|
|
|
177
397
|
clearTimeout(timer);
|
|
178
398
|
}
|
|
179
399
|
}
|
|
400
|
+
/**
|
|
401
|
+
* The not-reserving deadlines for `'admit-for-relay'` connections, keyed by
|
|
402
|
+
* remote peerId (a Set per peer — one peer can hold several in-flight
|
|
403
|
+
* connections). `arm` starts a timer that aborts the connection; `disarm`
|
|
404
|
+
* (called when a reservation for that peer is admitted) cancels every pending
|
|
405
|
+
* timer for the peer. Timers are unref'd so an armed deadline never holds a
|
|
406
|
+
* process open, and a timer that fires against an already-closed connection is
|
|
407
|
+
* a swallowed no-op.
|
|
408
|
+
*
|
|
409
|
+
* Disarming is final for the connections it cancelled: a peer that reserved
|
|
410
|
+
* once and then lets its reservation lapse keeps a mute connection (the
|
|
411
|
+
* fail-closed stream gates refuse it every members-only protocol) until either
|
|
412
|
+
* side closes it. Bounded — only a peer the reservation policy already admitted
|
|
413
|
+
* can reach that state, so unplaceable peers are bounded by the budget cap.
|
|
414
|
+
*/
|
|
415
|
+
class PendingReserveDeadlines {
|
|
416
|
+
constructor(deadlineMs) {
|
|
417
|
+
this.deadlineMs = deadlineMs;
|
|
418
|
+
this.byPeer = new Map();
|
|
419
|
+
}
|
|
420
|
+
arm(remotePeerId, maConn) {
|
|
421
|
+
const entry = {
|
|
422
|
+
maConn,
|
|
423
|
+
timer: setTimeout(() => this.expire(remotePeerId, entry), this.deadlineMs)
|
|
424
|
+
};
|
|
425
|
+
entry.timer.unref?.();
|
|
426
|
+
let entries = this.byPeer.get(remotePeerId);
|
|
427
|
+
if (!entries) {
|
|
428
|
+
entries = new Set();
|
|
429
|
+
this.byPeer.set(remotePeerId, entries);
|
|
430
|
+
}
|
|
431
|
+
entries.add(entry);
|
|
432
|
+
log('Admitted %s for relay only — dropping the connection unless a reservation is admitted within %dms', remotePeerId, this.deadlineMs);
|
|
433
|
+
}
|
|
434
|
+
disarm(remotePeerId) {
|
|
435
|
+
const entries = this.byPeer.get(remotePeerId);
|
|
436
|
+
if (!entries) {
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
this.byPeer.delete(remotePeerId);
|
|
440
|
+
for (const entry of entries) {
|
|
441
|
+
clearTimeout(entry.timer);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
expire(remotePeerId, entry) {
|
|
445
|
+
const entries = this.byPeer.get(remotePeerId);
|
|
446
|
+
entries?.delete(entry);
|
|
447
|
+
if (entries?.size === 0) {
|
|
448
|
+
this.byPeer.delete(remotePeerId);
|
|
449
|
+
}
|
|
450
|
+
log('Relay-only admission expired for %s — no reservation admitted within %dms, aborting the connection', remotePeerId, this.deadlineMs);
|
|
451
|
+
try {
|
|
452
|
+
entry.maConn.abort(new Error(`relay-only admission expired: no relay reservation admitted within ${this.deadlineMs}ms`));
|
|
453
|
+
}
|
|
454
|
+
catch (error) {
|
|
455
|
+
// Aborting a connection that already closed on its own is not a failure.
|
|
456
|
+
log('Aborting the expired relay-only connection from %s threw: %o', remotePeerId, error);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
}
|
|
180
460
|
//# sourceMappingURL=membership-connection-gater.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"membership-connection-gater.js","sourceRoot":"","sources":["../src/membership-connection-gater.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"membership-connection-gater.js","sourceRoot":"","sources":["../src/membership-connection-gater.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0JG;AAEH,OAAO,KAAK,MAAM,OAAO,CAAC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AE,IAAI,KAAK,CAAC,EAAE,CAAC;YACxB,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;QACnC,CAAC;QACD,GAAG,CAAC,oGAAoG,EAAE,YAAY,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;QACzI,IAAI,CAAC;YACH,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,sEAAsE,IAAI,CAAC,UAAU,IAAI,CAAC,CAAC,CAAC;QAC3H,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,yEAAyE;YACzE,GAAG,CAAC,8DAA8D,EAAE,YAAY,EAAE,KAAK,CAAC,CAAC;QAC3F,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Merging verified cadre addresses into a libp2p node's **address book** (its
|
|
3
|
+
* peerStore), so that every layer below cadre-core which dials by bare peer id
|
|
4
|
+
* — Optimystic's cluster client, repo client, FRET ping/announce, all funnelling
|
|
5
|
+
* into `libp2p.dialProtocol(peerId, …)` — has an address to use for a sibling
|
|
6
|
+
* whose connection has dropped, instead of failing with `NoValidAddressesError`
|
|
7
|
+
* until the next reconcile pass happens to re-dial it.
|
|
8
|
+
*
|
|
9
|
+
* Shared by the control-cohort reconcile pass and the strand one (both in
|
|
10
|
+
* `CadreNode`): each resolves a peer's addresses its own way and then needs
|
|
11
|
+
* exactly this write, with exactly this workaround.
|
|
12
|
+
*
|
|
13
|
+
* Two exports, in the order a caller uses them: {@link groupAddrsByPeerId} turns
|
|
14
|
+
* a flat address list into per-peer groups (the strand path starts from the
|
|
15
|
+
* peer-agnostic union the strand-addr RPC returns), and {@link mergePeerAddrs}
|
|
16
|
+
* writes one peer's group.
|
|
17
|
+
*/
|
|
18
|
+
import type { PeerId, PeerStore } from '@libp2p/interface';
|
|
19
|
+
import { type Multiaddr } from '@multiformats/multiaddr';
|
|
20
|
+
/** Outcome of one address-book merge, for logging and tests. */
|
|
21
|
+
export type MergeAddrsResult = 'merged' | 'restamped' | 'skipped' | 'failed';
|
|
22
|
+
/**
|
|
23
|
+
* The slice of a libp2p node {@link mergePeerAddrs} touches. Structurally
|
|
24
|
+
* satisfied by `Libp2p`; narrow so a caller (or a test) can pass a bare
|
|
25
|
+
* peerStore holder.
|
|
26
|
+
*/
|
|
27
|
+
export interface PeerAddrBookHost {
|
|
28
|
+
peerStore: PeerStore;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Write `addrs` into `host`'s address book for `peerId`, best-effort. `peerId`
|
|
32
|
+
* may be the string form callers hold (a `CadrePeer.PeerId` column, a strand-addr
|
|
33
|
+
* RPC result); parsing it is part of the best-effort contract below, so no caller
|
|
34
|
+
* has to wrap this in a second try/catch of its own.
|
|
35
|
+
*
|
|
36
|
+
* - **Empty `addrs` writes nothing** and reports `'skipped'`. That is how a
|
|
37
|
+
* revoked / stale / untrusted peer's existing entry is allowed to age out on
|
|
38
|
+
* its own: the caller resolves `[]` for it, and nothing here refreshes it.
|
|
39
|
+
* - Otherwise the addresses are merged, and — see the timestamp trap below —
|
|
40
|
+
* re-saved when the merge alone leaves them invisible (`'restamped'`).
|
|
41
|
+
* - Any throw (unparsable peer id, datastore failure, a peerStore double without
|
|
42
|
+
* `merge`) is logged and folded to `'failed'`. Callers are best-effort loops:
|
|
43
|
+
* an address-book failure must never abort a reconcile pass.
|
|
44
|
+
*
|
|
45
|
+
* NOTE: `peerStore.merge` cannot refresh an address's `observed` timestamp in
|
|
46
|
+
* `@libp2p/peer-store` 12.0.10 — `to-peer-pb.js` shadows its own loop variable
|
|
47
|
+
* (`…addresses?.find(addr => uint8ArrayEquals(addr.multiaddr, addr.multiaddr))`,
|
|
48
|
+
* always true, so it returns the FIRST stored address's timestamp), and every
|
|
49
|
+
* read filters out addresses older than `MAX_ADDRESS_AGE` (1 hour). So an entry
|
|
50
|
+
* silently dies at the one-hour mark and no amount of re-merging revives it —
|
|
51
|
+
* not even merging a brand-new address. The `save` below is the workaround:
|
|
52
|
+
* `save` is the one write path that omits `existingPeer` and therefore stamps
|
|
53
|
+
* `Date.now()`. Drop it once upstream stamps `Date.now()` on merge.
|
|
54
|
+
*
|
|
55
|
+
* NOTE: raising the store's own `maxAddressAge` (libp2p forwards `init.peerStore`
|
|
56
|
+
* straight into `persistentPeerStore`) would sidestep the bug in one line, and is
|
|
57
|
+
* deliberately NOT done: expiry is load-bearing here. Only *verified* addresses
|
|
58
|
+
* come through this helper, and the design relies on everything else — the
|
|
59
|
+
* cold-start seed entries, identify-learned addresses — still ageing out on the
|
|
60
|
+
* stock schedule. A global age bump would keep those alive too.
|
|
61
|
+
*
|
|
62
|
+
* NOTE: if the store ever REJECTS an address we hand it, the restamp repeats
|
|
63
|
+
* every pass forever — `allVisible` stays false, and `save` re-submits the same
|
|
64
|
+
* rejected address. libp2p wires the store's `addressFilter` to
|
|
65
|
+
* `connectionGater.filterMultiaddrForPeer`, which nothing in this repo implements
|
|
66
|
+
* today (`createMembershipConnectionGater` gates dials, not addresses), so the
|
|
67
|
+
* loop is unreachable. If an app ever supplies that gater hook via
|
|
68
|
+
* `NetworkConfig.connectionGater`, bound the retry — the cost is one redundant
|
|
69
|
+
* datastore write per gated sibling per reconcile pass.
|
|
70
|
+
*/
|
|
71
|
+
export declare function mergePeerAddrs(host: PeerAddrBookHost, peerId: PeerId | string, addrs: Multiaddr[]): Promise<MergeAddrsResult>;
|
|
72
|
+
/**
|
|
73
|
+
* Group multiaddr strings by the peer id in their final `/p2p/` component — the
|
|
74
|
+
* attribution step between a flat, peer-agnostic address list and the per-peer
|
|
75
|
+
* writes {@link mergePeerAddrs} takes.
|
|
76
|
+
*
|
|
77
|
+
* The **last** `/p2p/` component is the addressed peer, not the first: a relayed
|
|
78
|
+
* address (`…/p2p/<relay>/p2p-circuit/p2p/<dst>`) names the relay first and the
|
|
79
|
+
* destination last, while a direct address (`…/tcp/…/p2p/<dst>`) names only the
|
|
80
|
+
* destination. Taking the last therefore attributes both shapes to the peer the
|
|
81
|
+
* address actually reaches.
|
|
82
|
+
*
|
|
83
|
+
* Three shapes are dropped rather than attributed, because none of them names a
|
|
84
|
+
* peer the address reaches: one that does not parse, one carrying no `/p2p/`
|
|
85
|
+
* component at all (a peer advertising a bare listen addr), and one whose
|
|
86
|
+
* `p2p-circuit` comes AFTER its last `/p2p/` (`…/p2p/<relay>/p2p-circuit`) — that
|
|
87
|
+
* last one is a relay hop with the destination missing, so its trailing peer id
|
|
88
|
+
* is the RELAY's and filing it under the relay would claim the relay is reachable
|
|
89
|
+
* at a circuit leading nowhere. Duplicates collapse within a group. Insertion
|
|
90
|
+
* order is preserved, both between groups and inside one.
|
|
91
|
+
*/
|
|
92
|
+
export declare function groupAddrsByPeerId(addrs: string[]): Map<string, Multiaddr[]>;
|