@ocap/statedb-sqlite 1.30.25 → 1.31.0

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.
@@ -0,0 +1,833 @@
1
+ import { followMigrationChain } from "@ocap/statedb";
2
+ import { sql } from "kysely";
3
+ import { BN } from "@ocap/util";
4
+ import { ensureChecksumAddress } from "@ocap/state/lib/states/account";
5
+ import { toStakeAddress, toTokenCharterAssetAddress } from "@arcblock/did-util";
6
+
7
+ //#region src/reconcile.ts
8
+ /**
9
+ * Verification and reconcile are SQLite-only (they query SQLite tables through kysely). Fail loudly when given
10
+ * any other backend (e.g. statedb-dolt, statedb-memory) instead of crashing midway or silently skipping.
11
+ */
12
+ function assertSqliteStateDb(db) {
13
+ const candidate = db;
14
+ if (!candidate || typeof candidate.getKysely !== "function") throw new Error("Chain state verify/reconcile only supports @ocap/statedb-sqlite. Dolt and other backends are not supported (read-only verification for Dolt is not implemented).");
15
+ }
16
+ const CANONICAL_UINT_REGEX = /^\d+$/;
17
+ function parseJsonColumn(raw, fallback) {
18
+ if (raw === null || raw === void 0) return fallback;
19
+ if (typeof raw === "object") return raw;
20
+ if (typeof raw === "string") {
21
+ const trimmed = raw.trim();
22
+ if (!trimmed) return fallback;
23
+ try {
24
+ return JSON.parse(trimmed);
25
+ } catch {
26
+ return fallback;
27
+ }
28
+ }
29
+ return fallback;
30
+ }
31
+ /**
32
+ * Verifies all chain state & ledger invariants across `SqliteStateDB` (and optional `ledger`).
33
+ */
34
+ async function verifyChainStateInvariants(db, options = {}) {
35
+ assertSqliteStateDb(db);
36
+ const kysely = db.getKysely();
37
+ const violations = [];
38
+ const [accounts, assets, balances, tokens, tokenFactories, stakes] = await Promise.all([
39
+ kysely.selectFrom("account").selectAll().execute(),
40
+ kysely.selectFrom("asset").selectAll().execute(),
41
+ kysely.selectFrom("balance").selectAll().execute(),
42
+ kysely.selectFrom("token").selectAll().execute(),
43
+ kysely.selectFrom("tokenFactory").selectAll().execute(),
44
+ kysely.selectFrom("stake").selectAll().execute()
45
+ ]);
46
+ const factories = await kysely.selectFrom("factory").selectAll().execute();
47
+ const accountMap = /* @__PURE__ */ new Map();
48
+ for (const acc of accounts) accountMap.set(acc.address, acc);
49
+ const assetMap = /* @__PURE__ */ new Map();
50
+ for (const ast of assets) assetMap.set(ast.address, ast);
51
+ const tokenMap = /* @__PURE__ */ new Map();
52
+ for (const tok of tokens) tokenMap.set(tok.address, tok);
53
+ const stakeMap = /* @__PURE__ */ new Map();
54
+ for (const stk of stakes) stakeMap.set(stk.address, stk);
55
+ const factoryMap = /* @__PURE__ */ new Map();
56
+ for (const fac of factories) factoryMap.set(fac.address, fac);
57
+ const tokenActiveBalanceSums = /* @__PURE__ */ new Map();
58
+ for (const row of balances) {
59
+ const addr = String(row.address || "");
60
+ const tokAddr = String(row.tokenAddress || "");
61
+ const balStr = String(row.balance ?? "");
62
+ const compositeKey = `${addr}:${tokAddr}`;
63
+ const checksumAddr = ensureChecksumAddress(addr);
64
+ if (checksumAddr !== addr) violations.push({
65
+ code: "I-BAL-1",
66
+ severity: "HIGH",
67
+ table: "balance",
68
+ key: compositeKey,
69
+ message: `Non-canonical 0x address casing in balance table: "${addr}" (expected "${checksumAddr}")`,
70
+ details: {
71
+ address: addr,
72
+ expectedAddress: checksumAddr,
73
+ tokenAddress: tokAddr,
74
+ balance: balStr
75
+ },
76
+ reconcilable: true
77
+ });
78
+ if (!CANONICAL_UINT_REGEX.test(balStr)) {
79
+ violations.push({
80
+ code: "I-BAL-1",
81
+ severity: "CRITICAL",
82
+ table: "balance",
83
+ key: compositeKey,
84
+ message: `Malformed or negative balance "${balStr}" for (${addr}, ${tokAddr})`,
85
+ details: {
86
+ address: addr,
87
+ tokenAddress: tokAddr,
88
+ balance: balStr
89
+ },
90
+ reconcilable: false
91
+ });
92
+ continue;
93
+ }
94
+ const bnVal = new BN(balStr, 10);
95
+ if (bnVal.isZero()) violations.push({
96
+ code: "I-BAL-3",
97
+ severity: "MEDIUM",
98
+ table: "balance",
99
+ key: compositeKey,
100
+ message: `Zero-balance row persisted in balance table for (${addr}, ${tokAddr})`,
101
+ details: {
102
+ address: addr,
103
+ tokenAddress: tokAddr,
104
+ balance: balStr
105
+ },
106
+ reconcilable: true
107
+ });
108
+ const holderAccount = accountMap.get(checksumAddr) || accountMap.get(addr);
109
+ const holderStake = stakeMap.get(checksumAddr) || stakeMap.get(addr);
110
+ const holderFactory = factoryMap.get(checksumAddr) || factoryMap.get(addr);
111
+ if (!holderAccount && !holderStake && !holderFactory && bnVal.gt(new BN(0))) violations.push({
112
+ code: "I-BAL-1",
113
+ severity: "HIGH",
114
+ table: "balance",
115
+ key: compositeKey,
116
+ message: `Orphaned positive balance "${balStr}" for non-existent entity "${addr}"`,
117
+ details: {
118
+ address: addr,
119
+ tokenAddress: tokAddr,
120
+ balance: balStr
121
+ },
122
+ reconcilable: false
123
+ });
124
+ if (holderAccount) {
125
+ const migratedTo = parseJsonColumn(holderAccount.migratedTo, []);
126
+ if (migratedTo.length > 0 && bnVal.gt(new BN(0))) violations.push({
127
+ code: "I-BAL-2",
128
+ severity: "CRITICAL",
129
+ table: "balance",
130
+ key: compositeKey,
131
+ message: `Migrated account "${addr}" (migratedTo=${JSON.stringify(migratedTo)}) still retains positive balance "${balStr}" for token "${tokAddr}"`,
132
+ details: {
133
+ address: addr,
134
+ tokenAddress: tokAddr,
135
+ balance: balStr,
136
+ migratedTo
137
+ },
138
+ reconcilable: true
139
+ });
140
+ }
141
+ const prevSum = tokenActiveBalanceSums.get(tokAddr) || new BN(0);
142
+ tokenActiveBalanceSums.set(tokAddr, prevSum.add(bnVal));
143
+ }
144
+ const tokenRevokedSums = /* @__PURE__ */ new Map();
145
+ const globalStakedAssets = /* @__PURE__ */ new Map();
146
+ for (const stk of stakes) {
147
+ const revokedTokens = parseJsonColumn(stk.revokedTokens, {});
148
+ for (const [tokAddr, rawVal] of Object.entries(revokedTokens)) {
149
+ const valStr = String(rawVal ?? "0");
150
+ if (!CANONICAL_UINT_REGEX.test(valStr)) {
151
+ violations.push({
152
+ code: "I-STK-1",
153
+ severity: "CRITICAL",
154
+ table: "stake",
155
+ key: stk.address,
156
+ message: `Stake "${stk.address}" has malformed revokedTokens["${tokAddr}"] = "${valStr}"`,
157
+ details: {
158
+ stakeAddress: stk.address,
159
+ tokenAddress: tokAddr,
160
+ value: valStr
161
+ },
162
+ reconcilable: false
163
+ });
164
+ continue;
165
+ }
166
+ const prev = tokenRevokedSums.get(tokAddr) || new BN(0);
167
+ tokenRevokedSums.set(tokAddr, prev.add(new BN(valStr, 10)));
168
+ }
169
+ const activeAssets = parseJsonColumn(stk.assets, []);
170
+ const revokedAssets = parseJsonColumn(stk.revokedAssets, []);
171
+ const activeSet = new Set(activeAssets);
172
+ for (const revAsset of revokedAssets) if (activeSet.has(revAsset)) violations.push({
173
+ code: "I-STK-1",
174
+ severity: "HIGH",
175
+ table: "stake",
176
+ key: stk.address,
177
+ message: `Asset "${revAsset}" appears in both stake.assets and stake.revokedAssets on stake "${stk.address}"`,
178
+ details: {
179
+ stakeAddress: stk.address,
180
+ assetAddress: revAsset
181
+ },
182
+ reconcilable: true
183
+ });
184
+ for (const assetDid of [...activeAssets, ...revokedAssets]) {
185
+ const priorStake = globalStakedAssets.get(assetDid);
186
+ if (priorStake && priorStake !== stk.address) violations.push({
187
+ code: "I-STK-1",
188
+ severity: "CRITICAL",
189
+ table: "stake",
190
+ key: stk.address,
191
+ message: `Asset "${assetDid}" is listed in multiple stakes ("${priorStake}" and "${stk.address}")`,
192
+ details: {
193
+ assetAddress: assetDid,
194
+ stakes: [priorStake, stk.address]
195
+ },
196
+ reconcilable: false
197
+ });
198
+ globalStakedAssets.set(assetDid, stk.address);
199
+ const assetRow = assetMap.get(assetDid);
200
+ if (!assetRow) violations.push({
201
+ code: "I-STK-1",
202
+ severity: "HIGH",
203
+ table: "stake",
204
+ key: stk.address,
205
+ message: `Stake "${stk.address}" references non-existent asset "${assetDid}"`,
206
+ details: {
207
+ stakeAddress: stk.address,
208
+ assetAddress: assetDid
209
+ },
210
+ reconcilable: true
211
+ });
212
+ else if (assetRow.owner !== stk.address) violations.push({
213
+ code: "I-STK-1",
214
+ severity: "HIGH",
215
+ table: "stake",
216
+ key: stk.address,
217
+ message: `Stake "${stk.address}" lists asset "${assetDid}", but asset.owner is "${assetRow.owner}"`,
218
+ details: {
219
+ stakeAddress: stk.address,
220
+ assetAddress: assetDid,
221
+ actualOwner: assetRow.owner
222
+ },
223
+ reconcilable: true
224
+ });
225
+ }
226
+ }
227
+ for (const tok of tokens) {
228
+ const totalSupplyStr = String(tok.totalSupply ?? "0");
229
+ const maxSupplyStr = String(tok.maxTotalSupply ?? tok.maxSupply ?? "-1");
230
+ if (!CANONICAL_UINT_REGEX.test(totalSupplyStr)) {
231
+ violations.push({
232
+ code: "I-TOK-1",
233
+ severity: "CRITICAL",
234
+ table: "token",
235
+ key: tok.address,
236
+ message: `Token "${tok.address}" has non-canonical totalSupply "${totalSupplyStr}"`,
237
+ details: { totalSupply: totalSupplyStr },
238
+ reconcilable: true
239
+ });
240
+ continue;
241
+ }
242
+ const totalSupplyBn = new BN(totalSupplyStr, 10);
243
+ const activeSum = tokenActiveBalanceSums.get(tok.address) || new BN(0);
244
+ const revokedSum = tokenRevokedSums.get(tok.address) || new BN(0);
245
+ const accountedSum = activeSum.add(revokedSum);
246
+ if (!totalSupplyBn.eq(accountedSum)) violations.push({
247
+ code: "I-TOK-1",
248
+ severity: "CRITICAL",
249
+ table: "token",
250
+ key: tok.address,
251
+ message: `Token "${tok.address}" totalSupply (${totalSupplyBn.toString(10)}) != accounted balances + revokedTokens (${accountedSum.toString(10)})`,
252
+ details: {
253
+ tokenAddress: tok.address,
254
+ totalSupply: totalSupplyBn.toString(10),
255
+ activeBalancesSum: activeSum.toString(10),
256
+ revokedTokensSum: revokedSum.toString(10),
257
+ accountedSum: accountedSum.toString(10)
258
+ },
259
+ reconcilable: true
260
+ });
261
+ if (maxSupplyStr !== "-1" && CANONICAL_UINT_REGEX.test(maxSupplyStr)) {
262
+ const maxSupplyBn = new BN(maxSupplyStr, 10);
263
+ if (totalSupplyBn.gt(maxSupplyBn)) violations.push({
264
+ code: "I-TOK-2",
265
+ severity: "CRITICAL",
266
+ table: "token",
267
+ key: tok.address,
268
+ message: `Token "${tok.address}" totalSupply (${totalSupplyBn.toString(10)}) exceeds maxSupply (${maxSupplyBn.toString(10)})`,
269
+ details: {
270
+ totalSupply: totalSupplyBn.toString(10),
271
+ maxSupply: maxSupplyBn.toString(10)
272
+ },
273
+ reconcilable: false
274
+ });
275
+ }
276
+ try {
277
+ const charterAddress = toTokenCharterAssetAddress(tok.address);
278
+ const charterAsset = assetMap.get(charterAddress);
279
+ if (charterAsset) {
280
+ const charterData = parseJsonColumn(charterAsset.data, null);
281
+ if (charterData?.value?.kind !== "TokenCharter" || charterData?.value?.tokenAddress !== tok.address) violations.push({
282
+ code: "I-TOK-4",
283
+ severity: "HIGH",
284
+ table: "asset",
285
+ key: charterAddress,
286
+ message: `Token Charter Asset "${charterAddress}" for token "${tok.address}" has mismatched charter payload`,
287
+ details: {
288
+ tokenAddress: tok.address,
289
+ charterAddress,
290
+ data: charterData
291
+ },
292
+ reconcilable: true
293
+ });
294
+ }
295
+ } catch {}
296
+ }
297
+ for (const tf of tokenFactories) {
298
+ const tok = tokenMap.get(tf.tokenAddress);
299
+ if (!tok) {
300
+ violations.push({
301
+ code: "I-TOK-3",
302
+ severity: "CRITICAL",
303
+ table: "tokenFactory",
304
+ key: tf.address,
305
+ message: `TokenFactory "${tf.address}" references missing token "${tf.tokenAddress}"`,
306
+ details: {
307
+ factoryAddress: tf.address,
308
+ tokenAddress: tf.tokenAddress
309
+ },
310
+ reconcilable: false
311
+ });
312
+ continue;
313
+ }
314
+ const currentSupplyStr = String(tf.currentSupply ?? "0");
315
+ const reserveBalanceStr = String(tf.reserveBalance ?? "0");
316
+ const tokActiveSum = tokenActiveBalanceSums.get(tf.tokenAddress) || new BN(0);
317
+ const tokRevokedSum = tokenRevokedSums.get(tf.tokenAddress) || new BN(0);
318
+ const tokAccountedSum = tokActiveSum.add(tokRevokedSum).toString(10);
319
+ if (Number(tf.status || 0) === 0 && (currentSupplyStr !== String(tok.totalSupply ?? "0") || currentSupplyStr !== tokAccountedSum)) violations.push({
320
+ code: "I-TOK-3",
321
+ severity: "CRITICAL",
322
+ table: "tokenFactory",
323
+ key: tf.address,
324
+ message: `Active TokenFactory "${tf.address}" currentSupply (${currentSupplyStr}) != token.totalSupply (${tok.totalSupply}) / accounted supply (${tokAccountedSum})`,
325
+ details: {
326
+ factoryAddress: tf.address,
327
+ tokenAddress: tf.tokenAddress,
328
+ currentSupply: currentSupplyStr,
329
+ tokenTotalSupply: tok.totalSupply,
330
+ accountedSupply: tokAccountedSum
331
+ },
332
+ reconcilable: true
333
+ });
334
+ if (Number(tf.status || 0) === 0 && CANONICAL_UINT_REGEX.test(reserveBalanceStr)) {
335
+ const reserveBn = new BN(reserveBalanceStr, 10);
336
+ if (reserveBn.gt(new BN(0))) try {
337
+ const reserveStakeAddr = toStakeAddress(tf.address, tf.reserveAddress);
338
+ const actualStakeReserve = new BN((await db.balance.getBalance(reserveStakeAddr))[tf.reserveAddress] || "0", 10);
339
+ if (actualStakeReserve.lt(reserveBn)) violations.push({
340
+ code: "I-TOK-3",
341
+ severity: "CRITICAL",
342
+ table: "tokenFactory",
343
+ key: tf.address,
344
+ message: `TokenFactory "${tf.address}" reserveBalance (${reserveBn.toString(10)}) exceeds reserve stake "${reserveStakeAddr}" balance (${actualStakeReserve.toString(10)})`,
345
+ details: {
346
+ factoryAddress: tf.address,
347
+ reserveStakeAddr,
348
+ expectedReserve: reserveBn.toString(10),
349
+ actualStakeReserve: actualStakeReserve.toString(10)
350
+ },
351
+ reconcilable: false
352
+ });
353
+ } catch {}
354
+ }
355
+ }
356
+ for (const ast of assets) {
357
+ const ownerAcc = accountMap.get(ast.owner);
358
+ if (ownerAcc) {
359
+ const migratedTo = parseJsonColumn(ownerAcc.migratedTo, []);
360
+ if (migratedTo.length > 0) violations.push({
361
+ code: "I-AST-1",
362
+ severity: "HIGH",
363
+ table: "asset",
364
+ key: ast.address,
365
+ message: `Asset "${ast.address}" is still owned by migrated account "${ast.owner}" (migratedTo=${JSON.stringify(migratedTo)})`,
366
+ details: {
367
+ assetAddress: ast.address,
368
+ owner: ast.owner,
369
+ migratedTo
370
+ },
371
+ reconcilable: true
372
+ });
373
+ }
374
+ }
375
+ for (const acc of accounts) {
376
+ const migratedTo = parseJsonColumn(acc.migratedTo, []);
377
+ if (migratedTo.length > 0) {
378
+ if (migratedTo.length > 1) violations.push({
379
+ code: "I-MIG-1",
380
+ severity: "HIGH",
381
+ table: "account",
382
+ key: acc.address,
383
+ message: `Account "${acc.address}" migratedTo contains multiple entries (${JSON.stringify(migratedTo)}); linear migration requires a single immediate successor`,
384
+ details: {
385
+ address: acc.address,
386
+ migratedTo
387
+ },
388
+ reconcilable: false
389
+ });
390
+ const nextHop = ensureChecksumAddress(migratedTo[0]);
391
+ const targetAcc = accountMap.get(nextHop);
392
+ if (!targetAcc) {
393
+ violations.push({
394
+ code: "I-MIG-1",
395
+ severity: "HIGH",
396
+ table: "account",
397
+ key: acc.address,
398
+ message: `Account "${acc.address}" migratedTo references non-existent account "${nextHop}"`,
399
+ details: {
400
+ address: acc.address,
401
+ nextHop
402
+ },
403
+ reconcilable: false
404
+ });
405
+ continue;
406
+ }
407
+ if (!await followMigrationChain(acc.address, async (addr) => {
408
+ const row = accountMap.get(addr);
409
+ if (!row) return null;
410
+ return {
411
+ address: row.address,
412
+ migratedTo: parseJsonColumn(row.migratedTo, [])
413
+ };
414
+ }, ensureChecksumAddress)) violations.push({
415
+ code: "I-MIG-1",
416
+ severity: "CRITICAL",
417
+ table: "account",
418
+ key: acc.address,
419
+ message: `Account migration cycle or broken chain detected starting at "${acc.address}"`,
420
+ details: {
421
+ address: acc.address,
422
+ migratedTo
423
+ },
424
+ reconcilable: false
425
+ });
426
+ const targetMigratedFrom = parseJsonColumn(targetAcc.migratedFrom, []);
427
+ if (!targetMigratedFrom.includes(acc.address)) violations.push({
428
+ code: "I-MIG-1",
429
+ severity: "HIGH",
430
+ table: "account",
431
+ key: acc.address,
432
+ message: `Asymmetric migration link: "${acc.address}" -> "${nextHop}", but "${nextHop}".migratedFrom does not contain "${acc.address}"`,
433
+ details: {
434
+ from: acc.address,
435
+ to: nextHop,
436
+ targetMigratedFrom
437
+ },
438
+ reconcilable: true
439
+ });
440
+ }
441
+ }
442
+ try {
443
+ await db.verifyTrustedHash();
444
+ } catch (err) {
445
+ violations.push({
446
+ code: "I-LDG-1",
447
+ severity: "CRITICAL",
448
+ table: "_state_commit",
449
+ key: "1",
450
+ message: `StateDB trustedHash verification failed: ${err?.message || String(err)}`,
451
+ details: { code: err?.code },
452
+ reconcilable: false
453
+ });
454
+ }
455
+ if (options.ledger && typeof options.ledger.verifyChain === "function") {
456
+ const ledgerCheck = await options.ledger.verifyChain();
457
+ if (!ledgerCheck.valid) violations.push({
458
+ code: "I-LDG-2",
459
+ severity: "CRITICAL",
460
+ table: "ledger_entries",
461
+ key: "chain",
462
+ message: `Ledger hash-chain verification failed: ${ledgerCheck.error || "invalid chain"}`,
463
+ details: ledgerCheck,
464
+ reconcilable: false
465
+ });
466
+ }
467
+ const critical = violations.filter((v) => v.severity === "CRITICAL").length;
468
+ const high = violations.filter((v) => v.severity === "HIGH").length;
469
+ const medium = violations.filter((v) => v.severity === "MEDIUM").length;
470
+ return {
471
+ valid: violations.length === 0,
472
+ checkedAt: (/* @__PURE__ */ new Date()).toISOString(),
473
+ counts: {
474
+ accounts: accounts.length,
475
+ assets: assets.length,
476
+ balances: balances.length,
477
+ tokens: tokens.length,
478
+ tokenFactories: tokenFactories.length,
479
+ stakes: stakes.length,
480
+ violations: violations.length,
481
+ critical,
482
+ high,
483
+ medium
484
+ },
485
+ violations
486
+ };
487
+ }
488
+ /**
489
+ * Reconciles deterministically repairable state anomalies in `SqliteStateDB`.
490
+ * By default runs in `dryRun: true` preview mode; when `dryRun: false` is passed,
491
+ * executes all repairs atomically inside `db.runAsLambda()`.
492
+ */
493
+ async function reconcileChainState(db, options = {}) {
494
+ assertSqliteStateDb(db);
495
+ const dryRun = options.dryRun !== false;
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
+ const beforeReport = await verifyChainStateInvariants(db, { ledger: options.ledger });
498
+ const actions = [];
499
+ if (beforeReport.valid) return {
500
+ dryRun,
501
+ reconciled: true,
502
+ beforeReport,
503
+ afterReport: beforeReport,
504
+ actions
505
+ };
506
+ const executeRepairs = async (executor) => {
507
+ const allBalances = await executor.selectFrom("balance").selectAll().execute();
508
+ for (const row of allBalances) {
509
+ const addr = String(row.address || "");
510
+ const tokAddr = String(row.tokenAddress || "");
511
+ const balStr = String(row.balance ?? "0");
512
+ const checksumAddr = ensureChecksumAddress(addr);
513
+ if (CANONICAL_UINT_REGEX.test(balStr) && new BN(balStr, 10).isZero()) {
514
+ actions.push({
515
+ code: "I-BAL-3",
516
+ table: "balance",
517
+ key: `${addr}:${tokAddr}`,
518
+ action: "delete_zero_balance_row",
519
+ before: {
520
+ address: addr,
521
+ tokenAddress: tokAddr,
522
+ balance: balStr
523
+ },
524
+ after: null
525
+ });
526
+ if (!dryRun) await executor.deleteFrom("balance").where("address", "=", addr).where("tokenAddress", "=", tokAddr).execute();
527
+ continue;
528
+ }
529
+ if (checksumAddr !== addr && CANONICAL_UINT_REGEX.test(balStr)) {
530
+ const existingCanonical = await executor.selectFrom("balance").selectAll().where("address", "=", checksumAddr).where("tokenAddress", "=", tokAddr).executeTakeFirst();
531
+ 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
+ code: "I-BAL-1",
534
+ table: "balance",
535
+ key: `${addr}:${tokAddr}`,
536
+ action: "normalize_checksum_balance_row",
537
+ before: {
538
+ address: addr,
539
+ tokenAddress: tokAddr,
540
+ balance: balStr
541
+ },
542
+ after: {
543
+ address: checksumAddr,
544
+ tokenAddress: tokAddr,
545
+ balance: mergedBalance
546
+ }
547
+ });
548
+ if (!dryRun) {
549
+ await executor.deleteFrom("balance").where("address", "=", addr).where("tokenAddress", "=", tokAddr).execute();
550
+ await executor.insertInto("balance").values({
551
+ address: checksumAddr,
552
+ tokenAddress: tokAddr,
553
+ balance: mergedBalance,
554
+ context: row.context || "{}"
555
+ }).onConflict((oc) => oc.columns(["address", "tokenAddress"]).doUpdateSet({ balance: mergedBalance })).execute();
556
+ }
557
+ }
558
+ }
559
+ const accounts = await executor.selectFrom("account").selectAll().execute();
560
+ const accountMap = new Map(accounts.map((a) => [a.address, a]));
561
+ const tokensForMigration = await executor.selectFrom("token").selectAll().execute();
562
+ const declaredSupplyMap = /* @__PURE__ */ new Map();
563
+ for (const t of tokensForMigration) if (CANONICAL_UINT_REGEX.test(String(t.totalSupply ?? ""))) declaredSupplyMap.set(t.address, new BN(String(t.totalSupply), 10));
564
+ const preMigrationBalances = await executor.selectFrom("balance").selectAll().execute();
565
+ const preMigrationStakes = await executor.selectFrom("stake").selectAll().execute();
566
+ const runningAccountedSupply = /* @__PURE__ */ new Map();
567
+ const liveBalanceMap = /* @__PURE__ */ new Map();
568
+ for (const b of preMigrationBalances) if (CANONICAL_UINT_REGEX.test(String(b.balance ?? ""))) {
569
+ const bnVal = new BN(String(b.balance), 10);
570
+ const prev = runningAccountedSupply.get(b.tokenAddress) || new BN(0);
571
+ runningAccountedSupply.set(b.tokenAddress, prev.add(bnVal));
572
+ const balKey = `${ensureChecksumAddress(String(b.address || ""))}:${b.tokenAddress}`;
573
+ liveBalanceMap.set(balKey, (liveBalanceMap.get(balKey) || new BN(0)).add(bnVal));
574
+ }
575
+ for (const s of preMigrationStakes) {
576
+ const rev = parseJsonColumn(s.revokedTokens, {});
577
+ for (const [tAddr, val] of Object.entries(rev)) if (CANONICAL_UINT_REGEX.test(String(val ?? ""))) {
578
+ const prev = runningAccountedSupply.get(tAddr) || new BN(0);
579
+ runningAccountedSupply.set(tAddr, prev.add(new BN(String(val), 10)));
580
+ }
581
+ }
582
+ for (const acc of accounts) {
583
+ const migratedTo = parseJsonColumn(acc.migratedTo, []);
584
+ if (migratedTo.length === 0) continue;
585
+ const terminal = await followMigrationChain(acc.address, async (addr) => {
586
+ const row = accountMap.get(addr);
587
+ if (!row) return null;
588
+ return {
589
+ address: row.address,
590
+ migratedTo: parseJsonColumn(row.migratedTo, [])
591
+ };
592
+ }, ensureChecksumAddress);
593
+ if (!terminal || !terminal.address || terminal.address === acc.address) continue;
594
+ const terminalAddr = terminal.address;
595
+ if (!accountMap.get(terminalAddr)) continue;
596
+ const nextHop = ensureChecksumAddress(migratedTo[0]);
597
+ const nextAcc = accountMap.get(nextHop);
598
+ if (nextAcc) {
599
+ const nextMigratedFrom = parseJsonColumn(nextAcc.migratedFrom, []);
600
+ if (!nextMigratedFrom.includes(acc.address)) {
601
+ const updatedFrom = [...nextMigratedFrom, acc.address];
602
+ nextAcc.migratedFrom = JSON.stringify(updatedFrom);
603
+ actions.push({
604
+ code: "I-MIG-1",
605
+ table: "account",
606
+ key: nextHop,
607
+ action: "restore_symmetric_migratedFrom",
608
+ before: nextMigratedFrom,
609
+ after: updatedFrom
610
+ });
611
+ if (!dryRun) await executor.updateTable("account").set({ migratedFrom: JSON.stringify(updatedFrom) }).where("address", "=", nextHop).execute();
612
+ }
613
+ }
614
+ const zombieRows = await executor.selectFrom("balance").selectAll().where("address", "=", acc.address).execute();
615
+ for (const zRow of zombieRows) {
616
+ const tokAddr = String(zRow.tokenAddress || "");
617
+ const zBalStr = String(zRow.balance ?? "0");
618
+ if (!CANONICAL_UINT_REGEX.test(zBalStr)) continue;
619
+ const zBal = new BN(zBalStr, 10);
620
+ if (zBal.isZero()) continue;
621
+ const termBalKey = `${ensureChecksumAddress(terminalAddr)}:${tokAddr}`;
622
+ const termBal = liveBalanceMap.get(termBalKey) || new BN(0);
623
+ const declaredSupply = declaredSupplyMap.get(tokAddr) ?? null;
624
+ const accountedSupply = runningAccountedSupply.get(tokAddr) || new BN(0);
625
+ const excessSupply = declaredSupply === null ? termBal.gte(zBal) ? zBal : new BN(0) : accountedSupply.gt(declaredSupply) ? accountedSupply.sub(declaredSupply) : new BN(0);
626
+ const zombiePortion = termBal.gte(zBal) ? BN.min(zBal, excessSupply) : new BN(0);
627
+ const forwardPortion = zBal.sub(zombiePortion);
628
+ if (zombiePortion.gt(new BN(0))) runningAccountedSupply.set(tokAddr, accountedSupply.sub(zombiePortion));
629
+ if (forwardPortion.isZero()) {
630
+ actions.push({
631
+ code: "I-BAL-2",
632
+ table: "balance",
633
+ key: `${acc.address}:${tokAddr}`,
634
+ action: "delete_migrated_account_zombie_balance",
635
+ before: {
636
+ address: acc.address,
637
+ tokenAddress: tokAddr,
638
+ balance: zBalStr
639
+ },
640
+ after: null
641
+ });
642
+ if (!dryRun) await executor.deleteFrom("balance").where("address", "=", acc.address).where("tokenAddress", "=", tokAddr).execute();
643
+ } else {
644
+ const nextTermBalBn = termBal.add(forwardPortion);
645
+ const nextTermBal = nextTermBalBn.toString(10);
646
+ liveBalanceMap.set(termBalKey, nextTermBalBn);
647
+ actions.push({
648
+ code: "I-BAL-2",
649
+ table: "balance",
650
+ key: `${acc.address}:${tokAddr}`,
651
+ action: "forward_migrated_account_balance",
652
+ before: {
653
+ from: {
654
+ address: acc.address,
655
+ balance: zBalStr
656
+ },
657
+ to: {
658
+ address: terminalAddr,
659
+ balance: termBal.toString(10)
660
+ }
661
+ },
662
+ after: {
663
+ from: null,
664
+ to: {
665
+ address: terminalAddr,
666
+ balance: nextTermBal
667
+ },
668
+ deletedDuplicateZombiePortion: zombiePortion.toString(10)
669
+ }
670
+ });
671
+ if (!dryRun) {
672
+ await executor.deleteFrom("balance").where("address", "=", acc.address).where("tokenAddress", "=", tokAddr).execute();
673
+ await executor.insertInto("balance").values({
674
+ address: terminalAddr,
675
+ tokenAddress: tokAddr,
676
+ balance: nextTermBal,
677
+ context: zRow.context || "{}"
678
+ }).onConflict((oc) => oc.columns(["address", "tokenAddress"]).doUpdateSet({ balance: nextTermBal })).execute();
679
+ }
680
+ }
681
+ }
682
+ const orphanAssets = await executor.selectFrom("asset").selectAll().where("owner", "=", acc.address).execute();
683
+ for (const ast of orphanAssets) {
684
+ actions.push({
685
+ code: "I-AST-1",
686
+ table: "asset",
687
+ key: ast.address,
688
+ action: "forward_asset_to_migrated_terminal_owner",
689
+ before: { owner: acc.address },
690
+ after: { owner: terminalAddr }
691
+ });
692
+ if (!dryRun) await executor.updateTable("asset").set({ owner: terminalAddr }).where("address", "=", ast.address).execute();
693
+ }
694
+ }
695
+ const stakes = await executor.selectFrom("stake").selectAll().execute();
696
+ const assets = await executor.selectFrom("asset").selectAll().execute();
697
+ const assetMap = new Map(assets.map((a) => [a.address, a]));
698
+ for (const stk of stakes) {
699
+ const activeAssets = parseJsonColumn(stk.assets, []);
700
+ const revokedAssets = parseJsonColumn(stk.revokedAssets, []);
701
+ const validRevoked = revokedAssets.filter((id) => {
702
+ const a = assetMap.get(id);
703
+ return Boolean(a && a.owner === stk.address);
704
+ });
705
+ const revokedSet = new Set(validRevoked);
706
+ const validActive = activeAssets.filter((id) => {
707
+ if (revokedSet.has(id)) return false;
708
+ const a = assetMap.get(id);
709
+ return Boolean(a && a.owner === stk.address);
710
+ });
711
+ if (validActive.length !== activeAssets.length || validRevoked.length !== revokedAssets.length) {
712
+ actions.push({
713
+ code: "I-STK-1",
714
+ table: "stake",
715
+ key: stk.address,
716
+ action: "prune_dangling_or_duplicate_stake_assets",
717
+ before: {
718
+ assets: activeAssets,
719
+ revokedAssets
720
+ },
721
+ after: {
722
+ assets: validActive,
723
+ revokedAssets: validRevoked
724
+ }
725
+ });
726
+ if (!dryRun) await executor.updateTable("stake").set({
727
+ assets: JSON.stringify(validActive),
728
+ revokedAssets: JSON.stringify(validRevoked)
729
+ }).where("address", "=", stk.address).execute();
730
+ }
731
+ }
732
+ const tokens = await executor.selectFrom("token").selectAll().execute();
733
+ for (const tok of tokens) try {
734
+ const charterAddr = toTokenCharterAssetAddress(tok.address);
735
+ const charter = assetMap.get(charterAddr);
736
+ if (charter) {
737
+ const charterData = parseJsonColumn(charter.data, null);
738
+ if (charterData?.value?.kind !== "TokenCharter" || charterData?.value?.tokenAddress !== tok.address) {
739
+ const repairedData = {
740
+ type: "json",
741
+ value: {
742
+ ...charterData?.value || {},
743
+ kind: "TokenCharter",
744
+ tokenAddress: tok.address,
745
+ symbol: tok.symbol,
746
+ name: tok.name
747
+ }
748
+ };
749
+ actions.push({
750
+ code: "I-TOK-4",
751
+ table: "asset",
752
+ key: charterAddr,
753
+ action: "repair_token_charter_payload",
754
+ before: charterData,
755
+ after: repairedData
756
+ });
757
+ if (!dryRun) await executor.updateTable("asset").set({ data: JSON.stringify(repairedData) }).where("address", "=", charterAddr).execute();
758
+ }
759
+ }
760
+ } catch {}
761
+ if (options.reconcileTokenSupply) {
762
+ const freshBalances = await executor.selectFrom("balance").selectAll().execute();
763
+ const freshStakes = await executor.selectFrom("stake").selectAll().execute();
764
+ const sums = /* @__PURE__ */ new Map();
765
+ for (const b of freshBalances) if (CANONICAL_UINT_REGEX.test(String(b.balance ?? ""))) {
766
+ const prev = sums.get(b.tokenAddress) || new BN(0);
767
+ sums.set(b.tokenAddress, prev.add(new BN(String(b.balance), 10)));
768
+ }
769
+ for (const s of freshStakes) {
770
+ const rev = parseJsonColumn(s.revokedTokens, {});
771
+ for (const [tAddr, val] of Object.entries(rev)) if (CANONICAL_UINT_REGEX.test(String(val ?? ""))) {
772
+ const prev = sums.get(tAddr) || new BN(0);
773
+ sums.set(tAddr, prev.add(new BN(String(val), 10)));
774
+ }
775
+ }
776
+ for (const tok of tokens) {
777
+ const expected = (sums.get(tok.address) || new BN(0)).toString(10);
778
+ if (String(tok.totalSupply ?? "0") !== expected) {
779
+ actions.push({
780
+ code: "I-TOK-1",
781
+ table: "token",
782
+ key: tok.address,
783
+ action: "reconcile_token_total_supply",
784
+ before: { totalSupply: tok.totalSupply },
785
+ after: { totalSupply: expected }
786
+ });
787
+ if (!dryRun) await executor.updateTable("token").set({ totalSupply: expected }).where("address", "=", tok.address).execute();
788
+ }
789
+ }
790
+ const tokenFactories = await executor.selectFrom("tokenFactory").selectAll().execute();
791
+ for (const tf of tokenFactories) {
792
+ if (Number(tf.status || 0) !== 0) continue;
793
+ const expected = (sums.get(tf.tokenAddress) || new BN(0)).toString(10);
794
+ if (String(tf.currentSupply ?? "0") !== expected) {
795
+ actions.push({
796
+ code: "I-TOK-3",
797
+ table: "tokenFactory",
798
+ key: tf.address,
799
+ action: "reconcile_token_factory_current_supply",
800
+ before: { currentSupply: tf.currentSupply },
801
+ after: { currentSupply: expected }
802
+ });
803
+ if (!dryRun) await executor.updateTable("tokenFactory").set({ currentSupply: expected }).where("address", "=", tf.address).execute();
804
+ }
805
+ }
806
+ }
807
+ };
808
+ if (dryRun) {
809
+ await executeRepairs(db.getKysely());
810
+ return {
811
+ dryRun: true,
812
+ reconciled: false,
813
+ beforeReport,
814
+ afterReport: beforeReport,
815
+ actions
816
+ };
817
+ }
818
+ await db.runAsLambda(async (txn) => {
819
+ await executeRepairs(txn);
820
+ }, { commitMessage: "reconcile_chain_state" });
821
+ await sql`PRAGMA wal_checkpoint(PASSIVE)`.execute(db.getKysely()).catch(() => {});
822
+ const afterReport = await verifyChainStateInvariants(db, { ledger: options.ledger });
823
+ return {
824
+ dryRun: false,
825
+ reconciled: afterReport.valid,
826
+ beforeReport,
827
+ afterReport,
828
+ actions
829
+ };
830
+ }
831
+
832
+ //#endregion
833
+ export { assertSqliteStateDb, reconcileChainState, verifyChainStateInvariants };