@ocap/statedb-sqlite 1.31.1 → 1.31.2

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.
Files changed (41) hide show
  1. package/esm/db.d.mts +11 -0
  2. package/esm/db.mjs +45 -9
  3. package/esm/index.d.mts +4 -1
  4. package/esm/index.mjs +4 -1
  5. package/esm/migrations/002-delegation-extensions.mjs +9 -2
  6. package/esm/migrations/004-rollup-reserves.d.mts +20 -0
  7. package/esm/migrations/004-rollup-reserves.mjs +39 -0
  8. package/esm/migrations/index.mjs +30 -15
  9. package/esm/table/account.d.mts +4 -3
  10. package/esm/table/account.mjs +31 -7
  11. package/esm/table/balance.mjs +31 -4
  12. package/esm/table/base.d.mts +8 -1
  13. package/esm/table/base.mjs +72 -28
  14. package/esm/table/rollup.d.mts +5 -2
  15. package/esm/table/rollup.mjs +38 -5
  16. package/esm/table/token.d.mts +9 -0
  17. package/esm/table/token.mjs +14 -0
  18. package/esm/tombstone-cleanup.mjs +1 -1
  19. package/esm/write-set.d.mts +66 -0
  20. package/esm/write-set.mjs +339 -0
  21. package/lib/db.cjs +45 -10
  22. package/lib/db.d.cts +11 -0
  23. package/lib/index.cjs +19 -0
  24. package/lib/index.d.cts +4 -1
  25. package/lib/migrations/002-delegation-extensions.cjs +9 -2
  26. package/lib/migrations/004-rollup-reserves.cjs +47 -0
  27. package/lib/migrations/004-rollup-reserves.d.cts +20 -0
  28. package/lib/migrations/index.cjs +30 -15
  29. package/lib/table/account.cjs +31 -7
  30. package/lib/table/account.d.cts +4 -3
  31. package/lib/table/balance.cjs +31 -4
  32. package/lib/table/base.cjs +72 -28
  33. package/lib/table/base.d.cts +8 -1
  34. package/lib/table/rollup.cjs +38 -5
  35. package/lib/table/rollup.d.cts +5 -2
  36. package/lib/table/token.cjs +14 -0
  37. package/lib/table/token.d.cts +9 -0
  38. package/lib/tombstone-cleanup.cjs +1 -1
  39. package/lib/write-set.cjs +351 -0
  40. package/lib/write-set.d.cts +66 -0
  41. package/package.json +8 -8
@@ -0,0 +1,339 @@
1
+ import crypto from "node:crypto";
2
+
3
+ //#region src/write-set.ts
4
+ const STATE_COMMIT_DOMAIN_V2 = "ocap:state-commit:v2:";
5
+ function codeUnitCompare(a, b) {
6
+ return a < b ? -1 : a > b ? 1 : 0;
7
+ }
8
+ function sha256Hex(utf8Input) {
9
+ return crypto.createHash("sha256").update(utf8Input, "utf8").digest("hex");
10
+ }
11
+ function bytesToHexLower(bytes) {
12
+ let out = "";
13
+ for (let i = 0; i < bytes.length; i++) out += bytes[i].toString(16).padStart(2, "0");
14
+ return out;
15
+ }
16
+ /**
17
+ * Recursively normalize an arbitrary JS value into a deterministic JSON-serializable structure
18
+ * with lexicographically sorted object keys (code-unit order).
19
+ * Bit-for-bit identical to `@arcblock/ledger-core` `canonicalizeStateValue`.
20
+ */
21
+ function canonicalizeStateValue(value, seen = /* @__PURE__ */ new WeakSet()) {
22
+ if (value === null || value === void 0) return null;
23
+ if (typeof value === "boolean" || typeof value === "string") return value;
24
+ if (typeof value === "number") {
25
+ if (!Number.isFinite(value)) return null;
26
+ return Object.is(value, -0) ? 0 : value;
27
+ }
28
+ if (typeof value === "bigint") return { $bigint: value.toString(10) };
29
+ if (value instanceof ArrayBuffer) return { $bytes: bytesToHexLower(new Uint8Array(value)) };
30
+ if (ArrayBuffer.isView(value)) return { $bytes: bytesToHexLower(new Uint8Array(value.buffer, value.byteOffset, value.byteLength)) };
31
+ if (Array.isArray(value)) {
32
+ if (seen.has(value)) throw new TypeError("Cannot canonicalize circular state array");
33
+ seen.add(value);
34
+ try {
35
+ return value.map((item) => canonicalizeStateValue(item, seen));
36
+ } finally {
37
+ seen.delete(value);
38
+ }
39
+ }
40
+ if (typeof value === "object") {
41
+ if (seen.has(value)) throw new TypeError("Cannot canonicalize circular state object");
42
+ seen.add(value);
43
+ try {
44
+ const proto = Object.getPrototypeOf(value);
45
+ if (!(proto === Object.prototype || proto === null) && typeof value.toJSON === "function") return canonicalizeStateValue(value.toJSON(), seen);
46
+ const record = value;
47
+ const sortedKeys = Object.keys(record).sort(codeUnitCompare);
48
+ const normalized = Object.create(null);
49
+ for (const k of sortedKeys) {
50
+ const v = record[k];
51
+ if (v !== void 0) Object.defineProperty(normalized, k, {
52
+ value: canonicalizeStateValue(v, seen),
53
+ enumerable: true,
54
+ writable: true,
55
+ configurable: true
56
+ });
57
+ }
58
+ return normalized;
59
+ } finally {
60
+ seen.delete(value);
61
+ }
62
+ }
63
+ return String(value);
64
+ }
65
+ function canonicalizeStateJson(value) {
66
+ return JSON.stringify(canonicalizeStateValue(value));
67
+ }
68
+ /**
69
+ * Collapse an ordered sequence of intra-transaction state writes into a canonical, deterministic tuple array
70
+ * and hash it with SHA-256 (64 lowercase hex chars).
71
+ * Bit-for-bit identical to `@arcblock/ledger-core` `computeStateWriteSetDigest`.
72
+ */
73
+ function computeStateWriteSetDigest(writes) {
74
+ const tables = /* @__PURE__ */ new Map();
75
+ const rawOps = [];
76
+ const ensureTable = (table) => {
77
+ let entry = tables.get(table);
78
+ if (!entry) {
79
+ entry = {
80
+ reset: false,
81
+ rows: /* @__PURE__ */ new Map()
82
+ };
83
+ tables.set(table, entry);
84
+ }
85
+ return entry;
86
+ };
87
+ for (const w of writes) {
88
+ if (!w || typeof w !== "object") continue;
89
+ if (w.op === "reset") {
90
+ const t = ensureTable(String(w.table));
91
+ t.reset = true;
92
+ t.rows.clear();
93
+ } else if (w.op === "put") ensureTable(String(w.table)).rows.set(String(w.key), {
94
+ op: "put",
95
+ value: canonicalizeStateValue(w.value)
96
+ });
97
+ else if (w.op === "delete") ensureTable(String(w.table)).rows.set(String(w.key), { op: "delete" });
98
+ else if (w.op === "raw") rawOps.push({
99
+ sql: String(w.sql),
100
+ parameters: Array.isArray(w.parameters) ? w.parameters.map((p) => canonicalizeStateValue(p)) : []
101
+ });
102
+ }
103
+ const normalizedEntries = [];
104
+ const sortedTables = Array.from(tables.keys()).sort(codeUnitCompare);
105
+ for (const table of sortedTables) {
106
+ const t = tables.get(table);
107
+ if (t.reset) normalizedEntries.push(["reset", table]);
108
+ const sortedKeys = Array.from(t.rows.keys()).sort(codeUnitCompare);
109
+ for (const key of sortedKeys) {
110
+ const row = t.rows.get(key);
111
+ if (row.op === "delete") normalizedEntries.push([
112
+ "delete",
113
+ table,
114
+ key
115
+ ]);
116
+ else normalizedEntries.push([
117
+ "put",
118
+ table,
119
+ key,
120
+ row.value
121
+ ]);
122
+ }
123
+ }
124
+ for (const raw of rawOps) normalizedEntries.push([
125
+ "raw",
126
+ raw.sql,
127
+ raw.parameters
128
+ ]);
129
+ return sha256Hex(JSON.stringify(normalizedEntries));
130
+ }
131
+ /**
132
+ * Compute v2 state commit hash binding `(prevHash, seq, msg, writeSetDigest)` with unambiguous JSON framing.
133
+ * Bit-for-bit identical to `@arcblock/ledger-core` `computeStateCommitHash`.
134
+ */
135
+ function computeStateCommitHash(params) {
136
+ const { prevHash, seq, msg, writeSetDigest } = params;
137
+ if (!Number.isSafeInteger(seq) || seq < 1) throw new Error(`Invalid state commit sequence: ${seq}`);
138
+ if (typeof writeSetDigest !== "string" || !/^[0-9a-f]{64}$/i.test(writeSetDigest)) throw new Error("Invalid writeSetDigest: expected 64-char hex SHA-256");
139
+ return sha256Hex(`${STATE_COMMIT_DOMAIN_V2}${JSON.stringify([
140
+ String(prevHash),
141
+ seq,
142
+ String(msg),
143
+ writeSetDigest.toLowerCase()
144
+ ])}`);
145
+ }
146
+ /**
147
+ * Format a table's primary key into an unambiguous canonical string key.
148
+ * Single-column PKs serialize as `String(val)`; composite PKs serialize as a JSON string tuple.
149
+ */
150
+ function formatCanonicalPrimaryKey(uniqIndex, keyOrDoc) {
151
+ if (Array.isArray(uniqIndex)) {
152
+ const obj = keyOrDoc || {};
153
+ return JSON.stringify(uniqIndex.map((col) => obj[col]));
154
+ }
155
+ if (typeof keyOrDoc === "object" && keyOrDoc !== null) return String(keyOrDoc[uniqIndex]);
156
+ return String(keyOrDoc);
157
+ }
158
+ const TOP_LEVEL_MUTATING_STMT_RE = /^(?:INSERT|UPDATE|DELETE|REPLACE|ALTER|DROP|CREATE|ATTACH|DETACH|VACUUM|REINDEX|ANALYZE|PRAGMA)\b/i;
159
+ const CTE_MUTATING_TAIL_RE = /^WITH\b[\s\S]*?\b(?:INSERT|UPDATE|DELETE|REPLACE)\b/i;
160
+ function scanTopLevelSqlStatements(sqlText) {
161
+ const statements = [];
162
+ let current = "";
163
+ let depth = 0;
164
+ const len = sqlText.length;
165
+ let i = 0;
166
+ while (i < len) {
167
+ const ch = sqlText[i];
168
+ const next = i + 1 < len ? sqlText[i + 1] : "";
169
+ if (ch === "-" && next === "-") {
170
+ i += 2;
171
+ while (i < len && sqlText[i] !== "\n" && sqlText[i] !== "\r") i++;
172
+ if (depth === 0) current += " ";
173
+ continue;
174
+ }
175
+ if (ch === "/" && next === "*") {
176
+ i += 2;
177
+ while (i < len && !(sqlText[i] === "*" && sqlText[i + 1] === "/")) i++;
178
+ i = i < len ? i + 2 : len;
179
+ if (depth === 0) current += " ";
180
+ continue;
181
+ }
182
+ if (ch === "'" || ch === "\"" || ch === "`") {
183
+ const quote = ch;
184
+ i++;
185
+ while (i < len) {
186
+ if (sqlText[i] === quote) {
187
+ if (sqlText[i + 1] === quote) {
188
+ i += 2;
189
+ continue;
190
+ }
191
+ i++;
192
+ break;
193
+ }
194
+ i++;
195
+ }
196
+ if (depth === 0) current += " ";
197
+ continue;
198
+ }
199
+ if (ch === "[") {
200
+ i++;
201
+ while (i < len && sqlText[i] !== "]") i++;
202
+ if (i < len) i++;
203
+ if (depth === 0) current += " ";
204
+ continue;
205
+ }
206
+ if (ch === "(") {
207
+ if (depth === 0) current += " ";
208
+ depth++;
209
+ i++;
210
+ continue;
211
+ }
212
+ if (ch === ")") {
213
+ if (depth > 0) depth--;
214
+ if (depth === 0) current += " ";
215
+ i++;
216
+ continue;
217
+ }
218
+ if (ch === ";" && depth === 0) {
219
+ const trimmed = current.trim();
220
+ if (trimmed.length > 0) statements.push(trimmed);
221
+ current = "";
222
+ i++;
223
+ continue;
224
+ }
225
+ if (depth === 0) current += ch;
226
+ i++;
227
+ }
228
+ const tail = current.trim();
229
+ if (tail.length > 0) statements.push(tail);
230
+ return statements;
231
+ }
232
+ function isTrackedRawMutatingSql(sqlText) {
233
+ for (const stmt of scanTopLevelSqlStatements(sqlText)) if (TOP_LEVEL_MUTATING_STMT_RE.test(stmt) || CTE_MUTATING_TAIL_RE.test(stmt)) return true;
234
+ return false;
235
+ }
236
+ var WriteSetTracker = class {
237
+ constructor() {
238
+ this.writes = [];
239
+ }
240
+ recordPut(table, key, value) {
241
+ this.writes.push({
242
+ op: "put",
243
+ table,
244
+ key,
245
+ value
246
+ });
247
+ }
248
+ recordDelete(table, key) {
249
+ this.writes.push({
250
+ op: "delete",
251
+ table,
252
+ key
253
+ });
254
+ }
255
+ recordReset(table) {
256
+ this.writes.push({
257
+ op: "reset",
258
+ table
259
+ });
260
+ }
261
+ recordRaw(sqlText, parameters) {
262
+ this.writes.push({
263
+ op: "raw",
264
+ sql: sqlText,
265
+ parameters
266
+ });
267
+ }
268
+ getWrites() {
269
+ return this.writes;
270
+ }
271
+ computeDigest() {
272
+ return computeStateWriteSetDigest(this.writes);
273
+ }
274
+ };
275
+ var WriteSetRawDmlPlugin = class {
276
+ constructor(rawTxn, tracker, basePlugins) {
277
+ this.pendingByQueryId = /* @__PURE__ */ new WeakMap();
278
+ this.rawTxn = rawTxn;
279
+ this.tracker = tracker;
280
+ this.basePlugins = basePlugins;
281
+ }
282
+ transformQuery(args) {
283
+ try {
284
+ const compiled = this.rawTxn.getExecutor().compileQuery(args.node, args.queryId);
285
+ if (isTrackedRawMutatingSql(compiled.sql)) this.pendingByQueryId.set(args.queryId, {
286
+ sql: compiled.sql,
287
+ parameters: compiled.parameters
288
+ });
289
+ } catch {}
290
+ return args.node;
291
+ }
292
+ async transformResult(args) {
293
+ const key = args.queryId;
294
+ const pending = this.pendingByQueryId.get(key);
295
+ if (pending) {
296
+ this.pendingByQueryId.delete(key);
297
+ this.tracker.recordRaw(pending.sql, pending.parameters);
298
+ }
299
+ return args.result;
300
+ }
301
+ };
302
+ const TXN_TRACKER_MAP = /* @__PURE__ */ new WeakMap();
303
+ const RAW_TXN_MAP = /* @__PURE__ */ new WeakMap();
304
+ function attachWriteSetTracker(rawTxn, tracker) {
305
+ const basePlugins = rawTxn.getExecutor().plugins;
306
+ const plugin = new WriteSetRawDmlPlugin(rawTxn, tracker, basePlugins);
307
+ const wrappedTxn = rawTxn.withPlugin(plugin);
308
+ TXN_TRACKER_MAP.set(rawTxn, tracker);
309
+ TXN_TRACKER_MAP.set(wrappedTxn, tracker);
310
+ RAW_TXN_MAP.set(wrappedTxn, rawTxn);
311
+ return wrappedTxn;
312
+ }
313
+ function getWriteSetTracker(txn) {
314
+ if (!txn || typeof txn !== "object") return void 0;
315
+ const direct = TXN_TRACKER_MAP.get(txn);
316
+ if (direct) return direct;
317
+ const plugins = txn.getExecutor?.()?.plugins;
318
+ if (Array.isArray(plugins)) {
319
+ for (const p of plugins) if (p instanceof WriteSetRawDmlPlugin) return p.tracker;
320
+ }
321
+ }
322
+ function unwrapTableTransaction(txn) {
323
+ const direct = RAW_TXN_MAP.get(txn);
324
+ if (direct) return direct;
325
+ const plugins = txn.getExecutor?.()?.plugins;
326
+ if (Array.isArray(plugins)) {
327
+ const found = plugins.find((p) => p instanceof WriteSetRawDmlPlugin);
328
+ if (found) {
329
+ let stripped = txn.withoutPlugins();
330
+ for (const bp of found.basePlugins) stripped = stripped.withPlugin(bp);
331
+ RAW_TXN_MAP.set(txn, stripped);
332
+ return stripped;
333
+ }
334
+ }
335
+ return txn;
336
+ }
337
+
338
+ //#endregion
339
+ export { STATE_COMMIT_DOMAIN_V2, WriteSetTracker, attachWriteSetTracker, canonicalizeStateJson, canonicalizeStateValue, computeStateCommitHash, computeStateWriteSetDigest, formatCanonicalPrimaryKey, getWriteSetTracker, isTrackedRawMutatingSql, unwrapTableTransaction };
package/lib/db.cjs CHANGED
@@ -1,21 +1,27 @@
1
1
  Object.defineProperty(exports, '__esModule', { value: true });
2
2
  const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
3
3
  const require_kysely = require('./kysely.cjs');
4
+ const require_migrations_004_rollup_reserves = require('./migrations/004-rollup-reserves.cjs');
4
5
  const require_migrations_index = require('./migrations/index.cjs');
6
+ const require_write_set = require('./write-set.cjs');
5
7
  const require_table_base = require('./table/base.cjs');
6
8
  const require_table_account = require('./table/account.cjs');
7
9
  const require_table_balance = require('./table/balance.cjs');
8
10
  const require_table_rollup = require('./table/rollup.cjs');
9
11
  const require_table_token = require('./table/token.cjs');
10
12
  const require_package = require('./package.cjs');
11
- let node_crypto = require("node:crypto");
12
- node_crypto = require_rolldown_runtime.__toESM(node_crypto);
13
13
  let _ocap_statedb = require("@ocap/statedb");
14
14
  let kysely = require("kysely");
15
15
 
16
16
  //#region src/db.ts
17
17
  const { name, version } = require_package.default;
18
18
  const SQLITE_BUSY_CODES = ["SQLITE_BUSY", "SQLITE_LOCKED"];
19
+ function isSqliteBusyError(err) {
20
+ const code = err?.code;
21
+ if (code === 5 || code === 6 || SQLITE_BUSY_CODES.includes(String(code || ""))) return true;
22
+ const msg = err instanceof Error ? err.message : String(err ?? "");
23
+ return msg.includes("database is locked") || msg.includes("SQLITE_BUSY") || msg.includes("SQLITE_LOCKED");
24
+ }
19
25
  /**
20
26
  * SQLite StateDB Implementation
21
27
  * Uses SQLite via Kysely as the storage backend
@@ -218,11 +224,23 @@ var SqliteStateDB = class extends _ocap_statedb.StateDB {
218
224
  return;
219
225
  }
220
226
  await require_migrations_index.runMigrations(this.db);
227
+ await this.assertRollupReserveColumns();
221
228
  this.initialized = true;
222
229
  if (this.trustedStorage) await this.ensureTrustedStorageInitialized(true);
223
230
  this.startWalCheckpointWorker();
224
231
  }
225
232
  /**
233
+ * Fail startup instead of failing every reserve-tracking rollup write later (#1548): a file whose migration
234
+ * bookkeeping says 004 ran but whose `rollup` table lacks the columns (copied/hand-built DB) must not open.
235
+ * Mirrors the Dolt backend's `rollup.reserves` startup guard.
236
+ */
237
+ async assertRollupReserveColumns() {
238
+ const { rows } = await kysely.sql`SELECT name FROM pragma_table_info('rollup')`.execute(this.db);
239
+ const have = new Set(rows.map((r) => r.name));
240
+ const missing = require_migrations_004_rollup_reserves.ROLLUP_RESERVE_COLUMNS.map(([c]) => c).filter((c) => !have.has(c));
241
+ if (missing.length) throw new Error(`SQLite statedb migration incomplete: rollup is missing column(s) ${missing.join(", ")}`);
242
+ }
243
+ /**
226
244
  * Run a WAL checkpoint (`PRAGMA wal_checkpoint(<mode>)`) against the SQLite database.
227
245
  * No-op returning `{ busy: 0, log: 0, checkpointed: 0 }` when WAL mode is disabled.
228
246
  */
@@ -362,7 +380,7 @@ var SqliteStateDB = class extends _ocap_statedb.StateDB {
362
380
  return this.executeRunAsLambda(fn, options);
363
381
  }
364
382
  async executeRunAsLambda(fn, options = {}) {
365
- const { retryLimit = 2, verifyHash = false, commitMessage } = options;
383
+ const { retryLimit = 5, verifyHash = false, commitMessage } = options;
366
384
  if (!this.stateCommitTableReady) await this.ensureStateCommitTable();
367
385
  if (this.trustedStorage) {
368
386
  await this.ensureTrustedStorageInitialized();
@@ -370,6 +388,7 @@ var SqliteStateDB = class extends _ocap_statedb.StateDB {
370
388
  }
371
389
  let result;
372
390
  let committedHash;
391
+ let committedWriteSetDigest;
373
392
  let previousHash;
374
393
  let lastError;
375
394
  let committed = false;
@@ -379,25 +398,34 @@ var SqliteStateDB = class extends _ocap_statedb.StateDB {
379
398
  const savedTimerCounters = opCtx?.timer?.stateDbCounters && typeof opCtx.timer.stateDbCounters === "object" ? { ...opCtx.timer.stateDbCounters } : void 0;
380
399
  try {
381
400
  committedHash = void 0;
401
+ committedWriteSetDigest = void 0;
382
402
  previousHash = void 0;
383
403
  this._activeLambdaCount++;
384
404
  try {
385
- result = await this.db.transaction().execute(async (txn) => {
386
- const txResult = await fn(txn);
405
+ result = await this.db.transaction().execute(async (rawTxn) => {
406
+ const tracker = new require_write_set.WriteSetTracker();
407
+ const txResult = await fn(require_write_set.attachWriteSetTracker(rawTxn, tracker));
387
408
  const row = (await kysely.sql`
388
409
  SELECT "current_hash", "seq" FROM "_state_commit" WHERE "id" = 1
389
- `.execute(txn)).rows[0];
410
+ `.execute(rawTxn)).rows[0];
390
411
  const prevHash = row?.current_hash || "GENESIS";
391
412
  const nextSeq = Number(row?.seq || 0) + 1;
392
413
  const msg = commitMessage || txResult?.hash || "commit";
393
- const nextHash = node_crypto.default.createHash("sha256").update(`${prevHash}:${nextSeq}:${msg}`).digest("hex");
414
+ const writeSetDigest = tracker.computeDigest();
415
+ const nextHash = require_write_set.computeStateCommitHash({
416
+ prevHash,
417
+ seq: nextSeq,
418
+ msg,
419
+ writeSetDigest
420
+ });
394
421
  await kysely.sql`
395
422
  UPDATE "_state_commit"
396
423
  SET "current_hash" = ${nextHash}, "prev_hash" = ${prevHash}, "seq" = ${nextSeq}, "msg" = ${msg}
397
424
  WHERE "id" = 1
398
- `.execute(txn);
425
+ `.execute(rawTxn);
399
426
  previousHash = prevHash;
400
427
  committedHash = nextHash;
428
+ committedWriteSetDigest = writeSetDigest;
401
429
  return txResult;
402
430
  });
403
431
  } finally {
@@ -412,12 +440,12 @@ var SqliteStateDB = class extends _ocap_statedb.StateDB {
412
440
  }
413
441
  if (opCtx?.timer?.stateDbCounters && savedTimerCounters !== void 0) Object.assign(opCtx.timer.stateDbCounters, savedTimerCounters);
414
442
  lastError = err;
415
- const code = err?.code;
416
- if (!SQLITE_BUSY_CODES.includes(code)) throw err;
443
+ if (!isSqliteBusyError(err)) throw err;
417
444
  if (attempt < retryLimit) await new Promise((r) => setTimeout(r, 10 * 2 ** attempt));
418
445
  }
419
446
  }
420
447
  if (!committed) throw lastError;
448
+ this.lastWriteSetDigest = committedWriteSetDigest;
421
449
  if (this.trustedStorage && committedHash) await this.trustedStorage.updateHash(committedHash, previousHash);
422
450
  if (options.onCommit) try {
423
451
  await options.onCommit(committedHash);
@@ -434,6 +462,7 @@ var SqliteStateDB = class extends _ocap_statedb.StateDB {
434
462
  if (tableInstance?.reset) await tableInstance.reset();
435
463
  }
436
464
  if (this.stateCommitTableReady) {
465
+ this.lastWriteSetDigest = void 0;
437
466
  await kysely.sql`
438
467
  UPDATE "_state_commit"
439
468
  SET "current_hash" = 'GENESIS', "prev_hash" = '', "seq" = 0, "msg" = ''
@@ -460,6 +489,12 @@ var SqliteStateDB = class extends _ocap_statedb.StateDB {
460
489
  if (this.trustedStorage && typeof this.trustedStorage.destroy === "function") await this.trustedStorage.destroy();
461
490
  }
462
491
  /**
492
+ * Get the canonical SHA-256 digest of the write-set from the most recently committed `runAsLambda` transaction.
493
+ */
494
+ getLastWriteSetDigest() {
495
+ return this.lastWriteSetDigest;
496
+ }
497
+ /**
463
498
  * Get the underlying Kysely instance (for advanced operations)
464
499
  */
465
500
  getKysely() {
package/lib/db.d.cts CHANGED
@@ -37,6 +37,7 @@ declare class SqliteStateDB extends StateDB implements IStateDB {
37
37
  private walCheckpointPromise;
38
38
  private _activeLambdaCount;
39
39
  private closed;
40
+ private lastWriteSetDigest?;
40
41
  balance: IBalanceTable;
41
42
  account: IStateTable<IAccountState>;
42
43
  factory: IStateTable<IAssetFactoryState>;
@@ -77,6 +78,12 @@ declare class SqliteStateDB extends StateDB implements IStateDB {
77
78
  * Call this after constructor to ensure database schema is ready
78
79
  */
79
80
  initialize(): Promise<void>;
81
+ /**
82
+ * Fail startup instead of failing every reserve-tracking rollup write later (#1548): a file whose migration
83
+ * bookkeeping says 004 ran but whose `rollup` table lacks the columns (copied/hand-built DB) must not open.
84
+ * Mirrors the Dolt backend's `rollup.reserves` startup guard.
85
+ */
86
+ private assertRollupReserveColumns;
80
87
  /**
81
88
  * Run a WAL checkpoint (`PRAGMA wal_checkpoint(<mode>)`) against the SQLite database.
82
89
  * No-op returning `{ busy: 0, log: 0, checkpointed: 0 }` when WAL mode is disabled.
@@ -130,6 +137,10 @@ declare class SqliteStateDB extends StateDB implements IStateDB {
130
137
  * Close database connection
131
138
  */
132
139
  close(): Promise<void>;
140
+ /**
141
+ * Get the canonical SHA-256 digest of the write-set from the most recently committed `runAsLambda` transaction.
142
+ */
143
+ getLastWriteSetDigest(): string | undefined;
133
144
  /**
134
145
  * Get the underlying Kysely instance (for advanced operations)
135
146
  */
package/lib/index.cjs CHANGED
@@ -1,5 +1,8 @@
1
1
  Object.defineProperty(exports, '__esModule', { value: true });
2
2
  const require_kysely = require('./kysely.cjs');
3
+ const require_migrations_003_token_charter_assets = require('./migrations/003-token-charter-assets.cjs');
4
+ const require_migrations_index = require('./migrations/index.cjs');
5
+ const require_write_set = require('./write-set.cjs');
3
6
  const require_db = require('./db.cjs');
4
7
  const require_reconcile = require('./reconcile.cjs');
5
8
  const require_tombstone_cleanup = require('./tombstone-cleanup.cjs');
@@ -9,16 +12,32 @@ var src_default = require_db.default;
9
12
 
10
13
  //#endregion
11
14
  exports.DurableObjectSqliteDialect = require_kysely.DurableObjectSqliteDialect;
15
+ exports.STATE_COMMIT_DOMAIN_V2 = require_write_set.STATE_COMMIT_DOMAIN_V2;
12
16
  exports.SqliteStateDB = require_db.default;
17
+ exports.WriteSetTracker = require_write_set.WriteSetTracker;
13
18
  exports.applyTombstoneBalanceCleanup = require_tombstone_cleanup.applyTombstoneBalanceCleanup;
14
19
  exports.assertSqliteStateDb = require_reconcile.assertSqliteStateDb;
20
+ exports.canonicalizeStateJson = require_write_set.canonicalizeStateJson;
21
+ exports.canonicalizeStateValue = require_write_set.canonicalizeStateValue;
15
22
  exports.compareUpdatedAtTimestamps = require_tombstone_cleanup.compareUpdatedAtTimestamps;
23
+ exports.computeStateCommitHash = require_write_set.computeStateCommitHash;
24
+ exports.computeStateWriteSetDigest = require_write_set.computeStateWriteSetDigest;
16
25
  exports.computeTombstoneCleanupPlanDigest = require_tombstone_cleanup.computeTombstoneCleanupPlanDigest;
17
26
  exports.createKysely = require_kysely.createKysely;
18
27
  exports.default = src_default;
19
28
  exports.extractContextUpdatedAt = require_tombstone_cleanup.extractContextUpdatedAt;
29
+ exports.formatCanonicalPrimaryKey = require_write_set.formatCanonicalPrimaryKey;
20
30
  exports.getRawDatabase = require_kysely.getRawDatabase;
21
31
  exports.isBalanceTimestampNotAfterAccount = require_tombstone_cleanup.isBalanceTimestampNotAfterAccount;
32
+ exports.isTrackedRawMutatingSql = require_write_set.isTrackedRawMutatingSql;
22
33
  exports.planTombstoneBalanceCleanup = require_tombstone_cleanup.planTombstoneBalanceCleanup;
23
34
  exports.reconcileChainState = require_reconcile.reconcileChainState;
35
+ exports.rollbackMigrations = require_migrations_index.rollbackMigrations;
36
+ exports.runMigrations = require_migrations_index.runMigrations;
37
+ Object.defineProperty(exports, 'tokenCharterAssets', {
38
+ enumerable: true,
39
+ get: function () {
40
+ return require_migrations_003_token_charter_assets._003_token_charter_assets_exports;
41
+ }
42
+ });
24
43
  exports.verifyChainStateInvariants = require_reconcile.verifyChainStateInvariants;
package/lib/index.d.cts CHANGED
@@ -3,4 +3,7 @@ import { DurableObjectSqliteDialect, DurableObjectSqliteDialectConfig, IDurableO
3
3
  import SqliteStateDB from "./db.cjs";
4
4
  import { ChainStateAuditReport, InvariantSeverity, InvariantViolation, ReconcileAction, ReconcileOptions, ReconcileResult, assertSqliteStateDb, reconcileChainState, verifyChainStateInvariants } from "./reconcile.cjs";
5
5
  import { ApplyTombstoneCleanupOptions, TombstoneBalanceRow, TombstoneCleanupApplyResult, TombstoneCleanupPlan, TombstoneTokenSummary, applyTombstoneBalanceCleanup, compareUpdatedAtTimestamps, computeTombstoneCleanupPlanDigest, extractContextUpdatedAt, isBalanceTimestampNotAfterAccount, planTombstoneBalanceCleanup } from "./tombstone-cleanup.cjs";
6
- export { type ApplyTombstoneCleanupOptions, type ChainStateAuditReport, type Database, DurableObjectSqliteDialect, type DurableObjectSqliteDialectConfig, type IDurableObjectSqlStorage, type IDurableObjectSqlStorageCursor, type InvariantSeverity, type InvariantViolation, type ReconcileAction, type ReconcileOptions, type ReconcileResult, type SqliteConfig, type SqliteOperationContext, SqliteStateDB, SqliteStateDB as default, type TombstoneBalanceRow, type TombstoneCleanupApplyResult, type TombstoneCleanupPlan, type TombstoneTokenSummary, applyTombstoneBalanceCleanup, assertSqliteStateDb, compareUpdatedAtTimestamps, computeTombstoneCleanupPlanDigest, createKysely, extractContextUpdatedAt, getRawDatabase, isBalanceTimestampNotAfterAccount, planTombstoneBalanceCleanup, reconcileChainState, verifyChainStateInvariants };
6
+ import { _003_token_charter_assets_d_exports } from "./migrations/003-token-charter-assets.cjs";
7
+ import { rollbackMigrations, runMigrations } from "./migrations/index.cjs";
8
+ import { IStateCommitStepInput, IStateWriteRecord, STATE_COMMIT_DOMAIN_V2, WriteSetTracker, canonicalizeStateJson, canonicalizeStateValue, computeStateCommitHash, computeStateWriteSetDigest, formatCanonicalPrimaryKey, isTrackedRawMutatingSql } from "./write-set.cjs";
9
+ export { type ApplyTombstoneCleanupOptions, type ChainStateAuditReport, type Database, DurableObjectSqliteDialect, type DurableObjectSqliteDialectConfig, type IDurableObjectSqlStorage, type IDurableObjectSqlStorageCursor, type IStateCommitStepInput, type IStateWriteRecord, type InvariantSeverity, type InvariantViolation, type ReconcileAction, type ReconcileOptions, type ReconcileResult, STATE_COMMIT_DOMAIN_V2, type SqliteConfig, type SqliteOperationContext, SqliteStateDB, SqliteStateDB as default, type TombstoneBalanceRow, type TombstoneCleanupApplyResult, type TombstoneCleanupPlan, type TombstoneTokenSummary, WriteSetTracker, applyTombstoneBalanceCleanup, assertSqliteStateDb, canonicalizeStateJson, canonicalizeStateValue, compareUpdatedAtTimestamps, computeStateCommitHash, computeStateWriteSetDigest, computeTombstoneCleanupPlanDigest, createKysely, extractContextUpdatedAt, formatCanonicalPrimaryKey, getRawDatabase, isBalanceTimestampNotAfterAccount, isTrackedRawMutatingSql, planTombstoneBalanceCleanup, reconcileChainState, rollbackMigrations, runMigrations, _003_token_charter_assets_d_exports as tokenCharterAssets, verifyChainStateInvariants };
@@ -6,9 +6,16 @@ var _002_delegation_extensions_exports = /* @__PURE__ */ require_rolldown_runtim
6
6
  down: () => down,
7
7
  up: () => up
8
8
  });
9
+ async function addColumnIfNotExists(db, ddl) {
10
+ try {
11
+ await ddl.execute(db);
12
+ } catch (err) {
13
+ if (!(err instanceof Error ? err.message : String(err ?? "")).toLowerCase().includes("duplicate column name")) throw err;
14
+ }
15
+ }
9
16
  async function up(db) {
10
- await kysely.sql`ALTER TABLE delegation ADD COLUMN deny TEXT`.execute(db);
11
- await kysely.sql`ALTER TABLE delegation ADD COLUMN validUntil INTEGER DEFAULT 0`.execute(db);
17
+ await addColumnIfNotExists(db, kysely.sql`ALTER TABLE delegation ADD COLUMN deny TEXT`);
18
+ await addColumnIfNotExists(db, kysely.sql`ALTER TABLE delegation ADD COLUMN validUntil INTEGER DEFAULT 0`);
12
19
  }
13
20
  async function down(_db) {}
14
21
 
@@ -0,0 +1,47 @@
1
+ const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
2
+ let kysely = require("kysely");
3
+
4
+ //#region src/migrations/004-rollup-reserves.ts
5
+ var _004_rollup_reserves_exports = /* @__PURE__ */ require_rolldown_runtime.__exportAll({
6
+ ROLLUP_RESERVE_COLUMNS: () => ROLLUP_RESERVE_COLUMNS,
7
+ down: () => down,
8
+ up: () => up
9
+ });
10
+ /**
11
+ * Add the top-level rollup fields that the Dolt backend gained in `20261109-rollup-reserves` (#1169 / #1349):
12
+ * reserve accounting and multi-target config (#1548).
13
+ *
14
+ * Without these columns every write that carries them fails with `table rollup has no column named ...`:
15
+ * creating an OCAP_ISOMORPHIC / watermark-enforcing rollup, and `recordRollupDeposit` / `recordRollupWithdraw`
16
+ * on such a rollup. Column types mirror Dolt: JSON as TEXT, booleans as INTEGER 0/1, strings as TEXT.
17
+ *
18
+ * Schema-only, nullable, no backfill: existing rows read back NULL, which RollupTable strips so legacy rollups keep
19
+ * their `data.value.*` fallbacks and do NOT start reserve tracking. Idempotent (duplicate-column tolerated).
20
+ */
21
+ const ROLLUP_RESERVE_COLUMNS = [
22
+ ["reserves", "TEXT"],
23
+ ["targetType", "TEXT"],
24
+ ["targetChainId", "TEXT"],
25
+ ["supportedTokens", "TEXT"],
26
+ ["allowProposerSponsor", "INTEGER"],
27
+ ["enforceReserveWatermark", "INTEGER"]
28
+ ];
29
+ async function up(db) {
30
+ for (const [column, type] of ROLLUP_RESERVE_COLUMNS) try {
31
+ await kysely.sql.raw(`ALTER TABLE rollup ADD COLUMN "${column}" ${type}`).execute(db);
32
+ } catch (err) {
33
+ if (!(err instanceof Error ? err.message : String(err ?? "")).toLowerCase().includes("duplicate column name")) throw err;
34
+ }
35
+ }
36
+ async function down(_db) {}
37
+
38
+ //#endregion
39
+ exports.ROLLUP_RESERVE_COLUMNS = ROLLUP_RESERVE_COLUMNS;
40
+ Object.defineProperty(exports, '_004_rollup_reserves_exports', {
41
+ enumerable: true,
42
+ get: function () {
43
+ return _004_rollup_reserves_exports;
44
+ }
45
+ });
46
+ exports.down = down;
47
+ exports.up = up;
@@ -0,0 +1,20 @@
1
+ import { Kysely } from "kysely";
2
+
3
+ //#region src/migrations/004-rollup-reserves.d.ts
4
+
5
+ /**
6
+ * Add the top-level rollup fields that the Dolt backend gained in `20261109-rollup-reserves` (#1169 / #1349):
7
+ * reserve accounting and multi-target config (#1548).
8
+ *
9
+ * Without these columns every write that carries them fails with `table rollup has no column named ...`:
10
+ * creating an OCAP_ISOMORPHIC / watermark-enforcing rollup, and `recordRollupDeposit` / `recordRollupWithdraw`
11
+ * on such a rollup. Column types mirror Dolt: JSON as TEXT, booleans as INTEGER 0/1, strings as TEXT.
12
+ *
13
+ * Schema-only, nullable, no backfill: existing rows read back NULL, which RollupTable strips so legacy rollups keep
14
+ * their `data.value.*` fallbacks and do NOT start reserve tracking. Idempotent (duplicate-column tolerated).
15
+ */
16
+ declare const ROLLUP_RESERVE_COLUMNS: readonly [readonly ["reserves", "TEXT"], readonly ["targetType", "TEXT"], readonly ["targetChainId", "TEXT"], readonly ["supportedTokens", "TEXT"], readonly ["allowProposerSponsor", "INTEGER"], readonly ["enforceReserveWatermark", "INTEGER"]];
17
+ declare function up(db: Kysely<unknown>): Promise<void>;
18
+ declare function down(_db: Kysely<unknown>): Promise<void>;
19
+ //#endregion
20
+ export { ROLLUP_RESERVE_COLUMNS, down, up };