@serfab/cadre-core 1.10.0 → 1.11.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 (61) hide show
  1. package/dist/cadre-node.d.ts +0 -27
  2. package/dist/cadre-node.js +2 -94
  3. package/dist/cadre-node.js.map +1 -1
  4. package/dist/control-schema.js +1066 -1066
  5. package/dist/diagnostics/connection-path.d.ts +1 -18
  6. package/dist/diagnostics/connection-path.js +1 -20
  7. package/dist/diagnostics/connection-path.js.map +1 -1
  8. package/dist/identity-key.d.ts +0 -30
  9. package/dist/identity-key.js +7 -40
  10. package/dist/identity-key.js.map +1 -1
  11. package/dist/index.d.ts +3 -2
  12. package/dist/index.js +6 -4
  13. package/dist/index.js.map +1 -1
  14. package/dist/relay-stun.d.ts +24 -0
  15. package/dist/relay-stun.js +51 -0
  16. package/dist/relay-stun.js.map +1 -0
  17. package/dist/seed-bootstrap.js +4 -4
  18. package/dist/strand-membership-writer.js +32 -32
  19. package/package.json +2 -2
  20. package/src/cadre-node.ts +8654 -8753
  21. package/src/control-authorization.ts +117 -117
  22. package/src/control-cohort.ts +128 -128
  23. package/src/control-formation-recorder.ts +353 -353
  24. package/src/control-read-retry.ts +216 -216
  25. package/src/control-schema.ts +1078 -1078
  26. package/src/control-write-retry.ts +567 -567
  27. package/src/diagnostics/connection-path.ts +1 -29
  28. package/src/formation-approval.ts +610 -610
  29. package/src/hibernation-manager.ts +638 -638
  30. package/src/identity-key.ts +7 -62
  31. package/src/index.ts +644 -647
  32. package/src/invitation-bootstrap.ts +40 -40
  33. package/src/joined-strand-store.ts +498 -498
  34. package/src/link-budget.ts +684 -684
  35. package/src/membership-connection-gater.ts +570 -570
  36. package/src/node-local-snapshot.ts +298 -298
  37. package/src/peer-dial.ts +315 -315
  38. package/src/peer-join-backfill.ts +710 -710
  39. package/src/pending-join-runner.ts +735 -735
  40. package/src/relay-reservation.ts +1121 -1121
  41. package/src/relay-server.ts +98 -98
  42. package/src/relay-stun.ts +58 -0
  43. package/src/seed-bootstrap.ts +1448 -1448
  44. package/src/strand-addr-protocol.ts +547 -547
  45. package/src/strand-database.ts +287 -287
  46. package/src/strand-first-sync-gate.ts +391 -391
  47. package/src/strand-formation-deadlines.ts +227 -227
  48. package/src/strand-formation-manager.ts +725 -725
  49. package/src/strand-fret-addrs.ts +145 -145
  50. package/src/strand-instance-manager.ts +1789 -1789
  51. package/src/strand-membership-writer.ts +1974 -1974
  52. package/src/strand-network-state-file.ts +40 -40
  53. package/src/strand-network-state.ts +243 -243
  54. package/src/strand-revocation-enforcer.ts +654 -654
  55. package/src/strand-solicitation.ts +532 -532
  56. package/src/strand-wake-protocol.ts +401 -401
  57. package/src/types.ts +2222 -2222
  58. package/dist/diagnostics/webrtc-turn-tracker.d.ts +0 -50
  59. package/dist/diagnostics/webrtc-turn-tracker.js +0 -196
  60. package/dist/diagnostics/webrtc-turn-tracker.js.map +0 -1
  61. package/src/diagnostics/webrtc-turn-tracker.ts +0 -222
@@ -1,654 +1,654 @@
1
- /**
2
- * Strand revoked-peer enforcement: a per-strand deny gate that refuses the
3
- * network nodes of a REMOVED party, so removal means "we stop talking to you"
4
- * rather than only deleting a row.
5
- *
6
- * ## The deny set is revocation-keyed, never membership-keyed
7
- *
8
- * A strand node maintains a materialized in-memory set of revoked peer ids:
9
- * every `Strand.MemberPeer.PeerId` whose `MemberKey` has no live `Strand.Member`
10
- * row. `MemberPeer.MemberExists` runs on insert, so an orphaned binding can only
11
- * mean "this member existed and was removed" — it is exactly the durable,
12
- * replicated record of a removed party's machines. Everything NOT in the set is
13
- * admitted. Deliberate consequences:
14
- *
15
- * - **Open strands are untouched.** `Member`/`MemberPeer` carry `OnlyClosed`
16
- * checks, so an open strand has no rows and the set is empty — and
17
- * `StrandInstanceManager` additionally skips arming this enforcer entirely for
18
- * `Type !== 'c'`, so an open strand does not even poll.
19
- * - **A joining party mid-admission is admitted** (it has no orphaned binding).
20
- * An allowlist ("only peers bound to live members") was considered and
21
- * rejected: a joiner's first membership writes must travel through the very
22
- * cohort an allowlist would close to it — the same mutual-denial trap the
23
- * control network's gater doc describes (`membership-connection-gater.ts`),
24
- * with no enrollment window to carve out. A deny-list on positive evidence
25
- * has no such trap.
26
- * - **Stale-view failure direction: fail-open.** A remaining member that has not
27
- * yet replicated the revocation keeps serving the removed party until the
28
- * tombstoned state arrives — bounded by replication, consistent with
29
- * "revocation is forward-looking". The opposite error (denying a legitimate
30
- * member) is only reachable for a revoked-then-re-admitted party at a node
31
- * that saw the revoke but not the re-add; the re-add gives the member key a
32
- * live `Member` row again, which removes its peers from the deny set as the
33
- * row replicates — self-healing, and the re-adding manager's own node updates
34
- * first, so the heal never depends on talking to the peer being wrongly denied.
35
- * - **Orphaned `MemberPeer` rows are load-bearing**: they ARE the deny record —
36
- * the `Strand.Revocation` tombstone keeps only the stamp, not the peer id — so
37
- * nothing here (or anywhere) tombstones or auto-cleans them at revocation.
38
- * The manager-cleanup path (`removeMemberPeer`'s manager branch) stays as the
39
- * only way to clear a wrongly-registered binding, and clearing an orphan
40
- * forgets the denial (the NOTE at that branch says so).
41
- *
42
- * ## Removal cuts LIVE sessions too — the refresh IS the teardown seam
43
- *
44
- * Refusing NEW connections only half-honors "no longer be answered to or
45
- * communicated with": a long-lived strand connection may never re-dial, so
46
- * without teardown a removed party keeps whatever session it already holds.
47
- * There is no push notification to hang teardown off — a REPLICATED revocation
48
- * arrives with no change event (the strand database has no reactive
49
- * subscription; `StrandWatcher` polls for the same reason), and a LOCAL
50
- * `revokeMember`/`leaveStrand` writes through a bare `Database` handle this
51
- * runtime never sees — so teardown rides the snapshot refresh itself, with
52
- * `refresh()` exposed for immediacy
53
- * (`StrandInstanceManager.refreshRevocationEnforcement` →
54
- * `CadreNode.refreshRevocationEnforcement`).
55
- *
56
- * The sweep is driven by MEMBERSHIP in the deny set, never by an
57
- * entered-the-set diff: every refresh hangs up every currently-connected
58
- * revoked peer, so a resume, a crashed refresh, or a connection established
59
- * between two refreshes cannot leave a session standing (a diff would be a
60
- * strict subset of this, so none is kept). A peer with no open connection is
61
- * simply not swept, and a failed `hangUp` is logged, never thrown — the next
62
- * refresh retries it. `hangUp` closes DIRECT and RELAYED connections alike, and
63
- * on a strand node running the relay server it also drops any reservation the
64
- * revoked peer holds there (a reservation dies with its connection — see the
65
- * relay-seam doc in `membership-connection-gater.ts`).
66
- *
67
- * Teardown is also what stops OUTBOUND traffic. The gate is one-directional —
68
- * `authorizeStream` judges streams the revoked peer opens to US — so nothing
69
- * else prevents this node from pushing over an already-open connection, which
70
- * `PeerJoinBackfill` does for that connection's whole life. Closing the
71
- * connection stops both directions at once, which is why no separate outbound
72
- * gate exists.
73
- *
74
- * If this node's OWN peer id is in the set, it was itself removed (or it left —
75
- * a voluntary departure files the same tombstone). Nothing is torn down for
76
- * that: the other side's gate already refuses us, and every other revoked peer
77
- * is still swept. Instead `onSelfRevoked` fires ONCE per entry into the set
78
- * (re-armed if the party is re-admitted, so a second removal signals again),
79
- * which `CadreNode` surfaces as `strand:revoked` so the embedding app decides
80
- * what to do — this module never stops or leaves a strand on its own.
81
- * Best-effort by nature: a removed party learns only if the tombstone
82
- * replicated to it before the cut.
83
- *
84
- * ## What transfers from the control gater (`membership-connection-gater.ts`)
85
- *
86
- * Transfers: the two-layer shape (fail-closed per-stream gate as PRIMARY, an
87
- * opportunistic connection-level deny on top), the synchronous in-memory
88
- * snapshot judged by the stream gate (a live DB read inside a gate deadlocks —
89
- * the same argument as the control node's `authorizeInboundControlStream`), the
90
- * fail-open-on-error/timeout posture for the connection hooks
91
- * (`decideWithinDeadline`, reused as-is), and the "compose over the caller's
92
- * gater, every base hook preserved" mechanics.
93
- *
94
- * Does NOT transfer — the relay-reservation seam (`'admit-for-relay'` + the
95
- * unauthorized budget). That machinery exists because the control gate denies on
96
- * ABSENCE of placement, where a member whose row is in flight is
97
- * indistinguishable from an outsider and a wrong reservation deny is
98
- * unrecoverable. This gate denies on POSITIVE revocation evidence, so a
99
- * reservation deny is never that kind of wrong answer; the one stale-wrong case
100
- * (a missed re-add) heals via replication as above. `denyInboundRelayReservation`
101
- * here is therefore a plain "deny iff revoked" — no budget, no admit-for-relay
102
- * verdict, no reserve deadline.
103
- *
104
- * Does NOT transfer — the bring-up quiet period. A strand node's DB bring-up
105
- * NEEDS its cross-party cohort connections; this enforcer simply starts with an
106
- * empty snapshot (admit everything) until its first successful read — the same
107
- * fail-open posture as everywhere else here.
108
- *
109
- * Does NOT transfer — every stranger carve-out (enrollment window, formation
110
- * window, delegate grants). None of those protocols exist on a strand node.
111
- *
112
- * Unlike the control stream gate, no empty-snapshot carve-out is needed: an
113
- * empty deny set means "nothing revoked", which correctly admits everyone — the
114
- * deny-list shape makes the cold-start case and the steady case the same code
115
- * path.
116
- */
117
-
118
- import debug from 'debug';
119
- import type { ConnectionGater, PeerId, MultiaddrConnection } from '@libp2p/interface';
120
- import type { Database } from '@quereus/quereus';
121
- import { decideWithinDeadline } from './membership-connection-gater.js';
122
- import { ADMISSION_DECISION_TIMEOUT_MS } from './link-budget.js';
123
-
124
- const log = debug('sereus:cadre:strand-revocation');
125
-
126
- /**
127
- * Default cadence of the deny-set refresh. Bounded staleness is the accepted
128
- * failure direction (see the module doc's stale-view paragraph); 30 s keeps a
129
- * revoked peer's post-replication admit window in the same order as the
130
- * replication delay itself. Overridable via
131
- * {@link StrandRevocationEnforcementConfig.pollIntervalMs}.
132
- */
133
- export const DEFAULT_REVOCATION_POLL_INTERVAL_MS = 30_000;
134
-
135
- /**
136
- * Embedder tuning for the strand revoked-peer gate, threaded from
137
- * `CadreNodeConfig.strandRevocationEnforcement` to
138
- * `StartStrandConfig.revocationEnforcement` the way the backfill knob is.
139
- */
140
- export interface StrandRevocationEnforcementConfig {
141
- /**
142
- * Default true. False disarms the gate entirely for this node's strands —
143
- * the pre-existing behaviour (a removed party's peers keep being served).
144
- */
145
- enabled?: boolean;
146
- /** Deny-set refresh cadence, ms. Default {@link DEFAULT_REVOCATION_POLL_INTERVAL_MS}. */
147
- pollIntervalMs?: number;
148
- }
149
-
150
- /** One `Strand.MemberPeer` row, as the deny-set derivation consumes it. */
151
- export interface StrandMemberPeerBinding {
152
- memberKey: string;
153
- peerId: string;
154
- }
155
-
156
- /** The membership rows one deny-set derivation reads. */
157
- export interface StrandRevocationRows {
158
- /** Every live `Strand.Member.Key`. */
159
- memberKeys: ReadonlySet<string>;
160
- /** Every `Strand.MemberPeer` row (live-membered and orphaned alike). */
161
- bindings: readonly StrandMemberPeerBinding[];
162
- }
163
-
164
- /**
165
- * Timer seam so the refresh cadence is testable without fake timers — the
166
- * injectable-clock idiom of `UnauthorizedReservationBudget`'s `now` argument,
167
- * shaped for an interval-driven component. The default schedules a real
168
- * interval and unrefs it so an armed enforcer never holds a process open.
169
- */
170
- export interface RevocationRefreshScheduler {
171
- setInterval(fn: () => void, ms: number): unknown;
172
- clearInterval(handle: unknown): void;
173
- }
174
-
175
- const defaultScheduler: RevocationRefreshScheduler = {
176
- setInterval: (fn, ms) => {
177
- const handle = setInterval(fn, ms);
178
- (handle as { unref?: () => void }).unref?.();
179
- return handle;
180
- },
181
- clearInterval: (handle) => clearInterval(handle as Parameters<typeof clearInterval>[0])
182
- };
183
-
184
- /**
185
- * The minimum of a libp2p node the teardown sweep needs: who WE are, what is
186
- * open, and how to close it. Deliberately not the node itself, so the sweep
187
- * unit-tests against a three-property stub; `Libp2p` (hence
188
- * `Libp2pNodeWithRepo`) satisfies it structurally, which is what
189
- * `StrandInstanceManager` passes.
190
- */
191
- export interface StrandRevocationNetwork {
192
- /** This node's own peer id — how the enforcer recognizes ITSELF in the deny set. */
193
- readonly peerId: PeerId;
194
- /** Every open connection. Only `remotePeer` is read — and reused as the hangUp target. */
195
- getConnections(): readonly { readonly remotePeer: PeerId }[];
196
- /** Close every connection to `peer`: direct, relayed, and any reservation riding one. */
197
- hangUp(peer: PeerId): Promise<void>;
198
- }
199
-
200
- export interface StrandRevocationEnforcerDeps {
201
- /** Log tag naming which strand this enforcer serves (the strand id). */
202
- label: string;
203
- /**
204
- * Read the membership rows the deny set is derived from. Injectable so the
205
- * enforcer unit-tests without a strand database; production wires
206
- * {@link readStrandRevocationRows} over the strand's live DB. A throw or
207
- * rejection KEEPS the previous snapshot (never clears it) — the control
208
- * model, `refreshAuthorizedControlPeers`.
209
- */
210
- readRows: () => Promise<StrandRevocationRows>;
211
- /**
212
- * Handle on the strand's libp2p node for the teardown sweep, read LAZILY on
213
- * every sweep — never captured. It has to be: the enforcer is constructed
214
- * BEFORE the node (the node's options embed the enforcer's predicates), and
215
- * the node is dropped again on quiesce. Omit it, or return undefined, and the
216
- * gate still refuses new connections — only teardown of OPEN ones is skipped.
217
- */
218
- getNetwork?: () => StrandRevocationNetwork | undefined;
219
- /**
220
- * Called ONCE each time this node's own peer id ENTERS the deny set — i.e.
221
- * this node's party was removed from the strand, or left it. Not re-fired
222
- * while it stays there; re-armed if the party is re-admitted, so a second
223
- * removal signals again. Wired by `StrandInstanceManager` through to
224
- * `CadreNode`'s `strand:revoked` event. A resume rebuilds the enforcer, so a
225
- * still-revoked strand also legitimately re-fires once per resume. A throw is
226
- * logged and swallowed — an embedder's handler must not derail the sweep.
227
- */
228
- onSelfRevoked?: () => void;
229
- /** Timer seam; omit for real (unref'd) intervals. */
230
- scheduler?: RevocationRefreshScheduler;
231
- }
232
-
233
- /** Every `Strand.MemberPeer` binding — live-membered and orphaned alike. */
234
- async function scanBindings(db: Database): Promise<StrandMemberPeerBinding[]> {
235
- const bindings: StrandMemberPeerBinding[] = [];
236
- for await (const row of db.eval('select MemberKey, PeerId from Strand.MemberPeer')) {
237
- bindings.push({ memberKey: row.MemberKey as string, peerId: row.PeerId as string });
238
- }
239
- return bindings;
240
- }
241
-
242
- /** Every live `Strand.Member.Key`. */
243
- async function scanMemberKeys(db: Database): Promise<Set<string>> {
244
- const memberKeys = new Set<string>();
245
- for await (const row of db.eval('select Key from Strand.Member')) {
246
- memberKeys.add(row.Key as string);
247
- }
248
- return memberKeys;
249
- }
250
-
251
- /**
252
- * Read the deny-set inputs from a strand database: full-scan
253
- * `Strand.MemberPeer` and `Strand.Member`, to be joined in JavaScript by the
254
- * enforcer. Scan-and-filter, never seek — the composite-PK point-lookup
255
- * unreliability documented at `scanMemberPeers` in `strand-membership-writer.ts`
256
- * applies, so correctness depends only on each scan returning a superset of the
257
- * live rows.
258
- *
259
- * **Scan order is load-bearing, and it is BINDINGS FIRST.** The two scans are
260
- * separate reads with nothing holding them to one snapshot, so a write that
261
- * lands between them is seen by the second scan and not the first. A member
262
- * joins as `Member` row then `MemberPeer` row (`MemberExists` forces that
263
- * order), so reading `Member` first would let a join that commits mid-read
264
- * produce a binding whose member key is absent from the key set — a brand-new
265
- * member classified as REVOKED, the one fail-CLOSED outcome this module exists
266
- * to avoid. Reading bindings first inverts the skew: the new binding is simply
267
- * not in the older bindings snapshot, so the joiner is unclassified and
268
- * admitted, and a revocation that lands mid-read is still seen (the key is
269
- * missing from the NEWER key scan). Every skew window then resolves fail-open,
270
- * matching the module doc.
271
- *
272
- * NOTE: two whole-table scans per refresh (default every 30 s per closed
273
- * strand). Fine at strand scale (a handful of members and bindings); if
274
- * membership churn ever makes these tables large, the fix is a reliable
275
- * filtered read — which needs the networked point-lookup gap closed
276
- * (`debt-composite-pk-point-lookup-unreliable-untracked`) — not a bigger scan.
277
- */
278
- export async function readStrandRevocationRows(db: Database): Promise<StrandRevocationRows> {
279
- const bindings = await scanBindings(db);
280
- const memberKeys = await scanMemberKeys(db);
281
- return { memberKeys, bindings };
282
- }
283
-
284
- /**
285
- * The per-strand revoked-peer deny set and the synchronous predicates over it.
286
- *
287
- * Lifecycle mirrors `PeerJoinBackfill`: created in
288
- * `StrandInstanceManager.buildStrandRuntime` (closed strands only), started
289
- * after the strand database initializes, stopped and dropped in
290
- * `releaseRuntime` — so quiesce → resume rebuilds it with a fresh snapshot.
291
- * The snapshot starts EMPTY (admit everything) and repopulates on the first
292
- * successful read; a revoked peer therefore gets a short admit window per
293
- * resume at the connection level. Accepted: fail-open posture, and resume is
294
- * never blocked on a refresh.
295
- */
296
- export class StrandRevocationEnforcer {
297
- /** The materialized deny set — peer ids with an orphaned binding and no live one. */
298
- private revokedPeerIds: ReadonlySet<string> = new Set();
299
- private readonly scheduler: RevocationRefreshScheduler;
300
- private readonly pollIntervalMs: number;
301
- /** Tail of the refresh chain — what serializes refreshes (no two reads in flight). */
302
- private refreshTail: Promise<void> = Promise.resolve();
303
- private refreshing = false;
304
- private intervalHandle: unknown;
305
- private started = false;
306
- private stopped = false;
307
- /** Latch for the one-shot self-revocation signal — see {@link StrandRevocationEnforcerDeps.onSelfRevoked}. */
308
- private selfRevokedSignaled = false;
309
-
310
- constructor(
311
- private readonly deps: StrandRevocationEnforcerDeps,
312
- config?: StrandRevocationEnforcementConfig
313
- ) {
314
- this.scheduler = deps.scheduler ?? defaultScheduler;
315
- this.pollIntervalMs = config?.pollIntervalMs ?? DEFAULT_REVOCATION_POLL_INTERVAL_MS;
316
- }
317
-
318
- /** Number of peers currently denied — test/diagnostic surface. */
319
- get revokedCount(): number {
320
- return this.revokedPeerIds.size;
321
- }
322
-
323
- /** Kick an immediate refresh (not awaited — bring-up never blocks on it) and arm the poll. */
324
- start(): void {
325
- if (this.started || this.stopped) return;
326
- this.started = true;
327
- void this.refresh();
328
- this.intervalHandle = this.scheduler.setInterval(() => {
329
- // Skip a tick while a refresh is in flight rather than queueing behind it —
330
- // the interval exists to bound staleness, and a stack of queued reads
331
- // bounds nothing extra. Explicit refresh() calls still chain (see refresh).
332
- if (!this.refreshing) {
333
- void this.refresh();
334
- }
335
- }, this.pollIntervalMs);
336
- log('[%s] revocation enforcer started (poll %dms)', this.deps.label, this.pollIntervalMs);
337
- }
338
-
339
- /** Disarm the poll; a read already in flight completes but its result is discarded. */
340
- stop(): void {
341
- if (this.stopped) return;
342
- this.stopped = true;
343
- if (this.intervalHandle != null) {
344
- this.scheduler.clearInterval(this.intervalHandle);
345
- this.intervalHandle = undefined;
346
- }
347
- log('[%s] revocation enforcer stopped', this.deps.label);
348
- }
349
-
350
- /**
351
- * Refresh the deny set now. Never rejects. Serialized: concurrent calls chain,
352
- * so no two reads overlap, and each explicit call gets a read that STARTS
353
- * after the call (an on-demand refresh after a revocation commit observes that
354
- * commit). A failed or slow read keeps the previous snapshot — never clears it.
355
- */
356
- refresh(): Promise<void> {
357
- const run = this.refreshTail.then(() => this.doRefresh());
358
- this.refreshTail = run;
359
- return run;
360
- }
361
-
362
- /**
363
- * Is `remotePeerId` positively revoked in the current snapshot? Synchronous by
364
- * design — this is judged inside libp2p gates where an await against the DB
365
- * would deadlock (the control stream gate's argument).
366
- */
367
- isRevoked(remotePeerId: string): boolean {
368
- return this.revokedPeerIds.has(remotePeerId);
369
- }
370
-
371
- /**
372
- * The fail-closed PRIMARY layer: the per-stream predicate wired as
373
- * `createLibp2pNode({ authorizeInboundStream })`, gating all four
374
- * `/optimystic/strand-<id>/{repo,cluster,sync,block-transfer}` protocols.
375
- * Also what catches a revoked peer arriving over a RELAYED connection, or
376
- * over a connection admitted before the revocation replicated here.
377
- */
378
- authorizeStream(remotePeerId: string, _protocol: string): boolean {
379
- return !this.isRevoked(remotePeerId);
380
- }
381
-
382
- /**
383
- * One serialized snapshot rebuild; contains every failure (contract: never rejects).
384
- *
385
- * NOTE: nothing here carries a deadline — a `readRows` or a `hangUp` that never
386
- * settles wedges the refresh chain, which freezes the deny set (the poll skips
387
- * while `refreshing`, and every later `refresh()` chains behind the stuck one).
388
- * Both are bounded in practice (Quereus reads settle; libp2p's connection close
389
- * has its own timeout). If a strand is ever seen with a stale deny set and no
390
- * refresh log line, wrap both awaits in a timeout rather than hunting further.
391
- */
392
- private async doRefresh(): Promise<void> {
393
- if (this.stopped) return;
394
- this.refreshing = true;
395
- try {
396
- const rows = await this.deps.readRows();
397
- if (this.stopped) return;
398
- this.revokedPeerIds = deriveRevokedPeerIds(rows);
399
- log('[%s] deny set refreshed: %d revoked peer(s)', this.deps.label, this.revokedPeerIds.size);
400
- // Awaited inside the refresh, so an on-demand refresh() resolves only once
401
- // the cut has been made — that is what makes "revoke, then refresh" an
402
- // immediate teardown for the caller rather than a scheduled one.
403
- await this.tearDownRevoked(this.revokedPeerIds);
404
- } catch (error) {
405
- log('[%s] deny-set refresh failed — keeping previous snapshot: %o', this.deps.label, error);
406
- } finally {
407
- this.refreshing = false;
408
- }
409
- }
410
-
411
- /**
412
- * Close what is already OPEN to the peers of `revoked` — the teardown half of
413
- * enforcement (module doc: "Removal cuts LIVE sessions too"). Never rejects:
414
- * every failure is contained, so the refresh loop survives and simply retries
415
- * on the next tick.
416
- *
417
- * `revoked` is passed IN rather than read off the field, so the sweep judges
418
- * the exact snapshot its own refresh produced; likewise the connection list is
419
- * enumerated once, before any await, so a peer re-admitted mid-sweep is at
420
- * worst hung up once and reconnects.
421
- *
422
- * NOTE: dropping the revoked peer from the strand's Optimystic cohort needs no
423
- * separate action — machines quiesce strands routinely and the remaining cohort
424
- * keeps committing, so an unreachable cohort peer is already tolerated, and
425
- * `denyDialPeer` makes the exclusion fast (a local refusal, not a dial timeout).
426
- * Two residual facts, both accepted: the removed node keeps every block it had
427
- * already replicated (revocation is forward-looking), and a block whose only
428
- * holders were the removed party's machines can be unavailable to the rest
429
- * until repair.
430
- *
431
- * NOTE: post-removal commit latency is real and MEASURED, not hypothetical. On
432
- * the two-parties-by-two-machines fixture in
433
- * `integration-tests/src/scenarios/strand-removal-cuts-network.integration.ts`
434
- * the remaining cohort's FIRST write after a cut fails repeatedly with
435
- * `BlockUnavailableError: … (peers-unreachable)` before it commits — three or
436
- * four failures, 3.5-4.3 s, over four runs — because the cohort still listed
437
- * the removed party's now unreachable machines and had to downsize to the
438
- * live holders first. It
439
- * recovers on its own, so this is a latency cost, not a defect, and it is
440
- * stated for app authors in `docs/strands.md` → Removing Members. If it ever
441
- * stops recovering — a write that never commits, or `cluster-fetch:no-quorum` —
442
- * that is an upstream (Optimystic) conversation, not a workaround here.
443
- */
444
- private async tearDownRevoked(revoked: ReadonlySet<string>): Promise<void> {
445
- if (this.stopped) {
446
- return;
447
- }
448
- const network = this.resolveNetwork();
449
- if (!network) {
450
- return;
451
- }
452
- const selfPeerId = network.peerId.toString();
453
- // Judged on EVERY pass, including the empty-set one: leaving the set is what
454
- // re-arms the signal (see trackSelfRevoked). Being revoked ourselves tears
455
- // nothing down on our own behalf — the other side's gate already refuses us —
456
- // so the sweep below still runs for every OTHER revoked peer in the pass.
457
- this.trackSelfRevoked(revoked.has(selfPeerId));
458
- if (revoked.size === 0) {
459
- return;
460
- }
461
- for (const peer of connectedRevokedPeers(network, revoked, selfPeerId, this.deps.label)) {
462
- await this.hangUpRevoked(network, peer);
463
- }
464
- }
465
-
466
- /** The lazily-read network handle; an accessor that throws costs this pass only. */
467
- private resolveNetwork(): StrandRevocationNetwork | undefined {
468
- try {
469
- return this.deps.getNetwork?.();
470
- } catch (error) {
471
- log('[%s] network handle unavailable — teardown skipped this pass: %o', this.deps.label, error);
472
- return undefined;
473
- }
474
- }
475
-
476
- /** One hangUp, contained: a stopping node or an already-closed connection is not an error here. */
477
- private async hangUpRevoked(network: StrandRevocationNetwork, peer: PeerId): Promise<void> {
478
- try {
479
- await network.hangUp(peer);
480
- log('[%s] hung up revoked peer %s', this.deps.label, peer.toString());
481
- } catch (error) {
482
- log('[%s] hangUp of revoked peer %s failed — next refresh retries: %o', this.deps.label, peer.toString(), error);
483
- }
484
- }
485
-
486
- /**
487
- * Latch {@link StrandRevocationEnforcerDeps.onSelfRevoked} to the TRANSITION
488
- * into the revoked state: fired once on entry, and re-armed the moment this
489
- * node is out of the set again — a manager can re-admit a party it removed
490
- * (a fresh `Member` row makes the orphaned bindings live again), and a second
491
- * removal has to be reported like the first.
492
- */
493
- private trackSelfRevoked(selfRevoked: boolean): void {
494
- if (!selfRevoked) {
495
- this.selfRevokedSignaled = false;
496
- return;
497
- }
498
- if (this.selfRevokedSignaled) {
499
- return;
500
- }
501
- this.selfRevokedSignaled = true;
502
- log('[%s] THIS node is revoked from the strand (removed or left) — signalling', this.deps.label);
503
- try {
504
- this.deps.onSelfRevoked?.();
505
- } catch (error) {
506
- log('[%s] onSelfRevoked handler threw: %o', this.deps.label, error);
507
- }
508
- }
509
- }
510
-
511
- /**
512
- * The distinct peer ids of `network`'s open connections that are in `revoked`,
513
- * as the `PeerId` objects `hangUp` takes — reusing each connection's own
514
- * `remotePeer` rather than parsing the string back, so no peer-id codec is
515
- * needed here. Self is excluded (a node holds no connection to itself, and
516
- * hanging one up would be nonsense if it did), and a peer connected twice — say
517
- * one direct and one relayed — yields ONE hangUp, which closes both. A throw
518
- * mid-enumeration is logged and keeps whatever was collected before it: a
519
- * partial sweep beats none, and the next refresh re-enumerates anyway.
520
- */
521
- function connectedRevokedPeers(
522
- network: StrandRevocationNetwork,
523
- revoked: ReadonlySet<string>,
524
- selfPeerId: string,
525
- label: string
526
- ): PeerId[] {
527
- const targets = new Map<string, PeerId>();
528
- try {
529
- for (const connection of network.getConnections()) {
530
- const remotePeerId = connection.remotePeer.toString();
531
- if (remotePeerId !== selfPeerId && revoked.has(remotePeerId)) {
532
- targets.set(remotePeerId, connection.remotePeer);
533
- }
534
- }
535
- } catch (error) {
536
- log('[%s] connection enumeration failed — teardown skipped this pass: %o', label, error);
537
- }
538
- return [...targets.values()];
539
- }
540
-
541
- /**
542
- * Derive the deny set: a peer id is revoked iff it has at least one ORPHANED
543
- * binding (a `MemberPeer` row whose member key has no live `Member` row) AND no
544
- * live-membered binding. The second conjunct is what lets a peer id bound to
545
- * TWO member keys — revoked under one, live under the other — keep serving the
546
- * live member: a machine serving a live member must not be cut. Per-binding,
547
- * not per-member, so every machine of a multi-device removed party lands in
548
- * the set.
549
- */
550
- function deriveRevokedPeerIds(rows: StrandRevocationRows): Set<string> {
551
- const orphaned = new Set<string>();
552
- const live = new Set<string>();
553
- for (const binding of rows.bindings) {
554
- (rows.memberKeys.has(binding.memberKey) ? live : orphaned).add(binding.peerId);
555
- }
556
- for (const peerId of live) {
557
- orphaned.delete(peerId);
558
- }
559
- return orphaned;
560
- }
561
-
562
- /** The one capability the gater composition needs from the enforcer. */
563
- export type StrandRevocationJudge = Pick<StrandRevocationEnforcer, 'isRevoked'>;
564
-
565
- /**
566
- * Build a closed strand's connection gater: the caller-supplied gater (if any)
567
- * with revoked-peer denial composed onto three hooks. The opportunistic
568
- * SECONDARY layer — the per-stream gate above is the fail-closed primary.
569
- *
570
- * - `denyInboundEncryptedConnection`: deny iff revoked, so a removed party's
571
- * node is refused before any protocol negotiation.
572
- * - `denyDialPeer`: deny iff revoked — the owner ruling covers "communicated
573
- * with", not just "answered to", and refusing the dial locally is instant,
574
- * which keeps Optimystic from wasting a dial timeout on a peer this node
575
- * will not talk to anyway.
576
- * - `denyInboundRelayReservation`: deny iff revoked — matters when the strand
577
- * node runs the relay server (the storage-profile default), where admitting
578
- * the reservation would let a removed party keep using this node's
579
- * forwarding capacity.
580
- *
581
- * Composition semantics: every hook of `base` is preserved as-is; on the three
582
- * composed hooks a deny from EITHER the base gater or the revocation check
583
- * denies, and an error (or a decision slower than `decisionTimeoutMs`) in
584
- * either is fail-open — the base error admits through to the revocation check,
585
- * a revocation-check error admits outright, and the fail-closed stream gate
586
- * still stands behind both. The deadline is belt-and-braces: the snapshot read
587
- * is synchronous.
588
- *
589
- * NOTE: accepted tradeoff — swallowing a BASE hook's throw diverges from
590
- * `createMembershipConnectionGater`, which lets it propagate to libp2p (a
591
- * fail-closed outcome there). Uniform fail-open was chosen for this module so
592
- * every hook has one failure direction, and it was the shape the plan specified;
593
- * the cost is that an embedder gater which throws is silently admitted here
594
- * while the same gater passed raw to an OPEN strand would refuse. Loud in the
595
- * log either way. Revisit if an embedder ever ships a gater whose throw is a
596
- * meaningful deny, or if the two gaters are unified.
597
- *
598
- * NOTE: `base` is spread, so a gater passed as a CLASS INSTANCE would lose its
599
- * prototype methods — same caveat as `createMembershipConnectionGater`; every
600
- * caller in this repo supplies a plain object (the RN/web permissive gaters,
601
- * e.g. `{ denyDialMultiaddr: () => false }`, keep working unchanged).
602
- */
603
- export function createRevocationConnectionGater(
604
- enforcer: StrandRevocationJudge,
605
- base?: ConnectionGater,
606
- decisionTimeoutMs: number = ADMISSION_DECISION_TIMEOUT_MS
607
- ): ConnectionGater {
608
- /** The base gater's verdict, fail-open: an error or absence is "not denied". */
609
- const baseDenies = async (decide: () => Promise<boolean> | boolean | undefined, hook: string): Promise<boolean> => {
610
- try {
611
- return await decide() === true;
612
- } catch (error) {
613
- log('base gater %s threw — treating as not-denied (fail-open): %o', hook, error);
614
- return false;
615
- }
616
- };
617
- /** The revocation verdict, bounded and fail-open. */
618
- const revokedDenies = async (peerId: PeerId, hook: string): Promise<boolean> => {
619
- const remotePeerId = peerId.toString();
620
- try {
621
- const revoked = await decideWithinDeadline(
622
- () => enforcer.isRevoked(remotePeerId), false, decisionTimeoutMs, `${hook}(${remotePeerId})`
623
- );
624
- if (revoked) {
625
- log('%s: %s is revoked — denying', hook, remotePeerId);
626
- }
627
- return revoked;
628
- } catch (error) {
629
- log('%s revocation check threw for %s — admitting (fail-open; stream gate decides): %o', hook, remotePeerId, error);
630
- return false;
631
- }
632
- };
633
- return {
634
- ...base,
635
- denyDialPeer: async (peerId: PeerId): Promise<boolean> => {
636
- if (await baseDenies(() => base?.denyDialPeer?.(peerId), 'denyDialPeer')) {
637
- return true;
638
- }
639
- return revokedDenies(peerId, 'denyDialPeer');
640
- },
641
- denyInboundEncryptedConnection: async (peerId: PeerId, maConn: MultiaddrConnection): Promise<boolean> => {
642
- if (await baseDenies(() => base?.denyInboundEncryptedConnection?.(peerId, maConn), 'denyInboundEncryptedConnection')) {
643
- return true;
644
- }
645
- return revokedDenies(peerId, 'denyInboundEncryptedConnection');
646
- },
647
- denyInboundRelayReservation: async (peerId: PeerId): Promise<boolean> => {
648
- if (await baseDenies(() => base?.denyInboundRelayReservation?.(peerId), 'denyInboundRelayReservation')) {
649
- return true;
650
- }
651
- return revokedDenies(peerId, 'denyInboundRelayReservation');
652
- }
653
- };
654
- }
1
+ /**
2
+ * Strand revoked-peer enforcement: a per-strand deny gate that refuses the
3
+ * network nodes of a REMOVED party, so removal means "we stop talking to you"
4
+ * rather than only deleting a row.
5
+ *
6
+ * ## The deny set is revocation-keyed, never membership-keyed
7
+ *
8
+ * A strand node maintains a materialized in-memory set of revoked peer ids:
9
+ * every `Strand.MemberPeer.PeerId` whose `MemberKey` has no live `Strand.Member`
10
+ * row. `MemberPeer.MemberExists` runs on insert, so an orphaned binding can only
11
+ * mean "this member existed and was removed" — it is exactly the durable,
12
+ * replicated record of a removed party's machines. Everything NOT in the set is
13
+ * admitted. Deliberate consequences:
14
+ *
15
+ * - **Open strands are untouched.** `Member`/`MemberPeer` carry `OnlyClosed`
16
+ * checks, so an open strand has no rows and the set is empty — and
17
+ * `StrandInstanceManager` additionally skips arming this enforcer entirely for
18
+ * `Type !== 'c'`, so an open strand does not even poll.
19
+ * - **A joining party mid-admission is admitted** (it has no orphaned binding).
20
+ * An allowlist ("only peers bound to live members") was considered and
21
+ * rejected: a joiner's first membership writes must travel through the very
22
+ * cohort an allowlist would close to it — the same mutual-denial trap the
23
+ * control network's gater doc describes (`membership-connection-gater.ts`),
24
+ * with no enrollment window to carve out. A deny-list on positive evidence
25
+ * has no such trap.
26
+ * - **Stale-view failure direction: fail-open.** A remaining member that has not
27
+ * yet replicated the revocation keeps serving the removed party until the
28
+ * tombstoned state arrives — bounded by replication, consistent with
29
+ * "revocation is forward-looking". The opposite error (denying a legitimate
30
+ * member) is only reachable for a revoked-then-re-admitted party at a node
31
+ * that saw the revoke but not the re-add; the re-add gives the member key a
32
+ * live `Member` row again, which removes its peers from the deny set as the
33
+ * row replicates — self-healing, and the re-adding manager's own node updates
34
+ * first, so the heal never depends on talking to the peer being wrongly denied.
35
+ * - **Orphaned `MemberPeer` rows are load-bearing**: they ARE the deny record —
36
+ * the `Strand.Revocation` tombstone keeps only the stamp, not the peer id — so
37
+ * nothing here (or anywhere) tombstones or auto-cleans them at revocation.
38
+ * The manager-cleanup path (`removeMemberPeer`'s manager branch) stays as the
39
+ * only way to clear a wrongly-registered binding, and clearing an orphan
40
+ * forgets the denial (the NOTE at that branch says so).
41
+ *
42
+ * ## Removal cuts LIVE sessions too — the refresh IS the teardown seam
43
+ *
44
+ * Refusing NEW connections only half-honors "no longer be answered to or
45
+ * communicated with": a long-lived strand connection may never re-dial, so
46
+ * without teardown a removed party keeps whatever session it already holds.
47
+ * There is no push notification to hang teardown off — a REPLICATED revocation
48
+ * arrives with no change event (the strand database has no reactive
49
+ * subscription; `StrandWatcher` polls for the same reason), and a LOCAL
50
+ * `revokeMember`/`leaveStrand` writes through a bare `Database` handle this
51
+ * runtime never sees — so teardown rides the snapshot refresh itself, with
52
+ * `refresh()` exposed for immediacy
53
+ * (`StrandInstanceManager.refreshRevocationEnforcement` →
54
+ * `CadreNode.refreshRevocationEnforcement`).
55
+ *
56
+ * The sweep is driven by MEMBERSHIP in the deny set, never by an
57
+ * entered-the-set diff: every refresh hangs up every currently-connected
58
+ * revoked peer, so a resume, a crashed refresh, or a connection established
59
+ * between two refreshes cannot leave a session standing (a diff would be a
60
+ * strict subset of this, so none is kept). A peer with no open connection is
61
+ * simply not swept, and a failed `hangUp` is logged, never thrown — the next
62
+ * refresh retries it. `hangUp` closes DIRECT and RELAYED connections alike, and
63
+ * on a strand node running the relay server it also drops any reservation the
64
+ * revoked peer holds there (a reservation dies with its connection — see the
65
+ * relay-seam doc in `membership-connection-gater.ts`).
66
+ *
67
+ * Teardown is also what stops OUTBOUND traffic. The gate is one-directional —
68
+ * `authorizeStream` judges streams the revoked peer opens to US — so nothing
69
+ * else prevents this node from pushing over an already-open connection, which
70
+ * `PeerJoinBackfill` does for that connection's whole life. Closing the
71
+ * connection stops both directions at once, which is why no separate outbound
72
+ * gate exists.
73
+ *
74
+ * If this node's OWN peer id is in the set, it was itself removed (or it left —
75
+ * a voluntary departure files the same tombstone). Nothing is torn down for
76
+ * that: the other side's gate already refuses us, and every other revoked peer
77
+ * is still swept. Instead `onSelfRevoked` fires ONCE per entry into the set
78
+ * (re-armed if the party is re-admitted, so a second removal signals again),
79
+ * which `CadreNode` surfaces as `strand:revoked` so the embedding app decides
80
+ * what to do — this module never stops or leaves a strand on its own.
81
+ * Best-effort by nature: a removed party learns only if the tombstone
82
+ * replicated to it before the cut.
83
+ *
84
+ * ## What transfers from the control gater (`membership-connection-gater.ts`)
85
+ *
86
+ * Transfers: the two-layer shape (fail-closed per-stream gate as PRIMARY, an
87
+ * opportunistic connection-level deny on top), the synchronous in-memory
88
+ * snapshot judged by the stream gate (a live DB read inside a gate deadlocks —
89
+ * the same argument as the control node's `authorizeInboundControlStream`), the
90
+ * fail-open-on-error/timeout posture for the connection hooks
91
+ * (`decideWithinDeadline`, reused as-is), and the "compose over the caller's
92
+ * gater, every base hook preserved" mechanics.
93
+ *
94
+ * Does NOT transfer — the relay-reservation seam (`'admit-for-relay'` + the
95
+ * unauthorized budget). That machinery exists because the control gate denies on
96
+ * ABSENCE of placement, where a member whose row is in flight is
97
+ * indistinguishable from an outsider and a wrong reservation deny is
98
+ * unrecoverable. This gate denies on POSITIVE revocation evidence, so a
99
+ * reservation deny is never that kind of wrong answer; the one stale-wrong case
100
+ * (a missed re-add) heals via replication as above. `denyInboundRelayReservation`
101
+ * here is therefore a plain "deny iff revoked" — no budget, no admit-for-relay
102
+ * verdict, no reserve deadline.
103
+ *
104
+ * Does NOT transfer — the bring-up quiet period. A strand node's DB bring-up
105
+ * NEEDS its cross-party cohort connections; this enforcer simply starts with an
106
+ * empty snapshot (admit everything) until its first successful read — the same
107
+ * fail-open posture as everywhere else here.
108
+ *
109
+ * Does NOT transfer — every stranger carve-out (enrollment window, formation
110
+ * window, delegate grants). None of those protocols exist on a strand node.
111
+ *
112
+ * Unlike the control stream gate, no empty-snapshot carve-out is needed: an
113
+ * empty deny set means "nothing revoked", which correctly admits everyone — the
114
+ * deny-list shape makes the cold-start case and the steady case the same code
115
+ * path.
116
+ */
117
+
118
+ import debug from 'debug';
119
+ import type { ConnectionGater, PeerId, MultiaddrConnection } from '@libp2p/interface';
120
+ import type { Database } from '@quereus/quereus';
121
+ import { decideWithinDeadline } from './membership-connection-gater.js';
122
+ import { ADMISSION_DECISION_TIMEOUT_MS } from './link-budget.js';
123
+
124
+ const log = debug('sereus:cadre:strand-revocation');
125
+
126
+ /**
127
+ * Default cadence of the deny-set refresh. Bounded staleness is the accepted
128
+ * failure direction (see the module doc's stale-view paragraph); 30 s keeps a
129
+ * revoked peer's post-replication admit window in the same order as the
130
+ * replication delay itself. Overridable via
131
+ * {@link StrandRevocationEnforcementConfig.pollIntervalMs}.
132
+ */
133
+ export const DEFAULT_REVOCATION_POLL_INTERVAL_MS = 30_000;
134
+
135
+ /**
136
+ * Embedder tuning for the strand revoked-peer gate, threaded from
137
+ * `CadreNodeConfig.strandRevocationEnforcement` to
138
+ * `StartStrandConfig.revocationEnforcement` the way the backfill knob is.
139
+ */
140
+ export interface StrandRevocationEnforcementConfig {
141
+ /**
142
+ * Default true. False disarms the gate entirely for this node's strands —
143
+ * the pre-existing behaviour (a removed party's peers keep being served).
144
+ */
145
+ enabled?: boolean;
146
+ /** Deny-set refresh cadence, ms. Default {@link DEFAULT_REVOCATION_POLL_INTERVAL_MS}. */
147
+ pollIntervalMs?: number;
148
+ }
149
+
150
+ /** One `Strand.MemberPeer` row, as the deny-set derivation consumes it. */
151
+ export interface StrandMemberPeerBinding {
152
+ memberKey: string;
153
+ peerId: string;
154
+ }
155
+
156
+ /** The membership rows one deny-set derivation reads. */
157
+ export interface StrandRevocationRows {
158
+ /** Every live `Strand.Member.Key`. */
159
+ memberKeys: ReadonlySet<string>;
160
+ /** Every `Strand.MemberPeer` row (live-membered and orphaned alike). */
161
+ bindings: readonly StrandMemberPeerBinding[];
162
+ }
163
+
164
+ /**
165
+ * Timer seam so the refresh cadence is testable without fake timers — the
166
+ * injectable-clock idiom of `UnauthorizedReservationBudget`'s `now` argument,
167
+ * shaped for an interval-driven component. The default schedules a real
168
+ * interval and unrefs it so an armed enforcer never holds a process open.
169
+ */
170
+ export interface RevocationRefreshScheduler {
171
+ setInterval(fn: () => void, ms: number): unknown;
172
+ clearInterval(handle: unknown): void;
173
+ }
174
+
175
+ const defaultScheduler: RevocationRefreshScheduler = {
176
+ setInterval: (fn, ms) => {
177
+ const handle = setInterval(fn, ms);
178
+ (handle as { unref?: () => void }).unref?.();
179
+ return handle;
180
+ },
181
+ clearInterval: (handle) => clearInterval(handle as Parameters<typeof clearInterval>[0])
182
+ };
183
+
184
+ /**
185
+ * The minimum of a libp2p node the teardown sweep needs: who WE are, what is
186
+ * open, and how to close it. Deliberately not the node itself, so the sweep
187
+ * unit-tests against a three-property stub; `Libp2p` (hence
188
+ * `Libp2pNodeWithRepo`) satisfies it structurally, which is what
189
+ * `StrandInstanceManager` passes.
190
+ */
191
+ export interface StrandRevocationNetwork {
192
+ /** This node's own peer id — how the enforcer recognizes ITSELF in the deny set. */
193
+ readonly peerId: PeerId;
194
+ /** Every open connection. Only `remotePeer` is read — and reused as the hangUp target. */
195
+ getConnections(): readonly { readonly remotePeer: PeerId }[];
196
+ /** Close every connection to `peer`: direct, relayed, and any reservation riding one. */
197
+ hangUp(peer: PeerId): Promise<void>;
198
+ }
199
+
200
+ export interface StrandRevocationEnforcerDeps {
201
+ /** Log tag naming which strand this enforcer serves (the strand id). */
202
+ label: string;
203
+ /**
204
+ * Read the membership rows the deny set is derived from. Injectable so the
205
+ * enforcer unit-tests without a strand database; production wires
206
+ * {@link readStrandRevocationRows} over the strand's live DB. A throw or
207
+ * rejection KEEPS the previous snapshot (never clears it) — the control
208
+ * model, `refreshAuthorizedControlPeers`.
209
+ */
210
+ readRows: () => Promise<StrandRevocationRows>;
211
+ /**
212
+ * Handle on the strand's libp2p node for the teardown sweep, read LAZILY on
213
+ * every sweep — never captured. It has to be: the enforcer is constructed
214
+ * BEFORE the node (the node's options embed the enforcer's predicates), and
215
+ * the node is dropped again on quiesce. Omit it, or return undefined, and the
216
+ * gate still refuses new connections — only teardown of OPEN ones is skipped.
217
+ */
218
+ getNetwork?: () => StrandRevocationNetwork | undefined;
219
+ /**
220
+ * Called ONCE each time this node's own peer id ENTERS the deny set — i.e.
221
+ * this node's party was removed from the strand, or left it. Not re-fired
222
+ * while it stays there; re-armed if the party is re-admitted, so a second
223
+ * removal signals again. Wired by `StrandInstanceManager` through to
224
+ * `CadreNode`'s `strand:revoked` event. A resume rebuilds the enforcer, so a
225
+ * still-revoked strand also legitimately re-fires once per resume. A throw is
226
+ * logged and swallowed — an embedder's handler must not derail the sweep.
227
+ */
228
+ onSelfRevoked?: () => void;
229
+ /** Timer seam; omit for real (unref'd) intervals. */
230
+ scheduler?: RevocationRefreshScheduler;
231
+ }
232
+
233
+ /** Every `Strand.MemberPeer` binding — live-membered and orphaned alike. */
234
+ async function scanBindings(db: Database): Promise<StrandMemberPeerBinding[]> {
235
+ const bindings: StrandMemberPeerBinding[] = [];
236
+ for await (const row of db.eval('select MemberKey, PeerId from Strand.MemberPeer')) {
237
+ bindings.push({ memberKey: row.MemberKey as string, peerId: row.PeerId as string });
238
+ }
239
+ return bindings;
240
+ }
241
+
242
+ /** Every live `Strand.Member.Key`. */
243
+ async function scanMemberKeys(db: Database): Promise<Set<string>> {
244
+ const memberKeys = new Set<string>();
245
+ for await (const row of db.eval('select Key from Strand.Member')) {
246
+ memberKeys.add(row.Key as string);
247
+ }
248
+ return memberKeys;
249
+ }
250
+
251
+ /**
252
+ * Read the deny-set inputs from a strand database: full-scan
253
+ * `Strand.MemberPeer` and `Strand.Member`, to be joined in JavaScript by the
254
+ * enforcer. Scan-and-filter, never seek — the composite-PK point-lookup
255
+ * unreliability documented at `scanMemberPeers` in `strand-membership-writer.ts`
256
+ * applies, so correctness depends only on each scan returning a superset of the
257
+ * live rows.
258
+ *
259
+ * **Scan order is load-bearing, and it is BINDINGS FIRST.** The two scans are
260
+ * separate reads with nothing holding them to one snapshot, so a write that
261
+ * lands between them is seen by the second scan and not the first. A member
262
+ * joins as `Member` row then `MemberPeer` row (`MemberExists` forces that
263
+ * order), so reading `Member` first would let a join that commits mid-read
264
+ * produce a binding whose member key is absent from the key set — a brand-new
265
+ * member classified as REVOKED, the one fail-CLOSED outcome this module exists
266
+ * to avoid. Reading bindings first inverts the skew: the new binding is simply
267
+ * not in the older bindings snapshot, so the joiner is unclassified and
268
+ * admitted, and a revocation that lands mid-read is still seen (the key is
269
+ * missing from the NEWER key scan). Every skew window then resolves fail-open,
270
+ * matching the module doc.
271
+ *
272
+ * NOTE: two whole-table scans per refresh (default every 30 s per closed
273
+ * strand). Fine at strand scale (a handful of members and bindings); if
274
+ * membership churn ever makes these tables large, the fix is a reliable
275
+ * filtered read — which needs the networked point-lookup gap closed
276
+ * (`debt-composite-pk-point-lookup-unreliable-untracked`) — not a bigger scan.
277
+ */
278
+ export async function readStrandRevocationRows(db: Database): Promise<StrandRevocationRows> {
279
+ const bindings = await scanBindings(db);
280
+ const memberKeys = await scanMemberKeys(db);
281
+ return { memberKeys, bindings };
282
+ }
283
+
284
+ /**
285
+ * The per-strand revoked-peer deny set and the synchronous predicates over it.
286
+ *
287
+ * Lifecycle mirrors `PeerJoinBackfill`: created in
288
+ * `StrandInstanceManager.buildStrandRuntime` (closed strands only), started
289
+ * after the strand database initializes, stopped and dropped in
290
+ * `releaseRuntime` — so quiesce → resume rebuilds it with a fresh snapshot.
291
+ * The snapshot starts EMPTY (admit everything) and repopulates on the first
292
+ * successful read; a revoked peer therefore gets a short admit window per
293
+ * resume at the connection level. Accepted: fail-open posture, and resume is
294
+ * never blocked on a refresh.
295
+ */
296
+ export class StrandRevocationEnforcer {
297
+ /** The materialized deny set — peer ids with an orphaned binding and no live one. */
298
+ private revokedPeerIds: ReadonlySet<string> = new Set();
299
+ private readonly scheduler: RevocationRefreshScheduler;
300
+ private readonly pollIntervalMs: number;
301
+ /** Tail of the refresh chain — what serializes refreshes (no two reads in flight). */
302
+ private refreshTail: Promise<void> = Promise.resolve();
303
+ private refreshing = false;
304
+ private intervalHandle: unknown;
305
+ private started = false;
306
+ private stopped = false;
307
+ /** Latch for the one-shot self-revocation signal — see {@link StrandRevocationEnforcerDeps.onSelfRevoked}. */
308
+ private selfRevokedSignaled = false;
309
+
310
+ constructor(
311
+ private readonly deps: StrandRevocationEnforcerDeps,
312
+ config?: StrandRevocationEnforcementConfig
313
+ ) {
314
+ this.scheduler = deps.scheduler ?? defaultScheduler;
315
+ this.pollIntervalMs = config?.pollIntervalMs ?? DEFAULT_REVOCATION_POLL_INTERVAL_MS;
316
+ }
317
+
318
+ /** Number of peers currently denied — test/diagnostic surface. */
319
+ get revokedCount(): number {
320
+ return this.revokedPeerIds.size;
321
+ }
322
+
323
+ /** Kick an immediate refresh (not awaited — bring-up never blocks on it) and arm the poll. */
324
+ start(): void {
325
+ if (this.started || this.stopped) return;
326
+ this.started = true;
327
+ void this.refresh();
328
+ this.intervalHandle = this.scheduler.setInterval(() => {
329
+ // Skip a tick while a refresh is in flight rather than queueing behind it —
330
+ // the interval exists to bound staleness, and a stack of queued reads
331
+ // bounds nothing extra. Explicit refresh() calls still chain (see refresh).
332
+ if (!this.refreshing) {
333
+ void this.refresh();
334
+ }
335
+ }, this.pollIntervalMs);
336
+ log('[%s] revocation enforcer started (poll %dms)', this.deps.label, this.pollIntervalMs);
337
+ }
338
+
339
+ /** Disarm the poll; a read already in flight completes but its result is discarded. */
340
+ stop(): void {
341
+ if (this.stopped) return;
342
+ this.stopped = true;
343
+ if (this.intervalHandle != null) {
344
+ this.scheduler.clearInterval(this.intervalHandle);
345
+ this.intervalHandle = undefined;
346
+ }
347
+ log('[%s] revocation enforcer stopped', this.deps.label);
348
+ }
349
+
350
+ /**
351
+ * Refresh the deny set now. Never rejects. Serialized: concurrent calls chain,
352
+ * so no two reads overlap, and each explicit call gets a read that STARTS
353
+ * after the call (an on-demand refresh after a revocation commit observes that
354
+ * commit). A failed or slow read keeps the previous snapshot — never clears it.
355
+ */
356
+ refresh(): Promise<void> {
357
+ const run = this.refreshTail.then(() => this.doRefresh());
358
+ this.refreshTail = run;
359
+ return run;
360
+ }
361
+
362
+ /**
363
+ * Is `remotePeerId` positively revoked in the current snapshot? Synchronous by
364
+ * design — this is judged inside libp2p gates where an await against the DB
365
+ * would deadlock (the control stream gate's argument).
366
+ */
367
+ isRevoked(remotePeerId: string): boolean {
368
+ return this.revokedPeerIds.has(remotePeerId);
369
+ }
370
+
371
+ /**
372
+ * The fail-closed PRIMARY layer: the per-stream predicate wired as
373
+ * `createLibp2pNode({ authorizeInboundStream })`, gating all four
374
+ * `/optimystic/strand-<id>/{repo,cluster,sync,block-transfer}` protocols.
375
+ * Also what catches a revoked peer arriving over a RELAYED connection, or
376
+ * over a connection admitted before the revocation replicated here.
377
+ */
378
+ authorizeStream(remotePeerId: string, _protocol: string): boolean {
379
+ return !this.isRevoked(remotePeerId);
380
+ }
381
+
382
+ /**
383
+ * One serialized snapshot rebuild; contains every failure (contract: never rejects).
384
+ *
385
+ * NOTE: nothing here carries a deadline — a `readRows` or a `hangUp` that never
386
+ * settles wedges the refresh chain, which freezes the deny set (the poll skips
387
+ * while `refreshing`, and every later `refresh()` chains behind the stuck one).
388
+ * Both are bounded in practice (Quereus reads settle; libp2p's connection close
389
+ * has its own timeout). If a strand is ever seen with a stale deny set and no
390
+ * refresh log line, wrap both awaits in a timeout rather than hunting further.
391
+ */
392
+ private async doRefresh(): Promise<void> {
393
+ if (this.stopped) return;
394
+ this.refreshing = true;
395
+ try {
396
+ const rows = await this.deps.readRows();
397
+ if (this.stopped) return;
398
+ this.revokedPeerIds = deriveRevokedPeerIds(rows);
399
+ log('[%s] deny set refreshed: %d revoked peer(s)', this.deps.label, this.revokedPeerIds.size);
400
+ // Awaited inside the refresh, so an on-demand refresh() resolves only once
401
+ // the cut has been made — that is what makes "revoke, then refresh" an
402
+ // immediate teardown for the caller rather than a scheduled one.
403
+ await this.tearDownRevoked(this.revokedPeerIds);
404
+ } catch (error) {
405
+ log('[%s] deny-set refresh failed — keeping previous snapshot: %o', this.deps.label, error);
406
+ } finally {
407
+ this.refreshing = false;
408
+ }
409
+ }
410
+
411
+ /**
412
+ * Close what is already OPEN to the peers of `revoked` — the teardown half of
413
+ * enforcement (module doc: "Removal cuts LIVE sessions too"). Never rejects:
414
+ * every failure is contained, so the refresh loop survives and simply retries
415
+ * on the next tick.
416
+ *
417
+ * `revoked` is passed IN rather than read off the field, so the sweep judges
418
+ * the exact snapshot its own refresh produced; likewise the connection list is
419
+ * enumerated once, before any await, so a peer re-admitted mid-sweep is at
420
+ * worst hung up once and reconnects.
421
+ *
422
+ * NOTE: dropping the revoked peer from the strand's Optimystic cohort needs no
423
+ * separate action — machines quiesce strands routinely and the remaining cohort
424
+ * keeps committing, so an unreachable cohort peer is already tolerated, and
425
+ * `denyDialPeer` makes the exclusion fast (a local refusal, not a dial timeout).
426
+ * Two residual facts, both accepted: the removed node keeps every block it had
427
+ * already replicated (revocation is forward-looking), and a block whose only
428
+ * holders were the removed party's machines can be unavailable to the rest
429
+ * until repair.
430
+ *
431
+ * NOTE: post-removal commit latency is real and MEASURED, not hypothetical. On
432
+ * the two-parties-by-two-machines fixture in
433
+ * `integration-tests/src/scenarios/strand-removal-cuts-network.integration.ts`
434
+ * the remaining cohort's FIRST write after a cut fails repeatedly with
435
+ * `BlockUnavailableError: … (peers-unreachable)` before it commits — three or
436
+ * four failures, 3.5-4.3 s, over four runs — because the cohort still listed
437
+ * the removed party's now unreachable machines and had to downsize to the
438
+ * live holders first. It
439
+ * recovers on its own, so this is a latency cost, not a defect, and it is
440
+ * stated for app authors in `docs/strands.md` → Removing Members. If it ever
441
+ * stops recovering — a write that never commits, or `cluster-fetch:no-quorum` —
442
+ * that is an upstream (Optimystic) conversation, not a workaround here.
443
+ */
444
+ private async tearDownRevoked(revoked: ReadonlySet<string>): Promise<void> {
445
+ if (this.stopped) {
446
+ return;
447
+ }
448
+ const network = this.resolveNetwork();
449
+ if (!network) {
450
+ return;
451
+ }
452
+ const selfPeerId = network.peerId.toString();
453
+ // Judged on EVERY pass, including the empty-set one: leaving the set is what
454
+ // re-arms the signal (see trackSelfRevoked). Being revoked ourselves tears
455
+ // nothing down on our own behalf — the other side's gate already refuses us —
456
+ // so the sweep below still runs for every OTHER revoked peer in the pass.
457
+ this.trackSelfRevoked(revoked.has(selfPeerId));
458
+ if (revoked.size === 0) {
459
+ return;
460
+ }
461
+ for (const peer of connectedRevokedPeers(network, revoked, selfPeerId, this.deps.label)) {
462
+ await this.hangUpRevoked(network, peer);
463
+ }
464
+ }
465
+
466
+ /** The lazily-read network handle; an accessor that throws costs this pass only. */
467
+ private resolveNetwork(): StrandRevocationNetwork | undefined {
468
+ try {
469
+ return this.deps.getNetwork?.();
470
+ } catch (error) {
471
+ log('[%s] network handle unavailable — teardown skipped this pass: %o', this.deps.label, error);
472
+ return undefined;
473
+ }
474
+ }
475
+
476
+ /** One hangUp, contained: a stopping node or an already-closed connection is not an error here. */
477
+ private async hangUpRevoked(network: StrandRevocationNetwork, peer: PeerId): Promise<void> {
478
+ try {
479
+ await network.hangUp(peer);
480
+ log('[%s] hung up revoked peer %s', this.deps.label, peer.toString());
481
+ } catch (error) {
482
+ log('[%s] hangUp of revoked peer %s failed — next refresh retries: %o', this.deps.label, peer.toString(), error);
483
+ }
484
+ }
485
+
486
+ /**
487
+ * Latch {@link StrandRevocationEnforcerDeps.onSelfRevoked} to the TRANSITION
488
+ * into the revoked state: fired once on entry, and re-armed the moment this
489
+ * node is out of the set again — a manager can re-admit a party it removed
490
+ * (a fresh `Member` row makes the orphaned bindings live again), and a second
491
+ * removal has to be reported like the first.
492
+ */
493
+ private trackSelfRevoked(selfRevoked: boolean): void {
494
+ if (!selfRevoked) {
495
+ this.selfRevokedSignaled = false;
496
+ return;
497
+ }
498
+ if (this.selfRevokedSignaled) {
499
+ return;
500
+ }
501
+ this.selfRevokedSignaled = true;
502
+ log('[%s] THIS node is revoked from the strand (removed or left) — signalling', this.deps.label);
503
+ try {
504
+ this.deps.onSelfRevoked?.();
505
+ } catch (error) {
506
+ log('[%s] onSelfRevoked handler threw: %o', this.deps.label, error);
507
+ }
508
+ }
509
+ }
510
+
511
+ /**
512
+ * The distinct peer ids of `network`'s open connections that are in `revoked`,
513
+ * as the `PeerId` objects `hangUp` takes — reusing each connection's own
514
+ * `remotePeer` rather than parsing the string back, so no peer-id codec is
515
+ * needed here. Self is excluded (a node holds no connection to itself, and
516
+ * hanging one up would be nonsense if it did), and a peer connected twice — say
517
+ * one direct and one relayed — yields ONE hangUp, which closes both. A throw
518
+ * mid-enumeration is logged and keeps whatever was collected before it: a
519
+ * partial sweep beats none, and the next refresh re-enumerates anyway.
520
+ */
521
+ function connectedRevokedPeers(
522
+ network: StrandRevocationNetwork,
523
+ revoked: ReadonlySet<string>,
524
+ selfPeerId: string,
525
+ label: string
526
+ ): PeerId[] {
527
+ const targets = new Map<string, PeerId>();
528
+ try {
529
+ for (const connection of network.getConnections()) {
530
+ const remotePeerId = connection.remotePeer.toString();
531
+ if (remotePeerId !== selfPeerId && revoked.has(remotePeerId)) {
532
+ targets.set(remotePeerId, connection.remotePeer);
533
+ }
534
+ }
535
+ } catch (error) {
536
+ log('[%s] connection enumeration failed — teardown skipped this pass: %o', label, error);
537
+ }
538
+ return [...targets.values()];
539
+ }
540
+
541
+ /**
542
+ * Derive the deny set: a peer id is revoked iff it has at least one ORPHANED
543
+ * binding (a `MemberPeer` row whose member key has no live `Member` row) AND no
544
+ * live-membered binding. The second conjunct is what lets a peer id bound to
545
+ * TWO member keys — revoked under one, live under the other — keep serving the
546
+ * live member: a machine serving a live member must not be cut. Per-binding,
547
+ * not per-member, so every machine of a multi-device removed party lands in
548
+ * the set.
549
+ */
550
+ function deriveRevokedPeerIds(rows: StrandRevocationRows): Set<string> {
551
+ const orphaned = new Set<string>();
552
+ const live = new Set<string>();
553
+ for (const binding of rows.bindings) {
554
+ (rows.memberKeys.has(binding.memberKey) ? live : orphaned).add(binding.peerId);
555
+ }
556
+ for (const peerId of live) {
557
+ orphaned.delete(peerId);
558
+ }
559
+ return orphaned;
560
+ }
561
+
562
+ /** The one capability the gater composition needs from the enforcer. */
563
+ export type StrandRevocationJudge = Pick<StrandRevocationEnforcer, 'isRevoked'>;
564
+
565
+ /**
566
+ * Build a closed strand's connection gater: the caller-supplied gater (if any)
567
+ * with revoked-peer denial composed onto three hooks. The opportunistic
568
+ * SECONDARY layer — the per-stream gate above is the fail-closed primary.
569
+ *
570
+ * - `denyInboundEncryptedConnection`: deny iff revoked, so a removed party's
571
+ * node is refused before any protocol negotiation.
572
+ * - `denyDialPeer`: deny iff revoked — the owner ruling covers "communicated
573
+ * with", not just "answered to", and refusing the dial locally is instant,
574
+ * which keeps Optimystic from wasting a dial timeout on a peer this node
575
+ * will not talk to anyway.
576
+ * - `denyInboundRelayReservation`: deny iff revoked — matters when the strand
577
+ * node runs the relay server (the storage-profile default), where admitting
578
+ * the reservation would let a removed party keep using this node's
579
+ * forwarding capacity.
580
+ *
581
+ * Composition semantics: every hook of `base` is preserved as-is; on the three
582
+ * composed hooks a deny from EITHER the base gater or the revocation check
583
+ * denies, and an error (or a decision slower than `decisionTimeoutMs`) in
584
+ * either is fail-open — the base error admits through to the revocation check,
585
+ * a revocation-check error admits outright, and the fail-closed stream gate
586
+ * still stands behind both. The deadline is belt-and-braces: the snapshot read
587
+ * is synchronous.
588
+ *
589
+ * NOTE: accepted tradeoff — swallowing a BASE hook's throw diverges from
590
+ * `createMembershipConnectionGater`, which lets it propagate to libp2p (a
591
+ * fail-closed outcome there). Uniform fail-open was chosen for this module so
592
+ * every hook has one failure direction, and it was the shape the plan specified;
593
+ * the cost is that an embedder gater which throws is silently admitted here
594
+ * while the same gater passed raw to an OPEN strand would refuse. Loud in the
595
+ * log either way. Revisit if an embedder ever ships a gater whose throw is a
596
+ * meaningful deny, or if the two gaters are unified.
597
+ *
598
+ * NOTE: `base` is spread, so a gater passed as a CLASS INSTANCE would lose its
599
+ * prototype methods — same caveat as `createMembershipConnectionGater`; every
600
+ * caller in this repo supplies a plain object (the RN/web permissive gaters,
601
+ * e.g. `{ denyDialMultiaddr: () => false }`, keep working unchanged).
602
+ */
603
+ export function createRevocationConnectionGater(
604
+ enforcer: StrandRevocationJudge,
605
+ base?: ConnectionGater,
606
+ decisionTimeoutMs: number = ADMISSION_DECISION_TIMEOUT_MS
607
+ ): ConnectionGater {
608
+ /** The base gater's verdict, fail-open: an error or absence is "not denied". */
609
+ const baseDenies = async (decide: () => Promise<boolean> | boolean | undefined, hook: string): Promise<boolean> => {
610
+ try {
611
+ return await decide() === true;
612
+ } catch (error) {
613
+ log('base gater %s threw — treating as not-denied (fail-open): %o', hook, error);
614
+ return false;
615
+ }
616
+ };
617
+ /** The revocation verdict, bounded and fail-open. */
618
+ const revokedDenies = async (peerId: PeerId, hook: string): Promise<boolean> => {
619
+ const remotePeerId = peerId.toString();
620
+ try {
621
+ const revoked = await decideWithinDeadline(
622
+ () => enforcer.isRevoked(remotePeerId), false, decisionTimeoutMs, `${hook}(${remotePeerId})`
623
+ );
624
+ if (revoked) {
625
+ log('%s: %s is revoked — denying', hook, remotePeerId);
626
+ }
627
+ return revoked;
628
+ } catch (error) {
629
+ log('%s revocation check threw for %s — admitting (fail-open; stream gate decides): %o', hook, remotePeerId, error);
630
+ return false;
631
+ }
632
+ };
633
+ return {
634
+ ...base,
635
+ denyDialPeer: async (peerId: PeerId): Promise<boolean> => {
636
+ if (await baseDenies(() => base?.denyDialPeer?.(peerId), 'denyDialPeer')) {
637
+ return true;
638
+ }
639
+ return revokedDenies(peerId, 'denyDialPeer');
640
+ },
641
+ denyInboundEncryptedConnection: async (peerId: PeerId, maConn: MultiaddrConnection): Promise<boolean> => {
642
+ if (await baseDenies(() => base?.denyInboundEncryptedConnection?.(peerId, maConn), 'denyInboundEncryptedConnection')) {
643
+ return true;
644
+ }
645
+ return revokedDenies(peerId, 'denyInboundEncryptedConnection');
646
+ },
647
+ denyInboundRelayReservation: async (peerId: PeerId): Promise<boolean> => {
648
+ if (await baseDenies(() => base?.denyInboundRelayReservation?.(peerId), 'denyInboundRelayReservation')) {
649
+ return true;
650
+ }
651
+ return revokedDenies(peerId, 'denyInboundRelayReservation');
652
+ }
653
+ };
654
+ }