@optimystic/db-core 0.22.0 → 0.24.1

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 (141) hide show
  1. package/README.md +336 -336
  2. package/dist/src/cluster/structs.d.ts +39 -1
  3. package/dist/src/cluster/structs.d.ts.map +1 -1
  4. package/dist/src/cluster/structs.js +24 -0
  5. package/dist/src/cluster/structs.js.map +1 -1
  6. package/dist/src/collection/collection.d.ts +17 -0
  7. package/dist/src/collection/collection.d.ts.map +1 -1
  8. package/dist/src/collection/collection.js +24 -2
  9. package/dist/src/collection/collection.js.map +1 -1
  10. package/dist/src/collections/tree/tree.d.ts +5 -0
  11. package/dist/src/collections/tree/tree.d.ts.map +1 -1
  12. package/dist/src/collections/tree/tree.js +7 -0
  13. package/dist/src/collections/tree/tree.js.map +1 -1
  14. package/dist/src/network/i-peer-network.d.ts +16 -0
  15. package/dist/src/network/i-peer-network.d.ts.map +1 -1
  16. package/dist/src/network/struct.d.ts +39 -2
  17. package/dist/src/network/struct.d.ts.map +1 -1
  18. package/dist/src/network/struct.js +18 -0
  19. package/dist/src/network/struct.js.map +1 -1
  20. package/dist/src/testing/test-transactor.d.ts +95 -8
  21. package/dist/src/testing/test-transactor.d.ts.map +1 -1
  22. package/dist/src/testing/test-transactor.js +121 -8
  23. package/dist/src/testing/test-transactor.js.map +1 -1
  24. package/dist/src/transaction/transaction.d.ts +1 -1
  25. package/dist/src/transaction/transaction.js +1 -1
  26. package/dist/src/transactor/network-transactor.d.ts.map +1 -1
  27. package/dist/src/transactor/network-transactor.js +48 -13
  28. package/dist/src/transactor/network-transactor.js.map +1 -1
  29. package/dist/src/transactor/transactor-source.d.ts.map +1 -1
  30. package/dist/src/transactor/transactor-source.js +25 -2
  31. package/dist/src/transactor/transactor-source.js.map +1 -1
  32. package/package.json +1 -1
  33. package/src/cluster/membership.ts +85 -85
  34. package/src/cluster/structs.ts +43 -4
  35. package/src/cohort-topic/addressing.ts +120 -120
  36. package/src/cohort-topic/antidos/bootstrap-evidence-envelope.ts +253 -253
  37. package/src/cohort-topic/antidos/bootstrap-evidence.ts +106 -106
  38. package/src/cohort-topic/antidos/index.ts +5 -5
  39. package/src/cohort-topic/antidos/rate-limiter.ts +210 -210
  40. package/src/cohort-topic/antidos/replay-guard.ts +146 -146
  41. package/src/cohort-topic/antidos/topic-budget.ts +160 -160
  42. package/src/cohort-topic/antiflood/index.ts +2 -2
  43. package/src/cohort-topic/antiflood/invariants.ts +108 -108
  44. package/src/cohort-topic/antiflood/jitter.ts +117 -117
  45. package/src/cohort-topic/coldstart.ts +237 -237
  46. package/src/cohort-topic/dmax.ts +88 -88
  47. package/src/cohort-topic/gossip/bus.ts +254 -254
  48. package/src/cohort-topic/gossip/index.ts +3 -3
  49. package/src/cohort-topic/gossip/records.ts +45 -45
  50. package/src/cohort-topic/gossip/view.ts +91 -91
  51. package/src/cohort-topic/index.ts +20 -20
  52. package/src/cohort-topic/load/barometer.ts +134 -134
  53. package/src/cohort-topic/load/index.ts +1 -1
  54. package/src/cohort-topic/member-engine.ts +430 -430
  55. package/src/cohort-topic/membership/index.ts +3 -3
  56. package/src/cohort-topic/membership/publisher.ts +163 -163
  57. package/src/cohort-topic/membership/source.ts +41 -41
  58. package/src/cohort-topic/membership/verifier.ts +461 -461
  59. package/src/cohort-topic/ports.ts +157 -157
  60. package/src/cohort-topic/promotion.ts +405 -405
  61. package/src/cohort-topic/registration/bytes.ts +37 -37
  62. package/src/cohort-topic/registration/handoff.ts +154 -154
  63. package/src/cohort-topic/registration/index.ts +6 -6
  64. package/src/cohort-topic/registration/renewal.ts +495 -495
  65. package/src/cohort-topic/registration/sharding.ts +61 -61
  66. package/src/cohort-topic/registration/store.ts +81 -81
  67. package/src/cohort-topic/registration/types.ts +91 -91
  68. package/src/cohort-topic/ring-hash.ts +50 -50
  69. package/src/cohort-topic/service.ts +416 -416
  70. package/src/cohort-topic/sig/index.ts +2 -2
  71. package/src/cohort-topic/sig/payloads.ts +59 -59
  72. package/src/cohort-topic/sig/threshold.ts +64 -64
  73. package/src/cohort-topic/tiers.ts +74 -74
  74. package/src/cohort-topic/traffic.ts +233 -233
  75. package/src/cohort-topic/walk.ts +326 -326
  76. package/src/cohort-topic/willingness.ts +237 -237
  77. package/src/cohort-topic/wire/codec.ts +216 -216
  78. package/src/cohort-topic/wire/index.ts +18 -18
  79. package/src/cohort-topic/wire/payloads.ts +126 -126
  80. package/src/cohort-topic/wire/primitives.ts +188 -188
  81. package/src/cohort-topic/wire/types.ts +475 -475
  82. package/src/cohort-topic/wire/validate.ts +512 -512
  83. package/src/collection/collection-type-registry.ts +37 -37
  84. package/src/collection/collection.ts +25 -2
  85. package/src/collections/diary/diary.ts +68 -68
  86. package/src/collections/tree/readme.md +4 -0
  87. package/src/collections/tree/tree.ts +320 -312
  88. package/src/matchmaking/capability-filter.ts +45 -45
  89. package/src/matchmaking/config.ts +98 -98
  90. package/src/matchmaking/index.ts +21 -21
  91. package/src/matchmaking/multi-cohort-seeker.ts +234 -234
  92. package/src/matchmaking/provider.ts +123 -123
  93. package/src/matchmaking/query-eval.ts +105 -105
  94. package/src/matchmaking/seeker-walk.ts +127 -127
  95. package/src/matchmaking/seeker.ts +86 -86
  96. package/src/matchmaking/topic-anchor.ts +90 -90
  97. package/src/matchmaking/voting-quorum.ts +394 -394
  98. package/src/matchmaking/wire.ts +603 -603
  99. package/src/network/i-peer-network.ts +17 -0
  100. package/src/network/stale-failure.ts +43 -43
  101. package/src/network/struct.ts +41 -2
  102. package/src/network/types.ts +37 -37
  103. package/src/reactivity/backfill.ts +220 -220
  104. package/src/reactivity/backpressure.ts +191 -191
  105. package/src/reactivity/checkpoint.ts +308 -308
  106. package/src/reactivity/config.ts +172 -172
  107. package/src/reactivity/dedupe.ts +132 -132
  108. package/src/reactivity/forwarder.ts +87 -87
  109. package/src/reactivity/index.ts +34 -34
  110. package/src/reactivity/notification.ts +123 -123
  111. package/src/reactivity/policy.ts +79 -79
  112. package/src/reactivity/push-state.ts +310 -310
  113. package/src/reactivity/recover.ts +153 -153
  114. package/src/reactivity/replay-buffer.ts +141 -141
  115. package/src/reactivity/resume.ts +549 -549
  116. package/src/reactivity/rotation.ts +415 -415
  117. package/src/reactivity/subscriber.ts +132 -132
  118. package/src/reactivity/subscription.ts +66 -66
  119. package/src/reactivity/topic-anchor.ts +71 -71
  120. package/src/reactivity/verify.ts +73 -73
  121. package/src/reactivity/wire-validate.ts +13 -13
  122. package/src/reactivity/wire.ts +224 -224
  123. package/src/testing/async-wait.ts +65 -65
  124. package/src/testing/index.ts +2 -2
  125. package/src/testing/test-transactor.ts +638 -502
  126. package/src/transaction/errors.ts +91 -91
  127. package/src/transaction/operations-hash.ts +196 -196
  128. package/src/transaction/read-dependency-collector.ts +78 -78
  129. package/src/transaction/transaction.ts +1 -1
  130. package/src/transactor/change-notifier.ts +80 -80
  131. package/src/transactor/index.ts +5 -5
  132. package/src/transactor/network-transactor.ts +49 -14
  133. package/src/transactor/transactor-source.ts +25 -2
  134. package/src/transform/atomic-proxy.ts +92 -92
  135. package/src/transform/helpers.ts +159 -159
  136. package/src/utility/backoff.ts +95 -95
  137. package/src/utility/batch-coordinator.ts +191 -191
  138. package/dist/src/transaction/context.d.ts +0 -60
  139. package/dist/src/transaction/context.d.ts.map +0 -1
  140. package/dist/src/transaction/context.js +0 -91
  141. package/dist/src/transaction/context.js.map +0 -1
@@ -1,430 +1,430 @@
1
- /**
2
- * Cohort-topic substrate — cohort-side member engine.
3
- *
4
- * This is the logic a cohort member runs when FRET's `RouteAndMaybeAct` lands a `RegisterV1`/`RenewV1`
5
- * on it (the activity callback in `docs/cohort-topic.md` §FRET integration). It is the cohort-facing
6
- * half of the substrate, composed from the prereq tickets' pure modules — the participant-facing half
7
- * lives in {@link import("./service.js").CohortTopicService}. db-p2p's FRET host decodes an inbound
8
- * frame, supplies the routing context this module can't see (`followOn` — derived from the wire
9
- * `RegisterV1.followOn` flag, evidence-gated in step 1 — plus the quorum-willing tally), and serializes
10
- * whatever reply this returns back over the wire. db-core never imports FRET here.
11
- *
12
- * Inbound `RegisterV1` decision pipeline (§Registration mechanics, §Willingness, §Cold-start):
13
- *
14
- * 1. **Anti-DoS** — freshness/replay guard, per-(peer,topic) rate limit, and cold-root bootstrap
15
- * evidence. A failure short-circuits to `unwilling_cohort` (temporal back-off) or, for a replay,
16
- * a silent `no_state` (no state is served and nothing is recorded).
17
- * 2. **Hot vs cold** — if this cohort already serves the topic (forwarder state resident), run the
18
- * willingness check; a promoted topic answers `Promoted(d+1)`. If cold, the topic-budget +
19
- * cold-start gate decides whether to instantiate, else `no_state` (walk steps toward the root).
20
- * 3. **Admission** — on `accepted`, assign the deterministic primary/backup slots, persist the
21
- * soft-state record, count the arrival, run the promotion trigger, and attach the topic-traffic
22
- * signal to the reply.
23
- *
24
- * Every collaborator is injected, so the engine unit-tests without FRET or libp2p.
25
- */
26
-
27
- import type { IRingHash } from "./ports.js";
28
- import { bytesEqual, bytesKey } from "./registration/bytes.js";
29
- import type { RegistrationRecord, RegistrationStore } from "./registration/types.js";
30
- import { clampTtl } from "./registration/types.js";
31
- import type { SlotAssigner } from "./registration/sharding.js";
32
- import type { RenewalCohortSide } from "./registration/renewal.js";
33
- import type { WillingnessCheck } from "./willingness.js";
34
- import { backoffRetryMs } from "./willingness.js";
35
- import type { PromotionLifecycle } from "./promotion.js";
36
- import { promotedRedirectReply } from "./coldstart.js";
37
- import type { ColdStartManager } from "./coldstart.js";
38
- import { shouldInstantiate } from "./coldstart.js";
39
- import type { TrafficCounters } from "./traffic.js";
40
- import { attachTopicTraffic } from "./traffic.js";
41
- import type { RegisterRateLimiter } from "./antidos/rate-limiter.js";
42
- import type { CorrelationReplayGuard } from "./antidos/replay-guard.js";
43
- import type { TopicBudget } from "./antidos/topic-budget.js";
44
- import type { BootstrapEvidence } from "./antidos/bootstrap-evidence.js";
45
- import type { NodeProfile, Tier } from "./tiers.js";
46
- import { b64urlToBytes } from "./wire/codec.js";
47
- import type { DemotionNoticeV1, PromotionNoticeV1, RegisterReplyV1, RegisterV1, RenewReplyV1, RenewV1 } from "./wire/types.js";
48
-
49
- /** Routing context the FRET host supplies for an inbound register (db-core can't derive it). */
50
- export interface RegisterContext {
51
- /**
52
- * Did this register arrive as a follow-on to a parent cohort's `Promoted` redirect? The host derives
53
- * this from the wire `RegisterV1.followOn` flag (`followOn: reg.followOn === true`). It is participant-
54
- * asserted and therefore evidence-gated: `runGuards` (step 1) already ran `bootstrapEvidence.verify`,
55
- * which demands the same proof for `followOn: true` as for `bootstrap: true`, so an unbacked follow-on
56
- * never reaches this instantiation decision (it short-circuited to `unwilling_cohort`).
57
- */
58
- readonly followOn: boolean;
59
- /** Tree tier `d` this cohort serves the topic at (the walk position the register landed on). */
60
- readonly treeTier: number;
61
- /** The tier-`(d − 1)` parent cohort coord, for a cold-start forwarder's parent registration. */
62
- readonly parentCoord?: Uint8Array;
63
- }
64
-
65
- /** Current cohort snapshot — the FRET host fills this from the membership source. */
66
- export interface CohortSnapshotView {
67
- readonly members: readonly Uint8Array[];
68
- readonly cohortEpoch: Uint8Array;
69
- }
70
-
71
- export interface CohortMemberEngineDeps {
72
- /** This member's own peer id. */
73
- readonly self: Uint8Array;
74
- /** This member's static tier profile (Edge/Core willing-tier set). */
75
- readonly profile: NodeProfile;
76
- /** Hash used for slot assignment + keys (db-core's own SHA-256). */
77
- readonly hash: IRingHash;
78
- /** Replicated soft-state registration store. */
79
- readonly store: RegistrationStore;
80
- /** Deterministic primary/backup slot assignment, shared with renewal + handoff. */
81
- readonly slots: SlotAssigner;
82
- /** Per-member willingness / admission control. */
83
- readonly willingness: WillingnessCheck;
84
- /** Promotion / demotion lifecycle. */
85
- readonly promotion: PromotionLifecycle;
86
- /** Cold-start forwarder instantiation + parent registration. */
87
- readonly coldStart: ColdStartManager;
88
- /** Per-topic traffic counters (gossip-derived snapshots feed the reply signal). */
89
- readonly traffic: TrafficCounters;
90
- /** Cohort-side TTL renewal handler (touch / primary_moved / sweep). */
91
- readonly renewal: RenewalCohortSide;
92
- /** Live cohort snapshot (members + epoch). */
93
- readonly cohort: () => CohortSnapshotView;
94
- /** Whether a quorum of members is willing to serve `tier` (the cold-start gate input). */
95
- readonly quorumWilling: (tier: Tier) => boolean;
96
- // --- anti-DoS guards (all optional; absent = that gate is skipped) ---
97
- readonly rateLimiter?: RegisterRateLimiter;
98
- /**
99
- * Dedicated per-(peer, topic) rate limiter for the read-only {@link CohortMemberEngine.handleRegister}
100
- * **probe** path — its own budget, separate from {@link rateLimiter}, so a probe flood cannot exhaust a
101
- * participant's register budget (or vice-versa). An over-rate probe answers `unwilling_cohort`
102
- * (walk → `retry_later` → `CohortBackoffError`). Absent → the probe rate gate is skipped (the read-only
103
- * classify still runs), keeping key-less / unit / mock flows composing.
104
- */
105
- readonly probeRateLimiter?: RegisterRateLimiter;
106
- readonly replayGuard?: CorrelationReplayGuard;
107
- readonly topicBudget?: TopicBudget;
108
- readonly bootstrapEvidence?: BootstrapEvidence;
109
- /**
110
- * Optional participant peer-key signature verifier (db-p2p binds it to the peer-sig primitive over
111
- * {@link import("./wire/payloads.js").registerSigningPayload}). A `RegisterV1` whose signature does
112
- * not verify against the claimed `participantCoord` is answered `no_state` (serve nothing, record
113
- * nothing) — a cohort member cannot trust an unsigned/forged `participantCoord`. Verified first so a
114
- * forged frame never reaches (or pollutes) the replay/rate guards. Absent → the gate is skipped.
115
- */
116
- readonly verifyRegisterSig?: (reg: RegisterV1) => boolean;
117
- /**
118
- * Sink for a freshly threshold-signed promotion/demotion notice produced on an arrival (the `accept`
119
- * path's promotion trigger). db-p2p's host wires this to broadcast the notice over the `promote`
120
- * protocol to the cohort (and, for a demotion, the parent coord). Absent → notices are produced and
121
- * applied locally but not broadcast (unit/mock flows). Called fire-and-forget, off the reply path.
122
- */
123
- readonly onNotice?: (notice: PromotionNoticeV1 | DemotionNoticeV1) => void;
124
- /**
125
- * Optional logger. Used only to record a failed promotion sign/broadcast (the threshold-sign round
126
- * can reject when the cohort quorum is unreachable) rather than swallow it. Absent → silent.
127
- */
128
- readonly log?: (formatter: string, ...args: unknown[]) => void;
129
- /**
130
- * Sink for a freshly-admitted registration record (the `accept` path), symmetric to the renewal
131
- * `gossip.touch` hook. db-p2p wires this to the same per-coord gossip delta queue the renewal side
132
- * appends to, so an admitted record is replicated to the cohort at the next gossip round — closing the
133
- * durability window between admission and the participant's first renewal touch. A queue append, not a
134
- * synchronous broadcast: admission must not block on a round, and last-writer-wins by `lastPing` dedupes
135
- * an admit-then-touch landing in the same round. Absent → no admission-time replication (unit/mock flows
136
- * that don't exercise gossip).
137
- */
138
- readonly onAdmit?: (rec: RegistrationRecord) => void;
139
- }
140
-
141
- /** Cohort-side register/renew handler — the body of the FRET activity callback. */
142
- export interface CohortMemberEngine {
143
- /** Classify and admit (or decline) an inbound `RegisterV1`, returning the reply to serialize. */
144
- handleRegister(reg: RegisterV1, ctx: RegisterContext, now: number): Promise<RegisterReplyV1>;
145
- /** Handle an inbound `RenewV1` (ping / re-attach), delegating to the renewal cohort side. */
146
- handleRenew(msg: RenewV1, now: number): RenewReplyV1;
147
- /** Evict stale records and gossip each eviction; returns the evicted set. */
148
- sweepStale(now: number): readonly RegistrationRecord[];
149
- }
150
-
151
- class StoreCohortMemberEngine implements CohortMemberEngine {
152
- constructor(private readonly deps: CohortMemberEngineDeps) {}
153
-
154
- async handleRegister(reg: RegisterV1, ctx: RegisterContext, now: number): Promise<RegisterReplyV1> {
155
- const topicId = b64urlToBytes(reg.topicId);
156
- const participantId = b64urlToBytes(reg.participantCoord);
157
- const tier = reg.tier as Tier;
158
-
159
- // 0. Read-only lookup probe: classify + return the cohort snapshot without admitting anything.
160
- // Branches before the admission pipeline (and the durable-state anti-DoS guards), so even a
161
- // hand-crafted `probe: true, bootstrap: true` frame can never instantiate — defense in depth.
162
- if (reg.probe === true) {
163
- return this.handleProbe(reg, topicId, participantId, ctx, now);
164
- }
165
-
166
- // 1. Anti-DoS gates. A replay/stale frame is answered with NoState (serve nothing, record
167
- // nothing); an over-rate source or missing bootstrap evidence backs off in time.
168
- const guard = this.runGuards(reg, topicId, participantId, tier, now);
169
- if (guard !== undefined) {
170
- return guard;
171
- }
172
-
173
- // 2. Hot path: this cohort already serves the topic.
174
- if (this.serves(topicId)) {
175
- if (this.deps.promotion.isPromoted(topicId)) {
176
- // Promoted: bounce same-tier registrations onward (the cheap single-RPC redirect).
177
- return promotedRedirectReply(ctx.treeTier + 1, this.deps.traffic.snapshot(topicId));
178
- }
179
- return this.admitOrDecline(reg, topicId, participantId, tier, ctx, now);
180
- }
181
-
182
- // 3. Cold path: instantiate only at a legitimate growth point with a willing quorum. `ctx.followOn`
183
- // mirrors the wire `reg.followOn` flag (the host derives it), and by the time we reach here the
184
- // step-1 bootstrap-evidence gate has already verified a `followOn: true` carries the same proof a
185
- // `bootstrap: true` root cold-start carries — so this branch never instantiates an unbacked follow-on.
186
- const quorumWilling = this.deps.quorumWilling(tier);
187
- if (!shouldInstantiate({ bootstrap: reg.bootstrap === true, followOn: ctx.followOn, quorumWilling })) {
188
- return { v: 1, result: "no_state" };
189
- }
190
- if (this.deps.topicBudget?.admit(topicId) === false) {
191
- // Forwarder-state budget is full of populated topics — decline in time.
192
- return this.unwillingCohort(reg, now);
193
- }
194
- this.deps.coldStart.instantiate(topicId, ctx.treeTier, ctx.parentCoord, tier);
195
- return this.admitOrDecline(reg, topicId, participantId, tier, ctx, now);
196
- }
197
-
198
- /**
199
- * Read-only resolution of the cohort for a topic (the `lookup` probe). It walks the same path a
200
- * register does (the walk loop is byte-for-byte identical) but the **terminal** member action is a
201
- * classify, never an admission: it persists **no** record, counts **no** arrival, fires **no**
202
- * promotion trigger, touches **no** topic budget, and **never** instantiates a cold-start forwarder.
203
- *
204
- * DoS posture (`docs/cohort-topic.md` §Anti-DoS): a probe still runs the stateless participant-sig gate
205
- * (forged → `no_state`) and a dedicated probe rate limiter (over-rate → `unwilling_cohort`), but skips
206
- * the replay guard, the bootstrap-evidence gate, and the topic budget — an idempotent read records
207
- * nothing, so it is strictly cheaper than the register it replaces.
208
- *
209
- * Classification mirrors a register's terminal cases so the walk's existing branches drive a probe
210
- * unchanged: a served-and-promoted topic answers `promoted(treeTier + 1)` (the walk follows it), a
211
- * served topic answers `accepted` with the participant-specific slot assignment + read-only traffic
212
- * snapshot, and a topic this cohort does not serve answers `no_state` (the walk steps inward).
213
- */
214
- private handleProbe(reg: RegisterV1, topicId: Uint8Array, participantId: Uint8Array, ctx: RegisterContext, now: number): RegisterReplyV1 {
215
- if (this.deps.verifyRegisterSig?.(reg) === false) {
216
- // Unsigned / forged participant signature: serve nothing, exactly like the register path — a
217
- // cohort member never resolves a cohort snapshot for an untrusted `participantCoord`.
218
- return { v: 1, result: "no_state" };
219
- }
220
- const rate = this.deps.probeRateLimiter?.check(participantId, topicId, now);
221
- if (rate !== undefined && rate.ok === false) {
222
- return { v: 1, result: "unwilling_cohort", retryAfterMs: rate.retryAfterMs };
223
- }
224
- if (this.serves(topicId)) {
225
- if (this.deps.promotion.isPromoted(topicId)) {
226
- return promotedRedirectReply(ctx.treeTier + 1, this.deps.traffic.snapshot(topicId));
227
- }
228
- const { members, cohortEpoch } = this.deps.cohort();
229
- const { primary, backups } = this.deps.slots.assignSlots(participantId, cohortEpoch, members);
230
- const reply: RegisterReplyV1 = {
231
- v: 1,
232
- result: "accepted",
233
- primary: bytesKey(primary),
234
- backups: backups.map(bytesKey),
235
- cohortEpoch: bytesKey(cohortEpoch),
236
- cohortMembers: members.map(bytesKey),
237
- };
238
- return attachTopicTraffic(reply, this.deps.traffic.snapshot(topicId));
239
- }
240
- return { v: 1, result: "no_state" };
241
- }
242
-
243
- handleRenew(msg: RenewV1, now: number): RenewReplyV1 {
244
- const reply = this.deps.renewal.onRenew(msg, now);
245
- if (reply.result === "ok") {
246
- // A renewal is an arrival proxy for the traffic barometer (§Topic traffic signal).
247
- this.deps.traffic.recordArrival(b64urlToBytes(msg.topicId), now);
248
- } else if (reply.result === "withdrawn") {
249
- // A withdraw evicted a direct participant: re-touch the topic budget DOWN (mirrors the
250
- // sweepStale drain release below) so the freed slot does not leak. Never an arrival — it is a
251
- // departure, so the traffic barometer is deliberately not touched.
252
- const topicId = b64urlToBytes(msg.topicId);
253
- this.deps.topicBudget?.touch(topicId, this.deps.store.directParticipants(topicId));
254
- }
255
- return reply;
256
- }
257
-
258
- sweepStale(now: number): readonly RegistrationRecord[] {
259
- // Reclaim idle (peer, topic) rate-limiter keys on the existing gossip cadence. The limiters' own
260
- // hard LRU cap already bounds worst-case footprint; this sweep adds proactive steady-state reclaim
261
- // of long-quiet keys so a long-running coord does not hold register/probe state for departed peers.
262
- this.deps.rateLimiter?.sweep(now);
263
- this.deps.probeRateLimiter?.sweep(now);
264
-
265
- const evicted = this.deps.renewal.sweepStale(now);
266
- const budget = this.deps.topicBudget;
267
- if (budget !== undefined && evicted.length > 0) {
268
- // Mirror the `accept()` up-touch on the drain side: a TTL sweep that removes a topic's last
269
- // direct participant must re-`touch` the budget down so the topic falls to participantCount 0
270
- // and becomes the coldest-evictable resident again. Without this the budget slot leaks — a
271
- // drained topic keeps its stale positive count forever and `coldestEvictable()` never picks it,
272
- // so the cohort eventually refuses every new topic while serving nothing. Re-touch once per
273
- // distinct affected topic from `store.directParticipants` (the source of truth, post-eviction).
274
- const seen = new Set<string>();
275
- for (const rec of evicted) {
276
- const key = bytesKey(rec.topicId);
277
- if (seen.has(key)) continue;
278
- seen.add(key);
279
- budget.touch(rec.topicId, this.deps.store.directParticipants(rec.topicId));
280
- }
281
- }
282
- return evicted;
283
- }
284
-
285
- // --- admission ---
286
-
287
- /** Run the willingness check; on `accepted` persist the record and attach the traffic signal. */
288
- private async admitOrDecline(
289
- reg: RegisterV1,
290
- topicId: Uint8Array,
291
- participantId: Uint8Array,
292
- tier: Tier,
293
- ctx: RegisterContext,
294
- now: number,
295
- ): Promise<RegisterReplyV1> {
296
- const outcome = this.deps.willingness.evaluate(reg, this.deps.profile, now);
297
- switch (outcome.kind) {
298
- case "unwilling_member": {
299
- return { v: 1, result: "unwilling_member", candidateMembers: outcome.candidateMembers.map(bytesKey) };
300
- }
301
- case "unwilling_cohort": {
302
- return { v: 1, result: "unwilling_cohort", retryAfterMs: outcome.retryAfterMs };
303
- }
304
- case "accepted": {
305
- return this.accept(reg, topicId, participantId, tier, ctx, now);
306
- }
307
- default: {
308
- return this.unwillingCohort(reg, now);
309
- }
310
- }
311
- }
312
-
313
- private async accept(
314
- reg: RegisterV1,
315
- topicId: Uint8Array,
316
- participantId: Uint8Array,
317
- tier: Tier,
318
- ctx: RegisterContext,
319
- now: number,
320
- ): Promise<RegisterReplyV1> {
321
- const { members, cohortEpoch } = this.deps.cohort();
322
- const { primary, backups } = this.deps.slots.assignSlots(participantId, cohortEpoch, members);
323
- const ttl = clampTtl(reg.ttl);
324
- const record: RegistrationRecord = {
325
- topicId,
326
- participantId,
327
- tier,
328
- primary,
329
- backups,
330
- attachedAt: now,
331
- lastPing: now,
332
- ttl,
333
- appState: reg.appPayload === undefined ? undefined : b64urlToBytes(reg.appPayload),
334
- };
335
- this.deps.store.put(record);
336
- this.deps.onAdmit?.(record);
337
- this.deps.traffic.recordArrival(topicId, now);
338
- this.deps.topicBudget?.touch(topicId, this.deps.store.directParticipants(topicId));
339
- // Promotion may fire on this arrival; broadcast whatever notice comes back. Fire-and-forget so the
340
- // register reply is not delayed by the threshold-sign collection round.
341
- void this.firePromotion(topicId, now);
342
-
343
- const reply: RegisterReplyV1 = {
344
- v: 1,
345
- result: "accepted",
346
- primary: bytesKey(primary),
347
- backups: backups.map(bytesKey),
348
- cohortEpoch: bytesKey(cohortEpoch),
349
- cohortMembers: members.map(bytesKey),
350
- };
351
- return attachTopicTraffic(reply, this.deps.traffic.snapshot(topicId));
352
- }
353
-
354
- /** Run the promotion trigger for an arrival and hand any signed notice to {@link CohortMemberEngineDeps.onNotice}. */
355
- private async firePromotion(topicId: Uint8Array, now: number): Promise<void> {
356
- try {
357
- const notice = await this.deps.promotion.onParticipantCountChange(topicId, now);
358
- if (notice !== undefined) {
359
- this.deps.onNotice?.(notice);
360
- }
361
- } catch (err) {
362
- // Threshold signing rejects when the cohort quorum is unreachable this round; the next arrival
363
- // re-fires, so this is transient. Log rather than crash the register path — or leak the
364
- // unhandled rejection the bare `void promotion...` did once signing became real (db-p2p ticket 1).
365
- this.deps.log?.("cohort-topic: promotion sign/broadcast failed for an arrival: %o", err);
366
- }
367
- }
368
-
369
- // --- guards ---
370
-
371
- /** Returns a short-circuit reply if any anti-DoS gate rejects `reg`, else `undefined`. */
372
- private runGuards(
373
- reg: RegisterV1,
374
- topicId: Uint8Array,
375
- participantId: Uint8Array,
376
- tier: Tier,
377
- now: number,
378
- ): RegisterReplyV1 | undefined {
379
- if (this.deps.verifyRegisterSig?.(reg) === false) {
380
- // Unsigned / forged participant signature: serve nothing and record nothing (the cohort
381
- // cannot trust this participantCoord). Cheap + stateless, so checked first — a forged frame
382
- // cannot consume any downstream guard state.
383
- return { v: 1, result: "no_state" };
384
- }
385
- // Rate-check BEFORE the replay guard records anything: a fresh-correlationId spam frame that the
386
- // rate limiter would reject must not first insert a `seen` entry (which would let an attacker grow
387
- // the replay guard's memory at full attack speed regardless of the rate limit). Running it before
388
- // the potentially-expensive bootstrap verify (which may do PoW) also short-circuits floods sooner.
389
- // NOTE: a replayed frame now consumes a rate-limiter accept before the replay guard (below) drops it,
390
- // whereas the old order dropped replays pre-rate. Fine for spam (inbound traffic should be counted);
391
- // only matters if a legitimate client reuses one correlationId across retransmits — it would then
392
- // burn rate budget per retransmit. If clients adopt idempotent same-cid retry, revisit this order.
393
- const rate = this.deps.rateLimiter?.check(participantId, topicId, now);
394
- if (rate !== undefined && rate.ok === false) {
395
- return { v: 1, result: "unwilling_cohort", retryAfterMs: rate.retryAfterMs };
396
- }
397
- if (this.deps.bootstrapEvidence?.verify(reg, tier) === false) {
398
- return this.unwillingCohort(reg, now);
399
- }
400
- // Replay guard records LAST, so only frames that passed signature + rate + bootstrap ever insert a
401
- // `seen` entry. An *accepted* frame is still always recorded here, so a genuine replay of a served
402
- // correlationId is still caught; only rate-rejected frames (never served, never recorded) skip it.
403
- const correlationId = b64urlToBytes(reg.correlationId);
404
- if (this.deps.replayGuard?.accept(correlationId, participantId, reg.timestamp, now) === false) {
405
- // Stale / future-skewed / replayed: serve nothing and record nothing.
406
- return { v: 1, result: "no_state" };
407
- }
408
- return undefined;
409
- }
410
-
411
- private serves(topicId: Uint8Array): boolean {
412
- return this.deps.coldStart.get(topicId) !== undefined || this.deps.store.directParticipants(topicId) > 0;
413
- }
414
-
415
- private unwillingCohort(reg: RegisterV1, now: number): RegisterReplyV1 {
416
- void reg;
417
- void now;
418
- return { v: 1, result: "unwilling_cohort", retryAfterMs: backoffRetryMs(0) };
419
- }
420
- }
421
-
422
- /** Build a {@link CohortMemberEngine} over the injected cohort-side collaborators. */
423
- export function createCohortMemberEngine(deps: CohortMemberEngineDeps): CohortMemberEngine {
424
- return new StoreCohortMemberEngine(deps);
425
- }
426
-
427
- /** True iff `a` and `b` name the same peer (re-exported convenience for host equality checks). */
428
- export function samePeer(a: Uint8Array, b: Uint8Array): boolean {
429
- return bytesEqual(a, b);
430
- }
1
+ /**
2
+ * Cohort-topic substrate — cohort-side member engine.
3
+ *
4
+ * This is the logic a cohort member runs when FRET's `RouteAndMaybeAct` lands a `RegisterV1`/`RenewV1`
5
+ * on it (the activity callback in `docs/cohort-topic.md` §FRET integration). It is the cohort-facing
6
+ * half of the substrate, composed from the prereq tickets' pure modules — the participant-facing half
7
+ * lives in {@link import("./service.js").CohortTopicService}. db-p2p's FRET host decodes an inbound
8
+ * frame, supplies the routing context this module can't see (`followOn` — derived from the wire
9
+ * `RegisterV1.followOn` flag, evidence-gated in step 1 — plus the quorum-willing tally), and serializes
10
+ * whatever reply this returns back over the wire. db-core never imports FRET here.
11
+ *
12
+ * Inbound `RegisterV1` decision pipeline (§Registration mechanics, §Willingness, §Cold-start):
13
+ *
14
+ * 1. **Anti-DoS** — freshness/replay guard, per-(peer,topic) rate limit, and cold-root bootstrap
15
+ * evidence. A failure short-circuits to `unwilling_cohort` (temporal back-off) or, for a replay,
16
+ * a silent `no_state` (no state is served and nothing is recorded).
17
+ * 2. **Hot vs cold** — if this cohort already serves the topic (forwarder state resident), run the
18
+ * willingness check; a promoted topic answers `Promoted(d+1)`. If cold, the topic-budget +
19
+ * cold-start gate decides whether to instantiate, else `no_state` (walk steps toward the root).
20
+ * 3. **Admission** — on `accepted`, assign the deterministic primary/backup slots, persist the
21
+ * soft-state record, count the arrival, run the promotion trigger, and attach the topic-traffic
22
+ * signal to the reply.
23
+ *
24
+ * Every collaborator is injected, so the engine unit-tests without FRET or libp2p.
25
+ */
26
+
27
+ import type { IRingHash } from "./ports.js";
28
+ import { bytesEqual, bytesKey } from "./registration/bytes.js";
29
+ import type { RegistrationRecord, RegistrationStore } from "./registration/types.js";
30
+ import { clampTtl } from "./registration/types.js";
31
+ import type { SlotAssigner } from "./registration/sharding.js";
32
+ import type { RenewalCohortSide } from "./registration/renewal.js";
33
+ import type { WillingnessCheck } from "./willingness.js";
34
+ import { backoffRetryMs } from "./willingness.js";
35
+ import type { PromotionLifecycle } from "./promotion.js";
36
+ import { promotedRedirectReply } from "./coldstart.js";
37
+ import type { ColdStartManager } from "./coldstart.js";
38
+ import { shouldInstantiate } from "./coldstart.js";
39
+ import type { TrafficCounters } from "./traffic.js";
40
+ import { attachTopicTraffic } from "./traffic.js";
41
+ import type { RegisterRateLimiter } from "./antidos/rate-limiter.js";
42
+ import type { CorrelationReplayGuard } from "./antidos/replay-guard.js";
43
+ import type { TopicBudget } from "./antidos/topic-budget.js";
44
+ import type { BootstrapEvidence } from "./antidos/bootstrap-evidence.js";
45
+ import type { NodeProfile, Tier } from "./tiers.js";
46
+ import { b64urlToBytes } from "./wire/codec.js";
47
+ import type { DemotionNoticeV1, PromotionNoticeV1, RegisterReplyV1, RegisterV1, RenewReplyV1, RenewV1 } from "./wire/types.js";
48
+
49
+ /** Routing context the FRET host supplies for an inbound register (db-core can't derive it). */
50
+ export interface RegisterContext {
51
+ /**
52
+ * Did this register arrive as a follow-on to a parent cohort's `Promoted` redirect? The host derives
53
+ * this from the wire `RegisterV1.followOn` flag (`followOn: reg.followOn === true`). It is participant-
54
+ * asserted and therefore evidence-gated: `runGuards` (step 1) already ran `bootstrapEvidence.verify`,
55
+ * which demands the same proof for `followOn: true` as for `bootstrap: true`, so an unbacked follow-on
56
+ * never reaches this instantiation decision (it short-circuited to `unwilling_cohort`).
57
+ */
58
+ readonly followOn: boolean;
59
+ /** Tree tier `d` this cohort serves the topic at (the walk position the register landed on). */
60
+ readonly treeTier: number;
61
+ /** The tier-`(d − 1)` parent cohort coord, for a cold-start forwarder's parent registration. */
62
+ readonly parentCoord?: Uint8Array;
63
+ }
64
+
65
+ /** Current cohort snapshot — the FRET host fills this from the membership source. */
66
+ export interface CohortSnapshotView {
67
+ readonly members: readonly Uint8Array[];
68
+ readonly cohortEpoch: Uint8Array;
69
+ }
70
+
71
+ export interface CohortMemberEngineDeps {
72
+ /** This member's own peer id. */
73
+ readonly self: Uint8Array;
74
+ /** This member's static tier profile (Edge/Core willing-tier set). */
75
+ readonly profile: NodeProfile;
76
+ /** Hash used for slot assignment + keys (db-core's own SHA-256). */
77
+ readonly hash: IRingHash;
78
+ /** Replicated soft-state registration store. */
79
+ readonly store: RegistrationStore;
80
+ /** Deterministic primary/backup slot assignment, shared with renewal + handoff. */
81
+ readonly slots: SlotAssigner;
82
+ /** Per-member willingness / admission control. */
83
+ readonly willingness: WillingnessCheck;
84
+ /** Promotion / demotion lifecycle. */
85
+ readonly promotion: PromotionLifecycle;
86
+ /** Cold-start forwarder instantiation + parent registration. */
87
+ readonly coldStart: ColdStartManager;
88
+ /** Per-topic traffic counters (gossip-derived snapshots feed the reply signal). */
89
+ readonly traffic: TrafficCounters;
90
+ /** Cohort-side TTL renewal handler (touch / primary_moved / sweep). */
91
+ readonly renewal: RenewalCohortSide;
92
+ /** Live cohort snapshot (members + epoch). */
93
+ readonly cohort: () => CohortSnapshotView;
94
+ /** Whether a quorum of members is willing to serve `tier` (the cold-start gate input). */
95
+ readonly quorumWilling: (tier: Tier) => boolean;
96
+ // --- anti-DoS guards (all optional; absent = that gate is skipped) ---
97
+ readonly rateLimiter?: RegisterRateLimiter;
98
+ /**
99
+ * Dedicated per-(peer, topic) rate limiter for the read-only {@link CohortMemberEngine.handleRegister}
100
+ * **probe** path — its own budget, separate from {@link rateLimiter}, so a probe flood cannot exhaust a
101
+ * participant's register budget (or vice-versa). An over-rate probe answers `unwilling_cohort`
102
+ * (walk → `retry_later` → `CohortBackoffError`). Absent → the probe rate gate is skipped (the read-only
103
+ * classify still runs), keeping key-less / unit / mock flows composing.
104
+ */
105
+ readonly probeRateLimiter?: RegisterRateLimiter;
106
+ readonly replayGuard?: CorrelationReplayGuard;
107
+ readonly topicBudget?: TopicBudget;
108
+ readonly bootstrapEvidence?: BootstrapEvidence;
109
+ /**
110
+ * Optional participant peer-key signature verifier (db-p2p binds it to the peer-sig primitive over
111
+ * {@link import("./wire/payloads.js").registerSigningPayload}). A `RegisterV1` whose signature does
112
+ * not verify against the claimed `participantCoord` is answered `no_state` (serve nothing, record
113
+ * nothing) — a cohort member cannot trust an unsigned/forged `participantCoord`. Verified first so a
114
+ * forged frame never reaches (or pollutes) the replay/rate guards. Absent → the gate is skipped.
115
+ */
116
+ readonly verifyRegisterSig?: (reg: RegisterV1) => boolean;
117
+ /**
118
+ * Sink for a freshly threshold-signed promotion/demotion notice produced on an arrival (the `accept`
119
+ * path's promotion trigger). db-p2p's host wires this to broadcast the notice over the `promote`
120
+ * protocol to the cohort (and, for a demotion, the parent coord). Absent → notices are produced and
121
+ * applied locally but not broadcast (unit/mock flows). Called fire-and-forget, off the reply path.
122
+ */
123
+ readonly onNotice?: (notice: PromotionNoticeV1 | DemotionNoticeV1) => void;
124
+ /**
125
+ * Optional logger. Used only to record a failed promotion sign/broadcast (the threshold-sign round
126
+ * can reject when the cohort quorum is unreachable) rather than swallow it. Absent → silent.
127
+ */
128
+ readonly log?: (formatter: string, ...args: unknown[]) => void;
129
+ /**
130
+ * Sink for a freshly-admitted registration record (the `accept` path), symmetric to the renewal
131
+ * `gossip.touch` hook. db-p2p wires this to the same per-coord gossip delta queue the renewal side
132
+ * appends to, so an admitted record is replicated to the cohort at the next gossip round — closing the
133
+ * durability window between admission and the participant's first renewal touch. A queue append, not a
134
+ * synchronous broadcast: admission must not block on a round, and last-writer-wins by `lastPing` dedupes
135
+ * an admit-then-touch landing in the same round. Absent → no admission-time replication (unit/mock flows
136
+ * that don't exercise gossip).
137
+ */
138
+ readonly onAdmit?: (rec: RegistrationRecord) => void;
139
+ }
140
+
141
+ /** Cohort-side register/renew handler — the body of the FRET activity callback. */
142
+ export interface CohortMemberEngine {
143
+ /** Classify and admit (or decline) an inbound `RegisterV1`, returning the reply to serialize. */
144
+ handleRegister(reg: RegisterV1, ctx: RegisterContext, now: number): Promise<RegisterReplyV1>;
145
+ /** Handle an inbound `RenewV1` (ping / re-attach), delegating to the renewal cohort side. */
146
+ handleRenew(msg: RenewV1, now: number): RenewReplyV1;
147
+ /** Evict stale records and gossip each eviction; returns the evicted set. */
148
+ sweepStale(now: number): readonly RegistrationRecord[];
149
+ }
150
+
151
+ class StoreCohortMemberEngine implements CohortMemberEngine {
152
+ constructor(private readonly deps: CohortMemberEngineDeps) {}
153
+
154
+ async handleRegister(reg: RegisterV1, ctx: RegisterContext, now: number): Promise<RegisterReplyV1> {
155
+ const topicId = b64urlToBytes(reg.topicId);
156
+ const participantId = b64urlToBytes(reg.participantCoord);
157
+ const tier = reg.tier as Tier;
158
+
159
+ // 0. Read-only lookup probe: classify + return the cohort snapshot without admitting anything.
160
+ // Branches before the admission pipeline (and the durable-state anti-DoS guards), so even a
161
+ // hand-crafted `probe: true, bootstrap: true` frame can never instantiate — defense in depth.
162
+ if (reg.probe === true) {
163
+ return this.handleProbe(reg, topicId, participantId, ctx, now);
164
+ }
165
+
166
+ // 1. Anti-DoS gates. A replay/stale frame is answered with NoState (serve nothing, record
167
+ // nothing); an over-rate source or missing bootstrap evidence backs off in time.
168
+ const guard = this.runGuards(reg, topicId, participantId, tier, now);
169
+ if (guard !== undefined) {
170
+ return guard;
171
+ }
172
+
173
+ // 2. Hot path: this cohort already serves the topic.
174
+ if (this.serves(topicId)) {
175
+ if (this.deps.promotion.isPromoted(topicId)) {
176
+ // Promoted: bounce same-tier registrations onward (the cheap single-RPC redirect).
177
+ return promotedRedirectReply(ctx.treeTier + 1, this.deps.traffic.snapshot(topicId));
178
+ }
179
+ return this.admitOrDecline(reg, topicId, participantId, tier, ctx, now);
180
+ }
181
+
182
+ // 3. Cold path: instantiate only at a legitimate growth point with a willing quorum. `ctx.followOn`
183
+ // mirrors the wire `reg.followOn` flag (the host derives it), and by the time we reach here the
184
+ // step-1 bootstrap-evidence gate has already verified a `followOn: true` carries the same proof a
185
+ // `bootstrap: true` root cold-start carries — so this branch never instantiates an unbacked follow-on.
186
+ const quorumWilling = this.deps.quorumWilling(tier);
187
+ if (!shouldInstantiate({ bootstrap: reg.bootstrap === true, followOn: ctx.followOn, quorumWilling })) {
188
+ return { v: 1, result: "no_state" };
189
+ }
190
+ if (this.deps.topicBudget?.admit(topicId) === false) {
191
+ // Forwarder-state budget is full of populated topics — decline in time.
192
+ return this.unwillingCohort(reg, now);
193
+ }
194
+ this.deps.coldStart.instantiate(topicId, ctx.treeTier, ctx.parentCoord, tier);
195
+ return this.admitOrDecline(reg, topicId, participantId, tier, ctx, now);
196
+ }
197
+
198
+ /**
199
+ * Read-only resolution of the cohort for a topic (the `lookup` probe). It walks the same path a
200
+ * register does (the walk loop is byte-for-byte identical) but the **terminal** member action is a
201
+ * classify, never an admission: it persists **no** record, counts **no** arrival, fires **no**
202
+ * promotion trigger, touches **no** topic budget, and **never** instantiates a cold-start forwarder.
203
+ *
204
+ * DoS posture (`docs/cohort-topic.md` §Anti-DoS): a probe still runs the stateless participant-sig gate
205
+ * (forged → `no_state`) and a dedicated probe rate limiter (over-rate → `unwilling_cohort`), but skips
206
+ * the replay guard, the bootstrap-evidence gate, and the topic budget — an idempotent read records
207
+ * nothing, so it is strictly cheaper than the register it replaces.
208
+ *
209
+ * Classification mirrors a register's terminal cases so the walk's existing branches drive a probe
210
+ * unchanged: a served-and-promoted topic answers `promoted(treeTier + 1)` (the walk follows it), a
211
+ * served topic answers `accepted` with the participant-specific slot assignment + read-only traffic
212
+ * snapshot, and a topic this cohort does not serve answers `no_state` (the walk steps inward).
213
+ */
214
+ private handleProbe(reg: RegisterV1, topicId: Uint8Array, participantId: Uint8Array, ctx: RegisterContext, now: number): RegisterReplyV1 {
215
+ if (this.deps.verifyRegisterSig?.(reg) === false) {
216
+ // Unsigned / forged participant signature: serve nothing, exactly like the register path — a
217
+ // cohort member never resolves a cohort snapshot for an untrusted `participantCoord`.
218
+ return { v: 1, result: "no_state" };
219
+ }
220
+ const rate = this.deps.probeRateLimiter?.check(participantId, topicId, now);
221
+ if (rate !== undefined && rate.ok === false) {
222
+ return { v: 1, result: "unwilling_cohort", retryAfterMs: rate.retryAfterMs };
223
+ }
224
+ if (this.serves(topicId)) {
225
+ if (this.deps.promotion.isPromoted(topicId)) {
226
+ return promotedRedirectReply(ctx.treeTier + 1, this.deps.traffic.snapshot(topicId));
227
+ }
228
+ const { members, cohortEpoch } = this.deps.cohort();
229
+ const { primary, backups } = this.deps.slots.assignSlots(participantId, cohortEpoch, members);
230
+ const reply: RegisterReplyV1 = {
231
+ v: 1,
232
+ result: "accepted",
233
+ primary: bytesKey(primary),
234
+ backups: backups.map(bytesKey),
235
+ cohortEpoch: bytesKey(cohortEpoch),
236
+ cohortMembers: members.map(bytesKey),
237
+ };
238
+ return attachTopicTraffic(reply, this.deps.traffic.snapshot(topicId));
239
+ }
240
+ return { v: 1, result: "no_state" };
241
+ }
242
+
243
+ handleRenew(msg: RenewV1, now: number): RenewReplyV1 {
244
+ const reply = this.deps.renewal.onRenew(msg, now);
245
+ if (reply.result === "ok") {
246
+ // A renewal is an arrival proxy for the traffic barometer (§Topic traffic signal).
247
+ this.deps.traffic.recordArrival(b64urlToBytes(msg.topicId), now);
248
+ } else if (reply.result === "withdrawn") {
249
+ // A withdraw evicted a direct participant: re-touch the topic budget DOWN (mirrors the
250
+ // sweepStale drain release below) so the freed slot does not leak. Never an arrival — it is a
251
+ // departure, so the traffic barometer is deliberately not touched.
252
+ const topicId = b64urlToBytes(msg.topicId);
253
+ this.deps.topicBudget?.touch(topicId, this.deps.store.directParticipants(topicId));
254
+ }
255
+ return reply;
256
+ }
257
+
258
+ sweepStale(now: number): readonly RegistrationRecord[] {
259
+ // Reclaim idle (peer, topic) rate-limiter keys on the existing gossip cadence. The limiters' own
260
+ // hard LRU cap already bounds worst-case footprint; this sweep adds proactive steady-state reclaim
261
+ // of long-quiet keys so a long-running coord does not hold register/probe state for departed peers.
262
+ this.deps.rateLimiter?.sweep(now);
263
+ this.deps.probeRateLimiter?.sweep(now);
264
+
265
+ const evicted = this.deps.renewal.sweepStale(now);
266
+ const budget = this.deps.topicBudget;
267
+ if (budget !== undefined && evicted.length > 0) {
268
+ // Mirror the `accept()` up-touch on the drain side: a TTL sweep that removes a topic's last
269
+ // direct participant must re-`touch` the budget down so the topic falls to participantCount 0
270
+ // and becomes the coldest-evictable resident again. Without this the budget slot leaks — a
271
+ // drained topic keeps its stale positive count forever and `coldestEvictable()` never picks it,
272
+ // so the cohort eventually refuses every new topic while serving nothing. Re-touch once per
273
+ // distinct affected topic from `store.directParticipants` (the source of truth, post-eviction).
274
+ const seen = new Set<string>();
275
+ for (const rec of evicted) {
276
+ const key = bytesKey(rec.topicId);
277
+ if (seen.has(key)) continue;
278
+ seen.add(key);
279
+ budget.touch(rec.topicId, this.deps.store.directParticipants(rec.topicId));
280
+ }
281
+ }
282
+ return evicted;
283
+ }
284
+
285
+ // --- admission ---
286
+
287
+ /** Run the willingness check; on `accepted` persist the record and attach the traffic signal. */
288
+ private async admitOrDecline(
289
+ reg: RegisterV1,
290
+ topicId: Uint8Array,
291
+ participantId: Uint8Array,
292
+ tier: Tier,
293
+ ctx: RegisterContext,
294
+ now: number,
295
+ ): Promise<RegisterReplyV1> {
296
+ const outcome = this.deps.willingness.evaluate(reg, this.deps.profile, now);
297
+ switch (outcome.kind) {
298
+ case "unwilling_member": {
299
+ return { v: 1, result: "unwilling_member", candidateMembers: outcome.candidateMembers.map(bytesKey) };
300
+ }
301
+ case "unwilling_cohort": {
302
+ return { v: 1, result: "unwilling_cohort", retryAfterMs: outcome.retryAfterMs };
303
+ }
304
+ case "accepted": {
305
+ return this.accept(reg, topicId, participantId, tier, ctx, now);
306
+ }
307
+ default: {
308
+ return this.unwillingCohort(reg, now);
309
+ }
310
+ }
311
+ }
312
+
313
+ private async accept(
314
+ reg: RegisterV1,
315
+ topicId: Uint8Array,
316
+ participantId: Uint8Array,
317
+ tier: Tier,
318
+ ctx: RegisterContext,
319
+ now: number,
320
+ ): Promise<RegisterReplyV1> {
321
+ const { members, cohortEpoch } = this.deps.cohort();
322
+ const { primary, backups } = this.deps.slots.assignSlots(participantId, cohortEpoch, members);
323
+ const ttl = clampTtl(reg.ttl);
324
+ const record: RegistrationRecord = {
325
+ topicId,
326
+ participantId,
327
+ tier,
328
+ primary,
329
+ backups,
330
+ attachedAt: now,
331
+ lastPing: now,
332
+ ttl,
333
+ appState: reg.appPayload === undefined ? undefined : b64urlToBytes(reg.appPayload),
334
+ };
335
+ this.deps.store.put(record);
336
+ this.deps.onAdmit?.(record);
337
+ this.deps.traffic.recordArrival(topicId, now);
338
+ this.deps.topicBudget?.touch(topicId, this.deps.store.directParticipants(topicId));
339
+ // Promotion may fire on this arrival; broadcast whatever notice comes back. Fire-and-forget so the
340
+ // register reply is not delayed by the threshold-sign collection round.
341
+ void this.firePromotion(topicId, now);
342
+
343
+ const reply: RegisterReplyV1 = {
344
+ v: 1,
345
+ result: "accepted",
346
+ primary: bytesKey(primary),
347
+ backups: backups.map(bytesKey),
348
+ cohortEpoch: bytesKey(cohortEpoch),
349
+ cohortMembers: members.map(bytesKey),
350
+ };
351
+ return attachTopicTraffic(reply, this.deps.traffic.snapshot(topicId));
352
+ }
353
+
354
+ /** Run the promotion trigger for an arrival and hand any signed notice to {@link CohortMemberEngineDeps.onNotice}. */
355
+ private async firePromotion(topicId: Uint8Array, now: number): Promise<void> {
356
+ try {
357
+ const notice = await this.deps.promotion.onParticipantCountChange(topicId, now);
358
+ if (notice !== undefined) {
359
+ this.deps.onNotice?.(notice);
360
+ }
361
+ } catch (err) {
362
+ // Threshold signing rejects when the cohort quorum is unreachable this round; the next arrival
363
+ // re-fires, so this is transient. Log rather than crash the register path — or leak the
364
+ // unhandled rejection the bare `void promotion...` did once signing became real (db-p2p ticket 1).
365
+ this.deps.log?.("cohort-topic: promotion sign/broadcast failed for an arrival: %o", err);
366
+ }
367
+ }
368
+
369
+ // --- guards ---
370
+
371
+ /** Returns a short-circuit reply if any anti-DoS gate rejects `reg`, else `undefined`. */
372
+ private runGuards(
373
+ reg: RegisterV1,
374
+ topicId: Uint8Array,
375
+ participantId: Uint8Array,
376
+ tier: Tier,
377
+ now: number,
378
+ ): RegisterReplyV1 | undefined {
379
+ if (this.deps.verifyRegisterSig?.(reg) === false) {
380
+ // Unsigned / forged participant signature: serve nothing and record nothing (the cohort
381
+ // cannot trust this participantCoord). Cheap + stateless, so checked first — a forged frame
382
+ // cannot consume any downstream guard state.
383
+ return { v: 1, result: "no_state" };
384
+ }
385
+ // Rate-check BEFORE the replay guard records anything: a fresh-correlationId spam frame that the
386
+ // rate limiter would reject must not first insert a `seen` entry (which would let an attacker grow
387
+ // the replay guard's memory at full attack speed regardless of the rate limit). Running it before
388
+ // the potentially-expensive bootstrap verify (which may do PoW) also short-circuits floods sooner.
389
+ // NOTE: a replayed frame now consumes a rate-limiter accept before the replay guard (below) drops it,
390
+ // whereas the old order dropped replays pre-rate. Fine for spam (inbound traffic should be counted);
391
+ // only matters if a legitimate client reuses one correlationId across retransmits — it would then
392
+ // burn rate budget per retransmit. If clients adopt idempotent same-cid retry, revisit this order.
393
+ const rate = this.deps.rateLimiter?.check(participantId, topicId, now);
394
+ if (rate !== undefined && rate.ok === false) {
395
+ return { v: 1, result: "unwilling_cohort", retryAfterMs: rate.retryAfterMs };
396
+ }
397
+ if (this.deps.bootstrapEvidence?.verify(reg, tier) === false) {
398
+ return this.unwillingCohort(reg, now);
399
+ }
400
+ // Replay guard records LAST, so only frames that passed signature + rate + bootstrap ever insert a
401
+ // `seen` entry. An *accepted* frame is still always recorded here, so a genuine replay of a served
402
+ // correlationId is still caught; only rate-rejected frames (never served, never recorded) skip it.
403
+ const correlationId = b64urlToBytes(reg.correlationId);
404
+ if (this.deps.replayGuard?.accept(correlationId, participantId, reg.timestamp, now) === false) {
405
+ // Stale / future-skewed / replayed: serve nothing and record nothing.
406
+ return { v: 1, result: "no_state" };
407
+ }
408
+ return undefined;
409
+ }
410
+
411
+ private serves(topicId: Uint8Array): boolean {
412
+ return this.deps.coldStart.get(topicId) !== undefined || this.deps.store.directParticipants(topicId) > 0;
413
+ }
414
+
415
+ private unwillingCohort(reg: RegisterV1, now: number): RegisterReplyV1 {
416
+ void reg;
417
+ void now;
418
+ return { v: 1, result: "unwilling_cohort", retryAfterMs: backoffRetryMs(0) };
419
+ }
420
+ }
421
+
422
+ /** Build a {@link CohortMemberEngine} over the injected cohort-side collaborators. */
423
+ export function createCohortMemberEngine(deps: CohortMemberEngineDeps): CohortMemberEngine {
424
+ return new StoreCohortMemberEngine(deps);
425
+ }
426
+
427
+ /** True iff `a` and `b` name the same peer (re-exported convenience for host equality checks). */
428
+ export function samePeer(a: Uint8Array, b: Uint8Array): boolean {
429
+ return bytesEqual(a, b);
430
+ }