@odatano/core 1.11.0 → 2.0.0-rc.10
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 +255 -0
- package/@cds-models/CardanoAgentService/index.js +36 -0
- package/@cds-models/CardanoIndexerService/index.d.ts +177 -0
- package/@cds-models/CardanoIndexerService/index.js +27 -0
- package/@cds-models/CardanoODataService/index.d.ts +693 -2172
- package/@cds-models/CardanoODataService/index.js +88 -390
- package/@cds-models/CardanoSignService/index.d.ts +448 -1576
- package/@cds-models/CardanoSignService/index.js +62 -293
- package/@cds-models/CardanoTransactionService/index.d.ts +437 -1494
- package/@cds-models/CardanoTransactionService/index.js +60 -271
- package/@cds-models/CardanoWorkerService/index.d.ts +224 -0
- package/@cds-models/CardanoWorkerService/index.js +32 -0
- package/@cds-models/_/index.d.ts +70 -40
- package/@cds-models/_/index.js +17 -32
- package/@cds-models/cds/outbox/index.d.ts +28 -0
- package/@cds-models/index.d.ts +225 -522
- package/@cds-models/index.js +35 -218
- package/@cds-models/odatano/cardano/index.d.ts +919 -2884
- package/@cds-models/odatano/cardano/index.js +115 -536
- package/@cds-models/sap/common/index.d.ts +196 -600
- package/@cds-models/sap/common/index.js +28 -269
- package/README.md +194 -102
- package/db/schema.cds +388 -1
- package/db/types.cds +48 -0
- package/package.json +48 -33
- package/src/index.d.ts +7 -1
- package/src/index.js +30 -2
- package/src/plugin.js +33 -20
- package/srv/blockchain/backends/blockfrost-backend.d.ts +21 -2
- package/srv/blockchain/backends/blockfrost-backend.js +69 -25
- package/srv/blockchain/backends/cardano-backend.d.ts +69 -0
- package/srv/blockchain/backends/cardano-backend.js +13 -0
- package/srv/blockchain/backends/koios-backend.d.ts +26 -2
- package/srv/blockchain/backends/koios-backend.js +148 -25
- package/srv/blockchain/backends/ogmios-backend.d.ts +60 -9
- package/srv/blockchain/backends/ogmios-backend.js +326 -10
- package/srv/blockchain/cardano-client.d.ts +19 -0
- package/srv/blockchain/cardano-client.js +36 -0
- package/srv/blockchain/cardano-indexer.d.ts +50 -1
- package/srv/blockchain/cardano-indexer.js +193 -0
- package/srv/blockchain/crawler/crawler.d.ts +176 -0
- package/srv/blockchain/crawler/crawler.js +744 -0
- package/srv/blockchain/crawler/hooks.d.ts +43 -0
- package/srv/blockchain/crawler/hooks.js +62 -0
- package/srv/blockchain/crawler/index.d.ts +35 -0
- package/srv/blockchain/crawler/index.js +168 -0
- package/srv/blockchain/crawler/sync-state.d.ts +106 -0
- package/srv/blockchain/crawler/sync-state.js +287 -0
- package/srv/blockchain/signing/hsm-signer.d.ts +45 -0
- package/srv/blockchain/signing/hsm-signer.js +8 -1
- package/srv/blockchain/transaction-building/buildooor-tx.d.ts +20 -0
- package/srv/blockchain/transaction-building/buildooor-tx.js +104 -20
- package/srv/blockchain/wallet-worker/build-request.d.ts +26 -0
- package/srv/blockchain/wallet-worker/build-request.js +370 -0
- package/srv/blockchain/wallet-worker/confirmation-tracker.d.ts +117 -0
- package/srv/blockchain/wallet-worker/confirmation-tracker.js +290 -0
- package/srv/blockchain/wallet-worker/index.d.ts +14 -0
- package/srv/blockchain/wallet-worker/index.js +80 -0
- package/srv/blockchain/wallet-worker/job-store.d.ts +272 -0
- package/srv/blockchain/wallet-worker/job-store.js +597 -0
- package/srv/blockchain/wallet-worker/lease-heartbeat.d.ts +43 -0
- package/srv/blockchain/wallet-worker/lease-heartbeat.js +99 -0
- package/srv/blockchain/wallet-worker/signers.d.ts +57 -0
- package/srv/blockchain/wallet-worker/signers.js +131 -0
- package/srv/blockchain/wallet-worker/wallet-worker.d.ts +145 -0
- package/srv/blockchain/wallet-worker/wallet-worker.js +542 -0
- package/srv/cardano-agent-service.cds +150 -0
- package/srv/cardano-agent-service.d.ts +2 -0
- package/srv/cardano-agent-service.js +16 -0
- package/srv/cardano-indexer-service.cds +106 -0
- package/srv/cardano-indexer-service.d.ts +2 -0
- package/srv/cardano-indexer-service.js +79 -0
- package/srv/cardano-service.js +79 -4
- package/srv/cardano-sign-service.cds +200 -180
- package/srv/cardano-tx-service.cds +32 -26
- package/srv/cardano-tx-service.d.ts +1 -11
- package/srv/cardano-tx-service.js +107 -200
- package/srv/cardano-worker-service.cds +125 -0
- package/srv/cardano-worker-service.d.ts +2 -0
- package/srv/cardano-worker-service.js +199 -0
- package/srv/server.d.ts +34 -2
- package/srv/server.js +246 -36
- package/srv/utils/agent-grants-config.d.ts +65 -0
- package/srv/utils/agent-grants-config.js +148 -0
- package/srv/utils/agent-grants.d.ts +314 -0
- package/srv/utils/agent-grants.js +821 -0
- package/srv/utils/agent-token-auth.d.ts +19 -0
- package/srv/utils/agent-token-auth.js +81 -0
- package/srv/utils/collections.d.ts +13 -0
- package/srv/utils/collections.js +24 -0
- package/srv/utils/crypto.d.ts +16 -0
- package/srv/utils/crypto.js +75 -0
- package/srv/utils/db-sanitize.d.ts +40 -0
- package/srv/utils/db-sanitize.js +98 -0
- package/srv/utils/error-codes.d.ts +6 -0
- package/srv/utils/error-codes.js +6 -0
- package/srv/utils/errors.d.ts +12 -0
- package/srv/utils/errors.js +45 -0
- package/srv/utils/liveness.d.ts +23 -0
- package/srv/utils/liveness.js +49 -0
- package/srv/utils/mappers.d.ts +27 -0
- package/srv/utils/mappers.js +80 -10
- package/srv/utils/rate-limiter.d.ts +57 -0
- package/srv/utils/rate-limiter.js +126 -0
- package/srv/utils/service-events.d.ts +26 -0
- package/srv/utils/service-events.js +46 -0
- package/srv/utils/tx-request-parsers.d.ts +71 -0
- package/srv/utils/tx-request-parsers.js +202 -0
- package/srv/utils/tx-utils.d.ts +2 -1
- package/srv/utils/tx-utils.js +3 -2
- package/srv/utils/types.d.ts +17 -1
- package/@cds-models/CardanoODataService/index.d.ts.map +0 -1
- package/@cds-models/CardanoODataService/index.js.map +0 -1
- package/@cds-models/CardanoODataService/index.ts +0 -776
- package/@cds-models/CardanoSignService/index.d.ts.map +0 -1
- package/@cds-models/CardanoSignService/index.js.map +0 -1
- package/@cds-models/CardanoSignService/index.ts +0 -569
- package/@cds-models/CardanoTransactionService/index.d.ts.map +0 -1
- package/@cds-models/CardanoTransactionService/index.js.map +0 -1
- package/@cds-models/CardanoTransactionService/index.ts +0 -526
- package/@cds-models/_/index.d.ts.map +0 -1
- package/@cds-models/_/index.js.map +0 -1
- package/@cds-models/_/index.ts +0 -96
- package/@cds-models/cds/outbox/index.ts +0 -27
- package/@cds-models/index.d.ts.map +0 -1
- package/@cds-models/index.js.map +0 -1
- package/@cds-models/index.ts +0 -245
- package/@cds-models/odatano/cardano/index.d.ts.map +0 -1
- package/@cds-models/odatano/cardano/index.js.map +0 -1
- package/@cds-models/odatano/cardano/index.ts +0 -789
- package/@cds-models/sap/common/index.d.ts.map +0 -1
- package/@cds-models/sap/common/index.js.map +0 -1
- package/@cds-models/sap/common/index.ts +0 -249
|
@@ -9,6 +9,7 @@ const CardanoODataService_1 = require("#cds-models/CardanoODataService");
|
|
|
9
9
|
const CardanoTransactionService_1 = require("#cds-models/CardanoTransactionService");
|
|
10
10
|
const CardanoSignService_1 = require("#cds-models/CardanoSignService");
|
|
11
11
|
const mappers_1 = require("../utils/mappers");
|
|
12
|
+
const collections_1 = require("../utils/collections");
|
|
12
13
|
const { UPSERT, INSERT, UPDATE, SELECT, DELETE } = cds_1.default.ql;
|
|
13
14
|
const logger = cds_1.default.log('CardanoIndexer');
|
|
14
15
|
/**
|
|
@@ -28,6 +29,76 @@ class CardanoIndexer {
|
|
|
28
29
|
client;
|
|
29
30
|
txBuilder;
|
|
30
31
|
lastParamsFetchTime = 0;
|
|
32
|
+
static CRAWL_EPOCH_REFRESH_MS = 5 * 60 * 1000;
|
|
33
|
+
static CRAWL_EPOCH_RETRY_MS = 30 * 1000;
|
|
34
|
+
/**
|
|
35
|
+
* Crawler epoch memo. Network reads are refreshed periodically while an epoch is
|
|
36
|
+
* live and once more when the next epoch starts, so counters such as block_count,
|
|
37
|
+
* fees and lastBlockTime do not remain frozen at their first observed value.
|
|
38
|
+
*
|
|
39
|
+
* At most the current and immediately previous epoch are retained. The rows are
|
|
40
|
+
* intentionally UPSERTed in every block transaction: the caller owns that
|
|
41
|
+
* transaction, so an in-memory "persisted" bit cannot know whether its commit later
|
|
42
|
+
* succeeded. Repeating one local UPSERT is cheap and remains correct after rollback.
|
|
43
|
+
*/
|
|
44
|
+
crawlEpochCache = new Map();
|
|
45
|
+
crawlEpochCurrent = null;
|
|
46
|
+
crawlEpochPrevious = null;
|
|
47
|
+
/**
|
|
48
|
+
* Network-only epoch prefetch for the crawler. Call BEFORE opening the per-block
|
|
49
|
+
* write transaction — the backend round-trip must never run while the DB write
|
|
50
|
+
* lock is held (indexBlockFull then serves the epoch from this memo). Never throws.
|
|
51
|
+
*/
|
|
52
|
+
async prefetchCrawlEpoch(epochNumber) {
|
|
53
|
+
const previousEpoch = this.crawlEpochCurrent;
|
|
54
|
+
const enteringNewEpoch = previousEpoch !== null && previousEpoch !== epochNumber;
|
|
55
|
+
// At an epoch boundary, refresh the completed epoch one last time. A failed
|
|
56
|
+
// final refresh is retried with a short backoff on subsequent blocks.
|
|
57
|
+
if (enteringNewEpoch && previousEpoch !== null) {
|
|
58
|
+
await this.refreshCrawlEpoch(previousEpoch, true, true);
|
|
59
|
+
this.crawlEpochPrevious = previousEpoch;
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
for (const [cachedEpoch, entry] of this.crawlEpochCache) {
|
|
63
|
+
if (cachedEpoch !== epochNumber && !entry.finalized) {
|
|
64
|
+
await this.refreshCrawlEpoch(cachedEpoch, false, true);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
await this.refreshCrawlEpoch(epochNumber, false, false);
|
|
69
|
+
this.crawlEpochCurrent = epochNumber;
|
|
70
|
+
// Keep only the live epoch and its predecessor/final snapshot.
|
|
71
|
+
for (const cachedEpoch of this.crawlEpochCache.keys()) {
|
|
72
|
+
if (cachedEpoch !== epochNumber && cachedEpoch !== this.crawlEpochPrevious) {
|
|
73
|
+
this.crawlEpochCache.delete(cachedEpoch);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
async refreshCrawlEpoch(epochNumber, force, finalized) {
|
|
78
|
+
const now = Date.now();
|
|
79
|
+
const existing = this.crawlEpochCache.get(epochNumber);
|
|
80
|
+
if (existing?.finalized && finalized)
|
|
81
|
+
return;
|
|
82
|
+
if (!force && existing && now < existing.nextRefreshAt)
|
|
83
|
+
return;
|
|
84
|
+
try {
|
|
85
|
+
const row = (0, mappers_1.mapEpoch)(await this.client.getEpoch(epochNumber));
|
|
86
|
+
this.crawlEpochCache.set(epochNumber, {
|
|
87
|
+
row,
|
|
88
|
+
nextRefreshAt: now + CardanoIndexer.CRAWL_EPOCH_REFRESH_MS,
|
|
89
|
+
finalized,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
// Keep the last good snapshot, but retry transient/negative results soon rather
|
|
94
|
+
// than suppressing enrichment for the rest of the five-day epoch.
|
|
95
|
+
this.crawlEpochCache.set(epochNumber, {
|
|
96
|
+
row: existing?.row,
|
|
97
|
+
nextRefreshAt: now + CardanoIndexer.CRAWL_EPOCH_RETRY_MS,
|
|
98
|
+
finalized: false,
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
}
|
|
31
102
|
/**
|
|
32
103
|
* Create a new CardanoIndexer instance
|
|
33
104
|
* @param client - The CardanoClient instance for blockchain queries
|
|
@@ -389,6 +460,128 @@ class CardanoIndexer {
|
|
|
389
460
|
await tx.run(UPSERT.into(CardanoODataService_1.Block).entries(blockEntity));
|
|
390
461
|
return blockEntity;
|
|
391
462
|
}
|
|
463
|
+
/**
|
|
464
|
+
* Bulk-index a whole block and all of its transactions in ONE pass (chain crawler,
|
|
465
|
+
* v2.0). Unlike indexTransaction()/indexBlock() this does NOT re-fetch per hash — the
|
|
466
|
+
* crawler already carries the block + full tx list from the stream/page. Rows are
|
|
467
|
+
* accumulated across the block's txs and UPSERTed once per table (few statements).
|
|
468
|
+
*
|
|
469
|
+
* All writes run inside the caller's CAP transaction (`tx`), so a block is persisted
|
|
470
|
+
* atomically — a failure mid-block rolls the whole block back, keeping the cursor and
|
|
471
|
+
* the data consistent.
|
|
472
|
+
*
|
|
473
|
+
* Inputs from the Ogmios chain-sync path arrive as bare references (no address/amount);
|
|
474
|
+
* resolveInputs() backfills them from this block's own outputs and previously-indexed
|
|
475
|
+
* outputs before mapping. Blockfrost/Koios inputs already carry address/amount and are
|
|
476
|
+
* left untouched.
|
|
477
|
+
*
|
|
478
|
+
* @param tx CAP transaction (one per block, committed by the crawler)
|
|
479
|
+
* @param blockData block header/summary from the crawler source
|
|
480
|
+
* @param txs the block's full transaction list (in block order)
|
|
481
|
+
*/
|
|
482
|
+
async indexBlockFull(tx, blockData, txs) {
|
|
483
|
+
// Block row — best-effort epoch enrichment from the prefetched memo. The network
|
|
484
|
+
// fetch happens in prefetchCrawlEpoch() BEFORE the caller opened this write tx;
|
|
485
|
+
// if the caller skipped it, resolve now (memoized) and accept the in-tx fetch.
|
|
486
|
+
let epoch;
|
|
487
|
+
if (blockData.epoch != null) {
|
|
488
|
+
await this.prefetchCrawlEpoch(blockData.epoch);
|
|
489
|
+
epoch = this.crawlEpochCache.get(blockData.epoch)?.row;
|
|
490
|
+
const epochRows = [...this.crawlEpochCache.values()]
|
|
491
|
+
.map((entry) => entry.row)
|
|
492
|
+
.filter((row) => row !== undefined);
|
|
493
|
+
if (epochRows.length)
|
|
494
|
+
await tx.run(UPSERT.into(CardanoODataService_1.Epoch).entries(epochRows));
|
|
495
|
+
}
|
|
496
|
+
await tx.run(UPSERT.into(CardanoODataService_1.Block).entries((0, mappers_1.mapBlock)(blockData, epoch)));
|
|
497
|
+
// Backfill chain-sync inputs (empty address/amount) from local outputs
|
|
498
|
+
await this.resolveInputs(tx, txs);
|
|
499
|
+
// Accumulate rows across the whole block, then one bulk UPSERT per table.
|
|
500
|
+
// NUL safety (PostgreSQL rejects U+0000 in text/JSON) is enforced for every write
|
|
501
|
+
// of this plugin by the db-level hook in srv/utils/db-sanitize.ts, not per call site.
|
|
502
|
+
const txRows = txs.map(t => (0, mappers_1.mapTransaction)(t));
|
|
503
|
+
const inputRows = txs.flatMap(t => (0, mappers_1.mapTransactionInputs)(t.hash, t.inputs ?? []));
|
|
504
|
+
const inputAssetRows = txs.flatMap(t => (0, mappers_1.mapTransactionInputAssets)(t.hash, t.inputs ?? []));
|
|
505
|
+
const outputRows = txs.flatMap(t => (0, mappers_1.mapTransactionOutputs)(t.hash, t.outputs ?? []));
|
|
506
|
+
const outputAssetRows = txs.flatMap(t => (0, mappers_1.mapTransactionOutputAssets)(t.hash, t.outputs ?? []));
|
|
507
|
+
const metadataRows = txs.flatMap(t => (0, mappers_1.mapTransactionMetadata)(t.metadata ?? []));
|
|
508
|
+
if (txRows.length)
|
|
509
|
+
await tx.run(UPSERT.into(CardanoODataService_1.Transactions).entries(txRows));
|
|
510
|
+
if (inputRows.length)
|
|
511
|
+
await tx.run(UPSERT.into(CardanoODataService_1.TransactionInputs).entries(inputRows));
|
|
512
|
+
if (inputAssetRows.length)
|
|
513
|
+
await tx.run(UPSERT.into(CardanoODataService_1.TransactionInputAssets).entries(inputAssetRows));
|
|
514
|
+
if (outputRows.length)
|
|
515
|
+
await tx.run(UPSERT.into(CardanoODataService_1.TransactionOutputs).entries(outputRows));
|
|
516
|
+
if (outputAssetRows.length)
|
|
517
|
+
await tx.run(UPSERT.into(CardanoODataService_1.TransactionOutputAssets).entries(outputAssetRows));
|
|
518
|
+
if (metadataRows.length)
|
|
519
|
+
await tx.run(UPSERT.into(CardanoODataService_1.TransactionMetadata).entries(metadataRows));
|
|
520
|
+
logger.debug(`indexBlockFull: block ${blockData.hash} — ${txs.length} txs, ${outputRows.length} outputs`);
|
|
521
|
+
}
|
|
522
|
+
/**
|
|
523
|
+
* Backfill input address/amount for chain-sync transactions whose inputs are bare
|
|
524
|
+
* references. Resolves first from this block's own outputs (a tx may spend an earlier
|
|
525
|
+
* tx's output in the same block), then batch-reads previously-indexed outputs from the
|
|
526
|
+
* DB. Inputs that already carry an address (Blockfrost/Koios) are skipped.
|
|
527
|
+
*/
|
|
528
|
+
async resolveInputs(tx, txs) {
|
|
529
|
+
// 1. In-memory index of this block's outputs (txHash#outputIndex -> {address, amount})
|
|
530
|
+
const blockOutputs = new Map();
|
|
531
|
+
for (const t of txs) {
|
|
532
|
+
for (const o of t.outputs ?? []) {
|
|
533
|
+
blockOutputs.set(`${t.hash}#${o.outputIndex}`, { address: o.address, amount: o.amount ?? [] });
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
// 2. Collect inputs still needing resolution after the same-block pass
|
|
537
|
+
const unresolved = [];
|
|
538
|
+
for (const t of txs) {
|
|
539
|
+
for (const input of t.inputs ?? []) {
|
|
540
|
+
if (input.address)
|
|
541
|
+
continue; // already resolved by the backend (Blockfrost/Koios)
|
|
542
|
+
const key = `${input.txHash}#${input.outputIndex}`;
|
|
543
|
+
const local = blockOutputs.get(key);
|
|
544
|
+
if (local) {
|
|
545
|
+
input.address = local.address;
|
|
546
|
+
input.amount = local.amount;
|
|
547
|
+
}
|
|
548
|
+
else {
|
|
549
|
+
unresolved.push({ input, key });
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
if (!unresolved.length)
|
|
554
|
+
return;
|
|
555
|
+
// 3. Batch-read prior-block outputs from the DB — chunked so a dense block's
|
|
556
|
+
// input set can never exceed a driver's bind-variable cap (same IN_CHUNK
|
|
557
|
+
// discipline as the crawler's reorg deletes)
|
|
558
|
+
const sourceHashes = [...new Set(unresolved.map(u => u.input.txHash))];
|
|
559
|
+
const addrByKey = new Map();
|
|
560
|
+
const amtByKey = new Map();
|
|
561
|
+
for (const hashChunk of (0, collections_1.chunk)(sourceHashes, collections_1.IN_CHUNK)) {
|
|
562
|
+
const [outRows, assetRows] = await Promise.all([
|
|
563
|
+
tx.run(SELECT.from(CardanoODataService_1.TransactionOutputs).where({ tx_hash: { in: hashChunk } })),
|
|
564
|
+
tx.run(SELECT.from(CardanoODataService_1.TransactionOutputAssets).where({ output_tx_hash: { in: hashChunk } })),
|
|
565
|
+
]);
|
|
566
|
+
for (const r of outRows) {
|
|
567
|
+
addrByKey.set(`${r.tx_hash}#${r.outputIndex}`, r.address_address);
|
|
568
|
+
}
|
|
569
|
+
for (const r of assetRows) {
|
|
570
|
+
const k = `${r.output_tx_hash}#${r.output_outputIndex}`;
|
|
571
|
+
const list = amtByKey.get(k) ?? [];
|
|
572
|
+
list.push({ unit: r.unit, quantity: String(r.asset_quantity) });
|
|
573
|
+
amtByKey.set(k, list);
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
for (const { input, key } of unresolved) {
|
|
577
|
+
const addr = addrByKey.get(key);
|
|
578
|
+
if (addr != null)
|
|
579
|
+
input.address = addr;
|
|
580
|
+
const amt = amtByKey.get(key);
|
|
581
|
+
if (amt)
|
|
582
|
+
input.amount = amt;
|
|
583
|
+
}
|
|
584
|
+
}
|
|
392
585
|
/**
|
|
393
586
|
* Index & return the epoch information data
|
|
394
587
|
* @param tx CAP transaction object
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import type { CardanoClient } from '../cardano-client';
|
|
2
|
+
import type { CardanoIndexer } from '../cardano-indexer';
|
|
3
|
+
import { type CrawlSyncStatusValue } from './sync-state';
|
|
4
|
+
/** Runtime configuration for the chain crawler (loaded from cds.requires by the server). */
|
|
5
|
+
export interface CrawlerConfig {
|
|
6
|
+
enabled: boolean;
|
|
7
|
+
/** Pre-sync origin. Required (slot+hash) when enabled; the crawler resumes from here on a fresh DB. */
|
|
8
|
+
startSlot?: number;
|
|
9
|
+
startBlockHash?: string;
|
|
10
|
+
startHeight?: number;
|
|
11
|
+
/** 'ogmios' = chain-sync only, 'pagination' = Blockfrost/Koios only, 'auto' = chain-sync if available else pagination. */
|
|
12
|
+
source: 'ogmios' | 'pagination' | 'auto';
|
|
13
|
+
/** Blocks fetched per catch-up round (pagination path). */
|
|
14
|
+
batchSize: number;
|
|
15
|
+
/** Stay this many blocks behind the tip to avoid the volatile chain edge. */
|
|
16
|
+
confirmationDepth: number;
|
|
17
|
+
/** Poll cadence when caught up / on transient errors (pagination path). */
|
|
18
|
+
pollIntervalMs: number;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* CardanoCrawler — pre-sync engine (v2.0). Streams the chain forward from a configured
|
|
22
|
+
* start block and bulk-indexes Blocks + Transactions (+ inputs/outputs/assets) into the
|
|
23
|
+
* DB, so consumers can query local data instead of hitting a backend per request.
|
|
24
|
+
*
|
|
25
|
+
* Two sources (see CRAWLER_DESIGN.md):
|
|
26
|
+
* - **Ogmios chain-sync** (primary): ordered rollForward + native rollBackward (reorg).
|
|
27
|
+
* - **Blockfrost/Koios pagination** (fallback): forward walk + parent-hash reorg recovery.
|
|
28
|
+
*
|
|
29
|
+
* Lifecycle: start() launches the ingest pipeline detached so the HTTP server binds
|
|
30
|
+
* immediately, but the pipeline promise is retained — stop() halts the loops (waking
|
|
31
|
+
* any pending poll sleep) and AWAITS the pipeline, so shutdown never races in-flight
|
|
32
|
+
* block writes. All per-block writes are atomic. The crawler NEVER crawls from genesis
|
|
33
|
+
* implicitly — it requires an explicit configured start point or an existing cursor.
|
|
34
|
+
*/
|
|
35
|
+
export declare class CardanoCrawler {
|
|
36
|
+
private readonly client;
|
|
37
|
+
private readonly indexer;
|
|
38
|
+
private readonly network;
|
|
39
|
+
private readonly config;
|
|
40
|
+
/** Present for production instances created by index.ts; omitted in engine unit tests. */
|
|
41
|
+
private readonly leaseOwner?;
|
|
42
|
+
/** Transient-failure retries per block before giving up. */
|
|
43
|
+
private static readonly PERSIST_RETRIES;
|
|
44
|
+
/**
|
|
45
|
+
* A block whose persist keeps failing across crawler restarts is a poison block
|
|
46
|
+
* (e.g. data PostgreSQL rejects). After this many failed persistBlock() calls for
|
|
47
|
+
* the same hash the crawler latches off (desiredRunning=false) instead of
|
|
48
|
+
* restarting forever; an operator fixes the cause and calls resumeCrawler().
|
|
49
|
+
*/
|
|
50
|
+
private static readonly POISON_BLOCK_THRESHOLD;
|
|
51
|
+
/** Process-wide, survives instance restarts: the block currently failing and how often. */
|
|
52
|
+
private static poison;
|
|
53
|
+
/**
|
|
54
|
+
* Database errors that are deterministic for the block's data — the same bytes fail
|
|
55
|
+
* the same way on every restart. Only these count towards the poison-block latch:
|
|
56
|
+
* a DB outage, a statement timeout or an exhausted pool fails the same block too,
|
|
57
|
+
* but a restart fixes those, and latching on them would disable the pre-sync for
|
|
58
|
+
* good (the contract of halt()).
|
|
59
|
+
*/
|
|
60
|
+
private static readonly DATA_REJECTION_PATTERNS;
|
|
61
|
+
/** Timeout for direct backend calls (the crawler bypasses the client's resilience layer). */
|
|
62
|
+
private static readonly CALL_TIMEOUT_MS;
|
|
63
|
+
private running;
|
|
64
|
+
private chainSyncHandle;
|
|
65
|
+
/** The detached ingest pipeline — awaited by stop() so teardown never races it. */
|
|
66
|
+
private pipeline;
|
|
67
|
+
/** Resolver that cancels a pending poll sleep (set while sleeping). */
|
|
68
|
+
private wake;
|
|
69
|
+
/** Cancellers for backend waits, so shutdown is not held hostage by their timeout. */
|
|
70
|
+
private readonly callCancels;
|
|
71
|
+
/** Chain-sync callback promises outlive openChainSync(); stop() explicitly drains them. */
|
|
72
|
+
private readonly inFlightCallbacks;
|
|
73
|
+
private haltPromise;
|
|
74
|
+
private finalStatus;
|
|
75
|
+
/** Set only by an unrecoverable halt — clears desiredRunning so restarts stay down. */
|
|
76
|
+
private latchOnHalt;
|
|
77
|
+
private leaseHeld;
|
|
78
|
+
private leaseHeartbeat;
|
|
79
|
+
private leaseWake;
|
|
80
|
+
constructor(client: CardanoClient, indexer: CardanoIndexer, network: string, config: CrawlerConfig,
|
|
81
|
+
/** Present for production instances created by index.ts; omitted in engine unit tests. */
|
|
82
|
+
leaseOwner?: string | undefined);
|
|
83
|
+
isRunning(): boolean;
|
|
84
|
+
/** True once the crawler halted because of a failure (not via stop() or a lost lease). */
|
|
85
|
+
haltedWithError(): boolean;
|
|
86
|
+
/**
|
|
87
|
+
* Start crawling. Does NOT await the ingest pipeline (fire-and-forget).
|
|
88
|
+
* Refuses to start without a resume point: an explicit configured start block or an
|
|
89
|
+
* existing cursor — never an implicit full-chain sync from genesis.
|
|
90
|
+
*/
|
|
91
|
+
start(): Promise<void>;
|
|
92
|
+
/**
|
|
93
|
+
* Stop crawling and WAIT for the pipeline to finish its in-flight step: halts the
|
|
94
|
+
* loops (waking any pending poll sleep), closes the chain-sync stream, records the
|
|
95
|
+
* final status, then awaits the detached pipeline so callers (shutdownAppContext)
|
|
96
|
+
* can safely tear down backends/DB afterwards.
|
|
97
|
+
*/
|
|
98
|
+
stop(finalStatus?: CrawlSyncStatusValue): Promise<void>;
|
|
99
|
+
/**
|
|
100
|
+
* Halt without awaiting the pipeline — the variant safe to call FROM INSIDE the
|
|
101
|
+
* pipeline (persistBlock failure, stream onError), where awaiting the pipeline
|
|
102
|
+
* would deadlock.
|
|
103
|
+
*/
|
|
104
|
+
/**
|
|
105
|
+
* @param latch clears `desiredRunning`, so NO restart brings the crawler back —
|
|
106
|
+
* only an operator's resumeCrawler does. Reserved for failures a restart cannot
|
|
107
|
+
* fix (misconfiguration, missing resume point). Runtime failures — a dropped
|
|
108
|
+
* chain-sync socket, a provider outage, a node restart — must leave it false,
|
|
109
|
+
* otherwise a routine node restart silently disables the pre-sync for good.
|
|
110
|
+
*/
|
|
111
|
+
private halt;
|
|
112
|
+
/**
|
|
113
|
+
* Start teardown without awaiting it. Internal callers can therefore halt from
|
|
114
|
+
* inside a tracked callback/pipeline without waiting on their own promise.
|
|
115
|
+
*/
|
|
116
|
+
private beginHalt;
|
|
117
|
+
private runIngestPipeline;
|
|
118
|
+
/** Bound a direct backend call — these bypass the client's timeout/breaker layer. */
|
|
119
|
+
private withTimeout;
|
|
120
|
+
private runChainSync;
|
|
121
|
+
/**
|
|
122
|
+
* Candidate intersection points, NEWEST FIRST: the cursor, then an exponentially
|
|
123
|
+
* spaced ladder of our own already-crawled ancestors, then the configured start
|
|
124
|
+
* block as the deepest anchor.
|
|
125
|
+
*
|
|
126
|
+
* Why more than the cursor: if the cursor's block was orphaned while the crawler
|
|
127
|
+
* was down, a single-point intersection fails outright ("No intersection found")
|
|
128
|
+
* and the stream never opens. With ancestors on the list the node intersects at
|
|
129
|
+
* the last common block and reports it as a rollBackward — the ordinary reorg path
|
|
130
|
+
* that handleReorg already implements. Doubling offsets cover ~32k blocks with 16
|
|
131
|
+
* points; anything deeper is past any realistic rollback and should fail loudly.
|
|
132
|
+
*/
|
|
133
|
+
private buildIntersectionPoints;
|
|
134
|
+
/** Track streamed callbacks because closing a socket does not imply DB callbacks finished. */
|
|
135
|
+
private trackCallback;
|
|
136
|
+
private runPagination;
|
|
137
|
+
/**
|
|
138
|
+
* Pagination reorg recovery: walk back by height comparing the on-chain block hash with
|
|
139
|
+
* our stored one until they agree — that height is the fork point. If the last-indexed
|
|
140
|
+
* block still matches on-chain, there is no reorg (returns false → treat as transient).
|
|
141
|
+
*/
|
|
142
|
+
private tryReorgRecovery;
|
|
143
|
+
/**
|
|
144
|
+
* Persist one block atomically and advance the cursor. Transient failures are retried
|
|
145
|
+
* a few times (recording the error streak); only repeated failure stops the crawler —
|
|
146
|
+
* the cursor is not advanced on failure, so a resume re-syncs cleanly from it.
|
|
147
|
+
* Marks the cursor 'synced' when the block is at the reported tip.
|
|
148
|
+
* @returns true when the block was persisted, false when the crawler was stopped
|
|
149
|
+
*/
|
|
150
|
+
private persistBlock;
|
|
151
|
+
/**
|
|
152
|
+
* Handle a chain rollback: in one transaction, delete every block after the fork point
|
|
153
|
+
* and exactly the transactions belonging to those blocks (plus their child rows), reset
|
|
154
|
+
* the cursor to the fork, and write a CardanoReorgLog audit row.
|
|
155
|
+
*
|
|
156
|
+
* The cut runs on the absolute-slot axis of Blocks (same axis as the fork point), and
|
|
157
|
+
* transactions are resolved via their blockHash — so:
|
|
158
|
+
* - an unresolvable fork height can never widen the delete (no height-0 fallback), and
|
|
159
|
+
* - lazily-indexed transactions of blocks the crawler never wrote are left untouched.
|
|
160
|
+
*/
|
|
161
|
+
private handleReorg;
|
|
162
|
+
/** Renew the lease while an Ogmios stream is idle; block writes renew it transactionally. */
|
|
163
|
+
private startLeaseHeartbeat;
|
|
164
|
+
/** Whether a persist error is deterministic for the block's data (see DATA_REJECTION_PATTERNS). */
|
|
165
|
+
static isDataRejection(err: unknown): boolean;
|
|
166
|
+
/** Count final persist failures per block hash (a different hash resets the count). */
|
|
167
|
+
private static notePersistFailure;
|
|
168
|
+
/** Test seam: forget the poison-block memory (process-wide state). */
|
|
169
|
+
static resetPoisonMemory(): void;
|
|
170
|
+
/** Record state only while this instance still owns the lease. */
|
|
171
|
+
private recordCrawlerError;
|
|
172
|
+
private leaseSleep;
|
|
173
|
+
/** Cancellable, unref'd delay — halt() wakes it so shutdown never waits out a poll. */
|
|
174
|
+
private sleep;
|
|
175
|
+
}
|
|
176
|
+
//# sourceMappingURL=crawler.d.ts.map
|