@peerbit/shared-log 13.2.32 → 13.2.33

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.
@@ -0,0 +1,1045 @@
1
+ import { serialize } from "@dao-xyz/borsh";
2
+ import { type PublicSignKey, randomBytes, sha256Sync } from "@peerbit/crypto";
3
+ import {
4
+ SYNC_CAPABILITY_REPLICATION_INFO_V2_DECODE,
5
+ SYNC_CAPABILITY_REPLICATION_INFO_V2_SEND,
6
+ } from "./exchange-heads.js";
7
+ import { deriveReplicationInfoV2ReceiverBinding } from "./replication-info-v2-binding.js";
8
+ import {
9
+ AddedReplicationInfoV2Message,
10
+ AddedReplicationSegmentMessage,
11
+ AllReplicatingSegmentsMessage,
12
+ FullReplicationInfoV2Message,
13
+ type ReplicationInfoV2Message,
14
+ RequestReplicationInfoV2Message,
15
+ StoppedReplicating,
16
+ StoppedReplicationInfoV2Message,
17
+ } from "./replication.js";
18
+
19
+ const REQUIRED_SENDER_CAPABILITIES =
20
+ SYNC_CAPABILITY_REPLICATION_INFO_V2_DECODE |
21
+ SYNC_CAPABILITY_REPLICATION_INFO_V2_SEND;
22
+
23
+ const DEFAULT_REQUEST_RETRY_MS = 1_000;
24
+ const DEFAULT_MAX_REQUEST_RETRY_MS = 30_000;
25
+ const DEFAULT_REQUEST_MAX_ATTEMPTS = 7;
26
+ const DEFAULT_LEGACY_FALLBACK_DELAY_MS = 5_000;
27
+ const MAX_U64 = (1n << 64n) - 1n;
28
+
29
+ const bytesEqual = (left: Uint8Array, right: Uint8Array): boolean => {
30
+ if (left.byteLength !== right.byteLength) {
31
+ return false;
32
+ }
33
+ for (let index = 0; index < left.byteLength; index++) {
34
+ if (left[index] !== right[index]) {
35
+ return false;
36
+ }
37
+ }
38
+ return true;
39
+ };
40
+
41
+ type LegacyReplicationInfoMessage =
42
+ | AllReplicatingSegmentsMessage
43
+ | AddedReplicationSegmentMessage
44
+ | StoppedReplicating;
45
+
46
+ const replicationInfoPayloadFingerprint = (
47
+ message: ReplicationInfoV2Message | LegacyReplicationInfoMessage,
48
+ ): Uint8Array => {
49
+ const canonical =
50
+ message instanceof FullReplicationInfoV2Message
51
+ ? new AllReplicatingSegmentsMessage({ segments: message.segments })
52
+ : message instanceof AddedReplicationInfoV2Message
53
+ ? new AddedReplicationSegmentMessage({ segments: message.segments })
54
+ : message instanceof StoppedReplicationInfoV2Message
55
+ ? new StoppedReplicating({ segmentIds: message.segmentIds })
56
+ : message;
57
+ return sha256Sync(serialize(canonical));
58
+ };
59
+
60
+ export type ReplicationInfoV2ReceivePhase =
61
+ | "awaiting-full"
62
+ | "active"
63
+ | "resync";
64
+
65
+ type LocalCapabilityReady = {
66
+ peerHash: string;
67
+ receiverTransportSession: bigint;
68
+ /**
69
+ * The local capability envelope and the later RequestV2 envelope use the
70
+ * same millisecond clock. The sender deliberately requires the request to
71
+ * be strictly newer, so never construct it in this captured millisecond.
72
+ */
73
+ requestNotBeforeMs: number;
74
+ };
75
+
76
+ export type ReplicationInfoV2LocalCapabilityRefresh = {
77
+ receiverTransportSession: bigint;
78
+ requestNotBeforeMs: number;
79
+ };
80
+
81
+ export type ReplicationInfoV2ReceiveState = {
82
+ peerHash: string;
83
+ target: PublicSignKey;
84
+ peerSession: object;
85
+ receiveEpoch: object | null;
86
+ capabilities: number;
87
+ capabilityTimestamp: bigint;
88
+ senderTransportSession: bigint;
89
+ receiverTransportSession?: bigint;
90
+ receiverRequestChallenge: Uint8Array;
91
+ receiverBinding?: Uint8Array;
92
+ senderEpoch?: Uint8Array;
93
+ lastSequence?: bigint;
94
+ phase: ReplicationInfoV2ReceivePhase;
95
+ version: number;
96
+ controller: AbortController;
97
+ requestTimer?: ReturnType<typeof setTimeout>;
98
+ requestInFlight?: Promise<void>;
99
+ requestAttempts: number;
100
+ requestsSinceCapabilityRefresh: number;
101
+ requestParked: boolean;
102
+ capabilityRefreshRequired: boolean;
103
+ legacyFallbackTimer?: ReturnType<typeof setTimeout>;
104
+ legacyFallbackFingerprint?: Uint8Array;
105
+ legacyFallbackTimestamp?: bigint;
106
+ legacyFallbackAmbiguous: boolean;
107
+ lastCommittedTransportTimestamp?: bigint;
108
+ recentCommittedPayloads: Array<{
109
+ fingerprint: Uint8Array;
110
+ transportTimestamp: bigint;
111
+ }>;
112
+ lastLegacyObservationTimestamp?: bigint;
113
+ lastLegacyObservationFingerprint?: Uint8Array;
114
+ lastLegacyObservationAmbiguous: boolean;
115
+ reservedAdmission?: ReplicationInfoV2ReceiveAdmission;
116
+ };
117
+
118
+ export type ReplicationInfoV2ReceiveAdmission = {
119
+ state: ReplicationInfoV2ReceiveState;
120
+ version: number;
121
+ receiveEpoch: object | null;
122
+ message: ReplicationInfoV2Message;
123
+ kind: "full" | "added" | "stopped";
124
+ payloadFingerprint: Uint8Array;
125
+ transportTimestamp: bigint;
126
+ committed: boolean;
127
+ resyncAfterRelease?: boolean;
128
+ };
129
+
130
+ export type ReplicationInfoV2ReceiveDeps = {
131
+ getSelfKey: () => PublicSignKey;
132
+ getReceiverTransportSession: () => bigint;
133
+ isClosed: () => boolean;
134
+ isPeerStateCurrent: (
135
+ peerHash: string,
136
+ peerSession: object,
137
+ receiveEpoch: object | null,
138
+ ) => boolean;
139
+ isSenderTransportSessionCurrent: (
140
+ peerHash: string,
141
+ senderTransportSession: bigint,
142
+ ) => boolean;
143
+ sendRequest: (
144
+ request: RequestReplicationInfoV2Message,
145
+ target: PublicSignKey,
146
+ signal: AbortSignal,
147
+ ) => Promise<void>;
148
+ refreshLocalCapability: (properties: {
149
+ peerHash: string;
150
+ target: PublicSignKey;
151
+ peerSession: object;
152
+ receiveEpoch: object | null;
153
+ signal: AbortSignal;
154
+ }) => Promise<ReplicationInfoV2LocalCapabilityRefresh | undefined>;
155
+ onRequestError?: (error: unknown) => void;
156
+ now?: () => number;
157
+ requestRetryMs?: number;
158
+ maxRequestRetryMs?: number;
159
+ requestMaxAttempts?: number;
160
+ legacyFallbackDelayMs?: number;
161
+ };
162
+
163
+ /**
164
+ * Authenticated receive grants and sender-authoritative ordering for
165
+ * replication-info V2. State is bounded to one entry per subscribed peer and
166
+ * is always scoped to the exact PeerSession object.
167
+ */
168
+ export class ReplicationInfoV2ReceiveCoordinator {
169
+ _receiveStates!: Map<string, ReplicationInfoV2ReceiveState>;
170
+ _cutoverPeerSessions!: WeakSet<object>;
171
+ _localCapabilityReadyBySession!: WeakMap<object, LocalCapabilityReady>;
172
+ _reservedAdmissionsByPeer!: Map<string, ReplicationInfoV2ReceiveAdmission>;
173
+
174
+ private readonly now: () => number;
175
+ private readonly requestRetryMs: number;
176
+ private readonly maxRequestRetryMs: number;
177
+ private readonly requestMaxAttempts: number;
178
+ private readonly legacyFallbackDelayMs: number;
179
+
180
+ constructor(private readonly deps: ReplicationInfoV2ReceiveDeps) {
181
+ this.now = deps.now ?? Date.now;
182
+ this.requestRetryMs = Math.max(
183
+ 1,
184
+ deps.requestRetryMs ?? DEFAULT_REQUEST_RETRY_MS,
185
+ );
186
+ this.maxRequestRetryMs = Math.max(
187
+ this.requestRetryMs,
188
+ deps.maxRequestRetryMs ?? DEFAULT_MAX_REQUEST_RETRY_MS,
189
+ );
190
+ this.requestMaxAttempts = Math.max(
191
+ 1,
192
+ Math.floor(deps.requestMaxAttempts ?? DEFAULT_REQUEST_MAX_ATTEMPTS),
193
+ );
194
+ this.legacyFallbackDelayMs = Math.max(
195
+ this.requestRetryMs,
196
+ deps.legacyFallbackDelayMs ?? DEFAULT_LEGACY_FALLBACK_DELAY_MS,
197
+ );
198
+ this._receiveStates = new Map();
199
+ this._cutoverPeerSessions = new WeakSet();
200
+ this._localCapabilityReadyBySession = new WeakMap();
201
+ this._reservedAdmissionsByPeer = new Map();
202
+ }
203
+
204
+ resetForOpen(): void {
205
+ this.clearForClose();
206
+ this._receiveStates = new Map();
207
+ this._cutoverPeerSessions = new WeakSet();
208
+ this._localCapabilityReadyBySession = new WeakMap();
209
+ this._reservedAdmissionsByPeer = new Map();
210
+ }
211
+
212
+ clearForClose(): void {
213
+ for (const state of this._receiveStates?.values() ?? []) {
214
+ this.clearState(state);
215
+ }
216
+ this._receiveStates?.clear();
217
+ this._cutoverPeerSessions = new WeakSet();
218
+ this._localCapabilityReadyBySession = new WeakMap();
219
+ }
220
+
221
+ clearPeer(peerHash: string, expectedSession?: object): void {
222
+ const state = this._receiveStates.get(peerHash);
223
+ if (state && (!expectedSession || state.peerSession === expectedSession)) {
224
+ this.clearState(state);
225
+ this._localCapabilityReadyBySession.delete(state.peerSession);
226
+ this._cutoverPeerSessions.delete(state.peerSession);
227
+ }
228
+ if (expectedSession) {
229
+ this._localCapabilityReadyBySession.delete(expectedSession);
230
+ this._cutoverPeerSessions.delete(expectedSession);
231
+ }
232
+ }
233
+
234
+ /** Revoke an unauthenticated or downgraded capability generation. */
235
+ revokePeerCapability(peerHash: string, reopenLegacy = true): void {
236
+ const state = this._receiveStates.get(peerHash);
237
+ if (!state) {
238
+ return;
239
+ }
240
+ this.clearState(state);
241
+ if (reopenLegacy) {
242
+ this._cutoverPeerSessions.delete(state.peerSession);
243
+ }
244
+ }
245
+
246
+ private clearState(state: ReplicationInfoV2ReceiveState): void {
247
+ if (state.requestTimer) {
248
+ clearTimeout(state.requestTimer);
249
+ state.requestTimer = undefined;
250
+ }
251
+ if (state.legacyFallbackTimer) {
252
+ clearTimeout(state.legacyFallbackTimer);
253
+ state.legacyFallbackTimer = undefined;
254
+ }
255
+ state.controller.abort();
256
+ state.version++;
257
+ if (this._receiveStates.get(state.peerHash) === state) {
258
+ this._receiveStates.delete(state.peerHash);
259
+ }
260
+ }
261
+
262
+ /** Record success of this session's ACKed local APPLY advertisement. */
263
+ markLocalCapabilityReady(properties: {
264
+ peerHash: string;
265
+ peerSession: object;
266
+ receiveEpoch: object | null;
267
+ receiverTransportSession: bigint;
268
+ requestNotBeforeMs: number;
269
+ }): boolean {
270
+ const { peerHash, peerSession, receiverTransportSession } = properties;
271
+ const state = this._receiveStates.get(peerHash);
272
+ if (
273
+ this.deps.isClosed() ||
274
+ !this.deps.isPeerStateCurrent(
275
+ peerHash,
276
+ peerSession,
277
+ properties.receiveEpoch,
278
+ ) ||
279
+ this.deps.getReceiverTransportSession() !== receiverTransportSession
280
+ ) {
281
+ return false;
282
+ }
283
+
284
+ const ready: LocalCapabilityReady = {
285
+ peerHash,
286
+ receiverTransportSession,
287
+ requestNotBeforeMs: properties.requestNotBeforeMs,
288
+ };
289
+ this._localCapabilityReadyBySession.set(peerSession, ready);
290
+ if (
291
+ state?.peerSession === peerSession &&
292
+ state.receiveEpoch === properties.receiveEpoch
293
+ ) {
294
+ if (
295
+ state.receiverTransportSession !== undefined &&
296
+ state.receiverTransportSession !== receiverTransportSession
297
+ ) {
298
+ this.clearState(state);
299
+ return false;
300
+ }
301
+ this.bindLocalCapability(state, ready);
302
+ state.requestAttempts = 0;
303
+ state.requestsSinceCapabilityRefresh = 0;
304
+ state.requestParked = false;
305
+ this.armRequest(state, 0);
306
+ }
307
+ return true;
308
+ }
309
+
310
+ /**
311
+ * Promote one signed capability generation after the opening barrier has
312
+ * committed. Repeated same-session advertisements refresh freshness only;
313
+ * they never reset sequence state.
314
+ */
315
+ observeCapability(properties: {
316
+ peerHash: string;
317
+ target: PublicSignKey;
318
+ peerSession: object;
319
+ receiveEpoch: object | null;
320
+ capabilities: number;
321
+ senderTransportSession: bigint;
322
+ capabilityTimestamp: bigint;
323
+ }): boolean {
324
+ const {
325
+ peerHash,
326
+ target,
327
+ peerSession,
328
+ receiveEpoch,
329
+ capabilities,
330
+ senderTransportSession,
331
+ capabilityTimestamp,
332
+ } = properties;
333
+ if (
334
+ target.equals(this.deps.getSelfKey()) ||
335
+ this.deps.isClosed() ||
336
+ !this.deps.isPeerStateCurrent(peerHash, peerSession, receiveEpoch)
337
+ ) {
338
+ return false;
339
+ }
340
+
341
+ const senderReady =
342
+ (capabilities & REQUIRED_SENDER_CAPABILITIES) ===
343
+ REQUIRED_SENDER_CAPABILITIES;
344
+ let state = this._receiveStates.get(peerHash);
345
+ if (
346
+ state &&
347
+ (state.peerSession !== peerSession ||
348
+ state.senderTransportSession !== senderTransportSession ||
349
+ !state.target.equals(target))
350
+ ) {
351
+ const preserveCutover = state.peerSession === peerSession && senderReady;
352
+ this.clearState(state);
353
+ if (!preserveCutover) {
354
+ this._cutoverPeerSessions.delete(state.peerSession);
355
+ }
356
+ state = undefined;
357
+ }
358
+ if (!senderReady) {
359
+ if (state) {
360
+ this.clearState(state);
361
+ this._cutoverPeerSessions.delete(peerSession);
362
+ }
363
+ return false;
364
+ }
365
+
366
+ if (state) {
367
+ if (capabilityTimestamp < state.capabilityTimestamp) {
368
+ return false;
369
+ }
370
+ const previousCapabilities = state.capabilities;
371
+ const previousTimestamp = state.capabilityTimestamp;
372
+ const receiveEpochChanged = state.receiveEpoch !== receiveEpoch;
373
+ const addsCapabilities = (capabilities & ~previousCapabilities) !== 0;
374
+ if (
375
+ capabilityTimestamp === previousTimestamp &&
376
+ !addsCapabilities &&
377
+ !receiveEpochChanged
378
+ ) {
379
+ return true;
380
+ }
381
+ state.capabilities |= capabilities;
382
+ state.capabilityTimestamp = capabilityTimestamp;
383
+ if (receiveEpochChanged) {
384
+ state.receiveEpoch = receiveEpoch;
385
+ this.transitionToResync(state, {
386
+ force: true,
387
+ refreshCapability: true,
388
+ });
389
+ }
390
+ const ready = this._localCapabilityReadyBySession.get(peerSession);
391
+ if (ready?.peerHash === peerHash && state.receiverBinding === undefined) {
392
+ this.bindLocalCapability(state, ready);
393
+ }
394
+ if (state.phase !== "active") {
395
+ state.requestAttempts = 0;
396
+ state.requestParked = false;
397
+ this.armRequest(state, 0);
398
+ }
399
+ return true;
400
+ }
401
+
402
+ const retainedCutover = this._cutoverPeerSessions.has(peerSession);
403
+ state = {
404
+ peerHash,
405
+ target,
406
+ peerSession,
407
+ receiveEpoch,
408
+ capabilities,
409
+ capabilityTimestamp,
410
+ senderTransportSession,
411
+ receiverRequestChallenge: randomBytes(32),
412
+ phase: retainedCutover ? "resync" : "awaiting-full",
413
+ version: 0,
414
+ controller: new AbortController(),
415
+ requestAttempts: 0,
416
+ requestsSinceCapabilityRefresh: 0,
417
+ requestParked: false,
418
+ capabilityRefreshRequired: retainedCutover,
419
+ legacyFallbackAmbiguous: false,
420
+ recentCommittedPayloads: [],
421
+ lastLegacyObservationAmbiguous: false,
422
+ };
423
+ this._receiveStates.set(peerHash, state);
424
+ const ready = this._localCapabilityReadyBySession.get(peerSession);
425
+ if (ready?.peerHash === peerHash) {
426
+ this.bindLocalCapability(state, ready);
427
+ this.armRequest(state, 0);
428
+ }
429
+ return true;
430
+ }
431
+
432
+ private bindLocalCapability(
433
+ state: ReplicationInfoV2ReceiveState,
434
+ ready: LocalCapabilityReady,
435
+ ): void {
436
+ state.receiverTransportSession = ready.receiverTransportSession;
437
+ state.receiverBinding = deriveReplicationInfoV2ReceiverBinding({
438
+ receiverChallenge: state.receiverRequestChallenge,
439
+ receiver: this.deps.getSelfKey(),
440
+ receiverTransportSession: ready.receiverTransportSession,
441
+ sender: state.target,
442
+ senderTransportSession: state.senderTransportSession,
443
+ });
444
+ }
445
+
446
+ /** Require a fresh capability-bound grant and authoritative Full. */
447
+ advanceRecovery(properties: {
448
+ peerHash: string;
449
+ peerSession: object;
450
+ receiveEpoch: object | null;
451
+ }): boolean {
452
+ const state = this._receiveStates.get(properties.peerHash);
453
+ if (!state || state.peerSession !== properties.peerSession) {
454
+ return false;
455
+ }
456
+ state.receiveEpoch = properties.receiveEpoch;
457
+ this.transitionToResync(state, {
458
+ force: true,
459
+ refreshCapability: true,
460
+ });
461
+ return true;
462
+ }
463
+
464
+ private transitionToResync(
465
+ state: ReplicationInfoV2ReceiveState,
466
+ options?: { force?: boolean; refreshCapability?: boolean },
467
+ ): void {
468
+ const shouldRestart =
469
+ state.phase !== "resync" ||
470
+ options?.force === true ||
471
+ state.requestParked;
472
+ if (state.phase !== "resync" || options?.force === true) {
473
+ state.phase = "resync";
474
+ state.version++;
475
+ }
476
+ if (options?.refreshCapability) {
477
+ state.capabilityRefreshRequired = true;
478
+ }
479
+ if (shouldRestart) {
480
+ state.requestAttempts = 0;
481
+ state.requestParked = false;
482
+ this.armRequest(state, 0);
483
+ }
484
+ }
485
+
486
+ isLegacyCutover(peerSession: object | null): boolean {
487
+ return peerSession !== null && this._cutoverPeerSessions.has(peerSession);
488
+ }
489
+
490
+ prepare(
491
+ message: ReplicationInfoV2Message,
492
+ properties: {
493
+ from: PublicSignKey;
494
+ peerSession: object;
495
+ receiveEpoch: object | null;
496
+ senderTransportSession: bigint;
497
+ transportTimestamp: bigint;
498
+ },
499
+ ): ReplicationInfoV2ReceiveAdmission | undefined {
500
+ const peerHash = properties.from.hashcode();
501
+ const state = this._receiveStates.get(peerHash);
502
+ if (
503
+ !state ||
504
+ state.peerSession !== properties.peerSession ||
505
+ state.receiveEpoch !== properties.receiveEpoch ||
506
+ state.senderTransportSession !== properties.senderTransportSession ||
507
+ !state.target.equals(properties.from) ||
508
+ !state.receiverBinding ||
509
+ !bytesEqual(message.receiverChallenge, state.receiverBinding) ||
510
+ message.sequence <= 0n ||
511
+ !this.isStateCurrent(state)
512
+ ) {
513
+ return undefined;
514
+ }
515
+
516
+ const kind =
517
+ message instanceof FullReplicationInfoV2Message
518
+ ? "full"
519
+ : message instanceof AddedReplicationInfoV2Message
520
+ ? "added"
521
+ : message instanceof StoppedReplicationInfoV2Message
522
+ ? "stopped"
523
+ : undefined;
524
+ if (!kind) {
525
+ return undefined;
526
+ }
527
+
528
+ if (state.senderEpoch === undefined) {
529
+ if (kind !== "full") {
530
+ return undefined;
531
+ }
532
+ } else if (!bytesEqual(message.senderEpoch, state.senderEpoch)) {
533
+ return undefined;
534
+ }
535
+
536
+ const lastSequence = state.lastSequence;
537
+ if (kind === "full") {
538
+ if (lastSequence !== undefined && message.sequence <= lastSequence) {
539
+ return undefined;
540
+ }
541
+ } else {
542
+ if (
543
+ state.phase !== "active" ||
544
+ lastSequence === undefined ||
545
+ message.sequence !== lastSequence + 1n
546
+ ) {
547
+ if (
548
+ state.phase === "active" &&
549
+ lastSequence !== undefined &&
550
+ message.sequence > lastSequence + 1n
551
+ ) {
552
+ this.transitionToResync(state);
553
+ }
554
+ return undefined;
555
+ }
556
+ }
557
+
558
+ return {
559
+ state,
560
+ version: state.version,
561
+ receiveEpoch: state.receiveEpoch,
562
+ message,
563
+ kind,
564
+ payloadFingerprint: replicationInfoPayloadFingerprint(message),
565
+ transportTimestamp: properties.transportTimestamp,
566
+ committed: false,
567
+ };
568
+ }
569
+
570
+ /** Reserve at most one decoded V2 frame per peer ahead of the apply lane. */
571
+ reserve(
572
+ message: ReplicationInfoV2Message,
573
+ properties: {
574
+ from: PublicSignKey;
575
+ peerSession: object;
576
+ receiveEpoch: object | null;
577
+ senderTransportSession: bigint;
578
+ transportTimestamp: bigint;
579
+ },
580
+ ): ReplicationInfoV2ReceiveAdmission | undefined {
581
+ const peerHash = properties.from.hashcode();
582
+ const reserved = this._reservedAdmissionsByPeer.get(peerHash);
583
+ if (reserved) {
584
+ const state = reserved.state;
585
+ const currentState = this._receiveStates.get(peerHash);
586
+ const knownMessage =
587
+ message instanceof FullReplicationInfoV2Message ||
588
+ message instanceof AddedReplicationInfoV2Message ||
589
+ message instanceof StoppedReplicationInfoV2Message;
590
+ if (
591
+ knownMessage &&
592
+ this._receiveStates.get(peerHash) === state &&
593
+ state.peerSession === properties.peerSession &&
594
+ state.receiveEpoch === properties.receiveEpoch &&
595
+ state.senderTransportSession === properties.senderTransportSession &&
596
+ state.target.equals(properties.from) &&
597
+ state.receiverBinding !== undefined &&
598
+ bytesEqual(message.receiverChallenge, state.receiverBinding) &&
599
+ bytesEqual(message.senderEpoch, reserved.message.senderEpoch) &&
600
+ message.sequence > reserved.message.sequence &&
601
+ this.isStateCurrent(state)
602
+ ) {
603
+ // Transport ACKs precede application. Do not invalidate the frame
604
+ // already applying, and do not retain an unbounded successor queue.
605
+ // Commit the reservation, then request one authoritative Full.
606
+ reserved.resyncAfterRelease = true;
607
+ } else if (
608
+ knownMessage &&
609
+ currentState !== undefined &&
610
+ currentState !== state &&
611
+ this.prepare(message, properties)?.state === currentState
612
+ ) {
613
+ // A previous generation can still be parked in the host apply lane.
614
+ // Transport already ACKed this current-generation frame, so wake the
615
+ // current state once the peer-global reservation is finally released.
616
+ reserved.resyncAfterRelease = true;
617
+ }
618
+ return undefined;
619
+ }
620
+ const admission = this.prepare(message, properties);
621
+ if (!admission) {
622
+ return undefined;
623
+ }
624
+ const { state } = admission;
625
+ this._reservedAdmissionsByPeer.set(peerHash, admission);
626
+ state.reservedAdmission = admission;
627
+ return admission;
628
+ }
629
+
630
+ release(admission: ReplicationInfoV2ReceiveAdmission): void {
631
+ const { state } = admission;
632
+ if (this._reservedAdmissionsByPeer.get(state.peerHash) !== admission) {
633
+ return;
634
+ }
635
+ this._reservedAdmissionsByPeer.delete(state.peerHash);
636
+ if (state.reservedAdmission === admission) {
637
+ state.reservedAdmission = undefined;
638
+ }
639
+ const currentState = this._receiveStates.get(state.peerHash);
640
+ if (
641
+ currentState &&
642
+ this.isStateCurrent(currentState) &&
643
+ (admission.resyncAfterRelease ||
644
+ (currentState !== state && currentState.phase !== "active"))
645
+ ) {
646
+ this.transitionToResync(currentState, { force: true });
647
+ }
648
+ }
649
+
650
+ /**
651
+ * B9 can lose its sender grant while keeping the topic session open. Its
652
+ * unmatched legacy sidecar is the compatibility signal to re-handshake; a
653
+ * matching V2 payload before or after the legacy copy suppresses the timer.
654
+ */
655
+ noteLegacyAnnouncement(properties: {
656
+ peerHash: string;
657
+ peerSession: object;
658
+ receiveEpoch: object | null;
659
+ senderTransportSession: bigint;
660
+ transportTimestamp: bigint;
661
+ message: LegacyReplicationInfoMessage;
662
+ }): boolean {
663
+ const state = this._receiveStates.get(properties.peerHash);
664
+ if (
665
+ !state ||
666
+ state.peerSession !== properties.peerSession ||
667
+ state.receiveEpoch !== properties.receiveEpoch ||
668
+ state.senderTransportSession !== properties.senderTransportSession ||
669
+ !this._cutoverPeerSessions.has(properties.peerSession) ||
670
+ !this.isStateCurrent(state)
671
+ ) {
672
+ return false;
673
+ }
674
+ const fingerprint = replicationInfoPayloadFingerprint(properties.message);
675
+ if (
676
+ state.lastCommittedTransportTimestamp !== undefined &&
677
+ properties.transportTimestamp < state.lastCommittedTransportTimestamp
678
+ ) {
679
+ return true;
680
+ }
681
+ if (
682
+ state.recentCommittedPayloads.some(
683
+ (committed) =>
684
+ properties.transportTimestamp <= committed.transportTimestamp &&
685
+ bytesEqual(committed.fingerprint, fingerprint),
686
+ )
687
+ ) {
688
+ return true;
689
+ }
690
+ if (state.lastLegacyObservationTimestamp !== undefined) {
691
+ if (
692
+ properties.transportTimestamp < state.lastLegacyObservationTimestamp
693
+ ) {
694
+ return true;
695
+ }
696
+ if (
697
+ properties.transportTimestamp === state.lastLegacyObservationTimestamp
698
+ ) {
699
+ if (
700
+ state.lastLegacyObservationAmbiguous ||
701
+ (state.lastLegacyObservationFingerprint !== undefined &&
702
+ bytesEqual(state.lastLegacyObservationFingerprint, fingerprint))
703
+ ) {
704
+ return true;
705
+ }
706
+ state.lastLegacyObservationAmbiguous = true;
707
+ } else {
708
+ state.lastLegacyObservationTimestamp = properties.transportTimestamp;
709
+ state.lastLegacyObservationFingerprint = fingerprint.slice();
710
+ state.lastLegacyObservationAmbiguous = false;
711
+ }
712
+ } else {
713
+ state.lastLegacyObservationTimestamp = properties.transportTimestamp;
714
+ state.lastLegacyObservationFingerprint = fingerprint.slice();
715
+ state.lastLegacyObservationAmbiguous = false;
716
+ }
717
+ const reserved = this._reservedAdmissionsByPeer.get(properties.peerHash);
718
+ if (
719
+ reserved?.state === state &&
720
+ !bytesEqual(reserved.payloadFingerprint, fingerprint)
721
+ ) {
722
+ // A legacy sidecar observed while a different V2 payload is applying
723
+ // cannot be erased by that commit. The transport has already ACKed the
724
+ // sidecar, so require one authoritative successor after release.
725
+ reserved.resyncAfterRelease = true;
726
+ }
727
+ if (!state.legacyFallbackFingerprint) {
728
+ state.legacyFallbackFingerprint = fingerprint;
729
+ state.legacyFallbackTimestamp = properties.transportTimestamp;
730
+ state.legacyFallbackAmbiguous = false;
731
+ } else {
732
+ if (!bytesEqual(state.legacyFallbackFingerprint, fingerprint)) {
733
+ state.legacyFallbackAmbiguous = true;
734
+ }
735
+ if (
736
+ state.legacyFallbackTimestamp === undefined ||
737
+ properties.transportTimestamp > state.legacyFallbackTimestamp
738
+ ) {
739
+ state.legacyFallbackTimestamp = properties.transportTimestamp;
740
+ }
741
+ }
742
+ if (state.phase !== "active") {
743
+ if (state.requestParked) {
744
+ this.transitionToResync(state, {
745
+ force: true,
746
+ refreshCapability: true,
747
+ });
748
+ }
749
+ return true;
750
+ }
751
+ if (state.legacyFallbackTimer) {
752
+ return true;
753
+ }
754
+ state.legacyFallbackTimer = setTimeout(() => {
755
+ state.legacyFallbackTimer = undefined;
756
+ if (this.isStateCurrent(state) && state.phase === "active") {
757
+ this.transitionToResync(state, {
758
+ force: true,
759
+ refreshCapability: true,
760
+ });
761
+ }
762
+ }, this.legacyFallbackDelayMs);
763
+ state.legacyFallbackTimer.unref?.();
764
+ return true;
765
+ }
766
+
767
+ private clearLegacyFallback(state: ReplicationInfoV2ReceiveState): void {
768
+ if (state.legacyFallbackTimer) {
769
+ clearTimeout(state.legacyFallbackTimer);
770
+ state.legacyFallbackTimer = undefined;
771
+ }
772
+ state.legacyFallbackFingerprint = undefined;
773
+ state.legacyFallbackTimestamp = undefined;
774
+ state.legacyFallbackAmbiguous = false;
775
+ }
776
+
777
+ isAdmissionCurrent(admission: ReplicationInfoV2ReceiveAdmission): boolean {
778
+ const { state } = admission;
779
+ return (
780
+ state.version === admission.version &&
781
+ state.receiveEpoch === admission.receiveEpoch &&
782
+ this.isStateCurrent(state)
783
+ );
784
+ }
785
+
786
+ commit(admission: ReplicationInfoV2ReceiveAdmission): boolean {
787
+ if (admission.committed) {
788
+ return this.isAdmissionCurrent(admission);
789
+ }
790
+ if (!this.isAdmissionCurrent(admission)) {
791
+ this.release(admission);
792
+ return false;
793
+ }
794
+ const { state, message } = admission;
795
+ if (admission.kind === "full") {
796
+ if (state.senderEpoch === undefined) {
797
+ state.senderEpoch = message.senderEpoch.slice();
798
+ this._cutoverPeerSessions.add(state.peerSession);
799
+ } else if (!bytesEqual(state.senderEpoch, message.senderEpoch)) {
800
+ this.release(admission);
801
+ return false;
802
+ }
803
+ } else if (
804
+ state.senderEpoch === undefined ||
805
+ !bytesEqual(state.senderEpoch, message.senderEpoch)
806
+ ) {
807
+ this.release(admission);
808
+ return false;
809
+ }
810
+ state.lastSequence = message.sequence;
811
+ state.phase = "active";
812
+ state.requestAttempts = 0;
813
+ state.requestsSinceCapabilityRefresh = 0;
814
+ state.requestParked = false;
815
+ state.capabilityRefreshRequired = false;
816
+ state.lastCommittedTransportTimestamp = admission.transportTimestamp;
817
+ state.recentCommittedPayloads.push({
818
+ fingerprint: admission.payloadFingerprint.slice(),
819
+ transportTimestamp: admission.transportTimestamp,
820
+ });
821
+ if (state.recentCommittedPayloads.length > 8) {
822
+ state.recentCommittedPayloads.shift();
823
+ }
824
+ if (
825
+ (state.legacyFallbackTimestamp !== undefined &&
826
+ admission.transportTimestamp > state.legacyFallbackTimestamp) ||
827
+ (!state.legacyFallbackAmbiguous &&
828
+ state.legacyFallbackFingerprint !== undefined &&
829
+ bytesEqual(
830
+ state.legacyFallbackFingerprint,
831
+ admission.payloadFingerprint,
832
+ ))
833
+ ) {
834
+ this.clearLegacyFallback(state);
835
+ }
836
+ if (state.requestTimer) {
837
+ clearTimeout(state.requestTimer);
838
+ state.requestTimer = undefined;
839
+ }
840
+ state.version++;
841
+ admission.version = state.version;
842
+ admission.receiveEpoch = state.receiveEpoch;
843
+ admission.committed = true;
844
+ this.release(admission);
845
+ return true;
846
+ }
847
+
848
+ requireFullAfterFailure(
849
+ admission: ReplicationInfoV2ReceiveAdmission,
850
+ ): boolean {
851
+ if (!this.isAdmissionCurrent(admission)) {
852
+ this.release(admission);
853
+ return false;
854
+ }
855
+ this.release(admission);
856
+ this.transitionToResync(admission.state, { force: true });
857
+ return true;
858
+ }
859
+
860
+ private isStateCurrent(state: ReplicationInfoV2ReceiveState): boolean {
861
+ return (
862
+ this._receiveStates.get(state.peerHash) === state &&
863
+ !state.controller.signal.aborted &&
864
+ !this.deps.isClosed() &&
865
+ state.receiverTransportSession !== undefined &&
866
+ this.deps.getReceiverTransportSession() ===
867
+ state.receiverTransportSession &&
868
+ this.deps.isSenderTransportSessionCurrent(
869
+ state.peerHash,
870
+ state.senderTransportSession,
871
+ ) &&
872
+ this.deps.isPeerStateCurrent(
873
+ state.peerHash,
874
+ state.peerSession,
875
+ state.receiveEpoch,
876
+ )
877
+ );
878
+ }
879
+
880
+ private armRequest(
881
+ state: ReplicationInfoV2ReceiveState,
882
+ delayMs: number,
883
+ ): void {
884
+ if (state.requestTimer) {
885
+ clearTimeout(state.requestTimer);
886
+ }
887
+ if (
888
+ this._receiveStates.get(state.peerHash) !== state ||
889
+ state.controller.signal.aborted ||
890
+ this.deps.isClosed() ||
891
+ (this._reservedAdmissionsByPeer.has(state.peerHash) &&
892
+ this._reservedAdmissionsByPeer.get(state.peerHash)?.state !== state) ||
893
+ state.receiverBinding === undefined ||
894
+ state.phase === "active" ||
895
+ state.requestParked ||
896
+ state.lastSequence === MAX_U64
897
+ ) {
898
+ state.requestTimer = undefined;
899
+ return;
900
+ }
901
+ state.requestTimer = setTimeout(
902
+ () => {
903
+ state.requestTimer = undefined;
904
+ void this.runRequest(state);
905
+ },
906
+ Math.max(0, delayMs),
907
+ );
908
+ state.requestTimer.unref?.();
909
+ }
910
+
911
+ private requestRetryDelay(state: ReplicationInfoV2ReceiveState): number {
912
+ const exponent = Math.max(0, state.requestAttempts - 1);
913
+ return Math.min(
914
+ this.maxRequestRetryMs,
915
+ this.requestRetryMs * 2 ** Math.min(exponent, 20),
916
+ );
917
+ }
918
+
919
+ private async refreshGrant(
920
+ state: ReplicationInfoV2ReceiveState,
921
+ ): Promise<boolean> {
922
+ const refreshed = await this.deps.refreshLocalCapability({
923
+ peerHash: state.peerHash,
924
+ target: state.target,
925
+ peerSession: state.peerSession,
926
+ receiveEpoch: state.receiveEpoch,
927
+ signal: state.controller.signal,
928
+ });
929
+ if (
930
+ !refreshed ||
931
+ !this.isStateCurrent(state) ||
932
+ this.deps.getReceiverTransportSession() !==
933
+ refreshed.receiverTransportSession
934
+ ) {
935
+ return false;
936
+ }
937
+
938
+ const ready: LocalCapabilityReady = {
939
+ peerHash: state.peerHash,
940
+ receiverTransportSession: refreshed.receiverTransportSession,
941
+ requestNotBeforeMs: refreshed.requestNotBeforeMs,
942
+ };
943
+ this._localCapabilityReadyBySession.set(state.peerSession, ready);
944
+ state.receiverRequestChallenge = randomBytes(32);
945
+ state.senderEpoch = undefined;
946
+ state.lastSequence = undefined;
947
+ state.phase = "resync";
948
+ state.capabilityRefreshRequired = false;
949
+ state.requestsSinceCapabilityRefresh = 0;
950
+ state.version++;
951
+ this.bindLocalCapability(state, ready);
952
+ return true;
953
+ }
954
+
955
+ private async runRequest(
956
+ state: ReplicationInfoV2ReceiveState,
957
+ ): Promise<void> {
958
+ if (state.requestInFlight) {
959
+ return;
960
+ }
961
+ if (!this.isStateCurrent(state)) {
962
+ return;
963
+ }
964
+ if (
965
+ state.phase === "active" ||
966
+ state.requestParked ||
967
+ state.lastSequence === MAX_U64
968
+ ) {
969
+ return;
970
+ }
971
+ if (state.requestAttempts >= this.requestMaxAttempts) {
972
+ state.requestParked = true;
973
+ return;
974
+ }
975
+
976
+ let operation: Promise<void>;
977
+ operation = (async () => {
978
+ if (state.capabilityRefreshRequired) {
979
+ state.requestAttempts++;
980
+ if (!(await this.refreshGrant(state))) {
981
+ return;
982
+ }
983
+ }
984
+ if (!this.isStateCurrent(state)) {
985
+ return;
986
+ }
987
+ const ready = this._localCapabilityReadyBySession.get(state.peerSession);
988
+ if (
989
+ !ready ||
990
+ ready.peerHash !== state.peerHash ||
991
+ ready.receiverTransportSession !== state.receiverTransportSession
992
+ ) {
993
+ return;
994
+ }
995
+ const now = this.now();
996
+ if (now <= ready.requestNotBeforeMs) {
997
+ this.armRequest(state, ready.requestNotBeforeMs - now + 1);
998
+ return;
999
+ }
1000
+ if (state.requestAttempts >= this.requestMaxAttempts) {
1001
+ state.requestParked = true;
1002
+ return;
1003
+ }
1004
+ state.requestAttempts++;
1005
+ state.requestsSinceCapabilityRefresh++;
1006
+ const request = new RequestReplicationInfoV2Message({
1007
+ receiverChallenge: state.receiverRequestChallenge.slice(),
1008
+ intendedSender: state.target,
1009
+ senderSession: state.senderTransportSession,
1010
+ });
1011
+ await this.deps.sendRequest(
1012
+ request,
1013
+ state.target,
1014
+ state.controller.signal,
1015
+ );
1016
+ })()
1017
+ .catch((error) => {
1018
+ if (!state.controller.signal.aborted && !this.deps.isClosed()) {
1019
+ this.deps.onRequestError?.(error);
1020
+ }
1021
+ })
1022
+ .finally(() => {
1023
+ if (state.requestInFlight === operation) {
1024
+ state.requestInFlight = undefined;
1025
+ }
1026
+ if (
1027
+ this._receiveStates.get(state.peerHash) === state &&
1028
+ !state.controller.signal.aborted &&
1029
+ !state.requestTimer &&
1030
+ state.phase !== "active"
1031
+ ) {
1032
+ if (state.requestsSinceCapabilityRefresh >= 3) {
1033
+ state.capabilityRefreshRequired = true;
1034
+ }
1035
+ if (state.requestAttempts >= this.requestMaxAttempts) {
1036
+ state.requestParked = true;
1037
+ } else {
1038
+ this.armRequest(state, this.requestRetryDelay(state));
1039
+ }
1040
+ }
1041
+ });
1042
+ state.requestInFlight = operation;
1043
+ await operation;
1044
+ }
1045
+ }