@ocap/statedb-sqlite 1.31.4 → 1.31.5

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 CHANGED
@@ -94,6 +94,7 @@ declare class SqliteStateDB extends StateDB implements IStateDB {
94
94
  }>;
95
95
  private startWalCheckpointWorker;
96
96
  private ensureStateCommitTable;
97
+ private hasPopulatedStateTables;
97
98
  private ensureTrustedStorageInitialized;
98
99
  /**
99
100
  * Get the current and previous state commit hash tracked in SQLite StateDB.
package/esm/db.mjs CHANGED
@@ -216,15 +216,18 @@ var SqliteStateDB = class extends StateDB {
216
216
  */
217
217
  async initialize() {
218
218
  this.closed = false;
219
+ const allowTrustedRecovery = !this.config.readonly;
219
220
  if (this.initialized) {
220
- if (this.trustedStorage && !this.trustedInitialized) await this.ensureTrustedStorageInitialized(true);
221
+ if (this.trustedStorage && !this.trustedInitialized) await this.ensureTrustedStorageInitialized(allowTrustedRecovery);
221
222
  this.startWalCheckpointWorker();
222
223
  return;
223
224
  }
224
- await runMigrations(this.db);
225
- await this.assertRollupReserveColumns();
225
+ if (!this.config.readonly) {
226
+ await runMigrations(this.db);
227
+ await this.assertRollupReserveColumns();
228
+ }
226
229
  this.initialized = true;
227
- if (this.trustedStorage) await this.ensureTrustedStorageInitialized(true);
230
+ if (this.trustedStorage) await this.ensureTrustedStorageInitialized(allowTrustedRecovery);
228
231
  this.startWalCheckpointWorker();
229
232
  }
230
233
  /**
@@ -285,7 +288,7 @@ var SqliteStateDB = class extends StateDB {
285
288
  if (typeof this.walCheckpointTimer.unref === "function") this.walCheckpointTimer.unref();
286
289
  }
287
290
  async ensureStateCommitTable() {
288
- if (this.stateCommitTableReady) return;
291
+ if (this.stateCommitTableReady || this.config.readonly) return;
289
292
  await sql`
290
293
  CREATE TABLE IF NOT EXISTS "_state_commit" (
291
294
  "id" INTEGER PRIMARY KEY CHECK ("id" = 1),
@@ -301,15 +304,42 @@ var SqliteStateDB = class extends StateDB {
301
304
  `.execute(this.db);
302
305
  this.stateCommitTableReady = true;
303
306
  }
307
+ async hasPopulatedStateTables() {
308
+ for (const table of [
309
+ "chain",
310
+ "account",
311
+ "balance",
312
+ "token",
313
+ "tx",
314
+ "asset",
315
+ "stake",
316
+ "factory",
317
+ "delegation",
318
+ "rollup",
319
+ "rollupBlock",
320
+ "evidence",
321
+ "tokenFactory"
322
+ ]) try {
323
+ if ((await sql`SELECT 1 AS present FROM ${sql.table(table)} LIMIT 1`.execute(this.db)).rows.length > 0) return true;
324
+ } catch {}
325
+ return false;
326
+ }
304
327
  async ensureTrustedStorageInitialized(allowForwardRecovery = false) {
305
328
  if (!this.trustedStorage || this.trustedInitialized) return;
306
329
  await this.ensureStateCommitTable();
307
- if (typeof this.trustedStorage.initStorage === "function") await this.trustedStorage.initStorage();
330
+ if (!this.config.readonly && typeof this.trustedStorage.initStorage === "function") await this.trustedStorage.initStorage();
308
331
  if (!await this.trustedStorage.getHash()) {
309
- const { currentHash } = await this.getCommitHash();
310
- const newTrustedHash = await this.trustedStorage.createHash(currentHash);
311
- await this.trustedStorage.writeHash(newTrustedHash);
312
- } else await this.verifyTrustedHashInternal(allowForwardRecovery);
332
+ const { currentHash, prevHash, seq = 0 } = await this.getCommitHash();
333
+ if (!(seq === 0 && currentHash === "GENESIS" && !prevHash && !await this.hasPopulatedStateTables()) && !this.config.allowTrustedBootstrap) {
334
+ const err = /* @__PURE__ */ new Error("Trusted hash verification failed");
335
+ err.code = "INVALID_HASH";
336
+ throw err;
337
+ }
338
+ if (!this.config.readonly) {
339
+ const newTrustedHash = await this.trustedStorage.createHash(currentHash);
340
+ await this.trustedStorage.writeHash(newTrustedHash);
341
+ }
342
+ } else await this.verifyTrustedHashInternal(allowForwardRecovery && !this.config.readonly);
313
343
  this.trustedInitialized = true;
314
344
  }
315
345
  /**
@@ -324,16 +354,29 @@ var SqliteStateDB = class extends StateDB {
324
354
  * checkpoints and self-verifying segments.
325
355
  */
326
356
  async getCommitHash(executor = this.db) {
327
- if (!this.stateCommitTableReady) await this.ensureStateCommitTable();
328
- const row = (await sql`
329
- SELECT "current_hash", "prev_hash", "seq", "msg" FROM "_state_commit" WHERE "id" = 1
330
- `.execute(executor)).rows[0];
331
- return {
332
- currentHash: row?.current_hash || "GENESIS",
333
- prevHash: row?.prev_hash || "",
334
- seq: Number(row?.seq || 0),
335
- msg: row?.msg || ""
336
- };
357
+ if (!this.stateCommitTableReady && !this.config.readonly) await this.ensureStateCommitTable();
358
+ try {
359
+ const row = (await sql`
360
+ SELECT "current_hash", "prev_hash", "seq", "msg" FROM "_state_commit" WHERE "id" = 1
361
+ `.execute(executor)).rows[0];
362
+ return {
363
+ currentHash: row?.current_hash || "GENESIS",
364
+ prevHash: row?.prev_hash || "",
365
+ seq: Number(row?.seq || 0),
366
+ msg: row?.msg || ""
367
+ };
368
+ } catch (err) {
369
+ if (this.config.readonly) {
370
+ const msg = err instanceof Error ? err.message : String(err);
371
+ if (/no such table/i.test(msg)) return {
372
+ currentHash: "GENESIS",
373
+ prevHash: "",
374
+ seq: 0,
375
+ msg: ""
376
+ };
377
+ }
378
+ throw err;
379
+ }
337
380
  }
338
381
  async verifyTrustedHashInternal(allowForwardRecovery = false) {
339
382
  if (!this.trustedStorage) return;
@@ -354,9 +397,10 @@ var SqliteStateDB = class extends StateDB {
354
397
  */
355
398
  async verifyTrustedHash() {
356
399
  if (!this.trustedStorage) return;
400
+ const allowRecovery = !this.config.readonly;
357
401
  await this.enqueueCommit(async () => {
358
- await this.ensureTrustedStorageInitialized(true);
359
- await this.verifyTrustedHashInternal(true);
402
+ await this.ensureTrustedStorageInitialized(allowRecovery);
403
+ await this.verifyTrustedHashInternal(allowRecovery);
360
404
  });
361
405
  }
362
406
  enqueueCommit(task) {
@@ -377,6 +421,7 @@ var SqliteStateDB = class extends StateDB {
377
421
  return this.executeRunAsLambda(fn, options);
378
422
  }
379
423
  async executeRunAsLambda(fn, options = {}) {
424
+ if (this.config.readonly) throw new Error("Cannot execute runAsLambda on a SqliteStateDB opened in readonly mode");
380
425
  const { retryLimit = 5, verifyHash = false, commitMessage } = options;
381
426
  if (!this.stateCommitTableReady) await this.ensureStateCommitTable();
382
427
  if (this.trustedStorage) {
@@ -453,6 +498,7 @@ var SqliteStateDB = class extends StateDB {
453
498
  * Reset all tables (for testing)
454
499
  */
455
500
  async reset() {
501
+ if (this.config.readonly) throw new Error("Cannot reset a SqliteStateDB opened in readonly mode");
456
502
  const tables = Object.keys(this.readyMarks);
457
503
  for (const table of tables) {
458
504
  const tableInstance = this[table];
@@ -483,7 +529,7 @@ var SqliteStateDB = class extends StateDB {
483
529
  if (this.walCheckpointPromise) await this.walCheckpointPromise.catch(() => void 0);
484
530
  await this.commitQueue;
485
531
  await this.db.destroy();
486
- if (this.trustedStorage && typeof this.trustedStorage.destroy === "function") await this.trustedStorage.destroy();
532
+ if (this.trustedStorage && typeof this.trustedStorage.destroy === "function" && this.trustedStorage.filename !== ":memory:") await this.trustedStorage.destroy();
487
533
  }
488
534
  /**
489
535
  * Get the canonical SHA-256 digest of the write-set from the most recently committed `runAsLambda` transaction.
package/esm/kysely.d.mts CHANGED
@@ -59,6 +59,11 @@ interface SqliteConfig {
59
59
  dialect?: Dialect;
60
60
  /** Optional trusted storage instance for state commit hash verification */
61
61
  trustedStorage?: ITrustedStorage;
62
+ /**
63
+ * Explicitly allow bootstrapping an empty `trustedStorage` onto an already-populated StateDB
64
+ * (only set by operator `--force` recovery tools; false by default so wiped `trusted.sqlite` fails closed).
65
+ */
66
+ allowTrustedBootstrap?: boolean;
62
67
  /** Open database in readonly mode */
63
68
  readonly?: boolean;
64
69
  /** Throw error if file doesn't exist (default: false, creates new file) */
package/esm/kysely.mjs CHANGED
@@ -1545,7 +1545,7 @@ function createKysely(config) {
1545
1545
  const { BunSqliteDialect } = bunReq("kysely-bun-sqlite");
1546
1546
  const { Database: BunDatabase } = bunReq("bun:sqlite");
1547
1547
  const sqlite$1 = new BunDatabase(filename, {
1548
- create: config.create !== false,
1548
+ create: config.readonly && filename !== ":memory:" ? false : config.create !== false,
1549
1549
  readonly: config.readonly === true
1550
1550
  });
1551
1551
  if (config.walMode !== false && !config.readonly && filename !== ":memory:") {
@@ -1560,7 +1560,7 @@ function createKysely(config) {
1560
1560
  }
1561
1561
  const sqlite = new (__require("better-sqlite3"))(filename, {
1562
1562
  readonly: config.readonly === true,
1563
- fileMustExist: config.create === false
1563
+ fileMustExist: config.readonly === true || config.create === false
1564
1564
  });
1565
1565
  if (config.walMode !== false && !config.readonly && filename !== ":memory:") {
1566
1566
  sqlite.pragma("journal_mode = WAL");
package/esm/reconcile.mjs CHANGED
@@ -494,6 +494,7 @@ async function reconcileChainState(db, options = {}) {
494
494
  assertSqliteStateDb(db);
495
495
  const dryRun = options.dryRun !== false;
496
496
  if (!dryRun && options.allowProtocolBypass !== true) throw new Error("reconcileChainState({ dryRun: false }) writes directly to SQLite and bypasses protocol validation; pass allowProtocolBypass: true to confirm");
497
+ if (!dryRun && db.trustedStorage) await db.verifyTrustedHash();
497
498
  const beforeReport = await verifyChainStateInvariants(db, { ledger: options.ledger });
498
499
  const actions = [];
499
500
  if (beforeReport.valid) return {
@@ -503,7 +504,7 @@ async function reconcileChainState(db, options = {}) {
503
504
  afterReport: beforeReport,
504
505
  actions
505
506
  };
506
- const executeRepairs = async (executor) => {
507
+ const executeRepairs = async (executor, applyWrites, targetActions) => {
507
508
  const allBalances = await executor.selectFrom("balance").selectAll().execute();
508
509
  for (const row of allBalances) {
509
510
  const addr = String(row.address || "");
@@ -511,7 +512,7 @@ async function reconcileChainState(db, options = {}) {
511
512
  const balStr = String(row.balance ?? "0");
512
513
  const checksumAddr = ensureChecksumAddress(addr);
513
514
  if (CANONICAL_UINT_REGEX.test(balStr) && new BN(balStr, 10).isZero()) {
514
- actions.push({
515
+ targetActions.push({
515
516
  code: "I-BAL-3",
516
517
  table: "balance",
517
518
  key: `${addr}:${tokAddr}`,
@@ -523,13 +524,13 @@ async function reconcileChainState(db, options = {}) {
523
524
  },
524
525
  after: null
525
526
  });
526
- if (!dryRun) await executor.deleteFrom("balance").where("address", "=", addr).where("tokenAddress", "=", tokAddr).execute();
527
+ if (applyWrites) await executor.deleteFrom("balance").where("address", "=", addr).where("tokenAddress", "=", tokAddr).execute();
527
528
  continue;
528
529
  }
529
530
  if (checksumAddr !== addr && CANONICAL_UINT_REGEX.test(balStr)) {
530
531
  const existingCanonical = await executor.selectFrom("balance").selectAll().where("address", "=", checksumAddr).where("tokenAddress", "=", tokAddr).executeTakeFirst();
531
532
  const mergedBalance = existingCanonical && CANONICAL_UINT_REGEX.test(String(existingCanonical.balance)) ? new BN(String(existingCanonical.balance), 10).add(new BN(balStr, 10)).toString(10) : balStr;
532
- actions.push({
533
+ targetActions.push({
533
534
  code: "I-BAL-1",
534
535
  table: "balance",
535
536
  key: `${addr}:${tokAddr}`,
@@ -545,7 +546,7 @@ async function reconcileChainState(db, options = {}) {
545
546
  balance: mergedBalance
546
547
  }
547
548
  });
548
- if (!dryRun) {
549
+ if (applyWrites) {
549
550
  await executor.deleteFrom("balance").where("address", "=", addr).where("tokenAddress", "=", tokAddr).execute();
550
551
  await executor.insertInto("balance").values({
551
552
  address: checksumAddr,
@@ -600,7 +601,7 @@ async function reconcileChainState(db, options = {}) {
600
601
  if (!nextMigratedFrom.includes(acc.address)) {
601
602
  const updatedFrom = [...nextMigratedFrom, acc.address];
602
603
  nextAcc.migratedFrom = JSON.stringify(updatedFrom);
603
- actions.push({
604
+ targetActions.push({
604
605
  code: "I-MIG-1",
605
606
  table: "account",
606
607
  key: nextHop,
@@ -608,7 +609,7 @@ async function reconcileChainState(db, options = {}) {
608
609
  before: nextMigratedFrom,
609
610
  after: updatedFrom
610
611
  });
611
- if (!dryRun) await executor.updateTable("account").set({ migratedFrom: JSON.stringify(updatedFrom) }).where("address", "=", nextHop).execute();
612
+ if (applyWrites) await executor.updateTable("account").set({ migratedFrom: JSON.stringify(updatedFrom) }).where("address", "=", nextHop).execute();
612
613
  }
613
614
  }
614
615
  const zombieRows = await executor.selectFrom("balance").selectAll().where("address", "=", acc.address).execute();
@@ -627,7 +628,7 @@ async function reconcileChainState(db, options = {}) {
627
628
  const forwardPortion = zBal.sub(zombiePortion);
628
629
  if (zombiePortion.gt(new BN(0))) runningAccountedSupply.set(tokAddr, accountedSupply.sub(zombiePortion));
629
630
  if (forwardPortion.isZero()) {
630
- actions.push({
631
+ targetActions.push({
631
632
  code: "I-BAL-2",
632
633
  table: "balance",
633
634
  key: `${acc.address}:${tokAddr}`,
@@ -639,12 +640,12 @@ async function reconcileChainState(db, options = {}) {
639
640
  },
640
641
  after: null
641
642
  });
642
- if (!dryRun) await executor.deleteFrom("balance").where("address", "=", acc.address).where("tokenAddress", "=", tokAddr).execute();
643
+ if (applyWrites) await executor.deleteFrom("balance").where("address", "=", acc.address).where("tokenAddress", "=", tokAddr).execute();
643
644
  } else {
644
645
  const nextTermBalBn = termBal.add(forwardPortion);
645
646
  const nextTermBal = nextTermBalBn.toString(10);
646
647
  liveBalanceMap.set(termBalKey, nextTermBalBn);
647
- actions.push({
648
+ targetActions.push({
648
649
  code: "I-BAL-2",
649
650
  table: "balance",
650
651
  key: `${acc.address}:${tokAddr}`,
@@ -668,7 +669,7 @@ async function reconcileChainState(db, options = {}) {
668
669
  deletedDuplicateZombiePortion: zombiePortion.toString(10)
669
670
  }
670
671
  });
671
- if (!dryRun) {
672
+ if (applyWrites) {
672
673
  await executor.deleteFrom("balance").where("address", "=", acc.address).where("tokenAddress", "=", tokAddr).execute();
673
674
  await executor.insertInto("balance").values({
674
675
  address: terminalAddr,
@@ -681,7 +682,7 @@ async function reconcileChainState(db, options = {}) {
681
682
  }
682
683
  const orphanAssets = await executor.selectFrom("asset").selectAll().where("owner", "=", acc.address).execute();
683
684
  for (const ast of orphanAssets) {
684
- actions.push({
685
+ targetActions.push({
685
686
  code: "I-AST-1",
686
687
  table: "asset",
687
688
  key: ast.address,
@@ -689,7 +690,7 @@ async function reconcileChainState(db, options = {}) {
689
690
  before: { owner: acc.address },
690
691
  after: { owner: terminalAddr }
691
692
  });
692
- if (!dryRun) await executor.updateTable("asset").set({ owner: terminalAddr }).where("address", "=", ast.address).execute();
693
+ if (applyWrites) await executor.updateTable("asset").set({ owner: terminalAddr }).where("address", "=", ast.address).execute();
693
694
  }
694
695
  }
695
696
  const stakes = await executor.selectFrom("stake").selectAll().execute();
@@ -709,7 +710,7 @@ async function reconcileChainState(db, options = {}) {
709
710
  return Boolean(a && a.owner === stk.address);
710
711
  });
711
712
  if (validActive.length !== activeAssets.length || validRevoked.length !== revokedAssets.length) {
712
- actions.push({
713
+ targetActions.push({
713
714
  code: "I-STK-1",
714
715
  table: "stake",
715
716
  key: stk.address,
@@ -723,7 +724,7 @@ async function reconcileChainState(db, options = {}) {
723
724
  revokedAssets: validRevoked
724
725
  }
725
726
  });
726
- if (!dryRun) await executor.updateTable("stake").set({
727
+ if (applyWrites) await executor.updateTable("stake").set({
727
728
  assets: JSON.stringify(validActive),
728
729
  revokedAssets: JSON.stringify(validRevoked)
729
730
  }).where("address", "=", stk.address).execute();
@@ -746,7 +747,7 @@ async function reconcileChainState(db, options = {}) {
746
747
  name: tok.name
747
748
  }
748
749
  };
749
- actions.push({
750
+ targetActions.push({
750
751
  code: "I-TOK-4",
751
752
  table: "asset",
752
753
  key: charterAddr,
@@ -754,7 +755,7 @@ async function reconcileChainState(db, options = {}) {
754
755
  before: charterData,
755
756
  after: repairedData
756
757
  });
757
- if (!dryRun) await executor.updateTable("asset").set({ data: JSON.stringify(repairedData) }).where("address", "=", charterAddr).execute();
758
+ if (applyWrites) await executor.updateTable("asset").set({ data: JSON.stringify(repairedData) }).where("address", "=", charterAddr).execute();
758
759
  }
759
760
  }
760
761
  } catch {}
@@ -776,7 +777,7 @@ async function reconcileChainState(db, options = {}) {
776
777
  for (const tok of tokens) {
777
778
  const expected = (sums.get(tok.address) || new BN(0)).toString(10);
778
779
  if (String(tok.totalSupply ?? "0") !== expected) {
779
- actions.push({
780
+ targetActions.push({
780
781
  code: "I-TOK-1",
781
782
  table: "token",
782
783
  key: tok.address,
@@ -784,7 +785,7 @@ async function reconcileChainState(db, options = {}) {
784
785
  before: { totalSupply: tok.totalSupply },
785
786
  after: { totalSupply: expected }
786
787
  });
787
- if (!dryRun) await executor.updateTable("token").set({ totalSupply: expected }).where("address", "=", tok.address).execute();
788
+ if (applyWrites) await executor.updateTable("token").set({ totalSupply: expected }).where("address", "=", tok.address).execute();
788
789
  }
789
790
  }
790
791
  const tokenFactories = await executor.selectFrom("tokenFactory").selectAll().execute();
@@ -792,7 +793,7 @@ async function reconcileChainState(db, options = {}) {
792
793
  if (Number(tf.status || 0) !== 0) continue;
793
794
  const expected = (sums.get(tf.tokenAddress) || new BN(0)).toString(10);
794
795
  if (String(tf.currentSupply ?? "0") !== expected) {
795
- actions.push({
796
+ targetActions.push({
796
797
  code: "I-TOK-3",
797
798
  table: "tokenFactory",
798
799
  key: tf.address,
@@ -800,13 +801,13 @@ async function reconcileChainState(db, options = {}) {
800
801
  before: { currentSupply: tf.currentSupply },
801
802
  after: { currentSupply: expected }
802
803
  });
803
- if (!dryRun) await executor.updateTable("tokenFactory").set({ currentSupply: expected }).where("address", "=", tf.address).execute();
804
+ if (applyWrites) await executor.updateTable("tokenFactory").set({ currentSupply: expected }).where("address", "=", tf.address).execute();
804
805
  }
805
806
  }
806
807
  }
807
808
  };
808
809
  if (dryRun) {
809
- await executeRepairs(db.getKysely());
810
+ await executeRepairs(db.getKysely(), false, actions);
810
811
  return {
811
812
  dryRun: true,
812
813
  reconciled: false,
@@ -815,9 +816,22 @@ async function reconcileChainState(db, options = {}) {
815
816
  actions
816
817
  };
817
818
  }
819
+ const previewActions = [];
820
+ await executeRepairs(db.getKysely(), false, previewActions);
821
+ if (previewActions.length === 0) return {
822
+ dryRun: false,
823
+ reconciled: beforeReport.valid,
824
+ beforeReport,
825
+ afterReport: beforeReport,
826
+ actions: previewActions
827
+ };
818
828
  await db.runAsLambda(async (txn) => {
819
- await executeRepairs(txn);
820
- }, { commitMessage: "reconcile_chain_state" });
829
+ actions.length = 0;
830
+ await executeRepairs(txn, true, actions);
831
+ }, {
832
+ commitMessage: "reconcile_chain_state",
833
+ verifyHash: true
834
+ });
821
835
  await sql`PRAGMA wal_checkpoint(PASSIVE)`.execute(db.getKysely()).catch(() => {});
822
836
  const afterReport = await verifyChainStateInvariants(db, { ledger: options.ledger });
823
837
  return {
@@ -92,7 +92,7 @@ var BalanceTable = class extends base_default {
92
92
  const tokenBalances = await this.getBalance(normalizedAddress, ctx);
93
93
  const removedTokens = Object.keys(tokenBalances).filter((token) => !(token in tokens));
94
94
  const updatedTokens = Object.keys(tokens).filter((token) => tokenBalances[token] && tokenBalances[token] !== "0" || tokens[token] && tokens[token] !== "0").filter((token) => tokenBalances[token] !== tokens[token]);
95
- if (updatedTokens.length === 0 && removedTokens.length === 0) return { ...tokens };
95
+ if (updatedTokens.length === 0 && removedTokens.length === 0) return { ...tokenBalances };
96
96
  const batchRows = [];
97
97
  const pendingOps = [];
98
98
  for (const tokenAddress of updatedTokens) {
@@ -124,7 +124,10 @@ var BalanceTable = class extends base_default {
124
124
  }
125
125
  const sqlContext = ctx;
126
126
  const executor = this.getExecutor(sqlContext);
127
- if (removedTokens.length > 0) await executor.deleteFrom("balance").where("address", "=", normalizedAddress).where("tokenAddress", "in", removedTokens).execute();
127
+ if (removedTokens.length > 0) {
128
+ await executor.deleteFrom("balance").where("address", "=", normalizedAddress).where("tokenAddress", "in", removedTokens).execute();
129
+ for (const removedToken of removedTokens) delete tokenBalances[removedToken];
130
+ }
128
131
  if (batchRows.length > 0) for (let i = 0; i < batchRows.length; i += UPSERT_BATCH_SIZE) {
129
132
  const batch = batchRows.slice(i, i + UPSERT_BATCH_SIZE);
130
133
  await executor.insertInto("balance").values(batch).onConflict((oc) => oc.columns(["address", "tokenAddress"]).doUpdateSet((eb) => ({
@@ -160,7 +163,7 @@ var BalanceTable = class extends base_default {
160
163
  else await this.create(op.key, op.payload, ctx);
161
164
  tokenBalances[op.tokenAddress] = tokens[op.tokenAddress];
162
165
  }
163
- return { ...tokens };
166
+ return { ...tokenBalances };
164
167
  }
165
168
  };
166
169
  var balance_default = BalanceTable;
@@ -150,15 +150,19 @@ var SqliteTable = class SqliteTable extends StateDBTable {
150
150
  const insertedRow = await executor.insertInto(this.name).values(serialized).returningAll().executeTakeFirst();
151
151
  const deserializedInserted = this.deserializeFromDb(insertedRow ?? serialized) ?? insertAttrs;
152
152
  this.recordCommittedRowPut(sqlContext, lookupKey, deserializedInserted);
153
- if (this.syncBalance && attrs.tokens !== void 0) {
154
- const rawTokens = attrs.tokens || {};
155
- insertAttrs.tokens = Object.keys(rawTokens).length ? await this.balanceTable.updateBalance({
156
- address: insertAttrs.address || (typeof key === "string" ? key : ""),
157
- tokens: rawTokens,
158
- context: attrs.context
159
- }, context) : {};
153
+ const created = { ...deserializedInserted };
154
+ if (this.syncBalance) {
155
+ const balanceAddr = created.address || insertAttrs.address || (typeof key === "string" ? key : "");
156
+ if (attrs.tokens !== void 0) {
157
+ const rawTokens = attrs.tokens || {};
158
+ created.tokens = Object.keys(rawTokens).length ? await this.balanceTable.updateBalance({
159
+ address: balanceAddr,
160
+ tokens: rawTokens,
161
+ context: attrs.context
162
+ }, context) : await this.balanceTable.getBalance(balanceAddr, context);
163
+ } else created.tokens = await this.balanceTable.getBalance(balanceAddr, context);
160
164
  }
161
- return insertAttrs;
165
+ return created;
162
166
  }
163
167
  async _get(key, context) {
164
168
  if (!key) return null;
@@ -225,10 +229,7 @@ var SqliteTable = class SqliteTable extends StateDBTable {
225
229
  if (!updatedRow) throw new Error(`${this.name} does not exist: ${JSON.stringify(key)}`);
226
230
  const deserialized = this.deserializeFromDb(updatedRow);
227
231
  this.recordCommittedRowPut(sqlContext, lookupKey, deserialized);
228
- merged = {
229
- ...deserialized,
230
- ...updateAttrs
231
- };
232
+ merged = { ...deserialized };
232
233
  } else {
233
234
  let qb = executor.selectFrom(this.name).selectAll();
234
235
  for (const [k, v] of queryEntries) qb = qb.where(k, "=", v);
@@ -197,7 +197,10 @@ async function applyTombstoneBalanceCleanup(dbOrKysely, options) {
197
197
  planDigest: freshPlan.planDigest
198
198
  };
199
199
  };
200
- if (isSqliteStateDB(dbOrKysely)) return dbOrKysely.runAsLambda((txn) => executeInTxn(txn));
200
+ if (isSqliteStateDB(dbOrKysely)) return dbOrKysely.runAsLambda((txn) => executeInTxn(txn), {
201
+ verifyHash: true,
202
+ commitMessage: "cleanup_tombstone_balances"
203
+ });
201
204
  const kysely = resolveKyselyExecutor(dbOrKysely);
202
205
  if ("transaction" in kysely && typeof kysely.transaction === "function") return kysely.transaction().execute((txn) => executeInTxn(txn));
203
206
  return executeInTxn(kysely);
package/lib/db.cjs CHANGED
@@ -218,15 +218,18 @@ var SqliteStateDB = class extends _ocap_statedb.StateDB {
218
218
  */
219
219
  async initialize() {
220
220
  this.closed = false;
221
+ const allowTrustedRecovery = !this.config.readonly;
221
222
  if (this.initialized) {
222
- if (this.trustedStorage && !this.trustedInitialized) await this.ensureTrustedStorageInitialized(true);
223
+ if (this.trustedStorage && !this.trustedInitialized) await this.ensureTrustedStorageInitialized(allowTrustedRecovery);
223
224
  this.startWalCheckpointWorker();
224
225
  return;
225
226
  }
226
- await require_migrations_index.runMigrations(this.db);
227
- await this.assertRollupReserveColumns();
227
+ if (!this.config.readonly) {
228
+ await require_migrations_index.runMigrations(this.db);
229
+ await this.assertRollupReserveColumns();
230
+ }
228
231
  this.initialized = true;
229
- if (this.trustedStorage) await this.ensureTrustedStorageInitialized(true);
232
+ if (this.trustedStorage) await this.ensureTrustedStorageInitialized(allowTrustedRecovery);
230
233
  this.startWalCheckpointWorker();
231
234
  }
232
235
  /**
@@ -287,7 +290,7 @@ var SqliteStateDB = class extends _ocap_statedb.StateDB {
287
290
  if (typeof this.walCheckpointTimer.unref === "function") this.walCheckpointTimer.unref();
288
291
  }
289
292
  async ensureStateCommitTable() {
290
- if (this.stateCommitTableReady) return;
293
+ if (this.stateCommitTableReady || this.config.readonly) return;
291
294
  await kysely.sql`
292
295
  CREATE TABLE IF NOT EXISTS "_state_commit" (
293
296
  "id" INTEGER PRIMARY KEY CHECK ("id" = 1),
@@ -303,15 +306,42 @@ var SqliteStateDB = class extends _ocap_statedb.StateDB {
303
306
  `.execute(this.db);
304
307
  this.stateCommitTableReady = true;
305
308
  }
309
+ async hasPopulatedStateTables() {
310
+ for (const table of [
311
+ "chain",
312
+ "account",
313
+ "balance",
314
+ "token",
315
+ "tx",
316
+ "asset",
317
+ "stake",
318
+ "factory",
319
+ "delegation",
320
+ "rollup",
321
+ "rollupBlock",
322
+ "evidence",
323
+ "tokenFactory"
324
+ ]) try {
325
+ if ((await kysely.sql`SELECT 1 AS present FROM ${kysely.sql.table(table)} LIMIT 1`.execute(this.db)).rows.length > 0) return true;
326
+ } catch {}
327
+ return false;
328
+ }
306
329
  async ensureTrustedStorageInitialized(allowForwardRecovery = false) {
307
330
  if (!this.trustedStorage || this.trustedInitialized) return;
308
331
  await this.ensureStateCommitTable();
309
- if (typeof this.trustedStorage.initStorage === "function") await this.trustedStorage.initStorage();
332
+ if (!this.config.readonly && typeof this.trustedStorage.initStorage === "function") await this.trustedStorage.initStorage();
310
333
  if (!await this.trustedStorage.getHash()) {
311
- const { currentHash } = await this.getCommitHash();
312
- const newTrustedHash = await this.trustedStorage.createHash(currentHash);
313
- await this.trustedStorage.writeHash(newTrustedHash);
314
- } else await this.verifyTrustedHashInternal(allowForwardRecovery);
334
+ const { currentHash, prevHash, seq = 0 } = await this.getCommitHash();
335
+ if (!(seq === 0 && currentHash === "GENESIS" && !prevHash && !await this.hasPopulatedStateTables()) && !this.config.allowTrustedBootstrap) {
336
+ const err = /* @__PURE__ */ new Error("Trusted hash verification failed");
337
+ err.code = "INVALID_HASH";
338
+ throw err;
339
+ }
340
+ if (!this.config.readonly) {
341
+ const newTrustedHash = await this.trustedStorage.createHash(currentHash);
342
+ await this.trustedStorage.writeHash(newTrustedHash);
343
+ }
344
+ } else await this.verifyTrustedHashInternal(allowForwardRecovery && !this.config.readonly);
315
345
  this.trustedInitialized = true;
316
346
  }
317
347
  /**
@@ -326,16 +356,29 @@ var SqliteStateDB = class extends _ocap_statedb.StateDB {
326
356
  * checkpoints and self-verifying segments.
327
357
  */
328
358
  async getCommitHash(executor = this.db) {
329
- if (!this.stateCommitTableReady) await this.ensureStateCommitTable();
330
- const row = (await kysely.sql`
331
- SELECT "current_hash", "prev_hash", "seq", "msg" FROM "_state_commit" WHERE "id" = 1
332
- `.execute(executor)).rows[0];
333
- return {
334
- currentHash: row?.current_hash || "GENESIS",
335
- prevHash: row?.prev_hash || "",
336
- seq: Number(row?.seq || 0),
337
- msg: row?.msg || ""
338
- };
359
+ if (!this.stateCommitTableReady && !this.config.readonly) await this.ensureStateCommitTable();
360
+ try {
361
+ const row = (await kysely.sql`
362
+ SELECT "current_hash", "prev_hash", "seq", "msg" FROM "_state_commit" WHERE "id" = 1
363
+ `.execute(executor)).rows[0];
364
+ return {
365
+ currentHash: row?.current_hash || "GENESIS",
366
+ prevHash: row?.prev_hash || "",
367
+ seq: Number(row?.seq || 0),
368
+ msg: row?.msg || ""
369
+ };
370
+ } catch (err) {
371
+ if (this.config.readonly) {
372
+ const msg = err instanceof Error ? err.message : String(err);
373
+ if (/no such table/i.test(msg)) return {
374
+ currentHash: "GENESIS",
375
+ prevHash: "",
376
+ seq: 0,
377
+ msg: ""
378
+ };
379
+ }
380
+ throw err;
381
+ }
339
382
  }
340
383
  async verifyTrustedHashInternal(allowForwardRecovery = false) {
341
384
  if (!this.trustedStorage) return;
@@ -356,9 +399,10 @@ var SqliteStateDB = class extends _ocap_statedb.StateDB {
356
399
  */
357
400
  async verifyTrustedHash() {
358
401
  if (!this.trustedStorage) return;
402
+ const allowRecovery = !this.config.readonly;
359
403
  await this.enqueueCommit(async () => {
360
- await this.ensureTrustedStorageInitialized(true);
361
- await this.verifyTrustedHashInternal(true);
404
+ await this.ensureTrustedStorageInitialized(allowRecovery);
405
+ await this.verifyTrustedHashInternal(allowRecovery);
362
406
  });
363
407
  }
364
408
  enqueueCommit(task) {
@@ -379,6 +423,7 @@ var SqliteStateDB = class extends _ocap_statedb.StateDB {
379
423
  return this.executeRunAsLambda(fn, options);
380
424
  }
381
425
  async executeRunAsLambda(fn, options = {}) {
426
+ if (this.config.readonly) throw new Error("Cannot execute runAsLambda on a SqliteStateDB opened in readonly mode");
382
427
  const { retryLimit = 5, verifyHash = false, commitMessage } = options;
383
428
  if (!this.stateCommitTableReady) await this.ensureStateCommitTable();
384
429
  if (this.trustedStorage) {
@@ -455,6 +500,7 @@ var SqliteStateDB = class extends _ocap_statedb.StateDB {
455
500
  * Reset all tables (for testing)
456
501
  */
457
502
  async reset() {
503
+ if (this.config.readonly) throw new Error("Cannot reset a SqliteStateDB opened in readonly mode");
458
504
  const tables = Object.keys(this.readyMarks);
459
505
  for (const table of tables) {
460
506
  const tableInstance = this[table];
@@ -485,7 +531,7 @@ var SqliteStateDB = class extends _ocap_statedb.StateDB {
485
531
  if (this.walCheckpointPromise) await this.walCheckpointPromise.catch(() => void 0);
486
532
  await this.commitQueue;
487
533
  await this.db.destroy();
488
- if (this.trustedStorage && typeof this.trustedStorage.destroy === "function") await this.trustedStorage.destroy();
534
+ if (this.trustedStorage && typeof this.trustedStorage.destroy === "function" && this.trustedStorage.filename !== ":memory:") await this.trustedStorage.destroy();
489
535
  }
490
536
  /**
491
537
  * Get the canonical SHA-256 digest of the write-set from the most recently committed `runAsLambda` transaction.
package/lib/db.d.cts CHANGED
@@ -94,6 +94,7 @@ declare class SqliteStateDB extends StateDB implements IStateDB {
94
94
  }>;
95
95
  private startWalCheckpointWorker;
96
96
  private ensureStateCommitTable;
97
+ private hasPopulatedStateTables;
97
98
  private ensureTrustedStorageInitialized;
98
99
  /**
99
100
  * Get the current and previous state commit hash tracked in SQLite StateDB.
package/lib/kysely.cjs CHANGED
@@ -1545,7 +1545,7 @@ function createKysely(config) {
1545
1545
  const { BunSqliteDialect } = bunReq("kysely-bun-sqlite");
1546
1546
  const { Database: BunDatabase } = bunReq("bun:sqlite");
1547
1547
  const sqlite$1 = new BunDatabase(filename, {
1548
- create: config.create !== false,
1548
+ create: config.readonly && filename !== ":memory:" ? false : config.create !== false,
1549
1549
  readonly: config.readonly === true
1550
1550
  });
1551
1551
  if (config.walMode !== false && !config.readonly && filename !== ":memory:") {
@@ -1560,7 +1560,7 @@ function createKysely(config) {
1560
1560
  }
1561
1561
  const sqlite = new (require("better-sqlite3"))(filename, {
1562
1562
  readonly: config.readonly === true,
1563
- fileMustExist: config.create === false
1563
+ fileMustExist: config.readonly === true || config.create === false
1564
1564
  });
1565
1565
  if (config.walMode !== false && !config.readonly && filename !== ":memory:") {
1566
1566
  sqlite.pragma("journal_mode = WAL");
package/lib/kysely.d.cts CHANGED
@@ -59,6 +59,11 @@ interface SqliteConfig {
59
59
  dialect?: Dialect;
60
60
  /** Optional trusted storage instance for state commit hash verification */
61
61
  trustedStorage?: ITrustedStorage;
62
+ /**
63
+ * Explicitly allow bootstrapping an empty `trustedStorage` onto an already-populated StateDB
64
+ * (only set by operator `--force` recovery tools; false by default so wiped `trusted.sqlite` fails closed).
65
+ */
66
+ allowTrustedBootstrap?: boolean;
62
67
  /** Open database in readonly mode */
63
68
  readonly?: boolean;
64
69
  /** Throw error if file doesn't exist (default: false, creates new file) */
package/lib/reconcile.cjs CHANGED
@@ -495,6 +495,7 @@ async function reconcileChainState(db, options = {}) {
495
495
  assertSqliteStateDb(db);
496
496
  const dryRun = options.dryRun !== false;
497
497
  if (!dryRun && options.allowProtocolBypass !== true) throw new Error("reconcileChainState({ dryRun: false }) writes directly to SQLite and bypasses protocol validation; pass allowProtocolBypass: true to confirm");
498
+ if (!dryRun && db.trustedStorage) await db.verifyTrustedHash();
498
499
  const beforeReport = await verifyChainStateInvariants(db, { ledger: options.ledger });
499
500
  const actions = [];
500
501
  if (beforeReport.valid) return {
@@ -504,7 +505,7 @@ async function reconcileChainState(db, options = {}) {
504
505
  afterReport: beforeReport,
505
506
  actions
506
507
  };
507
- const executeRepairs = async (executor) => {
508
+ const executeRepairs = async (executor, applyWrites, targetActions) => {
508
509
  const allBalances = await executor.selectFrom("balance").selectAll().execute();
509
510
  for (const row of allBalances) {
510
511
  const addr = String(row.address || "");
@@ -512,7 +513,7 @@ async function reconcileChainState(db, options = {}) {
512
513
  const balStr = String(row.balance ?? "0");
513
514
  const checksumAddr = (0, _ocap_state_lib_states_account.ensureChecksumAddress)(addr);
514
515
  if (CANONICAL_UINT_REGEX.test(balStr) && new _ocap_util.BN(balStr, 10).isZero()) {
515
- actions.push({
516
+ targetActions.push({
516
517
  code: "I-BAL-3",
517
518
  table: "balance",
518
519
  key: `${addr}:${tokAddr}`,
@@ -524,13 +525,13 @@ async function reconcileChainState(db, options = {}) {
524
525
  },
525
526
  after: null
526
527
  });
527
- if (!dryRun) await executor.deleteFrom("balance").where("address", "=", addr).where("tokenAddress", "=", tokAddr).execute();
528
+ if (applyWrites) await executor.deleteFrom("balance").where("address", "=", addr).where("tokenAddress", "=", tokAddr).execute();
528
529
  continue;
529
530
  }
530
531
  if (checksumAddr !== addr && CANONICAL_UINT_REGEX.test(balStr)) {
531
532
  const existingCanonical = await executor.selectFrom("balance").selectAll().where("address", "=", checksumAddr).where("tokenAddress", "=", tokAddr).executeTakeFirst();
532
533
  const mergedBalance = existingCanonical && CANONICAL_UINT_REGEX.test(String(existingCanonical.balance)) ? new _ocap_util.BN(String(existingCanonical.balance), 10).add(new _ocap_util.BN(balStr, 10)).toString(10) : balStr;
533
- actions.push({
534
+ targetActions.push({
534
535
  code: "I-BAL-1",
535
536
  table: "balance",
536
537
  key: `${addr}:${tokAddr}`,
@@ -546,7 +547,7 @@ async function reconcileChainState(db, options = {}) {
546
547
  balance: mergedBalance
547
548
  }
548
549
  });
549
- if (!dryRun) {
550
+ if (applyWrites) {
550
551
  await executor.deleteFrom("balance").where("address", "=", addr).where("tokenAddress", "=", tokAddr).execute();
551
552
  await executor.insertInto("balance").values({
552
553
  address: checksumAddr,
@@ -601,7 +602,7 @@ async function reconcileChainState(db, options = {}) {
601
602
  if (!nextMigratedFrom.includes(acc.address)) {
602
603
  const updatedFrom = [...nextMigratedFrom, acc.address];
603
604
  nextAcc.migratedFrom = JSON.stringify(updatedFrom);
604
- actions.push({
605
+ targetActions.push({
605
606
  code: "I-MIG-1",
606
607
  table: "account",
607
608
  key: nextHop,
@@ -609,7 +610,7 @@ async function reconcileChainState(db, options = {}) {
609
610
  before: nextMigratedFrom,
610
611
  after: updatedFrom
611
612
  });
612
- if (!dryRun) await executor.updateTable("account").set({ migratedFrom: JSON.stringify(updatedFrom) }).where("address", "=", nextHop).execute();
613
+ if (applyWrites) await executor.updateTable("account").set({ migratedFrom: JSON.stringify(updatedFrom) }).where("address", "=", nextHop).execute();
613
614
  }
614
615
  }
615
616
  const zombieRows = await executor.selectFrom("balance").selectAll().where("address", "=", acc.address).execute();
@@ -628,7 +629,7 @@ async function reconcileChainState(db, options = {}) {
628
629
  const forwardPortion = zBal.sub(zombiePortion);
629
630
  if (zombiePortion.gt(new _ocap_util.BN(0))) runningAccountedSupply.set(tokAddr, accountedSupply.sub(zombiePortion));
630
631
  if (forwardPortion.isZero()) {
631
- actions.push({
632
+ targetActions.push({
632
633
  code: "I-BAL-2",
633
634
  table: "balance",
634
635
  key: `${acc.address}:${tokAddr}`,
@@ -640,12 +641,12 @@ async function reconcileChainState(db, options = {}) {
640
641
  },
641
642
  after: null
642
643
  });
643
- if (!dryRun) await executor.deleteFrom("balance").where("address", "=", acc.address).where("tokenAddress", "=", tokAddr).execute();
644
+ if (applyWrites) await executor.deleteFrom("balance").where("address", "=", acc.address).where("tokenAddress", "=", tokAddr).execute();
644
645
  } else {
645
646
  const nextTermBalBn = termBal.add(forwardPortion);
646
647
  const nextTermBal = nextTermBalBn.toString(10);
647
648
  liveBalanceMap.set(termBalKey, nextTermBalBn);
648
- actions.push({
649
+ targetActions.push({
649
650
  code: "I-BAL-2",
650
651
  table: "balance",
651
652
  key: `${acc.address}:${tokAddr}`,
@@ -669,7 +670,7 @@ async function reconcileChainState(db, options = {}) {
669
670
  deletedDuplicateZombiePortion: zombiePortion.toString(10)
670
671
  }
671
672
  });
672
- if (!dryRun) {
673
+ if (applyWrites) {
673
674
  await executor.deleteFrom("balance").where("address", "=", acc.address).where("tokenAddress", "=", tokAddr).execute();
674
675
  await executor.insertInto("balance").values({
675
676
  address: terminalAddr,
@@ -682,7 +683,7 @@ async function reconcileChainState(db, options = {}) {
682
683
  }
683
684
  const orphanAssets = await executor.selectFrom("asset").selectAll().where("owner", "=", acc.address).execute();
684
685
  for (const ast of orphanAssets) {
685
- actions.push({
686
+ targetActions.push({
686
687
  code: "I-AST-1",
687
688
  table: "asset",
688
689
  key: ast.address,
@@ -690,7 +691,7 @@ async function reconcileChainState(db, options = {}) {
690
691
  before: { owner: acc.address },
691
692
  after: { owner: terminalAddr }
692
693
  });
693
- if (!dryRun) await executor.updateTable("asset").set({ owner: terminalAddr }).where("address", "=", ast.address).execute();
694
+ if (applyWrites) await executor.updateTable("asset").set({ owner: terminalAddr }).where("address", "=", ast.address).execute();
694
695
  }
695
696
  }
696
697
  const stakes = await executor.selectFrom("stake").selectAll().execute();
@@ -710,7 +711,7 @@ async function reconcileChainState(db, options = {}) {
710
711
  return Boolean(a && a.owner === stk.address);
711
712
  });
712
713
  if (validActive.length !== activeAssets.length || validRevoked.length !== revokedAssets.length) {
713
- actions.push({
714
+ targetActions.push({
714
715
  code: "I-STK-1",
715
716
  table: "stake",
716
717
  key: stk.address,
@@ -724,7 +725,7 @@ async function reconcileChainState(db, options = {}) {
724
725
  revokedAssets: validRevoked
725
726
  }
726
727
  });
727
- if (!dryRun) await executor.updateTable("stake").set({
728
+ if (applyWrites) await executor.updateTable("stake").set({
728
729
  assets: JSON.stringify(validActive),
729
730
  revokedAssets: JSON.stringify(validRevoked)
730
731
  }).where("address", "=", stk.address).execute();
@@ -747,7 +748,7 @@ async function reconcileChainState(db, options = {}) {
747
748
  name: tok.name
748
749
  }
749
750
  };
750
- actions.push({
751
+ targetActions.push({
751
752
  code: "I-TOK-4",
752
753
  table: "asset",
753
754
  key: charterAddr,
@@ -755,7 +756,7 @@ async function reconcileChainState(db, options = {}) {
755
756
  before: charterData,
756
757
  after: repairedData
757
758
  });
758
- if (!dryRun) await executor.updateTable("asset").set({ data: JSON.stringify(repairedData) }).where("address", "=", charterAddr).execute();
759
+ if (applyWrites) await executor.updateTable("asset").set({ data: JSON.stringify(repairedData) }).where("address", "=", charterAddr).execute();
759
760
  }
760
761
  }
761
762
  } catch {}
@@ -777,7 +778,7 @@ async function reconcileChainState(db, options = {}) {
777
778
  for (const tok of tokens) {
778
779
  const expected = (sums.get(tok.address) || new _ocap_util.BN(0)).toString(10);
779
780
  if (String(tok.totalSupply ?? "0") !== expected) {
780
- actions.push({
781
+ targetActions.push({
781
782
  code: "I-TOK-1",
782
783
  table: "token",
783
784
  key: tok.address,
@@ -785,7 +786,7 @@ async function reconcileChainState(db, options = {}) {
785
786
  before: { totalSupply: tok.totalSupply },
786
787
  after: { totalSupply: expected }
787
788
  });
788
- if (!dryRun) await executor.updateTable("token").set({ totalSupply: expected }).where("address", "=", tok.address).execute();
789
+ if (applyWrites) await executor.updateTable("token").set({ totalSupply: expected }).where("address", "=", tok.address).execute();
789
790
  }
790
791
  }
791
792
  const tokenFactories = await executor.selectFrom("tokenFactory").selectAll().execute();
@@ -793,7 +794,7 @@ async function reconcileChainState(db, options = {}) {
793
794
  if (Number(tf.status || 0) !== 0) continue;
794
795
  const expected = (sums.get(tf.tokenAddress) || new _ocap_util.BN(0)).toString(10);
795
796
  if (String(tf.currentSupply ?? "0") !== expected) {
796
- actions.push({
797
+ targetActions.push({
797
798
  code: "I-TOK-3",
798
799
  table: "tokenFactory",
799
800
  key: tf.address,
@@ -801,13 +802,13 @@ async function reconcileChainState(db, options = {}) {
801
802
  before: { currentSupply: tf.currentSupply },
802
803
  after: { currentSupply: expected }
803
804
  });
804
- if (!dryRun) await executor.updateTable("tokenFactory").set({ currentSupply: expected }).where("address", "=", tf.address).execute();
805
+ if (applyWrites) await executor.updateTable("tokenFactory").set({ currentSupply: expected }).where("address", "=", tf.address).execute();
805
806
  }
806
807
  }
807
808
  }
808
809
  };
809
810
  if (dryRun) {
810
- await executeRepairs(db.getKysely());
811
+ await executeRepairs(db.getKysely(), false, actions);
811
812
  return {
812
813
  dryRun: true,
813
814
  reconciled: false,
@@ -816,9 +817,22 @@ async function reconcileChainState(db, options = {}) {
816
817
  actions
817
818
  };
818
819
  }
820
+ const previewActions = [];
821
+ await executeRepairs(db.getKysely(), false, previewActions);
822
+ if (previewActions.length === 0) return {
823
+ dryRun: false,
824
+ reconciled: beforeReport.valid,
825
+ beforeReport,
826
+ afterReport: beforeReport,
827
+ actions: previewActions
828
+ };
819
829
  await db.runAsLambda(async (txn) => {
820
- await executeRepairs(txn);
821
- }, { commitMessage: "reconcile_chain_state" });
830
+ actions.length = 0;
831
+ await executeRepairs(txn, true, actions);
832
+ }, {
833
+ commitMessage: "reconcile_chain_state",
834
+ verifyHash: true
835
+ });
822
836
  await kysely.sql`PRAGMA wal_checkpoint(PASSIVE)`.execute(db.getKysely()).catch(() => {});
823
837
  const afterReport = await verifyChainStateInvariants(db, { ledger: options.ledger });
824
838
  return {
@@ -94,7 +94,7 @@ var BalanceTable = class extends require_table_base.default {
94
94
  const tokenBalances = await this.getBalance(normalizedAddress, ctx);
95
95
  const removedTokens = Object.keys(tokenBalances).filter((token) => !(token in tokens));
96
96
  const updatedTokens = Object.keys(tokens).filter((token) => tokenBalances[token] && tokenBalances[token] !== "0" || tokens[token] && tokens[token] !== "0").filter((token) => tokenBalances[token] !== tokens[token]);
97
- if (updatedTokens.length === 0 && removedTokens.length === 0) return { ...tokens };
97
+ if (updatedTokens.length === 0 && removedTokens.length === 0) return { ...tokenBalances };
98
98
  const batchRows = [];
99
99
  const pendingOps = [];
100
100
  for (const tokenAddress of updatedTokens) {
@@ -126,7 +126,10 @@ var BalanceTable = class extends require_table_base.default {
126
126
  }
127
127
  const sqlContext = ctx;
128
128
  const executor = this.getExecutor(sqlContext);
129
- if (removedTokens.length > 0) await executor.deleteFrom("balance").where("address", "=", normalizedAddress).where("tokenAddress", "in", removedTokens).execute();
129
+ if (removedTokens.length > 0) {
130
+ await executor.deleteFrom("balance").where("address", "=", normalizedAddress).where("tokenAddress", "in", removedTokens).execute();
131
+ for (const removedToken of removedTokens) delete tokenBalances[removedToken];
132
+ }
130
133
  if (batchRows.length > 0) for (let i = 0; i < batchRows.length; i += UPSERT_BATCH_SIZE) {
131
134
  const batch = batchRows.slice(i, i + UPSERT_BATCH_SIZE);
132
135
  await executor.insertInto("balance").values(batch).onConflict((oc) => oc.columns(["address", "tokenAddress"]).doUpdateSet((eb) => ({
@@ -162,7 +165,7 @@ var BalanceTable = class extends require_table_base.default {
162
165
  else await this.create(op.key, op.payload, ctx);
163
166
  tokenBalances[op.tokenAddress] = tokens[op.tokenAddress];
164
167
  }
165
- return { ...tokens };
168
+ return { ...tokenBalances };
166
169
  }
167
170
  };
168
171
  var balance_default = BalanceTable;
@@ -154,15 +154,19 @@ var SqliteTable = class SqliteTable extends _ocap_statedb.StateDBTable {
154
154
  const insertedRow = await executor.insertInto(this.name).values(serialized).returningAll().executeTakeFirst();
155
155
  const deserializedInserted = this.deserializeFromDb(insertedRow ?? serialized) ?? insertAttrs;
156
156
  this.recordCommittedRowPut(sqlContext, lookupKey, deserializedInserted);
157
- if (this.syncBalance && attrs.tokens !== void 0) {
158
- const rawTokens = attrs.tokens || {};
159
- insertAttrs.tokens = Object.keys(rawTokens).length ? await this.balanceTable.updateBalance({
160
- address: insertAttrs.address || (typeof key === "string" ? key : ""),
161
- tokens: rawTokens,
162
- context: attrs.context
163
- }, context) : {};
157
+ const created = { ...deserializedInserted };
158
+ if (this.syncBalance) {
159
+ const balanceAddr = created.address || insertAttrs.address || (typeof key === "string" ? key : "");
160
+ if (attrs.tokens !== void 0) {
161
+ const rawTokens = attrs.tokens || {};
162
+ created.tokens = Object.keys(rawTokens).length ? await this.balanceTable.updateBalance({
163
+ address: balanceAddr,
164
+ tokens: rawTokens,
165
+ context: attrs.context
166
+ }, context) : await this.balanceTable.getBalance(balanceAddr, context);
167
+ } else created.tokens = await this.balanceTable.getBalance(balanceAddr, context);
164
168
  }
165
- return insertAttrs;
169
+ return created;
166
170
  }
167
171
  async _get(key, context) {
168
172
  if (!key) return null;
@@ -229,10 +233,7 @@ var SqliteTable = class SqliteTable extends _ocap_statedb.StateDBTable {
229
233
  if (!updatedRow) throw new Error(`${this.name} does not exist: ${JSON.stringify(key)}`);
230
234
  const deserialized = this.deserializeFromDb(updatedRow);
231
235
  this.recordCommittedRowPut(sqlContext, lookupKey, deserialized);
232
- merged = {
233
- ...deserialized,
234
- ...updateAttrs
235
- };
236
+ merged = { ...deserialized };
236
237
  } else {
237
238
  let qb = executor.selectFrom(this.name).selectAll();
238
239
  for (const [k, v] of queryEntries) qb = qb.where(k, "=", v);
@@ -198,7 +198,10 @@ async function applyTombstoneBalanceCleanup(dbOrKysely, options) {
198
198
  planDigest: freshPlan.planDigest
199
199
  };
200
200
  };
201
- if (isSqliteStateDB(dbOrKysely)) return dbOrKysely.runAsLambda((txn) => executeInTxn(txn));
201
+ if (isSqliteStateDB(dbOrKysely)) return dbOrKysely.runAsLambda((txn) => executeInTxn(txn), {
202
+ verifyHash: true,
203
+ commitMessage: "cleanup_tombstone_balances"
204
+ });
202
205
  const kysely$1 = resolveKyselyExecutor(dbOrKysely);
203
206
  if ("transaction" in kysely$1 && typeof kysely$1.transaction === "function") return kysely$1.transaction().execute((txn) => executeInTxn(txn));
204
207
  return executeInTxn(kysely$1);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ocap/statedb-sqlite",
3
3
  "description": "OCAP statedb adapter that uses SQLite as backend",
4
- "version": "1.31.4",
4
+ "version": "1.31.5",
5
5
  "author": "wangshijun <shijun@arcblock.io> (https://www.arcblock.io)",
6
6
  "bugs": {
7
7
  "url": "https://github.com/ArcBlock/blockchain/issues",
@@ -14,7 +14,7 @@
14
14
  "wangshijun <shijun@arcblock.io> (https://www.arcblock.io)"
15
15
  ],
16
16
  "devDependencies": {
17
- "@ocap/statedb-test": "1.31.4"
17
+ "@ocap/statedb-test": "1.31.5"
18
18
  },
19
19
  "homepage": "https://github.com/ArcBlock/blockchain/tree/master/statedb/sqlite",
20
20
  "keywords": [
@@ -63,12 +63,12 @@
63
63
  "clean": "rm -rf lib esm"
64
64
  },
65
65
  "dependencies": {
66
- "@arcblock/did": "1.31.4",
67
- "@arcblock/did-util": "1.31.4",
68
- "@ocap/state": "1.31.4",
69
- "@ocap/statedb": "1.31.4",
70
- "@ocap/types": "1.31.4",
71
- "@ocap/util": "1.31.4",
66
+ "@arcblock/did": "1.31.5",
67
+ "@arcblock/did-util": "1.31.5",
68
+ "@ocap/state": "1.31.5",
69
+ "@ocap/statedb": "1.31.5",
70
+ "@ocap/types": "1.31.5",
71
+ "@ocap/util": "1.31.5",
72
72
  "better-sqlite3": "^11.8.1",
73
73
  "kysely": "^0.28.14",
74
74
  "kysely-bun-sqlite": "^0.4.0",