@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
|
@@ -11,25 +11,15 @@ const DEFAULT_CONFIG = {
|
|
|
11
11
|
successThreshold: 2,
|
|
12
12
|
resetTimeoutMs: 30_000,
|
|
13
13
|
};
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
* Tracks per-backend failure counts and prevents hammering unhealthy backends.
|
|
17
|
-
*
|
|
18
|
-
* States:
|
|
19
|
-
* closed → normal operation, requests pass through
|
|
20
|
-
* open → backend is unhealthy, requests are skipped
|
|
21
|
-
* half-open → probe period, limited requests allowed to test recovery
|
|
22
|
-
*/
|
|
14
|
+
// Per-backend circuit breaker. Closed passes requests, open skips the backend.
|
|
15
|
+
// Half-open lets a few probe requests through to test recovery.
|
|
23
16
|
class CircuitBreakerManager {
|
|
24
17
|
circuits = new Map();
|
|
25
18
|
config;
|
|
26
19
|
constructor(config) {
|
|
27
20
|
this.config = { ...DEFAULT_CONFIG, ...config };
|
|
28
21
|
}
|
|
29
|
-
/**
|
|
30
|
-
* Check if a backend should be attempted.
|
|
31
|
-
* Transitions open → half-open if reset timeout has elapsed.
|
|
32
|
-
*/
|
|
22
|
+
/** Moves an open circuit to half-open once the reset timeout has elapsed. */
|
|
33
23
|
shouldAttempt(backendName) {
|
|
34
24
|
const entry = this.circuits.get(backendName);
|
|
35
25
|
if (!entry)
|
|
@@ -47,8 +37,8 @@ class CircuitBreakerManager {
|
|
|
47
37
|
}
|
|
48
38
|
return false;
|
|
49
39
|
}
|
|
50
|
-
// half-open: cap in-flight probes at successThreshold
|
|
51
|
-
//
|
|
40
|
+
// half-open: cap in-flight probes at successThreshold, so a burst of
|
|
41
|
+
// concurrent requests does not all probe a barely-recovering backend
|
|
52
42
|
if (entry.halfOpenProbes >= this.config.successThreshold) {
|
|
53
43
|
return false;
|
|
54
44
|
}
|
|
@@ -76,6 +66,12 @@ class CircuitBreakerManager {
|
|
|
76
66
|
entry.failures = 0;
|
|
77
67
|
}
|
|
78
68
|
}
|
|
69
|
+
// A verdict about the request (404, 400) shows the backend is up. It counts toward closing a
|
|
70
|
+
// half-open circuit, but in the closed state it does not wipe the count of real failures.
|
|
71
|
+
recordVerdict(backendName) {
|
|
72
|
+
if (this.circuits.get(backendName)?.state === 'half-open')
|
|
73
|
+
this.recordSuccess(backendName);
|
|
74
|
+
}
|
|
79
75
|
/** Record a failed call to a backend. */
|
|
80
76
|
recordFailure(backendName) {
|
|
81
77
|
let entry = this.circuits.get(backendName);
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import cds from '@sap/cds';
|
|
2
|
+
import type { RewardAccountStateBackend } from '../backends/cardano-backend';
|
|
3
|
+
export interface AccountSnapshotOptions {
|
|
4
|
+
backend: RewardAccountStateBackend;
|
|
5
|
+
epoch: number;
|
|
6
|
+
at: {
|
|
7
|
+
slot: number;
|
|
8
|
+
hash: string;
|
|
9
|
+
};
|
|
10
|
+
withdrawals: boolean;
|
|
11
|
+
pageSize?: number;
|
|
12
|
+
shouldStop?: () => boolean;
|
|
13
|
+
}
|
|
14
|
+
export interface AccountSnapshotResult {
|
|
15
|
+
taken: boolean;
|
|
16
|
+
asked: number;
|
|
17
|
+
accounts: number;
|
|
18
|
+
}
|
|
19
|
+
/** Where the crawl learns which accounts exist: the UTxO set and the crawled certificates. */
|
|
20
|
+
declare const SOURCES: readonly ["ledger", "certificates"];
|
|
21
|
+
type Source = (typeof SOURCES)[number];
|
|
22
|
+
/** The page query as an opaque value, for a test that renders its SQL. */
|
|
23
|
+
export declare function accountPageQuery(source: Source, after: string, limit: number): object;
|
|
24
|
+
export declare function creditedAmount(now: string | null, before: bigint | undefined, withdrawn: bigint): string | null;
|
|
25
|
+
export declare function snapshotAccounts(opts: AccountSnapshotOptions): Promise<AccountSnapshotResult>;
|
|
26
|
+
/** Removes the account snapshots read at a block after `forkSlot`. Part of the reorg transaction. */
|
|
27
|
+
export declare function dropAccountSnapshotsAfter(tx: cds.Transaction, forkSlot: number): Promise<void>;
|
|
28
|
+
export {};
|
|
29
|
+
//# sourceMappingURL=account-snapshots.d.ts.map
|
|
@@ -0,0 +1,173 @@
|
|
|
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.accountPageQuery = accountPageQuery;
|
|
7
|
+
exports.creditedAmount = creditedAmount;
|
|
8
|
+
exports.snapshotAccounts = snapshotAccounts;
|
|
9
|
+
exports.dropAccountSnapshotsAfter = dropAccountSnapshotsAfter;
|
|
10
|
+
// Reward account snapshot of one epoch. Asks the node, at the snapshot block, about every
|
|
11
|
+
// account the crawl knows and stores the ones it reports. Runs after the pool snapshot.
|
|
12
|
+
const cds_1 = __importDefault(require("@sap/cds"));
|
|
13
|
+
const cardano_1 = require("#cds-models/odatano/cardano");
|
|
14
|
+
const collections_1 = require("../../utils/collections");
|
|
15
|
+
const logger = cds_1.default.log('AccountSnapshots');
|
|
16
|
+
const { SELECT, UPSERT, UPDATE, DELETE } = cds_1.default.ql;
|
|
17
|
+
/** Where the crawl learns which accounts exist: the UTxO set and the crawled certificates. */
|
|
18
|
+
const SOURCES = ['ledger', 'certificates'];
|
|
19
|
+
const lovelace = (value) => {
|
|
20
|
+
if (value === null || value === undefined || value === '')
|
|
21
|
+
return 0n;
|
|
22
|
+
return BigInt(typeof value === 'number' ? Math.trunc(value) : String(value).split('.')[0]);
|
|
23
|
+
};
|
|
24
|
+
/** Page query of one source: stake addresses after `after`, in the order of the column's index. */
|
|
25
|
+
function pageQuery(source, after, limit) {
|
|
26
|
+
const q = source === 'ledger'
|
|
27
|
+
? SELECT.from(cardano_1.LedgerAccounts).columns('stakeAddress').where({ stakeAddress: { '>': after } }).limit(limit)
|
|
28
|
+
: SELECT.distinct.from(cardano_1.TransactionCertificates).columns('stakeAddress').where({ stakeAddress: { '>': after } }).limit(limit);
|
|
29
|
+
// nulls last is the order of a plain index; without it PostgreSQL sorts the range on every page
|
|
30
|
+
q.SELECT.orderBy = [{ ref: ['stakeAddress'], sort: 'asc', nulls: 'last' }];
|
|
31
|
+
return q;
|
|
32
|
+
}
|
|
33
|
+
/** The page query as an opaque value, for a test that renders its SQL. */
|
|
34
|
+
function accountPageQuery(source, after, limit) {
|
|
35
|
+
return pageQuery(source, after, limit);
|
|
36
|
+
}
|
|
37
|
+
async function accountPage(source, after, limit) {
|
|
38
|
+
const rows = await cds_1.default.tx((tx) => tx.run(pageQuery(source, after, limit)));
|
|
39
|
+
return rows.map(r => r.stakeAddress);
|
|
40
|
+
}
|
|
41
|
+
/** The accounts of `page` that the UTxO set does not list; those were asked in the first pass. */
|
|
42
|
+
async function notInLedger(page) {
|
|
43
|
+
const known = new Set();
|
|
44
|
+
await cds_1.default.tx(async (tx) => {
|
|
45
|
+
for (const part of (0, collections_1.chunk)(page, collections_1.IN_CHUNK)) {
|
|
46
|
+
const rows = await tx.run(SELECT.from(cardano_1.LedgerAccounts).columns('stakeAddress').where({ stakeAddress: { in: part } }));
|
|
47
|
+
for (const r of rows)
|
|
48
|
+
known.add(r.stakeAddress);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
return known.size ? page.filter(a => !known.has(a)) : page;
|
|
52
|
+
}
|
|
53
|
+
/** Slot of the account snapshot of `epoch`, or null when that epoch has none. */
|
|
54
|
+
async function accountSnapshotSlot(epoch) {
|
|
55
|
+
const row = await cds_1.default.tx((tx) => tx.run(SELECT.one.from(cardano_1.EpochLedgerSnapshots).columns('accountCount', 'accountSnapshotSlot').where({ epoch })));
|
|
56
|
+
return row?.accountCount == null || row.accountSnapshotSlot == null ? null : Number(row.accountSnapshotSlot);
|
|
57
|
+
}
|
|
58
|
+
// Balance difference to the previous snapshot plus what was withdrawn in between.
|
|
59
|
+
// Below zero means a withdrawal the crawl did not store, so the amount is unknown.
|
|
60
|
+
function creditedAmount(now, before, withdrawn) {
|
|
61
|
+
if (now === null || before === undefined)
|
|
62
|
+
return null;
|
|
63
|
+
const credited = BigInt(now) - before + withdrawn;
|
|
64
|
+
return credited < 0n ? null : credited.toString();
|
|
65
|
+
}
|
|
66
|
+
// True when every stored block in (fromSlot, toSlot] carries its withdrawals. A block crawled
|
|
67
|
+
// without them, for example over Blockfrost, would make a credited amount too small.
|
|
68
|
+
async function withdrawalsComplete(fromSlot, toSlot) {
|
|
69
|
+
return cds_1.default.tx(async (tx) => {
|
|
70
|
+
for (const marker of [false, null]) {
|
|
71
|
+
const gap = await tx.run(SELECT.one.from(cardano_1.Blocks).columns('hash')
|
|
72
|
+
.where({ slot: { between: fromSlot + 1, and: toSlot }, withdrawalsReported: marker }));
|
|
73
|
+
if (gap)
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
return true;
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
async function storePage(tx, reported, opts, previousSlot) {
|
|
80
|
+
const before = new Map();
|
|
81
|
+
const withdrawn = new Map();
|
|
82
|
+
if (previousSlot != null) {
|
|
83
|
+
for (const part of (0, collections_1.chunk)(reported.map(a => a.stakeAddress), collections_1.IN_CHUNK)) {
|
|
84
|
+
const balances = await tx.run(SELECT.from(cardano_1.AccountEpochSnapshots).columns('stakeAddress', 'withdrawableAmount').where({ epoch: opts.epoch - 1, stakeAddress: { in: part } }));
|
|
85
|
+
for (const r of balances)
|
|
86
|
+
if (r.withdrawableAmount != null)
|
|
87
|
+
before.set(r.stakeAddress, lovelace(r.withdrawableAmount));
|
|
88
|
+
const sums = await tx.run(SELECT.from(cardano_1.TransactionWithdrawals).columns('stakeAddress', 'sum(lovelace) as total')
|
|
89
|
+
.where({ stakeAddress: { in: part }, 'tx.slot': { between: previousSlot + 1, and: opts.at.slot } })
|
|
90
|
+
.groupBy('stakeAddress'));
|
|
91
|
+
for (const r of sums)
|
|
92
|
+
withdrawn.set(r.stakeAddress, lovelace(r.total));
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const rows = reported.map(a => ({
|
|
96
|
+
epoch: opts.epoch,
|
|
97
|
+
stakeAddress: a.stakeAddress,
|
|
98
|
+
withdrawableAmount: a.withdrawableAmount,
|
|
99
|
+
creditedAmount: creditedAmount(a.withdrawableAmount, before.get(a.stakeAddress), withdrawn.get(a.stakeAddress) ?? 0n),
|
|
100
|
+
deposit: a.deposit,
|
|
101
|
+
poolId: a.poolId,
|
|
102
|
+
drepId: a.drepId,
|
|
103
|
+
}));
|
|
104
|
+
for (const part of (0, collections_1.chunk)(rows, collections_1.IN_CHUNK))
|
|
105
|
+
await tx.run(UPSERT.into(cardano_1.AccountEpochSnapshots).entries(part));
|
|
106
|
+
}
|
|
107
|
+
// Takes the account snapshot of `epoch` unless it has one. The marker on EpochLedgerSnapshots is
|
|
108
|
+
// written last, and only while the snapshot block is still on the stored chain.
|
|
109
|
+
async function snapshotAccounts(opts) {
|
|
110
|
+
const { backend, epoch, at } = opts;
|
|
111
|
+
const pageSize = opts.pageSize ?? 2000;
|
|
112
|
+
const marker = await cds_1.default.tx((tx) => tx.run(SELECT.one.from(cardano_1.EpochLedgerSnapshots).columns('epoch', 'accountCount').where({ epoch })));
|
|
113
|
+
if (!marker)
|
|
114
|
+
return { taken: false, asked: 0, accounts: 0 };
|
|
115
|
+
if (marker.accountCount != null)
|
|
116
|
+
return { taken: false, asked: 0, accounts: Number(marker.accountCount) };
|
|
117
|
+
// rows of an attempt that did not finish were read at another block
|
|
118
|
+
await cds_1.default.tx((tx) => tx.run(DELETE.from(cardano_1.AccountEpochSnapshots).where({ epoch })));
|
|
119
|
+
const previousSnapshotSlot = await accountSnapshotSlot(epoch - 1);
|
|
120
|
+
// Without the withdrawals of the whole interval no credited amount is derived.
|
|
121
|
+
const previousSlot = previousSnapshotSlot != null && opts.withdrawals && await withdrawalsComplete(previousSnapshotSlot, at.slot)
|
|
122
|
+
? previousSnapshotSlot
|
|
123
|
+
: null;
|
|
124
|
+
let asked = 0;
|
|
125
|
+
await backend.rewardAccountsAt({ slot: at.slot, hash: at.hash }, async (ask) => {
|
|
126
|
+
let ledgerHasAccounts = false;
|
|
127
|
+
for (const source of SOURCES) {
|
|
128
|
+
for (let after = '';;) {
|
|
129
|
+
if (opts.shouldStop?.())
|
|
130
|
+
throw new Error(`account snapshot of epoch ${epoch} stopped`);
|
|
131
|
+
const page = await accountPage(source, after, pageSize);
|
|
132
|
+
if (!page.length)
|
|
133
|
+
break;
|
|
134
|
+
after = page[page.length - 1];
|
|
135
|
+
if (source === 'ledger')
|
|
136
|
+
ledgerHasAccounts = true;
|
|
137
|
+
const unasked = source === 'certificates' && ledgerHasAccounts ? await notInLedger(page) : page;
|
|
138
|
+
if (!unasked.length)
|
|
139
|
+
continue;
|
|
140
|
+
asked += unasked.length;
|
|
141
|
+
const reported = await ask(unasked);
|
|
142
|
+
if (reported.length)
|
|
143
|
+
await cds_1.default.tx((tx) => storePage(tx, reported, opts, previousSlot));
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
// A stopped attempt never writes the marker: its rows may be read at another block than the next one's.
|
|
148
|
+
if (opts.shouldStop?.())
|
|
149
|
+
throw new Error(`account snapshot of epoch ${epoch} stopped`);
|
|
150
|
+
const accounts = await cds_1.default.tx(async (tx) => {
|
|
151
|
+
const block = await tx.run(SELECT.one.from(cardano_1.Blocks).columns('hash').where({ hash: at.hash }));
|
|
152
|
+
if (!block) {
|
|
153
|
+
await tx.run(DELETE.from(cardano_1.AccountEpochSnapshots).where({ epoch }));
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
const counted = await tx.run(SELECT.one.from(cardano_1.AccountEpochSnapshots).columns('count(*) as n').where({ epoch }));
|
|
157
|
+
await tx.run(UPDATE.entity(cardano_1.EpochLedgerSnapshots).set({ accountCount: Number(counted.n), accountSnapshotSlot: at.slot }).where({ epoch }));
|
|
158
|
+
return Number(counted.n);
|
|
159
|
+
});
|
|
160
|
+
if (accounts == null)
|
|
161
|
+
throw new Error(`account snapshot of epoch ${epoch}: block ${at.hash} is no longer on the stored chain`);
|
|
162
|
+
logger.info(`epoch ${epoch} account snapshot (ogmios @ slot ${at.slot}): ${accounts} accounts, ${asked} asked`);
|
|
163
|
+
return { taken: true, asked, accounts };
|
|
164
|
+
}
|
|
165
|
+
/** Removes the account snapshots read at a block after `forkSlot`. Part of the reorg transaction. */
|
|
166
|
+
async function dropAccountSnapshotsAfter(tx, forkSlot) {
|
|
167
|
+
const stale = await tx.run(SELECT.from(cardano_1.EpochLedgerSnapshots).columns('epoch').where({ accountSnapshotSlot: { '>': forkSlot } }));
|
|
168
|
+
for (const { epoch } of stale) {
|
|
169
|
+
await tx.run(DELETE.from(cardano_1.AccountEpochSnapshots).where({ epoch }));
|
|
170
|
+
await tx.run(UPDATE.entity(cardano_1.EpochLedgerSnapshots).set({ accountCount: null, accountSnapshotSlot: null }).where({ epoch }));
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
//# sourceMappingURL=account-snapshots.js.map
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface DecimalsBackfillProgress {
|
|
2
|
+
/** Catalogue rows read. */
|
|
3
|
+
scanned: number;
|
|
4
|
+
/** Units with label 333 or 444. */
|
|
5
|
+
candidates: number;
|
|
6
|
+
/** Units that received a decimals value. */
|
|
7
|
+
filled: number;
|
|
8
|
+
}
|
|
9
|
+
export interface DecimalsBackfillOptions {
|
|
10
|
+
/** Rows per page. */
|
|
11
|
+
chunkSize?: number;
|
|
12
|
+
onProgress?: (p: DecimalsBackfillProgress) => void;
|
|
13
|
+
/** Stops the walk before the next page. */
|
|
14
|
+
shouldStop?: () => boolean;
|
|
15
|
+
}
|
|
16
|
+
export declare function backfillAssetDecimals(opts?: DecimalsBackfillOptions): Promise<DecimalsBackfillProgress>;
|
|
17
|
+
//# sourceMappingURL=asset-decimals-backfill.d.ts.map
|
|
@@ -0,0 +1,56 @@
|
|
|
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.backfillAssetDecimals = backfillAssetDecimals;
|
|
7
|
+
const cds_1 = __importDefault(require("@sap/cds"));
|
|
8
|
+
const cardano_1 = require("#cds-models/odatano/cardano");
|
|
9
|
+
const cip68_1 = require("../../cbor/cip68");
|
|
10
|
+
const asset_decimals_1 = require("../asset-decimals");
|
|
11
|
+
const key_pages_1 = require("./key-pages");
|
|
12
|
+
const logger = cds_1.default.log('DecimalsBackfill');
|
|
13
|
+
// Fill Assets.decimals from the CIP-68 reference datum for label 333 and 444 units. Pages by key
|
|
14
|
+
// only, so the query plan never depends on column statistics. A repeat run refreshes the values.
|
|
15
|
+
// A unit with more slices than a page holds is resolved per page; each part writes the same value.
|
|
16
|
+
async function backfillAssetDecimals(opts = {}) {
|
|
17
|
+
const chunkSize = opts.chunkSize ?? 5000;
|
|
18
|
+
const progress = { scanned: 0, candidates: 0, filled: 0 };
|
|
19
|
+
// units are adjacent in key order, so only the last unit of the previous page can go on
|
|
20
|
+
let carried = null;
|
|
21
|
+
await (0, key_pages_1.pageByKey)({
|
|
22
|
+
entity: cardano_1.Assets, keys: ['unit', 'validFrom'], columns: ['initialMintTxHash'], chunkSize, shouldStop: opts.shouldStop,
|
|
23
|
+
}, async (rows) => {
|
|
24
|
+
const mints = new Map();
|
|
25
|
+
for (const r of rows) {
|
|
26
|
+
if (!(0, cip68_1.cip68ReferenceUnit)(r.unit))
|
|
27
|
+
continue;
|
|
28
|
+
if (!mints.has(r.unit) || r.initialMintTxHash)
|
|
29
|
+
mints.set(r.unit, r.initialMintTxHash ?? null);
|
|
30
|
+
}
|
|
31
|
+
const filled = new Set();
|
|
32
|
+
await cds_1.default.tx(async (tx) => {
|
|
33
|
+
for (const [unit, mintTxHash] of mints) {
|
|
34
|
+
const decimals = await (0, asset_decimals_1.cip68Decimals)(tx, unit, mintTxHash);
|
|
35
|
+
if (decimals == null)
|
|
36
|
+
continue;
|
|
37
|
+
await (0, asset_decimals_1.setAssetDecimals)(tx, unit, decimals);
|
|
38
|
+
filled.add(unit);
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
progress.scanned += rows.length;
|
|
42
|
+
for (const unit of mints.keys()) {
|
|
43
|
+
const continued = carried?.unit === unit;
|
|
44
|
+
if (!continued)
|
|
45
|
+
progress.candidates++;
|
|
46
|
+
if (filled.has(unit) && !(continued && carried.filled))
|
|
47
|
+
progress.filled++;
|
|
48
|
+
}
|
|
49
|
+
const last = rows[rows.length - 1].unit;
|
|
50
|
+
carried = { unit: last, filled: filled.has(last) || (carried?.unit === last && carried.filled) };
|
|
51
|
+
opts.onProgress?.({ ...progress });
|
|
52
|
+
});
|
|
53
|
+
logger.info(`asset decimals filled: ${progress.filled} of ${progress.candidates} CIP-68 units, ${progress.scanned} catalogue rows read`);
|
|
54
|
+
return progress;
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=asset-decimals-backfill.js.map
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { CardanoClient } from '../cardano-client';
|
|
2
|
+
import type { CardanoIndexer } from '../cardano-indexer';
|
|
3
|
+
export interface RegistryBackfillProgress {
|
|
4
|
+
/** Units with a CIP-26 registry entry. */
|
|
5
|
+
units: number;
|
|
6
|
+
/** Units handled so far, filled or failed. */
|
|
7
|
+
processed: number;
|
|
8
|
+
/** Units whose Assets rows received the registry values. */
|
|
9
|
+
filled: number;
|
|
10
|
+
/** Units the backend did not resolve, or entry files that could not be read. */
|
|
11
|
+
failed: number;
|
|
12
|
+
/** Source file: registry units the database has no Assets row for. */
|
|
13
|
+
absent: number;
|
|
14
|
+
/** Source file: entries that describe no native asset. */
|
|
15
|
+
skipped: number;
|
|
16
|
+
}
|
|
17
|
+
export interface RegistryBackfillOptions {
|
|
18
|
+
client: CardanoClient;
|
|
19
|
+
indexer: CardanoIndexer;
|
|
20
|
+
/** Backend calls per second; one unit is one call. */
|
|
21
|
+
unitsPerSecond?: number;
|
|
22
|
+
onProgress?: (p: RegistryBackfillProgress) => void;
|
|
23
|
+
/** Stops the run before the next unit. */
|
|
24
|
+
shouldStop?: () => boolean;
|
|
25
|
+
}
|
|
26
|
+
export declare function backfillAssetRegistry(opts: RegistryBackfillOptions): Promise<RegistryBackfillProgress>;
|
|
27
|
+
/** The registry values of one unit, as the Assets columns hold them. */
|
|
28
|
+
export interface RegistryEntry {
|
|
29
|
+
unit: string;
|
|
30
|
+
registryName: string | null;
|
|
31
|
+
registryTicker: string | null;
|
|
32
|
+
registryDecimals: number | null;
|
|
33
|
+
registryDescription: string | null;
|
|
34
|
+
registryUrl: string | null;
|
|
35
|
+
registryLogo: string | null;
|
|
36
|
+
}
|
|
37
|
+
export declare function parseRegistryEntry(json: string): RegistryEntry | null;
|
|
38
|
+
export interface RegistryFileOptions {
|
|
39
|
+
directory: string;
|
|
40
|
+
onProgress?: (p: RegistryBackfillProgress) => void;
|
|
41
|
+
shouldStop?: () => boolean;
|
|
42
|
+
}
|
|
43
|
+
/** The entry files of `directory`; refuses a path that holds none. */
|
|
44
|
+
export declare function registryEntryFiles(directory: string): Promise<string[]>;
|
|
45
|
+
export declare function backfillAssetRegistryFromFiles(opts: RegistryFileOptions): Promise<RegistryBackfillProgress>;
|
|
46
|
+
//# sourceMappingURL=asset-registry-backfill.d.ts.map
|
|
@@ -0,0 +1,157 @@
|
|
|
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.backfillAssetRegistry = backfillAssetRegistry;
|
|
7
|
+
exports.parseRegistryEntry = parseRegistryEntry;
|
|
8
|
+
exports.registryEntryFiles = registryEntryFiles;
|
|
9
|
+
exports.backfillAssetRegistryFromFiles = backfillAssetRegistryFromFiles;
|
|
10
|
+
const cds_1 = __importDefault(require("@sap/cds"));
|
|
11
|
+
const fs_1 = require("fs");
|
|
12
|
+
const path_1 = __importDefault(require("path"));
|
|
13
|
+
const cardano_1 = require("#cds-models/odatano/cardano");
|
|
14
|
+
const errors_1 = require("../../utils/errors");
|
|
15
|
+
const collections_1 = require("../../utils/collections");
|
|
16
|
+
const validators_1 = require("../../utils/validators");
|
|
17
|
+
const { UPDATE } = cds_1.default.ql;
|
|
18
|
+
const logger = cds_1.default.log('RegistryBackfill');
|
|
19
|
+
/** The run stops when this many units in a row fail: the backend is down, not the units. */
|
|
20
|
+
const MAX_CONSECUTIVE_FAILURES = 25;
|
|
21
|
+
// Fill the CIP-26 registry fields of every Assets slice for the units the registry lists. The
|
|
22
|
+
// registry is off-chain, so each unit is read from the backend; a repeat run refreshes the values.
|
|
23
|
+
async function backfillAssetRegistry(opts) {
|
|
24
|
+
const backend = opts.client.getTokenRegistryBackend();
|
|
25
|
+
if (!backend)
|
|
26
|
+
throw new errors_1.ProviderUnavailableError('No backend lists the token registry (needs Koios)');
|
|
27
|
+
const units = await backend.getTokenRegistryUnits();
|
|
28
|
+
const intervalMs = 1000 / Math.max(1, opts.unitsPerSecond ?? 4);
|
|
29
|
+
const progress = { units: units.length, processed: 0, filled: 0, failed: 0, absent: 0, skipped: 0 };
|
|
30
|
+
const failuresByError = new Map();
|
|
31
|
+
let consecutiveFailures = 0;
|
|
32
|
+
for (const unit of units) {
|
|
33
|
+
if (opts.shouldStop?.())
|
|
34
|
+
break;
|
|
35
|
+
const started = Date.now();
|
|
36
|
+
try {
|
|
37
|
+
await cds_1.default.tx(async (tx) => {
|
|
38
|
+
const row = await opts.indexer.indexAssetFromBackend(tx, unit);
|
|
39
|
+
// Every slice of the unit carries the registry values, the bare catalogue row included.
|
|
40
|
+
await tx.run(UPDATE.entity(cardano_1.Assets).set({
|
|
41
|
+
registryName: row.registryName ?? null,
|
|
42
|
+
registryTicker: row.registryTicker ?? null,
|
|
43
|
+
registryDecimals: row.registryDecimals ?? null,
|
|
44
|
+
registryDescription: row.registryDescription ?? null,
|
|
45
|
+
registryUrl: row.registryUrl ?? null,
|
|
46
|
+
registryLogo: row.registryLogo ?? null,
|
|
47
|
+
}).where({ unit }));
|
|
48
|
+
if (row.decimals != null) {
|
|
49
|
+
await tx.run(UPDATE.entity(cardano_1.Assets).set({ decimals: row.decimals }).where({ unit, decimals: null }));
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
progress.filled++;
|
|
53
|
+
consecutiveFailures = 0;
|
|
54
|
+
}
|
|
55
|
+
catch (err) {
|
|
56
|
+
progress.failed++;
|
|
57
|
+
consecutiveFailures++;
|
|
58
|
+
const errorName = err instanceof Error ? err.name : typeof err;
|
|
59
|
+
failuresByError.set(errorName, (failuresByError.get(errorName) ?? 0) + 1);
|
|
60
|
+
logger.debug(`registry fill for ${unit} failed:`, err);
|
|
61
|
+
if (!(err instanceof errors_1.NotFoundError) && consecutiveFailures >= MAX_CONSECUTIVE_FAILURES) {
|
|
62
|
+
const last = err instanceof Error ? err.message : String(err);
|
|
63
|
+
throw new errors_1.ProviderUnavailableError(`${consecutiveFailures} units in a row failed, last: ${last}`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
progress.processed++;
|
|
67
|
+
opts.onProgress?.({ ...progress });
|
|
68
|
+
const wait = intervalMs - (Date.now() - started);
|
|
69
|
+
if (wait > 0)
|
|
70
|
+
await new Promise((r) => setTimeout(r, wait));
|
|
71
|
+
}
|
|
72
|
+
logger.info(`asset registry filled: ${progress.filled} of ${progress.units} registered units, ${progress.failed} failed`);
|
|
73
|
+
if (failuresByError.size > 0) {
|
|
74
|
+
const counts = [...failuresByError].map(([name, count]) => `${name} ${count}`).join(', ');
|
|
75
|
+
logger.warn(`asset registry: units not filled, by error: ${counts}`);
|
|
76
|
+
}
|
|
77
|
+
return progress;
|
|
78
|
+
}
|
|
79
|
+
// One entry file of the registry repository. Null for an entry whose subject is no asset unit, or
|
|
80
|
+
// that carries a preimage: the registry also describes scripts. A value too long for its column is left out.
|
|
81
|
+
function parseRegistryEntry(json) {
|
|
82
|
+
const entry = JSON.parse(json);
|
|
83
|
+
const unit = typeof entry.subject === 'string' ? entry.subject.toLowerCase() : '';
|
|
84
|
+
if (!(0, validators_1.isAssetUnit)(unit) || entry.preImage !== undefined)
|
|
85
|
+
return null;
|
|
86
|
+
const text = (name, max) => {
|
|
87
|
+
const value = entry[name]?.value;
|
|
88
|
+
return typeof value === 'string' && value.length > 0 && value.length <= max ? value : null;
|
|
89
|
+
};
|
|
90
|
+
const decimals = entry.decimals?.value;
|
|
91
|
+
return {
|
|
92
|
+
unit,
|
|
93
|
+
registryName: text('name', 128),
|
|
94
|
+
registryTicker: text('ticker', 16),
|
|
95
|
+
registryDecimals: typeof decimals === 'number' && Number.isInteger(decimals) && decimals >= 0 && decimals <= 255 ? decimals : null,
|
|
96
|
+
registryDescription: text('description', 5000),
|
|
97
|
+
registryUrl: text('url', 256),
|
|
98
|
+
registryLogo: text('logo', 500_000),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
/** The entry files of `directory`; refuses a path that holds none. */
|
|
102
|
+
async function registryEntryFiles(directory) {
|
|
103
|
+
let names;
|
|
104
|
+
try {
|
|
105
|
+
names = await fs_1.promises.readdir(directory);
|
|
106
|
+
}
|
|
107
|
+
catch (err) {
|
|
108
|
+
throw new errors_1.TransactionValidationError(`path is not a readable folder: ${err instanceof Error ? err.message : String(err)}`);
|
|
109
|
+
}
|
|
110
|
+
const files = names.filter(n => n.endsWith('.json')).sort();
|
|
111
|
+
if (!files.length) {
|
|
112
|
+
throw new errors_1.TransactionValidationError('path holds no entry files (*.json). Name the folder mappings of cardano-token-registry, or registry of metadata-registry-testnet.');
|
|
113
|
+
}
|
|
114
|
+
return files;
|
|
115
|
+
}
|
|
116
|
+
// Fill the registry fields from the entry files of a checkout of the registry repository. Only
|
|
117
|
+
// units that have an Assets row are written; no backend is asked and no row is created.
|
|
118
|
+
async function backfillAssetRegistryFromFiles(opts) {
|
|
119
|
+
const files = await registryEntryFiles(opts.directory);
|
|
120
|
+
const progress = { units: files.length, processed: 0, filled: 0, failed: 0, absent: 0, skipped: 0 };
|
|
121
|
+
for (const batch of (0, collections_1.chunk)(files, 200)) {
|
|
122
|
+
if (opts.shouldStop?.())
|
|
123
|
+
break;
|
|
124
|
+
const entries = [];
|
|
125
|
+
for (const name of batch) {
|
|
126
|
+
try {
|
|
127
|
+
const entry = parseRegistryEntry(await fs_1.promises.readFile(path_1.default.join(opts.directory, name), 'utf8'));
|
|
128
|
+
if (entry)
|
|
129
|
+
entries.push(entry);
|
|
130
|
+
else
|
|
131
|
+
progress.skipped++;
|
|
132
|
+
}
|
|
133
|
+
catch (err) {
|
|
134
|
+
progress.failed++;
|
|
135
|
+
logger.debug(`registry entry ${name} not read:`, err);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
await cds_1.default.tx(async (tx) => {
|
|
139
|
+
for (const { unit, ...values } of entries) {
|
|
140
|
+
const rows = Number(await tx.run(UPDATE.entity(cardano_1.Assets).set(values).where({ unit })));
|
|
141
|
+
if (!rows) {
|
|
142
|
+
progress.absent++;
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
progress.filled++;
|
|
146
|
+
if (values.registryDecimals != null) {
|
|
147
|
+
await tx.run(UPDATE.entity(cardano_1.Assets).set({ decimals: values.registryDecimals }).where({ unit, decimals: null }));
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
progress.processed += batch.length;
|
|
152
|
+
opts.onProgress?.({ ...progress });
|
|
153
|
+
}
|
|
154
|
+
logger.info(`asset registry filled from ${opts.directory}: ${progress.filled} of ${progress.units} entries, ${progress.absent} without an asset row, ${progress.skipped} not an asset, ${progress.failed} failed`);
|
|
155
|
+
return progress;
|
|
156
|
+
}
|
|
157
|
+
//# sourceMappingURL=asset-registry-backfill.js.map
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { type BulkSqlKind } from '../../utils/db-kind';
|
|
2
|
+
export interface AssetSliceBackfillProgress {
|
|
3
|
+
/** Table being walked; null when finished. */
|
|
4
|
+
table: AssetSliceTable['name'] | null;
|
|
5
|
+
outputs: number;
|
|
6
|
+
inputs: number;
|
|
7
|
+
ledgerUtxos: number;
|
|
8
|
+
ledgerAddresses: number;
|
|
9
|
+
utxoSlices: number;
|
|
10
|
+
addressSlices: number;
|
|
11
|
+
}
|
|
12
|
+
export interface AssetSliceBackfillOptions {
|
|
13
|
+
/** Slots per window on the transaction tables. */
|
|
14
|
+
slotWindow?: number;
|
|
15
|
+
/** Units, or addresses on the cached tables, per statement. */
|
|
16
|
+
chunkSize?: number;
|
|
17
|
+
shouldStop?: () => boolean;
|
|
18
|
+
onProgress?: (p: AssetSliceBackfillProgress) => void;
|
|
19
|
+
}
|
|
20
|
+
interface AssetSliceTable {
|
|
21
|
+
name: 'TransactionOutputAssets' | 'TransactionInputAssetCopies' | 'LedgerUTxOAssets' | 'LedgerAddressAssets' | 'UTxOAssets' | 'AddressAssets';
|
|
22
|
+
table: string;
|
|
23
|
+
counter: 'outputs' | 'inputs' | 'ledgerUtxos' | 'ledgerAddresses' | 'utxoSlices' | 'addressSlices';
|
|
24
|
+
/** txSeq column of a table walked by slot window. */
|
|
25
|
+
seqColumn?: string;
|
|
26
|
+
/** Entity whose txSeq bounds the window walk. */
|
|
27
|
+
rangeOf?: unknown;
|
|
28
|
+
/** Address column of a cached table, walked along its address index; every slice of a row is set. */
|
|
29
|
+
addressColumn?: string;
|
|
30
|
+
}
|
|
31
|
+
export declare const ASSET_SLICE_TABLES: readonly AssetSliceTable[];
|
|
32
|
+
export declare function sliceFillStatement(kind: BulkSqlKind, spec: AssetSliceTable, bounds?: [number, number]): string;
|
|
33
|
+
export declare function backfillAssetSlices(opts?: AssetSliceBackfillOptions): Promise<AssetSliceBackfillProgress>;
|
|
34
|
+
export {};
|
|
35
|
+
//# sourceMappingURL=asset-slice-backfill.d.ts.map
|