@optimystic/db-core 0.21.0 → 0.24.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 (185) hide show
  1. package/README.md +336 -336
  2. package/dist/src/btree/btree.d.ts +2 -1
  3. package/dist/src/btree/btree.d.ts.map +1 -1
  4. package/dist/src/btree/btree.js +1 -1
  5. package/dist/src/btree/btree.js.map +1 -1
  6. package/dist/src/chain/chain.d.ts +1 -1
  7. package/dist/src/chain/chain.d.ts.map +1 -1
  8. package/dist/src/chain/chain.js +1 -1
  9. package/dist/src/chain/chain.js.map +1 -1
  10. package/dist/src/cluster/structs.d.ts +39 -1
  11. package/dist/src/cluster/structs.d.ts.map +1 -1
  12. package/dist/src/cluster/structs.js +24 -0
  13. package/dist/src/cluster/structs.js.map +1 -1
  14. package/dist/src/collection/collection.d.ts +20 -1
  15. package/dist/src/collection/collection.d.ts.map +1 -1
  16. package/dist/src/collection/collection.js +31 -4
  17. package/dist/src/collection/collection.js.map +1 -1
  18. package/dist/src/collections/diary/diary.d.ts.map +1 -1
  19. package/dist/src/collections/diary/diary.js +2 -1
  20. package/dist/src/collections/diary/diary.js.map +1 -1
  21. package/dist/src/collections/diary/struct.js +1 -1
  22. package/dist/src/collections/diary/struct.js.map +1 -1
  23. package/dist/src/collections/tree/collection-trunk.js +1 -1
  24. package/dist/src/collections/tree/collection-trunk.js.map +1 -1
  25. package/dist/src/collections/tree/struct.d.ts +1 -1
  26. package/dist/src/collections/tree/struct.d.ts.map +1 -1
  27. package/dist/src/collections/tree/struct.js +2 -1
  28. package/dist/src/collections/tree/struct.js.map +1 -1
  29. package/dist/src/collections/tree/tree.d.ts +5 -0
  30. package/dist/src/collections/tree/tree.d.ts.map +1 -1
  31. package/dist/src/collections/tree/tree.js +7 -0
  32. package/dist/src/collections/tree/tree.js.map +1 -1
  33. package/dist/src/log/log.d.ts +1 -1
  34. package/dist/src/log/log.d.ts.map +1 -1
  35. package/dist/src/log/log.js +2 -2
  36. package/dist/src/log/log.js.map +1 -1
  37. package/dist/src/network/i-peer-network.d.ts +16 -0
  38. package/dist/src/network/i-peer-network.d.ts.map +1 -1
  39. package/dist/src/network/struct.d.ts +39 -2
  40. package/dist/src/network/struct.d.ts.map +1 -1
  41. package/dist/src/network/struct.js +18 -0
  42. package/dist/src/network/struct.js.map +1 -1
  43. package/dist/src/testing/test-transactor.d.ts +95 -8
  44. package/dist/src/testing/test-transactor.d.ts.map +1 -1
  45. package/dist/src/testing/test-transactor.js +133 -12
  46. package/dist/src/testing/test-transactor.js.map +1 -1
  47. package/dist/src/transaction/coordinator.d.ts.map +1 -1
  48. package/dist/src/transaction/coordinator.js +2 -1
  49. package/dist/src/transaction/coordinator.js.map +1 -1
  50. package/dist/src/transaction/transaction.d.ts +1 -1
  51. package/dist/src/transaction/transaction.js +1 -1
  52. package/dist/src/transactor/network-transactor.d.ts.map +1 -1
  53. package/dist/src/transactor/network-transactor.js +57 -18
  54. package/dist/src/transactor/network-transactor.js.map +1 -1
  55. package/dist/src/transactor/transactor-source.d.ts.map +1 -1
  56. package/dist/src/transactor/transactor-source.js +25 -2
  57. package/dist/src/transactor/transactor-source.js.map +1 -1
  58. package/dist/src/transform/cache-source.js +1 -1
  59. package/dist/src/transform/cache-source.js.map +1 -1
  60. package/dist/src/transform/helpers.d.ts +6 -1
  61. package/dist/src/transform/helpers.d.ts.map +1 -1
  62. package/dist/src/transform/helpers.js +7 -6
  63. package/dist/src/transform/helpers.js.map +1 -1
  64. package/dist/src/transform/tracker.d.ts.map +1 -1
  65. package/dist/src/transform/tracker.js +2 -1
  66. package/dist/src/transform/tracker.js.map +1 -1
  67. package/package.json +1 -1
  68. package/src/btree/btree.ts +2 -1
  69. package/src/chain/chain.ts +2 -1
  70. package/src/cluster/membership.ts +85 -85
  71. package/src/cluster/structs.ts +43 -4
  72. package/src/cohort-topic/addressing.ts +120 -120
  73. package/src/cohort-topic/antidos/bootstrap-evidence-envelope.ts +253 -253
  74. package/src/cohort-topic/antidos/bootstrap-evidence.ts +106 -106
  75. package/src/cohort-topic/antidos/index.ts +5 -5
  76. package/src/cohort-topic/antidos/rate-limiter.ts +210 -210
  77. package/src/cohort-topic/antidos/replay-guard.ts +146 -146
  78. package/src/cohort-topic/antidos/topic-budget.ts +160 -160
  79. package/src/cohort-topic/antiflood/index.ts +2 -2
  80. package/src/cohort-topic/antiflood/invariants.ts +108 -108
  81. package/src/cohort-topic/antiflood/jitter.ts +117 -117
  82. package/src/cohort-topic/coldstart.ts +237 -237
  83. package/src/cohort-topic/dmax.ts +88 -88
  84. package/src/cohort-topic/gossip/bus.ts +254 -254
  85. package/src/cohort-topic/gossip/index.ts +3 -3
  86. package/src/cohort-topic/gossip/records.ts +45 -45
  87. package/src/cohort-topic/gossip/view.ts +91 -91
  88. package/src/cohort-topic/index.ts +20 -20
  89. package/src/cohort-topic/load/barometer.ts +134 -134
  90. package/src/cohort-topic/load/index.ts +1 -1
  91. package/src/cohort-topic/member-engine.ts +430 -430
  92. package/src/cohort-topic/membership/index.ts +3 -3
  93. package/src/cohort-topic/membership/publisher.ts +163 -163
  94. package/src/cohort-topic/membership/source.ts +41 -41
  95. package/src/cohort-topic/membership/verifier.ts +461 -461
  96. package/src/cohort-topic/ports.ts +157 -157
  97. package/src/cohort-topic/promotion.ts +405 -405
  98. package/src/cohort-topic/registration/bytes.ts +37 -37
  99. package/src/cohort-topic/registration/handoff.ts +154 -154
  100. package/src/cohort-topic/registration/index.ts +6 -6
  101. package/src/cohort-topic/registration/renewal.ts +495 -495
  102. package/src/cohort-topic/registration/sharding.ts +61 -61
  103. package/src/cohort-topic/registration/store.ts +81 -81
  104. package/src/cohort-topic/registration/types.ts +91 -91
  105. package/src/cohort-topic/ring-hash.ts +50 -50
  106. package/src/cohort-topic/service.ts +416 -416
  107. package/src/cohort-topic/sig/index.ts +2 -2
  108. package/src/cohort-topic/sig/payloads.ts +59 -59
  109. package/src/cohort-topic/sig/threshold.ts +64 -64
  110. package/src/cohort-topic/tiers.ts +74 -74
  111. package/src/cohort-topic/traffic.ts +233 -233
  112. package/src/cohort-topic/walk.ts +326 -326
  113. package/src/cohort-topic/willingness.ts +237 -237
  114. package/src/cohort-topic/wire/codec.ts +216 -216
  115. package/src/cohort-topic/wire/index.ts +18 -18
  116. package/src/cohort-topic/wire/payloads.ts +126 -126
  117. package/src/cohort-topic/wire/primitives.ts +188 -188
  118. package/src/cohort-topic/wire/types.ts +475 -475
  119. package/src/cohort-topic/wire/validate.ts +512 -512
  120. package/src/collection/collection-type-registry.ts +37 -37
  121. package/src/collection/collection.ts +32 -4
  122. package/src/collections/diary/diary.ts +68 -67
  123. package/src/collections/diary/struct.ts +1 -1
  124. package/src/collections/tree/collection-trunk.ts +1 -1
  125. package/src/collections/tree/readme.md +4 -0
  126. package/src/collections/tree/struct.ts +3 -1
  127. package/src/collections/tree/tree.ts +320 -312
  128. package/src/log/log.ts +2 -2
  129. package/src/matchmaking/capability-filter.ts +45 -45
  130. package/src/matchmaking/config.ts +98 -98
  131. package/src/matchmaking/index.ts +21 -21
  132. package/src/matchmaking/multi-cohort-seeker.ts +234 -234
  133. package/src/matchmaking/provider.ts +123 -123
  134. package/src/matchmaking/query-eval.ts +105 -105
  135. package/src/matchmaking/seeker-walk.ts +127 -127
  136. package/src/matchmaking/seeker.ts +86 -86
  137. package/src/matchmaking/topic-anchor.ts +90 -90
  138. package/src/matchmaking/voting-quorum.ts +394 -394
  139. package/src/matchmaking/wire.ts +603 -603
  140. package/src/network/i-peer-network.ts +17 -0
  141. package/src/network/stale-failure.ts +43 -43
  142. package/src/network/struct.ts +41 -2
  143. package/src/network/types.ts +37 -37
  144. package/src/reactivity/backfill.ts +220 -220
  145. package/src/reactivity/backpressure.ts +191 -191
  146. package/src/reactivity/checkpoint.ts +308 -308
  147. package/src/reactivity/config.ts +172 -172
  148. package/src/reactivity/dedupe.ts +132 -132
  149. package/src/reactivity/forwarder.ts +87 -87
  150. package/src/reactivity/index.ts +34 -34
  151. package/src/reactivity/notification.ts +123 -123
  152. package/src/reactivity/policy.ts +79 -79
  153. package/src/reactivity/push-state.ts +310 -310
  154. package/src/reactivity/recover.ts +153 -153
  155. package/src/reactivity/replay-buffer.ts +141 -141
  156. package/src/reactivity/resume.ts +549 -549
  157. package/src/reactivity/rotation.ts +415 -415
  158. package/src/reactivity/subscriber.ts +132 -132
  159. package/src/reactivity/subscription.ts +66 -66
  160. package/src/reactivity/topic-anchor.ts +71 -71
  161. package/src/reactivity/verify.ts +73 -73
  162. package/src/reactivity/wire-validate.ts +13 -13
  163. package/src/reactivity/wire.ts +224 -224
  164. package/src/testing/async-wait.ts +65 -65
  165. package/src/testing/index.ts +2 -2
  166. package/src/testing/test-transactor.ts +638 -489
  167. package/src/transaction/coordinator.ts +2 -1
  168. package/src/transaction/errors.ts +91 -91
  169. package/src/transaction/operations-hash.ts +196 -196
  170. package/src/transaction/read-dependency-collector.ts +78 -78
  171. package/src/transaction/transaction.ts +1 -1
  172. package/src/transactor/change-notifier.ts +80 -80
  173. package/src/transactor/index.ts +5 -5
  174. package/src/transactor/network-transactor.ts +58 -19
  175. package/src/transactor/transactor-source.ts +25 -2
  176. package/src/transform/atomic-proxy.ts +92 -92
  177. package/src/transform/cache-source.ts +1 -1
  178. package/src/transform/helpers.ts +159 -158
  179. package/src/transform/tracker.ts +2 -1
  180. package/src/utility/backoff.ts +95 -95
  181. package/src/utility/batch-coordinator.ts +191 -191
  182. package/dist/src/transaction/context.d.ts +0 -60
  183. package/dist/src/transaction/context.d.ts.map +0 -1
  184. package/dist/src/transaction/context.js +0 -91
  185. package/dist/src/transaction/context.js.map +0 -1
@@ -1,495 +1,495 @@
1
- /**
2
- * Cohort-topic substrate — TTL renewal and failover.
3
- *
4
- * Per `docs/cohort-topic.md` §TTL and renewal and §Failure modes:
5
- *
6
- * - Participant pings `primary` every `ttl/3`. Success → primary touches `lastPing` and gossips it.
7
- * - Three consecutive ping failures → participant promotes `backups[0]` via a re-attach RPC: a renew
8
- * carrying a **signed `reattach` flag** (no full re-registration). The backup accepts when it holds
9
- * the record locally *and* is a computed backup under the current epoch — re-stamping `primary` to
10
- * itself and serving subsequent plain pings via an epoch-scoped failover override (the unchanged
11
- * `cohortEpoch` still names the dead node as the computed primary). A re-attach answered
12
- * `primary_moved` means a real rotation moved primary to a live member: the participant adopts that
13
- * payload instead of promoting the contacted backup (ignoring a reply that points back at the dead
14
- * primary). **Resolved (GROUNDING):** the participant's `cohortEpoch` hint refreshes lazily — on the
15
- * *next* ping/renewal after failover, not eagerly at failover time.
16
- * - All of primary + backups fail → participant re-runs lookup from `d_max` (walk ticket, injected).
17
- * - Cohort-side: evict where `now − lastPing > ttl`; eviction is gossiped so members converge.
18
- *
19
- * Both sides take their transport/gossip by injection so storage + sharding + TTL stay unit-testable
20
- * in isolation; db-core never imports FRET or libp2p here.
21
- */
22
-
23
- import { DEFAULT_REPLAY_MAX_AGE_MS, DEFAULT_REPLAY_MAX_FUTURE_SKEW_MS } from "../antidos/replay-guard.js";
24
- import { b64urlToBytes } from "../wire/codec.js";
25
- import type { RenewReplyV1, RenewV1 } from "../wire/types.js";
26
- import { bytesEqual, bytesKey, recordKey } from "./bytes.js";
27
- import type { SlotAssigner } from "./sharding.js";
28
- import { MAX_PING_FAILURES, pingIntervalMs } from "./types.js";
29
- import type { RegistrationRecord, RegistrationStore } from "./types.js";
30
-
31
- // --- participant side ---
32
-
33
- /** Participant-side TTL renewal: periodic ping with 3-fail backup promotion. */
34
- export interface RenewalParticipant {
35
- /** Run one ping cycle: ping the current primary, count failures, promote/relookup as needed. */
36
- pingLoop(): Promise<void>;
37
- /** Re-attach to `target` (a backup), carrying the existing record; resolves with the reply. */
38
- reattach(target: Uint8Array): Promise<RenewReplyV1>;
39
- /**
40
- * Best-effort remote tombstone: send a signed withdraw renew to the current primary so the cohort
41
- * frees the registration immediately rather than holding it for a full TTL. Swallows any transport
42
- * failure (TTL expiry remains the fallback) and does NOT touch the failure counters or trigger
43
- * failover — withdraw is one-shot and fire-and-forget.
44
- */
45
- withdraw(): Promise<void>;
46
- /** `ttl/3` cadence the scheduler should call {@link pingLoop} at. */
47
- readonly pingIntervalMs: number;
48
- /** The participant's current view of its registration (primary/backups). */
49
- readonly record: RegistrationRecord;
50
- /**
51
- * Cached cohort-epoch hint (subscriber-side `cohortHint`). Deliberately *not* refreshed at
52
- * failover; a `primary_moved` reply on the next ping refreshes it lazily.
53
- */
54
- readonly cohortEpochHint: Uint8Array | undefined;
55
- }
56
-
57
- /** The unsigned body of a renew/re-attach RPC; the injected signer turns it into a {@link RenewV1}. */
58
- export type UnsignedRenew = Omit<RenewV1, "signature">;
59
-
60
- /** Transport the participant drives; supplied by db-p2p (FRET dial / RouteAndMaybeAct underneath). */
61
- export interface RenewalParticipantTransport {
62
- /** Send a renew/re-attach to `target`; resolves with the reply, rejects on RPC failure. */
63
- send(target: Uint8Array, msg: RenewV1): Promise<RenewReplyV1>;
64
- /** Primary + all backups unreachable: re-run lookup from `d_max` (walk ticket owns the body). */
65
- relookup(): Promise<void>;
66
- }
67
-
68
- export interface RenewalParticipantDeps {
69
- transport: RenewalParticipantTransport;
70
- /** Monotonic-ish wall clock in unix ms (injected for deterministic tests). */
71
- clock: () => number;
72
- /** Signs the renew body; db-p2p supplies the (async) peer-key signature. */
73
- sign: (body: UnsignedRenew) => Promise<string>;
74
- /** Correlation id matching the original RegisterV1, base64url. */
75
- correlationId: string;
76
- /** Initial cohort-epoch hint from the registration reply (refreshed lazily thereafter). */
77
- initialCohortEpoch?: Uint8Array;
78
- }
79
-
80
- class TtlRenewalParticipant implements RenewalParticipant {
81
- private consecutiveFailures = 0;
82
- private current: RegistrationRecord;
83
- private epochHint: Uint8Array | undefined;
84
-
85
- constructor(initial: RegistrationRecord, private readonly deps: RenewalParticipantDeps) {
86
- this.current = initial;
87
- this.epochHint = deps.initialCohortEpoch;
88
- }
89
-
90
- get record(): RegistrationRecord {
91
- return this.current;
92
- }
93
-
94
- get cohortEpochHint(): Uint8Array | undefined {
95
- return this.epochHint;
96
- }
97
-
98
- get pingIntervalMs(): number {
99
- return pingIntervalMs(this.current.ttl);
100
- }
101
-
102
- async pingLoop(): Promise<void> {
103
- const reply = await this.trySend(this.current.primary);
104
- if (reply !== undefined) {
105
- this.onPingSuccess(reply);
106
- return;
107
- }
108
- this.consecutiveFailures++;
109
- if (this.consecutiveFailures < MAX_PING_FAILURES) {
110
- return;
111
- }
112
- await this.failover();
113
- }
114
-
115
- async reattach(target: Uint8Array): Promise<RenewReplyV1> {
116
- return this.deps.transport.send(target, await this.buildRenew({ reattach: true }));
117
- }
118
-
119
- async withdraw(): Promise<void> {
120
- // Fire-and-forget signed tombstone to the current primary. A send failure is swallowed — the
121
- // record TTL-expires as it does today (the local-half fallback) — and crucially we never bump
122
- // `consecutiveFailures` or fail over: the participant is leaving, not recovering.
123
- try {
124
- await this.deps.transport.send(this.current.primary, await this.buildRenew({ withdraw: true }));
125
- } catch {
126
- /* best-effort; the cohort TTL bounds the leak */
127
- }
128
- }
129
-
130
- /** Send a renew, mapping an RPC rejection to `undefined` (a counted failure). */
131
- private async trySend(target: Uint8Array): Promise<RenewReplyV1 | undefined> {
132
- try {
133
- return await this.deps.transport.send(target, await this.buildRenew({}));
134
- } catch {
135
- return undefined;
136
- }
137
- }
138
-
139
- private onPingSuccess(reply: RenewReplyV1): void {
140
- this.consecutiveFailures = 0;
141
- if (reply.result === "primary_moved") {
142
- // Lazy cohortEpoch refresh lands here: a move discovered on a normal ping updates the hint.
143
- this.applyPrimaryMoved(reply);
144
- }
145
- }
146
-
147
- /**
148
- * 3-fail path: re-attach to each backup in turn (a signed `reattach=true` renew). An `ok` means the
149
- * backup accepted the crash-failover promotion → promote it locally. A `primary_moved` means a real
150
- * rotation moved primary to a different live member → adopt that payload (not the contacted backup),
151
- * **unless** it points back at the just-failed primary (the bounce guard) — then keep trying. All
152
- * backups exhausted → re-run lookup from `d_max`.
153
- */
154
- private async failover(): Promise<void> {
155
- const failedPrimary = this.current.primary;
156
- for (const backup of this.current.backups) {
157
- const reply = await this.tryReattach(backup);
158
- if (reply === undefined) {
159
- continue;
160
- }
161
- if (reply.result === "ok") {
162
- this.promote(backup);
163
- return;
164
- }
165
- if (reply.result === "primary_moved") {
166
- // A genuine rotation: adopt the rotated assignment — but never re-adopt the dead primary
167
- // (the defensive guard against the exact bounce crash-failover is meant to fix).
168
- if (reply.newPrimary !== undefined && bytesEqual(b64(reply.newPrimary), failedPrimary)) {
169
- continue;
170
- }
171
- this.applyPrimaryMoved(reply);
172
- // We now have a fresh, live primary to ping — clear the strike count so the next single
173
- // transient failure doesn't immediately re-failover (matching `promote` and the
174
- // normal-ping `primary_moved` path, which both reset here).
175
- this.consecutiveFailures = 0;
176
- return;
177
- }
178
- // unknown_registration (replication lag) or anything else: try the next backup.
179
- }
180
- await this.deps.transport.relookup();
181
- // relookup is a terminal recovery action (it owns re-establishing the registration out of
182
- // band); reset the counter so a still-dead primary backs off to one relookup per
183
- // MAX_PING_FAILURES cycles rather than re-running the d_max walk on every subsequent ping.
184
- this.consecutiveFailures = 0;
185
- }
186
-
187
- private async tryReattach(target: Uint8Array): Promise<RenewReplyV1 | undefined> {
188
- try {
189
- return await this.reattach(target);
190
- } catch {
191
- return undefined;
192
- }
193
- }
194
-
195
- /**
196
- * Promote `target` (a backup) to primary. The `cohortEpoch` hint is deliberately *not* refreshed
197
- * here — per the resolved open question it refreshes lazily on the next ping/renewal.
198
- */
199
- private promote(target: Uint8Array): void {
200
- const backups = this.current.backups.filter((b) => !bytesEqual(b, target));
201
- this.current = { ...this.current, primary: target, backups, lastPing: this.deps.clock() };
202
- this.consecutiveFailures = 0;
203
- }
204
-
205
- private applyPrimaryMoved(reply: RenewReplyV1): void {
206
- const patch: Partial<RegistrationRecord> = { lastPing: this.deps.clock() };
207
- if (reply.newPrimary !== undefined) {
208
- patch.primary = b64(reply.newPrimary);
209
- }
210
- if (reply.newBackups !== undefined) {
211
- patch.backups = reply.newBackups.map(b64);
212
- }
213
- if (reply.cohortEpoch !== undefined) {
214
- // Lazy refresh: the epoch hint catches up here, on the ping that discovers the move.
215
- this.epochHint = b64(reply.cohortEpoch);
216
- }
217
- this.current = { ...this.current, ...patch };
218
- }
219
-
220
- /**
221
- * Build a signed renew. The `reattach=true` (crash-failover) / `withdraw=true` (leave tombstone)
222
- * flags are carried inside the signed body so the accepting member can trust the attestation; a plain
223
- * ping omits both fields entirely (a stray renew can never silently usurp a live primary or evict a
224
- * registration). Each flag is set only when true, matching the current plain-ping wire shape.
225
- */
226
- private async buildRenew(opts: { reattach?: boolean; withdraw?: boolean }): Promise<RenewV1> {
227
- const body: UnsignedRenew = {
228
- v: 1,
229
- topicId: bytesKey(this.current.topicId),
230
- participantId: bytesKey(this.current.participantId),
231
- correlationId: this.deps.correlationId,
232
- timestamp: this.deps.clock(),
233
- };
234
- if (opts.reattach === true) {
235
- body.reattach = true;
236
- }
237
- if (opts.withdraw === true) {
238
- body.withdraw = true;
239
- }
240
- return { ...body, signature: await this.deps.sign(body) };
241
- }
242
- }
243
-
244
- /** Build a {@link RenewalParticipant} around an initial record. */
245
- export function createRenewalParticipant(initial: RegistrationRecord, deps: RenewalParticipantDeps): RenewalParticipant {
246
- return new TtlRenewalParticipant(initial, deps);
247
- }
248
-
249
- // --- cohort side ---
250
-
251
- /** Emits cohort gossip so members converge on the active set after a touch or eviction. */
252
- export interface RenewalGossip {
253
- /** Gossip a `lastPing` touch for `rec`. */
254
- touch(rec: RegistrationRecord): void;
255
- /** Gossip an eviction for `rec`. */
256
- evicted(rec: RegistrationRecord): void;
257
- }
258
-
259
- export interface RenewalCohortSideDeps {
260
- store: RegistrationStore;
261
- /** This member's own peer id. */
262
- self: Uint8Array;
263
- /** Deterministic slot assignment, shared with the handoff. */
264
- slots: SlotAssigner;
265
- /** Current cohort snapshot — db-p2p supplies it from the membership source. */
266
- cohort: () => { members: readonly Uint8Array[]; cohortEpoch: Uint8Array };
267
- gossip: RenewalGossip;
268
- /**
269
- * Optional dual-serve predicate, wired to the rotation handoff's `isServing`. During a handoff
270
- * the previous primary is no longer the computed primary but must keep serving until it is acked;
271
- * when this returns `true` `onRenew` touches and serves instead of replying `primary_moved`.
272
- */
273
- isServing?: (topicId: Uint8Array, participantId: Uint8Array) => boolean;
274
- /**
275
- * Optional participant peer-key signature verifier (db-p2p binds it to the peer-sig primitive over
276
- * {@link import("../wire/payloads.js").renewSigningPayload}). It gates the two privileged
277
- * participant-attested paths: a `reattach` promotion and a `withdraw` eviction. A renew on either path
278
- * whose signature does not verify against the claimed `participantId` must never act — a stray/MITM'd
279
- * ping cannot usurp a live primary or evict someone else's registration (§TTL and renewal). Absent →
280
- * the gate is skipped (unit tests run without peer crypto); plain pings are never verified here (they
281
- * only touch `lastPing`).
282
- */
283
- verifyParticipantSig?: (renew: RenewV1) => boolean;
284
- /**
285
- * Optional freshness window for the two privileged, participant-attested paths (`withdraw` eviction and
286
- * `reattach` promotion). The renew signature already binds `timestamp` (see
287
- * {@link import("../wire/payloads.js").renewSigningPayload}), so an attacker cannot forge a fresher
288
- * timestamp onto a captured frame — replay is exact-frame only. A timestamp gate is therefore a
289
- * complete freshness regime for these frames: it rejects a stale/implausibly-future `timestamp`, and a
290
- * per-record monotonic check (`timestamp <= rec.lastPing`) closes the sub-`maxAge` fast-replay window
291
- * using state already on the record. Defaults to the register-path skew constants
292
- * ({@link DEFAULT_REPLAY_MAX_AGE_MS} / {@link DEFAULT_REPLAY_MAX_FUTURE_SKEW_MS}); db-p2p wires the same
293
- * `{ maxAgeMs, maxFutureSkewMs }` config the register path's replay guard consumes so an operator tuning
294
- * the skew window moves both paths together. Absent → defaults (key-less unit tests and callers work
295
- * unchanged, matching how {@link verifyParticipantSig} is optional). Plain pings are NOT gated (a
296
- * replayed ping is low-harm — it only re-touches `lastPing`).
297
- */
298
- freshness?: { maxAgeMs?: number; maxFutureSkewMs?: number };
299
- }
300
-
301
- /** Cohort-side TTL handling: touch on renew, redirect on rotation, sweep stale records. */
302
- export interface RenewalCohortSide {
303
- /** Handle an inbound renew. Touches the record, or replies `primary_moved` if this member is no longer primary. */
304
- onRenew(msg: RenewV1, now: number): RenewReplyV1;
305
- /** Evict every stale record and gossip each eviction; returns the evicted set. */
306
- sweepStale(now: number): readonly RegistrationRecord[];
307
- }
308
-
309
- class StoreRenewalCohortSide implements RenewalCohortSide {
310
- /**
311
- * Epoch-scoped crash-failover overrides: `recordKey → cohortEpoch under which this member accepted a
312
- * promotion`. A matching entry makes a *subsequent plain ping* serve here even though the computed
313
- * primary is still the dead node, so the migrated participant stops bouncing. Cleared on epoch change
314
- * (the next rotation handoff reasserts the deterministic assignment). Sibling to the rotation
315
- * `isServing` dual-serve exception — distinct state, OR-ed into the serve decision.
316
- */
317
- private readonly failoverServing = new Map<string, Uint8Array>();
318
-
319
- /** Resolved staleness window / forward-skew tolerance for the privileged freshness gate. */
320
- private readonly maxAgeMs: number;
321
- private readonly maxFutureSkewMs: number;
322
-
323
- constructor(private readonly deps: RenewalCohortSideDeps) {
324
- this.maxAgeMs = deps.freshness?.maxAgeMs ?? DEFAULT_REPLAY_MAX_AGE_MS;
325
- this.maxFutureSkewMs = deps.freshness?.maxFutureSkewMs ?? DEFAULT_REPLAY_MAX_FUTURE_SKEW_MS;
326
- }
327
-
328
- /**
329
- * Freshness gate for the two privileged, participant-attested branches (`withdraw`, `reattach`). Returns
330
- * `false` — reject — when `msg.timestamp` is stale (older than `now − maxAgeMs`), implausibly future
331
- * (newer than `now + maxFutureSkewMs`), or not strictly newer than the record's `lastPing`. The signed
332
- * timestamp is immutable to a replayer, so this fully bounds a captured privileged frame: the skew
333
- * window catches an old capture, and the `<= rec.lastPing` monotonic check catches a fast replay that
334
- * still fits inside the window (e.g. a `withdraw` captured at `t0` replayed after the record was
335
- * re-registered at `t_reregister > t0`, or a `reattach` replayed after its own accepted re-stamp
336
- * already advanced `lastPing`).
337
- */
338
- private isFreshPrivileged(msg: RenewV1, rec: RegistrationRecord, now: number): boolean {
339
- if (msg.timestamp < now - this.maxAgeMs) {
340
- return false; // stale
341
- }
342
- if (msg.timestamp > now + this.maxFutureSkewMs) {
343
- return false; // implausibly future
344
- }
345
- // NOTE: condition (3) compares a participant-supplied `msg.timestamp` against a server-maintained
346
- // `rec.lastPing` (set from whichever cohort member last touched the record, using its own clock) — two
347
- // different machines' clocks. In normal operation a genuine leave/failover post-dates the last ping, so
348
- // `timestamp > lastPing` holds. If a participant's clock lags the server that set `lastPing`, a
349
- // legitimate reattach can be rejected; the failure is soft (returns `primary_moved`, so the failover
350
- // loop tries the next backup / re-runs the d_max lookup — a delayed failover, not data loss). If
351
- // cross-node skew is ever observed to stall failovers, relax (3) to strict `<` (accept `timestamp ==
352
- // lastPing`); the `maxAge` window still backstops the replay-after-re-registration attack.
353
- if (msg.timestamp <= rec.lastPing) {
354
- return false; // replay / non-monotonic against the live record
355
- }
356
- return true;
357
- }
358
-
359
- onRenew(msg: RenewV1, now: number): RenewReplyV1 {
360
- const topicId = b64(msg.topicId);
361
- const participantId = b64(msg.participantId);
362
- const rec = this.deps.store.getByParticipant(topicId, participantId);
363
- if (rec === undefined) {
364
- return { v: 1, result: "unknown_registration" };
365
- }
366
- const key = recordKey(topicId, participantId);
367
-
368
- if (msg.withdraw === true) {
369
- // Signed leave attestation. A forged/missing signature must never evict someone else's
370
- // registration → ignore, revealing nothing (`unknown_registration`, the same opaque answer a
371
- // non-existent record gets). The gate is absent in key-less unit mode, matching reattach.
372
- // `withdraw` is checked before the slot/primary computation: a withdraw needs no slot or
373
- // primary check (any holder evicts its replica), and it takes precedence over a (malformed)
374
- // co-set `reattach` — a record being withdrawn is gone regardless of a promotion request.
375
- if (this.deps.verifyParticipantSig?.(msg) === false) {
376
- return { v: 1, result: "unknown_registration" };
377
- }
378
- // Freshness gate: a signed withdraw is valid forever without this, so a captured one could be
379
- // replayed after the victim's record TTL-expires and re-registers, evicting the *fresh* record.
380
- // Reject a stale/replayed frame with the same opaque `unknown_registration` the forged-sig branch
381
- // returns above — indistinguishable from an untrusted frame — and never delete.
382
- if (!this.isFreshPrivileged(msg, rec, now)) {
383
- return { v: 1, result: "unknown_registration" };
384
- }
385
- this.deps.store.delete(topicId, participantId);
386
- this.failoverServing.delete(key); // mirror sweepStale: drop any crash-failover override
387
- this.deps.gossip.evicted(rec);
388
- return { v: 1, result: "withdrawn" };
389
- }
390
-
391
- const { members, cohortEpoch } = this.deps.cohort();
392
- const { primary, backups } = this.deps.slots.assignSlots(participantId, cohortEpoch, members);
393
- const self = this.deps.self;
394
-
395
- if (msg.reattach === true) {
396
- // Crash-failover promotion request (participant attests primary unreachable). The signed
397
- // `reattach` flag is what a backup trusts to promote itself, so a missing/forged signature
398
- // must never escalate: fall through to a plain redirect (never promote).
399
- if (this.deps.verifyParticipantSig?.(msg) === false) {
400
- return this.primaryMoved(primary, backups, cohortEpoch);
401
- }
402
- // Freshness gate: a signed reattach is valid forever without this, so a captured one could be
403
- // replayed to force bogus primary re-stamps. Reject a stale/replayed frame with the same redirect
404
- // the forged-sig branch returns above — revealing nothing — and never promote.
405
- if (!this.isFreshPrivileged(msg, rec, now)) {
406
- return this.primaryMoved(primary, backups, cohortEpoch);
407
- }
408
- if (bytesEqual(primary, self)) {
409
- // A rotation already made this member the computed primary; serve, no override needed.
410
- return this.touchAndServe(rec, now);
411
- }
412
- if (backups.some((b) => bytesEqual(b, self))) {
413
- // Legitimate backup takeover: re-stamp primary, gossip the new assignment, and record an
414
- // epoch-scoped override so subsequent plain pings keep being served here.
415
- const restamped: RegistrationRecord = {
416
- ...rec,
417
- primary: self,
418
- backups: rec.backups.filter((b) => !bytesEqual(b, self)),
419
- lastPing: now,
420
- };
421
- this.deps.store.put(restamped);
422
- this.failoverServing.set(key, cohortEpoch);
423
- this.deps.gossip.touch(restamped);
424
- return { v: 1, result: "ok" };
425
- }
426
- if (this.deps.isServing?.(topicId, participantId) === true) {
427
- // Rotation dual-serve already covers this record here.
428
- return this.touchAndServe(rec, now);
429
- }
430
- // Not a valid takeover target (stale participant view): redirect, do not promote.
431
- return this.primaryMoved(primary, backups, cohortEpoch);
432
- }
433
-
434
- // Plain ping.
435
- // NOTE: plain pings are deliberately NOT run through the privileged freshness gate (isFreshPrivileged).
436
- // A replayed ping is low-harm — it can only re-touch a record's `lastPing`, never delete or usurp — and
437
- // the strict `timestamp <= lastPing` monotonic check would risk rejecting a legitimate ping that arrives
438
- // slightly out of order or under minor participant-clock non-monotonicity. If plain-ping replay ever
439
- // becomes a concern (e.g. touch-driven traffic accounting is abused), gate it here too.
440
- const isComputedPrimary = bytesEqual(primary, self);
441
- const override = this.failoverServing.get(key);
442
- const overrideMatches = override !== undefined && bytesEqual(override, cohortEpoch);
443
- // Housekeeping: drop a now-redundant override (this member is the computed primary again) or a
444
- // stale one tagged under a prior epoch (the rotation handoff governs across the epoch change).
445
- if (override !== undefined && (isComputedPrimary || !overrideMatches)) {
446
- this.failoverServing.delete(key);
447
- }
448
- const serving = isComputedPrimary || this.deps.isServing?.(topicId, participantId) === true || overrideMatches;
449
- if (serving) {
450
- return this.touchAndServe(rec, now);
451
- }
452
- return this.primaryMoved(primary, backups, cohortEpoch);
453
- }
454
-
455
- /** Existing touch path: stamp `lastPing`, gossip the touch, reply `ok`. */
456
- private touchAndServe(rec: RegistrationRecord, now: number): RenewReplyV1 {
457
- const touched: RegistrationRecord = { ...rec, lastPing: now };
458
- this.deps.store.put(touched);
459
- this.deps.gossip.touch(touched);
460
- return { v: 1, result: "ok" };
461
- }
462
-
463
- private primaryMoved(primary: Uint8Array, backups: readonly Uint8Array[], cohortEpoch: Uint8Array): RenewReplyV1 {
464
- return {
465
- v: 1,
466
- result: "primary_moved",
467
- newPrimary: bytesKey(primary),
468
- newBackups: backups.map(bytesKey),
469
- cohortEpoch: bytesKey(cohortEpoch),
470
- };
471
- }
472
-
473
- sweepStale(now: number): readonly RegistrationRecord[] {
474
- const evicted = this.deps.store.evictStale(now);
475
- for (const rec of evicted) {
476
- // Drop any crash-failover override for an evicted record. Otherwise it leaks (the record gets
477
- // no more pings, so the plain-ping housekeeping that would clear it never runs) and — if the
478
- // same `(topic, participant)` re-registers under the unchanged epoch — could wrongly make a
479
- // non-primary member keep serving via the stale override.
480
- this.failoverServing.delete(recordKey(rec.topicId, rec.participantId));
481
- this.deps.gossip.evicted(rec);
482
- }
483
- return evicted;
484
- }
485
- }
486
-
487
- /** Build a {@link RenewalCohortSide}. */
488
- export function createRenewalCohortSide(deps: RenewalCohortSideDeps): RenewalCohortSide {
489
- return new StoreRenewalCohortSide(deps);
490
- }
491
-
492
- /** Decode a base64url wire field to bytes (the canonical byte form on the wire). */
493
- function b64(s: string): Uint8Array {
494
- return b64urlToBytes(s);
495
- }
1
+ /**
2
+ * Cohort-topic substrate — TTL renewal and failover.
3
+ *
4
+ * Per `docs/cohort-topic.md` §TTL and renewal and §Failure modes:
5
+ *
6
+ * - Participant pings `primary` every `ttl/3`. Success → primary touches `lastPing` and gossips it.
7
+ * - Three consecutive ping failures → participant promotes `backups[0]` via a re-attach RPC: a renew
8
+ * carrying a **signed `reattach` flag** (no full re-registration). The backup accepts when it holds
9
+ * the record locally *and* is a computed backup under the current epoch — re-stamping `primary` to
10
+ * itself and serving subsequent plain pings via an epoch-scoped failover override (the unchanged
11
+ * `cohortEpoch` still names the dead node as the computed primary). A re-attach answered
12
+ * `primary_moved` means a real rotation moved primary to a live member: the participant adopts that
13
+ * payload instead of promoting the contacted backup (ignoring a reply that points back at the dead
14
+ * primary). **Resolved (GROUNDING):** the participant's `cohortEpoch` hint refreshes lazily — on the
15
+ * *next* ping/renewal after failover, not eagerly at failover time.
16
+ * - All of primary + backups fail → participant re-runs lookup from `d_max` (walk ticket, injected).
17
+ * - Cohort-side: evict where `now − lastPing > ttl`; eviction is gossiped so members converge.
18
+ *
19
+ * Both sides take their transport/gossip by injection so storage + sharding + TTL stay unit-testable
20
+ * in isolation; db-core never imports FRET or libp2p here.
21
+ */
22
+
23
+ import { DEFAULT_REPLAY_MAX_AGE_MS, DEFAULT_REPLAY_MAX_FUTURE_SKEW_MS } from "../antidos/replay-guard.js";
24
+ import { b64urlToBytes } from "../wire/codec.js";
25
+ import type { RenewReplyV1, RenewV1 } from "../wire/types.js";
26
+ import { bytesEqual, bytesKey, recordKey } from "./bytes.js";
27
+ import type { SlotAssigner } from "./sharding.js";
28
+ import { MAX_PING_FAILURES, pingIntervalMs } from "./types.js";
29
+ import type { RegistrationRecord, RegistrationStore } from "./types.js";
30
+
31
+ // --- participant side ---
32
+
33
+ /** Participant-side TTL renewal: periodic ping with 3-fail backup promotion. */
34
+ export interface RenewalParticipant {
35
+ /** Run one ping cycle: ping the current primary, count failures, promote/relookup as needed. */
36
+ pingLoop(): Promise<void>;
37
+ /** Re-attach to `target` (a backup), carrying the existing record; resolves with the reply. */
38
+ reattach(target: Uint8Array): Promise<RenewReplyV1>;
39
+ /**
40
+ * Best-effort remote tombstone: send a signed withdraw renew to the current primary so the cohort
41
+ * frees the registration immediately rather than holding it for a full TTL. Swallows any transport
42
+ * failure (TTL expiry remains the fallback) and does NOT touch the failure counters or trigger
43
+ * failover — withdraw is one-shot and fire-and-forget.
44
+ */
45
+ withdraw(): Promise<void>;
46
+ /** `ttl/3` cadence the scheduler should call {@link pingLoop} at. */
47
+ readonly pingIntervalMs: number;
48
+ /** The participant's current view of its registration (primary/backups). */
49
+ readonly record: RegistrationRecord;
50
+ /**
51
+ * Cached cohort-epoch hint (subscriber-side `cohortHint`). Deliberately *not* refreshed at
52
+ * failover; a `primary_moved` reply on the next ping refreshes it lazily.
53
+ */
54
+ readonly cohortEpochHint: Uint8Array | undefined;
55
+ }
56
+
57
+ /** The unsigned body of a renew/re-attach RPC; the injected signer turns it into a {@link RenewV1}. */
58
+ export type UnsignedRenew = Omit<RenewV1, "signature">;
59
+
60
+ /** Transport the participant drives; supplied by db-p2p (FRET dial / RouteAndMaybeAct underneath). */
61
+ export interface RenewalParticipantTransport {
62
+ /** Send a renew/re-attach to `target`; resolves with the reply, rejects on RPC failure. */
63
+ send(target: Uint8Array, msg: RenewV1): Promise<RenewReplyV1>;
64
+ /** Primary + all backups unreachable: re-run lookup from `d_max` (walk ticket owns the body). */
65
+ relookup(): Promise<void>;
66
+ }
67
+
68
+ export interface RenewalParticipantDeps {
69
+ transport: RenewalParticipantTransport;
70
+ /** Monotonic-ish wall clock in unix ms (injected for deterministic tests). */
71
+ clock: () => number;
72
+ /** Signs the renew body; db-p2p supplies the (async) peer-key signature. */
73
+ sign: (body: UnsignedRenew) => Promise<string>;
74
+ /** Correlation id matching the original RegisterV1, base64url. */
75
+ correlationId: string;
76
+ /** Initial cohort-epoch hint from the registration reply (refreshed lazily thereafter). */
77
+ initialCohortEpoch?: Uint8Array;
78
+ }
79
+
80
+ class TtlRenewalParticipant implements RenewalParticipant {
81
+ private consecutiveFailures = 0;
82
+ private current: RegistrationRecord;
83
+ private epochHint: Uint8Array | undefined;
84
+
85
+ constructor(initial: RegistrationRecord, private readonly deps: RenewalParticipantDeps) {
86
+ this.current = initial;
87
+ this.epochHint = deps.initialCohortEpoch;
88
+ }
89
+
90
+ get record(): RegistrationRecord {
91
+ return this.current;
92
+ }
93
+
94
+ get cohortEpochHint(): Uint8Array | undefined {
95
+ return this.epochHint;
96
+ }
97
+
98
+ get pingIntervalMs(): number {
99
+ return pingIntervalMs(this.current.ttl);
100
+ }
101
+
102
+ async pingLoop(): Promise<void> {
103
+ const reply = await this.trySend(this.current.primary);
104
+ if (reply !== undefined) {
105
+ this.onPingSuccess(reply);
106
+ return;
107
+ }
108
+ this.consecutiveFailures++;
109
+ if (this.consecutiveFailures < MAX_PING_FAILURES) {
110
+ return;
111
+ }
112
+ await this.failover();
113
+ }
114
+
115
+ async reattach(target: Uint8Array): Promise<RenewReplyV1> {
116
+ return this.deps.transport.send(target, await this.buildRenew({ reattach: true }));
117
+ }
118
+
119
+ async withdraw(): Promise<void> {
120
+ // Fire-and-forget signed tombstone to the current primary. A send failure is swallowed — the
121
+ // record TTL-expires as it does today (the local-half fallback) — and crucially we never bump
122
+ // `consecutiveFailures` or fail over: the participant is leaving, not recovering.
123
+ try {
124
+ await this.deps.transport.send(this.current.primary, await this.buildRenew({ withdraw: true }));
125
+ } catch {
126
+ /* best-effort; the cohort TTL bounds the leak */
127
+ }
128
+ }
129
+
130
+ /** Send a renew, mapping an RPC rejection to `undefined` (a counted failure). */
131
+ private async trySend(target: Uint8Array): Promise<RenewReplyV1 | undefined> {
132
+ try {
133
+ return await this.deps.transport.send(target, await this.buildRenew({}));
134
+ } catch {
135
+ return undefined;
136
+ }
137
+ }
138
+
139
+ private onPingSuccess(reply: RenewReplyV1): void {
140
+ this.consecutiveFailures = 0;
141
+ if (reply.result === "primary_moved") {
142
+ // Lazy cohortEpoch refresh lands here: a move discovered on a normal ping updates the hint.
143
+ this.applyPrimaryMoved(reply);
144
+ }
145
+ }
146
+
147
+ /**
148
+ * 3-fail path: re-attach to each backup in turn (a signed `reattach=true` renew). An `ok` means the
149
+ * backup accepted the crash-failover promotion → promote it locally. A `primary_moved` means a real
150
+ * rotation moved primary to a different live member → adopt that payload (not the contacted backup),
151
+ * **unless** it points back at the just-failed primary (the bounce guard) — then keep trying. All
152
+ * backups exhausted → re-run lookup from `d_max`.
153
+ */
154
+ private async failover(): Promise<void> {
155
+ const failedPrimary = this.current.primary;
156
+ for (const backup of this.current.backups) {
157
+ const reply = await this.tryReattach(backup);
158
+ if (reply === undefined) {
159
+ continue;
160
+ }
161
+ if (reply.result === "ok") {
162
+ this.promote(backup);
163
+ return;
164
+ }
165
+ if (reply.result === "primary_moved") {
166
+ // A genuine rotation: adopt the rotated assignment — but never re-adopt the dead primary
167
+ // (the defensive guard against the exact bounce crash-failover is meant to fix).
168
+ if (reply.newPrimary !== undefined && bytesEqual(b64(reply.newPrimary), failedPrimary)) {
169
+ continue;
170
+ }
171
+ this.applyPrimaryMoved(reply);
172
+ // We now have a fresh, live primary to ping — clear the strike count so the next single
173
+ // transient failure doesn't immediately re-failover (matching `promote` and the
174
+ // normal-ping `primary_moved` path, which both reset here).
175
+ this.consecutiveFailures = 0;
176
+ return;
177
+ }
178
+ // unknown_registration (replication lag) or anything else: try the next backup.
179
+ }
180
+ await this.deps.transport.relookup();
181
+ // relookup is a terminal recovery action (it owns re-establishing the registration out of
182
+ // band); reset the counter so a still-dead primary backs off to one relookup per
183
+ // MAX_PING_FAILURES cycles rather than re-running the d_max walk on every subsequent ping.
184
+ this.consecutiveFailures = 0;
185
+ }
186
+
187
+ private async tryReattach(target: Uint8Array): Promise<RenewReplyV1 | undefined> {
188
+ try {
189
+ return await this.reattach(target);
190
+ } catch {
191
+ return undefined;
192
+ }
193
+ }
194
+
195
+ /**
196
+ * Promote `target` (a backup) to primary. The `cohortEpoch` hint is deliberately *not* refreshed
197
+ * here — per the resolved open question it refreshes lazily on the next ping/renewal.
198
+ */
199
+ private promote(target: Uint8Array): void {
200
+ const backups = this.current.backups.filter((b) => !bytesEqual(b, target));
201
+ this.current = { ...this.current, primary: target, backups, lastPing: this.deps.clock() };
202
+ this.consecutiveFailures = 0;
203
+ }
204
+
205
+ private applyPrimaryMoved(reply: RenewReplyV1): void {
206
+ const patch: Partial<RegistrationRecord> = { lastPing: this.deps.clock() };
207
+ if (reply.newPrimary !== undefined) {
208
+ patch.primary = b64(reply.newPrimary);
209
+ }
210
+ if (reply.newBackups !== undefined) {
211
+ patch.backups = reply.newBackups.map(b64);
212
+ }
213
+ if (reply.cohortEpoch !== undefined) {
214
+ // Lazy refresh: the epoch hint catches up here, on the ping that discovers the move.
215
+ this.epochHint = b64(reply.cohortEpoch);
216
+ }
217
+ this.current = { ...this.current, ...patch };
218
+ }
219
+
220
+ /**
221
+ * Build a signed renew. The `reattach=true` (crash-failover) / `withdraw=true` (leave tombstone)
222
+ * flags are carried inside the signed body so the accepting member can trust the attestation; a plain
223
+ * ping omits both fields entirely (a stray renew can never silently usurp a live primary or evict a
224
+ * registration). Each flag is set only when true, matching the current plain-ping wire shape.
225
+ */
226
+ private async buildRenew(opts: { reattach?: boolean; withdraw?: boolean }): Promise<RenewV1> {
227
+ const body: UnsignedRenew = {
228
+ v: 1,
229
+ topicId: bytesKey(this.current.topicId),
230
+ participantId: bytesKey(this.current.participantId),
231
+ correlationId: this.deps.correlationId,
232
+ timestamp: this.deps.clock(),
233
+ };
234
+ if (opts.reattach === true) {
235
+ body.reattach = true;
236
+ }
237
+ if (opts.withdraw === true) {
238
+ body.withdraw = true;
239
+ }
240
+ return { ...body, signature: await this.deps.sign(body) };
241
+ }
242
+ }
243
+
244
+ /** Build a {@link RenewalParticipant} around an initial record. */
245
+ export function createRenewalParticipant(initial: RegistrationRecord, deps: RenewalParticipantDeps): RenewalParticipant {
246
+ return new TtlRenewalParticipant(initial, deps);
247
+ }
248
+
249
+ // --- cohort side ---
250
+
251
+ /** Emits cohort gossip so members converge on the active set after a touch or eviction. */
252
+ export interface RenewalGossip {
253
+ /** Gossip a `lastPing` touch for `rec`. */
254
+ touch(rec: RegistrationRecord): void;
255
+ /** Gossip an eviction for `rec`. */
256
+ evicted(rec: RegistrationRecord): void;
257
+ }
258
+
259
+ export interface RenewalCohortSideDeps {
260
+ store: RegistrationStore;
261
+ /** This member's own peer id. */
262
+ self: Uint8Array;
263
+ /** Deterministic slot assignment, shared with the handoff. */
264
+ slots: SlotAssigner;
265
+ /** Current cohort snapshot — db-p2p supplies it from the membership source. */
266
+ cohort: () => { members: readonly Uint8Array[]; cohortEpoch: Uint8Array };
267
+ gossip: RenewalGossip;
268
+ /**
269
+ * Optional dual-serve predicate, wired to the rotation handoff's `isServing`. During a handoff
270
+ * the previous primary is no longer the computed primary but must keep serving until it is acked;
271
+ * when this returns `true` `onRenew` touches and serves instead of replying `primary_moved`.
272
+ */
273
+ isServing?: (topicId: Uint8Array, participantId: Uint8Array) => boolean;
274
+ /**
275
+ * Optional participant peer-key signature verifier (db-p2p binds it to the peer-sig primitive over
276
+ * {@link import("../wire/payloads.js").renewSigningPayload}). It gates the two privileged
277
+ * participant-attested paths: a `reattach` promotion and a `withdraw` eviction. A renew on either path
278
+ * whose signature does not verify against the claimed `participantId` must never act — a stray/MITM'd
279
+ * ping cannot usurp a live primary or evict someone else's registration (§TTL and renewal). Absent →
280
+ * the gate is skipped (unit tests run without peer crypto); plain pings are never verified here (they
281
+ * only touch `lastPing`).
282
+ */
283
+ verifyParticipantSig?: (renew: RenewV1) => boolean;
284
+ /**
285
+ * Optional freshness window for the two privileged, participant-attested paths (`withdraw` eviction and
286
+ * `reattach` promotion). The renew signature already binds `timestamp` (see
287
+ * {@link import("../wire/payloads.js").renewSigningPayload}), so an attacker cannot forge a fresher
288
+ * timestamp onto a captured frame — replay is exact-frame only. A timestamp gate is therefore a
289
+ * complete freshness regime for these frames: it rejects a stale/implausibly-future `timestamp`, and a
290
+ * per-record monotonic check (`timestamp <= rec.lastPing`) closes the sub-`maxAge` fast-replay window
291
+ * using state already on the record. Defaults to the register-path skew constants
292
+ * ({@link DEFAULT_REPLAY_MAX_AGE_MS} / {@link DEFAULT_REPLAY_MAX_FUTURE_SKEW_MS}); db-p2p wires the same
293
+ * `{ maxAgeMs, maxFutureSkewMs }` config the register path's replay guard consumes so an operator tuning
294
+ * the skew window moves both paths together. Absent → defaults (key-less unit tests and callers work
295
+ * unchanged, matching how {@link verifyParticipantSig} is optional). Plain pings are NOT gated (a
296
+ * replayed ping is low-harm — it only re-touches `lastPing`).
297
+ */
298
+ freshness?: { maxAgeMs?: number; maxFutureSkewMs?: number };
299
+ }
300
+
301
+ /** Cohort-side TTL handling: touch on renew, redirect on rotation, sweep stale records. */
302
+ export interface RenewalCohortSide {
303
+ /** Handle an inbound renew. Touches the record, or replies `primary_moved` if this member is no longer primary. */
304
+ onRenew(msg: RenewV1, now: number): RenewReplyV1;
305
+ /** Evict every stale record and gossip each eviction; returns the evicted set. */
306
+ sweepStale(now: number): readonly RegistrationRecord[];
307
+ }
308
+
309
+ class StoreRenewalCohortSide implements RenewalCohortSide {
310
+ /**
311
+ * Epoch-scoped crash-failover overrides: `recordKey → cohortEpoch under which this member accepted a
312
+ * promotion`. A matching entry makes a *subsequent plain ping* serve here even though the computed
313
+ * primary is still the dead node, so the migrated participant stops bouncing. Cleared on epoch change
314
+ * (the next rotation handoff reasserts the deterministic assignment). Sibling to the rotation
315
+ * `isServing` dual-serve exception — distinct state, OR-ed into the serve decision.
316
+ */
317
+ private readonly failoverServing = new Map<string, Uint8Array>();
318
+
319
+ /** Resolved staleness window / forward-skew tolerance for the privileged freshness gate. */
320
+ private readonly maxAgeMs: number;
321
+ private readonly maxFutureSkewMs: number;
322
+
323
+ constructor(private readonly deps: RenewalCohortSideDeps) {
324
+ this.maxAgeMs = deps.freshness?.maxAgeMs ?? DEFAULT_REPLAY_MAX_AGE_MS;
325
+ this.maxFutureSkewMs = deps.freshness?.maxFutureSkewMs ?? DEFAULT_REPLAY_MAX_FUTURE_SKEW_MS;
326
+ }
327
+
328
+ /**
329
+ * Freshness gate for the two privileged, participant-attested branches (`withdraw`, `reattach`). Returns
330
+ * `false` — reject — when `msg.timestamp` is stale (older than `now − maxAgeMs`), implausibly future
331
+ * (newer than `now + maxFutureSkewMs`), or not strictly newer than the record's `lastPing`. The signed
332
+ * timestamp is immutable to a replayer, so this fully bounds a captured privileged frame: the skew
333
+ * window catches an old capture, and the `<= rec.lastPing` monotonic check catches a fast replay that
334
+ * still fits inside the window (e.g. a `withdraw` captured at `t0` replayed after the record was
335
+ * re-registered at `t_reregister > t0`, or a `reattach` replayed after its own accepted re-stamp
336
+ * already advanced `lastPing`).
337
+ */
338
+ private isFreshPrivileged(msg: RenewV1, rec: RegistrationRecord, now: number): boolean {
339
+ if (msg.timestamp < now - this.maxAgeMs) {
340
+ return false; // stale
341
+ }
342
+ if (msg.timestamp > now + this.maxFutureSkewMs) {
343
+ return false; // implausibly future
344
+ }
345
+ // NOTE: condition (3) compares a participant-supplied `msg.timestamp` against a server-maintained
346
+ // `rec.lastPing` (set from whichever cohort member last touched the record, using its own clock) — two
347
+ // different machines' clocks. In normal operation a genuine leave/failover post-dates the last ping, so
348
+ // `timestamp > lastPing` holds. If a participant's clock lags the server that set `lastPing`, a
349
+ // legitimate reattach can be rejected; the failure is soft (returns `primary_moved`, so the failover
350
+ // loop tries the next backup / re-runs the d_max lookup — a delayed failover, not data loss). If
351
+ // cross-node skew is ever observed to stall failovers, relax (3) to strict `<` (accept `timestamp ==
352
+ // lastPing`); the `maxAge` window still backstops the replay-after-re-registration attack.
353
+ if (msg.timestamp <= rec.lastPing) {
354
+ return false; // replay / non-monotonic against the live record
355
+ }
356
+ return true;
357
+ }
358
+
359
+ onRenew(msg: RenewV1, now: number): RenewReplyV1 {
360
+ const topicId = b64(msg.topicId);
361
+ const participantId = b64(msg.participantId);
362
+ const rec = this.deps.store.getByParticipant(topicId, participantId);
363
+ if (rec === undefined) {
364
+ return { v: 1, result: "unknown_registration" };
365
+ }
366
+ const key = recordKey(topicId, participantId);
367
+
368
+ if (msg.withdraw === true) {
369
+ // Signed leave attestation. A forged/missing signature must never evict someone else's
370
+ // registration → ignore, revealing nothing (`unknown_registration`, the same opaque answer a
371
+ // non-existent record gets). The gate is absent in key-less unit mode, matching reattach.
372
+ // `withdraw` is checked before the slot/primary computation: a withdraw needs no slot or
373
+ // primary check (any holder evicts its replica), and it takes precedence over a (malformed)
374
+ // co-set `reattach` — a record being withdrawn is gone regardless of a promotion request.
375
+ if (this.deps.verifyParticipantSig?.(msg) === false) {
376
+ return { v: 1, result: "unknown_registration" };
377
+ }
378
+ // Freshness gate: a signed withdraw is valid forever without this, so a captured one could be
379
+ // replayed after the victim's record TTL-expires and re-registers, evicting the *fresh* record.
380
+ // Reject a stale/replayed frame with the same opaque `unknown_registration` the forged-sig branch
381
+ // returns above — indistinguishable from an untrusted frame — and never delete.
382
+ if (!this.isFreshPrivileged(msg, rec, now)) {
383
+ return { v: 1, result: "unknown_registration" };
384
+ }
385
+ this.deps.store.delete(topicId, participantId);
386
+ this.failoverServing.delete(key); // mirror sweepStale: drop any crash-failover override
387
+ this.deps.gossip.evicted(rec);
388
+ return { v: 1, result: "withdrawn" };
389
+ }
390
+
391
+ const { members, cohortEpoch } = this.deps.cohort();
392
+ const { primary, backups } = this.deps.slots.assignSlots(participantId, cohortEpoch, members);
393
+ const self = this.deps.self;
394
+
395
+ if (msg.reattach === true) {
396
+ // Crash-failover promotion request (participant attests primary unreachable). The signed
397
+ // `reattach` flag is what a backup trusts to promote itself, so a missing/forged signature
398
+ // must never escalate: fall through to a plain redirect (never promote).
399
+ if (this.deps.verifyParticipantSig?.(msg) === false) {
400
+ return this.primaryMoved(primary, backups, cohortEpoch);
401
+ }
402
+ // Freshness gate: a signed reattach is valid forever without this, so a captured one could be
403
+ // replayed to force bogus primary re-stamps. Reject a stale/replayed frame with the same redirect
404
+ // the forged-sig branch returns above — revealing nothing — and never promote.
405
+ if (!this.isFreshPrivileged(msg, rec, now)) {
406
+ return this.primaryMoved(primary, backups, cohortEpoch);
407
+ }
408
+ if (bytesEqual(primary, self)) {
409
+ // A rotation already made this member the computed primary; serve, no override needed.
410
+ return this.touchAndServe(rec, now);
411
+ }
412
+ if (backups.some((b) => bytesEqual(b, self))) {
413
+ // Legitimate backup takeover: re-stamp primary, gossip the new assignment, and record an
414
+ // epoch-scoped override so subsequent plain pings keep being served here.
415
+ const restamped: RegistrationRecord = {
416
+ ...rec,
417
+ primary: self,
418
+ backups: rec.backups.filter((b) => !bytesEqual(b, self)),
419
+ lastPing: now,
420
+ };
421
+ this.deps.store.put(restamped);
422
+ this.failoverServing.set(key, cohortEpoch);
423
+ this.deps.gossip.touch(restamped);
424
+ return { v: 1, result: "ok" };
425
+ }
426
+ if (this.deps.isServing?.(topicId, participantId) === true) {
427
+ // Rotation dual-serve already covers this record here.
428
+ return this.touchAndServe(rec, now);
429
+ }
430
+ // Not a valid takeover target (stale participant view): redirect, do not promote.
431
+ return this.primaryMoved(primary, backups, cohortEpoch);
432
+ }
433
+
434
+ // Plain ping.
435
+ // NOTE: plain pings are deliberately NOT run through the privileged freshness gate (isFreshPrivileged).
436
+ // A replayed ping is low-harm — it can only re-touch a record's `lastPing`, never delete or usurp — and
437
+ // the strict `timestamp <= lastPing` monotonic check would risk rejecting a legitimate ping that arrives
438
+ // slightly out of order or under minor participant-clock non-monotonicity. If plain-ping replay ever
439
+ // becomes a concern (e.g. touch-driven traffic accounting is abused), gate it here too.
440
+ const isComputedPrimary = bytesEqual(primary, self);
441
+ const override = this.failoverServing.get(key);
442
+ const overrideMatches = override !== undefined && bytesEqual(override, cohortEpoch);
443
+ // Housekeeping: drop a now-redundant override (this member is the computed primary again) or a
444
+ // stale one tagged under a prior epoch (the rotation handoff governs across the epoch change).
445
+ if (override !== undefined && (isComputedPrimary || !overrideMatches)) {
446
+ this.failoverServing.delete(key);
447
+ }
448
+ const serving = isComputedPrimary || this.deps.isServing?.(topicId, participantId) === true || overrideMatches;
449
+ if (serving) {
450
+ return this.touchAndServe(rec, now);
451
+ }
452
+ return this.primaryMoved(primary, backups, cohortEpoch);
453
+ }
454
+
455
+ /** Existing touch path: stamp `lastPing`, gossip the touch, reply `ok`. */
456
+ private touchAndServe(rec: RegistrationRecord, now: number): RenewReplyV1 {
457
+ const touched: RegistrationRecord = { ...rec, lastPing: now };
458
+ this.deps.store.put(touched);
459
+ this.deps.gossip.touch(touched);
460
+ return { v: 1, result: "ok" };
461
+ }
462
+
463
+ private primaryMoved(primary: Uint8Array, backups: readonly Uint8Array[], cohortEpoch: Uint8Array): RenewReplyV1 {
464
+ return {
465
+ v: 1,
466
+ result: "primary_moved",
467
+ newPrimary: bytesKey(primary),
468
+ newBackups: backups.map(bytesKey),
469
+ cohortEpoch: bytesKey(cohortEpoch),
470
+ };
471
+ }
472
+
473
+ sweepStale(now: number): readonly RegistrationRecord[] {
474
+ const evicted = this.deps.store.evictStale(now);
475
+ for (const rec of evicted) {
476
+ // Drop any crash-failover override for an evicted record. Otherwise it leaks (the record gets
477
+ // no more pings, so the plain-ping housekeeping that would clear it never runs) and — if the
478
+ // same `(topic, participant)` re-registers under the unchanged epoch — could wrongly make a
479
+ // non-primary member keep serving via the stale override.
480
+ this.failoverServing.delete(recordKey(rec.topicId, rec.participantId));
481
+ this.deps.gossip.evicted(rec);
482
+ }
483
+ return evicted;
484
+ }
485
+ }
486
+
487
+ /** Build a {@link RenewalCohortSide}. */
488
+ export function createRenewalCohortSide(deps: RenewalCohortSideDeps): RenewalCohortSide {
489
+ return new StoreRenewalCohortSide(deps);
490
+ }
491
+
492
+ /** Decode a base64url wire field to bytes (the canonical byte form on the wire). */
493
+ function b64(s: string): Uint8Array {
494
+ return b64urlToBytes(s);
495
+ }