@kumiai/rpc 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/classify.d.ts +132 -0
- package/lib/classify.js +48 -0
- package/lib/commit-frame.d.ts +32 -0
- package/lib/commit-frame.js +43 -0
- package/lib/commit.d.ts +188 -0
- package/lib/commit.js +50 -0
- package/lib/crypto.d.ts +205 -32
- package/lib/crypto.js +16 -8
- package/lib/cursor.d.ts +33 -0
- package/lib/cursor.js +26 -0
- package/lib/directed-crypto.d.ts +17 -0
- package/lib/directed-crypto.js +80 -0
- package/lib/directed.d.ts +11 -4
- package/lib/directed.js +168 -29
- package/lib/handshake.d.ts +13 -6
- package/lib/handshake.js +12 -7
- package/lib/hub-mux.d.ts +51 -12
- package/lib/hub-mux.js +66 -26
- package/lib/index.d.ts +9 -5
- package/lib/index.js +8 -3
- package/lib/ledger-entries.d.ts +22 -0
- package/lib/ledger-entries.js +78 -0
- package/lib/peer.d.ts +105 -15
- package/lib/peer.js +1009 -100
- package/lib/recovery.d.ts +27 -7
- package/lib/recovery.js +73 -18
- package/lib/topic.d.ts +18 -7
- package/lib/topic.js +18 -7
- package/package.json +11 -10
- package/lib/memory-group-mls.d.ts +0 -21
- package/lib/memory-group-mls.js +0 -55
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import type { CommitHeader } from './crypto.js';
|
|
2
|
+
/**
|
|
3
|
+
* What the lane does with one frame from the commit log. Six rows, in EVALUATION order — the
|
|
4
|
+
* order is load-bearing, and this table is the `if` chain of {@link classifyCommit} plus the
|
|
5
|
+
* port's answer to its last row:
|
|
6
|
+
*
|
|
7
|
+
* | Frame | Cursor |
|
|
8
|
+
* |---|---|
|
|
9
|
+
* | Not a commit at all (unreadable header) | advance (poison — never retry, never heal); settled first, before any epoch question |
|
|
10
|
+
* | Framed at an epoch AHEAD of this peer's | advance; heal — the group moved on without it |
|
|
11
|
+
* | Below this peer's epoch, with no recorded applied-commit | advance, no fork check, no unwrap attempt |
|
|
12
|
+
* | Below this peer's epoch, with a record naming a different sequenceID | advance; the fork trigger |
|
|
13
|
+
* | At this peer's current epoch, committed by THIS peer | do not advance; heal |
|
|
14
|
+
* | At this peer's current epoch, committed by another — handed to the port | applied: advance and record this epoch -> sequenceID for the fork check; refused by policy or entries unresolvable: advance (poison — never retry, never heal) |
|
|
15
|
+
*
|
|
16
|
+
* Five rows are decided here, reading bytes and decrypting nothing; only the last — a frame at
|
|
17
|
+
* this peer's epoch authored by someone else — is handed to the MLS port, whose answer is the
|
|
18
|
+
* sixth row. Malformed-poison settles at the top because headerless bytes cannot be asked the
|
|
19
|
+
* epoch questions the other rows turn on.
|
|
20
|
+
*
|
|
21
|
+
* **Classify by epoch first, unwrap only what you can apply.** A frame's blob is sealed under
|
|
22
|
+
* the epoch the commit is framed at, so a peer walking history (late joiner, rejoiner,
|
|
23
|
+
* re-seeded) reaches frames it can never open — including the commit that added it. Unwrapping
|
|
24
|
+
* is a consequence of "I can apply this frame", never a precondition of reading it; unwrapping
|
|
25
|
+
* before classifying files ordinary history as a decryption failure.
|
|
26
|
+
*/
|
|
27
|
+
export type CommitDisposition =
|
|
28
|
+
/**
|
|
29
|
+
* Framed at this peer's epoch, committed by somebody else: applicable. Hand to the MLS port
|
|
30
|
+
* — and only now may its blob be opened, since it is sealed under the epoch this peer holds.
|
|
31
|
+
*/
|
|
32
|
+
{
|
|
33
|
+
row: 'apply';
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* A frame framed at an epoch AHEAD of this peer's: the group advanced where this peer did
|
|
37
|
+
* not, proof it has fallen out of the group's line. Advance, and heal.
|
|
38
|
+
*
|
|
39
|
+
* This is the only signal that a peer's own state is broken, so no other row needs to heal
|
|
40
|
+
* a dropped frame: a frame nobody can resolve blocks the whole group (nobody applies it),
|
|
41
|
+
* whereas a frame only THIS peer cannot resolve is a fault of its own — observable only
|
|
42
|
+
* later, when the group commits past it and lands here.
|
|
43
|
+
*/
|
|
44
|
+
| {
|
|
45
|
+
row: 'ahead';
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* A frame from an epoch BELOW this peer's with no applied-commit record: pre-join,
|
|
49
|
+
* pre-rejoin, or re-seeded history. Advance, no fork check, no unwrap. Neither fork nor
|
|
50
|
+
* poison — every healthy peer reads some.
|
|
51
|
+
*/
|
|
52
|
+
| {
|
|
53
|
+
row: 'history';
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Two different commits at one epoch: this peer applied `appliedSequenceID`, the log now
|
|
57
|
+
* carries another. The hub accepted both — only possible by serving different logs to
|
|
58
|
+
* different members. Advance, and heal if on the losing branch: the branch whose commit
|
|
59
|
+
* carries the HIGHER sequenceID, a tiebreak both sides evaluate alone once they see both.
|
|
60
|
+
*/
|
|
61
|
+
| {
|
|
62
|
+
row: 'fork';
|
|
63
|
+
appliedSequenceID: string;
|
|
64
|
+
branch: 'winning' | 'losing';
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* This peer's OWN commit, framed at the epoch it is STILL at. MLS MERGES a pending commit,
|
|
68
|
+
* it does not process one, so a peer can never apply the frame that is its own commit; the
|
|
69
|
+
* pending state died with the process that built it. Do not advance; heal.
|
|
70
|
+
*
|
|
71
|
+
* **Discriminate by authorship, not applicability.** "A frame at my epoch I cannot apply"
|
|
72
|
+
* matches every unapplied frame, so it would swallow the two rows below (policy-refused, and
|
|
73
|
+
* unresolvable entries) — a member-triggerable group-wide DoS: any member (including a
|
|
74
|
+
* removed one, who keeps the commit topic forever) publishes one well-formed policy-refused
|
|
75
|
+
* commit and every honest peer heals at once. Authorship stops the overlap.
|
|
76
|
+
*
|
|
77
|
+
* Read the committer from the commit itself, where MLS authenticates it — NEVER from the
|
|
78
|
+
* frame's transport sender, the untrusted hub's word. A hub that stamped each recipient's
|
|
79
|
+
* own DID onto one poison frame would otherwise heal the whole group at will.
|
|
80
|
+
*/
|
|
81
|
+
| {
|
|
82
|
+
row: 'own-unmerged';
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Not a commit at all. Advance — poison is stepped over, never retried. It is the LAST
|
|
86
|
+
* resort, never the fallback for "I could not apply this": a crash victim's own commit
|
|
87
|
+
* misfiled as malformed would walk the peer to the log's end stuck at its epoch forever.
|
|
88
|
+
*
|
|
89
|
+
* Two more cases are filed here on the port's answer, not this classification: a
|
|
90
|
+
* policy-refused commit, and a commit whose ledger entries will not resolve. Both advance,
|
|
91
|
+
* NEITHER heals — anything else hands any member a group-wide recovery storm per publish.
|
|
92
|
+
*
|
|
93
|
+
* Dropping an unresolvable-entries commit leaves this peer at an epoch it did not pass, and
|
|
94
|
+
* it may still commit from there onto a branch the group ignores. Known, accepted, bounded,
|
|
95
|
+
* and not attacker-reachable: bodies are sealed under the commit's epoch, which every member
|
|
96
|
+
* at that epoch holds, so a frame resolves for all or none — a peer that alone cannot resolve
|
|
97
|
+
* one was already on another branch. And the group's next commit is `ahead`, which heals it.
|
|
98
|
+
*
|
|
99
|
+
* Refusing to commit from such an epoch is the obvious guard and WORSE THAN THE BUG: when
|
|
100
|
+
* nobody can resolve the frame, every honest member skipped that epoch and would refuse,
|
|
101
|
+
* while the publisher sits an epoch ahead alone — no one can publish the commit that unsticks
|
|
102
|
+
* the group, and one unresolvable frame kills it permanently.
|
|
103
|
+
*/
|
|
104
|
+
| {
|
|
105
|
+
row: 'poison';
|
|
106
|
+
};
|
|
107
|
+
/** What the classifier reads about this peer. Nothing here needs the network or a key. */
|
|
108
|
+
export type CommitClassifierState = {
|
|
109
|
+
/** This peer's own identity — the DID that authenticates a commit this peer authored. */
|
|
110
|
+
localDID: string;
|
|
111
|
+
/** The epoch this peer's group handle is at right now. */
|
|
112
|
+
epoch: number;
|
|
113
|
+
/**
|
|
114
|
+
* The sequenceID of the commit this peer enacted at each epoch it has passed.
|
|
115
|
+
*
|
|
116
|
+
* In memory, DELIBERATELY. A restarted peer holds none and reads history as history: it can
|
|
117
|
+
* MISS a fork but can never invent one — the safe direction, since inventing forks would
|
|
118
|
+
* turn every late joiner/rejoiner/re-seeded peer into a recovery storm on first pull, while a
|
|
119
|
+
* missed fork is re-detected by the trim and `ahead` triggers on the next published frame.
|
|
120
|
+
* Durability needs a second host-provided store with nothing yet to spend it on: fork
|
|
121
|
+
* RESOLUTION (losing branch rejoining and re-enacting) is not built. Revisit when it is.
|
|
122
|
+
*/
|
|
123
|
+
appliedByEpoch: ReadonlyMap<number, string>;
|
|
124
|
+
};
|
|
125
|
+
/**
|
|
126
|
+
* Classify one frame of the commit log against this peer's state, before anything is
|
|
127
|
+
* applied and before anything is decrypted.
|
|
128
|
+
*
|
|
129
|
+
* `header` is the commit's own epoch and committer, read out of the commit bytes by the MLS
|
|
130
|
+
* port — `null` for bytes that are not a commit at all.
|
|
131
|
+
*/
|
|
132
|
+
export declare function classifyCommit(header: CommitHeader | null, sequenceID: string, state: CommitClassifierState): CommitDisposition;
|
package/lib/classify.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Classify one frame of the commit log against this peer's state, before anything is
|
|
3
|
+
* applied and before anything is decrypted.
|
|
4
|
+
*
|
|
5
|
+
* `header` is the commit's own epoch and committer, read out of the commit bytes by the MLS
|
|
6
|
+
* port — `null` for bytes that are not a commit at all.
|
|
7
|
+
*/ export function classifyCommit(header, sequenceID, state) {
|
|
8
|
+
// Headerless bytes cannot be asked the questions below, so settle first. Overlaps no other
|
|
9
|
+
// row: an unreadable frame is not somebody's commit, least of all this peer's own.
|
|
10
|
+
if (header == null) return {
|
|
11
|
+
row: 'poison'
|
|
12
|
+
};
|
|
13
|
+
// AHEAD, settled before `history`: the two are otherwise indistinguishable, since a peer
|
|
14
|
+
// holds no applied-commit record for an epoch it never reached any more than for one it was
|
|
15
|
+
// never part of — but the first is a peer fallen out of the group, the second is history.
|
|
16
|
+
//
|
|
17
|
+
// Cannot fire for a healthy peer: commits advance epochs and each compare-and-sets at the
|
|
18
|
+
// head, so the log runs in non-decreasing epoch order and a peer walking it advances in
|
|
19
|
+
// lockstep — the next frame is never ahead. A Welcome joiner at epoch N reads all frames
|
|
20
|
+
// below N as history, applies N, and rises with the log. So a frame ahead of this peer means
|
|
21
|
+
// exactly one thing: the group advanced at an epoch this peer did not (trimmed frames, or a
|
|
22
|
+
// frame it alone could not apply). Advance, and heal.
|
|
23
|
+
if (header.epoch > state.epoch) return {
|
|
24
|
+
row: 'ahead'
|
|
25
|
+
};
|
|
26
|
+
if (header.epoch < state.epoch) {
|
|
27
|
+
const applied = state.appliedByEpoch.get(header.epoch);
|
|
28
|
+
// No record for that epoch -> history, not fork. "A commit at an epoch the peer already
|
|
29
|
+
// passed" is NOT the fork test: a late joiner, rejoiner, or re-seeded peer walks epochs it
|
|
30
|
+
// never passed, and would falsely diagnose a fork on first pull.
|
|
31
|
+
if (applied == null || applied === sequenceID) return {
|
|
32
|
+
row: 'history'
|
|
33
|
+
};
|
|
34
|
+
return {
|
|
35
|
+
row: 'fork',
|
|
36
|
+
appliedSequenceID: applied,
|
|
37
|
+
branch: sequenceID < applied ? 'losing' : 'winning'
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
// At this peer's epoch. Discriminate by authorship, NOT applicability: a peer can never
|
|
41
|
+
// apply its own commit, and a frame it merely fails to apply is not a reason to heal.
|
|
42
|
+
if (header.committerDID === state.localDID) return {
|
|
43
|
+
row: 'own-unmerged'
|
|
44
|
+
};
|
|
45
|
+
return {
|
|
46
|
+
row: 'apply'
|
|
47
|
+
};
|
|
48
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The commit lane's frame: an MLS Commit plus the ledger-entry bodies that Commit enacts,
|
|
3
|
+
* sealed under the epoch secret the Commit is framed at.
|
|
4
|
+
*
|
|
5
|
+
* [ commitLength(4, LE) | commit bytes | sealed entry blob... ]
|
|
6
|
+
*
|
|
7
|
+
* Length-delimited halves, so the commit half is read WITHOUT touching the blob. Body
|
|
8
|
+
* delivery is atomic with the commit: a peer is never told of a body before the commit that
|
|
9
|
+
* enacts it.
|
|
10
|
+
*
|
|
11
|
+
* NOTHING IN THIS MODULE IMPORTS CRYPTO — reading a frame cannot decrypt, so "read this
|
|
12
|
+
* frame" and "opened its blob" cannot be conflated. Every peer walking the log reaches frames
|
|
13
|
+
* sealed under an epoch secret it does not hold (the late joiner reaches the commit that added
|
|
14
|
+
* it); an unopenable blob is history, not poison — read the commit half, step over the frame.
|
|
15
|
+
* The blob is opened by {@link "ledger-entries".createLedgerEntryResolver}, only for a commit
|
|
16
|
+
* the peer is applying.
|
|
17
|
+
*/
|
|
18
|
+
/** A commit frame's two halves, as bytes. The blob is opaque here — sealed, unread. */
|
|
19
|
+
export type CommitFrame = {
|
|
20
|
+
/** The framed MLS Commit. */
|
|
21
|
+
commit: Uint8Array;
|
|
22
|
+
/** The sealed ledger-entry bodies this Commit enacts. Opaque until opened. */
|
|
23
|
+
sealedEntries: Uint8Array;
|
|
24
|
+
};
|
|
25
|
+
/** Frame a Commit with the sealed blob of the entry bodies it enacts. */
|
|
26
|
+
export declare function encodeCommitFrame(commit: Uint8Array, sealedEntries: Uint8Array): Uint8Array;
|
|
27
|
+
/**
|
|
28
|
+
* Split a commit frame into its two halves. Throws only on bytes that are not a frame at
|
|
29
|
+
* all — too short, or a length that runs past the end. An unopenable blob is not a
|
|
30
|
+
* decoding failure: this never looks at the blob.
|
|
31
|
+
*/
|
|
32
|
+
export declare function decodeCommitFrame(frame: Uint8Array): CommitFrame;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The commit lane's frame: an MLS Commit plus the ledger-entry bodies that Commit enacts,
|
|
3
|
+
* sealed under the epoch secret the Commit is framed at.
|
|
4
|
+
*
|
|
5
|
+
* [ commitLength(4, LE) | commit bytes | sealed entry blob... ]
|
|
6
|
+
*
|
|
7
|
+
* Length-delimited halves, so the commit half is read WITHOUT touching the blob. Body
|
|
8
|
+
* delivery is atomic with the commit: a peer is never told of a body before the commit that
|
|
9
|
+
* enacts it.
|
|
10
|
+
*
|
|
11
|
+
* NOTHING IN THIS MODULE IMPORTS CRYPTO — reading a frame cannot decrypt, so "read this
|
|
12
|
+
* frame" and "opened its blob" cannot be conflated. Every peer walking the log reaches frames
|
|
13
|
+
* sealed under an epoch secret it does not hold (the late joiner reaches the commit that added
|
|
14
|
+
* it); an unopenable blob is history, not poison — read the commit half, step over the frame.
|
|
15
|
+
* The blob is opened by {@link "ledger-entries".createLedgerEntryResolver}, only for a commit
|
|
16
|
+
* the peer is applying.
|
|
17
|
+
*/ const COMMIT_LENGTH_BYTES = 4;
|
|
18
|
+
/** Frame a Commit with the sealed blob of the entry bodies it enacts. */ export function encodeCommitFrame(commit, sealedEntries) {
|
|
19
|
+
const frame = new Uint8Array(COMMIT_LENGTH_BYTES + commit.length + sealedEntries.length);
|
|
20
|
+
new DataView(frame.buffer).setUint32(0, commit.length, true);
|
|
21
|
+
frame.set(commit, COMMIT_LENGTH_BYTES);
|
|
22
|
+
frame.set(sealedEntries, COMMIT_LENGTH_BYTES + commit.length);
|
|
23
|
+
return frame;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Split a commit frame into its two halves. Throws only on bytes that are not a frame at
|
|
27
|
+
* all — too short, or a length that runs past the end. An unopenable blob is not a
|
|
28
|
+
* decoding failure: this never looks at the blob.
|
|
29
|
+
*/ export function decodeCommitFrame(frame) {
|
|
30
|
+
if (frame.length < COMMIT_LENGTH_BYTES) {
|
|
31
|
+
throw new Error('commit frame is too short');
|
|
32
|
+
}
|
|
33
|
+
const view = new DataView(frame.buffer, frame.byteOffset, frame.byteLength);
|
|
34
|
+
const commitLength = view.getUint32(0, true);
|
|
35
|
+
const blobStart = COMMIT_LENGTH_BYTES + commitLength;
|
|
36
|
+
if (frame.length < blobStart) {
|
|
37
|
+
throw new Error('commit frame is truncated: the commit runs past the end of the frame');
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
commit: frame.subarray(COMMIT_LENGTH_BYTES, blobStart),
|
|
41
|
+
sealedEntries: frame.subarray(blobStart)
|
|
42
|
+
};
|
|
43
|
+
}
|
package/lib/commit.d.ts
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a commit was, so a restart can route on it without parsing the framed commit. `ledger` is
|
|
3
|
+
* fully re-issuable from its bodies after a crash (signed, epoch-independent tokens). The
|
|
4
|
+
* proposal-carrying kinds are not: the intent lives in the MLS Add/Remove proposal and the
|
|
5
|
+
* KeyPackage, neither of which survives the process that built them.
|
|
6
|
+
*/
|
|
7
|
+
export type CommitKind = 'ledger' | 'invite' | 'remove';
|
|
8
|
+
/**
|
|
9
|
+
* A commit the host has BUILT but not adopted. MLS commits are non-mutating — they return a
|
|
10
|
+
* derived handle and never advance the source — so the host's live handle is still the
|
|
11
|
+
* pre-commit one, which is what lets the peer seal the bodies under the epoch every receiver of
|
|
12
|
+
* this commit is at.
|
|
13
|
+
*/
|
|
14
|
+
export type PendingCommit = {
|
|
15
|
+
/** Framed MLSMessage(Commit) bytes. */
|
|
16
|
+
commit: Uint8Array;
|
|
17
|
+
/** The signed ledger-entry tokens this commit enacts. Empty for a commit that enacts none. */
|
|
18
|
+
bodies: Array<string>;
|
|
19
|
+
kind: CommitKind;
|
|
20
|
+
/**
|
|
21
|
+
* Opaque host blob holding everything needed to adopt this commit after a restart: the
|
|
22
|
+
* serialized post-commit handle and any Welcome to deliver. Written to the journal BEFORE the
|
|
23
|
+
* peer publishes, handed straight back to `adoptJournalled` on replay. The peer never inspects it.
|
|
24
|
+
*/
|
|
25
|
+
journal: Uint8Array;
|
|
26
|
+
/**
|
|
27
|
+
* Runs only if the hub accepts. Adopt the post-commit handle here and send any Welcome. The
|
|
28
|
+
* ONLY place the host may adopt: adopting earlier rotates past the epoch the bodies must be
|
|
29
|
+
* sealed under, adopting later leaves the handle behind the group.
|
|
30
|
+
*
|
|
31
|
+
* ENFORCED: the peer records acceptance in the journal BEFORE this runs, so a restart can tell
|
|
32
|
+
* a commit that landed from one whose fate is unknown. An entry with no recorded acceptance,
|
|
33
|
+
* found at an epoch past the one it was framed at, is a host that adopted elsewhere — the peer
|
|
34
|
+
* REFUSES it with a {@link JournalEpochError} rather than re-sealing bodies under an epoch no
|
|
35
|
+
* receiver holds. Not replayed, slot kept.
|
|
36
|
+
*
|
|
37
|
+
* MUST be idempotent — it can run more than once. *publish → record acceptance → `onAccepted()`
|
|
38
|
+
* → clear slot* is four steps; a crash between any two leaves an entry whose `onAccepted` partly
|
|
39
|
+
* ran still in the slot, and it is replayed. Re-adopting the journalled handle is harmless (a
|
|
40
|
+
* fixed serialized value).
|
|
41
|
+
*
|
|
42
|
+
* So a Welcome is delivered AT LEAST ONCE, deliberately: suppressing the replayed repeat would
|
|
43
|
+
* strand an invitee added to a group and never told, which is why the Welcome is journalled. The
|
|
44
|
+
* sender does not deduplicate and must not try.
|
|
45
|
+
*
|
|
46
|
+
* The repeat is safe only because the invitee absorbs it: it joins with `processWelcomeOnce`
|
|
47
|
+
* from `@kumiai/mls`, which returns `null` for a Welcome whose group it already holds. Plain
|
|
48
|
+
* `processWelcome` does NOT — a pure function with no registry, so a repeat builds a second
|
|
49
|
+
* group state at the joining epoch, and adopting it rolls the member back (every member added
|
|
50
|
+
* since gone from its roster, group unreadable) with no error anywhere.
|
|
51
|
+
*/
|
|
52
|
+
onAccepted: () => Promise<void>;
|
|
53
|
+
};
|
|
54
|
+
/** A pending commit as it sits in the journal: what the peer needs to republish it. */
|
|
55
|
+
export type JournalEntry = {
|
|
56
|
+
/** The idempotency key the commit was published with, and is replayed with. */
|
|
57
|
+
publishID: string;
|
|
58
|
+
/** The head the publish was conditional on. `null`: the topic had no log publish yet. */
|
|
59
|
+
expectedHead: string | null;
|
|
60
|
+
/**
|
|
61
|
+
* The epoch the commit was FRAMED at — the pre-commit epoch, since the host adopts in
|
|
62
|
+
* `onAccepted` and nowhere else. Replay re-seals the bodies, correct only under this epoch's
|
|
63
|
+
* secret.
|
|
64
|
+
*/
|
|
65
|
+
epoch: number;
|
|
66
|
+
/**
|
|
67
|
+
* The sequenceID the hub accepted this commit as. Present: it LANDED and this peer knows it
|
|
68
|
+
* locally — replay adopts, no network. Absent: outcome UNKNOWN, replay must ask the store by
|
|
69
|
+
* republishing.
|
|
70
|
+
*/
|
|
71
|
+
acceptedAs?: string;
|
|
72
|
+
commit: Uint8Array;
|
|
73
|
+
bodies: Array<string>;
|
|
74
|
+
kind: CommitKind;
|
|
75
|
+
journal: Uint8Array;
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* Durable single-slot journal, host-provided (the host already persists handle state; the peer
|
|
79
|
+
* has no storage). Holds at most one pending commit — the peer's commit mutex written down: one
|
|
80
|
+
* commit in flight at a time, per group, per device.
|
|
81
|
+
*
|
|
82
|
+
* Written TWICE for a commit that lands: `put` before the publish, `markAccepted` once the hub
|
|
83
|
+
* answers. `markAccepted` MUST run BEFORE `onAccepted`, while the group is still at the framed
|
|
84
|
+
* epoch — moving it later breaks the design. After `onAccepted` the handle has advanced, so a
|
|
85
|
+
* crash before the slot clears leaves a journalled commit at an epoch behind its group, which is
|
|
86
|
+
* byte-for-byte what a host that adopted out of band and never landed its publish leaves.
|
|
87
|
+
* Recorded first, the two are told apart: the legal crash carries the acceptance, the misbehaving
|
|
88
|
+
* host carries none.
|
|
89
|
+
*
|
|
90
|
+
* `markAccepted` and `clear` both take the `publishID` so they only touch the entry they were
|
|
91
|
+
* given: a call for an entry the slot no longer holds is a no-op, never a write over someone
|
|
92
|
+
* else's pending commit.
|
|
93
|
+
*/
|
|
94
|
+
export type CommitJournal = {
|
|
95
|
+
put(entry: JournalEntry): Promise<void>;
|
|
96
|
+
/**
|
|
97
|
+
* Record that the hub accepted this commit as `sequenceID`. Must be durable before it
|
|
98
|
+
* resolves, and must land before the host adopts.
|
|
99
|
+
*/
|
|
100
|
+
markAccepted(publishID: string, sequenceID: string): Promise<void>;
|
|
101
|
+
get(): Promise<JournalEntry | null>;
|
|
102
|
+
clear(publishID: string): Promise<void>;
|
|
103
|
+
};
|
|
104
|
+
/**
|
|
105
|
+
* A commit that was journalled, never landed, and cannot be re-issued by the peer — the process
|
|
106
|
+
* that held its `build()` closure is gone.
|
|
107
|
+
*
|
|
108
|
+
* `ledger` carries the surviving signed tokens: the host re-issues them with an ordinary
|
|
109
|
+
* `commit()`, nothing lost. `invite` / `remove` carries none — that commit cannot be
|
|
110
|
+
* reconstructed, so the host must re-issue or tell the user. For a remove the notice is
|
|
111
|
+
* security-relevant: an admin who believes a member was evicted when they were not has no other
|
|
112
|
+
* signal.
|
|
113
|
+
*/
|
|
114
|
+
export type LostCommit = {
|
|
115
|
+
kind: 'ledger';
|
|
116
|
+
tokens: Array<string>;
|
|
117
|
+
} | {
|
|
118
|
+
kind: 'invite' | 'remove';
|
|
119
|
+
};
|
|
120
|
+
/**
|
|
121
|
+
* What a lane operation found that the host must act on. Always a RETURN VALUE, never a callback:
|
|
122
|
+
* found inside the peer's commit mutex, and the host's response to both is `commit()`, which takes
|
|
123
|
+
* that same mutex.
|
|
124
|
+
*
|
|
125
|
+
* `lost`: a commit journalled and never landed, whose closure died with its process. `reenact`:
|
|
126
|
+
* after a heal rejoined this peer, the signed entry tokens it held that the group's authenticated
|
|
127
|
+
* ledger does NOT contain — the work survived, the commit that carried it did not. Both are the
|
|
128
|
+
* host's to re-issue; the peer never re-issues either itself.
|
|
129
|
+
*
|
|
130
|
+
* An entry the group's ledger already holds is absent from `reenact`, whatever failure brought
|
|
131
|
+
* the peer here. Re-enacting it would append a second copy, and the fold is last-write-wins by
|
|
132
|
+
* position: it would win and silently revert whatever a later admin wrote over it.
|
|
133
|
+
*/
|
|
134
|
+
export type LaneResult = {
|
|
135
|
+
lost?: LostCommit;
|
|
136
|
+
reenact?: Array<string>;
|
|
137
|
+
};
|
|
138
|
+
/**
|
|
139
|
+
* `commit()` ran out of time rebasing: it kept losing the compare-and-set past its deadline.
|
|
140
|
+
* Losing one is not an error — this is losing them longer than the caller would wait.
|
|
141
|
+
*/
|
|
142
|
+
export declare class CommitDeadlineError extends Error {
|
|
143
|
+
name: string;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* A journalled commit whose outcome was never recorded is being replayed at an epoch other than
|
|
147
|
+
* its framed one — so the group advanced without the peer learning this commit landed, which can
|
|
148
|
+
* only mean the host adopted somewhere other than {@link PendingCommit.onAccepted}.
|
|
149
|
+
*
|
|
150
|
+
* Replay re-seals the bodies and can only seal under the host's current epoch. Sealing under the
|
|
151
|
+
* wrong one publishes a blob no member can open: the commit applies, every receiver fails to
|
|
152
|
+
* resolve its entries, and the lane wedges for the whole group on a frame nobody can pass. So the
|
|
153
|
+
* peer refuses locally and keeps the slot — a local error the host can fix beats a group-wide
|
|
154
|
+
* wedge it cannot.
|
|
155
|
+
*/
|
|
156
|
+
export declare class JournalEpochError extends Error {
|
|
157
|
+
name: string;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* `commit()` refuses rather than build on a state it cannot trust. Two causes, both meaning the
|
|
161
|
+
* same to the host: nothing was published, the epoch did not advance, re-issue once the peer is
|
|
162
|
+
* whole.
|
|
163
|
+
*
|
|
164
|
+
* 1. A strand the pull found — a frame proving this peer is not reconciled (its own un-merged
|
|
165
|
+
* commit, or a commit framed ahead of it); it cannot race a head it has not caught up to. The
|
|
166
|
+
* repair is a heal, already scheduled: it runs as its own lane operation the moment `commit()`
|
|
167
|
+
* releases the lane. Thrown rather than waited on because the heal needs the mutex `commit()`
|
|
168
|
+
* holds — waiting for it would wait on a queue including itself.
|
|
169
|
+
* 2. An incomplete ledger — rejoined by external commit, bootstrap not finished, so the handle
|
|
170
|
+
* holds an empty ledger against a live head (a roster reset: every admin promoted since
|
|
171
|
+
* genesis invisible, a commit built now judged against admins it cannot see). NO heal is
|
|
172
|
+
* scheduled: the peer holds its leaf, and the ledger gather that just failed for want of a
|
|
173
|
+
* responder IS the repair — it re-runs at the head of the next lane operation.
|
|
174
|
+
*
|
|
175
|
+
* The host must NOT retry in a tight loop: for the strand that retakes the mutex before the heal
|
|
176
|
+
* runs, for the incomplete ledger it re-gathers before a responder returns. A throw leaves any
|
|
177
|
+
* earlier `lost` / `reenact` work undrained — call {@link "peer".GroupPeer.replay} to collect it.
|
|
178
|
+
*/
|
|
179
|
+
export declare class RecoveryRequiredError extends Error {
|
|
180
|
+
name: string;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* The compare-and-set was lost: someone else's commit is at the head. The error crosses a
|
|
184
|
+
* transport and is rebuilt from a wire code, so a peer talking to a real hub holds a
|
|
185
|
+
* reconstructed instance — match on the NAME too, or a remote loss reads as an unknown publish
|
|
186
|
+
* failure and the peer keeps a journal slot it should have cleared.
|
|
187
|
+
*/
|
|
188
|
+
export declare function isHeadMismatch(error: unknown): boolean;
|
package/lib/commit.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { HeadMismatchError } from '@kumiai/hub-protocol';
|
|
2
|
+
/**
|
|
3
|
+
* `commit()` ran out of time rebasing: it kept losing the compare-and-set past its deadline.
|
|
4
|
+
* Losing one is not an error — this is losing them longer than the caller would wait.
|
|
5
|
+
*/ export class CommitDeadlineError extends Error {
|
|
6
|
+
name = 'CommitDeadlineError';
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* A journalled commit whose outcome was never recorded is being replayed at an epoch other than
|
|
10
|
+
* its framed one — so the group advanced without the peer learning this commit landed, which can
|
|
11
|
+
* only mean the host adopted somewhere other than {@link PendingCommit.onAccepted}.
|
|
12
|
+
*
|
|
13
|
+
* Replay re-seals the bodies and can only seal under the host's current epoch. Sealing under the
|
|
14
|
+
* wrong one publishes a blob no member can open: the commit applies, every receiver fails to
|
|
15
|
+
* resolve its entries, and the lane wedges for the whole group on a frame nobody can pass. So the
|
|
16
|
+
* peer refuses locally and keeps the slot — a local error the host can fix beats a group-wide
|
|
17
|
+
* wedge it cannot.
|
|
18
|
+
*/ export class JournalEpochError extends Error {
|
|
19
|
+
name = 'JournalEpochError';
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* `commit()` refuses rather than build on a state it cannot trust. Two causes, both meaning the
|
|
23
|
+
* same to the host: nothing was published, the epoch did not advance, re-issue once the peer is
|
|
24
|
+
* whole.
|
|
25
|
+
*
|
|
26
|
+
* 1. A strand the pull found — a frame proving this peer is not reconciled (its own un-merged
|
|
27
|
+
* commit, or a commit framed ahead of it); it cannot race a head it has not caught up to. The
|
|
28
|
+
* repair is a heal, already scheduled: it runs as its own lane operation the moment `commit()`
|
|
29
|
+
* releases the lane. Thrown rather than waited on because the heal needs the mutex `commit()`
|
|
30
|
+
* holds — waiting for it would wait on a queue including itself.
|
|
31
|
+
* 2. An incomplete ledger — rejoined by external commit, bootstrap not finished, so the handle
|
|
32
|
+
* holds an empty ledger against a live head (a roster reset: every admin promoted since
|
|
33
|
+
* genesis invisible, a commit built now judged against admins it cannot see). NO heal is
|
|
34
|
+
* scheduled: the peer holds its leaf, and the ledger gather that just failed for want of a
|
|
35
|
+
* responder IS the repair — it re-runs at the head of the next lane operation.
|
|
36
|
+
*
|
|
37
|
+
* The host must NOT retry in a tight loop: for the strand that retakes the mutex before the heal
|
|
38
|
+
* runs, for the incomplete ledger it re-gathers before a responder returns. A throw leaves any
|
|
39
|
+
* earlier `lost` / `reenact` work undrained — call {@link "peer".GroupPeer.replay} to collect it.
|
|
40
|
+
*/ export class RecoveryRequiredError extends Error {
|
|
41
|
+
name = 'RecoveryRequiredError';
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The compare-and-set was lost: someone else's commit is at the head. The error crosses a
|
|
45
|
+
* transport and is rebuilt from a wire code, so a peer talking to a real hub holds a
|
|
46
|
+
* reconstructed instance — match on the NAME too, or a remote loss reads as an unknown publish
|
|
47
|
+
* failure and the peer keeps a journal slot it should have cleared.
|
|
48
|
+
*/ export function isHeadMismatch(error) {
|
|
49
|
+
return error instanceof HeadMismatchError || error instanceof Error && error.name === 'HeadMismatchError';
|
|
50
|
+
}
|