@941design/marmot 0.0.0-stage → 0.2.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +739 -0
- package/LICENSE +21 -0
- package/README.md +33 -2
- package/dist/account/admin.d.ts +72 -0
- package/dist/account/admin.js +209 -0
- package/dist/account/device-deps.d.ts +379 -0
- package/dist/account/device-deps.js +36 -0
- package/dist/account/device-lineage.d.ts +199 -0
- package/dist/account/device-lineage.js +390 -0
- package/dist/account/device-registry.d.ts +93 -0
- package/dist/account/device-registry.js +1290 -0
- package/dist/account/device-rotation.d.ts +61 -0
- package/dist/account/device-rotation.js +827 -0
- package/dist/account/enrolment.d.ts +339 -0
- package/dist/account/enrolment.js +1247 -0
- package/dist/account/identity-proof.d.ts +79 -0
- package/dist/account/identity-proof.js +264 -0
- package/dist/account/index.d.ts +17 -0
- package/dist/account/index.js +16 -0
- package/dist/account/restore-rotation.d.ts +21 -0
- package/dist/account/restore-rotation.js +22 -0
- package/dist/account/same-account-authorization.d.ts +86 -0
- package/dist/account/same-account-authorization.js +180 -0
- package/dist/account/teardown.d.ts +190 -0
- package/dist/account/teardown.js +878 -0
- package/dist/client/admission-cleanup.d.ts +197 -0
- package/dist/client/admission-cleanup.js +635 -0
- package/dist/client/adoption-proof.d.ts +39 -0
- package/dist/client/adoption-proof.js +102 -0
- package/dist/client/before-create-group.d.ts +18 -0
- package/dist/client/before-create-group.js +65 -0
- package/dist/client/before-publish-hooks.d.ts +121 -0
- package/dist/client/before-publish-hooks.js +182 -0
- package/dist/client/client-config.d.ts +42 -0
- package/dist/client/client-config.js +13 -0
- package/dist/client/commit-redrive.d.ts +92 -0
- package/dist/client/commit-redrive.js +274 -0
- package/dist/client/component-update.d.ts +43 -0
- package/dist/client/component-update.js +53 -0
- package/dist/client/confirm-hooks.d.ts +55 -0
- package/dist/client/confirm-hooks.js +74 -0
- package/dist/client/create-client.d.ts +1126 -0
- package/dist/client/create-client.js +1232 -0
- package/dist/client/group-availability.d.ts +182 -0
- package/dist/client/group-availability.js +301 -0
- package/dist/client/group-disband.d.ts +28 -0
- package/dist/client/group-disband.js +64 -0
- package/dist/client/group-history.d.ts +461 -0
- package/dist/client/group-history.js +1365 -0
- package/dist/client/group-profile.d.ts +31 -0
- package/dist/client/group-profile.js +62 -0
- package/dist/client/group-records.d.ts +522 -0
- package/dist/client/group-records.js +1117 -0
- package/dist/client/group-removal.d.ts +36 -0
- package/dist/client/group-removal.js +127 -0
- package/dist/client/group-remove-member.d.ts +49 -0
- package/dist/client/group-remove-member.js +163 -0
- package/dist/client/group-runtime.d.ts +415 -0
- package/dist/client/group-runtime.js +2811 -0
- package/dist/client/group-snapshot.d.ts +104 -0
- package/dist/client/group-snapshot.js +474 -0
- package/dist/client/history-pages.d.ts +107 -0
- package/dist/client/history-pages.js +194 -0
- package/dist/client/inbox-send.d.ts +19 -0
- package/dist/client/inbox-send.js +71 -0
- package/dist/client/index.d.ts +24 -0
- package/dist/client/index.js +13 -0
- package/dist/client/ingest.d.ts +82 -0
- package/dist/client/ingest.js +1584 -0
- package/dist/client/invitation-outcomes.d.ts +50 -0
- package/dist/client/invitation-outcomes.js +135 -0
- package/dist/client/lifecycle-events.d.ts +63 -0
- package/dist/client/lifecycle-events.js +35 -0
- package/dist/client/opaque-candidates.d.ts +82 -0
- package/dist/client/opaque-candidates.js +170 -0
- package/dist/client/own-change-outbox.d.ts +66 -0
- package/dist/client/own-change-outbox.js +158 -0
- package/dist/client/pending-leaf.d.ts +116 -0
- package/dist/client/pending-leaf.js +378 -0
- package/dist/client/permissions.d.ts +9 -0
- package/dist/client/permissions.js +12 -0
- package/dist/client/pipeline.d.ts +583 -0
- package/dist/client/pipeline.js +3666 -0
- package/dist/client/reentrancy-guard.d.ts +102 -0
- package/dist/client/reentrancy-guard.js +159 -0
- package/dist/client/rekey.d.ts +58 -0
- package/dist/client/rekey.js +149 -0
- package/dist/client/transport-policy.d.ts +181 -0
- package/dist/client/transport-policy.js +214 -0
- package/dist/history/admission-lineage.d.ts +186 -0
- package/dist/history/admission-lineage.js +589 -0
- package/dist/history/equivocation.d.ts +55 -0
- package/dist/history/equivocation.js +113 -0
- package/dist/history/fencing.d.ts +66 -0
- package/dist/history/fencing.js +121 -0
- package/dist/history/hex.d.ts +3 -0
- package/dist/history/hex.js +10 -0
- package/dist/history/index.d.ts +74 -0
- package/dist/history/index.js +32 -0
- package/dist/history/notifications.d.ts +270 -0
- package/dist/history/notifications.js +264 -0
- package/dist/history/ordering-key.d.ts +24 -0
- package/dist/history/ordering-key.js +101 -0
- package/dist/history/own-change-fork.d.ts +112 -0
- package/dist/history/own-change-fork.js +196 -0
- package/dist/history/own-change.d.ts +118 -0
- package/dist/history/own-change.js +146 -0
- package/dist/history/policy.d.ts +48 -0
- package/dist/history/policy.js +52 -0
- package/dist/history/retained-window.d.ts +215 -0
- package/dist/history/retained-window.js +565 -0
- package/dist/inbox/demux.d.ts +90 -0
- package/dist/inbox/demux.js +78 -0
- package/dist/inbox/handler-registry.d.ts +42 -0
- package/dist/inbox/handler-registry.js +37 -0
- package/dist/inbox/index.d.ts +11 -0
- package/dist/inbox/index.js +10 -0
- package/dist/inbox/seen-set.d.ts +82 -0
- package/dist/inbox/seen-set.js +202 -0
- package/dist/inbox/subscription.d.ts +236 -0
- package/dist/inbox/subscription.js +644 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +9 -0
- package/dist/invitations/competing-welcome.d.ts +223 -0
- package/dist/invitations/competing-welcome.js +128 -0
- package/dist/invitations/decline.d.ts +67 -0
- package/dist/invitations/decline.js +149 -0
- package/dist/invitations/delivery-evidence.d.ts +288 -0
- package/dist/invitations/delivery-evidence.js +766 -0
- package/dist/invitations/expect-welcome.d.ts +66 -0
- package/dist/invitations/expect-welcome.js +138 -0
- package/dist/invitations/index.d.ts +18 -0
- package/dist/invitations/index.js +18 -0
- package/dist/invitations/invitation-outcome.d.ts +110 -0
- package/dist/invitations/invitation-outcome.js +210 -0
- package/dist/invitations/invite.d.ts +358 -0
- package/dist/invitations/invite.js +1102 -0
- package/dist/invitations/offers.d.ts +79 -0
- package/dist/invitations/offers.js +115 -0
- package/dist/invitations/on-proposal.d.ts +47 -0
- package/dist/invitations/on-proposal.js +40 -0
- package/dist/invitations/pending-proposal-policy.d.ts +52 -0
- package/dist/invitations/pending-proposal-policy.js +139 -0
- package/dist/invitations/proposal-deadline.d.ts +133 -0
- package/dist/invitations/proposal-deadline.js +164 -0
- package/dist/invitations/relay-planes.d.ts +43 -0
- package/dist/invitations/relay-planes.js +70 -0
- package/dist/invitations/welcome-processor.d.ts +272 -0
- package/dist/invitations/welcome-processor.js +1241 -0
- package/dist/key-packages/device-key.d.ts +131 -0
- package/dist/key-packages/device-key.js +272 -0
- package/dist/key-packages/device-rotation-records.d.ts +83 -0
- package/dist/key-packages/device-rotation-records.js +173 -0
- package/dist/key-packages/group-refs.d.ts +40 -0
- package/dist/key-packages/group-refs.js +79 -0
- package/dist/key-packages/index.d.ts +18 -0
- package/dist/key-packages/index.js +18 -0
- package/dist/key-packages/key-package-ref.d.ts +16 -0
- package/dist/key-packages/key-package-ref.js +21 -0
- package/dist/key-packages/observation-stream.d.ts +4 -0
- package/dist/key-packages/observation-stream.js +120 -0
- package/dist/key-packages/offer-rumor.d.ts +26 -0
- package/dist/key-packages/offer-rumor.js +69 -0
- package/dist/key-packages/publisher.d.ts +174 -0
- package/dist/key-packages/publisher.js +1247 -0
- package/dist/key-packages/select-key-package.d.ts +24 -0
- package/dist/key-packages/select-key-package.js +90 -0
- package/dist/key-packages/slot-manager.d.ts +167 -0
- package/dist/key-packages/slot-manager.js +327 -0
- package/dist/key-packages/validate-key-package-event.d.ts +3 -0
- package/dist/key-packages/validate-key-package-event.js +49 -0
- package/dist/media/cache.d.ts +14 -0
- package/dist/media/cache.js +10 -0
- package/dist/media/index.d.ts +52 -0
- package/dist/media/index.js +71 -0
- package/dist/mls.d.ts +15 -0
- package/dist/mls.js +15 -0
- package/dist/shared/account-pubkey.d.ts +4 -0
- package/dist/shared/account-pubkey.js +10 -0
- package/dist/shared/errors.d.ts +28 -0
- package/dist/shared/errors.js +96 -0
- package/dist/shared/hex.d.ts +4 -0
- package/dist/shared/hex.js +13 -0
- package/dist/shared/index.d.ts +14 -0
- package/dist/shared/index.js +14 -0
- package/dist/shared/kinds.d.ts +8 -0
- package/dist/shared/kinds.js +28 -0
- package/dist/shared/publish-classification.d.ts +26 -0
- package/dist/shared/publish-classification.js +31 -0
- package/dist/storage/envelope.d.ts +39 -0
- package/dist/storage/envelope.js +201 -0
- package/dist/storage/index.d.ts +237 -0
- package/dist/storage/index.js +491 -0
- package/dist/storage/indexeddb/idb-types.d.ts +68 -0
- package/dist/storage/indexeddb/idb-types.js +1 -0
- package/dist/storage/indexeddb/index.d.ts +17 -0
- package/dist/storage/indexeddb/index.js +141 -0
- package/dist/storage/lease.d.ts +54 -0
- package/dist/storage/lease.js +168 -0
- package/dist/storage/memory/deterministic-signer.d.ts +27 -0
- package/dist/storage/memory/deterministic-signer.js +65 -0
- package/dist/storage/memory/index.d.ts +32 -0
- package/dist/storage/memory/index.js +87 -0
- package/dist/storage/memory/lock-manager.d.ts +8 -0
- package/dist/storage/memory/lock-manager.js +71 -0
- package/package.json +66 -3
|
@@ -0,0 +1,2811 @@
|
|
|
1
|
+
import { createSimpleGroup, decryptGroupMessages, createGroupEvent, getGroupIdHex, getNostrGroupIdHex, getMarmotGroupView, getGroupMemberPubkeys, createCredential, generateKeyPackage, getPubkeyLeafNodes, getPubkeyLeafNodeIndexes, deserializeClientState, serializeClientState, } from '@941design/marmot-engine/core';
|
|
2
|
+
import { isAdminAccountLeaver, MarmotGroupEngine, OpaqueRecordInvalid, nextOwnApplicationGeneration } from '@941design/marmot-engine/engine';
|
|
3
|
+
import { ciphersuites, defaultCryptoProvider, getCiphersuiteImpl, nodeTypes, selfRemoveProposalType, } from '@941design/marmot-engine/mls';
|
|
4
|
+
import { accountLeafIndexes, activeAdmins, enforceStateProofs, isActiveAdmin, isLastActiveAdmin, RESTORE_ROTATION_EXTENSION, writeRestoreRotationOwed, } from '../account/index.js';
|
|
5
|
+
import { ownLeafOf } from './rekey.js';
|
|
6
|
+
import { DEFAULT_CIPHERSUITE_NAME, deviceProofSigner, ensureDeviceSigningKey, readDeviceSigningKey, withDeviceSignatureKey } from '../key-packages/index.js';
|
|
7
|
+
import { carriesBranch, judgeCompetingWelcome, toWelcomeOutcome } from '../invitations/index.js';
|
|
8
|
+
import { hexOfBytes, MarmotError, isMarmotError } from '../shared/index.js';
|
|
9
|
+
import { ADMISSION_LINEAGE_EXTENSION, canonicalPath, replaceLineageRecords, successionsInWindow, validateRestoredLineage, EQUIVOCATION_EXTENSION, FENCE_EXTENSION, fencedError, heldBranchScore, InvalidStoredEvidenceError, loadFenceMarker, readEquivocationNotifications, readWindow, recordedFencingError, restoreHistory, REWIND_HORIZON, tagOf, tipOnlyWindow, windowFor, writeWindow, } from '../history/index.js';
|
|
10
|
+
import { belongsTo, clearRemoval, readRemoval, REMOVAL_EXTENSION, settlesAt } from './group-removal.js';
|
|
11
|
+
import { beginDisposeAvailability, closeAvailability, createClientAvailability, beginLeaseEndAvailability, observeAvailability, publishAvailability, } from './group-availability.js';
|
|
12
|
+
import { readAdoptionProofOf, readAdoptionProofs, SWITCH_PROOF_EXTENSION, writeAdoptionProof } from './adoption-proof.js';
|
|
13
|
+
import { removeAdmissionIntent } from './admission-cleanup.js';
|
|
14
|
+
import { clearOpaque, isEmptyOpaque, OPAQUE_EXTENSION, OpaqueRecordCorrupt, readOpaque } from './opaque-candidates.js';
|
|
15
|
+
import { assertNotInHistoryBackend, awaitHistoryCallSettled, boundedHistoryCall, issueMutation, clearSettlementRecheck, decryptMediaOp, deliverTransition, reconcileInvitesWithWindow, encryptMediaOp, planTransition, recordTransition, scheduleHistoryDrain, WITHDRAWN_DISPOSITION, stopHistoryDrain, historyOf, readSettlementRecheckRecord, SETTLEMENT_RECHECK_EXTENSION, subscribeNotifications, rejectSyncWaiters, syncedLeaseLostError, accountLeaseEndedError } from './group-history.js';
|
|
16
|
+
import { clearRestoringRecord, GROUP_INDEX_EXTENSION, deletePendingIntentRecord, hasGroupState, listGroupIndex, listRestoringRecords, listPendingIntentRecords, listPendingIntentSummaries, listQueuedRumorRecords, RATCHET_SKIP_EXTENSION, ratchetSkipArmed, readGroupIndexEntry, readGroupState, readIngestCursor, readRatchetSkipMarker, readRestorePending, RESTORE_PENDING_EXTENSION, RESTORING_EXTENSION, writeGroupIndexEntry, writeGroupState, writeIngestCursor, writeRestorePending, writeTerminalLedgerEntry, writeRestoringRecord, } from './group-records.js';
|
|
17
|
+
import { DISBAND_EXTENSION, disbandedError, groupDisbandedOf, readDisband } from './group-disband.js';
|
|
18
|
+
import { drainConvergence, ingestEvents, requestHistoryResync, scheduleRemovalSettle, restartSyncOnRoutingChange, startGroupSync, stopGroupSync, treeFedPass } from './ingest.js';
|
|
19
|
+
import { clearPendingCommit, clearPendingLeaf, PENDING_COMMIT_EXTENSION, PENDING_LEAF_EXTENSION, pendingWatch, readPendingRestore, resolvePendingCommit, writePendingCommit, } from './pending-leaf.js';
|
|
20
|
+
import { ACCOUNT_SCOPE } from './reentrancy-guard.js';
|
|
21
|
+
import { advanceQueue, backoffMs, inviteIntent, proposeSelfRemoveIntent, removeLeafIntent, electionAtRest, removeMemberIntent, renameIntent, rekeyIntent, resumePendingInBackground, scheduleAwaitingFenceCleanup, scheduleDeferredReenqueueRetry, scheduleProposalDeadlineCheck, scheduleRekeyFollowUp, scheduleSelfRemoveCommit, notifySettled, ownMessage, scheduleWithdrawnRedrives, sendIntent, setAdminsIntent, } from './pipeline.js';
|
|
22
|
+
import { createHistorySilenceTracker, CURSOR_FUTURE_SKEW_S, CURSOR_REDELIVERY_MARGIN_S } from './transport-policy.js';
|
|
23
|
+
import { runConfirmHooks } from './confirm-hooks.js';
|
|
24
|
+
/** An error thrown by an `onConfirm` hook itself (as opposed to the store or the guard around it). */
|
|
25
|
+
class HookRefusal extends Error {
|
|
26
|
+
error;
|
|
27
|
+
constructor(error) {
|
|
28
|
+
super('an onConfirm hook refused');
|
|
29
|
+
this.error = error;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
import { authenticateWindowSenders, openGroupSnapshot, snapshotHistory } from './group-snapshot.js';
|
|
33
|
+
import { engineSeams, MARMOT_CONVERGENCE_POLICY } from './client-config.js';
|
|
34
|
+
import { MARMOT_HIDDEN_CREDIT_POLICY } from '../history/policy.js';
|
|
35
|
+
const CIPHERSUITE_NAME_BY_ID = new Map(Object.entries(ciphersuites).map(([name, id]) => [id, name]));
|
|
36
|
+
function ciphersuiteNameFor(id) {
|
|
37
|
+
const name = CIPHERSUITE_NAME_BY_ID.get(id);
|
|
38
|
+
if (name === undefined)
|
|
39
|
+
throw new MarmotError('store-failed', `unknown MLS ciphersuite id ${id}`, { details: { ciphersuite: id, malformed: true } });
|
|
40
|
+
return name;
|
|
41
|
+
}
|
|
42
|
+
function hex(bytes) {
|
|
43
|
+
return Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('');
|
|
44
|
+
}
|
|
45
|
+
/** Every leaf of `account` in `state`: its leaf index and signature key (hex). */
|
|
46
|
+
export function leafInfosOf(state, account) {
|
|
47
|
+
const indexes = getPubkeyLeafNodeIndexes(state, account);
|
|
48
|
+
const nodes = getPubkeyLeafNodes(state, account);
|
|
49
|
+
return indexes.map((leafIndex, i) => Object.freeze({ leafIndex, leafKey: hex(nodes[i].signaturePublicKey) }));
|
|
50
|
+
}
|
|
51
|
+
// ---------------------------------------------------------------------------
|
|
52
|
+
// GroupRuntime — the type `pipeline.ts` and `ingest.ts` operate on.
|
|
53
|
+
// ---------------------------------------------------------------------------
|
|
54
|
+
class SerialQueue {
|
|
55
|
+
#tail = Promise.resolve();
|
|
56
|
+
run(task) {
|
|
57
|
+
const result = this.#tail.then(task, task);
|
|
58
|
+
this.#tail = result.catch(() => undefined);
|
|
59
|
+
return result;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
async function ciphersuiteImplFor(cache, suiteId) {
|
|
63
|
+
let pending = cache.get(suiteId);
|
|
64
|
+
if (pending === undefined) {
|
|
65
|
+
pending = getCiphersuiteImpl(ciphersuiteNameFor(suiteId), defaultCryptoProvider);
|
|
66
|
+
cache.set(suiteId, pending);
|
|
67
|
+
}
|
|
68
|
+
return pending;
|
|
69
|
+
}
|
|
70
|
+
/** The seams a client's engines are built with: marmot's, as the (internal, test-only) override rewrites them. */
|
|
71
|
+
function effectiveSeams(ctx, ratchetTree) {
|
|
72
|
+
const marmotSeams = engineSeams(ratchetTree);
|
|
73
|
+
return ctx.engineSeamsOverride === undefined ? marmotSeams : ctx.engineSeamsOverride(marmotSeams);
|
|
74
|
+
}
|
|
75
|
+
/** A fresh copy of a state: never hand one `ClientState` object to two owners (ts-mls mutates some in place). */
|
|
76
|
+
function copyOf(state) {
|
|
77
|
+
return deserializeClientState(serializeClientState(state));
|
|
78
|
+
}
|
|
79
|
+
/** The "leaver is an active admin" predicate this client's engines run (the engine default when the seam is cleared). */
|
|
80
|
+
function leaverPredicateOf(ctx, state) {
|
|
81
|
+
return effectiveSeams(ctx, () => state.ratchetTree).isActiveAdminLeaver ?? isAdminAccountLeaver;
|
|
82
|
+
}
|
|
83
|
+
async function buildEngine(ctx, ciphersuiteCache, state, onSettleCheck, history, removal, opaque) {
|
|
84
|
+
const impl = await ciphersuiteImplFor(ciphersuiteCache, state.groupContext.cipherSuite);
|
|
85
|
+
const peeler = {
|
|
86
|
+
async peelGroupMessages(envelopes, s) {
|
|
87
|
+
const { read, unreadable } = await decryptGroupMessages(envelopes, s, impl);
|
|
88
|
+
return { read: read.map(({ event, message }) => ({ envelope: event, message })), unreadable };
|
|
89
|
+
},
|
|
90
|
+
wrapGroupMessage: (message, s) => createGroupEvent({ message, state: s, ciphersuite: impl }),
|
|
91
|
+
idOf: (e) => e.id,
|
|
92
|
+
};
|
|
93
|
+
// S8/S9: the marmot seams (client-config.ts) — ADR-012's proof gate around the same-account and
|
|
94
|
+
// admin receive rules, the admin + same-account `authorizeCommit` over this engine's current tree,
|
|
95
|
+
// the admin-leaver auto-commit predicate, and the signature-key-only equality `clientConfig`. A
|
|
96
|
+
// seam an (internal, test-only) override clears is the engine's own default.
|
|
97
|
+
let engine;
|
|
98
|
+
const seams = effectiveSeams(ctx, () => (engine === undefined ? state.ratchetTree : engine.state.ratchetTree));
|
|
99
|
+
engine = new MarmotGroupEngine({
|
|
100
|
+
state,
|
|
101
|
+
ciphersuite: impl,
|
|
102
|
+
peeler,
|
|
103
|
+
...(seams.adminPolicyCallback === undefined ? {} : { adminPolicyCallback: seams.adminPolicyCallback }),
|
|
104
|
+
...(seams.authorizeCommit === undefined ? {} : { authorizeCommit: seams.authorizeCommit }),
|
|
105
|
+
...(seams.isActiveAdminLeaver === undefined ? {} : { isActiveAdminLeaver: seams.isActiveAdminLeaver }),
|
|
106
|
+
clientConfig: seams.clientConfig,
|
|
107
|
+
// S10: structural branch selection only — see MARMOT_CONVERGENCE_POLICY.
|
|
108
|
+
convergencePolicy: MARMOT_CONVERGENCE_POLICY,
|
|
109
|
+
scheduler: {
|
|
110
|
+
setTimer: (ms, cb) => ctx.timers.set(cb, ms),
|
|
111
|
+
clearTimer: (handle) => ctx.timers.clear(handle),
|
|
112
|
+
},
|
|
113
|
+
now: () => ctx.clock.now(),
|
|
114
|
+
// The engine retains input that arrives after a convergence deadline and releases it only through
|
|
115
|
+
// driveConvergence(); without this continuation that input would never be processed.
|
|
116
|
+
onSettleCheck,
|
|
117
|
+
// S10: the persisted rewind window — without it a rebuilt engine has no retained root below its tip, and a
|
|
118
|
+
// sibling of an already applied commit is skipped as beyond-anchor instead of converging (same-epoch fork).
|
|
119
|
+
...(history === undefined ? {} : { retained: history.retained, historyTree: history.historyTree }),
|
|
120
|
+
// S20: a removal of the local leaf is a fork-recovery candidate like any branch, and stays revisable (its
|
|
121
|
+
// input fork material, never self-evicted) until the client settles it (client/group-removal.ts).
|
|
122
|
+
removalConvergence: true,
|
|
123
|
+
// ADR-016: at a node where one leaf authored two child commits (a restored stale leaf snapshot), only depth
|
|
124
|
+
// built by other leaves that were members there counts; ties keep the structural ordering above.
|
|
125
|
+
equivocationCredit: true,
|
|
126
|
+
// ADR-016 D6 (Accepted — in force; owner sign-off OQ-2 pending): a commit by this device's own leaf that it cannot
|
|
127
|
+
// open (a restored or cloned snapshot) is an opaque candidate, scored at its best case against the processable
|
|
128
|
+
// branches; the device fences only when that branch could win (client/opaque-candidates.ts persists the held
|
|
129
|
+
// candidates). The ONE opt-in site: inert unless `equivocationCredit` is on. The policy is one constant
|
|
130
|
+
// (history/policy.ts, OQ-1).
|
|
131
|
+
branchAwareFence: true,
|
|
132
|
+
hiddenCreditPolicy: MARMOT_HIDDEN_CREDIT_POLICY,
|
|
133
|
+
// ADR-017 (epic marmot-removal-finality): a removal is decisive at the node where it races a branch that keeps the
|
|
134
|
+
// removed leaf — that leaf's later commits there earn nothing, and a tie prefers the removal — within the rewind
|
|
135
|
+
// horizon of every candidate through the node. Inert unless `equivocationCredit` is on; the ONE opt-in site.
|
|
136
|
+
removalFinality: true,
|
|
137
|
+
// Internal, tests only: extra engine options (a test chooses the other hidden-credit policy here; never the
|
|
138
|
+
// opt-in, which is the literal above). Before `removal` and `opaque`, which are never overridden.
|
|
139
|
+
...(ctx.engineOptionsForTest ?? {}),
|
|
140
|
+
...(removal === undefined ? {} : { removal }),
|
|
141
|
+
// The held opaque candidates and `U` (client/opaque-candidates.ts): a rebuilt engine decides like the one it
|
|
142
|
+
// replaces. The engine ignores the record unless its opaque seam is on, so this is inert while the seam is off.
|
|
143
|
+
...(opaque === undefined ? {} : { opaque }),
|
|
144
|
+
});
|
|
145
|
+
return engine;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* {@link buildEngine} over a persisted opaque record, FAILING CLOSED (client/opaque-candidates.ts): a record the
|
|
149
|
+
* engine refuses (it throws `OpaqueRecordInvalid`, and only that, for one of another history, e.g. a candidate whose epoch disagrees with the
|
|
150
|
+
* node it names) is corrupt. The engine is then built WITHOUT it and `corrupt` carries the verdict the caller turns
|
|
151
|
+
* into a fence — never an engine that silently forgot a held candidate while the group goes on processing.
|
|
152
|
+
*/
|
|
153
|
+
async function buildEngineFailClosed(ctx, ciphersuiteCache, state, onSettleCheck, history, removal, opaque) {
|
|
154
|
+
try {
|
|
155
|
+
return { engine: await buildEngine(ctx, ciphersuiteCache, state, onSettleCheck, history, removal, opaque) };
|
|
156
|
+
}
|
|
157
|
+
catch (error) {
|
|
158
|
+
if (opaque === undefined || !(error instanceof OpaqueRecordInvalid))
|
|
159
|
+
throw error;
|
|
160
|
+
return { engine: await buildEngine(ctx, ciphersuiteCache, state, onSettleCheck, history, removal), corrupt: new OpaqueRecordCorrupt(error.message) };
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
/** The stored opaque record: `undefined` when none is held; a corrupt one is reported, never thrown (the caller fences). */
|
|
164
|
+
async function loadOpaque(guard, groupId, groupStore) {
|
|
165
|
+
try {
|
|
166
|
+
const record = await guard.call(groupId, 'store', () => readOpaque(groupStore.extension(OPAQUE_EXTENSION)));
|
|
167
|
+
return { record: isEmptyOpaque(record) ? undefined : record };
|
|
168
|
+
}
|
|
169
|
+
catch (error) {
|
|
170
|
+
if (error instanceof OpaqueRecordCorrupt)
|
|
171
|
+
return { record: undefined, corrupt: error };
|
|
172
|
+
// A payload that does not decode (truncated bytes) is the same corruption as a malformed record: fail closed, never
|
|
173
|
+
// a failed load. A stamp of another build (`version`) stays what it was: the store's own incompatibility path.
|
|
174
|
+
if (isMarmotError(error, 'envelope-mismatch') && error.details?.reason === 'malformed')
|
|
175
|
+
return { record: undefined, corrupt: new OpaqueRecordCorrupt(`stored payload does not decode (${error.message})`) };
|
|
176
|
+
throw error;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
export function createGroupRegistry(ctx, guard, invitations, teardown) {
|
|
180
|
+
const runtimes = new Map();
|
|
181
|
+
/** Epic marmot-invitation-outcomes S1: this client session's fences and lease-end flag (client/group-availability.ts). */
|
|
182
|
+
const availabilityClient = createClientAvailability(() => runtimes.values());
|
|
183
|
+
/** The recorded-but-undelivered `ended/'disposed'` deliveries ({@link GroupRegistry.beginDispose}, then `dispose`). */
|
|
184
|
+
const disposeAvailabilityDeliveries = [];
|
|
185
|
+
// Stryker disable next-line all: test seam (InternalClientOptions.exposeRuntimesForTest)
|
|
186
|
+
ctx.exposeRuntimesForTest?.((groupId) => runtimes.get(groupId));
|
|
187
|
+
const loading = new Map();
|
|
188
|
+
/** S9: groups purged by a teardown in this client's lifetime — never loaded again (unless re-joined). */
|
|
189
|
+
const abandoned = new Set();
|
|
190
|
+
/** S9: listeners of each group's exposed-state changes (the teardown driver's wake-up). */
|
|
191
|
+
const stateWatchers = new Map();
|
|
192
|
+
/** S10 (V1): the device port's rewind listeners (`DeviceGroupPort.onRewind`). */
|
|
193
|
+
const rewindListeners = new Set();
|
|
194
|
+
/** Epic marmot-device-key-rotation (S6): the device port's rotation listeners (`DeviceGroupPort.onRotation`). */
|
|
195
|
+
const rotationListeners = new Set();
|
|
196
|
+
/** Owner decision 13: the device port's restore listeners (`DeviceGroupPort.onRestored`). */
|
|
197
|
+
const restoredListeners = new Set();
|
|
198
|
+
function announceTo(listeners, groupId) {
|
|
199
|
+
for (const listener of [...listeners]) {
|
|
200
|
+
// A later macrotask: outside the group's queue (the listener may run group operations) and every frame.
|
|
201
|
+
ctx.timers.set(() => {
|
|
202
|
+
if (ctx.isDisposed())
|
|
203
|
+
return;
|
|
204
|
+
guard.detached(() => {
|
|
205
|
+
try {
|
|
206
|
+
listener(groupId);
|
|
207
|
+
}
|
|
208
|
+
catch {
|
|
209
|
+
// A listener never breaks the operation that persisted the transition.
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
}, 0);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
function announceRewind(groupId) {
|
|
216
|
+
announceTo(rewindListeners, groupId);
|
|
217
|
+
}
|
|
218
|
+
function announceRotation(groupId) {
|
|
219
|
+
announceTo(rotationListeners, groupId);
|
|
220
|
+
}
|
|
221
|
+
function notifyState(groupId) {
|
|
222
|
+
for (const listener of [...(stateWatchers.get(groupId) ?? [])]) {
|
|
223
|
+
try {
|
|
224
|
+
listener();
|
|
225
|
+
}
|
|
226
|
+
catch {
|
|
227
|
+
// A watcher never breaks the operation that changed the state.
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
/** Listens to `groupId`'s exposed-state changes (see {@link notifyState}); returns the unregister function. */
|
|
232
|
+
function watchState(groupId, listener) {
|
|
233
|
+
let set = stateWatchers.get(groupId);
|
|
234
|
+
if (set === undefined)
|
|
235
|
+
stateWatchers.set(groupId, (set = new Set()));
|
|
236
|
+
set.add(listener);
|
|
237
|
+
return () => {
|
|
238
|
+
const current = stateWatchers.get(groupId);
|
|
239
|
+
current?.delete(listener);
|
|
240
|
+
if (current?.size === 0)
|
|
241
|
+
stateWatchers.delete(groupId);
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
/** Fires (once) and clears the runtime's terminal watchers (GroupRuntime.terminalWatchers). Never throws. */
|
|
245
|
+
function endRuntime(rt, end) {
|
|
246
|
+
const watchers = [...rt.terminalWatchers];
|
|
247
|
+
rt.terminalWatchers.clear();
|
|
248
|
+
for (const watcher of watchers) {
|
|
249
|
+
try {
|
|
250
|
+
watcher(end);
|
|
251
|
+
}
|
|
252
|
+
catch {
|
|
253
|
+
// A watcher never breaks the operation that ended the runtime.
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
/** The engine's settle-check: releases convergence-retained input on the group's queue. */
|
|
258
|
+
function settleCheck(groupId) {
|
|
259
|
+
const rt = runtimes.get(groupId);
|
|
260
|
+
if (rt === undefined || rt.disposed || ctx.isDisposed())
|
|
261
|
+
return;
|
|
262
|
+
rt.run('converge', (rtc) => drainConvergence(rtc))
|
|
263
|
+
// The engine's quiescence window ended: a `competing` Welcome waiting on `syncing` is decided next (AC-FWJ-31).
|
|
264
|
+
.then(() => gateMayHaveCleared(groupId))
|
|
265
|
+
.catch(() => {
|
|
266
|
+
if (!rt.disposed && !ctx.isDisposed())
|
|
267
|
+
requestHistoryResync(rt);
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
const confirmHooks = [];
|
|
271
|
+
/** AC-MX-2: groups being restored — by `groups.restore`, or resumed after an earlier client left them unfinished. */
|
|
272
|
+
const restoring = new Map();
|
|
273
|
+
/**
|
|
274
|
+
* AC-MX-2: resolves when the client is disposed. A failed restore's take-back races it: the take-back queues
|
|
275
|
+
* behind the group's in-flight operation (which may never end: a publish to a relay that never answers), and a
|
|
276
|
+
* disposed client must not hold the restore — or its reservation — on that.
|
|
277
|
+
*/
|
|
278
|
+
let markClientDisposed;
|
|
279
|
+
const clientDisposed = new Promise((resolve) => (markClientDisposed = resolve));
|
|
280
|
+
/**
|
|
281
|
+
* AC-MX-2: reserves `groupId` for a restore (a `groups.restore` call, or a resumed unfinished one) — synchronously,
|
|
282
|
+
* so a second restore, a load or a Welcome adoption of the group from here on joins or refuses it.
|
|
283
|
+
*/
|
|
284
|
+
function reserveRestore(groupId) {
|
|
285
|
+
let resolveSettled;
|
|
286
|
+
let rejectEnded;
|
|
287
|
+
let resolveDone;
|
|
288
|
+
/** Rejects once the restore can no longer succeed (dispose, a purge; once the runtime exists, its terminal signal). */
|
|
289
|
+
const ended = new Promise((_resolve, reject) => (rejectEnded = reject));
|
|
290
|
+
ended.catch(() => undefined);
|
|
291
|
+
const end = (error) => {
|
|
292
|
+
entry.endError ??= error;
|
|
293
|
+
rejectEnded(error);
|
|
294
|
+
};
|
|
295
|
+
const settle = (rt) => {
|
|
296
|
+
if (rt === undefined)
|
|
297
|
+
entry.ending = true;
|
|
298
|
+
else if (typeof rt.then === 'function')
|
|
299
|
+
void Promise.resolve(rt).then((v) => void (v === undefined && (entry.ending = true)), () => undefined);
|
|
300
|
+
resolveSettled(rt);
|
|
301
|
+
};
|
|
302
|
+
const entry = {
|
|
303
|
+
settled: new Promise((resolve) => (resolveSettled = resolve)),
|
|
304
|
+
done: new Promise((resolve) => (resolveDone = resolve)),
|
|
305
|
+
ending: false,
|
|
306
|
+
checkingPurge: false,
|
|
307
|
+
endError: undefined,
|
|
308
|
+
runtime: undefined,
|
|
309
|
+
abort: () => end(restoreEndError(groupId, { reason: 'disposed' })),
|
|
310
|
+
retire: () => end(restoreEndError(groupId, { reason: 'retired' })),
|
|
311
|
+
};
|
|
312
|
+
restoreDone.set(entry, resolveDone);
|
|
313
|
+
restoring.set(groupId, entry);
|
|
314
|
+
return { entry, settle, end, ended };
|
|
315
|
+
}
|
|
316
|
+
/** Each reservation's `done` resolver (called by {@link releaseRestore}). */
|
|
317
|
+
const restoreDone = new WeakMap();
|
|
318
|
+
/** Throws once `entry`'s restore was ended (a purge, dispose) — for the steps that do not race `ended`. */
|
|
319
|
+
function assertNotEnded(entry) {
|
|
320
|
+
if (entry.endError !== undefined)
|
|
321
|
+
throw entry.endError;
|
|
322
|
+
}
|
|
323
|
+
/** Purges of this registry in flight, by group (`teardownPort.purge`). */
|
|
324
|
+
const purgesInFlight = new Map();
|
|
325
|
+
/**
|
|
326
|
+
* Purges of this registry ever started, by group — bumped synchronously as a purge starts, so a check that awaits
|
|
327
|
+
* (the restore's watermark read) sees a purge that both started and finished meanwhile.
|
|
328
|
+
*/
|
|
329
|
+
const purgeGenerations = new Map();
|
|
330
|
+
/**
|
|
331
|
+
* AC-MX-2: ends `groupId`'s restore reservation (a purge: it rejects `not-a-member`) and waits until it was
|
|
332
|
+
* released — its runtime retired as the last operation on its queue, so nothing it runs writes the group after
|
|
333
|
+
* the purge; the adoption is not cleared by the restore (the purge does it). Repeated for a reservation taken
|
|
334
|
+
* meanwhile. A reservation in its pre-adoption purge check (`checkingPurge`) is left alone: it waits for this
|
|
335
|
+
* purge itself. Returns the runtime the ended restore built, if any.
|
|
336
|
+
*/
|
|
337
|
+
async function endRestoresForPurge(groupId) {
|
|
338
|
+
let built;
|
|
339
|
+
for (let entry = restoring.get(groupId); entry !== undefined && !entry.checkingPurge; entry = restoring.get(groupId)) {
|
|
340
|
+
entry.retire();
|
|
341
|
+
await entry.done;
|
|
342
|
+
built = entry.runtime ?? built;
|
|
343
|
+
if (restoring.get(groupId) === entry)
|
|
344
|
+
break;
|
|
345
|
+
}
|
|
346
|
+
return built;
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* Group stores this client already opened, by id: the unfinished-restore mark is read by listings and scans
|
|
350
|
+
* (AC-MX-2) before a load of the same group — each `groupFactory` yield is a store open (and an `onIncompatible`
|
|
351
|
+
* subscription), so the load reuses the one the read opened. Dropped when a purge clears the group.
|
|
352
|
+
*/
|
|
353
|
+
const openedStores = new Map();
|
|
354
|
+
/**
|
|
355
|
+
* AC-MX-2: groups this client left marked unfinished (a resume or take-back that failed before it could clear the
|
|
356
|
+
* mark, a mark the scan could not read) — the only ones a later `resumeUnfinishedRestores` call re-reads.
|
|
357
|
+
*/
|
|
358
|
+
const leftMarked = new Set();
|
|
359
|
+
/** AC-MX-2: restore records (group-records.ts `RESTORING_EXTENSION`) the scan could not settle — re-read likewise. */
|
|
360
|
+
const leftRecords = new Set();
|
|
361
|
+
/** Whether the creation-time scan read the whole group index; the scan in flight, if any. */
|
|
362
|
+
let indexScanned = false;
|
|
363
|
+
let resumeScan;
|
|
364
|
+
function groupStoreOf(groupId) {
|
|
365
|
+
let store = openedStores.get(groupId);
|
|
366
|
+
if (store === undefined)
|
|
367
|
+
openedStores.set(groupId, (store = ctx.stores.groupFactory(groupId)));
|
|
368
|
+
return store;
|
|
369
|
+
}
|
|
370
|
+
/** AC-MX-2: whether `groupId` carries the unfinished-restore mark (group-records.ts `RESTORE_PENDING_EXTENSION`). */
|
|
371
|
+
function restorePending(groupId, groupStore) {
|
|
372
|
+
return guard.call(groupId, 'store', () => readRestorePending(groupStore.extension(RESTORE_PENDING_EXTENSION)));
|
|
373
|
+
}
|
|
374
|
+
/**
|
|
375
|
+
* AC-MX-2: whether `groupId` is an unfinished restore an earlier client left (marked, not restoring here) — which
|
|
376
|
+
* is then resumed ({@link resumeRestore}). For the listings that skip a restoring group instead of waiting on it.
|
|
377
|
+
*/
|
|
378
|
+
async function unfinishedRestore(groupId) {
|
|
379
|
+
if (restoring.has(groupId))
|
|
380
|
+
return true;
|
|
381
|
+
const cached = runtimes.get(groupId);
|
|
382
|
+
if (cached !== undefined && !cached.disposed)
|
|
383
|
+
return false;
|
|
384
|
+
if (!(await restorePending(groupId, groupStoreOf(groupId))))
|
|
385
|
+
return false;
|
|
386
|
+
resumeRestore(groupId);
|
|
387
|
+
return true;
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* Takes a restore's adoption back: the reverse of the order the adoption wrote it in, with the account restore
|
|
391
|
+
* record (group-records.ts `RESTORING_EXTENSION`) as the durable cleanup marker until both deletions are done —
|
|
392
|
+
* three separate transactions, account → group → account (never nested: group → account is the lock order):
|
|
393
|
+
* the record (re-)written, the group's stores cleared, then its index entry and the record deleted together. Every
|
|
394
|
+
* crash or failure point stays discoverable: before the clear the group is still marked (the scan resumes it);
|
|
395
|
+
* after it the record names an empty group store, whose index entry the scan deletes ({@link settleRestoringRecord}).
|
|
396
|
+
* Never continued once the client is disposed (its stores may belong to a new client by then).
|
|
397
|
+
*/
|
|
398
|
+
async function clearAdoptedGroup(groupId, groupStore) {
|
|
399
|
+
const assertLive = () => {
|
|
400
|
+
if (ctx.isDisposed())
|
|
401
|
+
throw new MarmotError('disposed', 'groups.restore: client was disposed while taking a failed restore back');
|
|
402
|
+
};
|
|
403
|
+
await guard.call(ACCOUNT_SCOPE, 'store', () => ctx.stores.account.transaction((tx) => writeRestoringRecord(tx.extension(RESTORING_EXTENSION), groupId, ctx.clock.now())));
|
|
404
|
+
assertLive();
|
|
405
|
+
await guard.call(groupId, 'store', () => groupStore.clear());
|
|
406
|
+
assertLive();
|
|
407
|
+
await guard.call(ACCOUNT_SCOPE, 'store', () => ctx.stores.account.transaction(async (tx) => {
|
|
408
|
+
await tx.extension(GROUP_INDEX_EXTENSION).delete(groupId);
|
|
409
|
+
await clearRestoringRecord(tx.extension(RESTORING_EXTENSION), groupId);
|
|
410
|
+
}));
|
|
411
|
+
}
|
|
412
|
+
/**
|
|
413
|
+
* AC-MX-2: settles `groupId`'s restore record (group-records.ts `RESTORING_EXTENSION`) an adoption left — the
|
|
414
|
+
* startup scan's walk of the adoptions the group index may not name. A marked group is resumed like any
|
|
415
|
+
* unfinished restore ({@link resumeRestore}: one with no index entry never finished its adoption and is discarded,
|
|
416
|
+
* its group store wiped, then the record). An unmarked one holds nothing the adoption wrote (a crash before its
|
|
417
|
+
* group write, or an adoption refused `already-member`): only the record is dropped — never while this client
|
|
418
|
+
* restores the group itself (checked inside the account transaction its own record write queues behind). An
|
|
419
|
+
* unmarked group store that holds no state either (a take-back interrupted after its clear, `clearAdoptedGroup`)
|
|
420
|
+
* loses its index entry with the record: that entry names a group nothing holds. Decided inside a group
|
|
421
|
+
* transaction that awaits the account one (group → account, the lock order): a Welcome adoption writing the
|
|
422
|
+
* group's state meanwhile is serialized with it, so an entry a fresh state was written for is never deleted.
|
|
423
|
+
*/
|
|
424
|
+
async function settleRestoringRecord(groupId) {
|
|
425
|
+
if (await unfinishedRestore(groupId)) {
|
|
426
|
+
// A record a take-back wrote before its clear outlives the resume it led to: settled again once that released
|
|
427
|
+
// the group (held: only the record goes; discarded: the take-back deleted it already).
|
|
428
|
+
void restoring.get(groupId)?.done.then(() => void (ctx.isDisposed() || abandoned.has(groupId) || leftRecords.add(groupId)));
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
const groupStore = groupStoreOf(groupId);
|
|
432
|
+
await guard.call(groupId, 'store', () => groupStore.transaction(async (gtx) => {
|
|
433
|
+
const empty = !(await hasGroupState(gtx.artifact('clientState'))) && !(await readRestorePending(gtx.extension(RESTORE_PENDING_EXTENSION)));
|
|
434
|
+
await guard.call(ACCOUNT_SCOPE, 'store', () => ctx.stores.account.transaction(async (tx) => {
|
|
435
|
+
if (restoring.has(groupId))
|
|
436
|
+
return;
|
|
437
|
+
if (empty)
|
|
438
|
+
await tx.extension(GROUP_INDEX_EXTENSION).delete(groupId);
|
|
439
|
+
await clearRestoringRecord(tx.extension(RESTORING_EXTENSION), groupId);
|
|
440
|
+
}));
|
|
441
|
+
}));
|
|
442
|
+
}
|
|
443
|
+
/** AC-MX-2: restoring groups a Welcome adoption was deferred for — their release asks for an inbox pass. */
|
|
444
|
+
const deferredAdoptions = new Set();
|
|
445
|
+
let restoreReleased;
|
|
446
|
+
/** Ends `groupId`'s restore reservation `entry` (never a later one); a Welcome adoption it deferred is retried by an inbox pass at once. */
|
|
447
|
+
function releaseRestore(groupId, entry) {
|
|
448
|
+
restoreDone.get(entry)?.();
|
|
449
|
+
if (restoring.get(groupId) !== entry)
|
|
450
|
+
return;
|
|
451
|
+
restoring.delete(groupId);
|
|
452
|
+
// A `competing` Welcome waiting for the restore to end is decided by the next inbox pass (AC-FWJ-31).
|
|
453
|
+
gateMayHaveCleared(groupId);
|
|
454
|
+
if (!deferredAdoptions.delete(groupId) || ctx.isDisposed())
|
|
455
|
+
return;
|
|
456
|
+
const listener = restoreReleased;
|
|
457
|
+
if (listener !== undefined)
|
|
458
|
+
guard.detached(() => listener(groupId));
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* AC-MX-2: a Welcome adoption of a group being restored is deferred, never waited out — it runs on the inbox
|
|
462
|
+
* queue, and a restore stuck on unreachable group relays would freeze every other wrap behind it. Transient
|
|
463
|
+
* (`retryable`, never counted toward the maintenance failure cap): the invitation stays `joining` and the
|
|
464
|
+
* restore's release asks for the inbox pass that completes it.
|
|
465
|
+
*/
|
|
466
|
+
function restoreInFlight(groupId) {
|
|
467
|
+
deferredAdoptions.add(groupId);
|
|
468
|
+
return new MarmotError('commit-pending', `invitations: group ${groupId} is being restored; its Welcome is joined once the restore settled`, {
|
|
469
|
+
details: { groupId, reason: 'restore-in-flight', retryable: true },
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
const ciphersuiteCache = new Map();
|
|
473
|
+
/**
|
|
474
|
+
* `MarmotGroup.invite`'s queue-and-deliver body, shared with `devicePort.addDevice` (S4): the public handle
|
|
475
|
+
* never passes `reopenedLeafKey` — only `addDevice` does, re-driving a reopened approval (enrolment.ts).
|
|
476
|
+
* Carries `invite()`'s own liveness and re-entrancy checks so bypassing the public handle here (to reach this
|
|
477
|
+
* optional third parameter, which `MarmotGroup.invite`'s public signature does not carry) does not drop them.
|
|
478
|
+
*/
|
|
479
|
+
async function inviteAndDeliver(rt, keyPackages, reopenedLeafKey, approvalEpoch, origin) {
|
|
480
|
+
if (rt.abandoned && !ctx.isDisposed())
|
|
481
|
+
throw new MarmotError('not-a-member', `MarmotGroup: group ${rt.groupId} was left or abandoned`, { details: { groupId: rt.groupId } });
|
|
482
|
+
if (rt.disposed || ctx.isDisposed())
|
|
483
|
+
throw new MarmotError('disposed', 'MarmotGroup: client has been disposed');
|
|
484
|
+
// The Welcome deliveries below wait on account-scope surfaces: called from inside one, they would queue behind it.
|
|
485
|
+
guard.assertNotReentrant(ACCOUNT_SCOPE, 'invite');
|
|
486
|
+
const commit = await rt.run('invite', (rtc) => inviteIntent(rtc, keyPackages, reopenedLeafKey, approvalEpoch, origin));
|
|
487
|
+
const outcomes = await invitations.deliver(rt.groupId, commit.inviteId);
|
|
488
|
+
// No record yet (the handoff is blocked with the confirmation and retried with it): not attempted.
|
|
489
|
+
return Object.freeze(commit.invitees.map((invitee) => outcomes.find((o) => o.inviteeRef === invitee.inviteeRef) ?? toWelcomeOutcome({ ...invitee, status: 'pending', reason: 'awaiting-confirmation' })));
|
|
490
|
+
}
|
|
491
|
+
function buildHandle(rt) {
|
|
492
|
+
function assertLive() {
|
|
493
|
+
if (rt.abandoned && !ctx.isDisposed())
|
|
494
|
+
throw new MarmotError('not-a-member', `MarmotGroup: group ${rt.groupId} was left or abandoned`, { details: { groupId: rt.groupId } });
|
|
495
|
+
if (rt.disposed || ctx.isDisposed())
|
|
496
|
+
throw new MarmotError('disposed', 'MarmotGroup: client has been disposed');
|
|
497
|
+
}
|
|
498
|
+
function accountArg(member, account) {
|
|
499
|
+
if (typeof account !== 'string')
|
|
500
|
+
throw new MarmotError('invalid-options', `MarmotGroup.${member}: account must be a pubkey string`);
|
|
501
|
+
return account;
|
|
502
|
+
}
|
|
503
|
+
/** The teardown operations: account-scope records, so refused from inside an account- or group-scope surface. */
|
|
504
|
+
function teardownOp(operation, run) {
|
|
505
|
+
try {
|
|
506
|
+
assertLive();
|
|
507
|
+
guard.assertNotReentrant(ACCOUNT_SCOPE, operation);
|
|
508
|
+
guard.assertNotReentrant(rt.groupId, operation);
|
|
509
|
+
}
|
|
510
|
+
catch (error) {
|
|
511
|
+
return Promise.reject(error);
|
|
512
|
+
}
|
|
513
|
+
return run(teardown());
|
|
514
|
+
}
|
|
515
|
+
function view() {
|
|
516
|
+
const v = getMarmotGroupView(rt.exposedState);
|
|
517
|
+
if (v === null)
|
|
518
|
+
throw new MarmotError('store-failed', `group ${rt.groupId}: state carries no Marmot group view`, { details: { groupId: rt.groupId, malformed: true } });
|
|
519
|
+
return v;
|
|
520
|
+
}
|
|
521
|
+
return {
|
|
522
|
+
get id() {
|
|
523
|
+
assertLive();
|
|
524
|
+
return rt.groupId;
|
|
525
|
+
},
|
|
526
|
+
get nostrGroupId() {
|
|
527
|
+
assertLive();
|
|
528
|
+
return getNostrGroupIdHex(rt.exposedState);
|
|
529
|
+
},
|
|
530
|
+
get name() {
|
|
531
|
+
assertLive();
|
|
532
|
+
return view().name;
|
|
533
|
+
},
|
|
534
|
+
get description() {
|
|
535
|
+
assertLive();
|
|
536
|
+
return view().description;
|
|
537
|
+
},
|
|
538
|
+
get admins() {
|
|
539
|
+
assertLive();
|
|
540
|
+
return Object.freeze([...view().adminPubkeys]);
|
|
541
|
+
},
|
|
542
|
+
get relays() {
|
|
543
|
+
assertLive();
|
|
544
|
+
return Object.freeze([...view().relays]);
|
|
545
|
+
},
|
|
546
|
+
get epoch() {
|
|
547
|
+
assertLive();
|
|
548
|
+
return rt.exposedState.groupContext.epoch;
|
|
549
|
+
},
|
|
550
|
+
get disbanded() {
|
|
551
|
+
assertLive();
|
|
552
|
+
return rt.disbanded === undefined ? undefined : groupDisbandedOf(rt.disbanded);
|
|
553
|
+
},
|
|
554
|
+
async send(intent) {
|
|
555
|
+
assertLive();
|
|
556
|
+
const outcome = await rt.run('send', (rtc) => sendIntent(rtc, intent));
|
|
557
|
+
// A queued rumor (AC-INVITE-3) is awaited here, off the group's queue: the commit it waits for runs on it.
|
|
558
|
+
if (outcome.queued !== undefined)
|
|
559
|
+
await outcome.queued;
|
|
560
|
+
},
|
|
561
|
+
ingest(events) {
|
|
562
|
+
assertLive();
|
|
563
|
+
return rt.run('ingest', (rtc) => ingestEvents(rtc, events));
|
|
564
|
+
},
|
|
565
|
+
observeAvailability(listener) {
|
|
566
|
+
// Exempt from the fenced refusal, and answers after teardown, dispose or lease end with the one `ended` (A-6).
|
|
567
|
+
return observeAvailability(rt, listener);
|
|
568
|
+
},
|
|
569
|
+
async synced() {
|
|
570
|
+
assertLive();
|
|
571
|
+
if (rt.fence !== undefined)
|
|
572
|
+
throw fencedError(rt.groupId, 'synced', rt.fence);
|
|
573
|
+
if (rt.disbanded !== undefined)
|
|
574
|
+
throw disbandedError(rt.groupId, 'synced', rt.disbanded);
|
|
575
|
+
if (rt.sendGateOpen)
|
|
576
|
+
return;
|
|
577
|
+
// The lease is gone for this client (observed on this group, or client-wide through the store's lease-end
|
|
578
|
+
// signal, also for a runtime built afterwards): the gate never opens, so the wait fails at once.
|
|
579
|
+
if (rt.leaseLost !== undefined)
|
|
580
|
+
throw syncedLeaseLostError(rt, rt.leaseLost);
|
|
581
|
+
if (rt.availabilityClient.leaseEnded)
|
|
582
|
+
throw syncedLeaseLostError(rt, accountLeaseEndedError());
|
|
583
|
+
// ADR-013: awaited from inside a consumer surface of this group (a verifier, onProposal, a store call of the
|
|
584
|
+
// very ingest that would open the gate), the wait could never end — rejected `reentrant` instead.
|
|
585
|
+
guard.assertNotReentrant(rt.groupId, 'synced');
|
|
586
|
+
await new Promise((resolve, reject) => rt.syncWaiters.add({ resolve, reject }));
|
|
587
|
+
},
|
|
588
|
+
async pendingIntents() {
|
|
589
|
+
assertLive();
|
|
590
|
+
// Direct read, deliberately NOT serialised behind the group's op
|
|
591
|
+
// queue: AC-PIPE-1 observes the resumed record's state WHILE (not
|
|
592
|
+
// after) the fire-and-forget `resumePending` op set up by
|
|
593
|
+
// `groups.get` may still be running.
|
|
594
|
+
const records = await guard.call(rt.groupId, 'store', () => listPendingIntentSummaries(rt.groupStore.artifact('pendingIntents')));
|
|
595
|
+
// Queued rumors (AC-INVITE-3) come after every recorded intent: they go out only after them.
|
|
596
|
+
const queued = await guard.call(rt.groupId, 'store', () => listQueuedRumorRecords(rt.groupStore.artifact('queuedRumors')));
|
|
597
|
+
return [...records, ...queued.map((q) => ({ intentId: q.queueId, state: 'queued', kind: 'application' }))];
|
|
598
|
+
},
|
|
599
|
+
members() {
|
|
600
|
+
assertLive();
|
|
601
|
+
return Object.freeze(getGroupMemberPubkeys(rt.exposedState));
|
|
602
|
+
},
|
|
603
|
+
// AC-DEV-8: accounts, not leaves (ADR-003) — `members()` already lists each account once.
|
|
604
|
+
memberCount() {
|
|
605
|
+
assertLive();
|
|
606
|
+
return getGroupMemberPubkeys(rt.exposedState).length;
|
|
607
|
+
},
|
|
608
|
+
// AC-DEV-1: every leaf `account` holds, in leaf order, with its signature key (the device registry's `leafKey`).
|
|
609
|
+
leaves(account) {
|
|
610
|
+
assertLive();
|
|
611
|
+
if (typeof account !== 'string')
|
|
612
|
+
throw new MarmotError('invalid-options', 'MarmotGroup.leaves: account must be a pubkey string');
|
|
613
|
+
return Object.freeze(leafInfosOf(rt.exposedState, account));
|
|
614
|
+
},
|
|
615
|
+
invite(keyPackages) {
|
|
616
|
+
return inviteAndDeliver(rt, keyPackages);
|
|
617
|
+
},
|
|
618
|
+
async retryWelcome(inviteeRef) {
|
|
619
|
+
assertLive();
|
|
620
|
+
guard.assertNotReentrant(ACCOUNT_SCOPE, 'retryWelcome');
|
|
621
|
+
return invitations.retry(rt.groupId, inviteeRef);
|
|
622
|
+
},
|
|
623
|
+
// AC-ADMIN-2 / AC-ADMIN-8: the same functions the engine seams evaluate (account/admin.ts).
|
|
624
|
+
activeAdmins() {
|
|
625
|
+
assertLive();
|
|
626
|
+
return Object.freeze(activeAdmins(rt.exposedState.ratchetTree, view().adminPubkeys));
|
|
627
|
+
},
|
|
628
|
+
isActiveAdmin(account) {
|
|
629
|
+
assertLive();
|
|
630
|
+
return isActiveAdmin(rt.exposedState.ratchetTree, view().adminPubkeys, accountArg('isActiveAdmin', account));
|
|
631
|
+
},
|
|
632
|
+
isLastActiveAdmin(account) {
|
|
633
|
+
assertLive();
|
|
634
|
+
return isLastActiveAdmin(rt.exposedState.ratchetTree, view().adminPubkeys, accountArg('isLastActiveAdmin', account));
|
|
635
|
+
},
|
|
636
|
+
async setAdmins(accounts) {
|
|
637
|
+
assertLive();
|
|
638
|
+
await rt.run('setAdmins', (rtc) => setAdminsIntent(rtc, accounts));
|
|
639
|
+
},
|
|
640
|
+
async rename(name, options) {
|
|
641
|
+
assertLive();
|
|
642
|
+
await rt.run('rename', (rtc) => renameIntent(rtc, name, options));
|
|
643
|
+
},
|
|
644
|
+
async removeMember(account) {
|
|
645
|
+
assertLive();
|
|
646
|
+
await rt.run('removeMember', (rtc) => removeMemberIntent(rtc, account));
|
|
647
|
+
},
|
|
648
|
+
// Epic marmot-invitation-outcomes S5 (SC-11): the cleanup ladder runs as one group operation.
|
|
649
|
+
async removeAdmission(ref) {
|
|
650
|
+
assertLive();
|
|
651
|
+
return rt.run('removeAdmission', (rtc) => removeAdmissionIntent(rtc, ref));
|
|
652
|
+
},
|
|
653
|
+
departAsAdmin() {
|
|
654
|
+
return teardownOp('departAsAdmin', (t) => t.depart(rt.groupId));
|
|
655
|
+
},
|
|
656
|
+
leave() {
|
|
657
|
+
return teardownOp('leave', (t) => t.leave(rt.groupId));
|
|
658
|
+
},
|
|
659
|
+
abandon() {
|
|
660
|
+
return teardownOp('abandon', (t) => t.abandon(rt.groupId));
|
|
661
|
+
},
|
|
662
|
+
destroy() {
|
|
663
|
+
return teardownOp('destroy', (t) => t.destroy(rt.groupId));
|
|
664
|
+
},
|
|
665
|
+
encryptMedia(bytes, options) {
|
|
666
|
+
assertLive();
|
|
667
|
+
return encryptMediaOp(rt, bytes, options);
|
|
668
|
+
},
|
|
669
|
+
decryptMedia(media) {
|
|
670
|
+
assertLive();
|
|
671
|
+
return decryptMediaOp(rt, media);
|
|
672
|
+
},
|
|
673
|
+
history() {
|
|
674
|
+
assertLive();
|
|
675
|
+
assertNotInHistoryBackend(rt);
|
|
676
|
+
return historyOf(rt);
|
|
677
|
+
},
|
|
678
|
+
onNotification(listener) {
|
|
679
|
+
assertLive();
|
|
680
|
+
return subscribeNotifications(rt, listener);
|
|
681
|
+
},
|
|
682
|
+
async equivocations() {
|
|
683
|
+
assertLive();
|
|
684
|
+
// Like pendingIntents(): a direct read of the durable records, not queued behind the group's operations.
|
|
685
|
+
return guard.call(rt.groupId, 'store', () => readEquivocationNotifications(rt.groupStore.extension(EQUIVOCATION_EXTENSION)));
|
|
686
|
+
},
|
|
687
|
+
};
|
|
688
|
+
}
|
|
689
|
+
function makeRuntime(groupId, groupStore, engine, joinedAtMs, persisted = {}) {
|
|
690
|
+
let exposed = engine.state;
|
|
691
|
+
let fence = persisted.fence;
|
|
692
|
+
// Epic marmot-invitation-outcomes S1: the availability facts below publish through their accessors (SC-10).
|
|
693
|
+
let caughtUp = false;
|
|
694
|
+
// AC-PIPE-15: closed only for state read back from the store (see GroupRuntime.sendGateOpen).
|
|
695
|
+
let sendGateOpen = persisted.loaded !== true;
|
|
696
|
+
let disbanded = persisted.disbanded;
|
|
697
|
+
// A-7: a new adoption (a join, a restore) starts the group over; a fence a load starts with is the session's.
|
|
698
|
+
if (fence !== undefined)
|
|
699
|
+
availabilityClient.sessionFences.record(groupId, fence);
|
|
700
|
+
else if (persisted.adoption === true)
|
|
701
|
+
availabilityClient.sessionFences.clear(groupId);
|
|
702
|
+
const rt = {
|
|
703
|
+
groupId,
|
|
704
|
+
groupStore,
|
|
705
|
+
engine,
|
|
706
|
+
get exposedState() {
|
|
707
|
+
return exposed;
|
|
708
|
+
},
|
|
709
|
+
set exposedState(state) {
|
|
710
|
+
const changed = state !== exposed;
|
|
711
|
+
exposed = state;
|
|
712
|
+
if (changed) {
|
|
713
|
+
notifyState(groupId);
|
|
714
|
+
publishAvailability(rt);
|
|
715
|
+
}
|
|
716
|
+
},
|
|
717
|
+
joinedAtMs,
|
|
718
|
+
resyncNeeded: false,
|
|
719
|
+
resync() {
|
|
720
|
+
requestHistoryResync(rt);
|
|
721
|
+
},
|
|
722
|
+
checkSyncRouting() {
|
|
723
|
+
restartSyncOnRoutingChange(rt);
|
|
724
|
+
},
|
|
725
|
+
ctx,
|
|
726
|
+
guard,
|
|
727
|
+
confirmHooks,
|
|
728
|
+
settledListeners: new Set(),
|
|
729
|
+
terminalWatchers: new Set(),
|
|
730
|
+
disposed: false,
|
|
731
|
+
abandoned: false,
|
|
732
|
+
leaseLost: undefined,
|
|
733
|
+
selfRemoveElection: undefined,
|
|
734
|
+
leaverPredicate: leaverPredicateOf(ctx, engine.state),
|
|
735
|
+
window: persisted.window ?? tipOnlyWindow(engine.state),
|
|
736
|
+
get fence() {
|
|
737
|
+
return fence;
|
|
738
|
+
},
|
|
739
|
+
set fence(cause) {
|
|
740
|
+
const was = fence;
|
|
741
|
+
fence = cause;
|
|
742
|
+
if (was === undefined && cause !== undefined) {
|
|
743
|
+
availabilityClient.sessionFences.record(groupId, cause);
|
|
744
|
+
endRuntime(rt, { reason: 'fenced', cause });
|
|
745
|
+
}
|
|
746
|
+
publishAvailability(rt);
|
|
747
|
+
},
|
|
748
|
+
removal: persisted.removal,
|
|
749
|
+
async removalSettled(actor) {
|
|
750
|
+
try {
|
|
751
|
+
if (await teardown().tearingDown(groupId))
|
|
752
|
+
return;
|
|
753
|
+
if (!ctx.isDisposed())
|
|
754
|
+
ctx.emit('groupRemoved', Object.freeze(actor === undefined ? { groupId } : { groupId, actor }));
|
|
755
|
+
}
|
|
756
|
+
catch {
|
|
757
|
+
// Best-effort notification: a failed teardown-record read loses it (documented at-most-once).
|
|
758
|
+
}
|
|
759
|
+
},
|
|
760
|
+
opaque: persisted.opaque,
|
|
761
|
+
pendingCommit: persisted.pendingCommit,
|
|
762
|
+
pendingLeaf: persisted.pendingLeaf,
|
|
763
|
+
get disbanded() {
|
|
764
|
+
return disbanded;
|
|
765
|
+
},
|
|
766
|
+
set disbanded(record) {
|
|
767
|
+
disbanded = record;
|
|
768
|
+
publishAvailability(rt);
|
|
769
|
+
},
|
|
770
|
+
availabilityListeners: new Set(),
|
|
771
|
+
availabilitySeq: 0,
|
|
772
|
+
availabilityLast: undefined,
|
|
773
|
+
availabilityEnded: undefined,
|
|
774
|
+
availabilityClient,
|
|
775
|
+
notificationListeners: new Set(),
|
|
776
|
+
announceRewind: () => announceRewind(groupId),
|
|
777
|
+
announceRotation: () => announceRotation(groupId),
|
|
778
|
+
historyDrain: { running: undefined, again: false, stopped: false, inFlight: undefined },
|
|
779
|
+
queue: new SerialQueue(),
|
|
780
|
+
handle: undefined,
|
|
781
|
+
historyTracker: createHistorySilenceTracker(ctx.historySilenceThreshold),
|
|
782
|
+
historyGapsReported: new Set(),
|
|
783
|
+
historyStalls: new Map(),
|
|
784
|
+
walkPasses: 0,
|
|
785
|
+
walkStartedAt: undefined,
|
|
786
|
+
catchUpSince: undefined,
|
|
787
|
+
syncStarted: false,
|
|
788
|
+
get caughtUp() {
|
|
789
|
+
return caughtUp;
|
|
790
|
+
},
|
|
791
|
+
set caughtUp(value) {
|
|
792
|
+
caughtUp = value;
|
|
793
|
+
publishAvailability(rt);
|
|
794
|
+
},
|
|
795
|
+
connectivityGeneration: 0,
|
|
796
|
+
get sendGateOpen() {
|
|
797
|
+
return sendGateOpen;
|
|
798
|
+
},
|
|
799
|
+
set sendGateOpen(value) {
|
|
800
|
+
sendGateOpen = value;
|
|
801
|
+
publishAvailability(rt);
|
|
802
|
+
},
|
|
803
|
+
// AC-RATCH-1: armed for the same loaded state unless its stored skip is still undelivered (see GroupRuntime.ratchetSkipPending).
|
|
804
|
+
ratchetSkipPending: persisted.loaded === true && persisted.ratchetSkipArmed !== false,
|
|
805
|
+
syncWaiters: new Set(),
|
|
806
|
+
syncSubscription: undefined,
|
|
807
|
+
syncRouting: undefined,
|
|
808
|
+
syncHistoryRetryTimer: undefined,
|
|
809
|
+
pipelineTimers: new Set(),
|
|
810
|
+
reenqueueRetryArmed: new Set(),
|
|
811
|
+
run(operation, fn) {
|
|
812
|
+
try {
|
|
813
|
+
guard.assertNotReentrant(groupId, operation);
|
|
814
|
+
}
|
|
815
|
+
catch (err) {
|
|
816
|
+
return Promise.reject(err);
|
|
817
|
+
}
|
|
818
|
+
return rt.queue.run(async () => {
|
|
819
|
+
if (rt.abandoned && !ctx.isDisposed())
|
|
820
|
+
throw new MarmotError('not-a-member', `${operation}: group ${groupId} was left or abandoned`, { details: { groupId } });
|
|
821
|
+
if (rt.disposed || ctx.isDisposed()) {
|
|
822
|
+
throw new MarmotError('disposed', `${operation}: client has been disposed`);
|
|
823
|
+
}
|
|
824
|
+
if (rt.needsReload) {
|
|
825
|
+
await rt.reloadFromDurable();
|
|
826
|
+
rt.needsReload = false;
|
|
827
|
+
}
|
|
828
|
+
return fn(rt);
|
|
829
|
+
});
|
|
830
|
+
},
|
|
831
|
+
runLast(fn) {
|
|
832
|
+
return rt.queue.run(async () => {
|
|
833
|
+
await fn();
|
|
834
|
+
});
|
|
835
|
+
},
|
|
836
|
+
async rebuildEngine(state, window, opaqueFrom) {
|
|
837
|
+
const old = rt.engine;
|
|
838
|
+
// S10: a rebuild restores the durable rewind window (it describes the durable state), unless handed one.
|
|
839
|
+
const stored = window ?? (await guard.call(groupId, 'store', () => readWindow(groupStore.artifact('rewindTree'))));
|
|
840
|
+
// S20: a tombstone state is rebuilt with the removal it stands for (the durable one — the state handed in is a
|
|
841
|
+
// durable state or a batch's starting state, both of which `rt.removal` describes).
|
|
842
|
+
const removal = belongsTo(rt.removal, state) ? rt.removal : undefined;
|
|
843
|
+
const history = restoreHistory(stored, removal === undefined ? state : copyOf(removal.record.parentState));
|
|
844
|
+
const built = await buildEngineFailClosed(ctx, ciphersuiteCache, state, () => settleCheck(groupId), history, removal?.record, opaqueFrom === undefined ? rt.opaque : opaqueFrom.record);
|
|
845
|
+
rt.engine = built.engine;
|
|
846
|
+
rt.window = windowFor(stored, state);
|
|
847
|
+
rt.leaverPredicate = leaverPredicateOf(ctx, state);
|
|
848
|
+
rt.selfRemoveElection = undefined;
|
|
849
|
+
old.dispose();
|
|
850
|
+
// A record the engine refused: the group is fenced (client/opaque-candidates.ts, fail closed).
|
|
851
|
+
if (built.corrupt !== undefined && rt.fence === undefined)
|
|
852
|
+
rt.fence = built.corrupt;
|
|
853
|
+
},
|
|
854
|
+
needsReload: false,
|
|
855
|
+
invitations,
|
|
856
|
+
queueWaiters: new Map(),
|
|
857
|
+
queuedHint: undefined,
|
|
858
|
+
drainScheduled: false,
|
|
859
|
+
drainFailures: 0,
|
|
860
|
+
drainRetryTimer: undefined,
|
|
861
|
+
queueCommitHold: undefined,
|
|
862
|
+
selfRemoveElectionWait: undefined,
|
|
863
|
+
scheduleQueueDrain() {
|
|
864
|
+
const r = rt;
|
|
865
|
+
if (r.queuedHint === 0 || r.drainScheduled || r.disposed || ctx.isDisposed())
|
|
866
|
+
return;
|
|
867
|
+
if (r.drainRetryTimer !== undefined) {
|
|
868
|
+
// A real trigger while a failed drain waits for its backoff: drain now instead.
|
|
869
|
+
ctx.timers.clear(r.drainRetryTimer);
|
|
870
|
+
r.pipelineTimers.delete(r.drainRetryTimer);
|
|
871
|
+
r.drainRetryTimer = undefined;
|
|
872
|
+
}
|
|
873
|
+
r.drainScheduled = true;
|
|
874
|
+
// A later microtask: never inside the caller's own frame or transaction. `Promise.resolve().then(...)`
|
|
875
|
+
// does not clear Node's AsyncLocalStorage context on its own (S3 AC-RE-1) — `guard.detached` does.
|
|
876
|
+
void Promise.resolve()
|
|
877
|
+
.then(() => guard.detached(() => r.run('drainQueue', async (rtc) => {
|
|
878
|
+
r.drainScheduled = false;
|
|
879
|
+
const { commitError } = await advanceQueue(rtc);
|
|
880
|
+
// A commit the drain could not even build or record (a signer or store fault) is retried
|
|
881
|
+
// like a thrown drain. A terminal one is held by holdQueueCommit; an unpublished one is a
|
|
882
|
+
// durable intent whose own retry and confirmation drain the queue again.
|
|
883
|
+
if (commitError !== undefined && !isMarmotError(commitError, 'send-rejected') && !isMarmotError(commitError, 'publish-failed'))
|
|
884
|
+
throw commitError;
|
|
885
|
+
r.drainFailures = 0;
|
|
886
|
+
})))
|
|
887
|
+
.catch((error) => {
|
|
888
|
+
r.drainScheduled = false;
|
|
889
|
+
// The queue is durable, but nothing else may come along to drain it: retried on the
|
|
890
|
+
// capped backoff (like Welcome delivery) until it succeeds, the client is disposed or
|
|
891
|
+
// the stores lose their lease (the next holder drains on load).
|
|
892
|
+
if (isMarmotError(error, 'disposed') || isMarmotError(error, 'no-lease'))
|
|
893
|
+
return;
|
|
894
|
+
r.scheduleQueueDrainRetry();
|
|
895
|
+
});
|
|
896
|
+
},
|
|
897
|
+
scheduleQueueDrainRetry() {
|
|
898
|
+
const r = rt;
|
|
899
|
+
if (r.disposed || ctx.isDisposed() || r.drainRetryTimer !== undefined)
|
|
900
|
+
return;
|
|
901
|
+
r.drainFailures += 1;
|
|
902
|
+
const handle = ctx.timers.set(() => {
|
|
903
|
+
r.pipelineTimers.delete(handle);
|
|
904
|
+
r.drainRetryTimer = undefined;
|
|
905
|
+
r.scheduleQueueDrain();
|
|
906
|
+
}, backoffMs(r.drainFailures));
|
|
907
|
+
r.drainRetryTimer = handle;
|
|
908
|
+
r.pipelineTimers.add(handle);
|
|
909
|
+
},
|
|
910
|
+
async reloadFromDurable() {
|
|
911
|
+
const state = await guard.call(groupId, 'store', () => readGroupState(groupStore.artifact('clientState')));
|
|
912
|
+
if (state === undefined)
|
|
913
|
+
throw new MarmotError('store-failed', `group ${groupId}: persisted clientState is missing`);
|
|
914
|
+
const removal = await guard.call(groupId, 'store', () => readRemoval(groupStore.extension(REMOVAL_EXTENSION)));
|
|
915
|
+
rt.removal = belongsTo(removal, state) ? removal : undefined;
|
|
916
|
+
// The held opaque record, read with the state it describes (the durable one: a rolled-back batch's is gone).
|
|
917
|
+
const opaque = await loadOpaque(guard, groupId, groupStore);
|
|
918
|
+
rt.opaque = opaque.record;
|
|
919
|
+
const pendingRestore = await guard.call(groupId, 'store', () => readPendingRestore(groupStore));
|
|
920
|
+
rt.pendingCommit = pendingRestore.pendingCommit;
|
|
921
|
+
rt.pendingLeaf = pendingRestore.pendingLeaf;
|
|
922
|
+
await rt.rebuildEngine(state);
|
|
923
|
+
if (opaque.corrupt !== undefined && rt.fence === undefined)
|
|
924
|
+
rt.fence = opaque.corrupt;
|
|
925
|
+
rt.exposedState = rt.engine.state;
|
|
926
|
+
rt.checkSyncRouting();
|
|
927
|
+
},
|
|
928
|
+
};
|
|
929
|
+
rt.handle = buildHandle(rt);
|
|
930
|
+
// The runtime's first availability snapshot (seq 1): what a subscriber's initial observation carries until a change.
|
|
931
|
+
publishAvailability(rt);
|
|
932
|
+
runtimes.set(groupId, rt);
|
|
933
|
+
// AC-FWJ-31: an own intent settling clears "own commit pending"; the first sync's end (the send gate opening)
|
|
934
|
+
// clears "syncing". Both ask whether a `competing` Welcome waits for the group.
|
|
935
|
+
rt.settledListeners.add(() => gateMayHaveCleared(groupId));
|
|
936
|
+
if (!rt.sendGateOpen)
|
|
937
|
+
rt.syncWaiters.add({ resolve: () => gateMayHaveCleared(groupId), reject: () => undefined });
|
|
938
|
+
return rt;
|
|
939
|
+
}
|
|
940
|
+
async function createGroupOp(name, options) {
|
|
941
|
+
// Called from inside a consumer account-store (or signer) call, the nested create would queue an account
|
|
942
|
+
// transaction behind the one awaiting it: reject instead of deadlocking.
|
|
943
|
+
guard.assertNotReentrant(ACCOUNT_SCOPE, 'groups.create');
|
|
944
|
+
if (typeof name !== 'string' || name.length === 0)
|
|
945
|
+
throw new MarmotError('invalid-options', 'groups.create: name must be a non-empty string');
|
|
946
|
+
if (options !== undefined && (typeof options !== 'object' || options === null)) {
|
|
947
|
+
throw new MarmotError('invalid-options', 'groups.create: options must be an object');
|
|
948
|
+
}
|
|
949
|
+
const relaysOpt = options?.relays;
|
|
950
|
+
if (relaysOpt !== undefined && (!Array.isArray(relaysOpt) || !relaysOpt.every((r) => typeof r === 'string'))) {
|
|
951
|
+
throw new MarmotError('invalid-options', 'groups.create: options.relays must be an array of relay URLs');
|
|
952
|
+
}
|
|
953
|
+
const adminsOpt = options?.admins;
|
|
954
|
+
if (adminsOpt !== undefined && (!Array.isArray(adminsOpt) || !adminsOpt.every((a) => typeof a === 'string'))) {
|
|
955
|
+
throw new MarmotError('invalid-options', 'groups.create: options.admins must be an array of pubkeys');
|
|
956
|
+
}
|
|
957
|
+
// AC-INBOX-7: before anything else happens — a rejecting gate aborts with its own rejection.
|
|
958
|
+
await ctx.beforeCreateGroup.pass();
|
|
959
|
+
if (ctx.isDisposed())
|
|
960
|
+
throw new MarmotError('disposed', 'groups.create: client has been disposed');
|
|
961
|
+
const selfPubkey = await guard.call(ACCOUNT_SCOPE, 'signer', () => ctx.signer.getPublicKey());
|
|
962
|
+
const impl = await ciphersuiteImplFor(ciphersuiteCache, ciphersuites[DEFAULT_CIPHERSUITE_NAME]);
|
|
963
|
+
const credential = createCredential(selfPubkey);
|
|
964
|
+
// S8: the creator leaf carries this client's device signing key, like every leaf it mints.
|
|
965
|
+
const deviceKey = await ensureDeviceSigningKey((fn) => guard.call(ACCOUNT_SCOPE, 'store', () => ctx.stores.account.transaction(fn)), impl, ctx.clock.now());
|
|
966
|
+
const kp = await generateKeyPackage({
|
|
967
|
+
credential,
|
|
968
|
+
ciphersuiteImpl: withDeviceSignatureKey(impl, deviceKey),
|
|
969
|
+
isLastResort: false,
|
|
970
|
+
// A device key that carries its stored proof (after a rotation prepare) reuses it: no kind-450 signer call.
|
|
971
|
+
accountProofSigner: deviceProofSigner(deviceKey, (template) => guard.call(ACCOUNT_SCOPE, 'signer', () => ctx.signer.signEvent(template))),
|
|
972
|
+
});
|
|
973
|
+
const { clientState } = await createSimpleGroup(kp, impl, name, {
|
|
974
|
+
adminPubkeys: [selfPubkey, ...(adminsOpt ?? [])],
|
|
975
|
+
relays: relaysOpt !== undefined ? [...relaysOpt] : [...ctx.relays.defaults],
|
|
976
|
+
});
|
|
977
|
+
const proofFailures = enforceStateProofs(clientState);
|
|
978
|
+
if (proofFailures.length > 0) {
|
|
979
|
+
throw new MarmotError('store-failed', 'groups.create: the created group state failed its own leaf-proof check', {
|
|
980
|
+
details: { failures: proofFailures },
|
|
981
|
+
});
|
|
982
|
+
}
|
|
983
|
+
const groupId = getGroupIdHex(clientState);
|
|
984
|
+
// The group becomes visible (runtime cache, account index) only once it
|
|
985
|
+
// is durable. A concurrent `groups.get/list` for this id meanwhile joins
|
|
986
|
+
// this creation (via `loading`) rather than loading a half-written group.
|
|
987
|
+
// `groupCreated` fires only once both writes committed (AC-INBOX-6).
|
|
988
|
+
const rt = await ctx.lifecycle.afterCommit('groupCreated', async () => {
|
|
989
|
+
let settle;
|
|
990
|
+
const creation = new Promise((resolve) => (settle = resolve));
|
|
991
|
+
loading.set(groupId, creation);
|
|
992
|
+
let created;
|
|
993
|
+
try {
|
|
994
|
+
created = await persistCreatedGroup(groupId, clientState);
|
|
995
|
+
}
|
|
996
|
+
catch (error) {
|
|
997
|
+
settle(undefined);
|
|
998
|
+
throw error;
|
|
999
|
+
}
|
|
1000
|
+
finally {
|
|
1001
|
+
loading.delete(groupId);
|
|
1002
|
+
}
|
|
1003
|
+
settle(created);
|
|
1004
|
+
startGroupSync(created);
|
|
1005
|
+
return created;
|
|
1006
|
+
}, () => ({ groupId }));
|
|
1007
|
+
return rt.handle;
|
|
1008
|
+
}
|
|
1009
|
+
/**
|
|
1010
|
+
* Writes the new group's state (group scope) — with the `onConfirm` hooks run as `kind: 'create'` inside that
|
|
1011
|
+
* same transaction (AC-MX-1: a throw stores nothing and rejects the creation with the hook's error) — then its
|
|
1012
|
+
* account index entry (account scope) — sequentially, never an account transaction awaiting a group scope —
|
|
1013
|
+
* and only then caches its runtime. A failed index write clears the brand-new group store again (best effort:
|
|
1014
|
+
* the state and whatever a hook wrote; an unindexed state is invisible to `groups.list`).
|
|
1015
|
+
*/
|
|
1016
|
+
async function persistCreatedGroup(groupId, clientState) {
|
|
1017
|
+
const groupStore = ctx.stores.groupFactory(groupId);
|
|
1018
|
+
const createdAt = ctx.clock.now();
|
|
1019
|
+
await guard.call(groupId, 'store', () => groupStore.transaction(async (tx) => {
|
|
1020
|
+
await writeGroupState(tx.artifact('clientState'), clientState);
|
|
1021
|
+
await runConfirmHooks({ groupId, guard, hooks: confirmHooks }, { intentId: groupId, kind: 'create', epoch: clientState.groupContext.epoch, tx, persisted: () => ({ state: clientState, window: tipOnlyWindow(clientState) }), notifications: [], createdAt });
|
|
1022
|
+
}));
|
|
1023
|
+
try {
|
|
1024
|
+
await guard.call(ACCOUNT_SCOPE, 'store', () => ctx.stores.account.transaction((tx) => writeGroupIndexEntry(tx.extension(GROUP_INDEX_EXTENSION), groupId, { createdAt })));
|
|
1025
|
+
}
|
|
1026
|
+
catch (error) {
|
|
1027
|
+
await guard.call(groupId, 'store', () => groupStore.clear()).catch(() => undefined);
|
|
1028
|
+
throw error;
|
|
1029
|
+
}
|
|
1030
|
+
if (ctx.isDisposed())
|
|
1031
|
+
throw new MarmotError('disposed', 'groups.create: client was disposed while creating the group');
|
|
1032
|
+
const engine = await buildEngine(ctx, ciphersuiteCache, clientState, () => settleCheck(groupId));
|
|
1033
|
+
if (ctx.isDisposed()) {
|
|
1034
|
+
engine.dispose();
|
|
1035
|
+
throw new MarmotError('disposed', 'groups.create: client was disposed while creating the group');
|
|
1036
|
+
}
|
|
1037
|
+
return makeRuntime(groupId, groupStore, engine, createdAt);
|
|
1038
|
+
}
|
|
1039
|
+
/**
|
|
1040
|
+
* S6 join: the group's state (group scope, re-read inside the transaction —
|
|
1041
|
+
* an existing state is never overwritten), then its account index entry
|
|
1042
|
+
* stamped `joinedAtMs` — the Welcome's arrival, so a late accept still
|
|
1043
|
+
* floors pre-join events at the Welcome's time (account scope: the floor
|
|
1044
|
+
* ingest uses for undecryptable pre-join events) — sequentially, group
|
|
1045
|
+
* then account, never an account transaction awaiting a group scope. A
|
|
1046
|
+
* state already stored without its index entry (a join interrupted between
|
|
1047
|
+
* the two writes) gets the entry now and reports `already-member`. Like
|
|
1048
|
+
* `groups.create`, the group becomes visible only once durable: a
|
|
1049
|
+
* concurrent `groups.get` joins this load.
|
|
1050
|
+
*/
|
|
1051
|
+
async function adoptJoinedGroupOp(groupId, clientState, joinedAtMs, joinEvidence, invitationId, welcomeCreatedAtMs) {
|
|
1052
|
+
// AC-MX-2: a restore of this group in flight decides first — it holds the group only once it succeeded; a
|
|
1053
|
+
// failed one left nothing, and this Welcome is adopted as usual. Deferred (transient), never awaited: the
|
|
1054
|
+
// caller is the inbox queue, and the restore may wait on unreachable group relays indefinitely.
|
|
1055
|
+
if (restoring.has(groupId))
|
|
1056
|
+
throw restoreInFlight(groupId);
|
|
1057
|
+
// S9: a purge a crash (or a failing step) interrupted finishes first — before anything reads the group as
|
|
1058
|
+
// held: its still-cached runtime would answer `already-member`, and the purge would then delete the group this
|
|
1059
|
+
// Welcome brings back. Then a group this client tore down comes back only through a Welcome newer than the
|
|
1060
|
+
// state it purged.
|
|
1061
|
+
if (ctx.finishPurgeBeforeJoin)
|
|
1062
|
+
await teardown().finishPurge(groupId);
|
|
1063
|
+
if (await teardown().refusesJoin(groupId, clientState.groupContext.epoch)) {
|
|
1064
|
+
throw new MarmotError('not-a-member', `invitations: group ${groupId} was left or abandoned by this client; this Welcome would bring its old state back`, { details: { groupId } });
|
|
1065
|
+
}
|
|
1066
|
+
const cached = runtimes.get(groupId);
|
|
1067
|
+
if (cached !== undefined && !cached.disposed)
|
|
1068
|
+
return 'already-member';
|
|
1069
|
+
// AC-MX-2: a restore started while the purge check above ran decides, like one in flight before it.
|
|
1070
|
+
if (restoring.has(groupId))
|
|
1071
|
+
throw restoreInFlight(groupId);
|
|
1072
|
+
// AC-MX-2: an unfinished restore an earlier client left is resumed, never replaced (its self-update may be on
|
|
1073
|
+
// the relays already, and only these stores can confirm it): this Welcome is deferred like for one in flight.
|
|
1074
|
+
// Checked before waiting on a load in flight: a load of such a group waits for the resume (never on this queue).
|
|
1075
|
+
if (await restorePending(groupId, groupStoreOf(groupId))) {
|
|
1076
|
+
resumeRestore(groupId);
|
|
1077
|
+
throw restoreInFlight(groupId);
|
|
1078
|
+
}
|
|
1079
|
+
if (restoring.has(groupId))
|
|
1080
|
+
throw restoreInFlight(groupId);
|
|
1081
|
+
if (runtimes.get(groupId)?.disposed === false)
|
|
1082
|
+
return 'already-member';
|
|
1083
|
+
const inFlight = loading.get(groupId);
|
|
1084
|
+
if (inFlight !== undefined) {
|
|
1085
|
+
assertNotWithinOwnLoad(groupId, 'invitations');
|
|
1086
|
+
if ((await inFlight.catch(() => undefined)) !== undefined)
|
|
1087
|
+
return 'already-member';
|
|
1088
|
+
}
|
|
1089
|
+
// AC-MX-2: and one started while the load above was awaited (its adoption must not be overwritten below).
|
|
1090
|
+
if (restoring.has(groupId))
|
|
1091
|
+
throw restoreInFlight(groupId);
|
|
1092
|
+
let settle;
|
|
1093
|
+
const adoption = new Promise((resolve) => (settle = resolve));
|
|
1094
|
+
loading.set(groupId, adoption);
|
|
1095
|
+
// A group this client left or abandoned earlier is joined again by a new Welcome.
|
|
1096
|
+
abandoned.delete(groupId);
|
|
1097
|
+
let rt;
|
|
1098
|
+
let outcome;
|
|
1099
|
+
// S10: the join-time window (the tip alone) — written in the same transaction as the state (below). Its root
|
|
1100
|
+
// carries the join evidence of a tagged Welcome (epic marmot-welcome-race-joiner): the digest of the commit that
|
|
1101
|
+
// produced the joined state and the committer, the held branch's only record of both when a second Welcome competes.
|
|
1102
|
+
const window = tipOnlyWindow(clientState, joinEvidence);
|
|
1103
|
+
try {
|
|
1104
|
+
const groupStore = groupStoreOf(groupId);
|
|
1105
|
+
const wrote = await guard.call(groupId, 'store', () => groupStore.transaction(async (tx) => {
|
|
1106
|
+
if ((await readGroupState(tx.artifact('clientState'))) !== undefined)
|
|
1107
|
+
return false;
|
|
1108
|
+
await writeGroupState(tx.artifact('clientState'), clientState);
|
|
1109
|
+
await writeWindow(tx.artifact('rewindTree'), window);
|
|
1110
|
+
// Owner decision 20 (BACKLOG engine-undecryptable-set-unbounded): the first sync fetches from the Welcome's
|
|
1111
|
+
// time minus the skew allowance, so the group's pre-join history (never decryptable here) neither costs the
|
|
1112
|
+
// fetch nor fills the branch-aware fence's undecryptable set. The Welcome's stamp is the sponsor's claim:
|
|
1113
|
+
// clamped to [arrival - grace, arrival] as the rebase path clamps it, so an offline invitee still fetches what
|
|
1114
|
+
// was sent between the Welcome's creation and its arrival, and a forged stamp cannot reach back for ever.
|
|
1115
|
+
// Every later `since` (history pass, live subscribe, resume window) inherits it; a fetch bound only — an
|
|
1116
|
+
// event below it that a relay delivers anyway is still processed.
|
|
1117
|
+
const cursorView = tx.artifact('ingestCursor');
|
|
1118
|
+
if ((await readIngestCursor(cursorView)) === undefined) {
|
|
1119
|
+
const welcomeMs = welcomeCreatedAtMs !== undefined && Number.isFinite(welcomeCreatedAtMs) ? welcomeCreatedAtMs : joinedAtMs;
|
|
1120
|
+
const floorMs = Math.min(joinedAtMs, Math.max(welcomeMs, joinedAtMs - ctx.graceWindowMs));
|
|
1121
|
+
await writeIngestCursor(cursorView, { since: Math.max(0, Math.floor(floorMs / 1000) - CURSOR_FUTURE_SKEW_S) });
|
|
1122
|
+
}
|
|
1123
|
+
// Epic marmot-leaf-backup-pre-publish S2: what an earlier state of this group parked or marked belongs to that state.
|
|
1124
|
+
await clearPendingLeaf(tx.extension(PENDING_LEAF_EXTENSION));
|
|
1125
|
+
await clearPendingCommit(tx.extension(PENDING_COMMIT_EXTENSION));
|
|
1126
|
+
// Epic marmot-invitation-outcomes S2 (ADR-019 D6): the adopting invitation's proof, with the state it adopted.
|
|
1127
|
+
// Adoption happens when this transaction commits and the compensating clear below does not retract it.
|
|
1128
|
+
if (invitationId !== undefined)
|
|
1129
|
+
await writeAdoptionProof(tx, invitationId, { via: 'join', tag: hexOfBytes(clientState.confirmationTag), at: joinedAtMs });
|
|
1130
|
+
// AC-MX-1: only an adoption that writes the group runs the hooks (never `already-member`); a throw
|
|
1131
|
+
// stores nothing — the join stays `joining` and a later inbox pass completes it.
|
|
1132
|
+
await runConfirmHooks({ groupId, guard, hooks: confirmHooks }, { intentId: groupId, kind: 'join', epoch: clientState.groupContext.epoch, tx, persisted: () => ({ state: clientState, window }), notifications: [], createdAt: joinedAtMs });
|
|
1133
|
+
return true;
|
|
1134
|
+
}));
|
|
1135
|
+
outcome = wrote ? 'joined' : 'already-member';
|
|
1136
|
+
try {
|
|
1137
|
+
await guard.call(ACCOUNT_SCOPE, 'store', () => ctx.stores.account.transaction(async (tx) => {
|
|
1138
|
+
const index = tx.extension(GROUP_INDEX_EXTENSION);
|
|
1139
|
+
if ((await readGroupIndexEntry(index, groupId)) === undefined)
|
|
1140
|
+
await writeGroupIndexEntry(index, groupId, { createdAt: joinedAtMs, ...(invitationId === undefined ? {} : { adoptedBy: invitationId }) });
|
|
1141
|
+
}));
|
|
1142
|
+
}
|
|
1143
|
+
catch (error) {
|
|
1144
|
+
// Only a group this call wrote is taken back (best effort: the state and whatever a hook wrote with it); an
|
|
1145
|
+
// unindexed state is invisible to `groups.list`.
|
|
1146
|
+
if (wrote)
|
|
1147
|
+
await guard.call(groupId, 'store', () => groupStore.clear()).catch(() => undefined);
|
|
1148
|
+
throw error;
|
|
1149
|
+
}
|
|
1150
|
+
if (ctx.isDisposed())
|
|
1151
|
+
throw new MarmotError('disposed', 'invitations: client was disposed while joining the group');
|
|
1152
|
+
if (wrote) {
|
|
1153
|
+
const engine = await buildEngine(ctx, ciphersuiteCache, clientState, () => settleCheck(groupId));
|
|
1154
|
+
if (ctx.isDisposed()) {
|
|
1155
|
+
engine.dispose();
|
|
1156
|
+
throw new MarmotError('disposed', 'invitations: client was disposed while joining the group');
|
|
1157
|
+
}
|
|
1158
|
+
rt = makeRuntime(groupId, groupStore, engine, joinedAtMs, { window, adoption: true });
|
|
1159
|
+
}
|
|
1160
|
+
}
|
|
1161
|
+
catch (error) {
|
|
1162
|
+
settle(undefined);
|
|
1163
|
+
throw error;
|
|
1164
|
+
}
|
|
1165
|
+
finally {
|
|
1166
|
+
loading.delete(groupId);
|
|
1167
|
+
}
|
|
1168
|
+
settle(rt);
|
|
1169
|
+
if (rt !== undefined) {
|
|
1170
|
+
startGroupSync(rt);
|
|
1171
|
+
// Epic marmot-device-key-rotation (AC-REKEY-11): a Welcome built on a retired-key KeyPackage lands an old-key leaf.
|
|
1172
|
+
scheduleRekeyFollowUp(rt);
|
|
1173
|
+
}
|
|
1174
|
+
return outcome;
|
|
1175
|
+
}
|
|
1176
|
+
/**
|
|
1177
|
+
* Refuses (`reentrant`) to wait on `groupId`'s load or adoption in flight from inside a consumer surface of that
|
|
1178
|
+
* group's own scope: that surface is (or runs inside) the very load waiting on this call — awaiting it would
|
|
1179
|
+
* hang both (the same rule {@link loadRuntime} applies to `groups.get`).
|
|
1180
|
+
*/
|
|
1181
|
+
function assertNotWithinOwnLoad(groupId, operation) {
|
|
1182
|
+
if (!guard.active(groupId))
|
|
1183
|
+
return;
|
|
1184
|
+
throw new MarmotError('reentrant', `${operation}(${JSON.stringify(groupId)}): called from inside a consumer surface of that group's own load`, {
|
|
1185
|
+
details: { scope: groupId, operation },
|
|
1186
|
+
});
|
|
1187
|
+
}
|
|
1188
|
+
async function hasGroupOp(groupId) {
|
|
1189
|
+
// AC-MX-2: a group being restored counts as held — a Welcome for it is joined through `adoptJoinedGroup`,
|
|
1190
|
+
// which defers it (transient) until the restore released the group (then already-member, or adopted). A
|
|
1191
|
+
// failed restore still being taken back (`ending`) holds nothing any more.
|
|
1192
|
+
const restore = restoring.get(groupId);
|
|
1193
|
+
if (restore !== undefined)
|
|
1194
|
+
return !restore.ending;
|
|
1195
|
+
if (abandoned.has(groupId))
|
|
1196
|
+
return false;
|
|
1197
|
+
const cached = runtimes.get(groupId);
|
|
1198
|
+
if (cached !== undefined && !cached.disposed)
|
|
1199
|
+
return true;
|
|
1200
|
+
const groupStore = groupStoreOf(groupId);
|
|
1201
|
+
// AC-MX-2: an unfinished restore an earlier client left is resumed — and counts as held, like one in flight.
|
|
1202
|
+
// Checked before waiting on a load in flight: a load of such a group waits for the resume.
|
|
1203
|
+
if (await restorePending(groupId, groupStore)) {
|
|
1204
|
+
resumeRestore(groupId);
|
|
1205
|
+
return true;
|
|
1206
|
+
}
|
|
1207
|
+
const inFlight = loading.get(groupId);
|
|
1208
|
+
if (inFlight !== undefined) {
|
|
1209
|
+
assertNotWithinOwnLoad(groupId, 'hasGroup');
|
|
1210
|
+
if ((await inFlight.catch(() => undefined)) !== undefined)
|
|
1211
|
+
return true;
|
|
1212
|
+
}
|
|
1213
|
+
return (await guard.call(groupId, 'store', () => readGroupState(groupStore.artifact('clientState')))) !== undefined;
|
|
1214
|
+
}
|
|
1215
|
+
async function getGroupOp(groupId) {
|
|
1216
|
+
if (typeof groupId !== 'string' || groupId.length === 0)
|
|
1217
|
+
throw new MarmotError('invalid-options', 'groups.get: groupId must be a non-empty string');
|
|
1218
|
+
return (await loadRuntime(groupId))?.handle;
|
|
1219
|
+
}
|
|
1220
|
+
/**
|
|
1221
|
+
* The group's one runtime: the cached one, else the single in-flight load
|
|
1222
|
+
* shared by every concurrent `groups.get`/`groups.list` for that id — two
|
|
1223
|
+
* loads racing would each build an engine, resume pending intents and
|
|
1224
|
+
* start a sync loop over the same stores.
|
|
1225
|
+
*/
|
|
1226
|
+
function loadRuntime(groupId) {
|
|
1227
|
+
// AC-MX-2: a group being restored (or resumed) is not handed out before its self-update confirmed — a load
|
|
1228
|
+
// waits for the restore (and finds nothing when it failed); never from inside a consumer surface (awaitRestore).
|
|
1229
|
+
const restore = restoring.get(groupId);
|
|
1230
|
+
if (restore !== undefined)
|
|
1231
|
+
return awaitRestore(groupId, restore);
|
|
1232
|
+
if (abandoned.has(groupId))
|
|
1233
|
+
return Promise.resolve(undefined);
|
|
1234
|
+
const cached = runtimes.get(groupId);
|
|
1235
|
+
if (cached !== undefined && !cached.disposed)
|
|
1236
|
+
return Promise.resolve(cached);
|
|
1237
|
+
const inFlight = loading.get(groupId);
|
|
1238
|
+
if (inFlight !== undefined) {
|
|
1239
|
+
// A consumer store read made by this very load, calling back into groups.get for the same group,
|
|
1240
|
+
// would await the promise that is waiting on it: reject instead of deadlocking.
|
|
1241
|
+
if (guard.active(groupId)) {
|
|
1242
|
+
return Promise.reject(new MarmotError('reentrant', `groups.get(${JSON.stringify(groupId)}): called from inside a consumer surface of that group's own load`, { details: { scope: groupId, operation: 'groups.get' } }));
|
|
1243
|
+
}
|
|
1244
|
+
return inFlight;
|
|
1245
|
+
}
|
|
1246
|
+
// Started on a later microtask so the load is registered in `loading` before its first consumer
|
|
1247
|
+
// store call runs: a call back into groups.get for this group then joins (or is rejected), never forks.
|
|
1248
|
+
// AC-RE-1: ONLY the fire-and-forget work below —
|
|
1249
|
+
// `resumePendingInBackground` through `startGroupSync`/`scheduleAwaitingFenceCleanup` — is background
|
|
1250
|
+
// work this load schedules, not something the caller awaits, so `guard.detached` wraps THAT. The store
|
|
1251
|
+
// reads and `buildEngine` ABOVE it are different: `groups.get`/`DeviceGroupPort.snapshot` (reached from
|
|
1252
|
+
// inside a `devices.*` operation's live DEVICE_SCOPE frame) directly AWAITS this whole function, so a
|
|
1253
|
+
// consumer store or engine seam that itself awaits a `devices.*` call during THESE reads must still be
|
|
1254
|
+
// rejected `reentrant` by `assertNotInDeviceOperation` (E1, device-deps.ts:123) — not detached, or such a
|
|
1255
|
+
// call would wrongly queue behind, and deadlock against, the very operation awaiting this load.
|
|
1256
|
+
const load = Promise.resolve()
|
|
1257
|
+
.then(async () => {
|
|
1258
|
+
const groupStore = groupStoreOf(groupId);
|
|
1259
|
+
// AC-MX-2: an unfinished restore an earlier client left is resumed, never loaded as held before its
|
|
1260
|
+
// self-update confirmed (nor discarded: that self-update may be on the relays already) — this load waits
|
|
1261
|
+
// for it like for a restore in flight.
|
|
1262
|
+
if (await restorePending(groupId, groupStore)) {
|
|
1263
|
+
const resumed = resumeRestore(groupId);
|
|
1264
|
+
// No longer a load: a later caller finds the resume's reservation (a purge never waits on this one).
|
|
1265
|
+
if (loading.get(groupId) === load)
|
|
1266
|
+
loading.delete(groupId);
|
|
1267
|
+
if (resumed !== undefined)
|
|
1268
|
+
return awaitRestore(groupId, resumed);
|
|
1269
|
+
// Not resumable here: the client is ending, or this client tore the group down.
|
|
1270
|
+
return undefined;
|
|
1271
|
+
}
|
|
1272
|
+
const rt = await readLoadedRuntime(groupId, groupStore);
|
|
1273
|
+
if (rt === undefined)
|
|
1274
|
+
return undefined;
|
|
1275
|
+
// Scheduled, not awaited: AC-PIPE-1's crash-recovery flow resumes in the background while the caller
|
|
1276
|
+
// already holds the handle; a transient whole-run failure is retried on the backoff. Detached (AC-RE-1):
|
|
1277
|
+
// this whole block never runs on the caller's own await chain, so none of it may
|
|
1278
|
+
// inherit whatever surface frame (e.g. a `devices.*` op's DEVICE_SCOPE) was live at the scheduling
|
|
1279
|
+
// call site above.
|
|
1280
|
+
guard.detached(() => startLoadedRuntime(rt));
|
|
1281
|
+
return rt;
|
|
1282
|
+
})
|
|
1283
|
+
.finally(() => {
|
|
1284
|
+
if (loading.get(groupId) === load)
|
|
1285
|
+
loading.delete(groupId);
|
|
1286
|
+
});
|
|
1287
|
+
loading.set(groupId, load);
|
|
1288
|
+
return load;
|
|
1289
|
+
}
|
|
1290
|
+
/**
|
|
1291
|
+
* AC-MX-2: a restore's handle as `groups.get` waits for it. That can take as long as the group's first sync
|
|
1292
|
+
* (offline: indefinitely), so it is never awaited from inside a consumer surface some group's or the account's
|
|
1293
|
+
* queue is waiting on (an `onConfirm` hook — of the restore's own self-update, where the wait could never end —, a
|
|
1294
|
+
* store call, an inbox handler): rejected `reentrant` instead of holding that queue.
|
|
1295
|
+
*/
|
|
1296
|
+
function awaitRestore(groupId, restore) {
|
|
1297
|
+
const frame = guard.withinConsumerScope();
|
|
1298
|
+
if (frame !== undefined) {
|
|
1299
|
+
return Promise.reject(new MarmotError('reentrant', `groups.get(${JSON.stringify(groupId)}): the group is being restored; not awaited from inside a consumer surface (${frame.surface} on ${JSON.stringify(frame.scope)})`, {
|
|
1300
|
+
details: { scope: frame.scope, operation: 'groups.get', surface: frame.surface },
|
|
1301
|
+
}));
|
|
1302
|
+
}
|
|
1303
|
+
return restore.settled;
|
|
1304
|
+
}
|
|
1305
|
+
/**
|
|
1306
|
+
* A stored group's runtime as a load builds it (`loaded`: the AC-PIPE-15 send gate closed until its first sync):
|
|
1307
|
+
* its state, index entry, rewind window, fence marker, revisable removal and ratchet-skip marker, read with the
|
|
1308
|
+
* state they describe. `undefined` when no state is stored. Nothing is started ({@link startLoadedRuntime}).
|
|
1309
|
+
*/
|
|
1310
|
+
async function readLoadedRuntime(groupId, groupStore) {
|
|
1311
|
+
const state = await guard.call(groupId, 'store', () => readGroupState(groupStore.artifact('clientState')));
|
|
1312
|
+
if (state === undefined)
|
|
1313
|
+
return undefined;
|
|
1314
|
+
// Framed on the group scope too, so a re-entrant groups.get for this group from inside the read is detected.
|
|
1315
|
+
const indexEntry = await guard.call(groupId, 'store', () => guard.call(ACCOUNT_SCOPE, 'store', () => readGroupIndexEntry(ctx.stores.account.extension(GROUP_INDEX_EXTENSION), groupId)));
|
|
1316
|
+
// S10: the rewind window and the fence marker, read with the state they describe.
|
|
1317
|
+
const stored = await guard.call(groupId, 'store', () => readWindow(groupStore.artifact('rewindTree')));
|
|
1318
|
+
// Epic marmot-invitation-outcomes S1 (A-7, AC-FENCE-4): a malformed present marker fails closed.
|
|
1319
|
+
const marker = await guard.call(groupId, 'store', () => loadFenceMarker(groupStore.extension(FENCE_EXTENSION)));
|
|
1320
|
+
const disbanded = await guard.call(groupId, 'store', () => readDisband(groupStore.extension(DISBAND_EXTENSION)));
|
|
1321
|
+
// S20: a stored tombstone keeps its revisable removal (the retained tip and history are the removal's parent).
|
|
1322
|
+
const storedRemoval = await guard.call(groupId, 'store', () => readRemoval(groupStore.extension(REMOVAL_EXTENSION)));
|
|
1323
|
+
const removal = belongsTo(storedRemoval, state) ? storedRemoval : undefined;
|
|
1324
|
+
// AC-RATCH-1: the last post-load skip, read with the state it describes.
|
|
1325
|
+
const skipMarker = await guard.call(groupId, 'store', () => readRatchetSkipMarker(groupStore.extension(RATCHET_SKIP_EXTENSION)));
|
|
1326
|
+
// The held opaque candidates and `U` (client/opaque-candidates.ts), read with the state they describe. A record that
|
|
1327
|
+
// is corrupt — unreadable here, or refused by the engine — fences the group (fail closed), unless it is fenced already.
|
|
1328
|
+
const opaque = await loadOpaque(guard, groupId, groupStore);
|
|
1329
|
+
// Epic marmot-leaf-backup-pre-publish S2: the pending adoption's marker and the parked pending node, read with the state they describe.
|
|
1330
|
+
const pendingRestore = await guard.call(groupId, 'store', () => readPendingRestore(groupStore));
|
|
1331
|
+
const history = restoreHistory(stored, removal === undefined ? state : copyOf(removal.record.parentState));
|
|
1332
|
+
const built = await buildEngineFailClosed(ctx, ciphersuiteCache, state, () => settleCheck(groupId), history, removal?.record, opaque.record);
|
|
1333
|
+
const engine = built.engine;
|
|
1334
|
+
const corrupt = opaque.corrupt ?? built.corrupt;
|
|
1335
|
+
if (ctx.isDisposed()) {
|
|
1336
|
+
engine.dispose();
|
|
1337
|
+
throw new MarmotError('disposed', 'groups.get: client was disposed while loading the group');
|
|
1338
|
+
}
|
|
1339
|
+
// A-7: a fence this session already raised for the group (its marker write may have failed), else the marker, else
|
|
1340
|
+
// a malformed present marker, else a corrupt held-candidate record.
|
|
1341
|
+
const fenceOnLoad = availabilityClient.sessionFences.get(groupId) ??
|
|
1342
|
+
(marker.kind === 'valid'
|
|
1343
|
+
? recordedFencingError(marker.record)
|
|
1344
|
+
: marker.kind === 'malformed'
|
|
1345
|
+
? new InvalidStoredEvidenceError(`group ${groupId}: the stored fence marker (${FENCE_EXTENSION}) is malformed`)
|
|
1346
|
+
: corrupt);
|
|
1347
|
+
return makeRuntime(groupId, groupStore, engine, indexEntry?.createdAt, {
|
|
1348
|
+
loaded: true,
|
|
1349
|
+
ratchetSkipArmed: ratchetSkipArmed(skipMarker, state.groupContext.epoch, tagOf(state), nextOwnApplicationGeneration(state)),
|
|
1350
|
+
window: windowFor(stored, state),
|
|
1351
|
+
...(removal === undefined ? {} : { removal }),
|
|
1352
|
+
...(corrupt !== undefined || opaque.record === undefined ? {} : { opaque: opaque.record }),
|
|
1353
|
+
...(fenceOnLoad === undefined ? {} : { fence: fenceOnLoad }),
|
|
1354
|
+
...(disbanded === undefined ? {} : { disbanded }),
|
|
1355
|
+
...pendingRestore,
|
|
1356
|
+
});
|
|
1357
|
+
}
|
|
1358
|
+
/** A loaded runtime's background work (call detached — AC-RE-1). */
|
|
1359
|
+
function startLoadedRuntime(rt) {
|
|
1360
|
+
resumePendingInBackground(rt);
|
|
1361
|
+
// S15 (AC-INVITE-4): a deferral deadline that passed while the group was not loaded is honoured now.
|
|
1362
|
+
scheduleProposalDeadlineCheck(rt);
|
|
1363
|
+
// S20: a revisable removal re-arms its settle (its window runs on the client clock across the reload).
|
|
1364
|
+
scheduleRemovalSettle(rt);
|
|
1365
|
+
// S9 (Z2): SelfRemoves staged by a persisted ingest whose commit was never recorded — re-elected at rest.
|
|
1366
|
+
const election = electionAtRest(rt.engine.state, rt.leaverPredicate);
|
|
1367
|
+
if (election !== undefined)
|
|
1368
|
+
scheduleSelfRemoveCommit(rt, election);
|
|
1369
|
+
// S5: Welcome deliveries of this group interrupted by the reload, and rumors still queued.
|
|
1370
|
+
// Reconciled with the stored window first (a rewind whose withdrawal a crash lost), then resumed.
|
|
1371
|
+
void reconcileInvitesWithWindow(rt, rt.window).then(() => invitations.resumeGroup(rt.groupId));
|
|
1372
|
+
rt.scheduleQueueDrain();
|
|
1373
|
+
// S10: history backend updates a crash left undelivered (their transitions are durable) go out now.
|
|
1374
|
+
void scheduleHistoryDrain(rt);
|
|
1375
|
+
// AC-HIST-4: a fenced group does not sync (nothing it fetches can be processed). AC-PIPE-15:
|
|
1376
|
+
// awaiting-sync intents a crash left behind the fence marker fail `fenced` now (never encrypted).
|
|
1377
|
+
// A disbanded group does not sync either: the group is over (client/group-disband.ts).
|
|
1378
|
+
if (rt.fence === undefined) {
|
|
1379
|
+
if (rt.disbanded === undefined)
|
|
1380
|
+
startGroupSync(rt);
|
|
1381
|
+
}
|
|
1382
|
+
else
|
|
1383
|
+
scheduleAwaitingFenceCleanup(rt);
|
|
1384
|
+
}
|
|
1385
|
+
/** AC-MX-2 / AC-RE-2: refused from inside a live group-scope `onConfirm` frame — that group's confirmation transaction waits on it. */
|
|
1386
|
+
function assertNotInGroupOnConfirm(operation) {
|
|
1387
|
+
const frame = guard.withinGroupScope('onConfirm');
|
|
1388
|
+
if (frame === undefined)
|
|
1389
|
+
return;
|
|
1390
|
+
throw new MarmotError('reentrant', `${operation}: re-entrant call from inside group ${JSON.stringify(frame.scope)}'s onConfirm surface`, {
|
|
1391
|
+
details: { scope: frame.scope, operation, surface: frame.surface },
|
|
1392
|
+
});
|
|
1393
|
+
}
|
|
1394
|
+
/**
|
|
1395
|
+
* AC-MX-2: `groups.restore`'s pre-adoption purge check — like a Welcome join, an interrupted purge is finished
|
|
1396
|
+
* first (`finishPurge`), then whether a group this client tore down refuses the snapshot's `epoch`
|
|
1397
|
+
* (`refusesJoin`). Runs flagged `checkingPurge` (a purge neither ends nor waits for the restore meanwhile: the
|
|
1398
|
+
* check may be waiting on that very purge), and repeats until no purge of the group is in flight at its end and
|
|
1399
|
+
* none started during the watermark read (one that started and finished meanwhile may not be in that read), so
|
|
1400
|
+
* the adoption that follows never races a purge's clear and reads the watermark that purge committed.
|
|
1401
|
+
*/
|
|
1402
|
+
async function refusedAfterPurges(groupId, entry, epoch) {
|
|
1403
|
+
entry.checkingPurge = true;
|
|
1404
|
+
try {
|
|
1405
|
+
for (;;) {
|
|
1406
|
+
if (ctx.finishPurgeBeforeJoin)
|
|
1407
|
+
await teardown().finishPurge(groupId);
|
|
1408
|
+
const purging = purgesInFlight.get(groupId);
|
|
1409
|
+
if (purging !== undefined) {
|
|
1410
|
+
await purging.catch(() => undefined);
|
|
1411
|
+
continue;
|
|
1412
|
+
}
|
|
1413
|
+
const generation = purgeGenerations.get(groupId);
|
|
1414
|
+
const refused = await teardown().refusesJoin(groupId, epoch);
|
|
1415
|
+
if (!purgesInFlight.has(groupId) && purgeGenerations.get(groupId) === generation)
|
|
1416
|
+
return refused;
|
|
1417
|
+
}
|
|
1418
|
+
}
|
|
1419
|
+
finally {
|
|
1420
|
+
entry.checkingPurge = false;
|
|
1421
|
+
}
|
|
1422
|
+
}
|
|
1423
|
+
/** What a restore of `groupId` rejects with when its runtime ended for good ({@link RuntimeEnd}). */
|
|
1424
|
+
function restoreEndError(groupId, end) {
|
|
1425
|
+
if (end.reason === 'fenced')
|
|
1426
|
+
return fencedError(groupId, 'groups.restore', end.cause);
|
|
1427
|
+
if (end.reason === 'disposed')
|
|
1428
|
+
return new MarmotError('disposed', 'groups.restore: client was disposed while restoring the group');
|
|
1429
|
+
if (end.reason === 'no-lease') {
|
|
1430
|
+
return new MarmotError('no-lease', `groups.restore: the stores lost their lease while restoring group ${groupId}; the next lease holder resumes it`, { cause: end.cause, details: { groupId } });
|
|
1431
|
+
}
|
|
1432
|
+
return new MarmotError('not-a-member', `groups.restore: group ${groupId} was torn down while being restored`, { details: { groupId } });
|
|
1433
|
+
}
|
|
1434
|
+
/**
|
|
1435
|
+
* AC-MX-2 `groups.restore(groupId, snapshot)`. Refused `reentrant` from inside an account-scope surface, a
|
|
1436
|
+
* group's `onConfirm` hook, or any consumer surface of `groupId`'s own scope (e.g. a store read of that group's
|
|
1437
|
+
* load in flight, which the restore would otherwise wait on while the load waits on the read). Checked before anything is written, in this order: the snapshot itself
|
|
1438
|
+
* (`envelope-mismatch`, pure — a malformed payload included: a state of another group than the payload names, a
|
|
1439
|
+
* rewind window with a cycle, a dangling link, a node not connected to the tip's tree, a node below the horizon or a node without the serialized
|
|
1440
|
+
* state or commit restoring it needs, or naming a sender for a link other than its commit's authenticated sender), that its serialized MLS state is
|
|
1441
|
+
* `groupId`'s (`invalid-options`, AC-BAK-10), that the group is not held or being
|
|
1442
|
+
* restored (`already-member`), then — like a Welcome join — an interrupted purge is finished and a group this
|
|
1443
|
+
* client tore down only comes back with a newer state (`not-a-member`). The snapshot is then adopted as a LOADED
|
|
1444
|
+
* state with the rewind window it carries (the AC-PIPE-15 send gate closed: its first sync replays every commit
|
|
1445
|
+
* since, rewinding within that window when a competing commit won convergence; the AC-RATCH-1 skip armed), and
|
|
1446
|
+
* once the gate opens a self-update is committed. The handle resolves only once that self-update confirmed.
|
|
1447
|
+
*
|
|
1448
|
+
* Every wait races the runtime's terminal signal (GroupRuntime.terminalWatchers): a fence (`fenced`), a teardown
|
|
1449
|
+
* purge (`not-a-member`) or dispose (`disposed`) ends the restore at once. Any failure after the adoption removes
|
|
1450
|
+
* the adopted group again (all-or-nothing) — on dispose without waiting for the group's queue: the adoption is
|
|
1451
|
+
* marked unfinished until its self-update confirms, and a later client over the same stores discards a group so
|
|
1452
|
+
* marked before reading it as held (also after a crash mid-restore).
|
|
1453
|
+
*/
|
|
1454
|
+
async function restoreGroupOp(groupId, snapshot) {
|
|
1455
|
+
guard.assertNotReentrant(ACCOUNT_SCOPE, 'groups.restore');
|
|
1456
|
+
assertNotInGroupOnConfirm('groups.restore');
|
|
1457
|
+
if (typeof groupId !== 'string' || groupId.length === 0)
|
|
1458
|
+
throw new MarmotError('invalid-options', 'groups.restore: groupId must be a non-empty string');
|
|
1459
|
+
// Nor from inside any consumer surface of this group's own scope: whatever invoked that surface — a load
|
|
1460
|
+
// (`groups.get`), a Welcome adoption, a purge, another restore — is what the checks below would wait on.
|
|
1461
|
+
guard.assertNotReentrant(groupId, 'groups.restore');
|
|
1462
|
+
// Opened in full before anything is written: a payload whose serialized state is of another group than it
|
|
1463
|
+
// names, or whose rewind window is no tree below its tip within the horizon or lacks a node's state or commit, is
|
|
1464
|
+
// `envelope-mismatch` (malformed) — as is a window that would still restore to no engine history: the history
|
|
1465
|
+
// is built here, before anything is written, so a restore never adopts a state without the window it advertises.
|
|
1466
|
+
const opened = openGroupSnapshot(snapshot);
|
|
1467
|
+
let content = opened;
|
|
1468
|
+
let history = snapshotHistory(content);
|
|
1469
|
+
// AC-BAK-10: the serialized MLS state's own group id — the payload's, as opened — decides which group this is.
|
|
1470
|
+
const snapshotGroupId = getGroupIdHex(content.state);
|
|
1471
|
+
if (snapshotGroupId !== groupId) {
|
|
1472
|
+
throw new MarmotError('invalid-options', `groups.restore: the snapshot is of group ${snapshotGroupId}, not ${groupId}`, { details: { groupId, snapshotGroupId } });
|
|
1473
|
+
}
|
|
1474
|
+
const alreadyMember = () => new MarmotError('already-member', `groups.restore: group ${groupId} is already held by this client`, { details: { groupId } });
|
|
1475
|
+
const cached = runtimes.get(groupId);
|
|
1476
|
+
const pending = restoring.get(groupId);
|
|
1477
|
+
// A failed restore still being taken back: transient — the group is free once its adoption was removed.
|
|
1478
|
+
if (pending?.ending === true) {
|
|
1479
|
+
throw new MarmotError('commit-pending', `groups.restore: an earlier restore of group ${groupId} failed and is still being taken back; retry once it was`, {
|
|
1480
|
+
details: { groupId, reason: 'restore-ending', retryable: true },
|
|
1481
|
+
});
|
|
1482
|
+
}
|
|
1483
|
+
if (pending !== undefined || (cached !== undefined && !cached.disposed))
|
|
1484
|
+
throw alreadyMember();
|
|
1485
|
+
// Reserved synchronously: a second restore (or a load, or a Welcome adoption) of this group from here on joins or refuses this one.
|
|
1486
|
+
const { entry, settle, end, ended } = reserveRestore(groupId);
|
|
1487
|
+
let rt;
|
|
1488
|
+
/** Whether this call wrote the group's restore record, and whether the group-store adoption was written. */
|
|
1489
|
+
let announced = false;
|
|
1490
|
+
let adopted = false;
|
|
1491
|
+
let resumeEarlier = false;
|
|
1492
|
+
try {
|
|
1493
|
+
// Every window link's sender re-derived from its commit at its parent, never the snapshot's stored claim
|
|
1494
|
+
// (ADR-016 D4a): a mismatching claim refuses the snapshot (`envelope-mismatch`, malformed) before anything is
|
|
1495
|
+
// written, and only derived senders reach the engine and the window store. After the synchronous reservation
|
|
1496
|
+
// (the re-resolution is async), so a `groups.get` racing this call waits on it; the history is rebuilt over them.
|
|
1497
|
+
if (history !== undefined) {
|
|
1498
|
+
content = await authenticateWindowSenders(opened, await ciphersuiteImplFor(ciphersuiteCache, opened.state.groupContext.cipherSuite), effectiveSeams(ctx, () => opened.state.ratchetTree).clientConfig);
|
|
1499
|
+
history = snapshotHistory(content);
|
|
1500
|
+
}
|
|
1501
|
+
const groupStore = groupStoreOf(groupId);
|
|
1502
|
+
// An unfinished restore an earlier client left (disposed or crashed mid-restore) is resumed, never replaced
|
|
1503
|
+
// — its self-update may be on the relays already, and only these stores can confirm it — and this call is
|
|
1504
|
+
// refused like any second restore of a group being restored. Checked before waiting on a load in flight:
|
|
1505
|
+
// that load, finding the mark, waits on this reservation (handed over to the resume below).
|
|
1506
|
+
if (await restorePending(groupId, groupStore)) {
|
|
1507
|
+
resumeEarlier = true;
|
|
1508
|
+
throw alreadyMember();
|
|
1509
|
+
}
|
|
1510
|
+
// A load already in flight (a `groups.get` racing this call) decides whether the group is held.
|
|
1511
|
+
const inFlight = loading.get(groupId);
|
|
1512
|
+
if (inFlight !== undefined && (await inFlight.catch(() => undefined)) !== undefined)
|
|
1513
|
+
throw alreadyMember();
|
|
1514
|
+
assertNotEnded(entry);
|
|
1515
|
+
if (await refusedAfterPurges(groupId, entry, content.state.groupContext.epoch)) {
|
|
1516
|
+
throw new MarmotError('not-a-member', `groups.restore: group ${groupId} was left or abandoned by this client; this snapshot would bring its old state back`, { details: { groupId } });
|
|
1517
|
+
}
|
|
1518
|
+
// A purge from here on ends this restore (`not-a-member`) and waits for it; every step below that does not
|
|
1519
|
+
// race `ended` re-checks.
|
|
1520
|
+
assertNotEnded(entry);
|
|
1521
|
+
// The window the snapshot carries (AC-RACE-1: what a rewind below the snapshot's tip needs), else the tip alone.
|
|
1522
|
+
const window = windowFor(content.window, content.state);
|
|
1523
|
+
const pendingMarker = content.pending === undefined ? undefined : { digest: content.pending.digest, parentTag: content.pending.parentTag, tipTag: tagOf(content.state) };
|
|
1524
|
+
// Discoverable before anything of the group is written: the startup scan walks the group index, written only
|
|
1525
|
+
// after the group store, so a crash in between would orphan the snapshot's key material. Three separate
|
|
1526
|
+
// transactions, account → group → account (never nested: group → account is the lock order) — every crash
|
|
1527
|
+
// point leaves what the next client's scan settles (group-records.ts `RESTORING_EXTENSION`).
|
|
1528
|
+
await guard.call(ACCOUNT_SCOPE, 'store', () => ctx.stores.account.transaction((tx) => writeRestoringRecord(tx.extension(RESTORING_EXTENSION), groupId, ctx.clock.now())));
|
|
1529
|
+
announced = true;
|
|
1530
|
+
assertNotEnded(entry);
|
|
1531
|
+
const wrote = await guard.call(groupId, 'store', () => groupStore.transaction(async (tx) => {
|
|
1532
|
+
if ((await readGroupState(tx.artifact('clientState'))) !== undefined)
|
|
1533
|
+
return false;
|
|
1534
|
+
// Marked unfinished until its self-update confirms (cleared in that confirmation's transaction).
|
|
1535
|
+
await writeRestorePending(tx.extension(RESTORE_PENDING_EXTENSION), ctx.clock.now());
|
|
1536
|
+
await writeGroupState(tx.artifact('clientState'), content.state);
|
|
1537
|
+
await writeWindow(tx.artifact('rewindTree'), window);
|
|
1538
|
+
// Epic marmot-invitation-outcomes S4 (SC-4): the snapshot's admission lineage, kept only where its own window
|
|
1539
|
+
// proves it; every other record restores as a gap (`restore`) — never settled until a transition re-proves it.
|
|
1540
|
+
await replaceLineageRecords(tx.extension(ADMISSION_LINEAGE_EXTENSION), validateRestoredLineage(content.lineage ?? [], window, content.state));
|
|
1541
|
+
// The restored engine is built with no opaque record (nothing was handed to the snapshot's state yet); a
|
|
1542
|
+
// `marmot.opaque` left by an earlier state of this group belongs to that state, so it goes in this transaction:
|
|
1543
|
+
// a reload must not rehydrate candidates that describe another history.
|
|
1544
|
+
await clearOpaque(tx.extension(OPAQUE_EXTENSION));
|
|
1545
|
+
// Epic marmot-leaf-backup-pre-publish S2: a record an earlier state of this group left never survives a new
|
|
1546
|
+
// adoption; a pending-tagged snapshot then writes its own marker (cleared once its commit was seen or the
|
|
1547
|
+
// first sync's resolution step ran — client/pending-leaf.ts).
|
|
1548
|
+
await clearPendingLeaf(tx.extension(PENDING_LEAF_EXTENSION));
|
|
1549
|
+
await clearPendingCommit(tx.extension(PENDING_COMMIT_EXTENSION));
|
|
1550
|
+
if (pendingMarker !== undefined)
|
|
1551
|
+
await writePendingCommit(tx.extension(PENDING_COMMIT_EXTENSION), pendingMarker);
|
|
1552
|
+
if (content.since !== undefined)
|
|
1553
|
+
await writeIngestCursor(tx.artifact('ingestCursor'), { since: content.since });
|
|
1554
|
+
return true;
|
|
1555
|
+
}));
|
|
1556
|
+
if (!wrote)
|
|
1557
|
+
throw alreadyMember();
|
|
1558
|
+
adopted = true;
|
|
1559
|
+
assertNotEnded(entry);
|
|
1560
|
+
const createdAt = content.createdAt ?? ctx.clock.now();
|
|
1561
|
+
// The index entry and the record's removal together: from here the index alone makes the adoption found.
|
|
1562
|
+
await guard.call(ACCOUNT_SCOPE, 'store', () => ctx.stores.account.transaction(async (tx) => {
|
|
1563
|
+
const index = tx.extension(GROUP_INDEX_EXTENSION);
|
|
1564
|
+
if ((await readGroupIndexEntry(index, groupId)) === undefined)
|
|
1565
|
+
await writeGroupIndexEntry(index, groupId, { createdAt });
|
|
1566
|
+
await clearRestoringRecord(tx.extension(RESTORING_EXTENSION), groupId);
|
|
1567
|
+
}));
|
|
1568
|
+
announced = false;
|
|
1569
|
+
// Synchronously with the clear below: a purge that started meanwhile marked the group abandoned — kept so.
|
|
1570
|
+
assertNotEnded(entry);
|
|
1571
|
+
abandoned.delete(groupId);
|
|
1572
|
+
if (ctx.isDisposed())
|
|
1573
|
+
throw new MarmotError('disposed', 'groups.restore: client was disposed while restoring the group');
|
|
1574
|
+
const engine = await buildEngine(ctx, ciphersuiteCache, content.state, () => settleCheck(groupId), history);
|
|
1575
|
+
if (ctx.isDisposed()) {
|
|
1576
|
+
engine.dispose();
|
|
1577
|
+
throw new MarmotError('disposed', 'groups.restore: client was disposed while restoring the group');
|
|
1578
|
+
}
|
|
1579
|
+
// A load, not a join: the snapshot may be older than what the relays hold (AC-PIPE-15, AC-RATCH-1).
|
|
1580
|
+
rt = makeRuntime(groupId, groupStore, engine, createdAt, { loaded: true, adoption: true, window, ...(pendingMarker === undefined ? {} : { pendingCommit: pendingMarker }) });
|
|
1581
|
+
entry.runtime = rt;
|
|
1582
|
+
const runtime = rt;
|
|
1583
|
+
runtime.terminalWatchers.add((ending) => end(restoreEndError(groupId, ending)));
|
|
1584
|
+
assertNotEnded(entry);
|
|
1585
|
+
guard.detached(() => startGroupSync(runtime));
|
|
1586
|
+
// The first sync: every relay answered (or was excluded as silent) and every commit since was replayed.
|
|
1587
|
+
await Promise.race([new Promise((resolve, reject) => (runtime.sendGateOpen ? resolve() : runtime.syncWaiters.add({ resolve, reject }))), ended]);
|
|
1588
|
+
await resolvePendingRestore(runtime, ended);
|
|
1589
|
+
await commitRestoreSelfUpdate(runtime, ended);
|
|
1590
|
+
}
|
|
1591
|
+
catch (error) {
|
|
1592
|
+
// Settled at once: a `groups.get` waiting on this restore learns "not held" without waiting for the
|
|
1593
|
+
// retirement below, which queues behind an operation still in flight (a slow signer or publish). The
|
|
1594
|
+
// reservation itself is held until the stores are cleared: released earlier, a Welcome would be adopted
|
|
1595
|
+
// (or a load would read the restored state back) and then be wiped by the clear. Except on dispose: the
|
|
1596
|
+
// client is ending and that operation may never finish (a publish no relay answers), so the restore
|
|
1597
|
+
// rejects at once; the stores are left to the unfinished-restore mark, which the next client over them
|
|
1598
|
+
// discards before it reads the group as held (see `undoRestore`).
|
|
1599
|
+
if (resumeEarlier) {
|
|
1600
|
+
// Whoever waits on this reservation (a load that found the mark) follows the resume instead.
|
|
1601
|
+
releaseRestore(groupId, entry);
|
|
1602
|
+
settle(resumeRestore(groupId)?.settled);
|
|
1603
|
+
throw error;
|
|
1604
|
+
}
|
|
1605
|
+
settle(undefined);
|
|
1606
|
+
if (adopted)
|
|
1607
|
+
await Promise.race([undoRestore(groupId, rt), clientDisposed]);
|
|
1608
|
+
else if (announced && !ctx.isDisposed()) {
|
|
1609
|
+
// Nothing of the group written by this call (refused, or ended before its group write): only the record goes
|
|
1610
|
+
// — while the reservation is held, so no restore of the group here writes one meanwhile. Left on a failure
|
|
1611
|
+
// (or once disposed: a new client may own the record by now) for the next scan, which drops it alike.
|
|
1612
|
+
await guard.call(ACCOUNT_SCOPE, 'store', () => ctx.stores.account.transaction((tx) => clearRestoringRecord(tx.extension(RESTORING_EXTENSION), groupId))).catch(() => undefined);
|
|
1613
|
+
}
|
|
1614
|
+
releaseRestore(groupId, entry);
|
|
1615
|
+
// Ended by a teardown purge (which waited for the release above): rejects once that purge removed the group —
|
|
1616
|
+
// stores, then index entry. The purge, not this take-back, clears it: it commits the purged epoch's watermark
|
|
1617
|
+
// from the stored state first.
|
|
1618
|
+
if (adopted && abandoned.has(groupId) && !ctx.isDisposed())
|
|
1619
|
+
await Promise.race([teardown().finishPurge(groupId).catch(() => undefined), clientDisposed]);
|
|
1620
|
+
// A fence ends the restore `fenced` whichever wait noticed it first.
|
|
1621
|
+
if (rt?.fence !== undefined && !isMarmotError(error, 'disposed'))
|
|
1622
|
+
throw fencedError(groupId, 'groups.restore', rt.fence);
|
|
1623
|
+
throw error;
|
|
1624
|
+
}
|
|
1625
|
+
releaseRestore(groupId, entry);
|
|
1626
|
+
settle(rt);
|
|
1627
|
+
// AC-REKEY-11: a restored leaf may sit on a retired key (a backup older than a rotation).
|
|
1628
|
+
scheduleRekeyFollowUp(rt);
|
|
1629
|
+
await oweRestoreRotation(groupId);
|
|
1630
|
+
return rt.handle;
|
|
1631
|
+
}
|
|
1632
|
+
/**
|
|
1633
|
+
* Epic `marmot-leaf-backup-pre-publish` S2 (Design Decision 7): the resolution of a pending-tagged snapshot's adoption,
|
|
1634
|
+
* run once the restore's first sync is over and before its self-update is committed. On the group's queue, gated on
|
|
1635
|
+
* the persisted tip and idempotent (client/pending-leaf.ts `resolvePendingCommit`); when it rewound the group onto the
|
|
1636
|
+
* pending commit's parent, the tree-fed convergence pass over the rebuilt tree is the structural replay, so a
|
|
1637
|
+
* competitor of the rewound-away tip that the sync already ledgered is weighed from the parent (no relay re-fetch).
|
|
1638
|
+
* A no-op for every group that carries no marker.
|
|
1639
|
+
*/
|
|
1640
|
+
async function resolvePendingRestore(rt, ended) {
|
|
1641
|
+
if (rt.pendingCommit === undefined)
|
|
1642
|
+
return;
|
|
1643
|
+
await Promise.race([
|
|
1644
|
+
rt.run('restorePending', async (rtc) => {
|
|
1645
|
+
if (await resolvePendingCommit(rtc))
|
|
1646
|
+
await treeFedPass(rtc);
|
|
1647
|
+
}),
|
|
1648
|
+
ended,
|
|
1649
|
+
]);
|
|
1650
|
+
}
|
|
1651
|
+
/**
|
|
1652
|
+
* AC-MX-2: resumes `groupId`'s unfinished restore — one an earlier client over these stores adopted but whose
|
|
1653
|
+
* self-update it never saw confirmed (disposed or crashed). Returns the reservation (an existing one when the
|
|
1654
|
+
* group is already being restored here), or `undefined` when nothing is resumed: the client is disposed, this
|
|
1655
|
+
* client tore the group down, or it holds the group already.
|
|
1656
|
+
*
|
|
1657
|
+
* Started by whatever first finds the mark — the startup scan ({@link GroupRegistry.resumeUnfinishedRestores},
|
|
1658
|
+
* on client creation and every inbox pass), `groups.get`/`list`, `hasGroup`, a Welcome adoption, `groups.restore`
|
|
1659
|
+
* — never by the app re-supplying the snapshot. While it runs the group behaves exactly as one being restored by
|
|
1660
|
+
* `groups.restore`: `groups.get` waits for it, `groups.list`/`heldGroups` skip it, a Welcome for it is deferred.
|
|
1661
|
+
*
|
|
1662
|
+
* The mark is never simply discarded: once the restore's self-update was recorded (S13: the record carries the
|
|
1663
|
+
* signed envelope and `pending.newState` before anything is published) a relay may hold that commit, and only
|
|
1664
|
+
* this record can confirm it — discarded, the commit would fence every later restore of this leaf. So the group
|
|
1665
|
+
* is loaded like any stored group (its rewind window, fence marker and ingest cursor as stored), whose
|
|
1666
|
+
* crash-recovery resume (AC-PIPE-1, `resumePending`) re-publishes the recorded envelope byte-identically (or finds
|
|
1667
|
+
* it on the relays) and confirms it — clearing the mark in that confirmation's transaction (pipeline.ts). Its own
|
|
1668
|
+
* echo is held as an own pending envelope until then (ingest.ts), never replayed as a foreign commit (no fence);
|
|
1669
|
+
* a competing commit that won the epoch goes through the ordinary convergence/rewind within the stored window.
|
|
1670
|
+
* A mark with no self-update recorded (a crash after the adoption, before the first self-update was recorded,
|
|
1671
|
+
* or between two attempts) continues the restore instead: its first sync, then a fresh self-update — exactly
|
|
1672
|
+
* what `groups.restore` would have done next, and safer than discarding (an earlier attempt's commit, if any,
|
|
1673
|
+
* is ledgered in these stores). Either way the resumed self-update counts as the first of
|
|
1674
|
+
* {@link RESTORE_SELF_UPDATE_ATTEMPTS}; when they all go terminal, or the group is fenced, the group is discarded
|
|
1675
|
+
* like a failed restore (its stores and index entry cleared — the terminal attempt was reported `intentFailed`).
|
|
1676
|
+
* A mark on a group whose adoption never finished (no state, or no index entry: nothing was ever recorded) is
|
|
1677
|
+
* discarded — such a group is found through its account restore record (`settleRestoringRecord`), not the index.
|
|
1678
|
+
* A store or engine failure before the runtime exists discards nothing: the mark stays, and the next
|
|
1679
|
+
* access or scan resumes again.
|
|
1680
|
+
*/
|
|
1681
|
+
function resumeRestore(groupId) {
|
|
1682
|
+
const existing = restoring.get(groupId);
|
|
1683
|
+
if (existing !== undefined)
|
|
1684
|
+
return existing;
|
|
1685
|
+
if (ctx.isDisposed() || abandoned.has(groupId))
|
|
1686
|
+
return undefined;
|
|
1687
|
+
const cached = runtimes.get(groupId);
|
|
1688
|
+
if (cached !== undefined && !cached.disposed)
|
|
1689
|
+
return undefined;
|
|
1690
|
+
const reservation = reserveRestore(groupId);
|
|
1691
|
+
guard.detached(() => void driveResumedRestore(groupId, reservation));
|
|
1692
|
+
return reservation.entry;
|
|
1693
|
+
}
|
|
1694
|
+
async function driveResumedRestore(groupId, r) {
|
|
1695
|
+
const groupStore = groupStoreOf(groupId);
|
|
1696
|
+
let rt;
|
|
1697
|
+
/** Whether a failure takes the adoption back (else the mark stays for the next resume). */
|
|
1698
|
+
let discard = false;
|
|
1699
|
+
try {
|
|
1700
|
+
if (!(await restorePending(groupId, groupStore))) {
|
|
1701
|
+
// Finished (or taken back) since the mark was read: an ordinary load decides.
|
|
1702
|
+
releaseRestore(groupId, r.entry);
|
|
1703
|
+
r.settle(loadRuntime(groupId).catch(() => undefined));
|
|
1704
|
+
return;
|
|
1705
|
+
}
|
|
1706
|
+
// A purge from here on ends this resume and waits for it; every step that does not race `ended` re-checks.
|
|
1707
|
+
assertNotEnded(r.entry);
|
|
1708
|
+
const indexed = await guard.call(ACCOUNT_SCOPE, 'store', () => readGroupIndexEntry(ctx.stores.account.extension(GROUP_INDEX_EXTENSION), groupId));
|
|
1709
|
+
assertNotEnded(r.entry);
|
|
1710
|
+
const records = await guard.call(groupId, 'store', () => listPendingIntentRecords(groupStore.artifact('pendingIntents')));
|
|
1711
|
+
assertNotEnded(r.entry);
|
|
1712
|
+
const recorded = records.find((record) => record.kind === 'self-update');
|
|
1713
|
+
if (indexed === undefined && recorded === undefined) {
|
|
1714
|
+
// The adoption never finished (the index entry is written before anything is recorded): nothing to resume.
|
|
1715
|
+
discard = true;
|
|
1716
|
+
throw new MarmotError('not-a-member', `groups.restore: group ${groupId}'s unfinished restore never completed its adoption`, { details: { groupId } });
|
|
1717
|
+
}
|
|
1718
|
+
rt = await readLoadedRuntime(groupId, groupStore);
|
|
1719
|
+
if (rt === undefined) {
|
|
1720
|
+
discard = true;
|
|
1721
|
+
throw new MarmotError('not-a-member', `groups.restore: group ${groupId}'s unfinished restore holds no state`, { details: { groupId } });
|
|
1722
|
+
}
|
|
1723
|
+
r.entry.runtime = rt;
|
|
1724
|
+
const runtime = rt;
|
|
1725
|
+
runtime.terminalWatchers.add((ending) => r.end(restoreEndError(groupId, ending)));
|
|
1726
|
+
// Before anything is started on the runtime: a purge that ran while it was read leaves the group torn down
|
|
1727
|
+
// (and its stores cleared) — the runtime is only retired, nothing written back.
|
|
1728
|
+
assertNotEnded(r.entry);
|
|
1729
|
+
if (abandoned.has(groupId))
|
|
1730
|
+
throw restoreEndError(groupId, { reason: 'retired' });
|
|
1731
|
+
if (!(await restorePending(groupId, groupStore))) {
|
|
1732
|
+
// The mark vanished while the group was read: nothing to resume — retired; the next access loads what is stored.
|
|
1733
|
+
throw new MarmotError('not-a-member', `groups.restore: group ${groupId}'s unfinished restore was settled elsewhere`, { details: { groupId } });
|
|
1734
|
+
}
|
|
1735
|
+
assertNotEnded(r.entry);
|
|
1736
|
+
if (abandoned.has(groupId))
|
|
1737
|
+
throw restoreEndError(groupId, { reason: 'retired' });
|
|
1738
|
+
discard = true;
|
|
1739
|
+
if (runtime.fence !== undefined)
|
|
1740
|
+
throw restoreEndError(groupId, { reason: 'fenced', cause: runtime.fence });
|
|
1741
|
+
// Collected from before the load's resume starts: the recorded self-update may settle at once.
|
|
1742
|
+
const early = [];
|
|
1743
|
+
const collect = (event) => void early.push(event);
|
|
1744
|
+
runtime.settledListeners.add(collect);
|
|
1745
|
+
try {
|
|
1746
|
+
guard.detached(() => startLoadedRuntime(runtime));
|
|
1747
|
+
// A pending adoption an earlier client left unresolved resolves identically (its marker survived the crash): after
|
|
1748
|
+
// this load's first sync, before the self-update. The tree-fed pass runs too whenever anything of the pending
|
|
1749
|
+
// machinery is in play (a marker, a parked node, an own-leaf fork node): a crash between the resolution's or the
|
|
1750
|
+
// reinstatement's transaction and its pass leaves no marker to say the pass is owed, and the pass is idempotent.
|
|
1751
|
+
// With a self-update already recorded the resolution ran before it was staged, so nothing waits here.
|
|
1752
|
+
if (recorded === undefined && pendingWatch(runtime) !== undefined) {
|
|
1753
|
+
await Promise.race([new Promise((resolve, reject) => (runtime.sendGateOpen ? resolve() : runtime.syncWaiters.add({ resolve, reject }))), r.ended]);
|
|
1754
|
+
await resolvePendingRestore(runtime, r.ended);
|
|
1755
|
+
await Promise.race([runtime.run('restorePending', (rtc) => treeFedPass(rtc)), r.ended]);
|
|
1756
|
+
}
|
|
1757
|
+
await commitRestoreSelfUpdate(runtime, r.ended, recorded === undefined ? undefined : { intentId: recorded.reenqueuedFrom ?? recorded.intentId, early });
|
|
1758
|
+
}
|
|
1759
|
+
finally {
|
|
1760
|
+
runtime.settledListeners.delete(collect);
|
|
1761
|
+
}
|
|
1762
|
+
}
|
|
1763
|
+
catch {
|
|
1764
|
+
// No caller to reject: a waiting `groups.get` learns "not held"; the self-update's own failure was reported.
|
|
1765
|
+
r.settle(undefined);
|
|
1766
|
+
await Promise.race([undoRestore(groupId, rt, discard), clientDisposed]);
|
|
1767
|
+
releaseRestore(groupId, r.entry);
|
|
1768
|
+
return;
|
|
1769
|
+
}
|
|
1770
|
+
releaseRestore(groupId, r.entry);
|
|
1771
|
+
r.settle(rt);
|
|
1772
|
+
if (rt !== undefined) {
|
|
1773
|
+
scheduleRekeyFollowUp(rt);
|
|
1774
|
+
// A restore resumed after a crash owes the rotation as the uninterrupted one does.
|
|
1775
|
+
await oweRestoreRotation(groupId);
|
|
1776
|
+
}
|
|
1777
|
+
}
|
|
1778
|
+
/**
|
|
1779
|
+
* Owner decision 13: a successful restore owes a device-key rotation (the old device may still hold the backed-up key).
|
|
1780
|
+
* The marker is durable before the device service is told; a failed write only loses the prompt rotation, never the
|
|
1781
|
+
* restore (the handle is already the caller's). A crash between the restore's success and this write loses it too.
|
|
1782
|
+
*/
|
|
1783
|
+
async function oweRestoreRotation(groupId) {
|
|
1784
|
+
if (ctx.disableRestoreRotationForTest)
|
|
1785
|
+
return;
|
|
1786
|
+
await guard
|
|
1787
|
+
.call(ACCOUNT_SCOPE, 'store', () => ctx.stores.account.transaction((tx) => writeRestoreRotationOwed(tx.extension(RESTORE_ROTATION_EXTENSION), groupId, ctx.clock.now())))
|
|
1788
|
+
.then(() => announceTo(restoredListeners, groupId))
|
|
1789
|
+
.catch(() => undefined);
|
|
1790
|
+
}
|
|
1791
|
+
/** Most self-updates a restore attempts when each one goes terminal (a foreign commit won its epoch, …). */
|
|
1792
|
+
const RESTORE_SELF_UPDATE_ATTEMPTS = 3;
|
|
1793
|
+
/**
|
|
1794
|
+
* First delay (ms) before a self-update the group refused `commit-pending` (retryable: an elected SelfRemove
|
|
1795
|
+
* wait, a deferred proposal, another own commit pending) is re-driven when nothing woke the restore sooner (an
|
|
1796
|
+
* own intent settling, the group's state changing); doubles per round up to {@link RESTORE_REDRIVE_MAX_MS}.
|
|
1797
|
+
*/
|
|
1798
|
+
const RESTORE_REDRIVE_BASE_MS = 250;
|
|
1799
|
+
const RESTORE_REDRIVE_MAX_MS = 30_000;
|
|
1800
|
+
/** Throws when the restored runtime can no longer make progress: fenced, torn down, disposed. */
|
|
1801
|
+
function assertRestorable(rt) {
|
|
1802
|
+
if (rt.fence !== undefined)
|
|
1803
|
+
throw restoreEndError(rt.groupId, { reason: 'fenced', cause: rt.fence });
|
|
1804
|
+
if (ctx.isDisposed())
|
|
1805
|
+
throw restoreEndError(rt.groupId, { reason: 'disposed' });
|
|
1806
|
+
if (rt.abandoned || rt.disposed)
|
|
1807
|
+
throw restoreEndError(rt.groupId, { reason: 'retired' });
|
|
1808
|
+
}
|
|
1809
|
+
/**
|
|
1810
|
+
* Commits a self-update on the restored group and resolves once it is confirmed — matched by the driven
|
|
1811
|
+
* record's id (or a re-enqueue of it, AC-RACE-1), whatever else settles meanwhile; it may be retried in the
|
|
1812
|
+
* background before that. A self-update that goes terminal is re-driven, up to {@link RESTORE_SELF_UPDATE_ATTEMPTS}.
|
|
1813
|
+
* A `commit-pending` refusal that recorded nothing (`details.retryable`) is re-driven once its condition may
|
|
1814
|
+
* have cleared — an own intent settled, the group's state changed, or the backoff passed — without counting as
|
|
1815
|
+
* an attempt. Every wait races `ended` (the runtime's terminal signal, dispose); every pass re-checks the runtime.
|
|
1816
|
+
*
|
|
1817
|
+
* `resumed` (a resumed unfinished restore): the self-update an earlier client recorded — driven by the load's own
|
|
1818
|
+
* crash-recovery resume, not here — is awaited as the first attempt; `early` holds what settled before this call.
|
|
1819
|
+
* A fresh drive waits for the group's first post-load sync (AC-PIPE-15) — already open for `groups.restore`.
|
|
1820
|
+
*/
|
|
1821
|
+
async function commitRestoreSelfUpdate(rt, ended, resumed) {
|
|
1822
|
+
const settled = [];
|
|
1823
|
+
let wake;
|
|
1824
|
+
const poke = () => {
|
|
1825
|
+
const w = wake;
|
|
1826
|
+
wake = undefined;
|
|
1827
|
+
w?.();
|
|
1828
|
+
};
|
|
1829
|
+
const onSettled = (event) => {
|
|
1830
|
+
settled.push(event);
|
|
1831
|
+
poke();
|
|
1832
|
+
};
|
|
1833
|
+
let timer;
|
|
1834
|
+
const clearTimer = () => {
|
|
1835
|
+
if (timer === undefined)
|
|
1836
|
+
return;
|
|
1837
|
+
ctx.timers.clear(timer);
|
|
1838
|
+
timer = undefined;
|
|
1839
|
+
};
|
|
1840
|
+
/** Until the next wake-up (or `ms` passed), or rejects when the restore ended. */
|
|
1841
|
+
const pause = async (ms) => {
|
|
1842
|
+
try {
|
|
1843
|
+
await Promise.race([
|
|
1844
|
+
new Promise((resolve) => {
|
|
1845
|
+
wake = resolve;
|
|
1846
|
+
if (ms !== undefined)
|
|
1847
|
+
timer = ctx.timers.set(poke, ms);
|
|
1848
|
+
}),
|
|
1849
|
+
ended,
|
|
1850
|
+
]);
|
|
1851
|
+
}
|
|
1852
|
+
finally {
|
|
1853
|
+
wake = undefined;
|
|
1854
|
+
clearTimer();
|
|
1855
|
+
}
|
|
1856
|
+
};
|
|
1857
|
+
rt.settledListeners.add(onSettled);
|
|
1858
|
+
if (resumed !== undefined)
|
|
1859
|
+
settled.push(...resumed.early);
|
|
1860
|
+
const unwatch = watchState(rt.groupId, poke);
|
|
1861
|
+
try {
|
|
1862
|
+
let failures = 0;
|
|
1863
|
+
let refusals = 0;
|
|
1864
|
+
let carried = resumed?.intentId;
|
|
1865
|
+
for (;;) {
|
|
1866
|
+
assertRestorable(rt);
|
|
1867
|
+
let driven = carried;
|
|
1868
|
+
let sendError;
|
|
1869
|
+
if (carried !== undefined) {
|
|
1870
|
+
carried = undefined;
|
|
1871
|
+
}
|
|
1872
|
+
else {
|
|
1873
|
+
settled.length = 0;
|
|
1874
|
+
if (!rt.sendGateOpen)
|
|
1875
|
+
await Promise.race([new Promise((resolve, reject) => (rt.sendGateOpen ? resolve() : rt.syncWaiters.add({ resolve, reject }))), ended]);
|
|
1876
|
+
try {
|
|
1877
|
+
await Promise.race([rt.run('restore', (rtc) => sendIntent(rtc, { type: 'self-update' }, { onRecorded: (intentId) => (driven = intentId) })), ended]);
|
|
1878
|
+
}
|
|
1879
|
+
catch (error) {
|
|
1880
|
+
assertRestorable(rt);
|
|
1881
|
+
if (driven === undefined) {
|
|
1882
|
+
// Refused before anything was recorded, for a condition that clears by itself: waited out, uncounted.
|
|
1883
|
+
if (isMarmotError(error, 'commit-pending') && error.details?.['retryable'] === true) {
|
|
1884
|
+
await pause(Math.min(RESTORE_REDRIVE_BASE_MS * 2 ** refusals, RESTORE_REDRIVE_MAX_MS));
|
|
1885
|
+
refusals += 1;
|
|
1886
|
+
continue;
|
|
1887
|
+
}
|
|
1888
|
+
throw error;
|
|
1889
|
+
}
|
|
1890
|
+
// Recorded and still on its way (retried in the background), or already reported terminal below.
|
|
1891
|
+
const pending = isMarmotError(error, 'publish-failed') || isMarmotError(error, 'confirm-blocked') || isMarmotError(error, 'commit-pending') || isMarmotError(error, 'send-rejected');
|
|
1892
|
+
if (!pending)
|
|
1893
|
+
throw error;
|
|
1894
|
+
sendError = error;
|
|
1895
|
+
}
|
|
1896
|
+
refusals = 0;
|
|
1897
|
+
}
|
|
1898
|
+
const ours = (event) => event.intentId === driven || event.rootIntentId === driven;
|
|
1899
|
+
let outcome;
|
|
1900
|
+
while ((outcome = settled.find(ours)) === undefined) {
|
|
1901
|
+
assertRestorable(rt);
|
|
1902
|
+
await pause();
|
|
1903
|
+
}
|
|
1904
|
+
if (outcome.error === undefined)
|
|
1905
|
+
return;
|
|
1906
|
+
failures += 1;
|
|
1907
|
+
if (failures >= RESTORE_SELF_UPDATE_ATTEMPTS)
|
|
1908
|
+
throw sendError ?? outcome.error;
|
|
1909
|
+
}
|
|
1910
|
+
}
|
|
1911
|
+
finally {
|
|
1912
|
+
rt.settledListeners.delete(onSettled);
|
|
1913
|
+
unwatch();
|
|
1914
|
+
clearTimer();
|
|
1915
|
+
}
|
|
1916
|
+
}
|
|
1917
|
+
/**
|
|
1918
|
+
* Takes a failed restore's adoption back (best effort): the runtime retired — as the last operation on its
|
|
1919
|
+
* queue, so an operation in flight finishes first and nothing it writes outlives the clear — unless a teardown
|
|
1920
|
+
* purge or dispose already did; then the group's stores cleared and its index entry removed.
|
|
1921
|
+
*
|
|
1922
|
+
* Never once the client is disposed: its stores may already belong to a new client over the same backend, which
|
|
1923
|
+
* may hold (or be restoring) the group by now — a late clear from here would wipe that. The group adopted here
|
|
1924
|
+
* stays marked unfinished instead (`RESTORE_PENDING_EXTENSION`, cleared only by a confirmed own self-update), and
|
|
1925
|
+
* that next client resumes it before reading it as held (`resumeRestore`). A failed clear leaves the mark too, so
|
|
1926
|
+
* the same holds for it. `clear: false` (a resume that failed before it could decide) only retires the runtime.
|
|
1927
|
+
*/
|
|
1928
|
+
async function undoRestore(groupId, rt, clear = true) {
|
|
1929
|
+
if (rt !== undefined) {
|
|
1930
|
+
await rt
|
|
1931
|
+
.runLast(() => {
|
|
1932
|
+
if (!rt.disposed)
|
|
1933
|
+
retireRuntime(rt, 'could not be restored', 'restore-undone');
|
|
1934
|
+
})
|
|
1935
|
+
.catch(() => undefined);
|
|
1936
|
+
if (runtimes.get(groupId) === rt)
|
|
1937
|
+
runtimes.delete(groupId);
|
|
1938
|
+
}
|
|
1939
|
+
if (ctx.isDisposed())
|
|
1940
|
+
return;
|
|
1941
|
+
// A purge of the group owns its clear (it waits for this take-back, then clears and drops the store it opened —
|
|
1942
|
+
// a clear from here would open and cache that store again).
|
|
1943
|
+
if (abandoned.has(groupId))
|
|
1944
|
+
return;
|
|
1945
|
+
if (!clear) {
|
|
1946
|
+
// Left marked: the next scan retries it (`resumeUnfinishedRestores`).
|
|
1947
|
+
leftMarked.add(groupId);
|
|
1948
|
+
return;
|
|
1949
|
+
}
|
|
1950
|
+
// Left to the restore record the take-back wrote first (or, when even that failed, to the mark): the next scan
|
|
1951
|
+
// settles it (`settleRestoringRecord` resumes a marked group, completes an interrupted clear).
|
|
1952
|
+
await clearAdoptedGroup(groupId, groupStoreOf(groupId)).catch(() => {
|
|
1953
|
+
if (!abandoned.has(groupId))
|
|
1954
|
+
leftRecords.add(groupId);
|
|
1955
|
+
});
|
|
1956
|
+
}
|
|
1957
|
+
/**
|
|
1958
|
+
* Every held group's handle. AC-MX-2: a group being restored (or resumed) is not held yet — it is skipped (never
|
|
1959
|
+
* waited for), so one restore that cannot finish (offline) never blocks the list; it appears once its restore
|
|
1960
|
+
* resolved. An unfinished restore an earlier client left is resumed here, and skipped likewise.
|
|
1961
|
+
*/
|
|
1962
|
+
async function listGroupsOp() {
|
|
1963
|
+
const ids = await guard.call(ACCOUNT_SCOPE, 'store', () => listGroupIndex(ctx.stores.account.extension(GROUP_INDEX_EXTENSION)));
|
|
1964
|
+
const out = [];
|
|
1965
|
+
for (const id of ids) {
|
|
1966
|
+
if (await unfinishedRestore(id))
|
|
1967
|
+
continue;
|
|
1968
|
+
const handle = await getGroupOp(id);
|
|
1969
|
+
if (handle !== undefined)
|
|
1970
|
+
out.push(handle);
|
|
1971
|
+
}
|
|
1972
|
+
return Object.freeze(out);
|
|
1973
|
+
}
|
|
1974
|
+
const devicePort = {
|
|
1975
|
+
// AC-MX-2: a group being restored (or resumed) is not held yet (as for `groups.list`): device operations never wait on it.
|
|
1976
|
+
// Nor is an index entry whose group store holds no state (a take-back or purge interrupted between its clear and
|
|
1977
|
+
// the entry's deletion, settled by the next scan) — as `groups.list`, whose load finds nothing there.
|
|
1978
|
+
async heldGroups() {
|
|
1979
|
+
const held = [];
|
|
1980
|
+
for (const id of await guard.call(ACCOUNT_SCOPE, 'store', () => listGroupIndex(ctx.stores.account.extension(GROUP_INDEX_EXTENSION)))) {
|
|
1981
|
+
if (await unfinishedRestore(id))
|
|
1982
|
+
continue;
|
|
1983
|
+
const live = runtimes.get(id);
|
|
1984
|
+
if ((live === undefined || live.disposed) && !(await guard.call(id, 'store', () => hasGroupState(groupStoreOf(id).artifact('clientState')))))
|
|
1985
|
+
continue;
|
|
1986
|
+
held.push(id);
|
|
1987
|
+
}
|
|
1988
|
+
return held;
|
|
1989
|
+
},
|
|
1990
|
+
async snapshot(groupId) {
|
|
1991
|
+
const rt = await loadRuntime(groupId);
|
|
1992
|
+
if (rt === undefined)
|
|
1993
|
+
return undefined;
|
|
1994
|
+
const self = await guard.call(ACCOUNT_SCOPE, 'signer', () => ctx.signer.getPublicKey());
|
|
1995
|
+
// A purge may retire the runtime after it was taken here (its last operation queued first): the group is then
|
|
1996
|
+
// not held — DeviceGroupPort.snapshot's `undefined` — although its index entry is dropped only after the purge.
|
|
1997
|
+
const notHeld = (error) => {
|
|
1998
|
+
if (rt.abandoned && isMarmotError(error, 'not-a-member'))
|
|
1999
|
+
return undefined;
|
|
2000
|
+
throw error;
|
|
2001
|
+
};
|
|
2002
|
+
if (rt.abandoned)
|
|
2003
|
+
return undefined;
|
|
2004
|
+
// On the group's queue: whatever was delivered before this read (a competing commit a live
|
|
2005
|
+
// subscription handed over while an own commit was being published) is ingested first.
|
|
2006
|
+
return rt.run('snapshot', async (rtc) => {
|
|
2007
|
+
const summaries = await guard.call(groupId, 'store', () => listPendingIntentSummaries(rtc.groupStore.artifact('pendingIntents')));
|
|
2008
|
+
// S20 follow-up: the settlement-recheck marker read on the same queue step as the tree, so a reopen decided
|
|
2009
|
+
// from this snapshot sees the withdrawn-leaf evidence as of THIS tree — never one read before later
|
|
2010
|
+
// transitions (a re-add pruned it) went in (VQ-S20-P14).
|
|
2011
|
+
const recheck = await guard.call(groupId, 'store', () => readSettlementRecheckRecord(rtc.groupStore.extension(SETTLEMENT_RECHECK_EXTENSION)));
|
|
2012
|
+
const state = rtc.exposedState;
|
|
2013
|
+
// The local account's signature-key successions on the canonical path (read with the tree, on the queue): what
|
|
2014
|
+
// the device service unites with its recorded links. `account` is each succession's own (the leaf's credential).
|
|
2015
|
+
const successions = successionsInWindow(rtc.window, state)
|
|
2016
|
+
.filter((s) => s.account === self)
|
|
2017
|
+
.map(({ account, leafIndex, fromLeafKey, toLeafKey, commitDigest }) => ({ account, leafIndex, fromLeafKey, toLeafKey, commitDigest }));
|
|
2018
|
+
const ownLeaf = ownLeafOf(state);
|
|
2019
|
+
return {
|
|
2020
|
+
groupId,
|
|
2021
|
+
member: state.groupActiveState.kind !== 'removedFromGroup',
|
|
2022
|
+
accountLeaves: leafInfosOf(state, self),
|
|
2023
|
+
...(ownLeaf === undefined ? {} : { ownLeaf }),
|
|
2024
|
+
successions,
|
|
2025
|
+
recheck,
|
|
2026
|
+
ownCommitPending: summaries.some((r) => r.kind === 'commit' || r.kind === 'self-update' || r.kind === 'proposal'),
|
|
2027
|
+
// The engine's `convergenceStatuses.syncing` (not exported by marmot-engine's barrels).
|
|
2028
|
+
syncing: rtc.engine.convergenceStatus === 'Syncing',
|
|
2029
|
+
};
|
|
2030
|
+
}).catch(notHeld);
|
|
2031
|
+
},
|
|
2032
|
+
async addDevice(groupId, keyPackageEvent, reopenedLeafKey, approvalEpoch, origin) {
|
|
2033
|
+
const rt = await loadRuntime(groupId);
|
|
2034
|
+
if (rt === undefined)
|
|
2035
|
+
throw new MarmotError('not-a-member', `devices: group ${groupId} is not held`, { details: { groupId } });
|
|
2036
|
+
await inviteAndDeliver(rt, [keyPackageEvent], reopenedLeafKey, approvalEpoch, origin);
|
|
2037
|
+
},
|
|
2038
|
+
async groupTip(groupId) {
|
|
2039
|
+
const rt = await loadRuntime(groupId);
|
|
2040
|
+
if (rt === undefined || rt.abandoned)
|
|
2041
|
+
return undefined;
|
|
2042
|
+
return `${Number(rt.exposedState.groupContext.epoch)}:${tagOf(rt.exposedState)}`;
|
|
2043
|
+
},
|
|
2044
|
+
async groupEpoch(groupId) {
|
|
2045
|
+
const rt = await loadRuntime(groupId);
|
|
2046
|
+
if (rt === undefined || rt.abandoned)
|
|
2047
|
+
return undefined;
|
|
2048
|
+
// A plain read of the exposed state (same as the public `MarmotGroup.epoch` getter) — never `rt.run(...)`,
|
|
2049
|
+
// so this never touches `pendingIntents` or any other queued read.
|
|
2050
|
+
return Number(rt.exposedState.groupContext.epoch);
|
|
2051
|
+
},
|
|
2052
|
+
async removeLeaf(groupId, leafKey) {
|
|
2053
|
+
const rt = await loadRuntime(groupId);
|
|
2054
|
+
if (rt === undefined)
|
|
2055
|
+
throw new MarmotError('not-a-member', `devices: group ${groupId} is not held`, { details: { groupId } });
|
|
2056
|
+
return rt.run('forget', (rtc) => removeLeafIntent(rtc, leafKey));
|
|
2057
|
+
},
|
|
2058
|
+
// The whole device chain in ONE commit (Design Decision 8): every leaf carrying any of the keys.
|
|
2059
|
+
async removeLeaves(groupId, leafKeys) {
|
|
2060
|
+
const rt = await loadRuntime(groupId);
|
|
2061
|
+
if (rt === undefined)
|
|
2062
|
+
throw new MarmotError('not-a-member', `devices: group ${groupId} is not held`, { details: { groupId } });
|
|
2063
|
+
return rt.run('forget', (rtc) => removeLeafIntent(rtc, leafKeys));
|
|
2064
|
+
},
|
|
2065
|
+
// AC-REKEY-9/10: on the group's queue; rt.run rejects `reentrant` from a consumer surface like removeLeaf.
|
|
2066
|
+
async rekey(groupId, target) {
|
|
2067
|
+
const rt = await loadRuntime(groupId);
|
|
2068
|
+
if (rt === undefined)
|
|
2069
|
+
throw new MarmotError('not-a-member', `devices: group ${groupId} is not held`, { details: { groupId } });
|
|
2070
|
+
return rt.run('rekey', (rtc) => rekeyIntent(rtc, target));
|
|
2071
|
+
},
|
|
2072
|
+
// The views carry `successions` and the registry may be told of a new one by `onRotation`.
|
|
2073
|
+
reportsSuccessions: true,
|
|
2074
|
+
onRotation(listener) {
|
|
2075
|
+
rotationListeners.add(listener);
|
|
2076
|
+
return () => {
|
|
2077
|
+
rotationListeners.delete(listener);
|
|
2078
|
+
};
|
|
2079
|
+
},
|
|
2080
|
+
async settlementRechecks() {
|
|
2081
|
+
const out = [];
|
|
2082
|
+
const ids = await guard.call(ACCOUNT_SCOPE, 'store', () => listGroupIndex(ctx.stores.account.extension(GROUP_INDEX_EXTENSION)));
|
|
2083
|
+
for (const groupId of ids) {
|
|
2084
|
+
if (abandoned.has(groupId))
|
|
2085
|
+
continue;
|
|
2086
|
+
const marker = await guard.call(groupId, 'store', () => readSettlementRecheckRecord(ctx.stores.groupFactory(groupId).extension(SETTLEMENT_RECHECK_EXTENSION)));
|
|
2087
|
+
if (marker !== undefined)
|
|
2088
|
+
out.push({ groupId, token: marker.token, ...(marker.withdrawnLeaves === undefined ? {} : { withdrawnLeaves: marker.withdrawnLeaves }) });
|
|
2089
|
+
}
|
|
2090
|
+
return out;
|
|
2091
|
+
},
|
|
2092
|
+
async leafOnBranches(groupId, leafKey) {
|
|
2093
|
+
const rt = await loadRuntime(groupId);
|
|
2094
|
+
if (rt === undefined || rt.abandoned)
|
|
2095
|
+
return undefined;
|
|
2096
|
+
const self = await guard.call(ACCOUNT_SCOPE, 'signer', () => ctx.signer.getPublicKey());
|
|
2097
|
+
const keys = typeof leafKey === 'string' ? [leafKey] : leafKey;
|
|
2098
|
+
const holds = (state) => leafInfosOf(state, self).some((l) => keys.includes(l.leafKey));
|
|
2099
|
+
return rt
|
|
2100
|
+
.run('leafOnBranches', async (rtc) => {
|
|
2101
|
+
const window = rtc.window;
|
|
2102
|
+
const path = new Set(canonicalPath(window).map((n) => n.tag));
|
|
2103
|
+
let canonical = holds(rtc.exposedState);
|
|
2104
|
+
let abandoned = false;
|
|
2105
|
+
for (const node of window.nodes.values()) {
|
|
2106
|
+
if (node.tag === window.tipTag || node.snapshot === undefined || !holds(deserializeClientState(node.snapshot)))
|
|
2107
|
+
continue;
|
|
2108
|
+
if (path.has(node.tag))
|
|
2109
|
+
canonical = true;
|
|
2110
|
+
else
|
|
2111
|
+
abandoned = true;
|
|
2112
|
+
}
|
|
2113
|
+
return { canonical, abandoned };
|
|
2114
|
+
})
|
|
2115
|
+
.catch((error) => {
|
|
2116
|
+
if (rt.abandoned && isMarmotError(error, 'not-a-member'))
|
|
2117
|
+
return undefined;
|
|
2118
|
+
throw error;
|
|
2119
|
+
});
|
|
2120
|
+
},
|
|
2121
|
+
async clearSettlementRecheck(groupId, token) {
|
|
2122
|
+
const store = ctx.stores.groupFactory(groupId);
|
|
2123
|
+
await guard.call(groupId, 'store', () => store.transaction((tx) => clearSettlementRecheck(tx.extension(SETTLEMENT_RECHECK_EXTENSION), token)));
|
|
2124
|
+
},
|
|
2125
|
+
onRewind(listener) {
|
|
2126
|
+
rewindListeners.add(listener);
|
|
2127
|
+
return () => {
|
|
2128
|
+
rewindListeners.delete(listener);
|
|
2129
|
+
};
|
|
2130
|
+
},
|
|
2131
|
+
onRestored(listener) {
|
|
2132
|
+
restoredListeners.add(listener);
|
|
2133
|
+
return () => {
|
|
2134
|
+
restoredListeners.delete(listener);
|
|
2135
|
+
};
|
|
2136
|
+
},
|
|
2137
|
+
};
|
|
2138
|
+
/** What `leave()` reads from the group's exposed state (account/teardown.ts). */
|
|
2139
|
+
function leaveViewOf(state, self, ownIntentPending, removalSettlesAt) {
|
|
2140
|
+
const member = state.groupActiveState.kind !== 'removedFromGroup';
|
|
2141
|
+
const ownLeaf = Number(state.privatePath.leafIndex);
|
|
2142
|
+
let occupied = 0;
|
|
2143
|
+
for (let i = 0; i < state.ratchetTree.length; i += 2)
|
|
2144
|
+
if (state.ratchetTree[i]?.nodeType === nodeTypes.leaf)
|
|
2145
|
+
occupied += 1;
|
|
2146
|
+
const admins = getMarmotGroupView(state)?.adminPubkeys ?? [];
|
|
2147
|
+
return {
|
|
2148
|
+
member,
|
|
2149
|
+
soleMember: member && occupied === 1,
|
|
2150
|
+
admin: admins.includes(self),
|
|
2151
|
+
accountHasOtherLeaf: accountLeafIndexes(state.ratchetTree, self).some((i) => i !== ownLeaf),
|
|
2152
|
+
lastActiveAdmin: isLastActiveAdmin(state.ratchetTree, admins, self),
|
|
2153
|
+
selfRemoveStaged: Object.values(state.unappliedProposals).some((p) => p.proposal.proposalType === selfRemoveProposalType && p.senderLeafIndex !== undefined && Number(p.senderLeafIndex) === ownLeaf),
|
|
2154
|
+
ownIntentPending,
|
|
2155
|
+
...(member || removalSettlesAt === undefined ? {} : { removalSettlesAt }),
|
|
2156
|
+
};
|
|
2157
|
+
}
|
|
2158
|
+
/**
|
|
2159
|
+
* Retires a group's runtime for good — inside its last operation (an abandoned group's purge, a failed restore
|
|
2160
|
+
* taken back): no sync, no timers, no engine; its handle and operations answer `not-a-member`; waiting queued
|
|
2161
|
+
* sends and `synced()` callers learn `not-a-member` ("group X {@link what} before …"); its terminal watchers fire;
|
|
2162
|
+
* its availability subscriptions end with `ended` (client/group-availability.ts) — after the last observation.
|
|
2163
|
+
*/
|
|
2164
|
+
function retireRuntime(rt, what, ended) {
|
|
2165
|
+
rt.abandoned = true;
|
|
2166
|
+
rt.disposed = true;
|
|
2167
|
+
for (const handle of rt.pipelineTimers)
|
|
2168
|
+
ctx.timers.clear(handle);
|
|
2169
|
+
rt.pipelineTimers.clear();
|
|
2170
|
+
rt.reenqueueRetryArmed.clear();
|
|
2171
|
+
for (const waiter of rt.queueWaiters.values())
|
|
2172
|
+
waiter.reject(new MarmotError('not-a-member', `send: group ${rt.groupId} ${what} before the queued rumor was published`, { details: { groupId: rt.groupId } }));
|
|
2173
|
+
rt.queueWaiters.clear();
|
|
2174
|
+
for (const waiter of rt.syncWaiters)
|
|
2175
|
+
waiter.reject(new MarmotError('not-a-member', `synced: group ${rt.groupId} ${what} before its first sync`, { details: { groupId: rt.groupId } }));
|
|
2176
|
+
rt.syncWaiters.clear();
|
|
2177
|
+
endRuntime(rt, { reason: 'retired' });
|
|
2178
|
+
closeAvailability(rt, ended);
|
|
2179
|
+
try {
|
|
2180
|
+
stopGroupSync(rt);
|
|
2181
|
+
}
|
|
2182
|
+
catch {
|
|
2183
|
+
// the transport's unsubscribe failed: the runtime is retired regardless
|
|
2184
|
+
}
|
|
2185
|
+
rt.engine.dispose();
|
|
2186
|
+
}
|
|
2187
|
+
async function heldRuntime(groupId, operation) {
|
|
2188
|
+
const rt = await loadRuntime(groupId);
|
|
2189
|
+
if (rt === undefined)
|
|
2190
|
+
throw new MarmotError('not-a-member', `${operation}: group ${groupId} is not held`, { details: { groupId } });
|
|
2191
|
+
return rt;
|
|
2192
|
+
}
|
|
2193
|
+
const teardownPort = {
|
|
2194
|
+
async view(groupId) {
|
|
2195
|
+
const rt = await loadRuntime(groupId);
|
|
2196
|
+
if (rt === undefined)
|
|
2197
|
+
return undefined;
|
|
2198
|
+
const self = await guard.call(ACCOUNT_SCOPE, 'signer', () => ctx.signer.getPublicKey());
|
|
2199
|
+
// On the group's queue: whatever was delivered before this read is ingested first.
|
|
2200
|
+
return rt.run('leaveView', async (rtc) => {
|
|
2201
|
+
const summaries = await guard.call(groupId, 'store', () => listPendingIntentSummaries(rtc.groupStore.artifact('pendingIntents')));
|
|
2202
|
+
const removalSettlesAt = belongsTo(rtc.removal, rtc.exposedState) ? settlesAt(rtc.removal, ctx.removalSettleMs) : undefined;
|
|
2203
|
+
return leaveViewOf(rtc.exposedState, self, summaries.some((r) => r.kind !== 'application'), removalSettlesAt);
|
|
2204
|
+
});
|
|
2205
|
+
},
|
|
2206
|
+
async proposeSelfRemove(groupId) {
|
|
2207
|
+
const rt = await heldRuntime(groupId, 'leave');
|
|
2208
|
+
await rt.run('leave', (rtc) => proposeSelfRemoveIntent(rtc));
|
|
2209
|
+
},
|
|
2210
|
+
async demote(groupId) {
|
|
2211
|
+
const rt = await heldRuntime(groupId, 'departAsAdmin');
|
|
2212
|
+
const self = await guard.call(ACCOUNT_SCOPE, 'signer', () => ctx.signer.getPublicKey());
|
|
2213
|
+
await rt.run('departAsAdmin', (rtc) => {
|
|
2214
|
+
const admins = (getMarmotGroupView(rtc.engine.state)?.adminPubkeys ?? []).filter((a) => a !== self);
|
|
2215
|
+
return setAdminsIntent(rtc, admins, 'departAsAdmin');
|
|
2216
|
+
});
|
|
2217
|
+
},
|
|
2218
|
+
purge(groupId, beforeClear) {
|
|
2219
|
+
purgeGenerations.set(groupId, (purgeGenerations.get(groupId) ?? 0) + 1);
|
|
2220
|
+
const run = purgeGroup(groupId, beforeClear);
|
|
2221
|
+
purgesInFlight.set(groupId, run);
|
|
2222
|
+
void run
|
|
2223
|
+
.finally(() => {
|
|
2224
|
+
if (purgesInFlight.get(groupId) === run)
|
|
2225
|
+
purgesInFlight.delete(groupId);
|
|
2226
|
+
})
|
|
2227
|
+
.catch(() => undefined);
|
|
2228
|
+
return run;
|
|
2229
|
+
},
|
|
2230
|
+
watch: watchState,
|
|
2231
|
+
};
|
|
2232
|
+
/** {@link TeardownGroupPort.purge}. */
|
|
2233
|
+
async function purgeGroup(groupId, beforeClear) {
|
|
2234
|
+
abandoned.add(groupId);
|
|
2235
|
+
leftMarked.delete(groupId);
|
|
2236
|
+
// AC-MX-2: a restore (or resume) of the group is ended first and its take-back awaited — before it has a
|
|
2237
|
+
// runtime it is in neither map below, and left running it would continue over the cleared stores (and hold its
|
|
2238
|
+
// reservation forever: the self-update record it waits on is gone). A resume is never started from here on.
|
|
2239
|
+
const restored = await endRestoresForPurge(groupId);
|
|
2240
|
+
const rt = runtimes.get(groupId) ?? (await loading.get(groupId)?.catch(() => undefined)) ?? restored;
|
|
2241
|
+
// F2: the stored epoch is read, and its watermark committed, in the same serialized step as the clear; then
|
|
2242
|
+
// AC-ADMIN-5: every artifact AND every extension namespace of the group, in one backend transaction.
|
|
2243
|
+
const deleteAll = async (store, live) => {
|
|
2244
|
+
// S10 (AC-ADMIN-7): the history backend's retained rumors go first — a failure leaves the group stored and
|
|
2245
|
+
// the purge is retried, never a purged group whose history survives in the backend. The group's history
|
|
2246
|
+
// worker is stopped first (no update is started after the purge); a purge that does not settle within the
|
|
2247
|
+
// history call timeout counts as failed, so a hanging backend never wedges the group's queue; the purge (and
|
|
2248
|
+
// the teardown) is retried at the teardown backoff until the backend answers.
|
|
2249
|
+
const history = ctx.history;
|
|
2250
|
+
if (history !== undefined) {
|
|
2251
|
+
// The worker's last call must have settled first, so a stalled append never lands after the purge.
|
|
2252
|
+
if (live !== undefined) {
|
|
2253
|
+
await stopHistoryDrain(live);
|
|
2254
|
+
await awaitHistoryCallSettled(live);
|
|
2255
|
+
await issueMutation(live, () => history.purge(groupId));
|
|
2256
|
+
}
|
|
2257
|
+
else {
|
|
2258
|
+
// No runtime in this client instance: none of its history calls can be outstanding.
|
|
2259
|
+
await boundedHistoryCall(ctx, guard, groupId, () => history.purge(groupId));
|
|
2260
|
+
}
|
|
2261
|
+
}
|
|
2262
|
+
const epoch = (await guard.call(groupId, 'store', () => readGroupState(store.artifact('clientState'))))?.groupContext.epoch;
|
|
2263
|
+
if (epoch !== undefined) {
|
|
2264
|
+
// Epic marmot-invitation-outcomes S2 (A1): the adoption proofs the clear deletes, read in the same serialized
|
|
2265
|
+
// step, so teardown stamps every invitation they make `joined` before they go.
|
|
2266
|
+
const proofs = await guard.call(groupId, 'store', () => readAdoptionProofs(store, { skipMalformed: true }));
|
|
2267
|
+
await guard.call(ACCOUNT_SCOPE, 'store', () => beforeClear(epoch, proofs));
|
|
2268
|
+
}
|
|
2269
|
+
await guard.call(groupId, 'store', () => store.clear());
|
|
2270
|
+
};
|
|
2271
|
+
if (rt !== undefined && !rt.abandoned && !rt.disposed) {
|
|
2272
|
+
// Last operation on the group's queue: every operation queued before it finishes first, none after it runs.
|
|
2273
|
+
await rt.run('abandon', async (rtc) => {
|
|
2274
|
+
await deleteAll(rtc.groupStore, rtc);
|
|
2275
|
+
retireRuntime(rtc, 'was left or abandoned', 'torn-down');
|
|
2276
|
+
});
|
|
2277
|
+
}
|
|
2278
|
+
else {
|
|
2279
|
+
// A runtime this client retired or never loaded: any history call it issued is still waited for.
|
|
2280
|
+
await deleteAll(ctx.stores.groupFactory(groupId), rt);
|
|
2281
|
+
}
|
|
2282
|
+
if (rt !== undefined && runtimes.get(groupId) === rt)
|
|
2283
|
+
runtimes.delete(groupId);
|
|
2284
|
+
openedStores.delete(groupId);
|
|
2285
|
+
// A-7: the purged group's session fence goes with it (a later join or restore starts the group over).
|
|
2286
|
+
availabilityClient.sessionFences.clear(groupId);
|
|
2287
|
+
notifyState(groupId);
|
|
2288
|
+
}
|
|
2289
|
+
// ---------------------------------------------------------------------------
|
|
2290
|
+
// The competing-Welcome ports (epic marmot-welcome-race-joiner)
|
|
2291
|
+
// ---------------------------------------------------------------------------
|
|
2292
|
+
/** The listener {@link GroupRegistry.onGateMayHaveCleared} set. */
|
|
2293
|
+
let gateClearedListener;
|
|
2294
|
+
/** Tells the client that a condition the gate reports may have cleared for `groupId`. Never throws. */
|
|
2295
|
+
function gateMayHaveCleared(groupId) {
|
|
2296
|
+
const listener = gateClearedListener;
|
|
2297
|
+
if (listener === undefined || ctx.isDisposed())
|
|
2298
|
+
return;
|
|
2299
|
+
guard.detached(() => {
|
|
2300
|
+
try {
|
|
2301
|
+
listener(groupId);
|
|
2302
|
+
}
|
|
2303
|
+
catch {
|
|
2304
|
+
// A listener never breaks the operation that cleared the condition.
|
|
2305
|
+
}
|
|
2306
|
+
});
|
|
2307
|
+
}
|
|
2308
|
+
/**
|
|
2309
|
+
* `competingGate` (Design Decision 6): open only when nothing is changing the group's state under the decision.
|
|
2310
|
+
* Closed, with the first reason that holds: a restore in flight, a purge, a group torn down, a group not loaded
|
|
2311
|
+
* (a load is started: its first sync ending asks for the pass that decides), a retired runtime, a fenced or
|
|
2312
|
+
* disbanded group (over for good: reported `fenced`), an own commit, self-update or proposal pending, or the
|
|
2313
|
+
* group still syncing — its engine inside the quiescence window, or its first sync since the load not ended.
|
|
2314
|
+
*/
|
|
2315
|
+
async function competingGateOp(groupId) {
|
|
2316
|
+
if (restoring.has(groupId))
|
|
2317
|
+
return { open: false, reason: 'restoring' };
|
|
2318
|
+
if (purgesInFlight.has(groupId))
|
|
2319
|
+
return { open: false, reason: 'purging' };
|
|
2320
|
+
if (abandoned.has(groupId))
|
|
2321
|
+
return { open: false, reason: 'abandoned' };
|
|
2322
|
+
const rt = runtimes.get(groupId);
|
|
2323
|
+
if (rt === undefined || (rt.disposed && !rt.abandoned)) {
|
|
2324
|
+
// Stored but not loaded: loaded now, so its first sync can end and ask for the pass that decides.
|
|
2325
|
+
if (!ctx.isDisposed())
|
|
2326
|
+
loadRuntime(groupId).catch(() => undefined);
|
|
2327
|
+
return { open: false, reason: 'not-loaded' };
|
|
2328
|
+
}
|
|
2329
|
+
if (rt.abandoned)
|
|
2330
|
+
return { open: false, reason: 'abandoned' };
|
|
2331
|
+
if (rt.fence !== undefined || rt.disbanded !== undefined)
|
|
2332
|
+
return { open: false, reason: 'fenced' };
|
|
2333
|
+
const summaries = await guard.call(groupId, 'store', () => listPendingIntentSummaries(rt.groupStore.artifact('pendingIntents')));
|
|
2334
|
+
if (summaries.some((r) => r.kind === 'commit' || r.kind === 'self-update' || r.kind === 'proposal'))
|
|
2335
|
+
return { open: false, reason: 'own-commit-pending' };
|
|
2336
|
+
// The engine's `convergenceStatuses.syncing` (not exported by marmot-engine's barrels).
|
|
2337
|
+
if (rt.engine.convergenceStatus === 'Syncing' || !rt.sendGateOpen)
|
|
2338
|
+
return { open: false, reason: 'syncing' };
|
|
2339
|
+
return { open: true };
|
|
2340
|
+
}
|
|
2341
|
+
/** The held branch of a stored or live state: its window-derived score, join facts and the tip's leaf accounts. */
|
|
2342
|
+
function heldViewOf(state, window, adoptedBy) {
|
|
2343
|
+
const epoch = Number(state.groupContext.epoch);
|
|
2344
|
+
// The window's root is the join root while it can be told from a root the horizon pruned to: a pruned root sits
|
|
2345
|
+
// exactly `REWIND_HORIZON` below the tip, so a root above that floor is the join (or creation) state, and one
|
|
2346
|
+
// carrying join evidence is the join state wherever it sits. A bare root at the floor is treated as pruned: an
|
|
2347
|
+
// untagged join exactly `REWIND_HORIZON` back is indistinguishable from it. That loses nothing: the decision's
|
|
2348
|
+
// bound (`tipEpoch - (joinEpoch - 1) <= REWIND_HORIZON`, the members' fork-parent bound) already puts a join root
|
|
2349
|
+
// that far back beyond the horizon.
|
|
2350
|
+
const root = canonicalPath(window)[0];
|
|
2351
|
+
const joinRoot = root !== undefined && (root.joinEvidence !== undefined || root.epoch > epoch - REWIND_HORIZON) ? root : undefined;
|
|
2352
|
+
const own = state.ratchetTree[Number(state.privatePath.leafIndex) * 2];
|
|
2353
|
+
return {
|
|
2354
|
+
epoch,
|
|
2355
|
+
tag: tagOf(state),
|
|
2356
|
+
joinEpoch: joinRoot?.epoch,
|
|
2357
|
+
joinTag: joinRoot?.tag,
|
|
2358
|
+
adoptedBy,
|
|
2359
|
+
pathTags: new Set(canonicalPath(window).map((n) => n.tag)),
|
|
2360
|
+
horizon: REWIND_HORIZON,
|
|
2361
|
+
score: heldBranchScore(window, state),
|
|
2362
|
+
ownLeafSignatureKey: own !== undefined && own !== null && own.nodeType === nodeTypes.leaf ? hex(own.leaf.signaturePublicKey) : undefined,
|
|
2363
|
+
memberAccounts: new Set(getGroupMemberPubkeys(state)),
|
|
2364
|
+
};
|
|
2365
|
+
}
|
|
2366
|
+
/** The group's account index entry (read through the group scope too, like a load does). */
|
|
2367
|
+
function indexEntryOf(groupId) {
|
|
2368
|
+
return guard.call(groupId, 'store', () => guard.call(ACCOUNT_SCOPE, 'store', () => readGroupIndexEntry(ctx.stores.account.extension(GROUP_INDEX_EXTENSION), groupId)));
|
|
2369
|
+
}
|
|
2370
|
+
/**
|
|
2371
|
+
* AC-FWJ-29: the keys of THIS device's directed ancestry for the held view's `ownLeafChain`: `deviceRegistry/self` and the
|
|
2372
|
+
* leaf the tip holds, each with every key from which it is reached along the recorded links (`DeviceLineage.ancestorsOf`,
|
|
2373
|
+
* not the lineage component: a live sibling installation sharing an origin key is another device). `undefined` when the
|
|
2374
|
+
* chain holds nothing beyond the held key, and when the account store cannot be read: the check then compares the
|
|
2375
|
+
* held key alone, as before, and the Welcome path is never blocked.
|
|
2376
|
+
*
|
|
2377
|
+
* The lineage and `self` are account-store reads made HERE, on their own, after the group reads and outside any group
|
|
2378
|
+
* transaction (lock order group -> account): the held view is built from frozen values.
|
|
2379
|
+
*/
|
|
2380
|
+
async function ownLeafChainOf(groupId, held) {
|
|
2381
|
+
const read = ctx.deviceLineage.read;
|
|
2382
|
+
if (read === undefined)
|
|
2383
|
+
return undefined;
|
|
2384
|
+
try {
|
|
2385
|
+
const { lineage, self } = await guard.call(groupId, 'store', () => guard.call(ACCOUNT_SCOPE, 'store', async () => ({ lineage: await read(), self: await readDeviceSigningKey((fn) => ctx.stores.account.transaction(fn)) })));
|
|
2386
|
+
const chain = new Set();
|
|
2387
|
+
for (const key of [self?.leafKey, held.ownLeafSignatureKey]) {
|
|
2388
|
+
if (key === undefined)
|
|
2389
|
+
continue;
|
|
2390
|
+
chain.add(key);
|
|
2391
|
+
for (const ancestor of lineage.ancestorsOf(key))
|
|
2392
|
+
chain.add(ancestor);
|
|
2393
|
+
}
|
|
2394
|
+
// Nothing beyond the key the tip holds (the held key is self and has no ancestors): the check is unchanged.
|
|
2395
|
+
return [...chain].some((key) => key !== held.ownLeafSignatureKey) ? chain : undefined;
|
|
2396
|
+
}
|
|
2397
|
+
catch {
|
|
2398
|
+
return undefined;
|
|
2399
|
+
}
|
|
2400
|
+
}
|
|
2401
|
+
/** `CompetingWelcomePorts.withOwnLeafChain`: `held` with the device chain (see {@link ownLeafChainOf}) when it has one. */
|
|
2402
|
+
async function withOwnLeafChain(groupId, held) {
|
|
2403
|
+
const ownLeafChain = await ownLeafChainOf(groupId, held);
|
|
2404
|
+
return ownLeafChain === undefined ? held : { ...held, ownLeafChain };
|
|
2405
|
+
}
|
|
2406
|
+
/**
|
|
2407
|
+
* `heldBranch`: the stored group's branch, loaded or not (a loaded runtime's exposed state and window are the
|
|
2408
|
+
* stored ones). `undefined` ONLY while a restore of the group is in flight or marked unfinished, and when nothing
|
|
2409
|
+
* is stored.
|
|
2410
|
+
*/
|
|
2411
|
+
async function heldBranchOp(groupId) {
|
|
2412
|
+
if (restoring.has(groupId))
|
|
2413
|
+
return undefined;
|
|
2414
|
+
const live = runtimes.get(groupId);
|
|
2415
|
+
if (live !== undefined && !live.disposed && !live.abandoned && !live.needsReload)
|
|
2416
|
+
return heldViewOf(live.exposedState, windowFor(live.window, live.exposedState), (await indexEntryOf(groupId))?.adoptedBy);
|
|
2417
|
+
if (abandoned.has(groupId))
|
|
2418
|
+
return undefined;
|
|
2419
|
+
const groupStore = groupStoreOf(groupId);
|
|
2420
|
+
if (await restorePending(groupId, groupStore))
|
|
2421
|
+
return undefined;
|
|
2422
|
+
const state = await guard.call(groupId, 'store', () => readGroupState(groupStore.artifact('clientState')));
|
|
2423
|
+
if (state === undefined)
|
|
2424
|
+
return undefined;
|
|
2425
|
+
const stored = await guard.call(groupId, 'store', () => readWindow(groupStore.artifact('rewindTree')));
|
|
2426
|
+
return heldViewOf(state, windowFor(stored, state), (await indexEntryOf(groupId))?.adoptedBy);
|
|
2427
|
+
}
|
|
2428
|
+
/** Where invitation `invitationId`'s switch to `to` stands in the STORED group (see `CompetingWelcomePorts.switchState`). */
|
|
2429
|
+
async function switchStateOp(groupId, to, invitationId) {
|
|
2430
|
+
if (abandoned.has(groupId))
|
|
2431
|
+
return 'absent';
|
|
2432
|
+
const groupStore = groupStoreOf(groupId);
|
|
2433
|
+
const state = await guard.call(groupId, 'store', () => readGroupState(groupStore.artifact('clientState')));
|
|
2434
|
+
if (state === undefined)
|
|
2435
|
+
return 'absent';
|
|
2436
|
+
if ((await guard.call(groupId, 'store', () => groupStore.extension(SWITCH_PROOF_EXTENSION).get(invitationId))) !== undefined)
|
|
2437
|
+
return 'written';
|
|
2438
|
+
const stored = await guard.call(groupId, 'store', () => readWindow(groupStore.artifact('rewindTree')));
|
|
2439
|
+
return carriesBranch(heldViewOf(state, windowFor(stored, state), undefined), to) ? 'foreign' : 'absent';
|
|
2440
|
+
}
|
|
2441
|
+
/**
|
|
2442
|
+
* The rest of a switch once the group state carries C (on the group's queue): the in-memory runtime follows the
|
|
2443
|
+
* durable state — reloaded when its window does not carry C yet (a failed rebuild, a crash recovered by a reload) —
|
|
2444
|
+
* its pre-join floor is read back from the index entry, and a history sync runs for the new branch. Idempotent. The
|
|
2445
|
+
* notifications of the switch were delivered by the process that ran the transaction (a restart loses them: the
|
|
2446
|
+
* ledger, the history outbox and the recheck marker are the durable record).
|
|
2447
|
+
*/
|
|
2448
|
+
async function finishRebase(rt, to) {
|
|
2449
|
+
if (!rt.window.nodes.has(to) || rt.needsReload)
|
|
2450
|
+
await rt.reloadFromDurable();
|
|
2451
|
+
const entry = await indexEntryOf(rt.groupId);
|
|
2452
|
+
if (entry !== undefined)
|
|
2453
|
+
rt.joinedAtMs = entry.createdAt;
|
|
2454
|
+
rt.checkSyncRouting();
|
|
2455
|
+
requestHistoryResync(rt);
|
|
2456
|
+
// AC-REKEY-11: the competing Welcome's state may carry an old-key leaf as well.
|
|
2457
|
+
scheduleRekeyFollowUp(rt);
|
|
2458
|
+
}
|
|
2459
|
+
/** The switch itself, on the group's queue (Design Decision 8): re-validated, then one group transaction. */
|
|
2460
|
+
async function rebaseOnQueue(rt, request) {
|
|
2461
|
+
const { groupId } = rt;
|
|
2462
|
+
const store = rt.groupStore;
|
|
2463
|
+
const stored = await guard.call(groupId, 'store', () => readGroupState(store.artifact('clientState')));
|
|
2464
|
+
if (stored === undefined)
|
|
2465
|
+
return 'stale';
|
|
2466
|
+
const storedWindow = windowFor(await guard.call(groupId, 'store', () => readWindow(store.artifact('rewindTree'))), stored);
|
|
2467
|
+
const held = heldViewOf(stored, storedWindow, undefined);
|
|
2468
|
+
// A crash after the transaction: this invitation's switch committed (its durable proof), whatever the tip is by
|
|
2469
|
+
// now. Nothing is validated or written again. A group that carries C WITHOUT that proof reached it by ordinary
|
|
2470
|
+
// ingest: not a switch to run (the Welcome service keeps it out as the group's own lineage).
|
|
2471
|
+
const proven = (await guard.call(groupId, 'store', () => store.extension(SWITCH_PROOF_EXTENSION).get(request.invitationId))) !== undefined;
|
|
2472
|
+
if (proven) {
|
|
2473
|
+
await finishRebase(rt, request.to);
|
|
2474
|
+
return 'switched';
|
|
2475
|
+
}
|
|
2476
|
+
if (carriesBranch(held, request.to))
|
|
2477
|
+
return 'stale';
|
|
2478
|
+
// The decision was made against another tip than the stored one: re-decided next pass.
|
|
2479
|
+
if (held.tag !== request.from)
|
|
2480
|
+
return 'stale';
|
|
2481
|
+
// The gate, the classification and the decision, against the tip as it is NOW: a rebase never runs with a closed gate.
|
|
2482
|
+
if (!(await competingGateOp(groupId)).open)
|
|
2483
|
+
return 'stale';
|
|
2484
|
+
// The chain is read here, on the queue but before the group transaction (group -> account): a `switching` marker written
|
|
2485
|
+
// while the chain could not be read, or by a build without it, must not complete a switch the chain keeps out.
|
|
2486
|
+
if (judgeCompetingWelcome(await withOwnLeafChain(groupId, held), request.competing).decision !== 'switch')
|
|
2487
|
+
return 'stale';
|
|
2488
|
+
const joined = request.state;
|
|
2489
|
+
const epoch = joined.groupContext.epoch;
|
|
2490
|
+
const joinedWindow = tipOnlyWindow(joined, request.joinEvidence);
|
|
2491
|
+
// What the abandoned branch applied is diffed against C's tip with the window floor lowered to H's root: a
|
|
2492
|
+
// tip-only window floors at C's own epoch, which would count every state of H below it as pruned, not abandoned.
|
|
2493
|
+
const joinedTip = joinedWindow.nodes.get(joinedWindow.tipTag);
|
|
2494
|
+
const hRoot = canonicalPath(storedWindow)[0];
|
|
2495
|
+
const diffWindow = { tipTag: joinedWindow.tipTag, nodes: new Map([[joinedTip.tag, { ...joinedTip, epoch: Math.min(joinedTip.epoch, hRoot?.epoch ?? joinedTip.epoch) }]]) };
|
|
2496
|
+
const transition = await planTransition(rt, storedWindow, stored, joinedWindow, joined, [], new Set(), diffWindow);
|
|
2497
|
+
// Own application messages minted on the abandoned branch that are still on their way (published, not confirmed):
|
|
2498
|
+
// no member on C can read them, and confirmed later against C's tip-only window they would read as canonical. They
|
|
2499
|
+
// are withdrawn with the branch (ledgered, announced `message-withdrawn`) and their records dropped.
|
|
2500
|
+
const inFlight = (await guard.call(groupId, 'store', () => listPendingIntentRecords(store.artifact('pendingIntents')))).filter((r) => r.kind === 'application' && r.envelope !== undefined);
|
|
2501
|
+
// The state each went out under: its staged ordering key, else read from its envelope over H's engine (as the
|
|
2502
|
+
// confirmation does) — before the engine is rebuilt.
|
|
2503
|
+
const lost = [];
|
|
2504
|
+
for (const r of inFlight) {
|
|
2505
|
+
const envelope = r.envelope;
|
|
2506
|
+
const digest = r.ordering?.acceptedDigest ?? (await ownMessage(rt, r, envelope))?.orderingKey.acceptedDigest ?? '';
|
|
2507
|
+
lost.push({ type: 'message-withdrawn', eventId: envelope.id, acceptedDigest: digest });
|
|
2508
|
+
}
|
|
2509
|
+
const plan = lost.length === 0 ? transition : { ...transition, notifications: [...transition.notifications, ...lost], withdrawn: [...transition.withdrawn, ...lost] };
|
|
2510
|
+
// The pre-join ingest floor (what the index entry and `joinedAtMs` hold) goes down to C's Welcome, and the ingest
|
|
2511
|
+
// cursor with it, so what was sent on C before the switch is fetched again and delivered. The Welcome's stamp is
|
|
2512
|
+
// the sponsor's claim: clamped to [joinedAt - grace, now] so a forged one cannot reach back for ever.
|
|
2513
|
+
const nowMs = ctx.clock.now();
|
|
2514
|
+
const welcomeMs = Math.min(Math.max(request.welcomeCreatedAtMs, (rt.joinedAtMs ?? nowMs) - ctx.graceWindowMs), nowMs);
|
|
2515
|
+
const floorMs = rt.joinedAtMs === undefined ? undefined : Math.min(rt.joinedAtMs, welcomeMs);
|
|
2516
|
+
const cursorFloor = Math.floor(welcomeMs / 1000) - CURSOR_FUTURE_SKEW_S;
|
|
2517
|
+
let restored = [];
|
|
2518
|
+
await guard.call(groupId, 'store', () => store.transaction(async (tx) => {
|
|
2519
|
+
await writeGroupState(tx.artifact('clientState'), joined);
|
|
2520
|
+
await writeWindow(tx.artifact('rewindTree'), joinedWindow);
|
|
2521
|
+
// What the abandoned state held belongs to it: exactly what a Welcome adoption clears.
|
|
2522
|
+
await clearOpaque(tx.extension(OPAQUE_EXTENSION));
|
|
2523
|
+
await clearRemoval(tx.extension(REMOVAL_EXTENSION));
|
|
2524
|
+
await clearPendingLeaf(tx.extension(PENDING_LEAF_EXTENSION));
|
|
2525
|
+
await clearPendingCommit(tx.extension(PENDING_COMMIT_EXTENSION));
|
|
2526
|
+
for (const [i, record] of inFlight.entries()) {
|
|
2527
|
+
const envelope = record.envelope;
|
|
2528
|
+
await writeTerminalLedgerEntry(tx.artifact('terminalLedger'), envelope.id, {
|
|
2529
|
+
disposition: WITHDRAWN_DISPOSITION,
|
|
2530
|
+
at: ctx.clock.now(),
|
|
2531
|
+
createdAt: envelope.created_at,
|
|
2532
|
+
...(lost[i].acceptedDigest === '' ? {} : { node: lost[i].acceptedDigest }),
|
|
2533
|
+
});
|
|
2534
|
+
await deletePendingIntentRecord(tx.artifact('pendingIntents'), record.intentId);
|
|
2535
|
+
}
|
|
2536
|
+
// The abandoned branch's messages withdrawn, its self-updates re-enqueued, the settlement-recheck marker
|
|
2537
|
+
// written (the switch is a rewind), the history backend's updates queued — one transition, one transaction.
|
|
2538
|
+
restored = await recordTransition(rt, tx, plan, epoch);
|
|
2539
|
+
const cursor = await readIngestCursor(tx.artifact('ingestCursor'));
|
|
2540
|
+
if (cursor !== undefined && (cursor.since > cursorFloor || (cursor.resume !== undefined && cursor.resume.from > cursorFloor))) {
|
|
2541
|
+
await writeIngestCursor(tx.artifact('ingestCursor'), {
|
|
2542
|
+
...cursor,
|
|
2543
|
+
since: Math.min(cursor.since, cursorFloor),
|
|
2544
|
+
...(cursor.resume === undefined ? {} : { resume: { ...cursor.resume, from: Math.min(cursor.resume.from, cursorFloor) } }),
|
|
2545
|
+
});
|
|
2546
|
+
}
|
|
2547
|
+
// The durable proof that this invitation's switch committed (it survives the window pruning the join root).
|
|
2548
|
+
await tx.extension(SWITCH_PROOF_EXTENSION).put(request.invitationId, { to: request.to });
|
|
2549
|
+
// The account's backup follows the new branch. A throw rolls the whole switch back; one raised by a hook itself is
|
|
2550
|
+
// reported as a hook refusal, which the Welcome service retries every pass without counting it toward its give-up.
|
|
2551
|
+
try {
|
|
2552
|
+
await runConfirmHooks({
|
|
2553
|
+
groupId,
|
|
2554
|
+
guard,
|
|
2555
|
+
hooks: confirmHooks.map((hook) => async (context) => {
|
|
2556
|
+
try {
|
|
2557
|
+
await hook(context);
|
|
2558
|
+
}
|
|
2559
|
+
catch (error) {
|
|
2560
|
+
throw new HookRefusal(error);
|
|
2561
|
+
}
|
|
2562
|
+
}),
|
|
2563
|
+
}, { intentId: groupId, kind: 'join', epoch, tx, persisted: () => ({ state: joined, window: joinedWindow }), notifications: [], createdAt: floorMs });
|
|
2564
|
+
}
|
|
2565
|
+
catch (cause) {
|
|
2566
|
+
if (!(cause instanceof HookRefusal))
|
|
2567
|
+
throw cause;
|
|
2568
|
+
throw new MarmotError('confirm-blocked', `rebaseToWelcome: an onConfirm hook refused the switch of group ${groupId}`, { cause: cause.error, details: { groupId, reason: 'hook', retryable: true } });
|
|
2569
|
+
}
|
|
2570
|
+
// Last, so a failure above leaves the floor where it was (a floor lowered for a switch that did not happen only refetches).
|
|
2571
|
+
if (floorMs !== undefined && floorMs < (rt.joinedAtMs ?? Infinity)) {
|
|
2572
|
+
await guard.call(ACCOUNT_SCOPE, 'store', () => ctx.stores.account.transaction(async (atx) => {
|
|
2573
|
+
const index = atx.extension(GROUP_INDEX_EXTENSION);
|
|
2574
|
+
const entry = await readGroupIndexEntry(index, groupId);
|
|
2575
|
+
if (entry !== undefined && entry.createdAt > floorMs)
|
|
2576
|
+
await writeGroupIndexEntry(index, groupId, { ...entry, createdAt: floorMs });
|
|
2577
|
+
}));
|
|
2578
|
+
}
|
|
2579
|
+
}));
|
|
2580
|
+
// Committed: the runtime follows. A failure from here leaves the durable state C's and the runtime reloaded
|
|
2581
|
+
// before its next operation; the next pass finds the stored group carrying C and finishes. The notifications and
|
|
2582
|
+
// the re-enqueues happen whatever the rebuild did (delivering never throws).
|
|
2583
|
+
let failure;
|
|
2584
|
+
try {
|
|
2585
|
+
rt.opaque = undefined;
|
|
2586
|
+
rt.removal = undefined;
|
|
2587
|
+
rt.pendingLeaf = undefined;
|
|
2588
|
+
rt.pendingCommit = undefined;
|
|
2589
|
+
await rt.rebuildEngine(copyOf(joined), joinedWindow);
|
|
2590
|
+
rt.exposedState = rt.engine.state;
|
|
2591
|
+
if (floorMs !== undefined)
|
|
2592
|
+
rt.joinedAtMs = floorMs;
|
|
2593
|
+
// Not caught up on the new branch until the sync below ran (a pass that began earlier proves nothing).
|
|
2594
|
+
rt.caughtUp = false;
|
|
2595
|
+
rt.connectivityGeneration += 1;
|
|
2596
|
+
}
|
|
2597
|
+
catch (error) {
|
|
2598
|
+
rt.needsReload = true;
|
|
2599
|
+
failure = { error };
|
|
2600
|
+
}
|
|
2601
|
+
await deliverTransition(rt, plan);
|
|
2602
|
+
const gone = new MarmotError('send-rejected', `group ${groupId} switched branch before the message was confirmed`, { details: { groupId, reason: 'branch-switched' } });
|
|
2603
|
+
for (const record of inFlight)
|
|
2604
|
+
notifySettled(rt, record, gone);
|
|
2605
|
+
scheduleWithdrawnRedrives(rt, plan);
|
|
2606
|
+
for (const intentId of restored)
|
|
2607
|
+
scheduleDeferredReenqueueRetry(rt, intentId);
|
|
2608
|
+
if (failure !== undefined)
|
|
2609
|
+
throw failure.error;
|
|
2610
|
+
// The lowered floor reaches the next pass even if a live event advances the stored cursor first (nextCursor would
|
|
2611
|
+
// recompute `since` from `high`): the catch-up floor is read afresh by that pass (BACKLOG welcome-race-rebase-lowers-only-stored).
|
|
2612
|
+
// Set before anything can start a pass. Residual: on the rebuild-failure path above the runtime reloads with no catch-up
|
|
2613
|
+
// floor and relies on the stored (lowered) `since` alone.
|
|
2614
|
+
const reachBack = cursorFloor - CURSOR_REDELIVERY_MARGIN_S;
|
|
2615
|
+
rt.catchUpSince = rt.catchUpSince === undefined ? reachBack : Math.min(rt.catchUpSince, reachBack);
|
|
2616
|
+
rt.checkSyncRouting();
|
|
2617
|
+
requestHistoryResync(rt);
|
|
2618
|
+
// AC-REKEY-11: the branch switched to may hold this device's leaf on a retired key (detached; queues only if owed).
|
|
2619
|
+
scheduleRekeyFollowUp(rt);
|
|
2620
|
+
return 'switched';
|
|
2621
|
+
}
|
|
2622
|
+
/** `rebaseToWelcome` (see {@link CompetingWelcomePorts.rebaseToWelcome}). */
|
|
2623
|
+
async function rebaseToWelcomeOp(request) {
|
|
2624
|
+
const { groupId } = request;
|
|
2625
|
+
// Refused without changing any store: a restore in flight decides the group first (AC-FWJ-25); a group torn down
|
|
2626
|
+
// is gone.
|
|
2627
|
+
if (restoring.has(groupId) || purgesInFlight.has(groupId) || abandoned.has(groupId))
|
|
2628
|
+
return 'stale';
|
|
2629
|
+
const rt = await loadRuntime(groupId);
|
|
2630
|
+
if (rt === undefined || rt.abandoned || rt.disposed)
|
|
2631
|
+
return 'stale';
|
|
2632
|
+
return rt.run('rebaseToWelcome', (rtc) => rebaseOnQueue(rtc, request)).catch((error) => {
|
|
2633
|
+
// A purge retired the runtime while the switch waited on the queue.
|
|
2634
|
+
if (rt.abandoned && isMarmotError(error, 'not-a-member'))
|
|
2635
|
+
return 'stale';
|
|
2636
|
+
throw error;
|
|
2637
|
+
});
|
|
2638
|
+
}
|
|
2639
|
+
const competingPorts = {
|
|
2640
|
+
competingGate: competingGateOp,
|
|
2641
|
+
heldBranch: heldBranchOp,
|
|
2642
|
+
withOwnLeafChain,
|
|
2643
|
+
rebaseToWelcome: rebaseToWelcomeOp,
|
|
2644
|
+
switchState: switchStateOp,
|
|
2645
|
+
onBranchSwitched(switched) {
|
|
2646
|
+
ctx.emit('groupBranchSwitched', { invitationId: switched.invitationId, groupId: switched.groupId, from: switched.from, to: switched.to });
|
|
2647
|
+
},
|
|
2648
|
+
};
|
|
2649
|
+
return {
|
|
2650
|
+
devicePort,
|
|
2651
|
+
teardownPort,
|
|
2652
|
+
groups: {
|
|
2653
|
+
create: createGroupOp,
|
|
2654
|
+
get: getGroupOp,
|
|
2655
|
+
list: listGroupsOp,
|
|
2656
|
+
restore: restoreGroupOp,
|
|
2657
|
+
},
|
|
2658
|
+
adoptJoinedGroup: adoptJoinedGroupOp,
|
|
2659
|
+
adoptionProofOf: (groupId, invitationId) => guard.call(groupId, 'store', () => readAdoptionProofOf(groupStoreOf(groupId), invitationId)),
|
|
2660
|
+
sessionFenced: (groupId) => availabilityClient.sessionFences.get(groupId) !== undefined || runtimes.get(groupId)?.fence !== undefined,
|
|
2661
|
+
competing: competingPorts,
|
|
2662
|
+
onGateMayHaveCleared(listener) {
|
|
2663
|
+
gateClearedListener = listener;
|
|
2664
|
+
},
|
|
2665
|
+
hasGroup: hasGroupOp,
|
|
2666
|
+
onRestoreReleased(listener) {
|
|
2667
|
+
restoreReleased = listener;
|
|
2668
|
+
},
|
|
2669
|
+
onConfirm(hook) {
|
|
2670
|
+
if (typeof hook !== 'function')
|
|
2671
|
+
throw new MarmotError('invalid-options', 'extensions.onConfirm: hook must be a function');
|
|
2672
|
+
confirmHooks.push(hook);
|
|
2673
|
+
return () => {
|
|
2674
|
+
const i = confirmHooks.indexOf(hook);
|
|
2675
|
+
if (i >= 0)
|
|
2676
|
+
confirmHooks.splice(i, 1);
|
|
2677
|
+
};
|
|
2678
|
+
},
|
|
2679
|
+
checkProposalDeadlines() {
|
|
2680
|
+
for (const rt of runtimes.values())
|
|
2681
|
+
if (!rt.disposed && !rt.abandoned)
|
|
2682
|
+
scheduleProposalDeadlineCheck(rt);
|
|
2683
|
+
},
|
|
2684
|
+
resumeUnfinishedRestores() {
|
|
2685
|
+
if (ctx.isDisposed() || resumeScan !== undefined)
|
|
2686
|
+
return;
|
|
2687
|
+
if (indexScanned && leftMarked.size === 0 && leftRecords.size === 0)
|
|
2688
|
+
return;
|
|
2689
|
+
guard.detached(() => {
|
|
2690
|
+
const scan = (async () => {
|
|
2691
|
+
let ids;
|
|
2692
|
+
let records;
|
|
2693
|
+
if (indexScanned) {
|
|
2694
|
+
ids = [...leftMarked];
|
|
2695
|
+
leftMarked.clear();
|
|
2696
|
+
records = [...leftRecords];
|
|
2697
|
+
leftRecords.clear();
|
|
2698
|
+
}
|
|
2699
|
+
else {
|
|
2700
|
+
// The adoptions the index may not name yet first (a crash mid-adoption), then the whole index.
|
|
2701
|
+
records = await guard.call(ACCOUNT_SCOPE, 'store', () => listRestoringRecords(ctx.stores.account.extension(RESTORING_EXTENSION)));
|
|
2702
|
+
ids = await guard.call(ACCOUNT_SCOPE, 'store', () => listGroupIndex(ctx.stores.account.extension(GROUP_INDEX_EXTENSION)));
|
|
2703
|
+
indexScanned = true;
|
|
2704
|
+
}
|
|
2705
|
+
for (const id of records) {
|
|
2706
|
+
if (ctx.isDisposed())
|
|
2707
|
+
return;
|
|
2708
|
+
try {
|
|
2709
|
+
await settleRestoringRecord(id);
|
|
2710
|
+
}
|
|
2711
|
+
catch {
|
|
2712
|
+
if (!abandoned.has(id))
|
|
2713
|
+
leftRecords.add(id);
|
|
2714
|
+
}
|
|
2715
|
+
}
|
|
2716
|
+
for (const id of ids) {
|
|
2717
|
+
if (ctx.isDisposed())
|
|
2718
|
+
return;
|
|
2719
|
+
try {
|
|
2720
|
+
await unfinishedRestore(id);
|
|
2721
|
+
}
|
|
2722
|
+
catch {
|
|
2723
|
+
if (!abandoned.has(id))
|
|
2724
|
+
leftMarked.add(id);
|
|
2725
|
+
}
|
|
2726
|
+
}
|
|
2727
|
+
})();
|
|
2728
|
+
resumeScan = scan;
|
|
2729
|
+
void scan
|
|
2730
|
+
.catch(() => undefined)
|
|
2731
|
+
.finally(() => {
|
|
2732
|
+
if (resumeScan === scan)
|
|
2733
|
+
resumeScan = undefined;
|
|
2734
|
+
});
|
|
2735
|
+
});
|
|
2736
|
+
},
|
|
2737
|
+
endAvailabilityForLease() {
|
|
2738
|
+
// The store's lease-end signal (A-5): in memory only — no group work, store access or transport call. Every
|
|
2739
|
+
// pending `synced()` wait fails `no-lease` now (the send gate never opens without the lease); a later one fails
|
|
2740
|
+
// at once through the client-wide flag. The waits fail before any availability listener runs: one reacting to its
|
|
2741
|
+
// `ended/'no-lease'` (a `dispose()`) must not turn the lease loss that came first into `disposed`.
|
|
2742
|
+
const deliverAvailabilityEnds = beginLeaseEndAvailability(availabilityClient, runtimes.values());
|
|
2743
|
+
const cause = accountLeaseEndedError();
|
|
2744
|
+
for (const rt of [...runtimes.values()])
|
|
2745
|
+
rejectSyncWaiters(rt, () => syncedLeaseLostError(rt, cause));
|
|
2746
|
+
deliverAvailabilityEnds();
|
|
2747
|
+
},
|
|
2748
|
+
beginDispose() {
|
|
2749
|
+
try {
|
|
2750
|
+
disposeAvailabilityDeliveries.push(beginDisposeAvailability(availabilityClient, runtimes.values()));
|
|
2751
|
+
}
|
|
2752
|
+
catch {
|
|
2753
|
+
// dispose() records whatever this missed
|
|
2754
|
+
}
|
|
2755
|
+
},
|
|
2756
|
+
dispose() {
|
|
2757
|
+
// Every runtime is torn down even when a step throws (a transport
|
|
2758
|
+
// unsubscribe, an engine dispose): the errors are returned, not thrown.
|
|
2759
|
+
const errors = [];
|
|
2760
|
+
// Every runtime's `ended/'disposed'` is recorded before any is delivered (and delivered only once all are torn
|
|
2761
|
+
// down): a listener of one group's `ended` never finds another group of this disposed client still open.
|
|
2762
|
+
// A runtime registered after `beginDispose` is marked here (the ones it marked are not marked twice).
|
|
2763
|
+
disposeAvailabilityDeliveries.push(beginDisposeAvailability(availabilityClient, runtimes.values()));
|
|
2764
|
+
for (const rt of runtimes.values()) {
|
|
2765
|
+
rt.disposed = true;
|
|
2766
|
+
for (const handle of rt.pipelineTimers) {
|
|
2767
|
+
try {
|
|
2768
|
+
ctx.timers.clear(handle);
|
|
2769
|
+
}
|
|
2770
|
+
catch (error) {
|
|
2771
|
+
errors.push(error);
|
|
2772
|
+
}
|
|
2773
|
+
}
|
|
2774
|
+
rt.pipelineTimers.clear();
|
|
2775
|
+
rt.reenqueueRetryArmed.clear();
|
|
2776
|
+
for (const waiter of rt.queueWaiters.values())
|
|
2777
|
+
waiter.reject(new MarmotError('disposed', 'send: client was disposed before the queued rumor was published'));
|
|
2778
|
+
rt.queueWaiters.clear();
|
|
2779
|
+
for (const waiter of rt.syncWaiters)
|
|
2780
|
+
waiter.reject(new MarmotError('disposed', 'synced: client was disposed before the group completed its first sync'));
|
|
2781
|
+
rt.syncWaiters.clear();
|
|
2782
|
+
endRuntime(rt, { reason: 'disposed' });
|
|
2783
|
+
try {
|
|
2784
|
+
stopGroupSync(rt);
|
|
2785
|
+
}
|
|
2786
|
+
catch (error) {
|
|
2787
|
+
errors.push(error);
|
|
2788
|
+
}
|
|
2789
|
+
try {
|
|
2790
|
+
rt.engine.dispose();
|
|
2791
|
+
}
|
|
2792
|
+
catch (error) {
|
|
2793
|
+
errors.push(error);
|
|
2794
|
+
}
|
|
2795
|
+
}
|
|
2796
|
+
for (const restore of restoring.values())
|
|
2797
|
+
restore.abort();
|
|
2798
|
+
markClientDisposed();
|
|
2799
|
+
runtimes.clear();
|
|
2800
|
+
loading.clear();
|
|
2801
|
+
openedStores.clear();
|
|
2802
|
+
confirmHooks.length = 0;
|
|
2803
|
+
rewindListeners.clear();
|
|
2804
|
+
restoredListeners.clear();
|
|
2805
|
+
rotationListeners.clear();
|
|
2806
|
+
for (const deliver of disposeAvailabilityDeliveries.splice(0))
|
|
2807
|
+
deliver();
|
|
2808
|
+
return errors;
|
|
2809
|
+
},
|
|
2810
|
+
};
|
|
2811
|
+
}
|