@kumiai/rpc 0.10.2 → 0.10.3
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/anchor.d.ts +15 -14
- package/lib/anchor.js +2 -12
- package/lib/app-cursor.d.ts +5 -8
- package/lib/app-cursor.js +3 -6
- package/lib/app-lane.js +16 -21
- package/lib/app-outbox.d.ts +57 -0
- package/lib/app-outbox.js +97 -0
- package/lib/commit.d.ts +6 -0
- package/lib/crypto.d.ts +67 -16
- package/lib/errors.d.ts +15 -0
- package/lib/errors.js +20 -0
- package/lib/frame-size.d.ts +8 -0
- package/lib/frame-size.js +13 -0
- package/lib/handshake.d.ts +2 -0
- package/lib/handshake.js +7 -1
- package/lib/host-boundary.d.ts +109 -0
- package/lib/host-boundary.js +258 -0
- package/lib/hub-mux.d.ts +2 -4
- package/lib/hub-mux.js +33 -12
- package/lib/index.d.ts +8 -5
- package/lib/index.js +4 -2
- package/lib/log-delivery.d.ts +46 -0
- package/lib/log-delivery.js +177 -0
- package/lib/peer-epoch.d.ts +47 -0
- package/lib/peer-epoch.js +376 -0
- package/lib/peer-types.d.ts +343 -0
- package/lib/peer-types.js +26 -0
- package/lib/peer.d.ts +4 -305
- package/lib/peer.js +1024 -664
- package/lib/recovery-confirmation.d.ts +63 -0
- package/lib/recovery-confirmation.js +132 -0
- package/lib/recovery.d.ts +14 -0
- package/lib/recovery.js +72 -8
- package/package.json +11 -11
package/lib/anchor.d.ts
CHANGED
|
@@ -7,21 +7,22 @@ export type Anchor = {
|
|
|
7
7
|
secret: Uint8Array<ArrayBufferLike>;
|
|
8
8
|
epoch: number;
|
|
9
9
|
};
|
|
10
|
+
/** One durable anchor and the advance whose rotation is still unresolved. */
|
|
11
|
+
export type AnchorSlot = {
|
|
12
|
+
anchor: Anchor;
|
|
13
|
+
pending?: {
|
|
14
|
+
epochBefore: number;
|
|
15
|
+
epochAfter: number;
|
|
16
|
+
rosterBefore: Array<string>;
|
|
17
|
+
forced: boolean;
|
|
18
|
+
advance: string;
|
|
19
|
+
};
|
|
20
|
+
};
|
|
10
21
|
/**
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* of something in flight.
|
|
14
|
-
*
|
|
15
|
-
* It exists because the anchor is PERSISTED STATE and cannot be re-derived. The anchor sits at
|
|
16
|
-
* the last roster change, the live handle runs ahead of it, and MLS ratchets forward: a rebooted
|
|
17
|
-
* handle can never re-export an earlier epoch's secret. A peer that re-seeded from its live
|
|
18
|
-
* handle at construction would derive different topic IDs from every member that did not restart
|
|
19
|
-
* — neither would see the other's app traffic, with nothing anywhere to report it.
|
|
20
|
-
*
|
|
21
|
-
* `load` returning `null` means first boot and only first boot: the peer seeds the anchor from
|
|
22
|
-
* its handle, as a group with no roster change yet must, and saves it.
|
|
22
|
+
* Durable single slot, owned by one live peer. Each record spans one advance and resolves before another.
|
|
23
|
+
* Only the same advance retries an ambiguous record. Known-unlanded advances clear pending.
|
|
23
24
|
*/
|
|
24
25
|
export type AnchorStore = {
|
|
25
|
-
load(): Promise<
|
|
26
|
-
save(
|
|
26
|
+
load(): Promise<AnchorSlot | null>;
|
|
27
|
+
save(slot: AnchorSlot): Promise<void>;
|
|
27
28
|
};
|
package/lib/anchor.js
CHANGED
|
@@ -3,16 +3,6 @@
|
|
|
3
3
|
* halves feed the topic derivation, so they only ever move as a pair — a secret from one epoch
|
|
4
4
|
* with another epoch's number derives a topic no member is on.
|
|
5
5
|
*/ /**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* of something in flight.
|
|
9
|
-
*
|
|
10
|
-
* It exists because the anchor is PERSISTED STATE and cannot be re-derived. The anchor sits at
|
|
11
|
-
* the last roster change, the live handle runs ahead of it, and MLS ratchets forward: a rebooted
|
|
12
|
-
* handle can never re-export an earlier epoch's secret. A peer that re-seeded from its live
|
|
13
|
-
* handle at construction would derive different topic IDs from every member that did not restart
|
|
14
|
-
* — neither would see the other's app traffic, with nothing anywhere to report it.
|
|
15
|
-
*
|
|
16
|
-
* `load` returning `null` means first boot and only first boot: the peer seeds the anchor from
|
|
17
|
-
* its handle, as a group with no roster change yet must, and saves it.
|
|
6
|
+
* Durable single slot, owned by one live peer. Each record spans one advance and resolves before another.
|
|
7
|
+
* Only the same advance retries an ambiguous record. Known-unlanded advances clear pending.
|
|
18
8
|
*/ export { };
|
package/lib/app-cursor.d.ts
CHANGED
|
@@ -32,17 +32,14 @@ export type AppCursorStore = {
|
|
|
32
32
|
save(topicID: string, position: string): Promise<void>;
|
|
33
33
|
};
|
|
34
34
|
/**
|
|
35
|
-
* A gap below
|
|
36
|
-
* the position this peer had read to, so whatever sat between them aged out unread.
|
|
35
|
+
* A gap below retention: the hub confirms that a frame after this peer's read position aged out.
|
|
37
36
|
*
|
|
38
37
|
* Reported rather than silent, which is the whole point — a peer back from a long absence is
|
|
39
38
|
* otherwise handed a partial history it cannot tell from a complete one, and neither it nor its
|
|
40
39
|
* host can say so. It is a NOTICE and not an error: the frames that survived are delivered anyway.
|
|
41
40
|
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
* fires whenever the floor has passed the cursor. It over-reports; it never stays quiet about a
|
|
45
|
-
* real gap.
|
|
41
|
+
* The hub reports the gap from its removal watermark, so removal of the cursor's own frame alone
|
|
42
|
+
* does not count. Notices are deduplicated per topic and cursor for this process.
|
|
46
43
|
*
|
|
47
44
|
* NO WALL-CLOCK, deliberately: rpc has no clock worth handing a host, and "messages since <date>"
|
|
48
45
|
* is the host's own sentence to write from its own HLC. What is carried is what rpc knows.
|
|
@@ -58,6 +55,6 @@ export type AppWindowPruned = {
|
|
|
58
55
|
protocol: string;
|
|
59
56
|
/** The last position this peer read to: the older edge of the gap, exclusive. */
|
|
60
57
|
cursor: string;
|
|
61
|
-
/** The hub's oldest retained frame
|
|
62
|
-
oldest: string;
|
|
58
|
+
/** The hub's oldest retained frame after the gap, or null when the window is empty. */
|
|
59
|
+
oldest: string | null;
|
|
63
60
|
};
|
package/lib/app-cursor.js
CHANGED
|
@@ -27,17 +27,14 @@
|
|
|
27
27
|
* Positions are log positions (see `cursor.ts`), opaque to the host: store the string, compare
|
|
28
28
|
* nothing.
|
|
29
29
|
*/ /**
|
|
30
|
-
* A gap below
|
|
31
|
-
* the position this peer had read to, so whatever sat between them aged out unread.
|
|
30
|
+
* A gap below retention: the hub confirms that a frame after this peer's read position aged out.
|
|
32
31
|
*
|
|
33
32
|
* Reported rather than silent, which is the whole point — a peer back from a long absence is
|
|
34
33
|
* otherwise handed a partial history it cannot tell from a complete one, and neither it nor its
|
|
35
34
|
* host can say so. It is a NOTICE and not an error: the frames that survived are delivered anyway.
|
|
36
35
|
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
* fires whenever the floor has passed the cursor. It over-reports; it never stays quiet about a
|
|
40
|
-
* real gap.
|
|
36
|
+
* The hub reports the gap from its removal watermark, so removal of the cursor's own frame alone
|
|
37
|
+
* does not count. Notices are deduplicated per topic and cursor for this process.
|
|
41
38
|
*
|
|
42
39
|
* NO WALL-CLOCK, deliberately: rpc has no clock worth handing a host, and "messages since <date>"
|
|
43
40
|
* is the host's own sentence to write from its own HLC. What is carried is what rpc knows.
|
package/lib/app-lane.js
CHANGED
|
@@ -59,6 +59,7 @@ function frameID(topicID, bytes) {
|
|
|
59
59
|
* (below) and dispensed epoch by epoch as the commit walk passes through it.
|
|
60
60
|
*/ let segment = new Map();
|
|
61
61
|
const pendingRecords = [];
|
|
62
|
+
const reportedPruned = new Map();
|
|
62
63
|
const blockingFrames = new Map();
|
|
63
64
|
let disposed = false;
|
|
64
65
|
const reportedUnknown = new Map();
|
|
@@ -164,19 +165,17 @@ function frameID(topicID, bytes) {
|
|
|
164
165
|
tail = op.then(()=>{}, ()=>{});
|
|
165
166
|
return op;
|
|
166
167
|
};
|
|
167
|
-
/**
|
|
168
|
-
|
|
169
|
-
* between the two aged out unread, and a returning member is holding a partial history it has no
|
|
170
|
-
* other way to know is partial. A gap below retention is REPORTED, never silent.
|
|
171
|
-
*
|
|
172
|
-
* `oldest > cursor` is the whole test and the only one available: nothing anywhere records which
|
|
173
|
-
* frames used to sit between them, so this over-reports (a cursor frame aging out with nothing
|
|
174
|
-
* behind it reads the same) rather than ever under-reporting. With no cursor there is no gap to
|
|
175
|
-
* speak of.
|
|
176
|
-
*/ const reportPrunedWindow = async (name, cursor, oldest)=>{
|
|
168
|
+
/** Tell the host once when a fetch with a cursor confirms unread frames were removed. */ const reportPrunedWindow = async (report)=>{
|
|
169
|
+
const { name, topicID, cursor, oldest } = report;
|
|
177
170
|
const group = groupID();
|
|
178
171
|
if (onAppWindowPruned == null || group == null) return;
|
|
179
|
-
|
|
172
|
+
let positions = reportedPruned.get(topicID);
|
|
173
|
+
if (positions == null) {
|
|
174
|
+
positions = new Set();
|
|
175
|
+
reportedPruned.set(topicID, positions);
|
|
176
|
+
}
|
|
177
|
+
if (positions.has(cursor)) return;
|
|
178
|
+
positions.add(cursor);
|
|
180
179
|
try {
|
|
181
180
|
await onAppWindowPruned({
|
|
182
181
|
groupID: group,
|
|
@@ -257,10 +256,6 @@ function frameID(topicID, bytes) {
|
|
|
257
256
|
if (push.topicID === topicID) takeAppFrame(frames, push.frame, crypto.pending != null);
|
|
258
257
|
}
|
|
259
258
|
staged.delete(name);
|
|
260
|
-
// The gap question is asked on the segment's FIRST pull only: it compares where this peer
|
|
261
|
-
// had read to against where the hub's retention now begins, and every later pull starts
|
|
262
|
-
// from a position this peer reached itself.
|
|
263
|
-
let reported = cursor.fetched != null;
|
|
264
259
|
let after = cursor.fetched ?? cursor.position;
|
|
265
260
|
while(true){
|
|
266
261
|
const result = await mux.fetchTopic({
|
|
@@ -271,12 +266,12 @@ function frameID(topicID, bytes) {
|
|
|
271
266
|
limit: APP_FETCH_LIMIT
|
|
272
267
|
});
|
|
273
268
|
assertForwardPage(after, result.messages);
|
|
274
|
-
if (
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
}
|
|
269
|
+
if (after != null && result.gap) await reportPrunedWindow({
|
|
270
|
+
name,
|
|
271
|
+
topicID,
|
|
272
|
+
cursor: after,
|
|
273
|
+
oldest: result.oldest
|
|
274
|
+
});
|
|
280
275
|
for (const message of result.messages){
|
|
281
276
|
const position = asLogPosition(message.sequenceID);
|
|
282
277
|
const restored = pendingRecords.find((record)=>record.frame.protocol === name && record.frame.topicID === topicID && record.frame.position === position);
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { SendAdmission } from './crypto.js';
|
|
2
|
+
export declare const MAX_APP_ENTRY_BYTES = 524288;
|
|
3
|
+
/** Encoded app plaintext and the latest prepared publication attempt. */
|
|
4
|
+
export type AppOutboxEntry = {
|
|
5
|
+
seq: number;
|
|
6
|
+
protocol: string;
|
|
7
|
+
prc: string;
|
|
8
|
+
data: Uint8Array;
|
|
9
|
+
lastAttempt: {
|
|
10
|
+
epoch: number;
|
|
11
|
+
floor: string | null;
|
|
12
|
+
attempts: number;
|
|
13
|
+
} | null;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* The last processed commit-log position at `epoch`. `stranded` records that the peer must rejoin:
|
|
17
|
+
* the strand that raised it is otherwise memory-only, and a restart at the same epoch could not
|
|
18
|
+
* rediscover a fork it already stepped past. `position` is null when no position is durable yet.
|
|
19
|
+
*/
|
|
20
|
+
export type CommitCursor = {
|
|
21
|
+
position: string | null;
|
|
22
|
+
epoch: number;
|
|
23
|
+
stranded?: true;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Owned by one live peer per group. Writes are durable before resolution.
|
|
27
|
+
* Hosts encrypt plaintext at rest and clear it on leave/deletion, choosing physical erasure policy.
|
|
28
|
+
*/
|
|
29
|
+
export type AppOutbox = {
|
|
30
|
+
getCommitCursor(): Promise<CommitCursor | null>;
|
|
31
|
+
/** Durable after the paired MLS state. Null discards an unusable cursor. */
|
|
32
|
+
putCommitCursor(cursor: CommitCursor | null): Promise<void>;
|
|
33
|
+
/** Atomic insert or replace by seq. A rejection leaves storage unchanged. */
|
|
34
|
+
put(entry: AppOutboxEntry): Promise<void>;
|
|
35
|
+
/** Ascending sequence order. */
|
|
36
|
+
list(): Promise<Array<AppOutboxEntry>>;
|
|
37
|
+
remove(seq: number): Promise<void>;
|
|
38
|
+
clear(): Promise<void>;
|
|
39
|
+
};
|
|
40
|
+
export type AppOutboxAcceptanceParams = {
|
|
41
|
+
outbox: AppOutbox;
|
|
42
|
+
limit: number;
|
|
43
|
+
admission: () => SendAdmission;
|
|
44
|
+
};
|
|
45
|
+
export type AppOutboxAcceptance = {
|
|
46
|
+
ready: () => Promise<void>;
|
|
47
|
+
accept: (input: Pick<AppOutboxEntry, 'protocol' | 'prc' | 'data'>) => Promise<void>;
|
|
48
|
+
entries: () => Array<AppOutboxEntry>;
|
|
49
|
+
lowestUnresolvedSeq: () => number | null;
|
|
50
|
+
replace: (entry: AppOutboxEntry) => void;
|
|
51
|
+
remove: (seq: number) => void;
|
|
52
|
+
clear: () => void;
|
|
53
|
+
stop: () => void;
|
|
54
|
+
close: () => void;
|
|
55
|
+
};
|
|
56
|
+
/** Accepted entries below the unresolved fence are available to the delivery worker. */
|
|
57
|
+
export declare function createAppOutboxAcceptance(params: AppOutboxAcceptanceParams): AppOutboxAcceptance;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { AppEntryTooLargeError, AppOutboxFullError, PeerDisposedError, PeerRemovedError, SendNotAdmissibleError } from './errors.js';
|
|
2
|
+
export const MAX_APP_ENTRY_BYTES = 524_288;
|
|
3
|
+
/** Accepted entries below the unresolved fence are available to the delivery worker. */ export function createAppOutboxAcceptance(params) {
|
|
4
|
+
const accepted = new Map();
|
|
5
|
+
const reservations = new Set();
|
|
6
|
+
let nextSeq = 0;
|
|
7
|
+
let listed = false;
|
|
8
|
+
let waiting = 0;
|
|
9
|
+
let closed = false;
|
|
10
|
+
let stopped = false;
|
|
11
|
+
let retry;
|
|
12
|
+
let resolveReady = ()=>{};
|
|
13
|
+
const ready = new Promise((resolve)=>{
|
|
14
|
+
resolveReady = resolve;
|
|
15
|
+
});
|
|
16
|
+
const load = async ()=>{
|
|
17
|
+
try {
|
|
18
|
+
const entries = await params.outbox.list();
|
|
19
|
+
if (closed) return;
|
|
20
|
+
for (const entry of entries){
|
|
21
|
+
accepted.set(entry.seq, entry);
|
|
22
|
+
nextSeq = Math.max(nextSeq, entry.seq + 1);
|
|
23
|
+
}
|
|
24
|
+
listed = true;
|
|
25
|
+
resolveReady();
|
|
26
|
+
} catch {
|
|
27
|
+
// No allocation happens until the durable inventory is known.
|
|
28
|
+
if (!closed) retry = setTimeout(()=>{
|
|
29
|
+
void load();
|
|
30
|
+
}, 1000);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
void load();
|
|
34
|
+
const accept = async (input)=>{
|
|
35
|
+
// Later calls must join the handoff until earlier waiters have resumed.
|
|
36
|
+
if (!listed || waiting > 0) {
|
|
37
|
+
waiting++;
|
|
38
|
+
try {
|
|
39
|
+
await ready;
|
|
40
|
+
} finally{
|
|
41
|
+
waiting--;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (closed) throw new PeerDisposedError('App outbox acceptance is closed');
|
|
45
|
+
if (stopped) throw new PeerRemovedError('App outbox sender was removed');
|
|
46
|
+
const admission = params.admission();
|
|
47
|
+
if (!admission.admissible) throw new SendNotAdmissibleError(admission.reason);
|
|
48
|
+
if (input.data.byteLength > MAX_APP_ENTRY_BYTES) {
|
|
49
|
+
throw new AppEntryTooLargeError('App entry exceeds the plaintext bound');
|
|
50
|
+
}
|
|
51
|
+
if (accepted.size + reservations.size >= params.limit) {
|
|
52
|
+
throw new AppOutboxFullError('App outbox is full');
|
|
53
|
+
}
|
|
54
|
+
const seq = nextSeq++;
|
|
55
|
+
const entry = {
|
|
56
|
+
...input,
|
|
57
|
+
data: input.data.slice(),
|
|
58
|
+
seq,
|
|
59
|
+
lastAttempt: null
|
|
60
|
+
};
|
|
61
|
+
reservations.add(seq);
|
|
62
|
+
try {
|
|
63
|
+
await params.outbox.put(entry);
|
|
64
|
+
accepted.set(seq, entry);
|
|
65
|
+
} finally{
|
|
66
|
+
reservations.delete(seq);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
return {
|
|
70
|
+
ready: ()=>ready,
|
|
71
|
+
accept,
|
|
72
|
+
entries: ()=>[
|
|
73
|
+
...accepted.values()
|
|
74
|
+
].sort((a, b)=>a.seq - b.seq),
|
|
75
|
+
lowestUnresolvedSeq: ()=>{
|
|
76
|
+
const first = reservations.values().next();
|
|
77
|
+
return first.done ? null : first.value;
|
|
78
|
+
},
|
|
79
|
+
replace: (entry)=>{
|
|
80
|
+
accepted.set(entry.seq, entry);
|
|
81
|
+
},
|
|
82
|
+
remove: (seq)=>{
|
|
83
|
+
accepted.delete(seq);
|
|
84
|
+
},
|
|
85
|
+
clear: ()=>{
|
|
86
|
+
accepted.clear();
|
|
87
|
+
},
|
|
88
|
+
stop: ()=>{
|
|
89
|
+
stopped = true;
|
|
90
|
+
},
|
|
91
|
+
close: ()=>{
|
|
92
|
+
closed = true;
|
|
93
|
+
clearTimeout(retry);
|
|
94
|
+
resolveReady();
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
}
|
package/lib/commit.d.ts
CHANGED
|
@@ -21,12 +21,16 @@ export type PendingCommit = {
|
|
|
21
21
|
* Opaque host blob holding everything needed to adopt this commit after a restart: the
|
|
22
22
|
* serialized post-commit handle and any Welcome to deliver. Written to the journal BEFORE the
|
|
23
23
|
* peer publishes, handed straight back to `adoptJournalled` on replay. The peer never inspects it.
|
|
24
|
+
* Since the Welcome is opaque to the peer, the host must check its final hub frame with
|
|
25
|
+
* `assertFrameFits` inside `build()`, before returning this PendingCommit.
|
|
24
26
|
*/
|
|
25
27
|
journal: Uint8Array;
|
|
26
28
|
/**
|
|
27
29
|
* Runs only if the hub accepts. Adopt the post-commit handle here and send any Welcome. The
|
|
28
30
|
* ONLY place the host may adopt: adopting earlier rotates past the epoch the bodies must be
|
|
29
31
|
* sealed under, adopting later leaves the handle behind the group.
|
|
32
|
+
* Any frame this callback sends through a hub must already have passed `assertFrameFits` inside
|
|
33
|
+
* `build()`. This callback runs after the Add commit is journalled and accepted.
|
|
30
34
|
*
|
|
31
35
|
* ENFORCED: the peer records acceptance in the journal BEFORE this runs, so a restart can tell
|
|
32
36
|
* a commit that landed from one whose fate is unknown. An entry with no recorded acceptance,
|
|
@@ -63,6 +67,8 @@ export type JournalEntry = {
|
|
|
63
67
|
* secret.
|
|
64
68
|
*/
|
|
65
69
|
epoch: number;
|
|
70
|
+
/** Holds log submissions until adoption or a known publication loss clears the slot. */
|
|
71
|
+
holdsLogSends?: true;
|
|
66
72
|
/**
|
|
67
73
|
* The sequenceID the hub accepted this commit as. Present: it LANDED and this peer knows it
|
|
68
74
|
* locally — replay adopts, no network. Absent: outcome UNKNOWN, replay must ask the store by
|
package/lib/crypto.d.ts
CHANGED
|
@@ -149,10 +149,31 @@ export type SealEntriesResult = {
|
|
|
149
149
|
sealed: Uint8Array;
|
|
150
150
|
epoch: number;
|
|
151
151
|
};
|
|
152
|
+
export type RecoveryRefusalReason = 'binding' | 'lapse' | 'floor' | 'policy' | 'invalid';
|
|
153
|
+
export type RecoveryVerdict = {
|
|
154
|
+
groupID: string;
|
|
155
|
+
requestID: string;
|
|
156
|
+
position: string;
|
|
157
|
+
commitDigest: string;
|
|
158
|
+
} & ({
|
|
159
|
+
verdict: 'confirmed';
|
|
160
|
+
epoch: number;
|
|
161
|
+
tag: string;
|
|
162
|
+
} | {
|
|
163
|
+
verdict: 'superseded';
|
|
164
|
+
} | {
|
|
165
|
+
verdict: 'refused';
|
|
166
|
+
reason: RecoveryRefusalReason;
|
|
167
|
+
});
|
|
168
|
+
export type OpenedRecoveryVerdict = {
|
|
169
|
+
signer: string;
|
|
170
|
+
verdict: RecoveryVerdict;
|
|
171
|
+
};
|
|
152
172
|
export type ProcessCommitResult = {
|
|
153
173
|
advanced: boolean;
|
|
154
174
|
epochBefore: number;
|
|
155
175
|
epochAfter: number;
|
|
176
|
+
refusal?: RecoveryRefusalReason;
|
|
156
177
|
};
|
|
157
178
|
/** A frame refused against the locked handle epoch, before spending a decrypt key. */
|
|
158
179
|
export declare class FrameEpochError extends Error {
|
|
@@ -293,27 +314,48 @@ export type CommitContext = {
|
|
|
293
314
|
resolveLedgerEntries?: (ids: Array<string>) => Promise<Array<string>>;
|
|
294
315
|
};
|
|
295
316
|
/**
|
|
296
|
-
*
|
|
297
|
-
*
|
|
298
|
-
* commit, since a peer that adopted first sits on its own branch the moment it loses the
|
|
299
|
-
* compare-and-set.
|
|
300
|
-
*
|
|
301
|
-
* Carries no entries — a GroupInfo has nowhere to put an entry envelope — so a heal is TWO
|
|
302
|
-
* commits: this one rejoins, and the entries the peer still owes ride an ordinary `commit()`
|
|
303
|
-
* behind it.
|
|
317
|
+
* A speculative external rejoin. Publication alone never authorises adoption.
|
|
318
|
+
* The caller confirms the target epoch before accepting it.
|
|
304
319
|
*/
|
|
305
320
|
export type PendingRecovery = {
|
|
321
|
+
epoch: number;
|
|
322
|
+
/** Mark an authoritative binding/lapse/floor refusal. Bounded memory survives ratchets, not restart. */
|
|
323
|
+
markBindingUnusable(): void;
|
|
324
|
+
/** Bind judgement to the published tuple and derive from the speculative epoch. */
|
|
325
|
+
confirmationKey(position: string, commitDigest: string): Promise<Uint8Array>;
|
|
326
|
+
/** Call after confirmationKey resolves. Failed bindings or signer gates are advisory. */
|
|
327
|
+
judgeVerdict(opened: OpenedRecoveryVerdict): 'authoritative' | 'advisory';
|
|
306
328
|
/** The external-commit bytes, framed at the epoch the sealed GroupInfo described. */
|
|
307
329
|
commit: Uint8Array;
|
|
308
330
|
/**
|
|
309
|
-
* Adopt the
|
|
310
|
-
*
|
|
311
|
-
*
|
|
312
|
-
* The rejoined handle's ledger is EMPTY — a GroupInfo carries a head and no entries — so until
|
|
313
|
-
* bootstrapped the handle is internally inconsistent: a roster reset, not a neutral one.
|
|
331
|
+
* Adopt the confirmed handle once. Repeated calls share the same acceptance.
|
|
332
|
+
* Standard groups still need ledger bootstrap after adoption.
|
|
314
333
|
*/
|
|
315
334
|
onAccepted: () => Promise<void>;
|
|
316
335
|
};
|
|
336
|
+
/** {@link GroupMLS.applyRecovery} result: a recovery to adopt, a renewal to run first, or nothing. */
|
|
337
|
+
export type AppliedRecovery = PendingRecovery | {
|
|
338
|
+
renewalRequired: true;
|
|
339
|
+
} | null;
|
|
340
|
+
export type SendAdmission = {
|
|
341
|
+
epoch: number;
|
|
342
|
+
admissible: true;
|
|
343
|
+
} | {
|
|
344
|
+
epoch: number;
|
|
345
|
+
admissible: false;
|
|
346
|
+
reason: 'lapsed';
|
|
347
|
+
};
|
|
348
|
+
/** The signed fields of a recovery ask that verified. */
|
|
349
|
+
export type VerifiedRecoveryRequest = {
|
|
350
|
+
groupID: string;
|
|
351
|
+
requestID: string;
|
|
352
|
+
requesterDID: string;
|
|
353
|
+
};
|
|
354
|
+
/** Confirmation key for an applied external commit, and the epoch it was derived at. */
|
|
355
|
+
export type RecoveryConfirmationKey = {
|
|
356
|
+
epoch: number;
|
|
357
|
+
key: Uint8Array;
|
|
358
|
+
};
|
|
317
359
|
/**
|
|
318
360
|
* Consumer-supplied MLS lifecycle port. Sibling to {@link GroupCrypto}: this drives the
|
|
319
361
|
* handshake lane — applying Commits to advance the epoch, re-syncing a stranded peer. group-rpc
|
|
@@ -326,6 +368,13 @@ export type PendingRecovery = {
|
|
|
326
368
|
* until the suite was made to cover the shape rather than a sample of it.
|
|
327
369
|
*/
|
|
328
370
|
export type GroupMLS = {
|
|
371
|
+
/** Verify the signed ask without requiring its issuer to remain in the current tree. */
|
|
372
|
+
verifyRecoveryRequest(request: Uint8Array): Promise<VerifiedRecoveryRequest | null>;
|
|
373
|
+
confirmationKey(position: string, commitDigest: string): Promise<RecoveryConfirmationKey>;
|
|
374
|
+
sealRecoveryVerdict(request: Uint8Array, verdict: RecoveryVerdict): Promise<Uint8Array>;
|
|
375
|
+
openRecoveryVerdict(sealed: Uint8Array, requestID: string): Promise<OpenedRecoveryVerdict | null>;
|
|
376
|
+
/** Published with the epoch, without taking the handle lock. */
|
|
377
|
+
sendAdmission(): SendAdmission;
|
|
329
378
|
/** Read the current handle epoch under the host's handle lock. */
|
|
330
379
|
readEpoch(): Promise<number>;
|
|
331
380
|
/**
|
|
@@ -385,6 +434,8 @@ export type GroupMLS = {
|
|
|
385
434
|
* {@link isMissingLedgerEntries}.
|
|
386
435
|
*/
|
|
387
436
|
processCommit(commit: Uint8Array, context: CommitContext): Promise<ProcessCommitResult>;
|
|
437
|
+
/** Check lifecycle binding availability once before a recovery attempt requests GroupInfo. */
|
|
438
|
+
prepareRecovery(): Promise<'ready' | 'renewal-required'>;
|
|
388
439
|
/**
|
|
389
440
|
* Mint the rendezvous request this peer publishes to ask the group for its state: an HPKE
|
|
390
441
|
* keypair for this one request, public half in a token signed by this member's identity key.
|
|
@@ -399,7 +450,7 @@ export type GroupMLS = {
|
|
|
399
450
|
* off the mint time is the natural bound; it lives here rather than as a new lane release
|
|
400
451
|
* method that would obligate every {@link GroupMLS}.
|
|
401
452
|
*/
|
|
402
|
-
createRecoveryRequest(requestID: string): Promise<Uint8Array>;
|
|
453
|
+
createRecoveryRequest(requestID: string, deadlineMs?: number): Promise<Uint8Array>;
|
|
403
454
|
/**
|
|
404
455
|
* Answer another member's request: verify the token, check the requester still holds a leaf in
|
|
405
456
|
* THIS member's current ratchet tree, seal current GroupInfo to the ephemeral key inside the
|
|
@@ -416,7 +467,7 @@ export type GroupMLS = {
|
|
|
416
467
|
* Open a sealed reply with the key minted for `requestID` and BUILD the external commit that
|
|
417
468
|
* rejoins this peer. Non-mutating: the rejoined handle is adopted only in
|
|
418
469
|
* {@link PendingRecovery.onAccepted}, because the commit still has to win a compare-and-set at
|
|
419
|
-
* the head.
|
|
470
|
+
* the head AND obtain a counted member confirmation.
|
|
420
471
|
*
|
|
421
472
|
* `null` for bytes this peer cannot open OR cannot trust. HPKE base mode needs only the
|
|
422
473
|
* requester's public ephemeral key, which rides the public request in the clear — so a
|
|
@@ -426,7 +477,7 @@ export type GroupMLS = {
|
|
|
426
477
|
* reply the AEAD refuses, or one that fails either check, is `null`; a throw is tolerated and
|
|
427
478
|
* read the same way.
|
|
428
479
|
*/
|
|
429
|
-
applyRecovery(sealed: Uint8Array, requestID: string): Promise<
|
|
480
|
+
applyRecovery(sealed: Uint8Array, requestID: string): Promise<AppliedRecovery>;
|
|
430
481
|
/**
|
|
431
482
|
* Whether the ledger this handle holds is the whole ledger its OWN GroupContext attests to: the
|
|
432
483
|
* head folded from the entries it holds, against the authenticated head it carries. Purely
|
package/lib/errors.d.ts
CHANGED
|
@@ -6,3 +6,18 @@
|
|
|
6
6
|
export declare class PeerDisposedError extends Error {
|
|
7
7
|
name: string;
|
|
8
8
|
}
|
|
9
|
+
export declare class PeerRemovedError extends Error {
|
|
10
|
+
name: string;
|
|
11
|
+
}
|
|
12
|
+
export declare class AppOutboxFullError extends Error {
|
|
13
|
+
name: string;
|
|
14
|
+
}
|
|
15
|
+
export declare class AppEntryTooLargeError extends Error {
|
|
16
|
+
name: string;
|
|
17
|
+
}
|
|
18
|
+
export declare class SendNotAdmissibleError extends Error {
|
|
19
|
+
#private;
|
|
20
|
+
name: string;
|
|
21
|
+
constructor(reason: 'lapsed');
|
|
22
|
+
get reason(): 'lapsed';
|
|
23
|
+
}
|
package/lib/errors.js
CHANGED
|
@@ -5,3 +5,23 @@
|
|
|
5
5
|
*/ export class PeerDisposedError extends Error {
|
|
6
6
|
name = 'PeerDisposedError';
|
|
7
7
|
}
|
|
8
|
+
export class PeerRemovedError extends Error {
|
|
9
|
+
name = 'PeerRemovedError';
|
|
10
|
+
}
|
|
11
|
+
export class AppOutboxFullError extends Error {
|
|
12
|
+
name = 'AppOutboxFullError';
|
|
13
|
+
}
|
|
14
|
+
export class AppEntryTooLargeError extends Error {
|
|
15
|
+
name = 'AppEntryTooLargeError';
|
|
16
|
+
}
|
|
17
|
+
export class SendNotAdmissibleError extends Error {
|
|
18
|
+
name = 'SendNotAdmissibleError';
|
|
19
|
+
#reason;
|
|
20
|
+
constructor(reason){
|
|
21
|
+
super(`Send is not admissible: ${reason}`);
|
|
22
|
+
this.#reason = reason;
|
|
23
|
+
}
|
|
24
|
+
get reason() {
|
|
25
|
+
return this.#reason;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare class FrameTooLargeError extends Error {
|
|
2
|
+
name: string;
|
|
3
|
+
constructor(params: {
|
|
4
|
+
rawBytes: number;
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
/** Check the final sealed and framed bytes before persistence or publication. */
|
|
8
|
+
export declare function assertFrameFits(payload: Uint8Array): void;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export class FrameTooLargeError extends Error {
|
|
2
|
+
name = 'FrameTooLargeError';
|
|
3
|
+
constructor(params){
|
|
4
|
+
super(`Frame of ${params.rawBytes} raw bytes exceeds the 1,048,576-character base64 limit`);
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
/** Check the final sealed and framed bytes before persistence or publication. */ export function assertFrameFits(payload) {
|
|
8
|
+
if (4 * Math.ceil(payload.length / 3) > 1_048_576) {
|
|
9
|
+
throw new FrameTooLargeError({
|
|
10
|
+
rawBytes: payload.length
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
}
|
package/lib/handshake.d.ts
CHANGED
|
@@ -55,6 +55,8 @@ export declare const HANDSHAKE_KIND: {
|
|
|
55
55
|
readonly ledgerRequest: 3;
|
|
56
56
|
/** A member's reply carrying its whole ordered ledger, for the requester to head-verify. */
|
|
57
57
|
readonly ledgerReply: 4;
|
|
58
|
+
readonly recoveryConfirmRequest: 5;
|
|
59
|
+
readonly recoveryVerdict: 6;
|
|
58
60
|
};
|
|
59
61
|
export type HandshakeKind = (typeof HANDSHAKE_KIND)[keyof typeof HANDSHAKE_KIND];
|
|
60
62
|
/** Wrap a payload with the magic + version + kind header. */
|
package/lib/handshake.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { assertFrameFits } from './frame-size.js';
|
|
1
2
|
/**
|
|
2
3
|
* Framing for the raw MLS control lanes: un-`wrap`ped bytes on non-rotating topics (Commits on
|
|
3
4
|
* the commit topic, recovery request/reply on the rendezvous topic). Self-identifying, so a
|
|
@@ -49,7 +50,9 @@ const HEADER_LENGTH = HANDSHAKE_MAGIC.length + 2;
|
|
|
49
50
|
* authenticated ledger head (a chain digest) and no entries, so a peer that rejoined by
|
|
50
51
|
* external commit cannot ask for "the ids it is missing" — nothing enumerates them.
|
|
51
52
|
*/ ledgerRequest: 3,
|
|
52
|
-
/** A member's reply carrying its whole ordered ledger, for the requester to head-verify. */ ledgerReply: 4
|
|
53
|
+
/** A member's reply carrying its whole ordered ledger, for the requester to head-verify. */ ledgerReply: 4,
|
|
54
|
+
recoveryConfirmRequest: 5,
|
|
55
|
+
recoveryVerdict: 6
|
|
53
56
|
};
|
|
54
57
|
const KINDS = new Set(Object.values(HANDSHAKE_KIND));
|
|
55
58
|
function isHandshakeKind(value) {
|
|
@@ -61,6 +64,9 @@ function isHandshakeKind(value) {
|
|
|
61
64
|
frame[HANDSHAKE_MAGIC.length] = HANDSHAKE_VERSION;
|
|
62
65
|
frame[HANDSHAKE_MAGIC.length + 1] = kind;
|
|
63
66
|
frame.set(payload, HEADER_LENGTH);
|
|
67
|
+
if (kind === HANDSHAKE_KIND.recoveryConfirmRequest || kind === HANDSHAKE_KIND.recoveryVerdict) {
|
|
68
|
+
assertFrameFits(frame);
|
|
69
|
+
}
|
|
64
70
|
return frame;
|
|
65
71
|
}
|
|
66
72
|
/**
|