@ocap/statedb-sqlite 1.31.0 → 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.
- package/esm/db.d.mts +11 -0
- package/esm/db.mjs +45 -9
- package/esm/index.d.mts +5 -1
- package/esm/index.mjs +5 -1
- package/esm/migrations/002-delegation-extensions.mjs +9 -2
- package/esm/migrations/004-rollup-reserves.d.mts +20 -0
- package/esm/migrations/004-rollup-reserves.mjs +39 -0
- package/esm/migrations/index.mjs +30 -15
- package/esm/table/account.d.mts +4 -3
- package/esm/table/account.mjs +31 -7
- package/esm/table/balance.mjs +31 -4
- package/esm/table/base.d.mts +8 -1
- package/esm/table/base.mjs +72 -28
- package/esm/table/rollup.d.mts +5 -2
- package/esm/table/rollup.mjs +38 -5
- package/esm/table/token.d.mts +9 -0
- package/esm/table/token.mjs +14 -0
- package/esm/tombstone-cleanup.d.mts +59 -0
- package/esm/tombstone-cleanup.mjs +207 -0
- package/esm/write-set.d.mts +66 -0
- package/esm/write-set.mjs +339 -0
- package/lib/db.cjs +45 -10
- package/lib/db.d.cts +11 -0
- package/lib/index.cjs +26 -0
- package/lib/index.d.cts +5 -1
- package/lib/migrations/002-delegation-extensions.cjs +9 -2
- package/lib/migrations/004-rollup-reserves.cjs +47 -0
- package/lib/migrations/004-rollup-reserves.d.cts +20 -0
- package/lib/migrations/index.cjs +30 -15
- package/lib/table/account.cjs +31 -7
- package/lib/table/account.d.cts +4 -3
- package/lib/table/balance.cjs +31 -4
- package/lib/table/base.cjs +72 -28
- package/lib/table/base.d.cts +8 -1
- package/lib/table/rollup.cjs +38 -5
- package/lib/table/rollup.d.cts +5 -2
- package/lib/table/token.cjs +14 -0
- package/lib/table/token.d.cts +9 -0
- package/lib/tombstone-cleanup.cjs +213 -0
- package/lib/tombstone-cleanup.d.cts +59 -0
- package/lib/write-set.cjs +351 -0
- package/lib/write-set.d.cts +66 -0
- package/package.json +8 -8
package/esm/db.d.mts
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/esm/db.mjs
CHANGED
|
@@ -1,18 +1,25 @@
|
|
|
1
1
|
import { createKysely, resolveWalCheckpointIntervalMs } from "./kysely.mjs";
|
|
2
|
+
import { ROLLUP_RESERVE_COLUMNS } from "./migrations/004-rollup-reserves.mjs";
|
|
2
3
|
import { runMigrations } from "./migrations/index.mjs";
|
|
4
|
+
import { WriteSetTracker, attachWriteSetTracker, computeStateCommitHash } from "./write-set.mjs";
|
|
3
5
|
import base_default from "./table/base.mjs";
|
|
4
6
|
import account_default from "./table/account.mjs";
|
|
5
7
|
import balance_default from "./table/balance.mjs";
|
|
6
8
|
import rollup_default from "./table/rollup.mjs";
|
|
7
9
|
import token_default from "./table/token.mjs";
|
|
8
10
|
import package_default from "./package.mjs";
|
|
9
|
-
import crypto from "node:crypto";
|
|
10
11
|
import { StateDB } from "@ocap/statedb";
|
|
11
12
|
import { sql } from "kysely";
|
|
12
13
|
|
|
13
14
|
//#region src/db.ts
|
|
14
15
|
const { name, version } = package_default;
|
|
15
16
|
const SQLITE_BUSY_CODES = ["SQLITE_BUSY", "SQLITE_LOCKED"];
|
|
17
|
+
function isSqliteBusyError(err) {
|
|
18
|
+
const code = err?.code;
|
|
19
|
+
if (code === 5 || code === 6 || SQLITE_BUSY_CODES.includes(String(code || ""))) return true;
|
|
20
|
+
const msg = err instanceof Error ? err.message : String(err ?? "");
|
|
21
|
+
return msg.includes("database is locked") || msg.includes("SQLITE_BUSY") || msg.includes("SQLITE_LOCKED");
|
|
22
|
+
}
|
|
16
23
|
/**
|
|
17
24
|
* SQLite StateDB Implementation
|
|
18
25
|
* Uses SQLite via Kysely as the storage backend
|
|
@@ -215,11 +222,23 @@ var SqliteStateDB = class extends StateDB {
|
|
|
215
222
|
return;
|
|
216
223
|
}
|
|
217
224
|
await runMigrations(this.db);
|
|
225
|
+
await this.assertRollupReserveColumns();
|
|
218
226
|
this.initialized = true;
|
|
219
227
|
if (this.trustedStorage) await this.ensureTrustedStorageInitialized(true);
|
|
220
228
|
this.startWalCheckpointWorker();
|
|
221
229
|
}
|
|
222
230
|
/**
|
|
231
|
+
* Fail startup instead of failing every reserve-tracking rollup write later (#1548): a file whose migration
|
|
232
|
+
* bookkeeping says 004 ran but whose `rollup` table lacks the columns (copied/hand-built DB) must not open.
|
|
233
|
+
* Mirrors the Dolt backend's `rollup.reserves` startup guard.
|
|
234
|
+
*/
|
|
235
|
+
async assertRollupReserveColumns() {
|
|
236
|
+
const { rows } = await sql`SELECT name FROM pragma_table_info('rollup')`.execute(this.db);
|
|
237
|
+
const have = new Set(rows.map((r) => r.name));
|
|
238
|
+
const missing = ROLLUP_RESERVE_COLUMNS.map(([c]) => c).filter((c) => !have.has(c));
|
|
239
|
+
if (missing.length) throw new Error(`SQLite statedb migration incomplete: rollup is missing column(s) ${missing.join(", ")}`);
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
223
242
|
* Run a WAL checkpoint (`PRAGMA wal_checkpoint(<mode>)`) against the SQLite database.
|
|
224
243
|
* No-op returning `{ busy: 0, log: 0, checkpointed: 0 }` when WAL mode is disabled.
|
|
225
244
|
*/
|
|
@@ -359,7 +378,7 @@ var SqliteStateDB = class extends StateDB {
|
|
|
359
378
|
return this.executeRunAsLambda(fn, options);
|
|
360
379
|
}
|
|
361
380
|
async executeRunAsLambda(fn, options = {}) {
|
|
362
|
-
const { retryLimit =
|
|
381
|
+
const { retryLimit = 5, verifyHash = false, commitMessage } = options;
|
|
363
382
|
if (!this.stateCommitTableReady) await this.ensureStateCommitTable();
|
|
364
383
|
if (this.trustedStorage) {
|
|
365
384
|
await this.ensureTrustedStorageInitialized();
|
|
@@ -367,6 +386,7 @@ var SqliteStateDB = class extends StateDB {
|
|
|
367
386
|
}
|
|
368
387
|
let result;
|
|
369
388
|
let committedHash;
|
|
389
|
+
let committedWriteSetDigest;
|
|
370
390
|
let previousHash;
|
|
371
391
|
let lastError;
|
|
372
392
|
let committed = false;
|
|
@@ -376,25 +396,34 @@ var SqliteStateDB = class extends StateDB {
|
|
|
376
396
|
const savedTimerCounters = opCtx?.timer?.stateDbCounters && typeof opCtx.timer.stateDbCounters === "object" ? { ...opCtx.timer.stateDbCounters } : void 0;
|
|
377
397
|
try {
|
|
378
398
|
committedHash = void 0;
|
|
399
|
+
committedWriteSetDigest = void 0;
|
|
379
400
|
previousHash = void 0;
|
|
380
401
|
this._activeLambdaCount++;
|
|
381
402
|
try {
|
|
382
|
-
result = await this.db.transaction().execute(async (
|
|
383
|
-
const
|
|
403
|
+
result = await this.db.transaction().execute(async (rawTxn) => {
|
|
404
|
+
const tracker = new WriteSetTracker();
|
|
405
|
+
const txResult = await fn(attachWriteSetTracker(rawTxn, tracker));
|
|
384
406
|
const row = (await sql`
|
|
385
407
|
SELECT "current_hash", "seq" FROM "_state_commit" WHERE "id" = 1
|
|
386
|
-
`.execute(
|
|
408
|
+
`.execute(rawTxn)).rows[0];
|
|
387
409
|
const prevHash = row?.current_hash || "GENESIS";
|
|
388
410
|
const nextSeq = Number(row?.seq || 0) + 1;
|
|
389
411
|
const msg = commitMessage || txResult?.hash || "commit";
|
|
390
|
-
const
|
|
412
|
+
const writeSetDigest = tracker.computeDigest();
|
|
413
|
+
const nextHash = computeStateCommitHash({
|
|
414
|
+
prevHash,
|
|
415
|
+
seq: nextSeq,
|
|
416
|
+
msg,
|
|
417
|
+
writeSetDigest
|
|
418
|
+
});
|
|
391
419
|
await sql`
|
|
392
420
|
UPDATE "_state_commit"
|
|
393
421
|
SET "current_hash" = ${nextHash}, "prev_hash" = ${prevHash}, "seq" = ${nextSeq}, "msg" = ${msg}
|
|
394
422
|
WHERE "id" = 1
|
|
395
|
-
`.execute(
|
|
423
|
+
`.execute(rawTxn);
|
|
396
424
|
previousHash = prevHash;
|
|
397
425
|
committedHash = nextHash;
|
|
426
|
+
committedWriteSetDigest = writeSetDigest;
|
|
398
427
|
return txResult;
|
|
399
428
|
});
|
|
400
429
|
} finally {
|
|
@@ -409,12 +438,12 @@ var SqliteStateDB = class extends StateDB {
|
|
|
409
438
|
}
|
|
410
439
|
if (opCtx?.timer?.stateDbCounters && savedTimerCounters !== void 0) Object.assign(opCtx.timer.stateDbCounters, savedTimerCounters);
|
|
411
440
|
lastError = err;
|
|
412
|
-
|
|
413
|
-
if (!SQLITE_BUSY_CODES.includes(code)) throw err;
|
|
441
|
+
if (!isSqliteBusyError(err)) throw err;
|
|
414
442
|
if (attempt < retryLimit) await new Promise((r) => setTimeout(r, 10 * 2 ** attempt));
|
|
415
443
|
}
|
|
416
444
|
}
|
|
417
445
|
if (!committed) throw lastError;
|
|
446
|
+
this.lastWriteSetDigest = committedWriteSetDigest;
|
|
418
447
|
if (this.trustedStorage && committedHash) await this.trustedStorage.updateHash(committedHash, previousHash);
|
|
419
448
|
if (options.onCommit) try {
|
|
420
449
|
await options.onCommit(committedHash);
|
|
@@ -431,6 +460,7 @@ var SqliteStateDB = class extends StateDB {
|
|
|
431
460
|
if (tableInstance?.reset) await tableInstance.reset();
|
|
432
461
|
}
|
|
433
462
|
if (this.stateCommitTableReady) {
|
|
463
|
+
this.lastWriteSetDigest = void 0;
|
|
434
464
|
await sql`
|
|
435
465
|
UPDATE "_state_commit"
|
|
436
466
|
SET "current_hash" = 'GENESIS', "prev_hash" = '', "seq" = 0, "msg" = ''
|
|
@@ -457,6 +487,12 @@ var SqliteStateDB = class extends StateDB {
|
|
|
457
487
|
if (this.trustedStorage && typeof this.trustedStorage.destroy === "function") await this.trustedStorage.destroy();
|
|
458
488
|
}
|
|
459
489
|
/**
|
|
490
|
+
* Get the canonical SHA-256 digest of the write-set from the most recently committed `runAsLambda` transaction.
|
|
491
|
+
*/
|
|
492
|
+
getLastWriteSetDigest() {
|
|
493
|
+
return this.lastWriteSetDigest;
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
460
496
|
* Get the underlying Kysely instance (for advanced operations)
|
|
461
497
|
*/
|
|
462
498
|
getKysely() {
|
package/esm/index.d.mts
CHANGED
|
@@ -2,4 +2,8 @@ import { Database, SqliteOperationContext } from "./interfaces.mjs";
|
|
|
2
2
|
import { DurableObjectSqliteDialect, DurableObjectSqliteDialectConfig, IDurableObjectSqlStorage, IDurableObjectSqlStorageCursor, SqliteConfig, createKysely, getRawDatabase } from "./kysely.mjs";
|
|
3
3
|
import SqliteStateDB from "./db.mjs";
|
|
4
4
|
import { ChainStateAuditReport, InvariantSeverity, InvariantViolation, ReconcileAction, ReconcileOptions, ReconcileResult, assertSqliteStateDb, reconcileChainState, verifyChainStateInvariants } from "./reconcile.mjs";
|
|
5
|
-
|
|
5
|
+
import { ApplyTombstoneCleanupOptions, TombstoneBalanceRow, TombstoneCleanupApplyResult, TombstoneCleanupPlan, TombstoneTokenSummary, applyTombstoneBalanceCleanup, compareUpdatedAtTimestamps, computeTombstoneCleanupPlanDigest, extractContextUpdatedAt, isBalanceTimestampNotAfterAccount, planTombstoneBalanceCleanup } from "./tombstone-cleanup.mjs";
|
|
6
|
+
import { _003_token_charter_assets_d_exports } from "./migrations/003-token-charter-assets.mjs";
|
|
7
|
+
import { rollbackMigrations, runMigrations } from "./migrations/index.mjs";
|
|
8
|
+
import { IStateCommitStepInput, IStateWriteRecord, STATE_COMMIT_DOMAIN_V2, WriteSetTracker, canonicalizeStateJson, canonicalizeStateValue, computeStateCommitHash, computeStateWriteSetDigest, formatCanonicalPrimaryKey, isTrackedRawMutatingSql } from "./write-set.mjs";
|
|
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 };
|
package/esm/index.mjs
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import { DurableObjectSqliteDialect, createKysely, getRawDatabase } from "./kysely.mjs";
|
|
2
|
+
import { _003_token_charter_assets_exports } from "./migrations/003-token-charter-assets.mjs";
|
|
3
|
+
import { rollbackMigrations, runMigrations } from "./migrations/index.mjs";
|
|
4
|
+
import { STATE_COMMIT_DOMAIN_V2, WriteSetTracker, canonicalizeStateJson, canonicalizeStateValue, computeStateCommitHash, computeStateWriteSetDigest, formatCanonicalPrimaryKey, isTrackedRawMutatingSql } from "./write-set.mjs";
|
|
2
5
|
import db_default from "./db.mjs";
|
|
3
6
|
import { assertSqliteStateDb, reconcileChainState, verifyChainStateInvariants } from "./reconcile.mjs";
|
|
7
|
+
import { applyTombstoneBalanceCleanup, compareUpdatedAtTimestamps, computeTombstoneCleanupPlanDigest, extractContextUpdatedAt, isBalanceTimestampNotAfterAccount, planTombstoneBalanceCleanup } from "./tombstone-cleanup.mjs";
|
|
4
8
|
|
|
5
9
|
//#region src/index.ts
|
|
6
10
|
var src_default = db_default;
|
|
7
11
|
|
|
8
12
|
//#endregion
|
|
9
|
-
export { DurableObjectSqliteDialect, db_default as SqliteStateDB, assertSqliteStateDb, createKysely, src_default as default, getRawDatabase, reconcileChainState, verifyChainStateInvariants };
|
|
13
|
+
export { DurableObjectSqliteDialect, STATE_COMMIT_DOMAIN_V2, db_default as SqliteStateDB, WriteSetTracker, applyTombstoneBalanceCleanup, assertSqliteStateDb, canonicalizeStateJson, canonicalizeStateValue, compareUpdatedAtTimestamps, computeStateCommitHash, computeStateWriteSetDigest, computeTombstoneCleanupPlanDigest, createKysely, src_default as default, extractContextUpdatedAt, formatCanonicalPrimaryKey, getRawDatabase, isBalanceTimestampNotAfterAccount, isTrackedRawMutatingSql, planTombstoneBalanceCleanup, reconcileChainState, rollbackMigrations, runMigrations, _003_token_charter_assets_exports as tokenCharterAssets, verifyChainStateInvariants };
|
|
@@ -6,9 +6,16 @@ var _002_delegation_extensions_exports = /* @__PURE__ */ __exportAll({
|
|
|
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 sql`ALTER TABLE delegation ADD COLUMN deny TEXT
|
|
11
|
-
await sql`ALTER TABLE delegation ADD COLUMN validUntil INTEGER DEFAULT 0
|
|
17
|
+
await addColumnIfNotExists(db, sql`ALTER TABLE delegation ADD COLUMN deny TEXT`);
|
|
18
|
+
await addColumnIfNotExists(db, sql`ALTER TABLE delegation ADD COLUMN validUntil INTEGER DEFAULT 0`);
|
|
12
19
|
}
|
|
13
20
|
async function down(_db) {}
|
|
14
21
|
|
|
@@ -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 };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { __exportAll } from "../_virtual/rolldown_runtime.mjs";
|
|
2
|
+
import { sql } from "kysely";
|
|
3
|
+
|
|
4
|
+
//#region src/migrations/004-rollup-reserves.ts
|
|
5
|
+
var _004_rollup_reserves_exports = /* @__PURE__ */ __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 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
|
+
export { ROLLUP_RESERVE_COLUMNS, _004_rollup_reserves_exports, down, up };
|
package/esm/migrations/index.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { _001_genesis_exports } from "./001-genesis.mjs";
|
|
2
2
|
import { _002_delegation_extensions_exports } from "./002-delegation-extensions.mjs";
|
|
3
3
|
import { _003_token_charter_assets_exports } from "./003-token-charter-assets.mjs";
|
|
4
|
+
import { _004_rollup_reserves_exports } from "./004-rollup-reserves.mjs";
|
|
4
5
|
import { Migrator } from "kysely";
|
|
5
6
|
|
|
6
7
|
//#region src/migrations/index.ts
|
|
@@ -16,6 +17,12 @@ var InMemoryMigrationProvider = class {
|
|
|
16
17
|
return this.migrations;
|
|
17
18
|
}
|
|
18
19
|
};
|
|
20
|
+
function isRetryableMigrationError(error) {
|
|
21
|
+
const code = error?.code;
|
|
22
|
+
if (code === "SQLITE_BUSY" || code === "SQLITE_LOCKED" || code === 5 || code === 6) return true;
|
|
23
|
+
const msg = (error instanceof Error ? error.message : String(error ?? "")).toLowerCase();
|
|
24
|
+
return msg.includes("database is locked") || msg.includes("sqlite_busy") || msg.includes("sqlite_locked") || msg.includes("already exists") || msg.includes("duplicate column name") || msg.includes("unique constraint failed: kysely_migration") || msg.includes("kysely_migration_lock");
|
|
25
|
+
}
|
|
19
26
|
/**
|
|
20
27
|
* Run all pending migrations
|
|
21
28
|
*
|
|
@@ -29,20 +36,27 @@ var InMemoryMigrationProvider = class {
|
|
|
29
36
|
* ```
|
|
30
37
|
*/
|
|
31
38
|
async function runMigrations(db) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
39
|
+
for (let attempt = 0;; attempt++) {
|
|
40
|
+
const { error, results } = await new Migrator({
|
|
41
|
+
db,
|
|
42
|
+
provider: new InMemoryMigrationProvider({
|
|
43
|
+
"001-genesis": _001_genesis_exports,
|
|
44
|
+
"002-delegation-extensions": _002_delegation_extensions_exports,
|
|
45
|
+
"003-token-charter-assets": _003_token_charter_assets_exports,
|
|
46
|
+
"004-rollup-reserves": _004_rollup_reserves_exports
|
|
47
|
+
})
|
|
48
|
+
}).migrateToLatest();
|
|
49
|
+
if (!error) {
|
|
50
|
+
results?.forEach((it) => {
|
|
51
|
+
if (it.status === "Error") console.error(`Migration "${it.migrationName}" failed`);
|
|
52
|
+
});
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (!isRetryableMigrationError(error) || attempt >= 8) {
|
|
56
|
+
console.error("Migration failed:", error);
|
|
57
|
+
throw error;
|
|
58
|
+
}
|
|
59
|
+
await new Promise((resolve) => setTimeout(resolve, 10 * 2 ** attempt));
|
|
46
60
|
}
|
|
47
61
|
}
|
|
48
62
|
/**
|
|
@@ -55,7 +69,8 @@ async function rollbackMigrations(db) {
|
|
|
55
69
|
provider: new InMemoryMigrationProvider({
|
|
56
70
|
"001-genesis": _001_genesis_exports,
|
|
57
71
|
"002-delegation-extensions": _002_delegation_extensions_exports,
|
|
58
|
-
"003-token-charter-assets": _003_token_charter_assets_exports
|
|
72
|
+
"003-token-charter-assets": _003_token_charter_assets_exports,
|
|
73
|
+
"004-rollup-reserves": _004_rollup_reserves_exports
|
|
59
74
|
})
|
|
60
75
|
});
|
|
61
76
|
let hasMore = true;
|
package/esm/table/account.d.mts
CHANGED
|
@@ -22,19 +22,20 @@ declare class AccountTable extends SqliteTable<IAccountState> {
|
|
|
22
22
|
* @returns true if a cycle would be created
|
|
23
23
|
*/
|
|
24
24
|
private wouldCreateCycle;
|
|
25
|
+
private resolveAddressKey;
|
|
25
26
|
/**
|
|
26
27
|
* Get account with migration chain following
|
|
27
28
|
* When an account has migratedTo set, follows the chain to get the final account
|
|
28
29
|
*/
|
|
29
|
-
_get(address: string, context?: AccountOperationContext): Promise<IAccountState | null>;
|
|
30
|
+
_get(address: string | Record<string, unknown>, context?: AccountOperationContext): Promise<IAccountState | null>;
|
|
30
31
|
/**
|
|
31
32
|
* Create account with address normalization and circular migration detection
|
|
32
33
|
*/
|
|
33
|
-
_create(key: string, attrs?: Partial<IAccountState>, context?: IOperationContext): Promise<IAccountState>;
|
|
34
|
+
_create(key: string | Record<string, unknown>, attrs?: Partial<IAccountState>, context?: IOperationContext): Promise<IAccountState>;
|
|
34
35
|
/**
|
|
35
36
|
* Update account with circular migration detection
|
|
36
37
|
*/
|
|
37
|
-
_update(key: string, updates: Partial<IAccountState>, context?: IOperationContext): Promise<IAccountState>;
|
|
38
|
+
_update(key: string | Record<string, unknown>, updates: Partial<IAccountState>, context?: IOperationContext): Promise<IAccountState>;
|
|
38
39
|
}
|
|
39
40
|
//#endregion
|
|
40
41
|
export { AccountTable as default };
|
package/esm/table/account.mjs
CHANGED
|
@@ -48,13 +48,24 @@ var AccountTable = class extends base_default {
|
|
|
48
48
|
for (const target of targets) if (await checkTarget(target)) return true;
|
|
49
49
|
return false;
|
|
50
50
|
}
|
|
51
|
+
resolveAddressKey(key) {
|
|
52
|
+
if (typeof key === "string") return key.length > 0 ? ensureChecksumAddress(key) : key;
|
|
53
|
+
if (typeof key === "object" && key !== null) {
|
|
54
|
+
const raw = key[this.uniqIndex];
|
|
55
|
+
if (typeof raw === "string" && raw.length > 0) return ensureChecksumAddress(raw);
|
|
56
|
+
return key;
|
|
57
|
+
}
|
|
58
|
+
return key;
|
|
59
|
+
}
|
|
51
60
|
/**
|
|
52
61
|
* Get account with migration chain following
|
|
53
62
|
* When an account has migratedTo set, follows the chain to get the final account
|
|
54
63
|
*/
|
|
55
64
|
async _get(address, context = {}) {
|
|
56
65
|
const { traceMigration = true, ...restContext } = context;
|
|
57
|
-
const
|
|
66
|
+
const normalizedKey = this.resolveAddressKey(address);
|
|
67
|
+
if (typeof normalizedKey !== "string" || normalizedKey.length === 0) return super._get(normalizedKey, restContext);
|
|
68
|
+
const normalizedAddress = normalizedKey;
|
|
58
69
|
const current = await super._get(normalizedAddress, restContext);
|
|
59
70
|
if (current && traceMigration && Array.isArray(current.migratedTo) && current.migratedTo.length) {
|
|
60
71
|
const visitedAddresses = context._visitedAddresses || /* @__PURE__ */ new Set();
|
|
@@ -71,13 +82,19 @@ var AccountTable = class extends base_default {
|
|
|
71
82
|
* Create account with address normalization and circular migration detection
|
|
72
83
|
*/
|
|
73
84
|
async _create(key, attrs = {}, context) {
|
|
74
|
-
const
|
|
75
|
-
|
|
85
|
+
const normalizedAttrs = {
|
|
86
|
+
...attrs,
|
|
87
|
+
balance: attrs.balance ?? "0"
|
|
88
|
+
};
|
|
89
|
+
const normalizedKey = this.resolveAddressKey(key);
|
|
90
|
+
if (typeof normalizedKey !== "string" || normalizedKey.length === 0) return super._create(normalizedKey, normalizedAttrs, context);
|
|
91
|
+
const address = normalizedKey;
|
|
92
|
+
const attrsWithMigration = normalizedAttrs;
|
|
76
93
|
if (attrsWithMigration.migratedTo?.length) {
|
|
77
94
|
if (await this.wouldCreateCycle(address, attrsWithMigration.migratedTo, context)) throw new Error(`Circular migration detected: ${address} -> ${attrsWithMigration.migratedTo.join(", ")}`);
|
|
78
95
|
}
|
|
79
96
|
return super._create(address, {
|
|
80
|
-
...
|
|
97
|
+
...normalizedAttrs,
|
|
81
98
|
[this.uniqIndex]: address
|
|
82
99
|
}, context);
|
|
83
100
|
}
|
|
@@ -85,12 +102,19 @@ var AccountTable = class extends base_default {
|
|
|
85
102
|
* Update account with circular migration detection
|
|
86
103
|
*/
|
|
87
104
|
async _update(key, updates, context) {
|
|
88
|
-
const
|
|
89
|
-
|
|
105
|
+
const normalizedUpdates = { ...updates || {} };
|
|
106
|
+
if ("balance" in normalizedUpdates) {
|
|
107
|
+
if (normalizedUpdates.balance === void 0) delete normalizedUpdates.balance;
|
|
108
|
+
else if (normalizedUpdates.balance === null) normalizedUpdates.balance = "0";
|
|
109
|
+
}
|
|
110
|
+
const normalizedKey = this.resolveAddressKey(key);
|
|
111
|
+
if (typeof normalizedKey !== "string" || normalizedKey.length === 0) return super._update(normalizedKey, normalizedUpdates, context);
|
|
112
|
+
const address = normalizedKey;
|
|
113
|
+
const updatesWithMigration = normalizedUpdates;
|
|
90
114
|
if (updatesWithMigration.migratedTo?.length) {
|
|
91
115
|
if (await this.wouldCreateCycle(address, updatesWithMigration.migratedTo, context)) throw new Error(`Circular migration detected: ${address} -> ${updatesWithMigration.migratedTo.join(", ")}`);
|
|
92
116
|
}
|
|
93
|
-
return super._update(address,
|
|
117
|
+
return super._update(address, normalizedUpdates, context);
|
|
94
118
|
}
|
|
95
119
|
};
|
|
96
120
|
var account_default = AccountTable;
|
package/esm/table/balance.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { formatCanonicalPrimaryKey, getWriteSetTracker } from "../write-set.mjs";
|
|
1
2
|
import base_default from "./base.mjs";
|
|
2
3
|
import { ensureChecksumAddress } from "@ocap/state/lib/states/account";
|
|
3
4
|
|
|
@@ -30,28 +31,37 @@ var BalanceTable = class extends base_default {
|
|
|
30
31
|
return query;
|
|
31
32
|
}
|
|
32
33
|
async _create(key, attrs = {}, context) {
|
|
34
|
+
const normalizedAttrs = {
|
|
35
|
+
...attrs,
|
|
36
|
+
balance: attrs.balance ?? "0"
|
|
37
|
+
};
|
|
33
38
|
const collector = attrs?.[BATCH_COLLECTOR];
|
|
34
39
|
if (collector) {
|
|
35
40
|
const insertAttrs = {
|
|
36
|
-
...
|
|
41
|
+
...normalizedAttrs,
|
|
37
42
|
...typeof key === "object" ? key : {}
|
|
38
43
|
};
|
|
39
44
|
collector.push(this.serializeForDb(insertAttrs));
|
|
40
45
|
return insertAttrs;
|
|
41
46
|
}
|
|
42
|
-
return super._create(key,
|
|
47
|
+
return super._create(key, normalizedAttrs, context);
|
|
43
48
|
}
|
|
44
49
|
async _update(key, updates, context) {
|
|
50
|
+
const normalizedUpdates = { ...updates || {} };
|
|
51
|
+
if ("balance" in normalizedUpdates) {
|
|
52
|
+
if (normalizedUpdates.balance === void 0) delete normalizedUpdates.balance;
|
|
53
|
+
else if (normalizedUpdates.balance === null) normalizedUpdates.balance = "0";
|
|
54
|
+
}
|
|
45
55
|
const collector = updates?.[BATCH_COLLECTOR];
|
|
46
56
|
if (collector) {
|
|
47
57
|
const merged = {
|
|
48
|
-
...
|
|
58
|
+
...normalizedUpdates,
|
|
49
59
|
...typeof key === "object" ? key : {}
|
|
50
60
|
};
|
|
51
61
|
collector.push(this.serializeForDb(merged));
|
|
52
62
|
return merged;
|
|
53
63
|
}
|
|
54
|
-
return super._update(key,
|
|
64
|
+
return super._update(key, normalizedUpdates, context);
|
|
55
65
|
}
|
|
56
66
|
/**
|
|
57
67
|
* Get all token balances for an address
|
|
@@ -122,6 +132,23 @@ var BalanceTable = class extends base_default {
|
|
|
122
132
|
context: eb.ref("excluded.context")
|
|
123
133
|
}))).execute();
|
|
124
134
|
}
|
|
135
|
+
const tracker = getWriteSetTracker(sqlContext?.txn);
|
|
136
|
+
if (tracker) {
|
|
137
|
+
for (const removedToken of removedTokens) tracker.recordDelete("balance", formatCanonicalPrimaryKey(this.uniqIndex, {
|
|
138
|
+
address: normalizedAddress,
|
|
139
|
+
tokenAddress: removedToken
|
|
140
|
+
}));
|
|
141
|
+
for (let idx = 0; idx < pendingOps.length; idx++) {
|
|
142
|
+
const op = pendingOps[idx];
|
|
143
|
+
const deserializedRow = this.deserializeFromDb(batchRows[idx]) ?? {
|
|
144
|
+
address: normalizedAddress,
|
|
145
|
+
tokenAddress: op.tokenAddress,
|
|
146
|
+
balance: tokens[op.tokenAddress] ?? "0",
|
|
147
|
+
context
|
|
148
|
+
};
|
|
149
|
+
tracker.recordPut("balance", formatCanonicalPrimaryKey(this.uniqIndex, op.key), deserializedRow);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
125
152
|
const noopCollector = [];
|
|
126
153
|
for (const op of pendingOps) {
|
|
127
154
|
Object.defineProperty(op.payload, BATCH_COLLECTOR, {
|
package/esm/table/base.d.mts
CHANGED
|
@@ -39,9 +39,11 @@ declare class SqliteTable<T = unknown> extends StateDBTable<T> {
|
|
|
39
39
|
syncBalance: boolean;
|
|
40
40
|
constructor(config: SqliteTableConfig);
|
|
41
41
|
/**
|
|
42
|
-
* Get the Kysely executor (use transaction if available
|
|
42
|
+
* Get the Kysely executor (use unwrapped transaction if available so ORM queries
|
|
43
|
+
* record structured row writes rather than raw-DML fallback)
|
|
43
44
|
*/
|
|
44
45
|
protected getExecutor(context?: SqliteOperationContext): Kysely<Database> | Transaction<Database>;
|
|
46
|
+
protected recordCommittedRowPut(context: SqliteOperationContext | undefined, lookupKey: unknown, deserializedRow: Record<string, unknown>): void;
|
|
45
47
|
/**
|
|
46
48
|
* Generate primary key from document
|
|
47
49
|
* Handles both simple string keys and composite keys
|
|
@@ -51,6 +53,11 @@ declare class SqliteTable<T = unknown> extends StateDBTable<T> {
|
|
|
51
53
|
* Convert a key to a query object
|
|
52
54
|
*/
|
|
53
55
|
protected formatKeyToQuery(key: unknown): Record<string, unknown>;
|
|
56
|
+
/**
|
|
57
|
+
* Check that a primary-key column value is a non-empty string or finite number.
|
|
58
|
+
* Prevents `undefined`/`null`/`''` key fields from silently dropping SQL WHERE clauses (#1438).
|
|
59
|
+
*/
|
|
60
|
+
protected isValidPrimaryKeyValue(v: unknown): boolean;
|
|
54
61
|
/**
|
|
55
62
|
* Map column name for reserved keywords
|
|
56
63
|
*/
|