@ocap/indexdb-sqlite 1.31.1 → 1.31.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/db/base.mjs +6 -2
- package/esm/db/index.mjs +2 -0
- package/esm/interfaces.d.mts +1 -0
- package/esm/migrations/003-delegation-extensions.mjs +9 -2
- package/esm/migrations/005-rollup-missing-columns.mjs +5 -1
- package/esm/migrations/010-tx-evidences.d.mts +16 -0
- package/esm/migrations/010-tx-evidences.mjs +26 -0
- package/esm/migrations/index.mjs +21 -7
- package/esm/table/base.d.mts +5 -0
- package/esm/table/base.mjs +25 -9
- package/esm/table/transaction.mjs +2 -1
- package/lib/db/base.cjs +6 -2
- package/lib/db/index.cjs +2 -0
- package/lib/interfaces.d.cts +1 -0
- package/lib/migrations/003-delegation-extensions.cjs +9 -2
- package/lib/migrations/005-rollup-missing-columns.cjs +5 -1
- package/lib/migrations/010-tx-evidences.cjs +33 -0
- package/lib/migrations/010-tx-evidences.d.cts +16 -0
- package/lib/migrations/index.cjs +21 -7
- package/lib/table/base.cjs +25 -9
- package/lib/table/base.d.cts +5 -0
- package/lib/table/transaction.cjs +2 -1
- package/package.json +6 -6
package/esm/db/base.mjs
CHANGED
|
@@ -89,7 +89,7 @@ var SqliteBaseIndexDB = class extends BaseIndexDB {
|
|
|
89
89
|
const { assets = [] } = assetFilter;
|
|
90
90
|
const { tokens = [] } = tokenFilter;
|
|
91
91
|
const { accounts = [] } = accountFilter;
|
|
92
|
-
const { txs = [], gasPayer: txGasPayer } = txFilter;
|
|
92
|
+
const { txs = [], gasPayer: txGasPayer, evidences = [] } = txFilter;
|
|
93
93
|
const gasPayer = addressGasPayer || txGasPayer || "";
|
|
94
94
|
const { rollups = [] } = rollupFilter;
|
|
95
95
|
const { stakes = [] } = stakeFilter;
|
|
@@ -151,7 +151,8 @@ var SqliteBaseIndexDB = class extends BaseIndexDB {
|
|
|
151
151
|
rollups: "rollup",
|
|
152
152
|
stakes: "stake",
|
|
153
153
|
delegations: "delegation",
|
|
154
|
-
tokenFactories: "tokenFactory"
|
|
154
|
+
tokenFactories: "tokenFactory",
|
|
155
|
+
evidences: "evidence"
|
|
155
156
|
};
|
|
156
157
|
const applyJsonArrayFilter = (column, values) => {
|
|
157
158
|
const refType = txRefTypeMap[column];
|
|
@@ -165,6 +166,7 @@ var SqliteBaseIndexDB = class extends BaseIndexDB {
|
|
|
165
166
|
if (stakes.length) applyJsonArrayFilter("stakes", stakes);
|
|
166
167
|
if (delegations.length) applyJsonArrayFilter("delegations", delegations);
|
|
167
168
|
if (tokenFactories.length) applyJsonArrayFilter("tokenFactories", tokenFactories);
|
|
169
|
+
if (evidences.length) applyJsonArrayFilter("evidences", evidences);
|
|
168
170
|
let total;
|
|
169
171
|
const activeJsonFilters = [];
|
|
170
172
|
if (factories.length) activeJsonFilters.push(["factory", factories]);
|
|
@@ -175,6 +177,7 @@ var SqliteBaseIndexDB = class extends BaseIndexDB {
|
|
|
175
177
|
if (stakes.length) activeJsonFilters.push(["stake", stakes]);
|
|
176
178
|
if (delegations.length) activeJsonFilters.push(["delegation", delegations]);
|
|
177
179
|
if (tokenFactories.length) activeJsonFilters.push(["tokenFactory", tokenFactories]);
|
|
180
|
+
if (evidences.length) activeJsonFilters.push(["evidence", evidences]);
|
|
178
181
|
const hasNonJsonFilters = !!(sender || receiver || gasPayer || types.length || txs.length || parsedStart || parsedEnd || validity);
|
|
179
182
|
const isUnionOnly = isUnion && !gasPayer && !types.length && !txs.length && !parsedStart && !parsedEnd && !validity && activeJsonFilters.length === 0;
|
|
180
183
|
const isMultiTypeOnly = types.length > 1 && !sender && !receiver && !gasPayer && !txs.length && !parsedStart && !parsedEnd && !validity && activeJsonFilters.length === 0;
|
|
@@ -251,6 +254,7 @@ var SqliteBaseIndexDB = class extends BaseIndexDB {
|
|
|
251
254
|
"stakes",
|
|
252
255
|
"delegations",
|
|
253
256
|
"tokenFactories",
|
|
257
|
+
"evidences",
|
|
254
258
|
"data"
|
|
255
259
|
].forEach((field) => {
|
|
256
260
|
if (typeof tx[field] === "string") try {
|
package/esm/db/index.mjs
CHANGED
|
@@ -60,6 +60,7 @@ var SqliteIndexDB = class extends base_default$1 {
|
|
|
60
60
|
"stakes",
|
|
61
61
|
"delegations",
|
|
62
62
|
"tokenFactories",
|
|
63
|
+
"evidences",
|
|
63
64
|
"data"
|
|
64
65
|
],
|
|
65
66
|
booleanFields: [
|
|
@@ -92,6 +93,7 @@ var SqliteIndexDB = class extends base_default$1 {
|
|
|
92
93
|
"stakes",
|
|
93
94
|
"delegations",
|
|
94
95
|
"tokenFactories",
|
|
96
|
+
"evidences",
|
|
95
97
|
"genesisTime",
|
|
96
98
|
"renaissanceTime",
|
|
97
99
|
"data"
|
package/esm/interfaces.d.mts
CHANGED
|
@@ -6,9 +6,16 @@ var _003_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
|
|
|
@@ -41,7 +41,11 @@ const MISSING_COLUMNS = [
|
|
|
41
41
|
}
|
|
42
42
|
];
|
|
43
43
|
async function up(db) {
|
|
44
|
-
for (const col of MISSING_COLUMNS)
|
|
44
|
+
for (const col of MISSING_COLUMNS) try {
|
|
45
|
+
await sql.raw(`ALTER TABLE rollup ADD COLUMN ${col.name} ${col.type}`).execute(db);
|
|
46
|
+
} catch (err) {
|
|
47
|
+
if (!(err instanceof Error ? err.message : String(err ?? "")).toLowerCase().includes("duplicate column name")) throw err;
|
|
48
|
+
}
|
|
45
49
|
await sql.raw(`UPDATE rollup SET
|
|
46
50
|
minDepositFee = json_extract(data, '$.minDepositFee'),
|
|
47
51
|
maxDepositFee = json_extract(data, '$.maxDepositFee'),
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Kysely } from "kysely";
|
|
2
|
+
|
|
3
|
+
//#region src/migrations/010-tx-evidences.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Migration 010 - Add evidences column on tx (#1479)
|
|
7
|
+
*
|
|
8
|
+
* `evidences` is a JSON array of bridge release evidence keys (`<kind>:<evidence>:<ordinal>`), written for
|
|
9
|
+
* `release_bridge_token` txs. Lookups go through the existing tx_ref junction table (type = 'evidence'),
|
|
10
|
+
* which already has the (type, value) index, so no new index is needed. Schema only: no historical rows
|
|
11
|
+
* carry a release tx.
|
|
12
|
+
*/
|
|
13
|
+
declare function up(db: Kysely<unknown>): Promise<void>;
|
|
14
|
+
declare function down(db: Kysely<unknown>): Promise<void>;
|
|
15
|
+
//#endregion
|
|
16
|
+
export { down, up };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { __exportAll } from "../_virtual/rolldown_runtime.mjs";
|
|
2
|
+
import { sql } from "kysely";
|
|
3
|
+
|
|
4
|
+
//#region src/migrations/010-tx-evidences.ts
|
|
5
|
+
var _010_tx_evidences_exports = /* @__PURE__ */ __exportAll({
|
|
6
|
+
down: () => down,
|
|
7
|
+
up: () => up
|
|
8
|
+
});
|
|
9
|
+
/**
|
|
10
|
+
* Migration 010 - Add evidences column on tx (#1479)
|
|
11
|
+
*
|
|
12
|
+
* `evidences` is a JSON array of bridge release evidence keys (`<kind>:<evidence>:<ordinal>`), written for
|
|
13
|
+
* `release_bridge_token` txs. Lookups go through the existing tx_ref junction table (type = 'evidence'),
|
|
14
|
+
* which already has the (type, value) index, so no new index is needed. Schema only: no historical rows
|
|
15
|
+
* carry a release tx.
|
|
16
|
+
*/
|
|
17
|
+
async function up(db) {
|
|
18
|
+
const columnsResult = await sql`PRAGMA table_info('tx')`.execute(db);
|
|
19
|
+
if (!new Set((columnsResult.rows || []).map((r) => r.name)).has("evidences")) await sql`ALTER TABLE tx ADD COLUMN evidences TEXT`.execute(db);
|
|
20
|
+
}
|
|
21
|
+
async function down(db) {
|
|
22
|
+
await sql`ALTER TABLE tx DROP COLUMN evidences`.execute(db);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
//#endregion
|
|
26
|
+
export { _010_tx_evidences_exports, down, up };
|
package/esm/migrations/index.mjs
CHANGED
|
@@ -7,6 +7,7 @@ import { _006_rollup_block_height_integer_exports } from "./006-rollup-block-hei
|
|
|
7
7
|
import { _007_token_factory_feerate_integer_exports } from "./007-token-factory-feerate-integer.mjs";
|
|
8
8
|
import { _008_index_failure_exports } from "./008-index-failure.mjs";
|
|
9
9
|
import { _009_tx_gas_payer_exports } from "./009-tx-gas-payer.mjs";
|
|
10
|
+
import { _010_tx_evidences_exports } from "./010-tx-evidences.mjs";
|
|
10
11
|
import { Migrator } from "kysely";
|
|
11
12
|
|
|
12
13
|
//#region src/migrations/index.ts
|
|
@@ -25,21 +26,34 @@ var InMemoryMigrationProvider = class {
|
|
|
25
26
|
"006-rollup-block-height-integer": _006_rollup_block_height_integer_exports,
|
|
26
27
|
"007-token-factory-feerate-integer": _007_token_factory_feerate_integer_exports,
|
|
27
28
|
"008-index-failure": _008_index_failure_exports,
|
|
28
|
-
"009-tx-gas-payer": _009_tx_gas_payer_exports
|
|
29
|
+
"009-tx-gas-payer": _009_tx_gas_payer_exports,
|
|
30
|
+
"010-tx-evidences": _010_tx_evidences_exports
|
|
29
31
|
};
|
|
30
32
|
}
|
|
31
33
|
};
|
|
34
|
+
function isRetryableMigrationError(error) {
|
|
35
|
+
const code = error?.code;
|
|
36
|
+
if (code === "SQLITE_BUSY" || code === "SQLITE_LOCKED" || code === 5 || code === 6) return true;
|
|
37
|
+
const msg = (error instanceof Error ? error.message : String(error ?? "")).toLowerCase();
|
|
38
|
+
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");
|
|
39
|
+
}
|
|
32
40
|
/**
|
|
33
41
|
* Run all pending migrations
|
|
34
42
|
* @param db - Kysely instance
|
|
35
43
|
*/
|
|
36
44
|
async function runMigrations(db) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
45
|
+
for (let attempt = 0;; attempt++) {
|
|
46
|
+
const { error, results } = await new Migrator({
|
|
47
|
+
db,
|
|
48
|
+
provider: new InMemoryMigrationProvider()
|
|
49
|
+
}).migrateToLatest();
|
|
50
|
+
if (!error) {
|
|
51
|
+
for (const result of results ?? []) if (result.status === "Error") throw new Error(`Migration "${result.migrationName}" failed`);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
if (!isRetryableMigrationError(error) || attempt >= 8) throw error;
|
|
55
|
+
await new Promise((resolve) => setTimeout(resolve, 10 * 2 ** attempt));
|
|
56
|
+
}
|
|
43
57
|
}
|
|
44
58
|
/**
|
|
45
59
|
* Rollback all migrations
|
package/esm/table/base.d.mts
CHANGED
|
@@ -47,6 +47,11 @@ declare class SqliteTable<T = unknown> extends BaseIndex<T> implements IIndexTab
|
|
|
47
47
|
* Handles both simple string keys and composite keys
|
|
48
48
|
*/
|
|
49
49
|
generatePrimaryKey(doc: string | Record<string, unknown>): string;
|
|
50
|
+
/**
|
|
51
|
+
* Check that a primary-key column value is a non-empty string or finite number.
|
|
52
|
+
* Prevents `undefined`/`null`/`''` key fields from silently dropping SQL WHERE clauses (#1438).
|
|
53
|
+
*/
|
|
54
|
+
protected isValidPrimaryKeyValue(v: unknown): boolean;
|
|
50
55
|
/**
|
|
51
56
|
* Map column name for reserved keywords
|
|
52
57
|
*/
|
package/esm/table/base.mjs
CHANGED
|
@@ -27,12 +27,21 @@ var SqliteTable = class extends BaseIndex {
|
|
|
27
27
|
generatePrimaryKey(doc) {
|
|
28
28
|
if (typeof doc === "string") return doc;
|
|
29
29
|
if (Array.isArray(this.uniqIndex)) {
|
|
30
|
-
const key = this.uniqIndex.map((id) => doc[id]).map((item) => typeof item === "object" ? JSON.stringify(item) : item).join("-");
|
|
30
|
+
const key = this.uniqIndex.map((id) => doc[id]).map((item) => typeof item === "object" ? JSON.stringify(item) : String(item ?? "")).map((item) => item.replace(/\\/g, "\\\\").replace(/-/g, "\\-")).join("-");
|
|
31
31
|
return crypto.createHash("md5").update(key).digest("hex");
|
|
32
32
|
}
|
|
33
33
|
return doc[this.uniqIndex];
|
|
34
34
|
}
|
|
35
35
|
/**
|
|
36
|
+
* Check that a primary-key column value is a non-empty string or finite number.
|
|
37
|
+
* Prevents `undefined`/`null`/`''` key fields from silently dropping SQL WHERE clauses (#1438).
|
|
38
|
+
*/
|
|
39
|
+
isValidPrimaryKeyValue(v) {
|
|
40
|
+
if (typeof v === "string") return v.length > 0;
|
|
41
|
+
if (typeof v === "number") return Number.isFinite(v);
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
36
45
|
* Map column name for reserved keywords
|
|
37
46
|
*/
|
|
38
47
|
mapColumnName(name) {
|
|
@@ -189,11 +198,10 @@ var SqliteTable = class extends BaseIndex {
|
|
|
189
198
|
const id = typeof key === "string" ? key : key[this.primaryKey];
|
|
190
199
|
query = { [this.mapColumnName(this.primaryKey)]: id };
|
|
191
200
|
}
|
|
201
|
+
const queryEntries = Object.entries(query);
|
|
202
|
+
if (queryEntries.length === 0 || queryEntries.some(([, v]) => !this.isValidPrimaryKeyValue(v))) return null;
|
|
192
203
|
let qb = this.getExecutor(context).selectFrom(this.name).selectAll();
|
|
193
|
-
for (const [k, v] of
|
|
194
|
-
if (v === void 0) continue;
|
|
195
|
-
qb = qb.where(k, "=", v);
|
|
196
|
-
}
|
|
204
|
+
for (const [k, v] of queryEntries) qb = qb.where(k, "=", v);
|
|
197
205
|
const result = await qb.executeTakeFirst();
|
|
198
206
|
return this.deserializeFromDb(result);
|
|
199
207
|
}
|
|
@@ -210,6 +218,15 @@ var SqliteTable = class extends BaseIndex {
|
|
|
210
218
|
* Internal update implementation - called by BaseIndex.update() via hooks
|
|
211
219
|
*/
|
|
212
220
|
async _update(key, updates, context) {
|
|
221
|
+
if (!key || typeof key === "object" && key === null) throw new Error(`Invalid or incomplete primary key for ${this.name} update: ${JSON.stringify(key)}`);
|
|
222
|
+
if (Array.isArray(this.uniqIndex)) {
|
|
223
|
+
if (typeof key !== "object") throw new Error(`Invalid or incomplete primary key for ${this.name} update: ${JSON.stringify(key)}`);
|
|
224
|
+
const keyObj = key;
|
|
225
|
+
for (const k of this.uniqIndex) if (!this.isValidPrimaryKeyValue(keyObj[k])) throw new Error(`Invalid or incomplete primary key for ${this.name} update: ${JSON.stringify(key)}`);
|
|
226
|
+
} else if (typeof key === "object") {
|
|
227
|
+
const id = key[this.primaryKey];
|
|
228
|
+
if (!this.isValidPrimaryKeyValue(id)) throw new Error(`Invalid or incomplete primary key for ${this.name} update: ${JSON.stringify(key)}`);
|
|
229
|
+
} else if (!this.isValidPrimaryKeyValue(key)) throw new Error(`Invalid or incomplete primary key for ${this.name} update: ${JSON.stringify(key)}`);
|
|
213
230
|
const existing = await this._get(key, context);
|
|
214
231
|
if (!existing) throw new Error(`${this.name} does not exist: ${JSON.stringify(key)}`);
|
|
215
232
|
let query;
|
|
@@ -220,16 +237,15 @@ var SqliteTable = class extends BaseIndex {
|
|
|
220
237
|
const id = typeof key === "string" ? key : key[this.primaryKey];
|
|
221
238
|
query = { [this.mapColumnName(this.primaryKey)]: id };
|
|
222
239
|
}
|
|
240
|
+
const queryEntries = Object.entries(query);
|
|
241
|
+
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)}`);
|
|
223
242
|
const updateAttrs = { ...updates };
|
|
224
243
|
if (Array.isArray(this.uniqIndex)) for (const k of this.uniqIndex) delete updateAttrs[k];
|
|
225
244
|
else delete updateAttrs[this.primaryKey];
|
|
226
245
|
if (Object.keys(updateAttrs).length > 0) {
|
|
227
246
|
const serialized = this.serializeForDbUpdate(updateAttrs, existing);
|
|
228
247
|
let qb = this.getExecutor(context).updateTable(this.name).set(serialized);
|
|
229
|
-
for (const [k, v] of
|
|
230
|
-
if (v === void 0) continue;
|
|
231
|
-
qb = qb.where(k, "=", v);
|
|
232
|
-
}
|
|
248
|
+
for (const [k, v] of queryEntries) qb = qb.where(k, "=", v);
|
|
233
249
|
await qb.execute();
|
|
234
250
|
}
|
|
235
251
|
return {
|
|
@@ -11,7 +11,8 @@ const TX_REF_TYPES = [
|
|
|
11
11
|
["rollup", "rollups"],
|
|
12
12
|
["stake", "stakes"],
|
|
13
13
|
["delegation", "delegations"],
|
|
14
|
-
["tokenFactory", "tokenFactories"]
|
|
14
|
+
["tokenFactory", "tokenFactories"],
|
|
15
|
+
["evidence", "evidences"]
|
|
15
16
|
];
|
|
16
17
|
/**
|
|
17
18
|
* Transaction table for SQLite IndexDB
|
package/lib/db/base.cjs
CHANGED
|
@@ -93,7 +93,7 @@ var SqliteBaseIndexDB = class extends _ocap_indexdb.BaseIndexDB {
|
|
|
93
93
|
const { assets = [] } = assetFilter;
|
|
94
94
|
const { tokens = [] } = tokenFilter;
|
|
95
95
|
const { accounts = [] } = accountFilter;
|
|
96
|
-
const { txs = [], gasPayer: txGasPayer } = txFilter;
|
|
96
|
+
const { txs = [], gasPayer: txGasPayer, evidences = [] } = txFilter;
|
|
97
97
|
const gasPayer = addressGasPayer || txGasPayer || "";
|
|
98
98
|
const { rollups = [] } = rollupFilter;
|
|
99
99
|
const { stakes = [] } = stakeFilter;
|
|
@@ -155,7 +155,8 @@ var SqliteBaseIndexDB = class extends _ocap_indexdb.BaseIndexDB {
|
|
|
155
155
|
rollups: "rollup",
|
|
156
156
|
stakes: "stake",
|
|
157
157
|
delegations: "delegation",
|
|
158
|
-
tokenFactories: "tokenFactory"
|
|
158
|
+
tokenFactories: "tokenFactory",
|
|
159
|
+
evidences: "evidence"
|
|
159
160
|
};
|
|
160
161
|
const applyJsonArrayFilter = (column, values) => {
|
|
161
162
|
const refType = txRefTypeMap[column];
|
|
@@ -169,6 +170,7 @@ var SqliteBaseIndexDB = class extends _ocap_indexdb.BaseIndexDB {
|
|
|
169
170
|
if (stakes.length) applyJsonArrayFilter("stakes", stakes);
|
|
170
171
|
if (delegations.length) applyJsonArrayFilter("delegations", delegations);
|
|
171
172
|
if (tokenFactories.length) applyJsonArrayFilter("tokenFactories", tokenFactories);
|
|
173
|
+
if (evidences.length) applyJsonArrayFilter("evidences", evidences);
|
|
172
174
|
let total;
|
|
173
175
|
const activeJsonFilters = [];
|
|
174
176
|
if (factories.length) activeJsonFilters.push(["factory", factories]);
|
|
@@ -179,6 +181,7 @@ var SqliteBaseIndexDB = class extends _ocap_indexdb.BaseIndexDB {
|
|
|
179
181
|
if (stakes.length) activeJsonFilters.push(["stake", stakes]);
|
|
180
182
|
if (delegations.length) activeJsonFilters.push(["delegation", delegations]);
|
|
181
183
|
if (tokenFactories.length) activeJsonFilters.push(["tokenFactory", tokenFactories]);
|
|
184
|
+
if (evidences.length) activeJsonFilters.push(["evidence", evidences]);
|
|
182
185
|
const hasNonJsonFilters = !!(sender || receiver || gasPayer || types.length || txs.length || parsedStart || parsedEnd || validity);
|
|
183
186
|
const isUnionOnly = isUnion && !gasPayer && !types.length && !txs.length && !parsedStart && !parsedEnd && !validity && activeJsonFilters.length === 0;
|
|
184
187
|
const isMultiTypeOnly = types.length > 1 && !sender && !receiver && !gasPayer && !txs.length && !parsedStart && !parsedEnd && !validity && activeJsonFilters.length === 0;
|
|
@@ -255,6 +258,7 @@ var SqliteBaseIndexDB = class extends _ocap_indexdb.BaseIndexDB {
|
|
|
255
258
|
"stakes",
|
|
256
259
|
"delegations",
|
|
257
260
|
"tokenFactories",
|
|
261
|
+
"evidences",
|
|
258
262
|
"data"
|
|
259
263
|
].forEach((field) => {
|
|
260
264
|
if (typeof tx[field] === "string") try {
|
package/lib/db/index.cjs
CHANGED
|
@@ -61,6 +61,7 @@ var SqliteIndexDB = class extends require_db_base.default {
|
|
|
61
61
|
"stakes",
|
|
62
62
|
"delegations",
|
|
63
63
|
"tokenFactories",
|
|
64
|
+
"evidences",
|
|
64
65
|
"data"
|
|
65
66
|
],
|
|
66
67
|
booleanFields: [
|
|
@@ -93,6 +94,7 @@ var SqliteIndexDB = class extends require_db_base.default {
|
|
|
93
94
|
"stakes",
|
|
94
95
|
"delegations",
|
|
95
96
|
"tokenFactories",
|
|
97
|
+
"evidences",
|
|
96
98
|
"genesisTime",
|
|
97
99
|
"renaissanceTime",
|
|
98
100
|
"data"
|
package/lib/interfaces.d.cts
CHANGED
|
@@ -6,9 +6,16 @@ var _003_delegation_extensions_exports = /* @__PURE__ */ require_rolldown_runtim
|
|
|
6
6
|
down: () => down,
|
|
7
7
|
up: () => up
|
|
8
8
|
});
|
|
9
|
+
async function addColumnIfNotExists(db, ddl) {
|
|
10
|
+
try {
|
|
11
|
+
await ddl.execute(db);
|
|
12
|
+
} catch (err) {
|
|
13
|
+
if (!(err instanceof Error ? err.message : String(err ?? "")).toLowerCase().includes("duplicate column name")) throw err;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
9
16
|
async function up(db) {
|
|
10
|
-
await kysely.sql`ALTER TABLE delegation ADD COLUMN deny TEXT
|
|
11
|
-
await kysely.sql`ALTER TABLE delegation ADD COLUMN validUntil INTEGER DEFAULT 0
|
|
17
|
+
await addColumnIfNotExists(db, kysely.sql`ALTER TABLE delegation ADD COLUMN deny TEXT`);
|
|
18
|
+
await addColumnIfNotExists(db, kysely.sql`ALTER TABLE delegation ADD COLUMN validUntil INTEGER DEFAULT 0`);
|
|
12
19
|
}
|
|
13
20
|
async function down(_db) {}
|
|
14
21
|
|
|
@@ -41,7 +41,11 @@ const MISSING_COLUMNS = [
|
|
|
41
41
|
}
|
|
42
42
|
];
|
|
43
43
|
async function up(db) {
|
|
44
|
-
for (const col of MISSING_COLUMNS)
|
|
44
|
+
for (const col of MISSING_COLUMNS) try {
|
|
45
|
+
await kysely.sql.raw(`ALTER TABLE rollup ADD COLUMN ${col.name} ${col.type}`).execute(db);
|
|
46
|
+
} catch (err) {
|
|
47
|
+
if (!(err instanceof Error ? err.message : String(err ?? "")).toLowerCase().includes("duplicate column name")) throw err;
|
|
48
|
+
}
|
|
45
49
|
await kysely.sql.raw(`UPDATE rollup SET
|
|
46
50
|
minDepositFee = json_extract(data, '$.minDepositFee'),
|
|
47
51
|
maxDepositFee = json_extract(data, '$.maxDepositFee'),
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
let kysely = require("kysely");
|
|
3
|
+
|
|
4
|
+
//#region src/migrations/010-tx-evidences.ts
|
|
5
|
+
var _010_tx_evidences_exports = /* @__PURE__ */ require_rolldown_runtime.__exportAll({
|
|
6
|
+
down: () => down,
|
|
7
|
+
up: () => up
|
|
8
|
+
});
|
|
9
|
+
/**
|
|
10
|
+
* Migration 010 - Add evidences column on tx (#1479)
|
|
11
|
+
*
|
|
12
|
+
* `evidences` is a JSON array of bridge release evidence keys (`<kind>:<evidence>:<ordinal>`), written for
|
|
13
|
+
* `release_bridge_token` txs. Lookups go through the existing tx_ref junction table (type = 'evidence'),
|
|
14
|
+
* which already has the (type, value) index, so no new index is needed. Schema only: no historical rows
|
|
15
|
+
* carry a release tx.
|
|
16
|
+
*/
|
|
17
|
+
async function up(db) {
|
|
18
|
+
const columnsResult = await kysely.sql`PRAGMA table_info('tx')`.execute(db);
|
|
19
|
+
if (!new Set((columnsResult.rows || []).map((r) => r.name)).has("evidences")) await kysely.sql`ALTER TABLE tx ADD COLUMN evidences TEXT`.execute(db);
|
|
20
|
+
}
|
|
21
|
+
async function down(db) {
|
|
22
|
+
await kysely.sql`ALTER TABLE tx DROP COLUMN evidences`.execute(db);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
//#endregion
|
|
26
|
+
Object.defineProperty(exports, '_010_tx_evidences_exports', {
|
|
27
|
+
enumerable: true,
|
|
28
|
+
get: function () {
|
|
29
|
+
return _010_tx_evidences_exports;
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
exports.down = down;
|
|
33
|
+
exports.up = up;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Kysely } from "kysely";
|
|
2
|
+
|
|
3
|
+
//#region src/migrations/010-tx-evidences.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Migration 010 - Add evidences column on tx (#1479)
|
|
7
|
+
*
|
|
8
|
+
* `evidences` is a JSON array of bridge release evidence keys (`<kind>:<evidence>:<ordinal>`), written for
|
|
9
|
+
* `release_bridge_token` txs. Lookups go through the existing tx_ref junction table (type = 'evidence'),
|
|
10
|
+
* which already has the (type, value) index, so no new index is needed. Schema only: no historical rows
|
|
11
|
+
* carry a release tx.
|
|
12
|
+
*/
|
|
13
|
+
declare function up(db: Kysely<unknown>): Promise<void>;
|
|
14
|
+
declare function down(db: Kysely<unknown>): Promise<void>;
|
|
15
|
+
//#endregion
|
|
16
|
+
export { down, up };
|
package/lib/migrations/index.cjs
CHANGED
|
@@ -8,6 +8,7 @@ const require_migrations_006_rollup_block_height_integer = require('./006-rollup
|
|
|
8
8
|
const require_migrations_007_token_factory_feerate_integer = require('./007-token-factory-feerate-integer.cjs');
|
|
9
9
|
const require_migrations_008_index_failure = require('./008-index-failure.cjs');
|
|
10
10
|
const require_migrations_009_tx_gas_payer = require('./009-tx-gas-payer.cjs');
|
|
11
|
+
const require_migrations_010_tx_evidences = require('./010-tx-evidences.cjs');
|
|
11
12
|
let kysely = require("kysely");
|
|
12
13
|
|
|
13
14
|
//#region src/migrations/index.ts
|
|
@@ -26,21 +27,34 @@ var InMemoryMigrationProvider = class {
|
|
|
26
27
|
"006-rollup-block-height-integer": require_migrations_006_rollup_block_height_integer._006_rollup_block_height_integer_exports,
|
|
27
28
|
"007-token-factory-feerate-integer": require_migrations_007_token_factory_feerate_integer._007_token_factory_feerate_integer_exports,
|
|
28
29
|
"008-index-failure": require_migrations_008_index_failure._008_index_failure_exports,
|
|
29
|
-
"009-tx-gas-payer": require_migrations_009_tx_gas_payer._009_tx_gas_payer_exports
|
|
30
|
+
"009-tx-gas-payer": require_migrations_009_tx_gas_payer._009_tx_gas_payer_exports,
|
|
31
|
+
"010-tx-evidences": require_migrations_010_tx_evidences._010_tx_evidences_exports
|
|
30
32
|
};
|
|
31
33
|
}
|
|
32
34
|
};
|
|
35
|
+
function isRetryableMigrationError(error) {
|
|
36
|
+
const code = error?.code;
|
|
37
|
+
if (code === "SQLITE_BUSY" || code === "SQLITE_LOCKED" || code === 5 || code === 6) return true;
|
|
38
|
+
const msg = (error instanceof Error ? error.message : String(error ?? "")).toLowerCase();
|
|
39
|
+
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");
|
|
40
|
+
}
|
|
33
41
|
/**
|
|
34
42
|
* Run all pending migrations
|
|
35
43
|
* @param db - Kysely instance
|
|
36
44
|
*/
|
|
37
45
|
async function runMigrations(db) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
46
|
+
for (let attempt = 0;; attempt++) {
|
|
47
|
+
const { error, results } = await new kysely.Migrator({
|
|
48
|
+
db,
|
|
49
|
+
provider: new InMemoryMigrationProvider()
|
|
50
|
+
}).migrateToLatest();
|
|
51
|
+
if (!error) {
|
|
52
|
+
for (const result of results ?? []) if (result.status === "Error") throw new Error(`Migration "${result.migrationName}" failed`);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (!isRetryableMigrationError(error) || attempt >= 8) throw error;
|
|
56
|
+
await new Promise((resolve) => setTimeout(resolve, 10 * 2 ** attempt));
|
|
57
|
+
}
|
|
44
58
|
}
|
|
45
59
|
/**
|
|
46
60
|
* Rollback all migrations
|
package/lib/table/base.cjs
CHANGED
|
@@ -30,12 +30,21 @@ var SqliteTable = class extends _ocap_indexdb.BaseIndex {
|
|
|
30
30
|
generatePrimaryKey(doc) {
|
|
31
31
|
if (typeof doc === "string") return doc;
|
|
32
32
|
if (Array.isArray(this.uniqIndex)) {
|
|
33
|
-
const key = this.uniqIndex.map((id) => doc[id]).map((item) => typeof item === "object" ? JSON.stringify(item) : item).join("-");
|
|
33
|
+
const key = this.uniqIndex.map((id) => doc[id]).map((item) => typeof item === "object" ? JSON.stringify(item) : String(item ?? "")).map((item) => item.replace(/\\/g, "\\\\").replace(/-/g, "\\-")).join("-");
|
|
34
34
|
return node_crypto.default.createHash("md5").update(key).digest("hex");
|
|
35
35
|
}
|
|
36
36
|
return doc[this.uniqIndex];
|
|
37
37
|
}
|
|
38
38
|
/**
|
|
39
|
+
* Check that a primary-key column value is a non-empty string or finite number.
|
|
40
|
+
* Prevents `undefined`/`null`/`''` key fields from silently dropping SQL WHERE clauses (#1438).
|
|
41
|
+
*/
|
|
42
|
+
isValidPrimaryKeyValue(v) {
|
|
43
|
+
if (typeof v === "string") return v.length > 0;
|
|
44
|
+
if (typeof v === "number") return Number.isFinite(v);
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
39
48
|
* Map column name for reserved keywords
|
|
40
49
|
*/
|
|
41
50
|
mapColumnName(name) {
|
|
@@ -192,11 +201,10 @@ var SqliteTable = class extends _ocap_indexdb.BaseIndex {
|
|
|
192
201
|
const id = typeof key === "string" ? key : key[this.primaryKey];
|
|
193
202
|
query = { [this.mapColumnName(this.primaryKey)]: id };
|
|
194
203
|
}
|
|
204
|
+
const queryEntries = Object.entries(query);
|
|
205
|
+
if (queryEntries.length === 0 || queryEntries.some(([, v]) => !this.isValidPrimaryKeyValue(v))) return null;
|
|
195
206
|
let qb = this.getExecutor(context).selectFrom(this.name).selectAll();
|
|
196
|
-
for (const [k, v] of
|
|
197
|
-
if (v === void 0) continue;
|
|
198
|
-
qb = qb.where(k, "=", v);
|
|
199
|
-
}
|
|
207
|
+
for (const [k, v] of queryEntries) qb = qb.where(k, "=", v);
|
|
200
208
|
const result = await qb.executeTakeFirst();
|
|
201
209
|
return this.deserializeFromDb(result);
|
|
202
210
|
}
|
|
@@ -213,6 +221,15 @@ var SqliteTable = class extends _ocap_indexdb.BaseIndex {
|
|
|
213
221
|
* Internal update implementation - called by BaseIndex.update() via hooks
|
|
214
222
|
*/
|
|
215
223
|
async _update(key, updates, context) {
|
|
224
|
+
if (!key || typeof key === "object" && key === null) throw new Error(`Invalid or incomplete primary key for ${this.name} update: ${JSON.stringify(key)}`);
|
|
225
|
+
if (Array.isArray(this.uniqIndex)) {
|
|
226
|
+
if (typeof key !== "object") throw new Error(`Invalid or incomplete primary key for ${this.name} update: ${JSON.stringify(key)}`);
|
|
227
|
+
const keyObj = key;
|
|
228
|
+
for (const k of this.uniqIndex) if (!this.isValidPrimaryKeyValue(keyObj[k])) throw new Error(`Invalid or incomplete primary key for ${this.name} update: ${JSON.stringify(key)}`);
|
|
229
|
+
} else if (typeof key === "object") {
|
|
230
|
+
const id = key[this.primaryKey];
|
|
231
|
+
if (!this.isValidPrimaryKeyValue(id)) throw new Error(`Invalid or incomplete primary key for ${this.name} update: ${JSON.stringify(key)}`);
|
|
232
|
+
} else if (!this.isValidPrimaryKeyValue(key)) throw new Error(`Invalid or incomplete primary key for ${this.name} update: ${JSON.stringify(key)}`);
|
|
216
233
|
const existing = await this._get(key, context);
|
|
217
234
|
if (!existing) throw new Error(`${this.name} does not exist: ${JSON.stringify(key)}`);
|
|
218
235
|
let query;
|
|
@@ -223,16 +240,15 @@ var SqliteTable = class extends _ocap_indexdb.BaseIndex {
|
|
|
223
240
|
const id = typeof key === "string" ? key : key[this.primaryKey];
|
|
224
241
|
query = { [this.mapColumnName(this.primaryKey)]: id };
|
|
225
242
|
}
|
|
243
|
+
const queryEntries = Object.entries(query);
|
|
244
|
+
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)}`);
|
|
226
245
|
const updateAttrs = { ...updates };
|
|
227
246
|
if (Array.isArray(this.uniqIndex)) for (const k of this.uniqIndex) delete updateAttrs[k];
|
|
228
247
|
else delete updateAttrs[this.primaryKey];
|
|
229
248
|
if (Object.keys(updateAttrs).length > 0) {
|
|
230
249
|
const serialized = this.serializeForDbUpdate(updateAttrs, existing);
|
|
231
250
|
let qb = this.getExecutor(context).updateTable(this.name).set(serialized);
|
|
232
|
-
for (const [k, v] of
|
|
233
|
-
if (v === void 0) continue;
|
|
234
|
-
qb = qb.where(k, "=", v);
|
|
235
|
-
}
|
|
251
|
+
for (const [k, v] of queryEntries) qb = qb.where(k, "=", v);
|
|
236
252
|
await qb.execute();
|
|
237
253
|
}
|
|
238
254
|
return {
|
package/lib/table/base.d.cts
CHANGED
|
@@ -47,6 +47,11 @@ declare class SqliteTable<T = unknown> extends BaseIndex<T> implements IIndexTab
|
|
|
47
47
|
* Handles both simple string keys and composite keys
|
|
48
48
|
*/
|
|
49
49
|
generatePrimaryKey(doc: string | Record<string, unknown>): string;
|
|
50
|
+
/**
|
|
51
|
+
* Check that a primary-key column value is a non-empty string or finite number.
|
|
52
|
+
* Prevents `undefined`/`null`/`''` key fields from silently dropping SQL WHERE clauses (#1438).
|
|
53
|
+
*/
|
|
54
|
+
protected isValidPrimaryKeyValue(v: unknown): boolean;
|
|
50
55
|
/**
|
|
51
56
|
* Map column name for reserved keywords
|
|
52
57
|
*/
|
|
@@ -13,7 +13,8 @@ const TX_REF_TYPES = [
|
|
|
13
13
|
["rollup", "rollups"],
|
|
14
14
|
["stake", "stakes"],
|
|
15
15
|
["delegation", "delegations"],
|
|
16
|
-
["tokenFactory", "tokenFactories"]
|
|
16
|
+
["tokenFactory", "tokenFactories"],
|
|
17
|
+
["evidence", "evidences"]
|
|
17
18
|
];
|
|
18
19
|
/**
|
|
19
20
|
* Transaction table for SQLite IndexDB
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ocap/indexdb-sqlite",
|
|
3
3
|
"description": "OCAP indexdb adapter that uses SQLite as backend",
|
|
4
|
-
"version": "1.31.
|
|
4
|
+
"version": "1.31.2",
|
|
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/indexdb-test": "1.31.
|
|
17
|
+
"@ocap/indexdb-test": "1.31.2"
|
|
18
18
|
},
|
|
19
19
|
"homepage": "https://github.com/ArcBlock/blockchain/tree/master/indexdb/sqlite",
|
|
20
20
|
"keywords": [
|
|
@@ -63,10 +63,10 @@
|
|
|
63
63
|
"clean": "rm -rf lib esm"
|
|
64
64
|
},
|
|
65
65
|
"dependencies": {
|
|
66
|
-
"@arcblock/did": "1.31.
|
|
67
|
-
"@ocap/indexdb": "1.31.
|
|
68
|
-
"@ocap/types": "1.31.
|
|
69
|
-
"@ocap/util": "1.31.
|
|
66
|
+
"@arcblock/did": "1.31.2",
|
|
67
|
+
"@ocap/indexdb": "1.31.2",
|
|
68
|
+
"@ocap/types": "1.31.2",
|
|
69
|
+
"@ocap/util": "1.31.2",
|
|
70
70
|
"debug": "^4.4.3",
|
|
71
71
|
"better-sqlite3": "^11.8.1",
|
|
72
72
|
"kysely": "^0.28.14",
|