@ocap/statedb-sqlite 1.31.1 → 1.31.3

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
@@ -2,6 +2,7 @@ const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
2
2
  const require_migrations_001_genesis = require('./001-genesis.cjs');
3
3
  const require_migrations_002_delegation_extensions = require('./002-delegation-extensions.cjs');
4
4
  const require_migrations_003_token_charter_assets = require('./003-token-charter-assets.cjs');
5
+ const require_migrations_004_rollup_reserves = require('./004-rollup-reserves.cjs');
5
6
  let kysely = require("kysely");
6
7
 
7
8
  //#region src/migrations/index.ts
@@ -17,6 +18,12 @@ var InMemoryMigrationProvider = class {
17
18
  return this.migrations;
18
19
  }
19
20
  };
21
+ function isRetryableMigrationError(error) {
22
+ const code = error?.code;
23
+ if (code === "SQLITE_BUSY" || code === "SQLITE_LOCKED" || code === 5 || code === 6) return true;
24
+ const msg = (error instanceof Error ? error.message : String(error ?? "")).toLowerCase();
25
+ 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");
26
+ }
20
27
  /**
21
28
  * Run all pending migrations
22
29
  *
@@ -30,20 +37,27 @@ var InMemoryMigrationProvider = class {
30
37
  * ```
31
38
  */
32
39
  async function runMigrations(db) {
33
- const { error, results } = await new kysely.Migrator({
34
- db,
35
- provider: new InMemoryMigrationProvider({
36
- "001-genesis": require_migrations_001_genesis._001_genesis_exports,
37
- "002-delegation-extensions": require_migrations_002_delegation_extensions._002_delegation_extensions_exports,
38
- "003-token-charter-assets": require_migrations_003_token_charter_assets._003_token_charter_assets_exports
39
- })
40
- }).migrateToLatest();
41
- results?.forEach((it) => {
42
- if (it.status === "Success") {} else if (it.status === "Error") console.error(`Migration "${it.migrationName}" failed`);
43
- });
44
- if (error) {
45
- console.error("Migration failed:", error);
46
- throw error;
40
+ for (let attempt = 0;; attempt++) {
41
+ const { error, results } = await new kysely.Migrator({
42
+ db,
43
+ provider: new InMemoryMigrationProvider({
44
+ "001-genesis": require_migrations_001_genesis._001_genesis_exports,
45
+ "002-delegation-extensions": require_migrations_002_delegation_extensions._002_delegation_extensions_exports,
46
+ "003-token-charter-assets": require_migrations_003_token_charter_assets._003_token_charter_assets_exports,
47
+ "004-rollup-reserves": require_migrations_004_rollup_reserves._004_rollup_reserves_exports
48
+ })
49
+ }).migrateToLatest();
50
+ if (!error) {
51
+ results?.forEach((it) => {
52
+ if (it.status === "Error") console.error(`Migration "${it.migrationName}" failed`);
53
+ });
54
+ return;
55
+ }
56
+ if (!isRetryableMigrationError(error) || attempt >= 8) {
57
+ console.error("Migration failed:", error);
58
+ throw error;
59
+ }
60
+ await new Promise((resolve) => setTimeout(resolve, 10 * 2 ** attempt));
47
61
  }
48
62
  }
49
63
  /**
@@ -56,7 +70,8 @@ async function rollbackMigrations(db) {
56
70
  provider: new InMemoryMigrationProvider({
57
71
  "001-genesis": require_migrations_001_genesis._001_genesis_exports,
58
72
  "002-delegation-extensions": require_migrations_002_delegation_extensions._002_delegation_extensions_exports,
59
- "003-token-charter-assets": require_migrations_003_token_charter_assets._003_token_charter_assets_exports
73
+ "003-token-charter-assets": require_migrations_003_token_charter_assets._003_token_charter_assets_exports,
74
+ "004-rollup-reserves": require_migrations_004_rollup_reserves._004_rollup_reserves_exports
60
75
  })
61
76
  });
62
77
  let hasMore = true;
@@ -50,13 +50,24 @@ var AccountTable = class extends require_table_base.default {
50
50
  for (const target of targets) if (await checkTarget(target)) return true;
51
51
  return false;
52
52
  }
53
+ resolveAddressKey(key) {
54
+ if (typeof key === "string") return key.length > 0 ? (0, _ocap_state_lib_states_account.ensureChecksumAddress)(key) : key;
55
+ if (typeof key === "object" && key !== null) {
56
+ const raw = key[this.uniqIndex];
57
+ if (typeof raw === "string" && raw.length > 0) return (0, _ocap_state_lib_states_account.ensureChecksumAddress)(raw);
58
+ return key;
59
+ }
60
+ return key;
61
+ }
53
62
  /**
54
63
  * Get account with migration chain following
55
64
  * When an account has migratedTo set, follows the chain to get the final account
56
65
  */
57
66
  async _get(address, context = {}) {
58
67
  const { traceMigration = true, ...restContext } = context;
59
- const normalizedAddress = (0, _ocap_state_lib_states_account.ensureChecksumAddress)(address);
68
+ const normalizedKey = this.resolveAddressKey(address);
69
+ if (typeof normalizedKey !== "string" || normalizedKey.length === 0) return super._get(normalizedKey, restContext);
70
+ const normalizedAddress = normalizedKey;
60
71
  const current = await super._get(normalizedAddress, restContext);
61
72
  if (current && traceMigration && Array.isArray(current.migratedTo) && current.migratedTo.length) {
62
73
  const visitedAddresses = context._visitedAddresses || /* @__PURE__ */ new Set();
@@ -73,13 +84,19 @@ var AccountTable = class extends require_table_base.default {
73
84
  * Create account with address normalization and circular migration detection
74
85
  */
75
86
  async _create(key, attrs = {}, context) {
76
- const address = (0, _ocap_state_lib_states_account.ensureChecksumAddress)(key);
77
- const attrsWithMigration = attrs;
87
+ const normalizedAttrs = {
88
+ ...attrs,
89
+ balance: attrs.balance ?? "0"
90
+ };
91
+ const normalizedKey = this.resolveAddressKey(key);
92
+ if (typeof normalizedKey !== "string" || normalizedKey.length === 0) return super._create(normalizedKey, normalizedAttrs, context);
93
+ const address = normalizedKey;
94
+ const attrsWithMigration = normalizedAttrs;
78
95
  if (attrsWithMigration.migratedTo?.length) {
79
96
  if (await this.wouldCreateCycle(address, attrsWithMigration.migratedTo, context)) throw new Error(`Circular migration detected: ${address} -> ${attrsWithMigration.migratedTo.join(", ")}`);
80
97
  }
81
98
  return super._create(address, {
82
- ...attrs,
99
+ ...normalizedAttrs,
83
100
  [this.uniqIndex]: address
84
101
  }, context);
85
102
  }
@@ -87,12 +104,19 @@ var AccountTable = class extends require_table_base.default {
87
104
  * Update account with circular migration detection
88
105
  */
89
106
  async _update(key, updates, context) {
90
- const address = (0, _ocap_state_lib_states_account.ensureChecksumAddress)(key);
91
- const updatesWithMigration = updates;
107
+ const normalizedUpdates = { ...updates || {} };
108
+ if ("balance" in normalizedUpdates) {
109
+ if (normalizedUpdates.balance === void 0) delete normalizedUpdates.balance;
110
+ else if (normalizedUpdates.balance === null) normalizedUpdates.balance = "0";
111
+ }
112
+ const normalizedKey = this.resolveAddressKey(key);
113
+ if (typeof normalizedKey !== "string" || normalizedKey.length === 0) return super._update(normalizedKey, normalizedUpdates, context);
114
+ const address = normalizedKey;
115
+ const updatesWithMigration = normalizedUpdates;
92
116
  if (updatesWithMigration.migratedTo?.length) {
93
117
  if (await this.wouldCreateCycle(address, updatesWithMigration.migratedTo, context)) throw new Error(`Circular migration detected: ${address} -> ${updatesWithMigration.migratedTo.join(", ")}`);
94
118
  }
95
- return super._update(address, updates, context);
119
+ return super._update(address, normalizedUpdates, context);
96
120
  }
97
121
  };
98
122
  var account_default = AccountTable;
@@ -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 };
@@ -1,5 +1,6 @@
1
1
  Object.defineProperty(exports, '__esModule', { value: true });
2
2
  const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
3
+ const require_write_set = require('../write-set.cjs');
3
4
  const require_table_base = require('./base.cjs');
4
5
  let _ocap_state_lib_states_account = require("@ocap/state/lib/states/account");
5
6
 
@@ -32,28 +33,37 @@ var BalanceTable = class extends require_table_base.default {
32
33
  return query;
33
34
  }
34
35
  async _create(key, attrs = {}, context) {
36
+ const normalizedAttrs = {
37
+ ...attrs,
38
+ balance: attrs.balance ?? "0"
39
+ };
35
40
  const collector = attrs?.[BATCH_COLLECTOR];
36
41
  if (collector) {
37
42
  const insertAttrs = {
38
- ...attrs,
43
+ ...normalizedAttrs,
39
44
  ...typeof key === "object" ? key : {}
40
45
  };
41
46
  collector.push(this.serializeForDb(insertAttrs));
42
47
  return insertAttrs;
43
48
  }
44
- return super._create(key, attrs, context);
49
+ return super._create(key, normalizedAttrs, context);
45
50
  }
46
51
  async _update(key, updates, context) {
52
+ const normalizedUpdates = { ...updates || {} };
53
+ if ("balance" in normalizedUpdates) {
54
+ if (normalizedUpdates.balance === void 0) delete normalizedUpdates.balance;
55
+ else if (normalizedUpdates.balance === null) normalizedUpdates.balance = "0";
56
+ }
47
57
  const collector = updates?.[BATCH_COLLECTOR];
48
58
  if (collector) {
49
59
  const merged = {
50
- ...updates,
60
+ ...normalizedUpdates,
51
61
  ...typeof key === "object" ? key : {}
52
62
  };
53
63
  collector.push(this.serializeForDb(merged));
54
64
  return merged;
55
65
  }
56
- return super._update(key, updates, context);
66
+ return super._update(key, normalizedUpdates, context);
57
67
  }
58
68
  /**
59
69
  * Get all token balances for an address
@@ -124,6 +134,23 @@ var BalanceTable = class extends require_table_base.default {
124
134
  context: eb.ref("excluded.context")
125
135
  }))).execute();
126
136
  }
137
+ const tracker = require_write_set.getWriteSetTracker(sqlContext?.txn);
138
+ if (tracker) {
139
+ for (const removedToken of removedTokens) tracker.recordDelete("balance", require_write_set.formatCanonicalPrimaryKey(this.uniqIndex, {
140
+ address: normalizedAddress,
141
+ tokenAddress: removedToken
142
+ }));
143
+ for (let idx = 0; idx < pendingOps.length; idx++) {
144
+ const op = pendingOps[idx];
145
+ const deserializedRow = this.deserializeFromDb(batchRows[idx]) ?? {
146
+ address: normalizedAddress,
147
+ tokenAddress: op.tokenAddress,
148
+ balance: tokens[op.tokenAddress] ?? "0",
149
+ context
150
+ };
151
+ tracker.recordPut("balance", require_write_set.formatCanonicalPrimaryKey(this.uniqIndex, op.key), deserializedRow);
152
+ }
153
+ }
127
154
  const noopCollector = [];
128
155
  for (const op of pendingOps) {
129
156
  Object.defineProperty(op.payload, BATCH_COLLECTOR, {
@@ -1,8 +1,9 @@
1
1
  Object.defineProperty(exports, '__esModule', { value: true });
2
2
  const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
3
+ const require_write_set = require('../write-set.cjs');
4
+ let _ocap_statedb = require("@ocap/statedb");
3
5
  let node_crypto = require("node:crypto");
4
6
  node_crypto = require_rolldown_runtime.__toESM(node_crypto);
5
- let _ocap_statedb = require("@ocap/statedb");
6
7
  let lodash_omit = require("lodash/omit");
7
8
  lodash_omit = require_rolldown_runtime.__toESM(lodash_omit);
8
9
 
@@ -23,10 +24,21 @@ var SqliteTable = class SqliteTable extends _ocap_statedb.StateDBTable {
23
24
  this.markReady();
24
25
  }
25
26
  /**
26
- * Get the Kysely executor (use transaction if available)
27
+ * Get the Kysely executor (use unwrapped transaction if available so ORM queries
28
+ * record structured row writes rather than raw-DML fallback)
27
29
  */
28
30
  getExecutor(context) {
29
- return context?.txn || this.db;
31
+ if (context?.txn) return require_write_set.unwrapTableTransaction(context.txn);
32
+ return this.db;
33
+ }
34
+ recordCommittedRowPut(context, lookupKey, deserializedRow) {
35
+ const tracker = require_write_set.getWriteSetTracker(context?.txn);
36
+ if (!tracker) return;
37
+ const canonicalKey = require_write_set.formatCanonicalPrimaryKey(this.uniqIndex, lookupKey);
38
+ if (this.syncBalance) {
39
+ const { tokens: _omitTokens, ...rowWithoutTokens } = deserializedRow;
40
+ tracker.recordPut(this.name, canonicalKey, rowWithoutTokens);
41
+ } else tracker.recordPut(this.name, canonicalKey, deserializedRow);
30
42
  }
31
43
  /**
32
44
  * Generate primary key from document
@@ -55,6 +67,15 @@ var SqliteTable = class SqliteTable extends _ocap_statedb.StateDBTable {
55
67
  return { [this.mapColumnName(this.primaryKey)]: key };
56
68
  }
57
69
  /**
70
+ * Check that a primary-key column value is a non-empty string or finite number.
71
+ * Prevents `undefined`/`null`/`''` key fields from silently dropping SQL WHERE clauses (#1438).
72
+ */
73
+ isValidPrimaryKeyValue(v) {
74
+ if (typeof v === "string") return v.length > 0;
75
+ if (typeof v === "number") return Number.isFinite(v);
76
+ return false;
77
+ }
78
+ /**
58
79
  * Map column name for reserved keywords
59
80
  */
60
81
  mapColumnName(name) {
@@ -101,7 +122,7 @@ var SqliteTable = class SqliteTable extends _ocap_statedb.StateDBTable {
101
122
  const booleanFields = new Set(this.schema.booleanFields || []);
102
123
  for (const [key, value] of Object.entries(data)) {
103
124
  const unmappedKey = this.unmapColumnName(key);
104
- if (booleanFields.has(unmappedKey) && typeof value === "number") result[unmappedKey] = value === 1;
125
+ if (booleanFields.has(unmappedKey) && (typeof value === "number" || value === null)) result[unmappedKey] = value === null ? false : value !== 0;
105
126
  else if (jsonFields.has(unmappedKey) && typeof value === "string") try {
106
127
  result[unmappedKey] = JSON.parse(value);
107
128
  } catch {
@@ -124,10 +145,15 @@ var SqliteTable = class SqliteTable extends _ocap_statedb.StateDBTable {
124
145
  insertAttrs[this.primaryKey] = id;
125
146
  }
126
147
  const lookupKey = Array.isArray(this.uniqIndex) ? Object.fromEntries(this.uniqIndex.map((k) => [k, insertAttrs[k]])) : insertAttrs[this.primaryKey];
148
+ const lookupQuery = this.formatKeyToQuery(lookupKey);
149
+ const lookupEntries = Object.entries(lookupQuery);
150
+ if (lookupEntries.length === 0 || lookupEntries.some(([, v]) => !this.isValidPrimaryKeyValue(v))) throw new Error(`Invalid or incomplete primary key for ${this.name} create: ${JSON.stringify(key)}`);
127
151
  if (await SqliteTable.prototype._get.call(this, lookupKey, context)) throw new Error(`${this.name} already exists: ${JSON.stringify(key)}`);
128
152
  if (this.syncBalance) delete insertAttrs.tokens;
129
153
  const serialized = this.serializeForDb(insertAttrs);
130
- await executor.insertInto(this.name).values(serialized).execute();
154
+ const insertedRow = await executor.insertInto(this.name).values(serialized).returningAll().executeTakeFirst();
155
+ const deserializedInserted = this.deserializeFromDb(insertedRow ?? serialized) ?? insertAttrs;
156
+ this.recordCommittedRowPut(sqlContext, lookupKey, deserializedInserted);
131
157
  if (this.syncBalance && attrs.tokens !== void 0) {
132
158
  const rawTokens = attrs.tokens || {};
133
159
  insertAttrs.tokens = Object.keys(rawTokens).length ? await this.balanceTable.updateBalance({
@@ -146,11 +172,10 @@ var SqliteTable = class SqliteTable extends _ocap_statedb.StateDBTable {
146
172
  if (Array.isArray(this.uniqIndex)) if (typeof key === "object") query = this.formatKeyToQuery(key);
147
173
  else return null;
148
174
  else query = this.formatKeyToQuery(key);
175
+ const queryEntries = Object.entries(query);
176
+ if (queryEntries.length === 0 || queryEntries.some(([, v]) => !this.isValidPrimaryKeyValue(v))) return null;
149
177
  let qb = executor.selectFrom(this.name).selectAll();
150
- for (const [k, v] of Object.entries(query)) {
151
- if (v === void 0) continue;
152
- qb = qb.where(k, "=", v);
153
- }
178
+ for (const [k, v] of queryEntries) qb = qb.where(k, "=", v);
154
179
  const result = await qb.executeTakeFirst();
155
180
  const deserialized = this.deserializeFromDb(result);
156
181
  if (deserialized && this.syncBalance) {
@@ -169,38 +194,48 @@ var SqliteTable = class SqliteTable extends _ocap_statedb.StateDBTable {
169
194
  async _update(key, updates, context) {
170
195
  const sqlContext = context;
171
196
  const executor = this.getExecutor(sqlContext);
172
- let lookupKey = key;
173
- if (Array.isArray(this.uniqIndex) && typeof key !== "object") {
174
- const updatesObj = updates;
175
- lookupKey = Object.fromEntries(this.uniqIndex.map((k) => [k, updatesObj[k]]));
176
- }
177
197
  let query;
178
- if (Array.isArray(this.uniqIndex)) query = this.formatKeyToQuery(lookupKey);
179
- else query = this.formatKeyToQuery(key);
198
+ let lookupKey;
199
+ if (Array.isArray(this.uniqIndex)) if (typeof key === "object" && key !== null) {
200
+ const keyObj = key;
201
+ lookupKey = Object.fromEntries(this.uniqIndex.map((k) => [k, keyObj[k]]));
202
+ query = this.formatKeyToQuery(key);
203
+ } else if (typeof key === "string" && this.isValidPrimaryKeyValue(key)) {
204
+ const updatesObj = updates || {};
205
+ const candidateKey = Object.fromEntries(this.uniqIndex.map((k, idx) => [k, updatesObj[k] !== void 0 ? updatesObj[k] : idx === 0 ? key : void 0]));
206
+ if (this.uniqIndex.every((k) => this.isValidPrimaryKeyValue(candidateKey[k]))) {
207
+ lookupKey = candidateKey;
208
+ query = this.formatKeyToQuery(candidateKey);
209
+ } else throw new Error(`Invalid or incomplete primary key for ${this.name} update: ${JSON.stringify(key)}`);
210
+ } else throw new Error(`Invalid or incomplete primary key for ${this.name} update: ${JSON.stringify(key)}`);
211
+ else {
212
+ if (!key || typeof key === "object" && key === null) throw new Error(`Invalid or incomplete primary key for ${this.name} update: ${JSON.stringify(key)}`);
213
+ lookupKey = typeof key === "object" ? key[this.primaryKey] : key;
214
+ query = this.formatKeyToQuery(key);
215
+ }
216
+ const queryEntries = Object.entries(query);
217
+ if (queryEntries.length === 0 || queryEntries.some(([, v]) => !this.isValidPrimaryKeyValue(v))) throw new Error(`Invalid or incomplete primary key for ${this.name} update: ${JSON.stringify(key)}`);
180
218
  const updateAttrs = { ...updates };
181
219
  if (Array.isArray(this.uniqIndex)) for (const k of this.uniqIndex) delete updateAttrs[k];
182
220
  else delete updateAttrs[this.primaryKey];
183
- if (this.syncBalance) delete updateAttrs.tokens;
221
+ if (this.syncBalance) if (updates.tokens !== void 0) updateAttrs.tokens = null;
222
+ else delete updateAttrs.tokens;
184
223
  let merged;
185
224
  if (Object.keys(updateAttrs).length > 0) {
186
225
  const serialized = this.serializeForDb(updateAttrs);
187
226
  let qb = executor.updateTable(this.name).set(serialized);
188
- for (const [k, v] of Object.entries(query)) {
189
- if (v === void 0) continue;
190
- qb = qb.where(k, "=", v);
191
- }
227
+ for (const [k, v] of queryEntries) qb = qb.where(k, "=", v);
192
228
  const updatedRow = await qb.returningAll().executeTakeFirst();
193
229
  if (!updatedRow) throw new Error(`${this.name} does not exist: ${JSON.stringify(key)}`);
230
+ const deserialized = this.deserializeFromDb(updatedRow);
231
+ this.recordCommittedRowPut(sqlContext, lookupKey, deserialized);
194
232
  merged = {
195
- ...this.deserializeFromDb(updatedRow),
233
+ ...deserialized,
196
234
  ...updateAttrs
197
235
  };
198
236
  } else {
199
237
  let qb = executor.selectFrom(this.name).selectAll();
200
- for (const [k, v] of Object.entries(query)) {
201
- if (v === void 0) continue;
202
- qb = qb.where(k, "=", v);
203
- }
238
+ for (const [k, v] of queryEntries) qb = qb.where(k, "=", v);
204
239
  const existingRow = await qb.executeTakeFirst();
205
240
  if (!existingRow) throw new Error(`${this.name} does not exist: ${JSON.stringify(key)}`);
206
241
  merged = this.deserializeFromDb(existingRow);
@@ -225,10 +260,19 @@ var SqliteTable = class SqliteTable extends _ocap_statedb.StateDBTable {
225
260
  async _reset(_context) {
226
261
  const sqlContext = _context;
227
262
  await this.getExecutor(sqlContext).deleteFrom(this.name).execute();
263
+ require_write_set.getWriteSetTracker(sqlContext?.txn)?.recordReset(this.name);
228
264
  }
229
265
  async updateOrCreate(exist, state, ctx) {
230
- const id = this.generatePrimaryKey(state);
231
- const attrs = (0, lodash_omit.default)(state, Array.isArray(this.uniqIndex) ? this.uniqIndex : [this.uniqIndex]);
266
+ const stateObj = state || {};
267
+ if (Array.isArray(this.uniqIndex)) {
268
+ const compositeKey = Object.fromEntries(this.uniqIndex.map((k) => [k, stateObj[k]]));
269
+ if (this.uniqIndex.some((k) => !this.isValidPrimaryKeyValue(compositeKey[k]))) throw new Error("Cannot update or create without uniq index");
270
+ const attrs$1 = (0, lodash_omit.default)(state, this.uniqIndex);
271
+ if (exist) return this.update(compositeKey, attrs$1, ctx);
272
+ return this.create(compositeKey, { ...state }, ctx);
273
+ }
274
+ const id = this.generatePrimaryKey(stateObj);
275
+ const attrs = (0, lodash_omit.default)(state, [this.uniqIndex]);
232
276
  if (!id) throw new Error("Cannot update or create without uniq index");
233
277
  if (exist) return this.update(id, attrs, ctx);
234
278
  return this.create(id, attrs, ctx);
@@ -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
  */
@@ -3,6 +3,21 @@ const require_table_base = require('./base.cjs');
3
3
 
4
4
  //#region src/table/rollup.ts
5
5
  /**
6
+ * Columns added by migration 004-rollup-reserves (#1548), mirroring Dolt's 20261109-rollup-reserves.
7
+ * Legacy rows read back NULL; those keys must stay ABSENT, not null/false: `hasRollupReserveTracking` treats
8
+ * `reserves !== undefined` as "tracking on", the boolean coercion would turn NULL into `false` and shadow the
9
+ * `data.value.*` fallbacks, and the state schema rejects null for these optional fields. Same as the Dolt
10
+ * RollupTable's `stripNullOptional`.
11
+ */
12
+ const OPTIONAL_NULLABLE_COLUMNS = [
13
+ "reserves",
14
+ "targetType",
15
+ "targetChainId",
16
+ "supportedTokens",
17
+ "allowProposerSponsor",
18
+ "enforceReserveWatermark"
19
+ ];
20
+ /**
6
21
  * Rollup Table
7
22
  * Extends base table with tokenAddress-based existence check
8
23
  */
@@ -19,24 +34,42 @@ var RollupTable = class extends require_table_base.default {
19
34
  "migrateHistory",
20
35
  "vaultHistory",
21
36
  "data",
22
- "context"
37
+ "context",
38
+ "reserves",
39
+ "supportedTokens"
23
40
  ],
24
- booleanFields: ["paused", "closed"]
41
+ booleanFields: [
42
+ "paused",
43
+ "closed",
44
+ "allowProposerSponsor",
45
+ "enforceReserveWatermark"
46
+ ]
25
47
  }
26
48
  });
27
49
  }
50
+ deserializeFromDb(data) {
51
+ const nullKeys = data ? OPTIONAL_NULLABLE_COLUMNS.filter((key) => data[key] === null || data[key] === void 0) : [];
52
+ const result = super.deserializeFromDb(data);
53
+ if (result) {
54
+ for (const key of nullKeys) delete result[key];
55
+ for (const key of OPTIONAL_NULLABLE_COLUMNS) if (result[key] === null) delete result[key];
56
+ }
57
+ return result;
58
+ }
28
59
  /**
29
- * Check if an active (non-paused) rollup with the given tokenAddress exists
60
+ * Check if a rollup that is not closed exists for the given tokenAddress (#1411).
61
+ * A paused rollup still owns the token: it can be resumed, and migrate_rollup requires it to be paused. Only
62
+ * close_rollup is terminal, so only a closed rollup frees the token for a new one.
30
63
  *
31
64
  * @param tokenAddress - Token address to check
32
65
  * @param context - Operation context with optional transaction
33
- * @returns true if active rollup with tokenAddress exists, false if tokenAddress is empty/undefined or rollup is paused
66
+ * @returns true if a non-closed rollup with tokenAddress exists, false if tokenAddress is empty/undefined
34
67
  */
35
68
  async existByToken(tokenAddress, context) {
36
69
  if (!tokenAddress) return false;
37
70
  const sqlContext = context;
38
71
  const executor = this.getExecutor(sqlContext);
39
- return ((await executor.selectFrom("rollup").select(executor.fn.count("address").as("count")).where("tokenAddress", "=", tokenAddress).where("paused", "=", 0).executeTakeFirst())?.count ?? 0) > 0;
72
+ return ((await executor.selectFrom("rollup").select(executor.fn.count("address").as("count")).where("tokenAddress", "=", tokenAddress).where((eb) => eb.or([eb("closed", "is", null), eb("closed", "=", 0)])).executeTakeFirst())?.count ?? 0) > 0;
40
73
  }
41
74
  };
42
75
  var rollup_default = RollupTable;
@@ -9,12 +9,15 @@ import { IOperationContext, IRollupState } from "@ocap/types";
9
9
  */
10
10
  declare class RollupTable extends SqliteTable<IRollupState> {
11
11
  constructor(config: Omit<SqliteTableConfig, 'name' | 'uniqIndex' | 'schema'>);
12
+ protected deserializeFromDb(data: Record<string, unknown> | undefined | null): Record<string, unknown> | null;
12
13
  /**
13
- * Check if an active (non-paused) rollup with the given tokenAddress exists
14
+ * Check if a rollup that is not closed exists for the given tokenAddress (#1411).
15
+ * A paused rollup still owns the token: it can be resumed, and migrate_rollup requires it to be paused. Only
16
+ * close_rollup is terminal, so only a closed rollup frees the token for a new one.
14
17
  *
15
18
  * @param tokenAddress - Token address to check
16
19
  * @param context - Operation context with optional transaction
17
- * @returns true if active rollup with tokenAddress exists, false if tokenAddress is empty/undefined or rollup is paused
20
+ * @returns true if a non-closed rollup with tokenAddress exists, false if tokenAddress is empty/undefined
18
21
  */
19
22
  existByToken(tokenAddress: string, context?: IOperationContext): Promise<boolean>;
20
23
  }
@@ -36,6 +36,20 @@ var TokenTable = class extends require_table_base.default {
36
36
  const executor = this.getExecutor(sqlContext);
37
37
  return ((await executor.selectFrom("token").select(executor.fn.count("address").as("count")).where("symbol", "=", symbol).executeTakeFirst())?.count ?? 0) > 0;
38
38
  }
39
+ /**
40
+ * Address of the token that owns the given symbol
41
+ *
42
+ * @param symbol - Token symbol to look up
43
+ * @param context - Operation context with optional transaction
44
+ * @returns token address, or null when no token uses the symbol
45
+ * @throws Error if symbol is empty
46
+ */
47
+ async getAddressBySymbol(symbol, context) {
48
+ if (!symbol) throw new Error("param symbol is required");
49
+ const sqlContext = context;
50
+ const result = await this.getExecutor(sqlContext).selectFrom("token").select("address").where("symbol", "=", symbol).orderBy("address").executeTakeFirst();
51
+ return result?.address ? String(result.address) : null;
52
+ }
39
53
  };
40
54
  var token_default = TokenTable;
41
55
 
@@ -18,6 +18,15 @@ declare class TokenTable extends SqliteTable<ITokenState> {
18
18
  * @throws Error if symbol is empty
19
19
  */
20
20
  existBySymbol(symbol: string, context?: IOperationContext): Promise<boolean>;
21
+ /**
22
+ * Address of the token that owns the given symbol
23
+ *
24
+ * @param symbol - Token symbol to look up
25
+ * @param context - Operation context with optional transaction
26
+ * @returns token address, or null when no token uses the symbol
27
+ * @throws Error if symbol is empty
28
+ */
29
+ getAddressBySymbol(symbol: string, context?: IOperationContext): Promise<string | null>;
21
30
  }
22
31
  //#endregion
23
32
  export { TokenTable as default };
@@ -1,7 +1,7 @@
1
1
  const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
2
- let node_crypto = require("node:crypto");
3
2
  let kysely = require("kysely");
4
3
  let _ocap_util = require("@ocap/util");
4
+ let node_crypto = require("node:crypto");
5
5
 
6
6
  //#region src/tombstone-cleanup.ts
7
7
  const CANONICAL_UINT_REGEX = /^(?:0|[1-9]\d*)$/;