@kumiai/rpc 0.12.4 → 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.js +4 -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/peer-epoch.js +62 -18
- package/lib/peer-types.d.ts +10 -4
- package/lib/peer.js +136 -39
- 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
|
@@ -68,7 +68,8 @@ import { inboxTopic, protocolTopic } from './topic.js';
|
|
|
68
68
|
const hinted = crypto.frameAAD(b);
|
|
69
69
|
const decoded = hinted == null ? null : decodeAppAAD(hinted);
|
|
70
70
|
const intent = decoded?.topicID === topicID ? decoded.intent : 'ephemeral';
|
|
71
|
-
return crypto.unwrap(
|
|
71
|
+
return crypto.unwrap({
|
|
72
|
+
bytes: b,
|
|
72
73
|
expectedAAD: encodeAppAAD({
|
|
73
74
|
topicID,
|
|
74
75
|
intent
|
|
@@ -124,12 +125,15 @@ import { inboxTopic, protocolTopic } from './topic.js';
|
|
|
124
125
|
// `builtFor` is the topic this transport was constructed with, and it is the fallback
|
|
125
126
|
// rather than the answer: anything published through here was sealed by the `wrap` below,
|
|
126
127
|
// so the recorded topic is the one that agrees with the seal.
|
|
127
|
-
publish: async (builtFor, payload)=>{
|
|
128
|
-
await mux.bus.publish(
|
|
128
|
+
publish: async ({ topicID: builtFor, payload })=>{
|
|
129
|
+
await mux.bus.publish({
|
|
130
|
+
topicID: sealedOn.get(payload) ?? builtFor,
|
|
131
|
+
payload
|
|
132
|
+
});
|
|
129
133
|
},
|
|
130
134
|
// Already-opened plaintext, from the topic's one inbound path. The `unwrap` below only
|
|
131
135
|
// recovers the sender the open already authenticated.
|
|
132
|
-
subscribe: (_listenOn, onMessage)=>inbound(onMessage)
|
|
136
|
+
subscribe: ({ topicID: _listenOn, onMessage })=>inbound(onMessage)
|
|
133
137
|
},
|
|
134
138
|
wrap: async (bytes)=>{
|
|
135
139
|
const sealed = await sealForSegment(name, bytes);
|
|
@@ -144,14 +148,19 @@ import { inboxTopic, protocolTopic } from './topic.js';
|
|
|
144
148
|
// ONE inbox lane for the whole peer, not one per protocol: the topic does not name a
|
|
145
149
|
// protocol, so every acceptor and directed client opening its own frames is the defect this
|
|
146
150
|
// shape prevents.
|
|
147
|
-
const selfInbox = inboxTopic(
|
|
151
|
+
const selfInbox = inboxTopic({
|
|
152
|
+
secret: anchor().secret,
|
|
153
|
+
epoch: anchor().epoch,
|
|
154
|
+
memberDID: localDID
|
|
155
|
+
});
|
|
148
156
|
inboxLane = {
|
|
149
157
|
topicID: selfInbox,
|
|
150
158
|
path: createInboxPath({
|
|
151
159
|
mux,
|
|
152
160
|
topicID: selfInbox,
|
|
153
161
|
ingress,
|
|
154
|
-
unwrap: (b)=>crypto.unwrap(
|
|
162
|
+
unwrap: (b)=>crypto.unwrap({
|
|
163
|
+
bytes: b,
|
|
155
164
|
expectedAAD: encodeAppAAD({
|
|
156
165
|
topicID: selfInbox,
|
|
157
166
|
intent: 'ephemeral'
|
|
@@ -172,7 +181,11 @@ import { inboxTopic, protocolTopic } from './topic.js';
|
|
|
172
181
|
for (const [name, protocol] of Object.entries(protocols)){
|
|
173
182
|
// The app topic is bound to the ANCHOR, not the live epoch — see {@link sealForSegment}.
|
|
174
183
|
// Content stays sealed under the live epoch crypto below; only the topic ID is anchor-bound.
|
|
175
|
-
const topicID = protocolTopic(
|
|
184
|
+
const topicID = protocolTopic({
|
|
185
|
+
secret: anchor().secret,
|
|
186
|
+
epoch: anchor().epoch,
|
|
187
|
+
protocol: name
|
|
188
|
+
});
|
|
176
189
|
// Subscribed for the member's whole life, like the commit and rendezvous topics: the mux
|
|
177
190
|
// never unsubscribes on rotation, only tears down the LISTENERS on the epoch left.
|
|
178
191
|
// Unsubscribing would tell the hub to drop this member's pending deliveries and free any
|
|
@@ -203,7 +216,11 @@ import { inboxTopic, protocolTopic } from './topic.js';
|
|
|
203
216
|
localDID,
|
|
204
217
|
selfInboxTopic: selfInbox,
|
|
205
218
|
inbound: inboxLane.path,
|
|
206
|
-
resolveSendTopic: (senderDID)=>inboxTopic(
|
|
219
|
+
resolveSendTopic: (senderDID)=>inboxTopic({
|
|
220
|
+
secret: anchor().secret,
|
|
221
|
+
epoch: anchor().epoch,
|
|
222
|
+
memberDID: senderDID
|
|
223
|
+
}),
|
|
207
224
|
protocol: protocol,
|
|
208
225
|
protocolName: name,
|
|
209
226
|
handlers: handlers[name],
|
|
@@ -257,8 +274,13 @@ import { inboxTopic, protocolTopic } from './topic.js';
|
|
|
257
274
|
continue;
|
|
258
275
|
}
|
|
259
276
|
const at = anchor();
|
|
260
|
-
const topicID = protocolTopic(
|
|
261
|
-
|
|
277
|
+
const topicID = protocolTopic({
|
|
278
|
+
secret: at.secret,
|
|
279
|
+
epoch: at.epoch,
|
|
280
|
+
protocol: name
|
|
281
|
+
});
|
|
282
|
+
const payload = await crypto.wrap({
|
|
283
|
+
bytes,
|
|
262
284
|
aad: encodeAppAAD({
|
|
263
285
|
topicID,
|
|
264
286
|
intent
|
|
@@ -286,8 +308,13 @@ import { inboxTopic, protocolTopic } from './topic.js';
|
|
|
286
308
|
*/ const sealDirectedReply = async (recipientDID, tagged)=>{
|
|
287
309
|
while(true){
|
|
288
310
|
const at = anchor();
|
|
289
|
-
const topicID = inboxTopic(
|
|
290
|
-
|
|
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,
|
|
291
318
|
aad: encodeAppAAD({
|
|
292
319
|
topicID,
|
|
293
320
|
intent: 'ephemeral'
|
|
@@ -311,8 +338,14 @@ import { inboxTopic, protocolTopic } from './topic.js';
|
|
|
311
338
|
// subscribers still receive it through the same drain.
|
|
312
339
|
const protocol = protocols[name];
|
|
313
340
|
if (protocol === undefined) throw new Error(`Unknown protocol: ${name}`);
|
|
314
|
-
if (retentionOf(
|
|
315
|
-
|
|
341
|
+
if (retentionOf({
|
|
342
|
+
protocol,
|
|
343
|
+
procedureName: prc
|
|
344
|
+
}) === 'log') {
|
|
345
|
+
const { topicID, payload } = await sealForSegment(name, encodeEventFrame({
|
|
346
|
+
prc,
|
|
347
|
+
data
|
|
348
|
+
}), 'log');
|
|
316
349
|
await mux.publish({
|
|
317
350
|
topicID,
|
|
318
351
|
payload,
|
|
@@ -320,13 +353,20 @@ import { inboxTopic, protocolTopic } from './topic.js';
|
|
|
320
353
|
});
|
|
321
354
|
return;
|
|
322
355
|
}
|
|
323
|
-
await runtime.client.dispatch(
|
|
356
|
+
await runtime.client.dispatch({
|
|
357
|
+
prc,
|
|
358
|
+
data
|
|
359
|
+
});
|
|
324
360
|
},
|
|
325
|
-
request: (prc, config)=>runtime.client.request(
|
|
361
|
+
request: (prc, config)=>runtime.client.request({
|
|
362
|
+
prc,
|
|
363
|
+
prm: config?.param,
|
|
326
364
|
errorThreshold: config?.errorThreshold,
|
|
327
365
|
timeoutMs: config?.timeoutMs
|
|
328
366
|
}),
|
|
329
|
-
gather: (prc, config)=>runtime.client.gather(
|
|
367
|
+
gather: (prc, config)=>runtime.client.gather({
|
|
368
|
+
prc,
|
|
369
|
+
prm: config?.param,
|
|
330
370
|
quorum: config?.quorum,
|
|
331
371
|
timeoutMs: config?.timeoutMs,
|
|
332
372
|
onReply: config == null ? undefined : wrapGatherOptions(boundary, config).onReply,
|
|
@@ -346,7 +386,11 @@ import { inboxTopic, protocolTopic } from './topic.js';
|
|
|
346
386
|
mux,
|
|
347
387
|
localDID,
|
|
348
388
|
memberDID: member,
|
|
349
|
-
sendTopicID: inboxTopic(
|
|
389
|
+
sendTopicID: inboxTopic({
|
|
390
|
+
secret: anchor().secret,
|
|
391
|
+
epoch: anchor().epoch,
|
|
392
|
+
memberDID: member
|
|
393
|
+
}),
|
|
350
394
|
// The epoch's own inbox topic and its one open path: reading replies through a path
|
|
351
395
|
// built for a topic this client does not receive on would spend keys opening frames for
|
|
352
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,8 +298,14 @@ 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();
|
|
@@ -803,7 +822,10 @@ export function createGroupPeer(input) {
|
|
|
803
822
|
position: request.position,
|
|
804
823
|
commitDigest: request.commitDigest
|
|
805
824
|
}, record.value);
|
|
806
|
-
const sealed = port.sealRecoveryVerdict(
|
|
825
|
+
const sealed = port.sealRecoveryVerdict({
|
|
826
|
+
request: request.request,
|
|
827
|
+
verdict
|
|
828
|
+
}).catch((error)=>{
|
|
807
829
|
if (verdictCache.get(cacheKey)?.value === sealed) verdictCache.delete(cacheKey);
|
|
808
830
|
throw error;
|
|
809
831
|
});
|
|
@@ -821,7 +843,13 @@ export function createGroupPeer(input) {
|
|
|
821
843
|
if (disposed || blocked() || expiresAt <= Date.now()) return;
|
|
822
844
|
void mux.publish({
|
|
823
845
|
topicID,
|
|
824
|
-
payload: encodeHandshakeFrame(
|
|
846
|
+
payload: encodeHandshakeFrame({
|
|
847
|
+
kind: HANDSHAKE_KIND.recoveryVerdict,
|
|
848
|
+
payload: encodeRecoveryVerdict({
|
|
849
|
+
requestID: request.requestID,
|
|
850
|
+
sealed
|
|
851
|
+
})
|
|
852
|
+
})
|
|
825
853
|
}).catch(()=>{});
|
|
826
854
|
}, Math.max(0, Math.min(getReplyDelayMs(), recoveryTimeoutMs, expiresAt - Date.now())));
|
|
827
855
|
verdictTimers.add(timer);
|
|
@@ -852,7 +880,13 @@ export function createGroupPeer(input) {
|
|
|
852
880
|
// head, and its reader — the requester — subscribed before it asked.
|
|
853
881
|
await mux.publish({
|
|
854
882
|
topicID,
|
|
855
|
-
payload: encodeHandshakeFrame(
|
|
883
|
+
payload: encodeHandshakeFrame({
|
|
884
|
+
kind: HANDSHAKE_KIND.recoveryReply,
|
|
885
|
+
payload: encodeRecoveryReply({
|
|
886
|
+
requestID,
|
|
887
|
+
groupInfo
|
|
888
|
+
})
|
|
889
|
+
})
|
|
856
890
|
});
|
|
857
891
|
} catch {
|
|
858
892
|
// a refused or failed reply just means another responder (or a retry) covers it
|
|
@@ -923,7 +957,13 @@ export function createGroupPeer(input) {
|
|
|
923
957
|
if (disposed) return;
|
|
924
958
|
await mux.publish({
|
|
925
959
|
topicID,
|
|
926
|
-
payload: encodeHandshakeFrame(
|
|
960
|
+
payload: encodeHandshakeFrame({
|
|
961
|
+
kind: HANDSHAKE_KIND.ledgerReply,
|
|
962
|
+
payload: encodeLedgerReply({
|
|
963
|
+
requestID: request.requestID,
|
|
964
|
+
sealed
|
|
965
|
+
})
|
|
966
|
+
})
|
|
927
967
|
});
|
|
928
968
|
} catch {
|
|
929
969
|
// a refused or failed reply just means another responder (or a retry) covers it
|
|
@@ -1311,17 +1351,26 @@ export function createGroupPeer(input) {
|
|
|
1311
1351
|
epochAfter: framedEpoch
|
|
1312
1352
|
};
|
|
1313
1353
|
}
|
|
1314
|
-
const result = await port.processCommit(
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
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
|
+
}
|
|
1322
1368
|
});
|
|
1323
1369
|
if (header?.external && result.advanced && !stranded) {
|
|
1324
|
-
const derived = await port.confirmationKey(
|
|
1370
|
+
const derived = await port.confirmationKey({
|
|
1371
|
+
position,
|
|
1372
|
+
commitDigest
|
|
1373
|
+
});
|
|
1325
1374
|
recordCommitOutcome(position, {
|
|
1326
1375
|
kind: 'applied',
|
|
1327
1376
|
commitDigest,
|
|
@@ -1589,7 +1638,13 @@ export function createGroupPeer(input) {
|
|
|
1589
1638
|
if (framedAt != null && epoch !== framedAt) {
|
|
1590
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.');
|
|
1591
1640
|
}
|
|
1592
|
-
const payload = encodeHandshakeFrame(
|
|
1641
|
+
const payload = encodeHandshakeFrame({
|
|
1642
|
+
kind: HANDSHAKE_KIND.commit,
|
|
1643
|
+
payload: encodeCommitFrame({
|
|
1644
|
+
commit,
|
|
1645
|
+
sealedEntries
|
|
1646
|
+
})
|
|
1647
|
+
});
|
|
1593
1648
|
assertFrameFits(payload);
|
|
1594
1649
|
return {
|
|
1595
1650
|
payload,
|
|
@@ -1695,7 +1750,10 @@ export function createGroupPeer(input) {
|
|
|
1695
1750
|
// Record acceptance BEFORE adopting, as `commit()` does: adopting moves the handle past the
|
|
1696
1751
|
// framed epoch, and a crash between the two would leave a journalled commit indistinguishable
|
|
1697
1752
|
// from a host that adopted out of band. Written first, replay is idempotent.
|
|
1698
|
-
await journal.markAccepted(
|
|
1753
|
+
await journal.markAccepted({
|
|
1754
|
+
publishID: entry.publishID,
|
|
1755
|
+
sequenceID
|
|
1756
|
+
});
|
|
1699
1757
|
if (disposed) return false;
|
|
1700
1758
|
// This peer's own accepted frame is BOTH the last thing it processed and the log's tip —
|
|
1701
1759
|
// otherwise a `commit()` right after a `replay()` would anchor on a stale tip.
|
|
@@ -1815,7 +1873,10 @@ export function createGroupPeer(input) {
|
|
|
1815
1873
|
// responder seals to) — without the first any stranger gets the group's whole authority state
|
|
1816
1874
|
// for one publish; without the second, so does the hub.
|
|
1817
1875
|
const requestID = newPublishID();
|
|
1818
|
-
const request = await port.createRecoveryRequest(
|
|
1876
|
+
const request = await port.createRecoveryRequest({
|
|
1877
|
+
requestID,
|
|
1878
|
+
deadlineMs: Math.max(recoveryTimeoutMs, recoveryDeadlineMs)
|
|
1879
|
+
});
|
|
1819
1880
|
if (disposed) return false;
|
|
1820
1881
|
return await new Promise((resolve)=>{
|
|
1821
1882
|
let settled = false;
|
|
@@ -1844,7 +1905,10 @@ export function createGroupPeer(input) {
|
|
|
1844
1905
|
// Bytes this peer cannot open: another member's reply to another request, or a
|
|
1845
1906
|
// hub-injected forgery. Dropped, gather waits — the per-request key is NOT consumed
|
|
1846
1907
|
// here, since the next responder's reply is sealed to the same key.
|
|
1847
|
-
const tokens = await port.openSealedLedger(
|
|
1908
|
+
const tokens = await port.openSealedLedger({
|
|
1909
|
+
sealed,
|
|
1910
|
+
requestID
|
|
1911
|
+
});
|
|
1848
1912
|
if (tokens == null) return;
|
|
1849
1913
|
// Timeout or disposal can settle the gather while opening the reply.
|
|
1850
1914
|
if (settled || disposed) return;
|
|
@@ -1866,14 +1930,20 @@ export function createGroupPeer(input) {
|
|
|
1866
1930
|
});
|
|
1867
1931
|
void mux.publish({
|
|
1868
1932
|
topicID,
|
|
1869
|
-
payload: encodeHandshakeFrame(
|
|
1933
|
+
payload: encodeHandshakeFrame({
|
|
1934
|
+
kind: HANDSHAKE_KIND.ledgerRequest,
|
|
1935
|
+
payload: encodeLedgerRequest({
|
|
1936
|
+
requestID,
|
|
1937
|
+
request
|
|
1938
|
+
})
|
|
1939
|
+
})
|
|
1870
1940
|
}).catch(()=>{});
|
|
1871
1941
|
});
|
|
1872
1942
|
};
|
|
1873
1943
|
/**
|
|
1874
1944
|
* Commit to the group, rebasing until it lands or the deadline passes. Runs under the commit
|
|
1875
1945
|
* mutex for its whole life, so `build()` never races another `build()` on this device.
|
|
1876
|
-
*/ const commit = async (buildInput, options)=>{
|
|
1946
|
+
*/ const commit = async ({ build: buildInput, ...options })=>{
|
|
1877
1947
|
if (options?.holdLogSends === true) revokeHolds++;
|
|
1878
1948
|
try {
|
|
1879
1949
|
return await commitHeld(buildInput, options);
|
|
@@ -2000,7 +2070,10 @@ export function createGroupPeer(input) {
|
|
|
2000
2070
|
// the framed epoch — an entry carrying its acceptance can be adopted on restart; one
|
|
2001
2071
|
// carrying none at a later epoch is a host that adopted outside `onAccepted`. Recorded
|
|
2002
2072
|
// after the adopt, the two would be indistinguishable.
|
|
2003
|
-
await slot.markAccepted(
|
|
2073
|
+
await slot.markAccepted({
|
|
2074
|
+
publishID,
|
|
2075
|
+
sequenceID
|
|
2076
|
+
});
|
|
2004
2077
|
assertLive();
|
|
2005
2078
|
// The commit is the group's now — this frame is both the last position processed and the
|
|
2006
2079
|
// log's new tip.
|
|
@@ -2073,7 +2146,13 @@ export function createGroupPeer(input) {
|
|
|
2073
2146
|
}, wait));
|
|
2074
2147
|
void Promise.resolve(mux.publish({
|
|
2075
2148
|
topicID,
|
|
2076
|
-
payload: encodeHandshakeFrame(
|
|
2149
|
+
payload: encodeHandshakeFrame({
|
|
2150
|
+
kind: HANDSHAKE_KIND.recoveryRequest,
|
|
2151
|
+
payload: encodeRecoveryRequest({
|
|
2152
|
+
requestID,
|
|
2153
|
+
request
|
|
2154
|
+
})
|
|
2155
|
+
})
|
|
2077
2156
|
})).catch((error)=>{
|
|
2078
2157
|
if (!recoveryWaiters.delete(requestID)) return;
|
|
2079
2158
|
const timer = recoveryTimers.get(requestID);
|
|
@@ -2174,7 +2253,10 @@ export function createGroupPeer(input) {
|
|
|
2174
2253
|
assertLive();
|
|
2175
2254
|
}
|
|
2176
2255
|
const requestID = newPublishID();
|
|
2177
|
-
const request = await port.createRecoveryRequest(
|
|
2256
|
+
const request = await port.createRecoveryRequest({
|
|
2257
|
+
requestID,
|
|
2258
|
+
deadlineMs: Math.max(0, deadline - Date.now())
|
|
2259
|
+
});
|
|
2178
2260
|
assertLive();
|
|
2179
2261
|
const outcome = await requestGroupInfo(request, requestID, rendezvous, deadline);
|
|
2180
2262
|
if (outcome.kind === 'disposed' || disposed) throw new PeerDisposedError('Peer is disposed');
|
|
@@ -2191,7 +2273,10 @@ export function createGroupPeer(input) {
|
|
|
2191
2273
|
assertLive();
|
|
2192
2274
|
let pending;
|
|
2193
2275
|
try {
|
|
2194
|
-
pending = await port.applyRecovery(
|
|
2276
|
+
pending = await port.applyRecovery({
|
|
2277
|
+
sealed: outcome.sealed,
|
|
2278
|
+
requestID
|
|
2279
|
+
});
|
|
2195
2280
|
if (pending != null && !('renewalRequired' in pending)) {
|
|
2196
2281
|
pending = wrapPendingRecovery(boundary, pending);
|
|
2197
2282
|
}
|
|
@@ -2246,7 +2331,10 @@ export function createGroupPeer(input) {
|
|
|
2246
2331
|
commitDigest,
|
|
2247
2332
|
pending
|
|
2248
2333
|
};
|
|
2249
|
-
const key = await pending.confirmationKey(
|
|
2334
|
+
const key = await pending.confirmationKey({
|
|
2335
|
+
position: sequenceID,
|
|
2336
|
+
commitDigest
|
|
2337
|
+
});
|
|
2250
2338
|
const verified = await port.verifyRecoveryRequest(request);
|
|
2251
2339
|
if (verified == null) throw new Error('The recovery request did not verify');
|
|
2252
2340
|
return {
|
|
@@ -2278,12 +2366,15 @@ export function createGroupPeer(input) {
|
|
|
2278
2366
|
waiters: confirmationWaiters,
|
|
2279
2367
|
send: ()=>mux.publish({
|
|
2280
2368
|
topicID: rendezvous,
|
|
2281
|
-
payload: encodeHandshakeFrame(
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
|
|
2286
|
-
|
|
2369
|
+
payload: encodeHandshakeFrame({
|
|
2370
|
+
kind: HANDSHAKE_KIND.recoveryConfirmRequest,
|
|
2371
|
+
payload: encodeRecoveryConfirmRequest({
|
|
2372
|
+
requestID,
|
|
2373
|
+
request,
|
|
2374
|
+
position: sequenceID,
|
|
2375
|
+
commitDigest
|
|
2376
|
+
})
|
|
2377
|
+
})
|
|
2287
2378
|
})
|
|
2288
2379
|
});
|
|
2289
2380
|
assertLive();
|
|
@@ -2645,11 +2736,17 @@ export function createGroupPeer(input) {
|
|
|
2645
2736
|
assertLive();
|
|
2646
2737
|
const protocol = protocols[key];
|
|
2647
2738
|
if (protocol == null) throw new Error(`Unknown protocol: ${key}`);
|
|
2648
|
-
if (appOutboxAcceptance != null && retentionOf(
|
|
2739
|
+
if (appOutboxAcceptance != null && retentionOf({
|
|
2740
|
+
protocol,
|
|
2741
|
+
procedureName: prc
|
|
2742
|
+
}) === 'log') {
|
|
2649
2743
|
await appOutboxAcceptance.accept({
|
|
2650
2744
|
protocol: key,
|
|
2651
2745
|
prc,
|
|
2652
|
-
data: encodeEventFrame(
|
|
2746
|
+
data: encodeEventFrame({
|
|
2747
|
+
prc,
|
|
2748
|
+
data: config?.data ?? {}
|
|
2749
|
+
})
|
|
2653
2750
|
});
|
|
2654
2751
|
logDelivery?.trigger();
|
|
2655
2752
|
return;
|
|
@@ -2673,7 +2770,7 @@ export function createGroupPeer(input) {
|
|
|
2673
2770
|
appLane.retryDelivery(topicID);
|
|
2674
2771
|
retryAppPull();
|
|
2675
2772
|
},
|
|
2676
|
-
dropAppFrame: async (topicID, position)=>{
|
|
2773
|
+
dropAppFrame: async ({ topicID, position })=>{
|
|
2677
2774
|
await ready;
|
|
2678
2775
|
assertLive();
|
|
2679
2776
|
await runSerial(async ()=>{
|
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;
|
package/lib/protocol.js
CHANGED
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
/**
|
|
26
26
|
* The retention a procedure was declared with in its group protocol definition. `'log'` only for
|
|
27
27
|
* an `event` procedure carrying `retain: 'log'`; `'ephemeral'` for everything else (the default).
|
|
28
|
-
*/ export function retentionOf(protocol, procedureName) {
|
|
28
|
+
*/ export function retentionOf({ protocol, procedureName }) {
|
|
29
29
|
const procedure = protocol[procedureName];
|
|
30
30
|
return procedure?.type === 'event' && procedure.retain === 'log' ? 'log' : 'ephemeral';
|
|
31
31
|
}
|
|
@@ -86,13 +86,18 @@ export function waitForRecoveryConfirmation(params) {
|
|
|
86
86
|
receive: (sealed)=>{
|
|
87
87
|
opening = opening.then(async ()=>{
|
|
88
88
|
if (finished) return;
|
|
89
|
-
const opened = await port.openRecoveryVerdict(
|
|
89
|
+
const opened = await port.openRecoveryVerdict({
|
|
90
|
+
sealed,
|
|
91
|
+
requestID
|
|
92
|
+
});
|
|
90
93
|
if (finished || opened == null) return;
|
|
91
94
|
const verdict = opened.verdict;
|
|
92
95
|
const boundToAttempt = verdict.groupID === groupID && verdict.requestID === requestID && verdict.position === position && verdict.commitDigest === commitDigest;
|
|
93
96
|
if (!boundToAttempt) return;
|
|
94
97
|
if (verdict.verdict === 'confirmed' && (verdict.epoch !== pending.epoch || verdict.tag !== confirmationTag(key, requestID))) return;
|
|
95
|
-
|
|
98
|
+
const standing = await pending.judgeVerdict(opened);
|
|
99
|
+
if (finished) return;
|
|
100
|
+
if (standing === 'advisory') {
|
|
96
101
|
advisory.set(JSON.stringify(opened), opened);
|
|
97
102
|
if (advisory.size > 1024) {
|
|
98
103
|
const oldest = advisory.keys().next().value;
|
package/lib/recovery.d.ts
CHANGED
|
@@ -1,23 +1,39 @@
|
|
|
1
|
-
export
|
|
1
|
+
export type EncodeRecoveryRequestParams = {
|
|
2
|
+
requestID: string;
|
|
3
|
+
request: Uint8Array;
|
|
4
|
+
};
|
|
5
|
+
export declare function encodeRecoveryRequest({ requestID, request, }: EncodeRecoveryRequestParams): Uint8Array;
|
|
2
6
|
export declare function decodeRecoveryRequest(payload: Uint8Array): {
|
|
3
7
|
requestID: string;
|
|
4
8
|
request: Uint8Array;
|
|
5
9
|
};
|
|
6
|
-
export
|
|
10
|
+
export type EncodeRecoveryReplyParams = {
|
|
11
|
+
requestID: string;
|
|
12
|
+
groupInfo: Uint8Array;
|
|
13
|
+
};
|
|
14
|
+
export declare function encodeRecoveryReply({ requestID, groupInfo, }: EncodeRecoveryReplyParams): Uint8Array;
|
|
7
15
|
export declare function decodeRecoveryReply(payload: Uint8Array): {
|
|
8
16
|
requestID: string;
|
|
9
17
|
groupInfo: Uint8Array;
|
|
10
18
|
};
|
|
19
|
+
export type EncodeLedgerRequestParams = {
|
|
20
|
+
requestID: string;
|
|
21
|
+
request: Uint8Array;
|
|
22
|
+
};
|
|
11
23
|
/**
|
|
12
24
|
* A ledger gather carries the port's signed request blob, as a recovery request does: it is
|
|
13
25
|
* what a responder authorizes against and the only key it will seal to. A request with no blob
|
|
14
26
|
* is from nobody, and every responder refuses it.
|
|
15
27
|
*/
|
|
16
|
-
export declare function encodeLedgerRequest(requestID
|
|
28
|
+
export declare function encodeLedgerRequest({ requestID, request }: EncodeLedgerRequestParams): Uint8Array;
|
|
17
29
|
export declare function decodeLedgerRequest(payload: Uint8Array): {
|
|
18
30
|
requestID: string;
|
|
19
31
|
request: Uint8Array;
|
|
20
32
|
};
|
|
33
|
+
export type EncodeLedgerReplyParams = {
|
|
34
|
+
requestID: string;
|
|
35
|
+
sealed: Uint8Array;
|
|
36
|
+
};
|
|
21
37
|
/**
|
|
22
38
|
* A responder's whole ordered ledger, SEALED to the ephemeral key inside the request it
|
|
23
39
|
* answers. The lane carries the bytes and reads none of them.
|
|
@@ -25,7 +41,7 @@ export declare function decodeLedgerRequest(payload: Uint8Array): {
|
|
|
25
41
|
* Order is inside the seal and load-bearing: the head is a chain digest, so a permuted list of
|
|
26
42
|
* the same tokens folds to a different head and the requester rejects it.
|
|
27
43
|
*/
|
|
28
|
-
export declare function encodeLedgerReply(requestID
|
|
44
|
+
export declare function encodeLedgerReply({ requestID, sealed }: EncodeLedgerReplyParams): Uint8Array;
|
|
29
45
|
export declare function decodeLedgerReply(payload: Uint8Array): {
|
|
30
46
|
requestID: string;
|
|
31
47
|
sealed: Uint8Array;
|
|
@@ -38,7 +54,11 @@ export type RecoveryConfirmRequest = {
|
|
|
38
54
|
};
|
|
39
55
|
export declare function encodeRecoveryConfirmRequest(value: RecoveryConfirmRequest): Uint8Array;
|
|
40
56
|
export declare function decodeRecoveryConfirmRequest(payload: Uint8Array): RecoveryConfirmRequest;
|
|
41
|
-
export
|
|
57
|
+
export type EncodeRecoveryVerdictParams = {
|
|
58
|
+
requestID: string;
|
|
59
|
+
sealed: Uint8Array;
|
|
60
|
+
};
|
|
61
|
+
export declare function encodeRecoveryVerdict({ requestID, sealed, }: EncodeRecoveryVerdictParams): Uint8Array;
|
|
42
62
|
export type RecoveryVerdictFrame = {
|
|
43
63
|
requestID: string;
|
|
44
64
|
sealed: Uint8Array;
|