@cero-base/core 1.18.2 → 2.0.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/README.md +5 -6
- package/package.json +14 -11
- package/src/blobs/index.js +17 -17
- package/src/blobs/server.js +0 -2
- package/src/database/changes.js +34 -60
- package/src/database/dispatch.js +156 -245
- package/src/database/encryption.js +22 -52
- package/src/database/envelope.js +1 -3
- package/src/database/index.js +514 -513
- package/src/database/rotation.js +191 -193
- package/src/identity/index.js +8 -12
- package/src/index.js +5 -9
- package/src/lib/constants.js +3 -12
- package/src/lib/errors.js +6 -10
- package/src/lib/schema.js +2 -5
- package/src/lib/utils.js +55 -38
- package/src/network/index.js +138 -166
- package/src/pairing/candidate.js +123 -0
- package/src/pairing/index.js +36 -267
- package/src/pairing/invite.js +3 -4
- package/src/pairing/request.js +96 -0
- package/src/rpc/index.js +4 -14
- package/src/rpc/peer.js +3 -7
- package/src/storage/index.js +36 -49
- package/types/blobs/codec.d.ts +8 -8
- package/types/blobs/index.d.ts +38 -37
- package/types/blobs/server.d.ts +8 -10
- package/types/database/changes.d.ts +3 -8
- package/types/database/dispatch.d.ts +28 -25
- package/types/database/encryption.d.ts +22 -30
- package/types/database/envelope.d.ts +2 -2
- package/types/database/index.d.ts +196 -221
- package/types/database/rotation.d.ts +47 -30
- package/types/identity/index.d.ts +83 -87
- package/types/index.d.ts +42 -55
- package/types/lib/constants.d.ts +34 -38
- package/types/lib/errors.d.ts +16 -20
- package/types/lib/schema.d.ts +49 -42
- package/types/lib/spec/index.d.ts +29 -55
- package/types/lib/utils.d.ts +26 -20
- package/types/network/discovery.d.ts +7 -7
- package/types/network/index.d.ts +64 -72
- package/types/pairing/candidate.d.ts +36 -0
- package/types/pairing/index.d.ts +91 -151
- package/types/pairing/invite.d.ts +91 -91
- package/types/pairing/request.d.ts +133 -0
- package/types/rpc/client.d.ts +2 -2
- package/types/rpc/index.d.ts +30 -33
- package/types/rpc/peer.d.ts +11 -11
- package/types/rpc/server.d.ts +2 -2
- package/types/storage/index.d.ts +70 -71
- package/src/database/bootstrap.js +0 -133
- package/src/database/hooks.js +0 -112
- package/src/database/membership.js +0 -67
- package/types/database/bootstrap.d.ts +0 -20
- package/types/database/hooks.d.ts +0 -8
- package/types/database/membership.d.ts +0 -7
package/src/database/dispatch.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import b4a from 'b4a'
|
|
2
|
-
import
|
|
2
|
+
import hid from 'hypercore-id-encoding'
|
|
3
3
|
|
|
4
4
|
import {
|
|
5
5
|
SINGLE,
|
|
@@ -12,29 +12,11 @@ import {
|
|
|
12
12
|
ASSIGN,
|
|
13
13
|
WRITE
|
|
14
14
|
} from '../lib/constants.js'
|
|
15
|
-
import {
|
|
16
|
-
toId,
|
|
17
|
-
toKey,
|
|
18
|
-
can,
|
|
19
|
-
grants,
|
|
20
|
-
outranks,
|
|
21
|
-
addWriterPayload,
|
|
22
|
-
claimWriterPayload
|
|
23
|
-
} from '../lib/utils.js'
|
|
15
|
+
import { can, grants, outranks, admission, ownership } from '../lib/utils.js'
|
|
24
16
|
import { CeroError } from '../lib/errors.js'
|
|
25
17
|
import { Identity } from '../identity/index.js'
|
|
26
18
|
|
|
27
|
-
//
|
|
28
|
-
export const BUILTINS = [
|
|
29
|
-
{ name: 'members', verb: 'member' },
|
|
30
|
-
{ name: 'devices', verb: 'device' },
|
|
31
|
-
{ name: 'invites', verb: 'invite' },
|
|
32
|
-
{ name: 'handles', verb: 'handle' },
|
|
33
|
-
{ name: 'files', verb: 'file' }
|
|
34
|
-
]
|
|
35
|
-
|
|
36
|
-
// Remote op fields are variable-length buffers on the wire — reject wrong sizes
|
|
37
|
-
// before they reach sodium/toId, which throw instead of returning false.
|
|
19
|
+
// wire fields are variable-length: reject wrong sizes before sodium and hid throw on them
|
|
38
20
|
const isKey = (b) => b?.byteLength === 32
|
|
39
21
|
const isSig = (b) => b?.byteLength === 64
|
|
40
22
|
|
|
@@ -42,39 +24,33 @@ const isSig = (b) => b?.byteLength === 64
|
|
|
42
24
|
* Build the hyperdispatch router for a spec: wires membership, builtin, and
|
|
43
25
|
* spec-defined collection/action ops, returning the router plus an apply loop.
|
|
44
26
|
*
|
|
45
|
-
* @param {
|
|
46
|
-
* @param {
|
|
47
|
-
* @param {
|
|
48
|
-
* @param {
|
|
49
|
-
* @param {() =>
|
|
50
|
-
* @param {
|
|
51
|
-
* @
|
|
27
|
+
* @param {object} opts
|
|
28
|
+
* @param {{ dispatch: { Router: Function }, meta?: { refs?: Record<string, { kind?: string, builtin?: boolean, verb?: string }> } }} opts.spec Generated hyperdispatch spec.
|
|
29
|
+
* @param {string} opts.ns Namespace prefix for collection and op names.
|
|
30
|
+
* @param {Record<string, Function>} opts.routes Custom action handlers keyed by route name.
|
|
31
|
+
* @param {(err: Error) => void} opts.onerror Called when a malformed node is skipped.
|
|
32
|
+
* @param {() => Uint8Array | null} opts.key The database key, null until the bee booted.
|
|
33
|
+
* @param {(row: { epoch: number, stamp: number, wrapped: Uint8Array, commit: Uint8Array }) => Promise<void>} opts.onepoch Per-peer side of an applied rotation (skipped in dry runs).
|
|
34
|
+
* @returns {{ dispatch: (value: Buffer, ctx: object) => Promise<void>, apply: (nodes: Array<{ value: Buffer, key: Buffer }>, view: object, host: object) => Promise<void> }}
|
|
52
35
|
*/
|
|
53
|
-
export function makeDispatcher(
|
|
54
|
-
spec
|
|
55
|
-
ns,
|
|
56
|
-
routes,
|
|
57
|
-
onerror = safetyCatch,
|
|
58
|
-
getDbKey = () => null,
|
|
59
|
-
hooks = {}
|
|
60
|
-
) {
|
|
61
|
-
const dispatcher = new spec.dispatch.Router()
|
|
36
|
+
export function makeDispatcher({ spec, ns, routes, onerror, key, onepoch }) {
|
|
37
|
+
const router = new spec.dispatch.Router()
|
|
62
38
|
|
|
63
39
|
const countersCol = `@${ns}/${COUNTERS}`
|
|
64
40
|
const getMember = (view, id) => view.get(`@${ns}/members`, { id })
|
|
65
41
|
const getDevice = (view, id) => view.get(`@${ns}/devices`, { id })
|
|
66
42
|
|
|
67
43
|
const getRole = async (view, identityKey) =>
|
|
68
|
-
identityKey ? ((await getMember(view,
|
|
44
|
+
identityKey ? ((await getMember(view, hid.encode(identityKey)))?.role ?? null) : null
|
|
69
45
|
|
|
70
46
|
const getSignerRole = async (view, writerKey) => {
|
|
71
47
|
if (!writerKey) return null
|
|
72
|
-
const d = await getDevice(view,
|
|
48
|
+
const d = await getDevice(view, hid.encode(writerKey))
|
|
73
49
|
return d?.memberId ? ((await getMember(view, d.memberId))?.role ?? null) : null
|
|
74
50
|
}
|
|
75
51
|
const isGenesis = async (view) => !(await view.findOne(`@${ns}/members`, {}))
|
|
76
52
|
const getSignerMember = async (view, key) =>
|
|
77
|
-
(key ? (await getDevice(view,
|
|
53
|
+
(key ? (await getDevice(view, hid.encode(key)))?.memberId : null) ?? null
|
|
78
54
|
const canRemoveMember = async (view, signerKey, targetMemberId) => {
|
|
79
55
|
const r = await getSignerRole(view, signerKey)
|
|
80
56
|
if (!can(r, REMOVE)) return false
|
|
@@ -96,22 +72,14 @@ export function makeDispatcher(
|
|
|
96
72
|
await view.insert(col, op)
|
|
97
73
|
}
|
|
98
74
|
|
|
99
|
-
//
|
|
100
|
-
//
|
|
101
|
-
// otherwise keep writing every collection.
|
|
102
|
-
//
|
|
103
|
-
// A signer we cannot resolve to a role is allowed through: admission is
|
|
104
|
-
// optimistic, so a recovering device's first ops can apply before its own
|
|
105
|
-
// add-writer has been adopted in this view, and refusing those would drop
|
|
106
|
-
// them permanently. Being an admitted writer is the gate in that window.
|
|
75
|
+
// a demoted member keeps its writer seat, so rows are gated by role. An unresolved signer
|
|
76
|
+
// passes: optimistic admission applies a recovering device's ops before its add-writer
|
|
107
77
|
const requireWrite = async (ctx) => {
|
|
108
78
|
const r = await getSignerRole(ctx.view, ctx.key)
|
|
109
79
|
if (r !== null && !can(r, WRITE)) throw CeroError.REFUSED('write')
|
|
110
80
|
}
|
|
111
81
|
|
|
112
|
-
//
|
|
113
|
-
// an `own` collection just enforces it: the author may change their own rows,
|
|
114
|
-
// and anyone who can REMOVE may moderate the rest.
|
|
82
|
+
// own: the author may change their rows, anyone with REMOVE may moderate the rest
|
|
115
83
|
const requireOwn = async (ctx, existing) => {
|
|
116
84
|
if (existing?.memberId == null) return
|
|
117
85
|
if (existing.memberId === (await getSignerMember(ctx.view, ctx.key))) return
|
|
@@ -120,11 +88,10 @@ export function makeDispatcher(
|
|
|
120
88
|
|
|
121
89
|
const upsert = (name, col, kind, own) => async (op, ctx) => {
|
|
122
90
|
await requireWrite(ctx)
|
|
123
|
-
const d = ctx.key && (await getDevice(ctx.view,
|
|
91
|
+
const d = ctx.key && (await getDevice(ctx.view, hid.encode(ctx.key)))
|
|
124
92
|
op.memberId = d?.memberId || null
|
|
125
93
|
if (kind !== COLLECTION) return ctx.view.insert(col, op)
|
|
126
|
-
//
|
|
127
|
-
// same ownership rule as `set`
|
|
94
|
+
// add on an existing id is an overwrite, same ownership rule as set
|
|
128
95
|
if (own) await requireOwn(ctx, await ctx.view.get(col, { id: op.id }))
|
|
129
96
|
await insert(ctx.view, name, col, op)
|
|
130
97
|
}
|
|
@@ -133,9 +100,8 @@ export function makeDispatcher(
|
|
|
133
100
|
const existing = await ctx.view.get(col, { id: op.id })
|
|
134
101
|
if (!existing) return
|
|
135
102
|
if (own) await requireOwn(ctx, existing)
|
|
136
|
-
// memberId is attribution
|
|
137
|
-
|
|
138
|
-
const d = ctx.key && (await getDevice(ctx.view, toId(ctx.key)))
|
|
103
|
+
// memberId is attribution: derived here, never merged off the wire
|
|
104
|
+
const d = ctx.key && (await getDevice(ctx.view, hid.encode(ctx.key)))
|
|
139
105
|
await insert(ctx.view, name, col, { ...existing, ...op, memberId: d?.memberId || null })
|
|
140
106
|
}
|
|
141
107
|
const remove = (col, own) => async (op, ctx) => {
|
|
@@ -143,32 +109,25 @@ export function makeDispatcher(
|
|
|
143
109
|
if (own) await requireOwn(ctx, await ctx.view.get(col, { id: op.id }))
|
|
144
110
|
return ctx.view.delete(col, op)
|
|
145
111
|
}
|
|
146
|
-
//
|
|
147
|
-
// the view alone, so every peer reaches it. Decode failures and unknown routes
|
|
148
|
-
// must not — an older peer would drop a batch its newer neighbour applies.
|
|
112
|
+
// only REFUSED aborts a batch: every peer derives it. Decode failures and unknown routes skip one node
|
|
149
113
|
const isRefusal = (err) => err?.isCeroError === true && err.code === 'REFUSED'
|
|
150
114
|
|
|
151
|
-
const add = (verb, fn) =>
|
|
115
|
+
const add = (verb, fn) => router.add(`@${ns}/${verb}`, fn)
|
|
152
116
|
|
|
153
117
|
add('add-writer', async (op, ctx) => {
|
|
154
118
|
if (!isKey(op.master) || !isKey(op.writer) || !isSig(op.sig)) return
|
|
155
|
-
if (!Identity.verify(op.master,
|
|
119
|
+
if (!Identity.verify(op.master, admission(ctx.dbKey, op.writer, ctx.key), op.sig)) return
|
|
156
120
|
const genesis = await isGenesis(ctx.view)
|
|
157
121
|
const inviter = await getRole(ctx.view, op.master)
|
|
158
122
|
if (!genesis && !can(inviter, INVITE)) throw CeroError.REFUSED('invite')
|
|
159
|
-
// the rank is add-member's decision
|
|
160
|
-
//
|
|
161
|
-
// always an indexer, like claim-writer below. The op's optional isIndexer
|
|
162
|
-
// bool decodes to false when absent (nothing sets it), which silently made
|
|
163
|
-
// every device a weight-1 writer — autobee 2.0 gc's caught-up non-indexer
|
|
164
|
-
// sessions, and a gc'd writer misses later appends until a wakeup revives it
|
|
123
|
+
// the rank is add-member's decision in the same transaction: a refused grant discards this admission.
|
|
124
|
+
// Always an indexer: autobee gc's caught-up non-indexer sessions and they miss later appends
|
|
165
125
|
await ctx.host.addWriter(op.writer, { isIndexer: true })
|
|
166
126
|
const ts = op.ts || 0
|
|
167
127
|
await insert(ctx.view, 'devices', `@${ns}/devices`, {
|
|
168
|
-
id:
|
|
169
|
-
//
|
|
170
|
-
|
|
171
|
-
memberId: op.memberId || toId(op.master),
|
|
128
|
+
id: hid.encode(op.writer),
|
|
129
|
+
// a member row that never lands must resolve to nobody, not to the inviter
|
|
130
|
+
memberId: op.memberId || hid.encode(op.master),
|
|
172
131
|
createdAt: ts,
|
|
173
132
|
updatedAt: ts
|
|
174
133
|
})
|
|
@@ -176,28 +135,27 @@ export function makeDispatcher(
|
|
|
176
135
|
|
|
177
136
|
add('del-writer', async (op, ctx) => {
|
|
178
137
|
if (!isKey(op.writer)) return
|
|
179
|
-
const target = await getDevice(ctx.view,
|
|
138
|
+
const target = await getDevice(ctx.view, hid.encode(op.writer))
|
|
180
139
|
if (target?.memberId !== (await getSignerMember(ctx.view, ctx.key))) {
|
|
181
140
|
if (!(await canRemoveMember(ctx.view, ctx.key, target?.memberId))) {
|
|
182
141
|
throw CeroError.REFUSED('remove')
|
|
183
142
|
}
|
|
184
143
|
}
|
|
185
144
|
await ctx.host.removeWriter(op.writer)
|
|
186
|
-
await ctx.view.delete(`@${ns}/devices`, { id:
|
|
145
|
+
await ctx.view.delete(`@${ns}/devices`, { id: hid.encode(op.writer) })
|
|
187
146
|
})
|
|
188
147
|
|
|
189
148
|
add('claim-writer', async (op, ctx) => {
|
|
190
149
|
if (!isKey(op.identity) || !isKey(op.writer) || !isSig(op.sig)) return
|
|
191
|
-
if (!Identity.verify(op.identity,
|
|
192
|
-
// a claim is
|
|
193
|
-
// inapplicable, not refused, so it must not abort that writer's batch
|
|
150
|
+
if (!Identity.verify(op.identity, ownership(ctx.dbKey, op.writer), op.sig)) return
|
|
151
|
+
// from any other core a claim is inapplicable, not refused: it must not abort that writer's batch
|
|
194
152
|
if (!b4a.equals(ctx.key, op.writer)) return
|
|
195
|
-
const memberId =
|
|
153
|
+
const memberId = hid.encode(op.identity)
|
|
196
154
|
if (!can(await getRole(ctx.view, op.identity), WRITE)) throw CeroError.REFUSED('write')
|
|
197
155
|
await ctx.host.addWriter(op.writer, { isIndexer: true })
|
|
198
156
|
const ts = op.ts || 0
|
|
199
157
|
await insert(ctx.view, 'devices', `@${ns}/devices`, {
|
|
200
|
-
id:
|
|
158
|
+
id: hid.encode(op.writer),
|
|
201
159
|
memberId,
|
|
202
160
|
createdAt: ts,
|
|
203
161
|
updatedAt: ts
|
|
@@ -211,7 +169,7 @@ export function makeDispatcher(
|
|
|
211
169
|
if (!can(r, INVITE) || !grants(r, op.role)) throw CeroError.REFUSED('invite')
|
|
212
170
|
}
|
|
213
171
|
await insert(ctx.view, 'members', `@${ns}/members`, op)
|
|
214
|
-
const deviceId =
|
|
172
|
+
const deviceId = hid.encode(op.key)
|
|
215
173
|
const existingDevice = await getDevice(ctx.view, deviceId)
|
|
216
174
|
const ts = op.updatedAt || 0
|
|
217
175
|
await insert(ctx.view, 'devices', `@${ns}/devices`, {
|
|
@@ -227,12 +185,8 @@ export function makeDispatcher(
|
|
|
227
185
|
add('set-member', async (op, ctx) => {
|
|
228
186
|
const existing = await getMember(ctx.view, op.id)
|
|
229
187
|
if (!existing) return
|
|
230
|
-
//
|
|
231
|
-
//
|
|
232
|
-
// whatever else an app declares), so a name list would silently drop them.
|
|
233
|
-
// Your own profile is yours to write; another member's row is untouchable
|
|
234
|
-
// apart from an authorised role change. Identity and provenance never come
|
|
235
|
-
// off the wire, so nobody overwrites the key a member was admitted with.
|
|
188
|
+
// whose row it is decides what may change: your own is writable, another member's only
|
|
189
|
+
// for an authorised role change. Key and provenance never come off the wire
|
|
236
190
|
const isSelf = op.id === (await getSignerMember(ctx.view, ctx.key))
|
|
237
191
|
const next = isSelf ? { ...op } : { ...existing }
|
|
238
192
|
next.key = existing.key
|
|
@@ -248,18 +202,14 @@ export function makeDispatcher(
|
|
|
248
202
|
next.role = op.role
|
|
249
203
|
}
|
|
250
204
|
await insert(ctx.view, 'members', `@${ns}/members`, next)
|
|
251
|
-
// a demotion that takes WRITE away
|
|
252
|
-
// otherwise the member keeps appending, and only the apply-time WRITE
|
|
253
|
-
// gate stands between them and every collection
|
|
205
|
+
// a demotion that takes WRITE away takes the writer seat with it
|
|
254
206
|
if (can(existing.role, WRITE) && !can(next.role, WRITE)) {
|
|
255
207
|
if (next.key) await ctx.host.removeWriter(next.key)
|
|
256
208
|
for (const device of await ctx.view.find(`@${ns}/devices`, {}).toArray()) {
|
|
257
209
|
if (device.memberId !== next.id) continue
|
|
258
|
-
await ctx.host.removeWriter(
|
|
210
|
+
await ctx.host.removeWriter(hid.decode(device.id))
|
|
259
211
|
}
|
|
260
|
-
//
|
|
261
|
-
// deleting them would leave the demoted writer unresolvable — which the
|
|
262
|
-
// apply-time gate reads as "not yet enrolled" and lets through
|
|
212
|
+
// device rows stay: an unresolvable writer would read as not yet enrolled and pass the gate
|
|
263
213
|
}
|
|
264
214
|
})
|
|
265
215
|
|
|
@@ -271,64 +221,50 @@ export function makeDispatcher(
|
|
|
271
221
|
if (!can(r, REMOVE) || !outranks(r, existing.role)) throw CeroError.REFUSED('remove')
|
|
272
222
|
}
|
|
273
223
|
if (existing.key) await ctx.host.removeWriter(existing.key)
|
|
274
|
-
// every device of the member goes with them
|
|
275
|
-
// leave writers admitted or stale device rows behind
|
|
224
|
+
// every device of the member goes with them
|
|
276
225
|
for (const device of await ctx.view.find(`@${ns}/devices`, {}).toArray()) {
|
|
277
226
|
if (device.memberId !== op.id) continue
|
|
278
|
-
await ctx.host.removeWriter(
|
|
227
|
+
await ctx.host.removeWriter(hid.decode(device.id))
|
|
279
228
|
await ctx.view.delete(`@${ns}/devices`, { id: device.id })
|
|
280
229
|
}
|
|
281
230
|
await ctx.view.delete(`@${ns}/members`, op)
|
|
282
231
|
})
|
|
283
232
|
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
commit: op.commit
|
|
317
|
-
}
|
|
318
|
-
await ctx.view.insert(`@${ns}/${EPOCHS}`, row)
|
|
319
|
-
// per-peer hydration (unseal own envelope → keyring) — never in a dry
|
|
320
|
-
// run: the announcement itself must be appended under the OLD epoch
|
|
321
|
-
if (hooks.onEpoch && !ctx.dryRun) {
|
|
322
|
-
try {
|
|
323
|
-
await hooks.onEpoch(row)
|
|
324
|
-
} catch (err) {
|
|
325
|
-
onerror(err)
|
|
326
|
-
}
|
|
233
|
+
add('rotate-key', async (op, ctx) => {
|
|
234
|
+
if (!op.wrapped?.byteLength) return
|
|
235
|
+
// the commitment lets every member verify the sealed secret, so a rotator cannot split the room
|
|
236
|
+
if (op.commit?.byteLength !== 32) return
|
|
237
|
+
// the stamp is picked before any block is written, so epochs reorder freely; uniqueness is enforced here
|
|
238
|
+
if (!Number.isInteger(op.stamp) || op.stamp <= 0 || op.stamp > 0xffffffff) return
|
|
239
|
+
if (!can(await getSignerRole(ctx.view, ctx.key), REMOVE)) {
|
|
240
|
+
throw CeroError.REFUSED('rotate', 'rotate requires the remove permission')
|
|
241
|
+
}
|
|
242
|
+
const rows = await ctx.view.find(`@${ns}/${EPOCHS}`, {}).toArray()
|
|
243
|
+
if (rows.some((r) => r.stamp === op.stamp)) return
|
|
244
|
+
// sequence numbers order epochs; concurrent rotations get consecutive sequences, both readable
|
|
245
|
+
const counter = (await ctx.view.get(countersCol, { name: EPOCHS })) ?? {
|
|
246
|
+
name: EPOCHS,
|
|
247
|
+
value: 0
|
|
248
|
+
}
|
|
249
|
+
const epoch = counter.value + 1
|
|
250
|
+
await ctx.view.insert(countersCol, { name: EPOCHS, value: epoch })
|
|
251
|
+
const row = {
|
|
252
|
+
epoch,
|
|
253
|
+
stamp: op.stamp,
|
|
254
|
+
wrapped: op.wrapped,
|
|
255
|
+
createdAt: op.createdAt || 0,
|
|
256
|
+
commit: op.commit
|
|
257
|
+
}
|
|
258
|
+
await ctx.view.insert(`@${ns}/${EPOCHS}`, row)
|
|
259
|
+
// never in a dry run: the announcement itself is appended under the old epoch
|
|
260
|
+
if (!ctx.dryRun) {
|
|
261
|
+
try {
|
|
262
|
+
await onepoch(row)
|
|
263
|
+
} catch (err) {
|
|
264
|
+
onerror(err)
|
|
327
265
|
}
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
// pre-rotation spec
|
|
331
|
-
}
|
|
266
|
+
}
|
|
267
|
+
})
|
|
332
268
|
|
|
333
269
|
add('del-device', async (op, ctx) => {
|
|
334
270
|
const existing = await getDevice(ctx.view, op.id)
|
|
@@ -338,99 +274,84 @@ export function makeDispatcher(
|
|
|
338
274
|
throw CeroError.REFUSED('remove')
|
|
339
275
|
}
|
|
340
276
|
}
|
|
341
|
-
await ctx.host.removeWriter(
|
|
277
|
+
await ctx.host.removeWriter(hid.decode(existing.id))
|
|
342
278
|
await ctx.view.delete(`@${ns}/devices`, op)
|
|
343
279
|
})
|
|
344
280
|
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
// breaks the invite for everyone), and altering or revoking one is a
|
|
378
|
-
// moderation act: it needs REMOVE.
|
|
379
|
-
add(`add-${b.verb}`, async (op, ctx) => {
|
|
380
|
-
await requireWrite(ctx)
|
|
381
|
-
if (await ctx.view.get(col, { id: op.id })) throw CeroError.REFUSED('invite')
|
|
382
|
-
await insert(ctx.view, b.name, col, op)
|
|
383
|
-
})
|
|
384
|
-
// like requireWrite, a signer with no resolvable role yet (genesis, or
|
|
385
|
-
// the optimistic-admission window) is not refused
|
|
386
|
-
const moderate = async (ctx) => {
|
|
387
|
-
const r = await getSignerRole(ctx.view, ctx.key)
|
|
388
|
-
if (r !== null && !can(r, REMOVE)) throw CeroError.REFUSED('invite')
|
|
389
|
-
}
|
|
390
|
-
add(`set-${b.verb}`, async (op, ctx) => {
|
|
391
|
-
await moderate(ctx)
|
|
392
|
-
const existing = await ctx.view.get(col, { id: op.id })
|
|
393
|
-
if (!existing) return
|
|
394
|
-
await insert(ctx.view, b.name, col, { ...existing, ...op })
|
|
395
|
-
})
|
|
396
|
-
// deleting is two different acts: revoking (moderation, REMOVE) and
|
|
397
|
-
// consuming a single-use invite — which is done by whichever replica
|
|
398
|
-
// served the join, so any member who may serve invites may do it
|
|
399
|
-
add(`del-${b.verb}`, async (op, ctx) => {
|
|
400
|
-
const existing = await ctx.view.get(col, { id: op.id })
|
|
401
|
-
const consume = existing && existing.reuse !== true
|
|
402
|
-
const r = await getSignerRole(ctx.view, ctx.key)
|
|
403
|
-
if (r !== null && !can(r, REMOVE) && !(consume && can(r, INVITE))) {
|
|
404
|
-
throw CeroError.REFUSED('invite')
|
|
405
|
-
}
|
|
406
|
-
return ctx.view.delete(col, op)
|
|
407
|
-
})
|
|
408
|
-
continue
|
|
409
|
-
}
|
|
410
|
-
if (b.verb === 'file') {
|
|
411
|
-
add('add-file', async (op, ctx) => {
|
|
412
|
-
if (!can(await getSignerRole(ctx.view, ctx.key), WRITE)) throw CeroError.REFUSED('write')
|
|
413
|
-
const memberId = await getSignerMember(ctx.view, ctx.key)
|
|
414
|
-
await insert(ctx.view, b.name, col, {
|
|
415
|
-
id: op.id,
|
|
416
|
-
name: op.name ?? null,
|
|
417
|
-
memberId,
|
|
418
|
-
stamp: op.stamp ?? 0
|
|
419
|
-
})
|
|
420
|
-
})
|
|
421
|
-
} else {
|
|
422
|
-
add(`add-${b.verb}`, upsert(b.name, col, COLLECTION))
|
|
423
|
-
}
|
|
424
|
-
add(`set-${b.verb}`, update(b.name, col))
|
|
425
|
-
add(`del-${b.verb}`, remove(col))
|
|
281
|
+
// a device row is the writer→role mapping, so memberId never comes off the wire: an unknown id
|
|
282
|
+
// binds to the signer's own member, or one unauthenticated op could inherit the owner's authority
|
|
283
|
+
const devices = `@${ns}/devices`
|
|
284
|
+
const bind = async (ctx, existing) =>
|
|
285
|
+
existing?.memberId ?? (await getSignerMember(ctx.view, ctx.key))
|
|
286
|
+
add('add-device', async (op, ctx) => {
|
|
287
|
+
const existing = await getDevice(ctx.view, op.id)
|
|
288
|
+
await insert(ctx.view, 'devices', devices, { ...op, memberId: await bind(ctx, existing) })
|
|
289
|
+
})
|
|
290
|
+
add('set-device', async (op, ctx) => {
|
|
291
|
+
const existing = await getDevice(ctx.view, op.id)
|
|
292
|
+
const ts = op.updatedAt || 0
|
|
293
|
+
await insert(ctx.view, 'devices', devices, {
|
|
294
|
+
...op,
|
|
295
|
+
memberId: await bind(ctx, existing),
|
|
296
|
+
createdAt: existing?.createdAt || op.createdAt || ts,
|
|
297
|
+
updatedAt: ts
|
|
298
|
+
})
|
|
299
|
+
})
|
|
300
|
+
|
|
301
|
+
// any WRITE member mints an invite, its rank capped at admission; an existing row is never
|
|
302
|
+
// overwritten, and altering or revoking one needs REMOVE
|
|
303
|
+
const invites = `@${ns}/invites`
|
|
304
|
+
add('add-invite', async (op, ctx) => {
|
|
305
|
+
await requireWrite(ctx)
|
|
306
|
+
if (await ctx.view.get(invites, { id: op.id })) throw CeroError.REFUSED('invite')
|
|
307
|
+
await insert(ctx.view, 'invites', invites, op)
|
|
308
|
+
})
|
|
309
|
+
// like requireWrite, a signer with no resolvable role yet is not refused
|
|
310
|
+
const moderate = async (ctx) => {
|
|
311
|
+
const r = await getSignerRole(ctx.view, ctx.key)
|
|
312
|
+
if (r !== null && !can(r, REMOVE)) throw CeroError.REFUSED('invite')
|
|
426
313
|
}
|
|
314
|
+
add('set-invite', async (op, ctx) => {
|
|
315
|
+
await moderate(ctx)
|
|
316
|
+
const existing = await ctx.view.get(invites, { id: op.id })
|
|
317
|
+
if (!existing) return
|
|
318
|
+
await insert(ctx.view, 'invites', invites, { ...existing, ...op })
|
|
319
|
+
})
|
|
320
|
+
// revoking needs REMOVE; consuming a single-use invite is done by whichever replica served the join
|
|
321
|
+
add('del-invite', async (op, ctx) => {
|
|
322
|
+
const existing = await ctx.view.get(invites, { id: op.id })
|
|
323
|
+
const consume = existing && existing.reuse !== true
|
|
324
|
+
const r = await getSignerRole(ctx.view, ctx.key)
|
|
325
|
+
if (r !== null && !can(r, REMOVE) && !(consume && can(r, INVITE))) {
|
|
326
|
+
throw CeroError.REFUSED('invite')
|
|
327
|
+
}
|
|
328
|
+
return ctx.view.delete(invites, op)
|
|
329
|
+
})
|
|
330
|
+
|
|
331
|
+
const files = `@${ns}/files`
|
|
332
|
+
add('add-file', async (op, ctx) => {
|
|
333
|
+
if (!can(await getSignerRole(ctx.view, ctx.key), WRITE)) throw CeroError.REFUSED('write')
|
|
334
|
+
const memberId = await getSignerMember(ctx.view, ctx.key)
|
|
335
|
+
await insert(ctx.view, 'files', files, {
|
|
336
|
+
id: op.id,
|
|
337
|
+
name: op.name ?? null,
|
|
338
|
+
memberId,
|
|
339
|
+
stamp: op.stamp ?? 0
|
|
340
|
+
})
|
|
341
|
+
})
|
|
342
|
+
add('set-file', update('files', files))
|
|
343
|
+
add('del-file', remove(files))
|
|
344
|
+
|
|
345
|
+
const handles = `@${ns}/handles`
|
|
346
|
+
add('add-handle', upsert('handles', handles, COLLECTION))
|
|
347
|
+
add('set-handle', update('handles', handles))
|
|
348
|
+
add('del-handle', remove(handles))
|
|
427
349
|
|
|
428
350
|
for (const [name, info] of Object.entries(spec.meta?.refs || {})) {
|
|
429
|
-
if (info.
|
|
351
|
+
if (info.internal) continue
|
|
430
352
|
if (info.kind === 'handle') continue
|
|
431
353
|
if (info.kind === ACTION) {
|
|
432
|
-
//
|
|
433
|
-
// surface it instead of silently diverging from peers that ran it
|
|
354
|
+
// an action with no local route diverges this peer from those that ran it: surface it
|
|
434
355
|
add(
|
|
435
356
|
name,
|
|
436
357
|
routes[name] ||
|
|
@@ -453,17 +374,12 @@ export function makeDispatcher(
|
|
|
453
374
|
}
|
|
454
375
|
|
|
455
376
|
return {
|
|
456
|
-
|
|
377
|
+
dispatch: (value, ctx) => router.dispatch(value, ctx),
|
|
457
378
|
async apply(nodes, view, host) {
|
|
458
|
-
const dbKey =
|
|
459
|
-
//
|
|
460
|
-
// whole batch — a batch is one writer's contiguous run (autobee keys it on
|
|
461
|
-
// batch[0].key), so an abort only ever drops the offending writer's ops.
|
|
379
|
+
const dbKey = key()
|
|
380
|
+
// a batch is one writer's contiguous run, so a refusal discards only that writer's ops
|
|
462
381
|
const tx = view.transaction()
|
|
463
|
-
//
|
|
464
|
-
// survive a discarded transaction and leave a writer with no device row,
|
|
465
|
-
// which the WRITE gate reads as "not yet enrolled". So host calls are
|
|
466
|
-
// recorded and replayed only once the batch is known to commit.
|
|
382
|
+
// host calls survive a discarded transaction, so they replay once the batch commits
|
|
467
383
|
const calls = []
|
|
468
384
|
const deferred = Object.create(host)
|
|
469
385
|
deferred.addWriter = (...args) => void calls.push(['addWriter', args])
|
|
@@ -471,13 +387,9 @@ export function makeDispatcher(
|
|
|
471
387
|
let refusal = null
|
|
472
388
|
for (const node of nodes) {
|
|
473
389
|
try {
|
|
474
|
-
await
|
|
390
|
+
await router.dispatch(node.value, { view: tx, host: deferred, key: node.key, dbKey })
|
|
475
391
|
} catch (err) {
|
|
476
|
-
//
|
|
477
|
-
// the same view, so discarding the batch keeps them convergent — and
|
|
478
|
-
// the batch is one writer's own run. Anything else (undecodable bytes,
|
|
479
|
-
// a route this version lacks) skips a single node instead: an older
|
|
480
|
-
// peer must not drop a batch its newer neighbour applies.
|
|
392
|
+
// anything but a refusal skips one node, so an older peer never drops a batch its newer neighbour applies
|
|
481
393
|
if (!isRefusal(err)) {
|
|
482
394
|
onerror(err)
|
|
483
395
|
continue
|
|
@@ -491,8 +403,7 @@ export function makeDispatcher(
|
|
|
491
403
|
onerror(refusal)
|
|
492
404
|
} else {
|
|
493
405
|
await tx.flush()
|
|
494
|
-
//
|
|
495
|
-
// after, and addWriter reads it (rc.31 derives coord/carrier from it)
|
|
406
|
+
// before apply() returns: autobee clears host.applying right after, and addWriter reads it
|
|
496
407
|
for (const [fn, args] of calls) await host[fn](...args)
|
|
497
408
|
}
|
|
498
409
|
await view.flush()
|