@kumiai/rpc 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/classify.d.ts +132 -0
- package/lib/classify.js +48 -0
- package/lib/commit-frame.d.ts +32 -0
- package/lib/commit-frame.js +43 -0
- package/lib/commit.d.ts +188 -0
- package/lib/commit.js +50 -0
- package/lib/crypto.d.ts +205 -32
- package/lib/crypto.js +16 -8
- package/lib/cursor.d.ts +33 -0
- package/lib/cursor.js +26 -0
- package/lib/directed-crypto.d.ts +17 -0
- package/lib/directed-crypto.js +80 -0
- package/lib/directed.d.ts +11 -4
- package/lib/directed.js +168 -29
- package/lib/handshake.d.ts +13 -6
- package/lib/handshake.js +12 -7
- package/lib/hub-mux.d.ts +51 -12
- package/lib/hub-mux.js +66 -26
- package/lib/index.d.ts +9 -5
- package/lib/index.js +8 -3
- package/lib/ledger-entries.d.ts +22 -0
- package/lib/ledger-entries.js +78 -0
- package/lib/peer.d.ts +105 -15
- package/lib/peer.js +1009 -100
- package/lib/recovery.d.ts +27 -7
- package/lib/recovery.js +73 -18
- package/lib/topic.d.ts +18 -7
- package/lib/topic.js +18 -7
- package/package.json +11 -10
- package/lib/memory-group-mls.d.ts +0 -21
- package/lib/memory-group-mls.js +0 -55
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { fromUTF, toUTF } from '@sozai/codec';
|
|
2
|
+
/**
|
|
3
|
+
* The blob a commit frame carries: the signed control-ledger tokens the Commit enacts, sealed
|
|
4
|
+
* with `GroupCrypto.wrap` under the epoch secret the Commit is framed at (the epoch every peer
|
|
5
|
+
* that can apply it holds). The hub sees only the sealed bytes, never a body.
|
|
6
|
+
*
|
|
7
|
+
* [ count(2, LE) | (tokenLength(4, LE) | token utf8)... ]
|
|
8
|
+
*
|
|
9
|
+
* This module is the ONLY place a commit frame's blob is opened, via a resolver that runs only
|
|
10
|
+
* when the MLS port asks for the bodies of a commit it is applying. Unwrapping is a consequence
|
|
11
|
+
* of "I can apply this frame", never a precondition of reading it.
|
|
12
|
+
*/ const COUNT_BYTES = 2;
|
|
13
|
+
const LENGTH_BYTES = 4;
|
|
14
|
+
/** Encode the signed tokens a Commit enacts, for sealing into its frame. */ export function encodeLedgerEntries(tokens) {
|
|
15
|
+
const encoded = tokens.map((token)=>fromUTF(token));
|
|
16
|
+
const size = encoded.reduce((total, bytes)=>total + LENGTH_BYTES + bytes.length, COUNT_BYTES);
|
|
17
|
+
const out = new Uint8Array(size);
|
|
18
|
+
const view = new DataView(out.buffer);
|
|
19
|
+
view.setUint16(0, encoded.length, true);
|
|
20
|
+
let offset = COUNT_BYTES;
|
|
21
|
+
for (const bytes of encoded){
|
|
22
|
+
view.setUint32(offset, bytes.length, true);
|
|
23
|
+
out.set(bytes, offset + LENGTH_BYTES);
|
|
24
|
+
offset += LENGTH_BYTES + bytes.length;
|
|
25
|
+
}
|
|
26
|
+
return out;
|
|
27
|
+
}
|
|
28
|
+
/** Decode the signed tokens sealed into a commit frame. Throws on bytes that are not a list. */ export function decodeLedgerEntries(bytes) {
|
|
29
|
+
if (bytes.length < COUNT_BYTES) {
|
|
30
|
+
throw new Error('ledger entry blob is too short');
|
|
31
|
+
}
|
|
32
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
33
|
+
const count = view.getUint16(0, true);
|
|
34
|
+
const tokens = [];
|
|
35
|
+
let offset = COUNT_BYTES;
|
|
36
|
+
for(let i = 0; i < count; i++){
|
|
37
|
+
if (offset + LENGTH_BYTES > bytes.length) {
|
|
38
|
+
throw new Error('ledger entry blob is truncated');
|
|
39
|
+
}
|
|
40
|
+
const length = view.getUint32(offset, true);
|
|
41
|
+
const start = offset + LENGTH_BYTES;
|
|
42
|
+
if (start + length > bytes.length) {
|
|
43
|
+
throw new Error('ledger entry blob is truncated');
|
|
44
|
+
}
|
|
45
|
+
tokens.push(toUTF(bytes.subarray(start, start + length)));
|
|
46
|
+
offset = start + length;
|
|
47
|
+
}
|
|
48
|
+
return tokens;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* A ledger-entry resolver over the blob of the commit frame being applied. The MLS port calls
|
|
52
|
+
* it with the ids the Commit's control envelope names; it answers with the bodies sealed into
|
|
53
|
+
* that same frame — making body delivery atomic with the commit, so first delivery cannot
|
|
54
|
+
* strand.
|
|
55
|
+
*
|
|
56
|
+
* Answers with every token in the blob, not just the named subset: tokens are
|
|
57
|
+
* content-addressed, so the caller binds each body to its id by digesting, and an unrequested
|
|
58
|
+
* body is ignored. A responder can fail to answer, never inject.
|
|
59
|
+
*
|
|
60
|
+
* A blob this peer cannot open yields NO entries, not an error: the commit fails to resolve,
|
|
61
|
+
* the port raises missing-entries, and the lane leaves the cursor put so the frame is re-read
|
|
62
|
+
* — never reported as corruption. In practice this cannot arise on the apply path: the port
|
|
63
|
+
* asks only for a commit it is applying, framed at this peer's epoch, the epoch the blob is
|
|
64
|
+
* sealed under.
|
|
65
|
+
*/ export function createLedgerEntryResolver(sealedEntries, unwrap) {
|
|
66
|
+
return async (_ids)=>{
|
|
67
|
+
try {
|
|
68
|
+
const opened = await unwrap(sealedEntries);
|
|
69
|
+
const payload = opened instanceof Uint8Array ? opened : opened.payload;
|
|
70
|
+
// Body trust comes from the token's own signature + content-address, which the caller
|
|
71
|
+
// re-verifies — not from the seal, which only buys confidentiality from the hub. The
|
|
72
|
+
// recovered sender is deliberately unused.
|
|
73
|
+
return decodeLedgerEntries(payload);
|
|
74
|
+
} catch {
|
|
75
|
+
return [];
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
package/lib/peer.d.ts
CHANGED
|
@@ -2,13 +2,35 @@ import type { Client } from '@enkaku/client';
|
|
|
2
2
|
import type { ProtocolDefinition } from '@enkaku/protocol';
|
|
3
3
|
import type { ProcedureHandlers } from '@enkaku/server';
|
|
4
4
|
import { type GatheredReply, type GatherOptions, type RequestOptions, type SuppressConfig } from '@kumiai/broadcast';
|
|
5
|
-
import type {
|
|
6
|
-
import type
|
|
5
|
+
import type { LogHub } from '@kumiai/hub-tunnel';
|
|
6
|
+
import { type CommitJournal, type LaneResult, type PendingCommit } from './commit.js';
|
|
7
|
+
import { type GroupCrypto, type GroupMLS } from './crypto.js';
|
|
8
|
+
/**
|
|
9
|
+
* The MLS half of a peer: the lifecycle port, the durable journal that carries a pending commit
|
|
10
|
+
* across a crash, and the host hook that adopts one after a restart. They arrive together or not
|
|
11
|
+
* at all — a peer with a port and no journal would silently lose every commit whose process died
|
|
12
|
+
* in the acceptance window, and the type is what stops a host wiring that.
|
|
13
|
+
*/
|
|
14
|
+
export type GroupPeerMLSParams = {
|
|
15
|
+
/** MLS lifecycle port. When provided, the peer runs the commit lane. */
|
|
16
|
+
mls: GroupMLS;
|
|
17
|
+
/** Durable single-slot journal. Written before every publish, cleared on both outcomes. */
|
|
18
|
+
journal: CommitJournal;
|
|
19
|
+
/**
|
|
20
|
+
* Adopt a journalled commit now confirmed accepted — the restart half of
|
|
21
|
+
* {@link PendingCommit.onAccepted}, over the same opaque blob: deserialize the post-commit
|
|
22
|
+
* handle, adopt it, deliver any Welcome it carried.
|
|
23
|
+
*
|
|
24
|
+
* MUST be idempotent, as `onAccepted` must: the peer cannot tell an entry whose `onAccepted`
|
|
25
|
+
* already ran from one whose process died before it. The Welcome goes out again, at-least-once
|
|
26
|
+
* and by design — see {@link PendingCommit.onAccepted} for why the sender must not suppress the
|
|
27
|
+
* repeat, and what absorbs it.
|
|
28
|
+
*/
|
|
29
|
+
adoptJournalled: (journal: Uint8Array) => Promise<void>;
|
|
30
|
+
};
|
|
7
31
|
export type GroupPeerParams<Protocols extends Record<string, ProtocolDefinition>> = {
|
|
8
|
-
hub:
|
|
32
|
+
hub: LogHub;
|
|
9
33
|
crypto: GroupCrypto;
|
|
10
|
-
/** MLS lifecycle port. When provided, the peer runs the handshake lane. */
|
|
11
|
-
mls?: GroupMLS;
|
|
12
34
|
localDID: string;
|
|
13
35
|
protocols: Protocols;
|
|
14
36
|
handlers: {
|
|
@@ -16,12 +38,32 @@ export type GroupPeerParams<Protocols extends Record<string, ProtocolDefinition>
|
|
|
16
38
|
};
|
|
17
39
|
suppress?: SuppressConfig;
|
|
18
40
|
getRandomID?: () => string;
|
|
19
|
-
/**
|
|
41
|
+
/**
|
|
42
|
+
* Recovery rendezvous tuning. `timeoutMs`: how long one request waits for a reply. `getDelayMs`:
|
|
43
|
+
* responder reply jitter. `deadlineMs`: how long `recover()` keeps re-requesting and rebuilding
|
|
44
|
+
* before giving up and leaving the peer degraded.
|
|
45
|
+
*/
|
|
20
46
|
recovery?: {
|
|
21
47
|
timeoutMs?: number;
|
|
22
48
|
getDelayMs?: () => number;
|
|
49
|
+
deadlineMs?: number;
|
|
23
50
|
};
|
|
24
|
-
|
|
51
|
+
/**
|
|
52
|
+
* Commit-log retention the hub is asked to hold, in seconds. Default 30 days. A liveness dial:
|
|
53
|
+
* within it a returning member converges by pulling the log; beyond it, another live member
|
|
54
|
+
* must heal it.
|
|
55
|
+
*/
|
|
56
|
+
commitLogRetentionSeconds?: number;
|
|
57
|
+
/**
|
|
58
|
+
* How long `commit` rebases before giving up, in ms. Default 30s. Losing a compare-and-set is
|
|
59
|
+
* the expected path, not an error path.
|
|
60
|
+
*/
|
|
61
|
+
commitDeadlineMs?: number;
|
|
62
|
+
} & (GroupPeerMLSParams | {
|
|
63
|
+
mls?: undefined;
|
|
64
|
+
journal?: undefined;
|
|
65
|
+
adoptJournalled?: undefined;
|
|
66
|
+
});
|
|
25
67
|
export type ProtocolSurface<Protocol extends ProtocolDefinition> = {
|
|
26
68
|
dispatch: (prc: string, data?: Record<string, unknown>) => Promise<void>;
|
|
27
69
|
request: (prc: string, prm?: unknown, options?: RequestOptions) => Promise<unknown>;
|
|
@@ -31,19 +73,67 @@ export type ProtocolSurface<Protocol extends ProtocolDefinition> = {
|
|
|
31
73
|
export type GroupPeer<Protocols extends Record<string, ProtocolDefinition>> = {
|
|
32
74
|
protocol: <K extends keyof Protocols>(name: K) => ProtocolSurface<Protocols[K]>;
|
|
33
75
|
/**
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
76
|
+
* Commit to the group, rebasing until it lands.
|
|
77
|
+
*
|
|
78
|
+
* The peer replays its journal, pulls the log to the end, calls `build()`, journals the result,
|
|
79
|
+
* and publishes conditionally on the head it pulled to. Win: `onAccepted` runs, slot clears.
|
|
80
|
+
* Lose (someone committed first): drop the pending commit untouched and call `build()` again
|
|
81
|
+
* against the now-current handle — the expected path, not an error path.
|
|
82
|
+
*
|
|
83
|
+
* `build()` produces a commit against the host's CURRENT handle and does not adopt it. Called
|
|
84
|
+
* once per attempt; it MUST read the host's live handle each time and have no side effects until
|
|
85
|
+
* `onAccepted` runs — a losing attempt is discarded whole.
|
|
86
|
+
*
|
|
87
|
+
* Holds the commit mutex for its whole run. The compare-and-set resolves races between devices,
|
|
88
|
+
* not two callers on one: two `build()` calls against a single handle would both frame at that
|
|
89
|
+
* handle's epoch and diverge.
|
|
90
|
+
*
|
|
91
|
+
* A RESULT means it landed and `onAccepted` ran; a THROW means it did not — stranded or ledger
|
|
92
|
+
* incomplete ({@link "commit".RecoveryRequiredError}), or lost the compare-and-set past its
|
|
93
|
+
* deadline ({@link "commit".CommitDeadlineError}). A throw publishes and advances nothing but
|
|
94
|
+
* may leave earlier `lost` / `reenact` work undrained (only the success path returns it) — call
|
|
95
|
+
* {@link replay} after a throw to collect it.
|
|
96
|
+
*/
|
|
97
|
+
commit: (build: () => Promise<PendingCommit>) => Promise<LaneResult>;
|
|
98
|
+
/**
|
|
99
|
+
* Replay the journal on its own, for startup: republish any pending commit under its original
|
|
100
|
+
* idempotency key and hand back what did not survive. Every lane operation replays first, so
|
|
101
|
+
* this is not the only way a loss surfaces — but it is the one a host can call before anything
|
|
102
|
+
* else, and the collector to call after a `commit()` that threw.
|
|
103
|
+
*
|
|
104
|
+
* Builds and publishes nothing, so unlike `commit()` an incomplete ledger is no hazard: it
|
|
105
|
+
* re-attempts the bootstrap like any lane operation and returns what it holds WITHOUT throwing,
|
|
106
|
+
* leaving the peer degraded until a responder answers. A `{}` result means "no orphaned work to
|
|
107
|
+
* re-issue", never "the peer is whole" — the completeness gate that a reset roster must not
|
|
108
|
+
* commit lives on `commit()`, and `replay()` is the retry that eventually clears it.
|
|
37
109
|
*/
|
|
38
|
-
|
|
110
|
+
replay: () => Promise<LaneResult>;
|
|
39
111
|
/**
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
112
|
+
* Heal a peer the group has left behind: rejoin by external commit, refold the ledger, hand
|
|
113
|
+
* back the entries the group's ledger does not already hold.
|
|
114
|
+
*
|
|
115
|
+
* A TOP-LEVEL lane operation with a compare-and-set loop of its own — takes the commit mutex
|
|
116
|
+
* itself, never called from inside another lane operation. The external commit changes the
|
|
117
|
+
* ratchet tree, so it races at the head like any commit; losing (the likely outcome) DISCARDS
|
|
118
|
+
* THE GROUPINFO as well as the commit built from it — the GroupInfo describes a tree the winner
|
|
119
|
+
* already changed, so rebuilding from it would publish a commit no member can apply.
|
|
120
|
+
*
|
|
121
|
+
* A heal is TWO commits: the rejoin carries no entries (a GroupInfo has nowhere to put them), so
|
|
122
|
+
* the entries this peer still owes ride an ordinary `commit()` the CALLER makes after this
|
|
123
|
+
* releases the lane. That is `reenact`, filtered by MEMBERSHIP and never by which failure
|
|
124
|
+
* brought the peer here: re-enact an entry iff the group's authenticated ledger does not already
|
|
125
|
+
* contain it. The ledger does not dedup — a re-appended entry WINS the fold — so re-enacting one
|
|
126
|
+
* the group already holds silently reverts whatever a later admin wrote over it.
|
|
127
|
+
*
|
|
128
|
+
* `{ advanced: false }` when no member answers the rendezvous, and when the rejoin landed but
|
|
129
|
+
* the ledger could not be bootstrapped: an incomplete ledger is a reset roster, and reporting it
|
|
130
|
+
* healed would hand the host a group with every role gone. It stays degraded and retries.
|
|
131
|
+
*
|
|
132
|
+
* A peer merely BEHIND never needs this — it pulls the log and catches up.
|
|
44
133
|
*/
|
|
45
134
|
recover: () => Promise<{
|
|
46
135
|
advanced: boolean;
|
|
136
|
+
reenact: Array<string>;
|
|
47
137
|
}>;
|
|
48
138
|
resync: () => Promise<void>;
|
|
49
139
|
dispose: () => Promise<void>;
|