@kumiai/rpc 0.12.3 → 0.13.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/app-cursor.d.ts +5 -1
- package/lib/app-cursor.js +0 -28
- package/lib/app-lane.js +19 -5
- package/lib/commit-frame.d.ts +5 -1
- package/lib/commit-frame.js +1 -1
- package/lib/commit.d.ts +5 -1
- package/lib/crypto.d.ts +64 -20
- package/lib/crypto.js +2 -31
- package/lib/directed.d.ts +1 -0
- package/lib/directed.js +7 -2
- package/lib/handlers.d.ts +1 -1
- package/lib/handlers.js +5 -2
- package/lib/handshake.d.ts +5 -1
- package/lib/handshake.js +1 -1
- package/lib/hub-mux.js +13 -7
- package/lib/index.d.ts +11 -1
- package/lib/ledger-entries.d.ts +5 -1
- package/lib/ledger-entries.js +1 -1
- package/lib/open-once.d.ts +4 -2
- package/lib/open-once.js +7 -5
- package/lib/peer-epoch.d.ts +1 -1
- package/lib/peer-epoch.js +79 -21
- package/lib/peer-types.d.ts +10 -4
- package/lib/peer.js +160 -43
- package/lib/protocol.d.ts +5 -1
- package/lib/protocol.js +1 -1
- package/lib/recovery-confirmation.js +7 -2
- package/lib/recovery.d.ts +25 -5
- package/lib/recovery.js +5 -5
- package/lib/roster.d.ts +10 -2
- package/lib/roster.js +2 -2
- package/lib/topic.d.ts +13 -2
- package/lib/topic.js +24 -6
- package/package.json +13 -13
package/lib/peer-epoch.js
CHANGED
|
@@ -13,6 +13,18 @@ import { inboxTopic, protocolTopic } from './topic.js';
|
|
|
13
13
|
/** The app lane's anchor-bound runtimes per protocol, plus the peer's one inbox lane. */ export function createEpochRuntime(params) {
|
|
14
14
|
const { boundary, mux, crypto, localDID, protocols, handlers, suppress, appLogRetentionSeconds, anchor, sealBarrier, sealError, isDisposed } = params;
|
|
15
15
|
let runtimes = new Map();
|
|
16
|
+
// Keep one queue across topics AND epoch rebuilds. A retained pull spends keys before
|
|
17
|
+
// any later live or inbox frame can advance the same group's receive ratchet.
|
|
18
|
+
let ingressTail = Promise.resolve();
|
|
19
|
+
const ingress = (task)=>{
|
|
20
|
+
const op = ingressTail.then(async ()=>{
|
|
21
|
+
if (isDisposed()) throw new PeerDisposedError('Peer is disposed');
|
|
22
|
+
await task();
|
|
23
|
+
if (isDisposed()) throw new PeerDisposedError('Peer is disposed');
|
|
24
|
+
});
|
|
25
|
+
ingressTail = op.catch(()=>{});
|
|
26
|
+
return op;
|
|
27
|
+
};
|
|
16
28
|
/**
|
|
17
29
|
* The epoch's self-inbox topic and the one path that opens its frames. Held per peer rather
|
|
18
30
|
* than per protocol because the topic is not per protocol, and rebuilt with the epoch — the
|
|
@@ -40,7 +52,7 @@ import { inboxTopic, protocolTopic } from './topic.js';
|
|
|
40
52
|
* `true` withholds the ack so the frame survives a reconnect. Mailbox-class frames have no staging
|
|
41
53
|
* of their own (unlike `app-lane.ts`'s `note`/`ahead`), which is what made acking a transient
|
|
42
54
|
* refusal here a permanent loss.
|
|
43
|
-
*/ const retainOnFailure = (_message, error)=>isFrameAhead(error);
|
|
55
|
+
*/ const retainOnFailure = (_message, error)=>isDisposed() || isFrameAhead(error);
|
|
44
56
|
/**
|
|
45
57
|
* The app lane's inbound path: one open per topic, fanned out as plaintext, with each frame's
|
|
46
58
|
* log position noted once its open settles. Every consumer's own `unwrap` is then a pure lookup of the
|
|
@@ -51,11 +63,13 @@ import { inboxTopic, protocolTopic } from './topic.js';
|
|
|
51
63
|
return createOpenOncePath({
|
|
52
64
|
mux,
|
|
53
65
|
topicID,
|
|
66
|
+
ingress,
|
|
54
67
|
unwrap: (b)=>{
|
|
55
68
|
const hinted = crypto.frameAAD(b);
|
|
56
69
|
const decoded = hinted == null ? null : decodeAppAAD(hinted);
|
|
57
70
|
const intent = decoded?.topicID === topicID ? decoded.intent : 'ephemeral';
|
|
58
|
-
return crypto.unwrap(
|
|
71
|
+
return crypto.unwrap({
|
|
72
|
+
bytes: b,
|
|
59
73
|
expectedAAD: encodeAppAAD({
|
|
60
74
|
topicID,
|
|
61
75
|
intent
|
|
@@ -82,11 +96,11 @@ import { inboxTopic, protocolTopic } from './topic.js';
|
|
|
82
96
|
return payload;
|
|
83
97
|
},
|
|
84
98
|
note: (message, failure)=>params.note(name, topicID, message, failure),
|
|
85
|
-
wakeup: (message)=>{
|
|
99
|
+
wakeup: async (message)=>{
|
|
86
100
|
if (crypto.pending == null) return false;
|
|
87
101
|
const aad = crypto.frameAAD(message.payload);
|
|
88
102
|
if (aad == null || decodeAppAAD(aad)?.intent !== 'log') return false;
|
|
89
|
-
params.requestAppPull();
|
|
103
|
+
await params.requestAppPull();
|
|
90
104
|
return true;
|
|
91
105
|
}
|
|
92
106
|
});
|
|
@@ -111,12 +125,15 @@ import { inboxTopic, protocolTopic } from './topic.js';
|
|
|
111
125
|
// `builtFor` is the topic this transport was constructed with, and it is the fallback
|
|
112
126
|
// rather than the answer: anything published through here was sealed by the `wrap` below,
|
|
113
127
|
// so the recorded topic is the one that agrees with the seal.
|
|
114
|
-
publish: async (builtFor, payload)=>{
|
|
115
|
-
await mux.bus.publish(
|
|
128
|
+
publish: async ({ topicID: builtFor, payload })=>{
|
|
129
|
+
await mux.bus.publish({
|
|
130
|
+
topicID: sealedOn.get(payload) ?? builtFor,
|
|
131
|
+
payload
|
|
132
|
+
});
|
|
116
133
|
},
|
|
117
134
|
// Already-opened plaintext, from the topic's one inbound path. The `unwrap` below only
|
|
118
135
|
// recovers the sender the open already authenticated.
|
|
119
|
-
subscribe: (_listenOn, onMessage)=>inbound(onMessage)
|
|
136
|
+
subscribe: ({ topicID: _listenOn, onMessage })=>inbound(onMessage)
|
|
120
137
|
},
|
|
121
138
|
wrap: async (bytes)=>{
|
|
122
139
|
const sealed = await sealForSegment(name, bytes);
|
|
@@ -131,13 +148,19 @@ import { inboxTopic, protocolTopic } from './topic.js';
|
|
|
131
148
|
// ONE inbox lane for the whole peer, not one per protocol: the topic does not name a
|
|
132
149
|
// protocol, so every acceptor and directed client opening its own frames is the defect this
|
|
133
150
|
// shape prevents.
|
|
134
|
-
const selfInbox = inboxTopic(
|
|
151
|
+
const selfInbox = inboxTopic({
|
|
152
|
+
secret: anchor().secret,
|
|
153
|
+
epoch: anchor().epoch,
|
|
154
|
+
memberDID: localDID
|
|
155
|
+
});
|
|
135
156
|
inboxLane = {
|
|
136
157
|
topicID: selfInbox,
|
|
137
158
|
path: createInboxPath({
|
|
138
159
|
mux,
|
|
139
160
|
topicID: selfInbox,
|
|
140
|
-
|
|
161
|
+
ingress,
|
|
162
|
+
unwrap: (b)=>crypto.unwrap({
|
|
163
|
+
bytes: b,
|
|
141
164
|
expectedAAD: encodeAppAAD({
|
|
142
165
|
topicID: selfInbox,
|
|
143
166
|
intent: 'ephemeral'
|
|
@@ -158,7 +181,11 @@ import { inboxTopic, protocolTopic } from './topic.js';
|
|
|
158
181
|
for (const [name, protocol] of Object.entries(protocols)){
|
|
159
182
|
// The app topic is bound to the ANCHOR, not the live epoch — see {@link sealForSegment}.
|
|
160
183
|
// Content stays sealed under the live epoch crypto below; only the topic ID is anchor-bound.
|
|
161
|
-
const topicID = protocolTopic(
|
|
184
|
+
const topicID = protocolTopic({
|
|
185
|
+
secret: anchor().secret,
|
|
186
|
+
epoch: anchor().epoch,
|
|
187
|
+
protocol: name
|
|
188
|
+
});
|
|
162
189
|
// Subscribed for the member's whole life, like the commit and rendezvous topics: the mux
|
|
163
190
|
// never unsubscribes on rotation, only tears down the LISTENERS on the epoch left.
|
|
164
191
|
// Unsubscribing would tell the hub to drop this member's pending deliveries and free any
|
|
@@ -189,7 +216,11 @@ import { inboxTopic, protocolTopic } from './topic.js';
|
|
|
189
216
|
localDID,
|
|
190
217
|
selfInboxTopic: selfInbox,
|
|
191
218
|
inbound: inboxLane.path,
|
|
192
|
-
resolveSendTopic: (senderDID)=>inboxTopic(
|
|
219
|
+
resolveSendTopic: (senderDID)=>inboxTopic({
|
|
220
|
+
secret: anchor().secret,
|
|
221
|
+
epoch: anchor().epoch,
|
|
222
|
+
memberDID: senderDID
|
|
223
|
+
}),
|
|
193
224
|
protocol: protocol,
|
|
194
225
|
protocolName: name,
|
|
195
226
|
handlers: handlers[name],
|
|
@@ -243,8 +274,13 @@ import { inboxTopic, protocolTopic } from './topic.js';
|
|
|
243
274
|
continue;
|
|
244
275
|
}
|
|
245
276
|
const at = anchor();
|
|
246
|
-
const topicID = protocolTopic(
|
|
247
|
-
|
|
277
|
+
const topicID = protocolTopic({
|
|
278
|
+
secret: at.secret,
|
|
279
|
+
epoch: at.epoch,
|
|
280
|
+
protocol: name
|
|
281
|
+
});
|
|
282
|
+
const payload = await crypto.wrap({
|
|
283
|
+
bytes,
|
|
248
284
|
aad: encodeAppAAD({
|
|
249
285
|
topicID,
|
|
250
286
|
intent
|
|
@@ -272,8 +308,13 @@ import { inboxTopic, protocolTopic } from './topic.js';
|
|
|
272
308
|
*/ const sealDirectedReply = async (recipientDID, tagged)=>{
|
|
273
309
|
while(true){
|
|
274
310
|
const at = anchor();
|
|
275
|
-
const topicID = inboxTopic(
|
|
276
|
-
|
|
311
|
+
const topicID = inboxTopic({
|
|
312
|
+
secret: at.secret,
|
|
313
|
+
epoch: at.epoch,
|
|
314
|
+
memberDID: recipientDID
|
|
315
|
+
});
|
|
316
|
+
const payload = await crypto.wrap({
|
|
317
|
+
bytes: tagged,
|
|
277
318
|
aad: encodeAppAAD({
|
|
278
319
|
topicID,
|
|
279
320
|
intent: 'ephemeral'
|
|
@@ -297,8 +338,14 @@ import { inboxTopic, protocolTopic } from './topic.js';
|
|
|
297
338
|
// subscribers still receive it through the same drain.
|
|
298
339
|
const protocol = protocols[name];
|
|
299
340
|
if (protocol === undefined) throw new Error(`Unknown protocol: ${name}`);
|
|
300
|
-
if (retentionOf(
|
|
301
|
-
|
|
341
|
+
if (retentionOf({
|
|
342
|
+
protocol,
|
|
343
|
+
procedureName: prc
|
|
344
|
+
}) === 'log') {
|
|
345
|
+
const { topicID, payload } = await sealForSegment(name, encodeEventFrame({
|
|
346
|
+
prc,
|
|
347
|
+
data
|
|
348
|
+
}), 'log');
|
|
302
349
|
await mux.publish({
|
|
303
350
|
topicID,
|
|
304
351
|
payload,
|
|
@@ -306,13 +353,20 @@ import { inboxTopic, protocolTopic } from './topic.js';
|
|
|
306
353
|
});
|
|
307
354
|
return;
|
|
308
355
|
}
|
|
309
|
-
await runtime.client.dispatch(
|
|
356
|
+
await runtime.client.dispatch({
|
|
357
|
+
prc,
|
|
358
|
+
data
|
|
359
|
+
});
|
|
310
360
|
},
|
|
311
|
-
request: (prc, config)=>runtime.client.request(
|
|
361
|
+
request: (prc, config)=>runtime.client.request({
|
|
362
|
+
prc,
|
|
363
|
+
prm: config?.param,
|
|
312
364
|
errorThreshold: config?.errorThreshold,
|
|
313
365
|
timeoutMs: config?.timeoutMs
|
|
314
366
|
}),
|
|
315
|
-
gather: (prc, config)=>runtime.client.gather(
|
|
367
|
+
gather: (prc, config)=>runtime.client.gather({
|
|
368
|
+
prc,
|
|
369
|
+
prm: config?.param,
|
|
316
370
|
quorum: config?.quorum,
|
|
317
371
|
timeoutMs: config?.timeoutMs,
|
|
318
372
|
onReply: config == null ? undefined : wrapGatherOptions(boundary, config).onReply,
|
|
@@ -332,7 +386,11 @@ import { inboxTopic, protocolTopic } from './topic.js';
|
|
|
332
386
|
mux,
|
|
333
387
|
localDID,
|
|
334
388
|
memberDID: member,
|
|
335
|
-
sendTopicID: inboxTopic(
|
|
389
|
+
sendTopicID: inboxTopic({
|
|
390
|
+
secret: anchor().secret,
|
|
391
|
+
epoch: anchor().epoch,
|
|
392
|
+
memberDID: member
|
|
393
|
+
}),
|
|
336
394
|
// The epoch's own inbox topic and its one open path: reading replies through a path
|
|
337
395
|
// built for a topic this client does not receive on would spend keys opening frames for
|
|
338
396
|
// a lane nobody listens to.
|
package/lib/peer-types.d.ts
CHANGED
|
@@ -257,10 +257,18 @@ export type InternalSurface = {
|
|
|
257
257
|
} & GatherOptions) => Promise<Array<GatheredReply>>;
|
|
258
258
|
to: (memberDID: string) => Promise<Client<ProtocolDefinition>>;
|
|
259
259
|
};
|
|
260
|
+
export type GroupPeerDropAppFrameParams = {
|
|
261
|
+
topicID: string;
|
|
262
|
+
position: string;
|
|
263
|
+
};
|
|
264
|
+
export type GroupPeerCommitParams = {
|
|
265
|
+
build: () => Promise<PendingCommit>;
|
|
266
|
+
holdLogSends?: true;
|
|
267
|
+
};
|
|
260
268
|
export type GroupPeer<Protocols extends Record<string, GroupProtocolDefinition>> = {
|
|
261
269
|
protocol: <K extends keyof Protocols>(name: K) => ProtocolSurface<Protocols[K]>;
|
|
262
270
|
/** Accept loss of one buffered sealed app frame, then resume the journal-first walk. */
|
|
263
|
-
dropAppFrame: (
|
|
271
|
+
dropAppFrame: (params: GroupPeerDropAppFrameParams) => Promise<void>;
|
|
264
272
|
/** Schedule an immediate app pull and wake matching delivery workers. No-op after disposal. */
|
|
265
273
|
retryAppDelivery: (topicID?: string) => Promise<void>;
|
|
266
274
|
/**
|
|
@@ -281,9 +289,7 @@ export type GroupPeer<Protocols extends Record<string, GroupProtocolDefinition>>
|
|
|
281
289
|
* ({@link "commit".CommitDeadlineError}). Call {@link replay} after a throw to collect any
|
|
282
290
|
* undrained `lost` / `reenact` work.
|
|
283
291
|
*/
|
|
284
|
-
commit: (
|
|
285
|
-
holdLogSends?: true;
|
|
286
|
-
}) => Promise<LaneResult>;
|
|
292
|
+
commit: (params: GroupPeerCommitParams) => Promise<LaneResult>;
|
|
287
293
|
/**
|
|
288
294
|
* Replay the journal on its own, for startup: republish any pending commit under its original
|
|
289
295
|
* idempotency key and hand back what did not survive. The host's collector to call before
|
package/lib/peer.js
CHANGED
|
@@ -87,7 +87,10 @@ export function createGroupPeer(input) {
|
|
|
87
87
|
await hostJournal.put(entry);
|
|
88
88
|
heldJournalID = entry.holdsLogSends === true ? entry.publishID : undefined;
|
|
89
89
|
},
|
|
90
|
-
markAccepted: (publishID, sequenceID)=>hostJournal.markAccepted(
|
|
90
|
+
markAccepted: ({ publishID, sequenceID })=>hostJournal.markAccepted({
|
|
91
|
+
publishID,
|
|
92
|
+
sequenceID
|
|
93
|
+
}),
|
|
91
94
|
clear: async (publishID)=>{
|
|
92
95
|
await hostJournal.clear(publishID);
|
|
93
96
|
if (heldJournalID === publishID) heldJournalID = undefined;
|
|
@@ -218,14 +221,24 @@ export function createGroupPeer(input) {
|
|
|
218
221
|
};
|
|
219
222
|
/** The advance whose anchor rotation was last captured, so a retry never captures it twice. */ let capturedAdvance;
|
|
220
223
|
const captureAnchor = async (advance)=>{
|
|
221
|
-
const { secret, epoch } = await crypto.exportSecret(
|
|
224
|
+
const { secret, epoch } = await crypto.exportSecret({
|
|
225
|
+
label: APP_TOPIC_LABEL
|
|
226
|
+
});
|
|
222
227
|
if (disposed) return;
|
|
223
228
|
for (const name of Object.keys(protocols)){
|
|
224
|
-
mux.retainTopic(protocolTopic(
|
|
229
|
+
mux.retainTopic(protocolTopic({
|
|
230
|
+
secret,
|
|
231
|
+
epoch,
|
|
232
|
+
protocol: name
|
|
233
|
+
}), {
|
|
225
234
|
retention: appLogRetentionSeconds
|
|
226
235
|
});
|
|
227
236
|
}
|
|
228
|
-
mux.retainTopic(inboxTopic(
|
|
237
|
+
mux.retainTopic(inboxTopic({
|
|
238
|
+
secret,
|
|
239
|
+
epoch,
|
|
240
|
+
memberDID: localDID
|
|
241
|
+
}), {
|
|
229
242
|
retention: appLogRetentionSeconds
|
|
230
243
|
});
|
|
231
244
|
anchor = {
|
|
@@ -285,20 +298,27 @@ export function createGroupPeer(input) {
|
|
|
285
298
|
const roster = entries.map((entry)=>normalizeDID(entry.did));
|
|
286
299
|
// An Add of a DID the roster already held — a stale-leaf replacement — moves no DID, and its
|
|
287
300
|
// Welcome joiner seeds at this epoch all the same: the fresh leaf is what says so.
|
|
288
|
-
const admitted = record.admissionsBefore != null && detectLeafAdmission(
|
|
289
|
-
|
|
301
|
+
const admitted = record.admissionsBefore != null && detectLeafAdmission({
|
|
302
|
+
before: record.admissionsBefore,
|
|
303
|
+
after: admissionsOf(entries)
|
|
304
|
+
});
|
|
305
|
+
if (record.forced || admitted || detectRosterChange({
|
|
306
|
+
before: record.rosterBefore,
|
|
307
|
+
after: roster
|
|
308
|
+
})) {
|
|
290
309
|
await captureAnchor(record.advance);
|
|
291
310
|
} else {
|
|
292
311
|
await keepAnchor();
|
|
293
312
|
}
|
|
294
313
|
};
|
|
295
|
-
// Live log pushes carry no authoritative
|
|
296
|
-
//
|
|
314
|
+
// Live log pushes carry no authoritative position. Ingress waits for a successful journal-first
|
|
315
|
+
// pull. Failures retry without releasing later live opens or waiting for another hub delivery.
|
|
297
316
|
let appPullNeeded = false;
|
|
298
317
|
let appPullActive = false;
|
|
299
318
|
let appPullTimer;
|
|
300
319
|
let appPullBackoff = 1000;
|
|
301
320
|
let appPullRetryNow = false;
|
|
321
|
+
const appPullWaiters = new Set();
|
|
302
322
|
const armAppPull = (delay)=>{
|
|
303
323
|
if (disposed || appPullActive || appPullTimer != null) return;
|
|
304
324
|
appPullTimer = setTimeout(()=>{
|
|
@@ -306,6 +326,10 @@ export function createGroupPeer(input) {
|
|
|
306
326
|
if (disposed) return;
|
|
307
327
|
appPullActive = true;
|
|
308
328
|
appPullNeeded = false;
|
|
329
|
+
// Requests arriving during this pass need a subsequent fetch, even if this pass succeeds.
|
|
330
|
+
const waiters = [
|
|
331
|
+
...appPullWaiters
|
|
332
|
+
];
|
|
309
333
|
let retryDelay = 0;
|
|
310
334
|
void (async ()=>{
|
|
311
335
|
try {
|
|
@@ -313,11 +337,17 @@ export function createGroupPeer(input) {
|
|
|
313
337
|
if (disposed) return;
|
|
314
338
|
await runSerial(async ()=>{
|
|
315
339
|
await replayJournal();
|
|
316
|
-
await ensureLedger(Date.now() + recoveryTimeoutMs)
|
|
340
|
+
if (!await ensureLedger(Date.now() + recoveryTimeoutMs)) {
|
|
341
|
+
throw new Error('app pull is waiting for the ledger');
|
|
342
|
+
}
|
|
317
343
|
await reconcileCommits();
|
|
318
344
|
});
|
|
319
345
|
await healIfRequested();
|
|
320
346
|
appPullBackoff = 1000;
|
|
347
|
+
for (const resolve of waiters){
|
|
348
|
+
appPullWaiters.delete(resolve);
|
|
349
|
+
resolve();
|
|
350
|
+
}
|
|
321
351
|
} catch {
|
|
322
352
|
appPullNeeded = true;
|
|
323
353
|
retryDelay = appPullBackoff;
|
|
@@ -394,7 +424,14 @@ export function createGroupPeer(input) {
|
|
|
394
424
|
sealError: ()=>sealError,
|
|
395
425
|
isDisposed: ()=>disposed,
|
|
396
426
|
note: (name, topicID, message, failure)=>appLane.note(name, topicID, message, failure),
|
|
397
|
-
requestAppPull
|
|
427
|
+
requestAppPull: ()=>new Promise((resolve)=>{
|
|
428
|
+
if (disposed) {
|
|
429
|
+
resolve();
|
|
430
|
+
return;
|
|
431
|
+
}
|
|
432
|
+
appPullWaiters.add(resolve);
|
|
433
|
+
requestAppPull();
|
|
434
|
+
})
|
|
398
435
|
});
|
|
399
436
|
const { buildEpoch, teardownEpoch, rebuildEpoch, epochSettled, surfaceFor, sealForSegment } = epochRuntime;
|
|
400
437
|
const appOutboxAcceptance = mls == null ? undefined : createAppOutboxAcceptance({
|
|
@@ -785,7 +822,10 @@ export function createGroupPeer(input) {
|
|
|
785
822
|
position: request.position,
|
|
786
823
|
commitDigest: request.commitDigest
|
|
787
824
|
}, record.value);
|
|
788
|
-
const sealed = port.sealRecoveryVerdict(
|
|
825
|
+
const sealed = port.sealRecoveryVerdict({
|
|
826
|
+
request: request.request,
|
|
827
|
+
verdict
|
|
828
|
+
}).catch((error)=>{
|
|
789
829
|
if (verdictCache.get(cacheKey)?.value === sealed) verdictCache.delete(cacheKey);
|
|
790
830
|
throw error;
|
|
791
831
|
});
|
|
@@ -803,7 +843,13 @@ export function createGroupPeer(input) {
|
|
|
803
843
|
if (disposed || blocked() || expiresAt <= Date.now()) return;
|
|
804
844
|
void mux.publish({
|
|
805
845
|
topicID,
|
|
806
|
-
payload: encodeHandshakeFrame(
|
|
846
|
+
payload: encodeHandshakeFrame({
|
|
847
|
+
kind: HANDSHAKE_KIND.recoveryVerdict,
|
|
848
|
+
payload: encodeRecoveryVerdict({
|
|
849
|
+
requestID: request.requestID,
|
|
850
|
+
sealed
|
|
851
|
+
})
|
|
852
|
+
})
|
|
807
853
|
}).catch(()=>{});
|
|
808
854
|
}, Math.max(0, Math.min(getReplyDelayMs(), recoveryTimeoutMs, expiresAt - Date.now())));
|
|
809
855
|
verdictTimers.add(timer);
|
|
@@ -834,7 +880,13 @@ export function createGroupPeer(input) {
|
|
|
834
880
|
// head, and its reader — the requester — subscribed before it asked.
|
|
835
881
|
await mux.publish({
|
|
836
882
|
topicID,
|
|
837
|
-
payload: encodeHandshakeFrame(
|
|
883
|
+
payload: encodeHandshakeFrame({
|
|
884
|
+
kind: HANDSHAKE_KIND.recoveryReply,
|
|
885
|
+
payload: encodeRecoveryReply({
|
|
886
|
+
requestID,
|
|
887
|
+
groupInfo
|
|
888
|
+
})
|
|
889
|
+
})
|
|
838
890
|
});
|
|
839
891
|
} catch {
|
|
840
892
|
// a refused or failed reply just means another responder (or a retry) covers it
|
|
@@ -905,7 +957,13 @@ export function createGroupPeer(input) {
|
|
|
905
957
|
if (disposed) return;
|
|
906
958
|
await mux.publish({
|
|
907
959
|
topicID,
|
|
908
|
-
payload: encodeHandshakeFrame(
|
|
960
|
+
payload: encodeHandshakeFrame({
|
|
961
|
+
kind: HANDSHAKE_KIND.ledgerReply,
|
|
962
|
+
payload: encodeLedgerReply({
|
|
963
|
+
requestID: request.requestID,
|
|
964
|
+
sealed
|
|
965
|
+
})
|
|
966
|
+
})
|
|
909
967
|
});
|
|
910
968
|
} catch {
|
|
911
969
|
// a refused or failed reply just means another responder (or a retry) covers it
|
|
@@ -1293,17 +1351,26 @@ export function createGroupPeer(input) {
|
|
|
1293
1351
|
epochAfter: framedEpoch
|
|
1294
1352
|
};
|
|
1295
1353
|
}
|
|
1296
|
-
const result = await port.processCommit(
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1354
|
+
const result = await port.processCommit({
|
|
1355
|
+
commit: commitFrame.commit,
|
|
1356
|
+
context: {
|
|
1357
|
+
senderDID: message.senderDID,
|
|
1358
|
+
// The resolver, not the bodies: the blob opens only if the port asks for entries
|
|
1359
|
+
// this commit names, and only for a commit it applies — framed at this peer's
|
|
1360
|
+
// epoch, the epoch the blob is sealed under, making body delivery atomic with the
|
|
1361
|
+
// commit. Called from INSIDE the apply, so the open must not touch the handle's
|
|
1362
|
+
// ratchet: `openEntries` reads only the epoch's exporter secret and is pure.
|
|
1363
|
+
resolveLedgerEntries: createLedgerEntryResolver({
|
|
1364
|
+
sealedEntries: commitFrame.sealedEntries,
|
|
1365
|
+
openEntries: crypto.openEntries
|
|
1366
|
+
})
|
|
1367
|
+
}
|
|
1304
1368
|
});
|
|
1305
1369
|
if (header?.external && result.advanced && !stranded) {
|
|
1306
|
-
const derived = await port.confirmationKey(
|
|
1370
|
+
const derived = await port.confirmationKey({
|
|
1371
|
+
position,
|
|
1372
|
+
commitDigest
|
|
1373
|
+
});
|
|
1307
1374
|
recordCommitOutcome(position, {
|
|
1308
1375
|
kind: 'applied',
|
|
1309
1376
|
commitDigest,
|
|
@@ -1571,7 +1638,13 @@ export function createGroupPeer(input) {
|
|
|
1571
1638
|
if (framedAt != null && epoch !== framedAt) {
|
|
1572
1639
|
throw new Error('commit: the local group has already advanced past the epoch this commit was framed at. A commit is adopted in onAccepted, never before.');
|
|
1573
1640
|
}
|
|
1574
|
-
const payload = encodeHandshakeFrame(
|
|
1641
|
+
const payload = encodeHandshakeFrame({
|
|
1642
|
+
kind: HANDSHAKE_KIND.commit,
|
|
1643
|
+
payload: encodeCommitFrame({
|
|
1644
|
+
commit,
|
|
1645
|
+
sealedEntries
|
|
1646
|
+
})
|
|
1647
|
+
});
|
|
1575
1648
|
assertFrameFits(payload);
|
|
1576
1649
|
return {
|
|
1577
1650
|
payload,
|
|
@@ -1677,7 +1750,10 @@ export function createGroupPeer(input) {
|
|
|
1677
1750
|
// Record acceptance BEFORE adopting, as `commit()` does: adopting moves the handle past the
|
|
1678
1751
|
// framed epoch, and a crash between the two would leave a journalled commit indistinguishable
|
|
1679
1752
|
// from a host that adopted out of band. Written first, replay is idempotent.
|
|
1680
|
-
await journal.markAccepted(
|
|
1753
|
+
await journal.markAccepted({
|
|
1754
|
+
publishID: entry.publishID,
|
|
1755
|
+
sequenceID
|
|
1756
|
+
});
|
|
1681
1757
|
if (disposed) return false;
|
|
1682
1758
|
// This peer's own accepted frame is BOTH the last thing it processed and the log's tip —
|
|
1683
1759
|
// otherwise a `commit()` right after a `replay()` would anchor on a stale tip.
|
|
@@ -1797,7 +1873,10 @@ export function createGroupPeer(input) {
|
|
|
1797
1873
|
// responder seals to) — without the first any stranger gets the group's whole authority state
|
|
1798
1874
|
// for one publish; without the second, so does the hub.
|
|
1799
1875
|
const requestID = newPublishID();
|
|
1800
|
-
const request = await port.createRecoveryRequest(
|
|
1876
|
+
const request = await port.createRecoveryRequest({
|
|
1877
|
+
requestID,
|
|
1878
|
+
deadlineMs: Math.max(recoveryTimeoutMs, recoveryDeadlineMs)
|
|
1879
|
+
});
|
|
1801
1880
|
if (disposed) return false;
|
|
1802
1881
|
return await new Promise((resolve)=>{
|
|
1803
1882
|
let settled = false;
|
|
@@ -1826,7 +1905,10 @@ export function createGroupPeer(input) {
|
|
|
1826
1905
|
// Bytes this peer cannot open: another member's reply to another request, or a
|
|
1827
1906
|
// hub-injected forgery. Dropped, gather waits — the per-request key is NOT consumed
|
|
1828
1907
|
// here, since the next responder's reply is sealed to the same key.
|
|
1829
|
-
const tokens = await port.openSealedLedger(
|
|
1908
|
+
const tokens = await port.openSealedLedger({
|
|
1909
|
+
sealed,
|
|
1910
|
+
requestID
|
|
1911
|
+
});
|
|
1830
1912
|
if (tokens == null) return;
|
|
1831
1913
|
// Timeout or disposal can settle the gather while opening the reply.
|
|
1832
1914
|
if (settled || disposed) return;
|
|
@@ -1848,14 +1930,20 @@ export function createGroupPeer(input) {
|
|
|
1848
1930
|
});
|
|
1849
1931
|
void mux.publish({
|
|
1850
1932
|
topicID,
|
|
1851
|
-
payload: encodeHandshakeFrame(
|
|
1933
|
+
payload: encodeHandshakeFrame({
|
|
1934
|
+
kind: HANDSHAKE_KIND.ledgerRequest,
|
|
1935
|
+
payload: encodeLedgerRequest({
|
|
1936
|
+
requestID,
|
|
1937
|
+
request
|
|
1938
|
+
})
|
|
1939
|
+
})
|
|
1852
1940
|
}).catch(()=>{});
|
|
1853
1941
|
});
|
|
1854
1942
|
};
|
|
1855
1943
|
/**
|
|
1856
1944
|
* Commit to the group, rebasing until it lands or the deadline passes. Runs under the commit
|
|
1857
1945
|
* mutex for its whole life, so `build()` never races another `build()` on this device.
|
|
1858
|
-
*/ const commit = async (buildInput, options)=>{
|
|
1946
|
+
*/ const commit = async ({ build: buildInput, ...options })=>{
|
|
1859
1947
|
if (options?.holdLogSends === true) revokeHolds++;
|
|
1860
1948
|
try {
|
|
1861
1949
|
return await commitHeld(buildInput, options);
|
|
@@ -1982,7 +2070,10 @@ export function createGroupPeer(input) {
|
|
|
1982
2070
|
// the framed epoch — an entry carrying its acceptance can be adopted on restart; one
|
|
1983
2071
|
// carrying none at a later epoch is a host that adopted outside `onAccepted`. Recorded
|
|
1984
2072
|
// after the adopt, the two would be indistinguishable.
|
|
1985
|
-
await slot.markAccepted(
|
|
2073
|
+
await slot.markAccepted({
|
|
2074
|
+
publishID,
|
|
2075
|
+
sequenceID
|
|
2076
|
+
});
|
|
1986
2077
|
assertLive();
|
|
1987
2078
|
// The commit is the group's now — this frame is both the last position processed and the
|
|
1988
2079
|
// log's new tip.
|
|
@@ -2055,7 +2146,13 @@ export function createGroupPeer(input) {
|
|
|
2055
2146
|
}, wait));
|
|
2056
2147
|
void Promise.resolve(mux.publish({
|
|
2057
2148
|
topicID,
|
|
2058
|
-
payload: encodeHandshakeFrame(
|
|
2149
|
+
payload: encodeHandshakeFrame({
|
|
2150
|
+
kind: HANDSHAKE_KIND.recoveryRequest,
|
|
2151
|
+
payload: encodeRecoveryRequest({
|
|
2152
|
+
requestID,
|
|
2153
|
+
request
|
|
2154
|
+
})
|
|
2155
|
+
})
|
|
2059
2156
|
})).catch((error)=>{
|
|
2060
2157
|
if (!recoveryWaiters.delete(requestID)) return;
|
|
2061
2158
|
const timer = recoveryTimers.get(requestID);
|
|
@@ -2156,7 +2253,10 @@ export function createGroupPeer(input) {
|
|
|
2156
2253
|
assertLive();
|
|
2157
2254
|
}
|
|
2158
2255
|
const requestID = newPublishID();
|
|
2159
|
-
const request = await port.createRecoveryRequest(
|
|
2256
|
+
const request = await port.createRecoveryRequest({
|
|
2257
|
+
requestID,
|
|
2258
|
+
deadlineMs: Math.max(0, deadline - Date.now())
|
|
2259
|
+
});
|
|
2160
2260
|
assertLive();
|
|
2161
2261
|
const outcome = await requestGroupInfo(request, requestID, rendezvous, deadline);
|
|
2162
2262
|
if (outcome.kind === 'disposed' || disposed) throw new PeerDisposedError('Peer is disposed');
|
|
@@ -2173,7 +2273,10 @@ export function createGroupPeer(input) {
|
|
|
2173
2273
|
assertLive();
|
|
2174
2274
|
let pending;
|
|
2175
2275
|
try {
|
|
2176
|
-
pending = await port.applyRecovery(
|
|
2276
|
+
pending = await port.applyRecovery({
|
|
2277
|
+
sealed: outcome.sealed,
|
|
2278
|
+
requestID
|
|
2279
|
+
});
|
|
2177
2280
|
if (pending != null && !('renewalRequired' in pending)) {
|
|
2178
2281
|
pending = wrapPendingRecovery(boundary, pending);
|
|
2179
2282
|
}
|
|
@@ -2228,7 +2331,10 @@ export function createGroupPeer(input) {
|
|
|
2228
2331
|
commitDigest,
|
|
2229
2332
|
pending
|
|
2230
2333
|
};
|
|
2231
|
-
const key = await pending.confirmationKey(
|
|
2334
|
+
const key = await pending.confirmationKey({
|
|
2335
|
+
position: sequenceID,
|
|
2336
|
+
commitDigest
|
|
2337
|
+
});
|
|
2232
2338
|
const verified = await port.verifyRecoveryRequest(request);
|
|
2233
2339
|
if (verified == null) throw new Error('The recovery request did not verify');
|
|
2234
2340
|
return {
|
|
@@ -2260,12 +2366,15 @@ export function createGroupPeer(input) {
|
|
|
2260
2366
|
waiters: confirmationWaiters,
|
|
2261
2367
|
send: ()=>mux.publish({
|
|
2262
2368
|
topicID: rendezvous,
|
|
2263
|
-
payload: encodeHandshakeFrame(
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
|
|
2369
|
+
payload: encodeHandshakeFrame({
|
|
2370
|
+
kind: HANDSHAKE_KIND.recoveryConfirmRequest,
|
|
2371
|
+
payload: encodeRecoveryConfirmRequest({
|
|
2372
|
+
requestID,
|
|
2373
|
+
request,
|
|
2374
|
+
position: sequenceID,
|
|
2375
|
+
commitDigest
|
|
2376
|
+
})
|
|
2377
|
+
})
|
|
2269
2378
|
})
|
|
2270
2379
|
});
|
|
2271
2380
|
assertLive();
|
|
@@ -2627,11 +2736,17 @@ export function createGroupPeer(input) {
|
|
|
2627
2736
|
assertLive();
|
|
2628
2737
|
const protocol = protocols[key];
|
|
2629
2738
|
if (protocol == null) throw new Error(`Unknown protocol: ${key}`);
|
|
2630
|
-
if (appOutboxAcceptance != null && retentionOf(
|
|
2739
|
+
if (appOutboxAcceptance != null && retentionOf({
|
|
2740
|
+
protocol,
|
|
2741
|
+
procedureName: prc
|
|
2742
|
+
}) === 'log') {
|
|
2631
2743
|
await appOutboxAcceptance.accept({
|
|
2632
2744
|
protocol: key,
|
|
2633
2745
|
prc,
|
|
2634
|
-
data: encodeEventFrame(
|
|
2746
|
+
data: encodeEventFrame({
|
|
2747
|
+
prc,
|
|
2748
|
+
data: config?.data ?? {}
|
|
2749
|
+
})
|
|
2635
2750
|
});
|
|
2636
2751
|
logDelivery?.trigger();
|
|
2637
2752
|
return;
|
|
@@ -2655,7 +2770,7 @@ export function createGroupPeer(input) {
|
|
|
2655
2770
|
appLane.retryDelivery(topicID);
|
|
2656
2771
|
retryAppPull();
|
|
2657
2772
|
},
|
|
2658
|
-
dropAppFrame: async (topicID, position)=>{
|
|
2773
|
+
dropAppFrame: async ({ topicID, position })=>{
|
|
2659
2774
|
await ready;
|
|
2660
2775
|
assertLive();
|
|
2661
2776
|
await runSerial(async ()=>{
|
|
@@ -2716,6 +2831,8 @@ export function createGroupPeer(input) {
|
|
|
2716
2831
|
if (ledgerRetryTimer != null) clearTimeout(ledgerRetryTimer);
|
|
2717
2832
|
ledgerRetryTimer = undefined;
|
|
2718
2833
|
appPullTimer = undefined;
|
|
2834
|
+
for (const resolve of appPullWaiters)resolve();
|
|
2835
|
+
appPullWaiters.clear();
|
|
2719
2836
|
// Synchronous and FIRST, before anything is awaited: a lane op that already passed its own
|
|
2720
2837
|
// `assertLive` can be running inside `runSerial`, past the point this `dispose()` can reach
|
|
2721
2838
|
// it — awaiting the commit mutex here is unsafe (`build()`/`onAccepted()` are host-supplied
|
package/lib/protocol.d.ts
CHANGED
|
@@ -54,8 +54,12 @@ export type GroupProtocolDefinition = Record<string, GroupProcedureDefinition>;
|
|
|
54
54
|
* boundary. It fires only on `retain: 'log'`, since `'ephemeral'` is legal on every kind.
|
|
55
55
|
*/
|
|
56
56
|
export declare function defineGroupProtocol<const Definition extends GroupProtocolDefinition>(definition: Definition): Definition;
|
|
57
|
+
export type RetentionOfParams = {
|
|
58
|
+
protocol: GroupProtocolDefinition;
|
|
59
|
+
procedureName: string;
|
|
60
|
+
};
|
|
57
61
|
/**
|
|
58
62
|
* The retention a procedure was declared with in its group protocol definition. `'log'` only for
|
|
59
63
|
* an `event` procedure carrying `retain: 'log'`; `'ephemeral'` for everything else (the default).
|
|
60
64
|
*/
|
|
61
|
-
export declare function retentionOf(protocol
|
|
65
|
+
export declare function retentionOf({ protocol, procedureName }: RetentionOfParams): Retention;
|