@odatano/core 2.0.0-rc.5 → 2.0.0-rc.51
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/@cds-models/CardanoAgentService/index.d.ts +99 -51
- package/@cds-models/CardanoAgentService/index.js +15 -0
- package/@cds-models/CardanoBackendService/index.d.ts +358 -0
- package/@cds-models/CardanoBackendService/index.js +41 -0
- package/@cds-models/CardanoIndexerService/index.d.ts +594 -11
- package/@cds-models/CardanoIndexerService/index.js +74 -0
- package/@cds-models/CardanoODataService/index.d.ts +417 -57
- package/@cds-models/CardanoODataService/index.js +42 -0
- package/@cds-models/CardanoSignService/index.d.ts +45 -19
- package/@cds-models/CardanoSignService/index.js +4 -0
- package/@cds-models/CardanoTransactionService/index.d.ts +70 -44
- package/@cds-models/CardanoTransactionService/index.js +5 -0
- package/@cds-models/CardanoWorkerService/index.d.ts +35 -22
- package/@cds-models/CardanoWorkerService/index.js +2 -0
- package/@cds-models/index.d.ts +114 -4
- package/@cds-models/index.js +20 -4
- package/@cds-models/odatano/cardano/index.d.ts +451 -32
- package/@cds-models/odatano/cardano/index.js +48 -0
- package/README.md +103 -167
- package/db/schema.cds +2455 -1705
- package/db/types.cds +456 -295
- package/package.json +41 -53
- package/scripts/deploy-schema.mjs +113 -0
- package/scripts/migrate-schema.mjs +528 -0
- package/scripts/migrate-slot-leader.mjs +96 -0
- package/src/index.d.ts +20 -21
- package/src/index.js +45 -31
- package/src/plugin.js +17 -35
- package/srv/blockchain/asset-decimals.d.ts +7 -0
- package/srv/blockchain/asset-decimals.js +91 -0
- package/srv/blockchain/backends/blockfrost-backend.d.ts +25 -137
- package/srv/blockchain/backends/blockfrost-backend.js +125 -215
- package/srv/blockchain/backends/call-signal.d.ts +7 -0
- package/srv/blockchain/backends/call-signal.js +22 -0
- package/srv/blockchain/backends/cardano-backend.d.ts +100 -189
- package/srv/blockchain/backends/cardano-backend.js +29 -5
- package/srv/blockchain/backends/koios-backend.d.ts +108 -170
- package/srv/blockchain/backends/koios-backend.js +488 -482
- package/srv/blockchain/backends/odatano-backend.d.ts +55 -0
- package/srv/blockchain/backends/odatano-backend.js +211 -0
- package/srv/blockchain/backends/ogmios-backend.d.ts +122 -190
- package/srv/blockchain/backends/ogmios-backend.js +683 -327
- package/srv/blockchain/backends/ogmios-frame-guard.d.ts +14 -0
- package/srv/blockchain/backends/ogmios-frame-guard.js +89 -0
- package/srv/blockchain/cardano-client.d.ts +71 -241
- package/srv/blockchain/cardano-client.js +354 -322
- package/srv/blockchain/cardano-indexer.d.ts +295 -283
- package/srv/blockchain/cardano-indexer.js +1690 -502
- package/srv/blockchain/cardano-tx-builder.d.ts +25 -89
- package/srv/blockchain/cardano-tx-builder.js +164 -140
- package/srv/blockchain/circuit-breaker.d.ts +2 -13
- package/srv/blockchain/circuit-breaker.js +11 -15
- package/srv/blockchain/crawler/account-snapshots.d.ts +29 -0
- package/srv/blockchain/crawler/account-snapshots.js +173 -0
- package/srv/blockchain/crawler/asset-decimals-backfill.d.ts +17 -0
- package/srv/blockchain/crawler/asset-decimals-backfill.js +56 -0
- package/srv/blockchain/crawler/asset-registry-backfill.d.ts +46 -0
- package/srv/blockchain/crawler/asset-registry-backfill.js +157 -0
- package/srv/blockchain/crawler/asset-slice-backfill.d.ts +35 -0
- package/srv/blockchain/crawler/asset-slice-backfill.js +149 -0
- package/srv/blockchain/crawler/certificate-backfill.d.ts +56 -0
- package/srv/blockchain/crawler/certificate-backfill.js +174 -0
- package/srv/blockchain/crawler/crawler.d.ts +125 -81
- package/srv/blockchain/crawler/crawler.js +738 -167
- package/srv/blockchain/crawler/credential-backfill.d.ts +30 -0
- package/srv/blockchain/crawler/credential-backfill.js +101 -0
- package/srv/blockchain/crawler/hooks.d.ts +2 -17
- package/srv/blockchain/crawler/index.d.ts +1 -8
- package/srv/blockchain/crawler/index.js +31 -17
- package/srv/blockchain/crawler/input-asset-copies.d.ts +34 -0
- package/srv/blockchain/crawler/input-asset-copies.js +161 -0
- package/srv/blockchain/crawler/key-pages.d.ts +12 -0
- package/srv/blockchain/crawler/key-pages.js +48 -0
- package/srv/blockchain/crawler/metadata-position-backfill.d.ts +22 -0
- package/srv/blockchain/crawler/metadata-position-backfill.js +59 -0
- package/srv/blockchain/crawler/output-datum-backfill.d.ts +41 -0
- package/srv/blockchain/crawler/output-datum-backfill.js +100 -0
- package/srv/blockchain/crawler/sync-state.d.ts +31 -47
- package/srv/blockchain/crawler/sync-state.js +135 -128
- package/srv/blockchain/crawler/transaction-backfill.d.ts +46 -0
- package/srv/blockchain/crawler/transaction-backfill.js +271 -0
- package/srv/blockchain/crawler/utxo-set-import.d.ts +66 -0
- package/srv/blockchain/crawler/utxo-set-import.js +399 -0
- package/srv/blockchain/crawler/window-plan.d.ts +31 -0
- package/srv/blockchain/crawler/window-plan.js +59 -0
- package/srv/blockchain/ledger-state.d.ts +62 -0
- package/srv/blockchain/ledger-state.js +521 -0
- package/srv/blockchain/pending-spends.d.ts +16 -0
- package/srv/blockchain/pending-spends.js +81 -0
- package/srv/blockchain/pool-metadata.d.ts +15 -0
- package/srv/blockchain/pool-metadata.js +132 -0
- package/srv/blockchain/request-coalescer.d.ts +2 -16
- package/srv/blockchain/request-coalescer.js +2 -16
- package/srv/blockchain/signing/cose-verifier.d.ts +3 -17
- package/srv/blockchain/signing/cose-verifier.js +146 -70
- package/srv/blockchain/signing/external-signer.d.ts +15 -115
- package/srv/blockchain/signing/external-signer.js +22 -131
- package/srv/blockchain/signing/failed-recheck.d.ts +17 -0
- package/srv/blockchain/signing/failed-recheck.js +135 -0
- package/srv/blockchain/signing/hsm-signer.d.ts +10 -54
- package/srv/blockchain/signing/hsm-signer.js +28 -79
- package/srv/blockchain/signing/landing-proof.d.ts +60 -0
- package/srv/blockchain/signing/landing-proof.js +224 -0
- package/srv/blockchain/signing/signature-verifier.d.ts +9 -47
- package/srv/blockchain/signing/signature-verifier.js +71 -87
- package/srv/blockchain/signing/submission-finalizer.d.ts +37 -45
- package/srv/blockchain/signing/submission-finalizer.js +280 -86
- package/srv/blockchain/stored-chain.d.ts +19 -0
- package/srv/blockchain/stored-chain.js +258 -0
- package/srv/blockchain/transaction-building/buildooor-tx.d.ts +31 -229
- package/srv/blockchain/transaction-building/buildooor-tx.js +663 -421
- package/srv/blockchain/transaction-building/cardano-tx.d.ts +9 -37
- package/srv/blockchain/transaction-presence.d.ts +12 -0
- package/srv/blockchain/transaction-presence.js +29 -0
- package/srv/blockchain/transaction-rows.d.ts +12 -0
- package/srv/blockchain/transaction-rows.js +44 -0
- package/srv/blockchain/wallet-worker/build-request.d.ts +8 -23
- package/srv/blockchain/wallet-worker/build-request.js +40 -353
- package/srv/blockchain/wallet-worker/confirmation-tracker.d.ts +27 -47
- package/srv/blockchain/wallet-worker/confirmation-tracker.js +158 -61
- package/srv/blockchain/wallet-worker/index.d.ts +1 -1
- package/srv/blockchain/wallet-worker/index.js +3 -11
- package/srv/blockchain/wallet-worker/invalidation.d.ts +35 -0
- package/srv/blockchain/wallet-worker/invalidation.js +128 -0
- package/srv/blockchain/wallet-worker/job-store.d.ts +58 -150
- package/srv/blockchain/wallet-worker/job-store.js +200 -273
- package/srv/blockchain/wallet-worker/lease-heartbeat.d.ts +2 -26
- package/srv/blockchain/wallet-worker/lease-heartbeat.js +6 -28
- package/srv/blockchain/wallet-worker/signers.d.ts +5 -24
- package/srv/blockchain/wallet-worker/signers.js +4 -12
- package/srv/blockchain/wallet-worker/wallet-worker.d.ts +29 -82
- package/srv/blockchain/wallet-worker/wallet-worker.js +334 -198
- package/srv/cardano-agent-service.cds +122 -84
- package/srv/cardano-agent-service.js +12 -9
- package/srv/cardano-backend-service.cds +81 -0
- package/srv/cardano-backend-service.d.ts +2 -0
- package/srv/cardano-backend-service.js +259 -0
- package/srv/cardano-indexer-service.cds +393 -18
- package/srv/cardano-indexer-service.d.ts +5 -1
- package/srv/cardano-indexer-service.js +477 -16
- package/srv/cardano-service.cds +426 -269
- package/srv/cardano-service.js +640 -217
- package/srv/cardano-sign-service.cds +211 -180
- package/srv/cardano-sign-service.js +210 -287
- package/srv/cardano-tx-service.cds +462 -370
- package/srv/cardano-tx-service.js +283 -800
- package/srv/cardano-ui.cds +16 -16
- package/srv/cardano-worker-service.cds +141 -125
- package/srv/cardano-worker-service.js +104 -61
- package/srv/cbor/cip68.d.ts +13 -0
- package/srv/cbor/cip68.js +56 -0
- package/srv/cbor/index.d.ts +3 -1
- package/srv/cbor/index.js +9 -1
- package/srv/cbor/parse.d.ts +30 -18
- package/srv/cbor/parse.js +44 -9
- package/srv/cbor/plutus-data-json.d.ts +20 -0
- package/srv/cbor/plutus-data-json.js +32 -0
- package/srv/config.d.ts +117 -0
- package/srv/config.js +419 -0
- package/srv/server.d.ts +15 -110
- package/srv/server.js +83 -405
- package/srv/utils/agent-access.d.ts +11 -0
- package/srv/utils/agent-access.js +86 -0
- package/srv/utils/agent-grants-config.d.ts +0 -46
- package/srv/utils/agent-grants-config.js +17 -85
- package/srv/utils/agent-grants.d.ts +106 -122
- package/srv/utils/agent-grants.js +538 -237
- package/srv/utils/agent-token-auth.d.ts +4 -99
- package/srv/utils/agent-token-auth.js +62 -186
- package/srv/utils/backend-request-handler.d.ts +3 -17
- package/srv/utils/backend-request-handler.js +3 -18
- package/srv/utils/build-request.d.ts +52 -0
- package/srv/utils/build-request.js +487 -0
- package/srv/utils/collections.d.ts +4 -10
- package/srv/utils/collections.js +14 -10
- package/srv/utils/const.d.ts +32 -75
- package/srv/utils/const.js +40 -80
- package/srv/utils/crypto.d.ts +3 -12
- package/srv/utils/crypto.js +11 -25
- package/srv/utils/db-indexes.d.ts +31 -0
- package/srv/utils/db-indexes.js +185 -0
- package/srv/utils/db-kind.d.ts +23 -0
- package/srv/utils/db-kind.js +42 -0
- package/srv/utils/db-sanitize.d.ts +20 -0
- package/srv/utils/db-sanitize.js +80 -0
- package/srv/utils/deep-json.d.ts +6 -0
- package/srv/utils/deep-json.js +240 -0
- package/srv/utils/detached-run.d.ts +41 -0
- package/srv/utils/detached-run.js +101 -0
- package/srv/utils/epoch-slots.d.ts +14 -0
- package/srv/utils/epoch-slots.js +45 -0
- package/srv/utils/error-codes.d.ts +26 -75
- package/srv/utils/error-codes.js +27 -72
- package/srv/utils/errors.d.ts +55 -187
- package/srv/utils/errors.js +161 -235
- package/srv/utils/lease.d.ts +20 -0
- package/srv/utils/lease.js +62 -0
- package/srv/utils/liveness.d.ts +12 -0
- package/srv/utils/liveness.js +35 -0
- package/srv/utils/mappers.d.ts +111 -223
- package/srv/utils/mappers.js +432 -371
- package/srv/utils/navigation.d.ts +17 -0
- package/srv/utils/navigation.js +52 -0
- package/srv/utils/network-context.d.ts +0 -1
- package/srv/utils/network-context.js +9 -15
- package/srv/utils/paging.d.ts +31 -0
- package/srv/utils/paging.js +43 -0
- package/srv/utils/pg-inline-json.d.ts +8 -0
- package/srv/utils/pg-inline-json.js +58 -0
- package/srv/utils/pg-order-nulls.d.ts +14 -0
- package/srv/utils/pg-order-nulls.js +54 -0
- package/srv/utils/plutus-placeholders.d.ts +16 -22
- package/srv/utils/plutus-placeholders.js +62 -33
- package/srv/utils/rate-limiter.d.ts +2 -21
- package/srv/utils/rate-limiter.js +5 -17
- package/srv/utils/readiness.d.ts +12 -0
- package/srv/utils/readiness.js +29 -0
- package/srv/utils/reference-script.d.ts +19 -0
- package/srv/utils/reference-script.js +84 -0
- package/srv/utils/resolution.d.ts +13 -0
- package/srv/utils/resolution.js +47 -0
- package/srv/utils/result-numbers.d.ts +15 -0
- package/srv/utils/result-numbers.js +61 -0
- package/srv/utils/row-values.d.ts +7 -0
- package/srv/utils/row-values.js +27 -0
- package/srv/utils/service-events.d.ts +0 -24
- package/srv/utils/service-events.js +3 -25
- package/srv/utils/signing-helper.d.ts +3 -20
- package/srv/utils/signing-helper.js +35 -64
- package/srv/utils/tx-build-helper.d.ts +25 -105
- package/srv/utils/tx-build-helper.js +108 -156
- package/srv/utils/tx-request-parsers.d.ts +82 -33
- package/srv/utils/tx-request-parsers.js +401 -49
- package/srv/utils/tx-utils.d.ts +3 -27
- package/srv/utils/tx-utils.js +32 -50
- package/srv/utils/types.d.ts +294 -232
- package/srv/utils/types.js +2 -22
- package/srv/utils/validators.d.ts +22 -92
- package/srv/utils/validators.js +60 -170
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export interface CredentialBackfillProgress {
|
|
2
|
+
/** Table being walked; null when finished. */
|
|
3
|
+
table: 'TransactionOutputs' | 'TransactionInputs' | 'LedgerUTxOs' | null;
|
|
4
|
+
outputs: number;
|
|
5
|
+
inputs: number;
|
|
6
|
+
ledgerUtxos: number;
|
|
7
|
+
}
|
|
8
|
+
export interface CredentialBackfillOptions {
|
|
9
|
+
/** Rows per statement. */
|
|
10
|
+
chunkSize?: number;
|
|
11
|
+
onProgress?: (p: CredentialBackfillProgress) => void;
|
|
12
|
+
/** Stops the walk before the next page. */
|
|
13
|
+
shouldStop?: () => boolean;
|
|
14
|
+
}
|
|
15
|
+
export interface CredentialTableSpec {
|
|
16
|
+
name: NonNullable<CredentialBackfillProgress['table']>;
|
|
17
|
+
entity: unknown;
|
|
18
|
+
table: string;
|
|
19
|
+
/** Primary key in walk order. */
|
|
20
|
+
keys: [first: string, second: string];
|
|
21
|
+
/** Postgres type of the first key column. */
|
|
22
|
+
firstKeyType: 'bigint' | 'text';
|
|
23
|
+
column: string;
|
|
24
|
+
address: string;
|
|
25
|
+
counter: 'outputs' | 'inputs' | 'ledgerUtxos';
|
|
26
|
+
}
|
|
27
|
+
export declare const CREDENTIAL_TABLES: readonly CredentialTableSpec[];
|
|
28
|
+
export declare function credentialFillStatement(kind: string, spec: CredentialTableSpec): string | null;
|
|
29
|
+
export declare function backfillUtxoCredentials(opts?: CredentialBackfillOptions): Promise<CredentialBackfillProgress>;
|
|
30
|
+
//# sourceMappingURL=credential-backfill.d.ts.map
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.CREDENTIAL_TABLES = void 0;
|
|
7
|
+
exports.credentialFillStatement = credentialFillStatement;
|
|
8
|
+
exports.backfillUtxoCredentials = backfillUtxoCredentials;
|
|
9
|
+
const cds_1 = __importDefault(require("@sap/cds"));
|
|
10
|
+
const mappers_1 = require("../../utils/mappers");
|
|
11
|
+
const collections_1 = require("../../utils/collections");
|
|
12
|
+
const db_kind_1 = require("../../utils/db-kind");
|
|
13
|
+
const key_pages_1 = require("./key-pages");
|
|
14
|
+
const cardano_1 = require("#cds-models/odatano/cardano");
|
|
15
|
+
const { UPDATE } = cds_1.default.ql;
|
|
16
|
+
const logger = cds_1.default.log('CredentialBackfill');
|
|
17
|
+
exports.CREDENTIAL_TABLES = [
|
|
18
|
+
{ name: 'TransactionOutputs', entity: cardano_1.TransactionOutputs, table: 'odatano_cardano_TransactionOutputs', keys: ['txSeq', 'outputIndex'], firstKeyType: 'bigint', column: 'utxo_paymentCredential', address: 'address_address', counter: 'outputs' },
|
|
19
|
+
{ name: 'TransactionInputs', entity: cardano_1.TransactionInputs, table: 'odatano_cardano_TransactionInputs', keys: ['txSeq', 'inputIndex'], firstKeyType: 'bigint', column: 'utxoData_paymentCredential', address: 'address_address', counter: 'inputs' },
|
|
20
|
+
{ name: 'LedgerUTxOs', entity: cardano_1.LedgerUTxOs, table: 'odatano_cardano_LedgerUTxOs', keys: ['txHash', 'outputIndex'], firstKeyType: 'text', column: 'utxo_paymentCredential', address: 'address', counter: 'ledgerUtxos' },
|
|
21
|
+
];
|
|
22
|
+
// One UPDATE per chunk that joins a JSON array of fills on the key. Touches only rows without a
|
|
23
|
+
// credential. Null for databases other than Postgres and SQLite, which update row by row.
|
|
24
|
+
function credentialFillStatement(kind, spec) {
|
|
25
|
+
const bulk = (0, db_kind_1.bulkSqlKind)(kind);
|
|
26
|
+
if (!bulk)
|
|
27
|
+
return null;
|
|
28
|
+
const [k1, k2] = spec.keys;
|
|
29
|
+
return (0, db_kind_1.jsonFillUpdate)(bulk, {
|
|
30
|
+
table: spec.table,
|
|
31
|
+
keys: [[k1, spec.firstKeyType], [k2, 'integer']],
|
|
32
|
+
values: [['credential', 'text']],
|
|
33
|
+
set: `${spec.column} = v.credential`,
|
|
34
|
+
where: `t.${spec.column} IS NULL`,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
async function writeFills(tx, spec, fills, chunkSize) {
|
|
38
|
+
if (!fills.length)
|
|
39
|
+
return;
|
|
40
|
+
const sql = credentialFillStatement((0, db_kind_1.connectedDbKind)(), spec);
|
|
41
|
+
const [k1, k2] = spec.keys;
|
|
42
|
+
if (!sql) {
|
|
43
|
+
for (const f of fills) {
|
|
44
|
+
await tx.run(UPDATE.entity(spec.entity).set({ [spec.column]: f.credential }).where({ [k1]: f.first, [k2]: f.second, [spec.column]: null }));
|
|
45
|
+
}
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
for (const part of (0, collections_1.chunk)(fills, chunkSize)) {
|
|
49
|
+
const records = part.map((f) => ({ [k1.toLowerCase()]: f.first, [k2.toLowerCase()]: f.second, credential: f.credential }));
|
|
50
|
+
await tx.run(sql, [JSON.stringify(records)]);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const keyValue = (v, type) => type === 'bigint' ? Number(v) : String(v);
|
|
54
|
+
/** Walks one table from its lowest key; returns the rows that received a credential, or null when stopped. */
|
|
55
|
+
async function fillTable(spec, chunkSize, onChunk, shouldStop) {
|
|
56
|
+
let filled = 0;
|
|
57
|
+
const decoded = new Map();
|
|
58
|
+
const [k1, k2] = spec.keys;
|
|
59
|
+
const finished = await (0, key_pages_1.pageByKey)({
|
|
60
|
+
entity: spec.entity, keys: spec.keys, columns: [spec.address, spec.column], chunkSize, shouldStop,
|
|
61
|
+
}, async (rows) => {
|
|
62
|
+
const fills = [];
|
|
63
|
+
for (const r of rows) {
|
|
64
|
+
if (r[spec.column] != null)
|
|
65
|
+
continue;
|
|
66
|
+
const address = r[spec.address];
|
|
67
|
+
if (!address)
|
|
68
|
+
continue;
|
|
69
|
+
if (!decoded.has(address))
|
|
70
|
+
decoded.set(address, (0, mappers_1.paymentCredentialOf)(address));
|
|
71
|
+
const credential = decoded.get(address);
|
|
72
|
+
if (credential)
|
|
73
|
+
fills.push({ first: keyValue(r[k1], spec.firstKeyType), second: Number(r[k2]), credential });
|
|
74
|
+
}
|
|
75
|
+
await cds_1.default.tx((tx) => writeFills(tx, spec, fills, chunkSize));
|
|
76
|
+
filled += fills.length;
|
|
77
|
+
onChunk(filled);
|
|
78
|
+
if (decoded.size > 50_000)
|
|
79
|
+
decoded.clear();
|
|
80
|
+
});
|
|
81
|
+
return finished ? filled : null;
|
|
82
|
+
}
|
|
83
|
+
async function backfillUtxoCredentials(opts = {}) {
|
|
84
|
+
const chunkSize = opts.chunkSize ?? 5000;
|
|
85
|
+
const progress = { table: null, outputs: 0, inputs: 0, ledgerUtxos: 0 };
|
|
86
|
+
for (const spec of exports.CREDENTIAL_TABLES) {
|
|
87
|
+
progress.table = spec.name;
|
|
88
|
+
opts.onProgress?.({ ...progress });
|
|
89
|
+
const filled = await fillTable(spec, chunkSize, (n) => {
|
|
90
|
+
progress[spec.counter] = n;
|
|
91
|
+
opts.onProgress?.({ ...progress });
|
|
92
|
+
}, opts.shouldStop);
|
|
93
|
+
if (filled === null)
|
|
94
|
+
return progress;
|
|
95
|
+
progress[spec.counter] = filled;
|
|
96
|
+
}
|
|
97
|
+
progress.table = null;
|
|
98
|
+
logger.info(`payment credentials filled: ${progress.outputs} outputs, ${progress.inputs} inputs, ${progress.ledgerUtxos} ledger UTxOs`);
|
|
99
|
+
return progress;
|
|
100
|
+
}
|
|
101
|
+
//# sourceMappingURL=credential-backfill.js.map
|
|
@@ -1,15 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Crawler event hooks (v2.1).
|
|
3
|
-
*
|
|
4
|
-
* Small listener registry that lets other subsystems (currently the wallet-worker
|
|
5
|
-
* confirmation tracker) observe crawler progress without the crawler importing
|
|
6
|
-
* them (dependency direction stays crawler ← consumer). The crawler emits:
|
|
7
|
-
* - `blockIndexed` after each successfully persisted block (with its tx hashes), and
|
|
8
|
-
* - `reorg` after a completed rollback (with the fork slot).
|
|
9
|
-
*
|
|
10
|
-
* Listener failures are logged and swallowed — a broken consumer must never
|
|
11
|
-
* stop the crawler.
|
|
12
|
-
*/
|
|
13
1
|
export interface BlockIndexedEvent {
|
|
14
2
|
hash: string;
|
|
15
3
|
slot: number | null;
|
|
@@ -20,14 +8,11 @@ export interface BlockIndexedEvent {
|
|
|
20
8
|
tipHeight: number | null;
|
|
21
9
|
}
|
|
22
10
|
export interface ReorgEvent {
|
|
23
|
-
/** Absolute slot of the fork point
|
|
11
|
+
/** Absolute slot of the fork point. Everything after it was rolled back. */
|
|
24
12
|
forkSlot: number;
|
|
25
13
|
/** How many blocks the rollback removed (0 when the fork was already the tip). */
|
|
26
14
|
blocksRolledBack?: number;
|
|
27
|
-
/**
|
|
28
|
-
* Block height of the fork point, when known. Listeners tracking confirmation
|
|
29
|
-
* depth must clamp their tip to this — the pre-fork tip height no longer exists.
|
|
30
|
-
*/
|
|
15
|
+
/** Block height of the fork point, when known; depth trackers must clamp their tip to it. */
|
|
31
16
|
forkHeight: number | null;
|
|
32
17
|
}
|
|
33
18
|
export type BlockIndexedListener = (event: BlockIndexedEvent) => void;
|
|
@@ -1,21 +1,14 @@
|
|
|
1
1
|
import type { CardanoClient } from '../cardano-client';
|
|
2
2
|
import type { CardanoIndexer } from '../cardano-indexer';
|
|
3
3
|
import { CardanoCrawler, type CrawlerConfig } from './crawler';
|
|
4
|
+
export declare function standbyDelayMs(consecutiveErrors: number): number;
|
|
4
5
|
export interface StartCrawlerDeps {
|
|
5
6
|
client: CardanoClient;
|
|
6
7
|
indexer: CardanoIndexer;
|
|
7
8
|
network: string;
|
|
8
9
|
config: CrawlerConfig;
|
|
9
10
|
}
|
|
10
|
-
/**
|
|
11
|
-
* Start the crawler (idempotent — a no-op if one is already running). Fire-and-forget:
|
|
12
|
-
* returns once the ingest pipeline is launched, not when catch-up completes.
|
|
13
|
-
*/
|
|
14
11
|
export declare function startCrawler(deps: StartCrawlerDeps, resumeCluster?: boolean): Promise<void>;
|
|
15
|
-
/**
|
|
16
|
-
* Stop the local crawler. `pauseCluster=true` additionally fences every instance via
|
|
17
|
-
* shared DB intent; ordinary app shutdown leaves the desired state intact for failover.
|
|
18
|
-
*/
|
|
19
12
|
export declare function stopCrawler(pauseCluster?: boolean): Promise<void>;
|
|
20
13
|
/** Whether a crawler is currently running. */
|
|
21
14
|
export declare function isCrawlerRunning(): boolean;
|
|
@@ -3,6 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.standbyDelayMs = standbyDelayMs;
|
|
6
7
|
exports.startCrawler = startCrawler;
|
|
7
8
|
exports.stopCrawler = stopCrawler;
|
|
8
9
|
exports.isCrawlerRunning = isCrawlerRunning;
|
|
@@ -13,21 +14,22 @@ const node_crypto_1 = require("node:crypto");
|
|
|
13
14
|
const crawler_1 = require("./crawler");
|
|
14
15
|
const sync_state_1 = require("./sync-state");
|
|
15
16
|
const logger = cds_1.default.log('CardanoCrawler');
|
|
16
|
-
|
|
17
|
-
* Module-level singleton lifecycle for the chain crawler. Mirrors NIGHTGATE's
|
|
18
|
-
* srv/crawler/index.ts: one active crawler per process, started fire-and-forget from
|
|
19
|
-
* the server's `served` hook and controlled (pause/resume/status) via the indexer service.
|
|
20
|
-
*/
|
|
17
|
+
// Process-wide crawler, started from the `served` hook and controlled via the indexer service.
|
|
21
18
|
let active = null;
|
|
22
19
|
let lifecycleQueue = Promise.resolve();
|
|
23
20
|
let standbyDeps = null;
|
|
24
21
|
let standbyTimer = null;
|
|
25
22
|
let standbyGeneration = 0;
|
|
26
23
|
const STANDBY_RETRY_MS = 5_000;
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
24
|
+
const STANDBY_MAX_BACKOFF_MS = 5 * 60_000;
|
|
25
|
+
// Delay before the next standby start: 5 s when healthy. After an error halt it grows
|
|
26
|
+
// exponentially up to 5 min with the shared cursor's error streak.
|
|
27
|
+
function standbyDelayMs(consecutiveErrors) {
|
|
28
|
+
if (!Number.isFinite(consecutiveErrors) || consecutiveErrors <= 1)
|
|
29
|
+
return STANDBY_RETRY_MS;
|
|
30
|
+
return Math.min(STANDBY_MAX_BACKOFF_MS, STANDBY_RETRY_MS * 2 ** Math.min(consecutiveErrors - 1, 10));
|
|
31
|
+
}
|
|
32
|
+
// Start the crawler, a no-op if one runs. Returns once the pipeline is launched, not when caught up.
|
|
31
33
|
async function startCrawler(deps, resumeCluster = false) {
|
|
32
34
|
const generation = ++standbyGeneration;
|
|
33
35
|
standbyDeps = deps;
|
|
@@ -70,10 +72,7 @@ async function startCrawlerAttempt(deps, resumeCluster, generation) {
|
|
|
70
72
|
active = null;
|
|
71
73
|
});
|
|
72
74
|
}
|
|
73
|
-
|
|
74
|
-
* Stop the local crawler. `pauseCluster=true` additionally fences every instance via
|
|
75
|
-
* shared DB intent; ordinary app shutdown leaves the desired state intact for failover.
|
|
76
|
-
*/
|
|
75
|
+
// Stop the local crawler. `pauseCluster` stops every instance. A normal shutdown keeps failover possible.
|
|
77
76
|
async function stopCrawler(pauseCluster = false) {
|
|
78
77
|
++standbyGeneration; // invalidate callbacks that already left the timer queue
|
|
79
78
|
standbyDeps = null;
|
|
@@ -113,7 +112,7 @@ function serializeLifecycle(operation) {
|
|
|
113
112
|
return result;
|
|
114
113
|
}
|
|
115
114
|
/** Keep non-leader instances warm so an expired/released lease fails over automatically. */
|
|
116
|
-
function scheduleStandby(generation) {
|
|
115
|
+
function scheduleStandby(generation, delayMs = STANDBY_RETRY_MS) {
|
|
117
116
|
if (standbyTimer || !standbyDeps)
|
|
118
117
|
return;
|
|
119
118
|
standbyTimer = setTimeout(() => {
|
|
@@ -123,13 +122,28 @@ function scheduleStandby(generation) {
|
|
|
123
122
|
const deps = standbyDeps;
|
|
124
123
|
if (!deps)
|
|
125
124
|
return;
|
|
125
|
+
// Local evidence only, taken BEFORE the attempt replaces `active`.
|
|
126
|
+
const failedHere = active?.haltedWithError() ?? false;
|
|
126
127
|
void startCrawlerAttempt(deps, false, generation)
|
|
127
128
|
.catch((err) => logger.error('standby lease attempt failed:', err))
|
|
128
|
-
.
|
|
129
|
+
.then(() => nextStandbyDelay(failedHere))
|
|
130
|
+
.then((delay) => {
|
|
129
131
|
if (generation === standbyGeneration)
|
|
130
|
-
scheduleStandby(generation);
|
|
132
|
+
scheduleStandby(generation, delay);
|
|
131
133
|
});
|
|
132
|
-
},
|
|
134
|
+
}, delayMs);
|
|
133
135
|
standbyTimer.unref?.();
|
|
134
136
|
}
|
|
137
|
+
/** Backoff from the error streak after a local failure. A healthy tick costs no DB read. */
|
|
138
|
+
async function nextStandbyDelay(failedHere) {
|
|
139
|
+
if (!failedHere)
|
|
140
|
+
return STANDBY_RETRY_MS;
|
|
141
|
+
try {
|
|
142
|
+
const cursor = await cds_1.default.tx((tx) => (0, sync_state_1.readCursor)(tx));
|
|
143
|
+
return standbyDelayMs(cursor?.consecutiveErrors ?? 0);
|
|
144
|
+
}
|
|
145
|
+
catch {
|
|
146
|
+
return STANDBY_RETRY_MS;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
135
149
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/** Columns of an asset row besides its key, the same in the copies and in the output assets. */
|
|
2
|
+
export declare const ASSET_COLUMNS: string[];
|
|
3
|
+
export interface InputCopyPruneProgress {
|
|
4
|
+
atSlot: number;
|
|
5
|
+
toSlot: number;
|
|
6
|
+
referenced: number;
|
|
7
|
+
kept: number;
|
|
8
|
+
deleted: number;
|
|
9
|
+
}
|
|
10
|
+
export interface InputCopyRestoreProgress {
|
|
11
|
+
atSlot: number;
|
|
12
|
+
toSlot: number;
|
|
13
|
+
restored: number;
|
|
14
|
+
rows: number;
|
|
15
|
+
}
|
|
16
|
+
export interface CopyWalkOptions<P> {
|
|
17
|
+
fromSlot: number;
|
|
18
|
+
toSlot: number;
|
|
19
|
+
slotWindow?: number;
|
|
20
|
+
onProgress?: (p: P) => void;
|
|
21
|
+
shouldStop?: () => boolean;
|
|
22
|
+
}
|
|
23
|
+
export declare function referenceStatement(kind: string): string | null;
|
|
24
|
+
export declare function deleteCopiesStatement(kind: string): string | null;
|
|
25
|
+
/** Counts the inputs of a window that have no reference. */
|
|
26
|
+
export declare function keptStatement(kind: string): string | null;
|
|
27
|
+
/** Writes the copies of the inputs of a window that have a reference, from the outputs they spent. */
|
|
28
|
+
export declare function restoreCopiesStatement(kind: string): string | null;
|
|
29
|
+
/** Takes the reference from the inputs of a window, once their copies are written. */
|
|
30
|
+
export declare function dropReferenceStatement(kind: string): string | null;
|
|
31
|
+
export declare function bothStatement(kind: string): string | null;
|
|
32
|
+
export declare function pruneInputAssetCopies(opts: CopyWalkOptions<InputCopyPruneProgress>): Promise<InputCopyPruneProgress>;
|
|
33
|
+
export declare function restoreInputAssetCopies(opts: CopyWalkOptions<InputCopyRestoreProgress>): Promise<InputCopyRestoreProgress>;
|
|
34
|
+
//# sourceMappingURL=input-asset-copies.d.ts.map
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ASSET_COLUMNS = void 0;
|
|
7
|
+
exports.referenceStatement = referenceStatement;
|
|
8
|
+
exports.deleteCopiesStatement = deleteCopiesStatement;
|
|
9
|
+
exports.keptStatement = keptStatement;
|
|
10
|
+
exports.restoreCopiesStatement = restoreCopiesStatement;
|
|
11
|
+
exports.dropReferenceStatement = dropReferenceStatement;
|
|
12
|
+
exports.bothStatement = bothStatement;
|
|
13
|
+
exports.pruneInputAssetCopies = pruneInputAssetCopies;
|
|
14
|
+
exports.restoreInputAssetCopies = restoreInputAssetCopies;
|
|
15
|
+
const cds_1 = __importDefault(require("@sap/cds"));
|
|
16
|
+
const cardano_1 = require("#cds-models/odatano/cardano");
|
|
17
|
+
const window_plan_1 = require("./window-plan");
|
|
18
|
+
const db_kind_1 = require("../../utils/db-kind");
|
|
19
|
+
const logger = cds_1.default.log('InputAssetCopies');
|
|
20
|
+
// An input reads its assets from a stored copy or, through `spentTxSeq`, from the output it spent.
|
|
21
|
+
// It has one of the two, never both. The prune moves the inputs of a slot range from copies to the
|
|
22
|
+
// reference, the restore moves them back. Both walk by the slot of the spending transaction.
|
|
23
|
+
const INPUTS = 'odatano_cardano_TransactionInputs';
|
|
24
|
+
const COPIES = 'odatano_cardano_TransactionInputAssetCopies';
|
|
25
|
+
const TRANSACTIONS = 'odatano_cardano_Transactions';
|
|
26
|
+
const OUTPUTS = 'odatano_cardano_TransactionOutputs';
|
|
27
|
+
const OUTPUT_ASSETS = 'odatano_cardano_TransactionOutputAssets';
|
|
28
|
+
/** Columns of an asset row besides its key, the same in the copies and in the output assets. */
|
|
29
|
+
exports.ASSET_COLUMNS = ['asset_quantity', 'asset_policyId', 'asset_assetNameHex', 'asset_assetName', 'asset_fingerprint'];
|
|
30
|
+
const supported = (kind) => (0, db_kind_1.bulkSqlKind)(kind) !== null;
|
|
31
|
+
/** The two window bounds as numbered parameters, so a statement can name each more than once. */
|
|
32
|
+
const bounds = (kind) => (kind === 'postgres' ? ['$1', '$2'] : ['?1', '?2']);
|
|
33
|
+
// Points the inputs of a window at their output. Only where the output is stored and holds
|
|
34
|
+
// exactly the asset rows of the input's copy, so the input reads the same assets afterwards.
|
|
35
|
+
function referenceStatement(kind) {
|
|
36
|
+
if (!supported(kind))
|
|
37
|
+
return null;
|
|
38
|
+
const [lo, hi] = bounds(kind);
|
|
39
|
+
const copy = `c.input_txSeq = i.txSeq AND c.input_inputIndex = i.inputIndex`;
|
|
40
|
+
const spent = `a.output_txSeq = t.txSeq AND a.output_outputIndex = i.spentOutputIndex`;
|
|
41
|
+
return `UPDATE ${INPUTS} AS i SET spentTxSeq = t.txSeq FROM ${TRANSACTIONS} AS t ` +
|
|
42
|
+
`WHERE t.hash = i.spentTxHash AND i.spentTxSeq IS NULL AND i.txSeq >= ${lo} AND i.txSeq < ${hi} ` +
|
|
43
|
+
`AND EXISTS (SELECT 1 FROM ${OUTPUTS} AS o WHERE o.txSeq = t.txSeq AND o.outputIndex = i.spentOutputIndex) ` +
|
|
44
|
+
`AND NOT EXISTS (SELECT 1 FROM ${COPIES} AS c WHERE ${copy} AND NOT EXISTS (` +
|
|
45
|
+
`SELECT 1 FROM ${OUTPUT_ASSETS} AS a WHERE ${spent} AND a.unit = c.unit AND a.asset_quantity = c.asset_quantity)) ` +
|
|
46
|
+
`AND (SELECT count(*) FROM ${COPIES} AS c WHERE ${copy}) = (SELECT count(*) FROM ${OUTPUT_ASSETS} AS a WHERE ${spent})`;
|
|
47
|
+
}
|
|
48
|
+
// Deletes the copies of the inputs of a window that have a reference. The inputs carry the window
|
|
49
|
+
// bounds too: without them the database may read every input to find those with a reference.
|
|
50
|
+
function deleteCopiesStatement(kind) {
|
|
51
|
+
if (!supported(kind))
|
|
52
|
+
return null;
|
|
53
|
+
const [lo, hi] = bounds(kind);
|
|
54
|
+
return `DELETE FROM ${COPIES} WHERE input_txSeq >= ${lo} AND input_txSeq < ${hi} AND EXISTS (` +
|
|
55
|
+
`SELECT 1 FROM ${INPUTS} AS i WHERE i.txSeq = ${COPIES}.input_txSeq AND i.inputIndex = ${COPIES}.input_inputIndex ` +
|
|
56
|
+
`AND i.spentTxSeq IS NOT NULL AND i.txSeq >= ${lo} AND i.txSeq < ${hi})`;
|
|
57
|
+
}
|
|
58
|
+
/** Counts the inputs of a window that have no reference. */
|
|
59
|
+
function keptStatement(kind) {
|
|
60
|
+
if (!supported(kind))
|
|
61
|
+
return null;
|
|
62
|
+
const [lo, hi] = bounds(kind);
|
|
63
|
+
return `SELECT count(*) AS n FROM ${INPUTS} WHERE txSeq >= ${lo} AND txSeq < ${hi} AND spentTxSeq IS NULL`;
|
|
64
|
+
}
|
|
65
|
+
/** Writes the copies of the inputs of a window that have a reference, from the outputs they spent. */
|
|
66
|
+
function restoreCopiesStatement(kind) {
|
|
67
|
+
if (!supported(kind))
|
|
68
|
+
return null;
|
|
69
|
+
const [lo, hi] = bounds(kind);
|
|
70
|
+
const columns = exports.ASSET_COLUMNS.join(', ');
|
|
71
|
+
return `INSERT INTO ${COPIES} (input_txSeq, input_inputIndex, unit, ${columns}) ` +
|
|
72
|
+
`SELECT i.txSeq, i.inputIndex, a.unit, ${exports.ASSET_COLUMNS.map(c => `a.${c}`).join(', ')} FROM ${INPUTS} AS i ` +
|
|
73
|
+
`JOIN ${OUTPUT_ASSETS} AS a ON a.output_txSeq = i.spentTxSeq AND a.output_outputIndex = i.spentOutputIndex ` +
|
|
74
|
+
`WHERE i.txSeq >= ${lo} AND i.txSeq < ${hi}`;
|
|
75
|
+
}
|
|
76
|
+
/** Takes the reference from the inputs of a window, once their copies are written. */
|
|
77
|
+
function dropReferenceStatement(kind) {
|
|
78
|
+
if (!supported(kind))
|
|
79
|
+
return null;
|
|
80
|
+
const [lo, hi] = bounds(kind);
|
|
81
|
+
return `UPDATE ${INPUTS} SET spentTxSeq = NULL WHERE txSeq >= ${lo} AND txSeq < ${hi} AND spentTxSeq IS NOT NULL`;
|
|
82
|
+
}
|
|
83
|
+
// Counts the inputs of a window that have copies and a reference. The view would show their
|
|
84
|
+
// assets twice, so a window that leaves one behind is rolled back.
|
|
85
|
+
function bothStatement(kind) {
|
|
86
|
+
if (!supported(kind))
|
|
87
|
+
return null;
|
|
88
|
+
const [lo, hi] = bounds(kind);
|
|
89
|
+
return `SELECT count(*) AS n FROM ${INPUTS} AS i WHERE i.txSeq >= ${lo} AND i.txSeq < ${hi} AND i.spentTxSeq IS NOT NULL ` +
|
|
90
|
+
`AND EXISTS (SELECT 1 FROM ${COPIES} AS c WHERE c.input_txSeq = i.txSeq AND c.input_inputIndex = i.inputIndex)`;
|
|
91
|
+
}
|
|
92
|
+
async function assertOneSource(tx, check, window) {
|
|
93
|
+
const [found] = await tx.run(check, window);
|
|
94
|
+
const both = Number(found?.n ?? 0);
|
|
95
|
+
if (both > 0)
|
|
96
|
+
throw new Error(`${both} inputs between txSeq ${window[0]} and ${window[1]} would have copies and a reference; the window is left unchanged`);
|
|
97
|
+
}
|
|
98
|
+
/** Rows a plain statement changed; the drivers answer with their own result object. */
|
|
99
|
+
function changed(result) {
|
|
100
|
+
const r = result;
|
|
101
|
+
return typeof r === 'number' ? r : Number(r?.changes ?? r?.rowCount) || 0;
|
|
102
|
+
}
|
|
103
|
+
function statementFor(build) {
|
|
104
|
+
const sql = build((0, db_kind_1.connectedDbKind)());
|
|
105
|
+
if (!sql)
|
|
106
|
+
throw new Error(`Input asset copies are pruned and restored on PostgreSQL and SQLite only (database kind: ${(0, db_kind_1.connectedDbKind)() || 'none'})`);
|
|
107
|
+
return sql;
|
|
108
|
+
}
|
|
109
|
+
/** Walks the windows of the asked slots that hold stored inputs. Null when there is none. */
|
|
110
|
+
async function walk(opts, progress, step) {
|
|
111
|
+
const range = await (0, window_plan_1.slotRangeOf)(cardano_1.TransactionInputs, opts);
|
|
112
|
+
if (!range)
|
|
113
|
+
return null;
|
|
114
|
+
progress.toSlot = range.to;
|
|
115
|
+
await (0, window_plan_1.walkSlotWindows)(range, {
|
|
116
|
+
slotWindow: opts.slotWindow,
|
|
117
|
+
shouldStop: opts.shouldStop,
|
|
118
|
+
afterWindow: (atSlot) => { progress.atSlot = atSlot; opts.onProgress?.({ ...progress }); },
|
|
119
|
+
}, step);
|
|
120
|
+
return range;
|
|
121
|
+
}
|
|
122
|
+
// Moves the inputs of the slot range from copies to the reference. Per window, in one transaction:
|
|
123
|
+
// set the reference where the output's assets equal the copy, then delete the copies of those inputs.
|
|
124
|
+
async function pruneInputAssetCopies(opts) {
|
|
125
|
+
const reference = statementFor(referenceStatement);
|
|
126
|
+
const remove = statementFor(deleteCopiesStatement);
|
|
127
|
+
const count = statementFor(keptStatement);
|
|
128
|
+
const check = statementFor(bothStatement);
|
|
129
|
+
const progress = { atSlot: 0, toSlot: 0, referenced: 0, kept: 0, deleted: 0 };
|
|
130
|
+
const range = await walk(opts, progress, async (tx, window) => {
|
|
131
|
+
const referenced = changed(await tx.run(reference, window));
|
|
132
|
+
const deleted = changed(await tx.run(remove, window));
|
|
133
|
+
await assertOneSource(tx, check, window);
|
|
134
|
+
const [left] = await tx.run(count, window);
|
|
135
|
+
progress.referenced += referenced;
|
|
136
|
+
progress.deleted += deleted;
|
|
137
|
+
progress.kept += Number(left?.n ?? 0);
|
|
138
|
+
});
|
|
139
|
+
if (range)
|
|
140
|
+
logger.info(`input asset copies pruned: ${progress.referenced} inputs read from their output, ${progress.kept} left without a reference,${progress.deleted} rows deleted, slots ${range.from} to ${range.to}`);
|
|
141
|
+
return progress;
|
|
142
|
+
}
|
|
143
|
+
// Moves the inputs of the slot range back to copies. Per window, in one transaction: write the
|
|
144
|
+
// copies from the outputs, then take the reference from those inputs.
|
|
145
|
+
async function restoreInputAssetCopies(opts) {
|
|
146
|
+
const write = statementFor(restoreCopiesStatement);
|
|
147
|
+
const release = statementFor(dropReferenceStatement);
|
|
148
|
+
const check = statementFor(bothStatement);
|
|
149
|
+
const progress = { atSlot: 0, toSlot: 0, restored: 0, rows: 0 };
|
|
150
|
+
const range = await walk(opts, progress, async (tx, window) => {
|
|
151
|
+
const rows = changed(await tx.run(write, window));
|
|
152
|
+
const restored = changed(await tx.run(release, window));
|
|
153
|
+
await assertOneSource(tx, check, window);
|
|
154
|
+
progress.rows += rows;
|
|
155
|
+
progress.restored += restored;
|
|
156
|
+
});
|
|
157
|
+
if (range)
|
|
158
|
+
logger.info(`input asset copies restored: ${progress.restored} inputs, ${progress.rows} rows, slots ${range.from} to ${range.to}`);
|
|
159
|
+
return progress;
|
|
160
|
+
}
|
|
161
|
+
//# sourceMappingURL=input-asset-copies.js.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export interface KeyPageSpec {
|
|
2
|
+
entity: unknown;
|
|
3
|
+
/** Primary key in walk order. */
|
|
4
|
+
keys: [first: string, second: string];
|
|
5
|
+
/** Columns to read; the keys are added. */
|
|
6
|
+
columns: string[];
|
|
7
|
+
chunkSize: number;
|
|
8
|
+
/** Stops the walk before the next page. */
|
|
9
|
+
shouldStop?: () => boolean;
|
|
10
|
+
}
|
|
11
|
+
export declare function pageByKey<R extends Record<string, unknown>>(spec: KeyPageSpec, onPage: (rows: R[]) => Promise<void>): Promise<boolean>;
|
|
12
|
+
//# sourceMappingURL=key-pages.d.ts.map
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.pageByKey = pageByKey;
|
|
7
|
+
const cds_1 = __importDefault(require("@sap/cds"));
|
|
8
|
+
const window_plan_1 = require("./window-plan");
|
|
9
|
+
const { SELECT } = cds_1.default.ql;
|
|
10
|
+
// Calls `onPage` with each page, outside the read transaction, so it may call a backend.
|
|
11
|
+
// Returns false when `shouldStop` ended the walk.
|
|
12
|
+
async function pageByKey(spec, onPage) {
|
|
13
|
+
const [k1, k2] = spec.keys;
|
|
14
|
+
const columns = [...new Set([k1, k2, ...spec.columns])];
|
|
15
|
+
let at = null;
|
|
16
|
+
for (;;) {
|
|
17
|
+
await (0, window_plan_1.yieldToEventLoop)();
|
|
18
|
+
if (spec.shouldStop?.())
|
|
19
|
+
return false;
|
|
20
|
+
const where = {};
|
|
21
|
+
if (at?.within) {
|
|
22
|
+
where[k1] = at.first;
|
|
23
|
+
where[k2] = { '>': at.second };
|
|
24
|
+
}
|
|
25
|
+
else if (at) {
|
|
26
|
+
where[k1] = { '>': at.first };
|
|
27
|
+
}
|
|
28
|
+
const rows = (await cds_1.default.tx((tx) => tx.run(SELECT.from(spec.entity).columns(...columns).where(where).orderBy(k1, k2).limit(spec.chunkSize)))) ?? [];
|
|
29
|
+
const wasWithin = at?.within ?? false;
|
|
30
|
+
if (!rows.length) {
|
|
31
|
+
if (!wasWithin)
|
|
32
|
+
return true;
|
|
33
|
+
at = { ...at, within: false };
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
const full = rows.length === spec.chunkSize;
|
|
37
|
+
const lastFirst = rows[rows.length - 1][k1];
|
|
38
|
+
const allSame = rows.every((r) => r[k1] === lastFirst);
|
|
39
|
+
// the last first key of a full page may go on, so its rows wait for the next page
|
|
40
|
+
const page = full && !allSame ? rows.filter((r) => r[k1] !== lastFirst) : rows;
|
|
41
|
+
await onPage(page);
|
|
42
|
+
const last = page[page.length - 1];
|
|
43
|
+
at = { first: last[k1], second: last[k2], within: full && allSame };
|
|
44
|
+
if (!full && !wasWithin)
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=key-pages.js.map
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export interface MetadataPositionProgress {
|
|
2
|
+
/** Slot the walk has reached. */
|
|
3
|
+
atSlot: number;
|
|
4
|
+
/** Last slot that holds a stored transaction. */
|
|
5
|
+
toSlot: number;
|
|
6
|
+
/** Metadata rows that received their position. */
|
|
7
|
+
rows: number;
|
|
8
|
+
}
|
|
9
|
+
export interface MetadataPositionOptions {
|
|
10
|
+
/** First slot to walk; default the first stored transaction. */
|
|
11
|
+
fromSlot?: number | null;
|
|
12
|
+
/** Last slot to walk; default the last stored transaction. */
|
|
13
|
+
toSlot?: number | null;
|
|
14
|
+
/** Slots per statement. */
|
|
15
|
+
slotWindow?: number;
|
|
16
|
+
onProgress?: (p: MetadataPositionProgress) => void;
|
|
17
|
+
/** Stops the walk before the next window. */
|
|
18
|
+
shouldStop?: () => boolean;
|
|
19
|
+
}
|
|
20
|
+
export declare function metadataPositionStatement(kind: string): string | null;
|
|
21
|
+
export declare function backfillMetadataPositions(opts?: MetadataPositionOptions): Promise<MetadataPositionProgress>;
|
|
22
|
+
//# sourceMappingURL=metadata-position-backfill.d.ts.map
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.metadataPositionStatement = metadataPositionStatement;
|
|
7
|
+
exports.backfillMetadataPositions = backfillMetadataPositions;
|
|
8
|
+
const cds_1 = __importDefault(require("@sap/cds"));
|
|
9
|
+
const cardano_1 = require("#cds-models/odatano/cardano");
|
|
10
|
+
const window_plan_1 = require("./window-plan");
|
|
11
|
+
const db_kind_1 = require("../../utils/db-kind");
|
|
12
|
+
const { SELECT, UPDATE } = cds_1.default.ql;
|
|
13
|
+
const logger = cds_1.default.log('MetadataPositionBackfill');
|
|
14
|
+
const METADATA_TABLE = 'odatano_cardano_TransactionMetadata';
|
|
15
|
+
const TRANSACTIONS_TABLE = 'odatano_cardano_Transactions';
|
|
16
|
+
// One UPDATE per window that takes the position from the transaction row. Null for databases
|
|
17
|
+
// other than Postgres and SQLite, which update transaction by transaction. The only way into the
|
|
18
|
+
// metadata table is the join on tx_hash: a filter on txSeq would let the planner scan for it.
|
|
19
|
+
function metadataPositionStatement(kind) {
|
|
20
|
+
const bulk = (0, db_kind_1.bulkSqlKind)(kind);
|
|
21
|
+
if (!bulk)
|
|
22
|
+
return null;
|
|
23
|
+
const [lo, hi] = bulk === 'postgres' ? ['$1', '$2'] : ['?', '?'];
|
|
24
|
+
return `UPDATE ${METADATA_TABLE} AS m SET txSeq = t.txSeq FROM ${TRANSACTIONS_TABLE} AS t ` +
|
|
25
|
+
`WHERE t.hash = m.tx_hash AND t.txSeq >= ${lo} AND t.txSeq < ${hi} AND m.txSeq IS DISTINCT FROM t.txSeq`;
|
|
26
|
+
}
|
|
27
|
+
async function fillWindow(tx, lo, hi) {
|
|
28
|
+
const sql = metadataPositionStatement((0, db_kind_1.connectedDbKind)());
|
|
29
|
+
if (sql) {
|
|
30
|
+
// a plain statement answers with the driver's result object
|
|
31
|
+
const result = await tx.run(sql, [lo, hi]);
|
|
32
|
+
return Number(result?.changes) || 0;
|
|
33
|
+
}
|
|
34
|
+
const txs = (await tx.run(SELECT.from(cardano_1.Transactions).columns('hash', 'txSeq').where({ txSeq: { '>=': lo, '<': hi } }))) ?? [];
|
|
35
|
+
let rows = 0;
|
|
36
|
+
for (const t of txs) {
|
|
37
|
+
// the same rule as the bulk statement: a row without its transaction's position, or with another one
|
|
38
|
+
const seq = Number(t.txSeq);
|
|
39
|
+
rows += Number(await tx.run(UPDATE.entity(cardano_1.TransactionMetadata).set({ txSeq: seq }).where({ tx_hash: t.hash, txSeq: null }))) || 0;
|
|
40
|
+
rows += Number(await tx.run(UPDATE.entity(cardano_1.TransactionMetadata).set({ txSeq: seq }).where({ tx_hash: t.hash, txSeq: { '!=': seq } }))) || 0;
|
|
41
|
+
}
|
|
42
|
+
return rows;
|
|
43
|
+
}
|
|
44
|
+
async function backfillMetadataPositions(opts = {}) {
|
|
45
|
+
const progress = { atSlot: 0, toSlot: 0, rows: 0 };
|
|
46
|
+
const range = await (0, window_plan_1.slotRangeOf)(cardano_1.Transactions, opts);
|
|
47
|
+
if (!range)
|
|
48
|
+
return progress;
|
|
49
|
+
progress.atSlot = range.from;
|
|
50
|
+
progress.toSlot = range.to;
|
|
51
|
+
await (0, window_plan_1.walkSlotWindows)(range, {
|
|
52
|
+
slotWindow: opts.slotWindow,
|
|
53
|
+
shouldStop: opts.shouldStop,
|
|
54
|
+
afterWindow: (atSlot) => { progress.atSlot = atSlot; opts.onProgress?.({ ...progress }); },
|
|
55
|
+
}, async (tx, [lo, hi]) => { progress.rows += await fillWindow(tx, lo, hi); });
|
|
56
|
+
logger.info(`metadata positions: ${progress.rows} rows filled, slots ${range.from} to ${progress.atSlot}`);
|
|
57
|
+
return progress;
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=metadata-position-backfill.js.map
|