@serfab/cadre-core 0.12.0 → 1.0.0

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