@odatano/core 2.0.0-rc.4 → 2.0.0-rc.40
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 +251 -0
- package/@cds-models/CardanoAgentService/index.js +36 -0
- package/@cds-models/CardanoBackendService/index.d.ts +357 -0
- package/@cds-models/CardanoBackendService/index.js +40 -0
- package/@cds-models/CardanoIndexerService/index.d.ts +194 -2
- package/@cds-models/CardanoIndexerService/index.js +25 -0
- package/@cds-models/CardanoODataService/index.d.ts +305 -7
- package/@cds-models/CardanoODataService/index.js +34 -0
- package/@cds-models/CardanoSignService/index.d.ts +28 -3
- package/@cds-models/CardanoSignService/index.js +3 -0
- package/@cds-models/CardanoTransactionService/index.d.ts +46 -13
- package/@cds-models/CardanoTransactionService/index.js +4 -0
- package/@cds-models/index.d.ts +72 -0
- package/@cds-models/index.js +12 -0
- package/@cds-models/odatano/cardano/index.d.ts +408 -7
- package/@cds-models/odatano/cardano/index.js +42 -0
- package/README.md +64 -5
- package/db/schema.cds +2297 -1633
- package/db/types.cds +114 -0
- package/package.json +27 -6
- package/scripts/migrate-slot-leader.mjs +104 -0
- package/scripts/migrate-txseq.mjs +147 -0
- package/src/index.d.ts +18 -11
- package/src/index.js +44 -14
- package/src/plugin.js +42 -52
- package/srv/blockchain/backends/blockfrost-backend.d.ts +29 -131
- package/srv/blockchain/backends/blockfrost-backend.js +53 -162
- package/srv/blockchain/backends/cardano-backend.d.ts +104 -158
- package/srv/blockchain/backends/cardano-backend.js +19 -5
- package/srv/blockchain/backends/koios-backend.d.ts +126 -60
- package/srv/blockchain/backends/koios-backend.js +376 -235
- package/srv/blockchain/backends/odatano-backend.d.ts +64 -0
- package/srv/blockchain/backends/odatano-backend.js +170 -0
- package/srv/blockchain/backends/ogmios-backend.d.ts +162 -94
- package/srv/blockchain/backends/ogmios-backend.js +556 -201
- package/srv/blockchain/backends/ogmios-frame-guard.d.ts +32 -0
- package/srv/blockchain/backends/ogmios-frame-guard.js +142 -0
- package/srv/blockchain/cardano-client.d.ts +87 -202
- package/srv/blockchain/cardano-client.js +210 -240
- package/srv/blockchain/cardano-indexer.d.ts +311 -242
- package/srv/blockchain/cardano-indexer.js +1238 -362
- package/srv/blockchain/cardano-tx-builder.d.ts +44 -81
- package/srv/blockchain/cardano-tx-builder.js +173 -132
- package/srv/blockchain/crawler/certificate-backfill.d.ts +67 -0
- package/srv/blockchain/crawler/certificate-backfill.js +169 -0
- package/srv/blockchain/crawler/crawler.d.ts +139 -54
- package/srv/blockchain/crawler/crawler.js +478 -96
- package/srv/blockchain/crawler/hooks.d.ts +3 -14
- package/srv/blockchain/crawler/index.d.ts +5 -0
- package/srv/blockchain/crawler/index.js +35 -7
- package/srv/blockchain/crawler/sync-state.d.ts +46 -28
- package/srv/blockchain/crawler/sync-state.js +128 -43
- package/srv/blockchain/crawler/transaction-backfill.d.ts +52 -0
- package/srv/blockchain/crawler/transaction-backfill.js +283 -0
- package/srv/blockchain/crawler/utxo-set-import.d.ts +78 -0
- package/srv/blockchain/crawler/utxo-set-import.js +363 -0
- package/srv/blockchain/ledger-state.d.ts +101 -0
- package/srv/blockchain/ledger-state.js +432 -0
- package/srv/blockchain/pending-spends.d.ts +28 -0
- package/srv/blockchain/pending-spends.js +89 -0
- package/srv/blockchain/signing/cose-verifier.d.ts +5 -12
- package/srv/blockchain/signing/cose-verifier.js +6 -12
- package/srv/blockchain/signing/external-signer.d.ts +16 -113
- package/srv/blockchain/signing/external-signer.js +19 -122
- package/srv/blockchain/signing/hsm-signer.d.ts +13 -45
- package/srv/blockchain/signing/hsm-signer.js +23 -63
- package/srv/blockchain/signing/signature-verifier.d.ts +14 -45
- package/srv/blockchain/signing/signature-verifier.js +73 -76
- package/srv/blockchain/signing/submission-finalizer.d.ts +9 -36
- package/srv/blockchain/signing/submission-finalizer.js +16 -49
- package/srv/blockchain/stored-chain.d.ts +33 -0
- package/srv/blockchain/stored-chain.js +234 -0
- package/srv/blockchain/transaction-building/buildooor-tx.d.ts +111 -173
- package/srv/blockchain/transaction-building/buildooor-tx.js +649 -326
- package/srv/blockchain/transaction-building/caller-datums.d.ts +29 -0
- package/srv/blockchain/transaction-building/caller-datums.js +139 -0
- package/srv/blockchain/transaction-building/cardano-tx.d.ts +10 -35
- package/srv/blockchain/transaction-rows.d.ts +12 -0
- package/srv/blockchain/transaction-rows.js +43 -0
- package/srv/blockchain/wallet-worker/build-request.d.ts +4 -17
- package/srv/blockchain/wallet-worker/build-request.js +18 -23
- package/srv/blockchain/wallet-worker/confirmation-tracker.d.ts +25 -17
- package/srv/blockchain/wallet-worker/confirmation-tracker.js +87 -41
- package/srv/blockchain/wallet-worker/index.js +2 -8
- package/srv/blockchain/wallet-worker/job-store.d.ts +36 -103
- package/srv/blockchain/wallet-worker/job-store.js +0 -0
- package/srv/blockchain/wallet-worker/lease-heartbeat.d.ts +4 -20
- package/srv/blockchain/wallet-worker/lease-heartbeat.js +4 -20
- package/srv/blockchain/wallet-worker/signers.d.ts +6 -19
- package/srv/blockchain/wallet-worker/signers.js +3 -8
- package/srv/blockchain/wallet-worker/wallet-worker.d.ts +15 -71
- package/srv/blockchain/wallet-worker/wallet-worker.js +40 -91
- package/srv/cardano-agent-service.cds +146 -0
- package/srv/cardano-agent-service.d.ts +2 -0
- package/srv/cardano-agent-service.js +12 -0
- package/srv/cardano-backend-service.cds +52 -0
- package/srv/cardano-backend-service.d.ts +2 -0
- package/srv/cardano-backend-service.js +244 -0
- package/srv/cardano-indexer-service.cds +201 -73
- package/srv/cardano-indexer-service.js +280 -11
- package/srv/cardano-service.cds +65 -2
- package/srv/cardano-service.js +170 -129
- package/srv/cardano-sign-service.cds +200 -180
- package/srv/cardano-sign-service.js +57 -184
- package/srv/cardano-tx-service.cds +439 -370
- package/srv/cardano-tx-service.js +231 -190
- package/srv/cardano-ui.cds +16 -16
- package/srv/cardano-worker-service.js +7 -15
- package/srv/cbor/index.d.ts +1 -1
- package/srv/cbor/parse.d.ts +27 -0
- package/srv/cbor/parse.js +63 -2
- package/srv/server.d.ts +28 -86
- package/srv/server.js +132 -135
- package/srv/utils/agent-grants-config.d.ts +37 -0
- package/srv/utils/agent-grants-config.js +138 -0
- package/srv/utils/agent-grants.d.ts +262 -0
- package/srv/utils/agent-grants.js +922 -0
- package/srv/utils/agent-token-auth.d.ts +16 -0
- package/srv/utils/agent-token-auth.js +78 -0
- package/srv/utils/backend-request-handler.d.ts +2 -16
- package/srv/utils/backend-request-handler.js +3 -18
- package/srv/utils/collections.d.ts +4 -8
- package/srv/utils/collections.js +4 -8
- package/srv/utils/const.d.ts +30 -67
- package/srv/utils/const.js +32 -72
- package/srv/utils/crypto.d.ts +3 -12
- package/srv/utils/crypto.js +5 -18
- package/srv/utils/db-indexes.d.ts +25 -0
- package/srv/utils/db-indexes.js +94 -0
- package/srv/utils/db-sanitize.d.ts +27 -0
- package/srv/utils/db-sanitize.js +85 -0
- package/srv/utils/epoch-slots.d.ts +20 -0
- package/srv/utils/epoch-slots.js +43 -0
- package/srv/utils/error-codes.d.ts +20 -73
- package/srv/utils/error-codes.js +19 -70
- package/srv/utils/errors.d.ts +50 -177
- package/srv/utils/errors.js +94 -218
- package/srv/utils/legacy-service-paths.d.ts +11 -0
- package/srv/utils/legacy-service-paths.js +58 -0
- package/srv/utils/liveness.d.ts +16 -0
- package/srv/utils/liveness.js +40 -0
- package/srv/utils/mappers.d.ts +128 -214
- package/srv/utils/mappers.js +354 -299
- package/srv/utils/network-context.js +2 -11
- package/srv/utils/pg-inline-json.d.ts +8 -0
- package/srv/utils/pg-inline-json.js +62 -0
- package/srv/utils/pg-order-nulls.d.ts +14 -0
- package/srv/utils/pg-order-nulls.js +56 -0
- package/srv/utils/plutus-placeholders.d.ts +23 -16
- package/srv/utils/plutus-placeholders.js +63 -27
- package/srv/utils/rate-limiter.d.ts +46 -0
- package/srv/utils/rate-limiter.js +116 -0
- package/srv/utils/service-events.d.ts +2 -22
- package/srv/utils/service-events.js +3 -23
- package/srv/utils/signing-helper.d.ts +6 -16
- package/srv/utils/signing-helper.js +36 -60
- package/srv/utils/tx-build-helper.d.ts +28 -93
- package/srv/utils/tx-build-helper.js +75 -124
- package/srv/utils/tx-request-parsers.d.ts +101 -28
- package/srv/utils/tx-request-parsers.js +410 -43
- package/srv/utils/tx-utils.d.ts +4 -23
- package/srv/utils/tx-utils.js +12 -41
- package/srv/utils/types.d.ts +252 -179
- package/srv/utils/types.js +2 -6
- package/srv/utils/validators.d.ts +24 -90
- package/srv/utils/validators.js +50 -147
|
@@ -7,21 +7,23 @@ exports.CardanoClient = void 0;
|
|
|
7
7
|
const cds_1 = __importDefault(require("@sap/cds"));
|
|
8
8
|
const cardano_backend_1 = require("./backends/cardano-backend");
|
|
9
9
|
const errors_1 = require("../utils/errors");
|
|
10
|
+
const pending_spends_1 = require("./pending-spends");
|
|
10
11
|
const circuit_breaker_1 = require("./circuit-breaker");
|
|
11
12
|
const request_coalescer_1 = require("./request-coalescer");
|
|
12
13
|
const ogmios_backend_1 = require("./backends/ogmios-backend");
|
|
13
14
|
const blockfrost_backend_1 = require("./backends/blockfrost-backend");
|
|
14
15
|
const koios_backend_1 = require("./backends/koios-backend");
|
|
16
|
+
const odatano_backend_1 = require("./backends/odatano-backend");
|
|
15
17
|
const logger = cds_1.default.log('CardanoClient');
|
|
16
|
-
/**
|
|
17
|
-
* Method routing configuration - defines which backend type to prefer for each method
|
|
18
|
-
*/
|
|
18
|
+
/** Which backend type to prefer per method. */
|
|
19
19
|
const METHOD_ROUTING = {
|
|
20
20
|
getTransaction: { preferLive: false },
|
|
21
|
+
// node first: UTxOs at the tip; Ogmios decodes the type itself (base/enterprise/…, not shelley/byron)
|
|
21
22
|
getAddress: { preferLive: true },
|
|
22
|
-
getAddressUtxos: { preferLive:
|
|
23
|
+
getAddressUtxos: { preferLive: true },
|
|
23
24
|
getAddressTransactions: { preferLive: false },
|
|
24
|
-
|
|
25
|
+
// providers first: they report circulation; Ogmios only has treasury and reserves
|
|
26
|
+
getNetworkInformation: { preferLive: false },
|
|
25
27
|
getTransactionMetadata: { preferLive: false },
|
|
26
28
|
getBlock: { preferLive: false },
|
|
27
29
|
getEpoch: { preferLive: false },
|
|
@@ -30,19 +32,17 @@ const METHOD_ROUTING = {
|
|
|
30
32
|
getCurrentSlot: { preferLive: true },
|
|
31
33
|
isUtxoUnspent: { preferLive: true },
|
|
32
34
|
getPool: { preferLive: true },
|
|
33
|
-
|
|
35
|
+
// node first; a DRep the ledger no longer lists (retired) falls through to the providers
|
|
36
|
+
getDrep: { preferLive: true },
|
|
34
37
|
getAccount: { preferLive: true },
|
|
35
38
|
getAssetInfo: { preferLive: false },
|
|
36
39
|
getProtocolParameters: { preferLive: true },
|
|
37
|
-
|
|
40
|
+
// a node's rejection (4xx) is final: another backend submits to the same ledger
|
|
41
|
+
submitTransaction: { preferLive: true, finalOnClientError: true },
|
|
38
42
|
};
|
|
39
43
|
/**
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
* Routes requests to appropriate backends:
|
|
43
|
-
* - Live/State queries → Ogmios (if available)
|
|
44
|
-
* - Historical queries → Blockfrost/Koios (if available)
|
|
45
|
-
* - Automatic fallback between backends
|
|
44
|
+
* Multi-backend Cardano client: live/state queries prefer Ogmios, historical queries prefer
|
|
45
|
+
* Blockfrost/Koios, with automatic fallback between them.
|
|
46
46
|
*/
|
|
47
47
|
class CardanoClient {
|
|
48
48
|
config;
|
|
@@ -55,18 +55,18 @@ class CardanoClient {
|
|
|
55
55
|
uninitializedBackends = new Set();
|
|
56
56
|
network;
|
|
57
57
|
max_age_ms = 60000; // default 1 minute for temporary caching
|
|
58
|
+
/** Transactions submitted through this client that the ledger view does not show yet. */
|
|
59
|
+
pendingSpends = new pending_spends_1.PendingSpends();
|
|
58
60
|
protocolParamsCache;
|
|
59
61
|
protocolParamsFetchPromise = null;
|
|
62
|
+
/** Last protocol parameters the live backend (node) answered. */
|
|
63
|
+
lastLiveProtocolParams;
|
|
60
64
|
static PROTOCOL_PARAMS_TTL_MS = 5 * 60 * 1000; // 5 minutes
|
|
61
65
|
// Request coalescers — deduplicate concurrent fetches for the same resource
|
|
62
66
|
txCoalescer = new request_coalescer_1.RequestCoalescer();
|
|
63
67
|
addrCoalescer = new request_coalescer_1.RequestCoalescer();
|
|
64
68
|
credCoalescer = new request_coalescer_1.RequestCoalescer();
|
|
65
|
-
/**
|
|
66
|
-
* Constructor for CardanoClient
|
|
67
|
-
* @param liveBackend - Optional backend for live/state queries (typically Ogmios)
|
|
68
|
-
* @param historicalBackends - Optional backends for historical queries (typically Blockfrost/Koios)
|
|
69
|
-
*/
|
|
69
|
+
/** Builds the configured backends; throws ConfigError when none is configured. */
|
|
70
70
|
constructor(clientConfig) {
|
|
71
71
|
this.network = clientConfig.network;
|
|
72
72
|
const backends = clientConfig.backends;
|
|
@@ -79,13 +79,15 @@ class CardanoClient {
|
|
|
79
79
|
if (backends.includes('koios')) {
|
|
80
80
|
this.historicalBackends.push(new koios_backend_1.KoiosBackend(clientConfig.network, clientConfig.primaryTimeoutMs, clientConfig.koiosApiKey));
|
|
81
81
|
}
|
|
82
|
+
if (backends.includes('odatano')) {
|
|
83
|
+
// A remote ODATANO runs its own failover chain: allow its primary timeout plus the fallback.
|
|
84
|
+
this.historicalBackends.push(new odatano_backend_1.OdatanoBackend(clientConfig.network, clientConfig.fallbackTimeoutMs, clientConfig.odatanoUrl, clientConfig.odatanoApiKey));
|
|
85
|
+
}
|
|
82
86
|
if (!this.liveBackend && this.historicalBackends.length === 0) {
|
|
83
87
|
throw new errors_1.ConfigError('No valid backends configured for CardanoClient');
|
|
84
88
|
}
|
|
85
89
|
this.circuitBreaker = new circuit_breaker_1.CircuitBreakerManager(clientConfig.circuitBreaker);
|
|
86
|
-
//
|
|
87
|
-
// Previously max_age_ms stayed hardcoded at 60s and indexTtlMs was dead — the
|
|
88
|
-
// documented/configurable 1h default never took effect.
|
|
90
|
+
// indexTtlMs is the cache TTL the indexer reads via max_age_ms
|
|
89
91
|
if (Number.isFinite(clientConfig.indexTtlMs) && clientConfig.indexTtlMs > 0) {
|
|
90
92
|
this.max_age_ms = clientConfig.indexTtlMs;
|
|
91
93
|
}
|
|
@@ -101,16 +103,13 @@ class CardanoClient {
|
|
|
101
103
|
return names;
|
|
102
104
|
}
|
|
103
105
|
/**
|
|
104
|
-
* Ensure backends are initialized.
|
|
105
|
-
*
|
|
106
|
-
* a transient startup failure (e.g. Ogmios briefly down) left the client
|
|
107
|
-
* permanently broken until process restart.
|
|
108
|
-
* @returns {Promise<void>}
|
|
106
|
+
* Ensure backends are initialized. A rejected init promise is cleared so the next request
|
|
107
|
+
* retries instead of leaving the client permanently broken.
|
|
109
108
|
*/
|
|
110
109
|
async ensureInitialized() {
|
|
111
110
|
if (this.initialized)
|
|
112
111
|
return;
|
|
113
|
-
//
|
|
112
|
+
// ??= keeps concurrent callers on one init promise
|
|
114
113
|
this.initPromise ??= this.initBackends().catch((err) => {
|
|
115
114
|
this.initPromise = null;
|
|
116
115
|
throw err;
|
|
@@ -118,10 +117,8 @@ class CardanoClient {
|
|
|
118
117
|
await this.initPromise;
|
|
119
118
|
}
|
|
120
119
|
/**
|
|
121
|
-
* Transient (retryable) init failures: a backend momentarily unreachable at
|
|
122
|
-
*
|
|
123
|
-
* a freshly-arrived block ("Premature close"), or the socket still coming up
|
|
124
|
-
* (ECONNREFUSED). Config/validation errors are NOT transient.
|
|
120
|
+
* Transient (retryable) init failures: a backend momentarily unreachable at startup
|
|
121
|
+
* (connection refused/reset, a cut-short health response). Config/validation errors are not.
|
|
125
122
|
*/
|
|
126
123
|
static isTransientInitError(err) {
|
|
127
124
|
const e = err;
|
|
@@ -132,12 +129,8 @@ class CardanoClient {
|
|
|
132
129
|
return /premature close|socket hang up|econnreset|econnrefused|etimedout|timeout|fetch failed|terminated|other side closed/.test(msg);
|
|
133
130
|
}
|
|
134
131
|
/**
|
|
135
|
-
* Init a backend, retrying brief transient failures.
|
|
136
|
-
*
|
|
137
|
-
* time must stay well under it (≤3 attempts × 4s init-timeout + 1s backoff
|
|
138
|
-
* ≈ 14s worst case; in practice "Premature close" returns sub-second). With
|
|
139
|
-
* the node at tip these failures are momentary block-processing blips, so a
|
|
140
|
-
* couple of quick retries ride over them; a sustained outage still fails fast.
|
|
132
|
+
* Init a backend, retrying brief transient failures. Kept small (3 attempts, 1 s backoff) so
|
|
133
|
+
* a sustained outage still fails fast and the total stays under the test bootstrap hook.
|
|
141
134
|
*/
|
|
142
135
|
async initBackendWithRetry(backend) {
|
|
143
136
|
const maxAttempts = 3;
|
|
@@ -155,12 +148,8 @@ class CardanoClient {
|
|
|
155
148
|
}
|
|
156
149
|
}
|
|
157
150
|
/**
|
|
158
|
-
* Initialize all backends
|
|
159
|
-
*
|
|
160
|
-
* and tracked in `uninitializedBackends` — the request loops retry their
|
|
161
|
-
* init lazily, so a backend that comes back online recovers without a
|
|
162
|
-
* process restart. The circuit breaker bounds the retry cost.
|
|
163
|
-
* @returns {Promise<void>}
|
|
151
|
+
* Initialize all backends. Backends whose init fails stay in rotation, tracked in
|
|
152
|
+
* `uninitializedBackends`, and are retried lazily per request (bounded by the circuit breaker).
|
|
164
153
|
*/
|
|
165
154
|
async initBackends() {
|
|
166
155
|
const initErrors = [];
|
|
@@ -220,13 +209,7 @@ class CardanoClient {
|
|
|
220
209
|
return false;
|
|
221
210
|
}
|
|
222
211
|
}
|
|
223
|
-
/**
|
|
224
|
-
* Wrap a promise with a timeout
|
|
225
|
-
* @param promise - the promise to wrap
|
|
226
|
-
* @param ms - timeout in milliseconds
|
|
227
|
-
* @param backendName - name of the backend (for error context)
|
|
228
|
-
* @returns {Promise<T>} the result of the promise or a timeout error
|
|
229
|
-
*/
|
|
212
|
+
/** Wrap a promise with a timeout that rejects with ProviderUnavailableError. */
|
|
230
213
|
withTimeout(promise, ms, backendName) {
|
|
231
214
|
let timeoutId;
|
|
232
215
|
const timeoutPromise = new Promise((_, reject) => {
|
|
@@ -236,22 +219,15 @@ class CardanoClient {
|
|
|
236
219
|
clearTimeout(timeoutId);
|
|
237
220
|
});
|
|
238
221
|
}
|
|
239
|
-
/**
|
|
240
|
-
* Get timeout for specific backend
|
|
241
|
-
* @param backend CardanoBackend instance
|
|
242
|
-
* @returns {number} timeout in milliseconds
|
|
243
|
-
*/
|
|
222
|
+
/** Per-backend timeout: Koios and a remote ODATANO get the fallback timeout, everything else the primary one. */
|
|
244
223
|
getTimeoutForBackend(backend) {
|
|
245
|
-
if (backend.name === 'koios')
|
|
224
|
+
if (backend.name === 'koios' || backend.name === 'odatano')
|
|
246
225
|
return this.config.fallbackTimeoutMs;
|
|
247
226
|
return this.config.primaryTimeoutMs;
|
|
248
227
|
}
|
|
249
228
|
/**
|
|
250
|
-
*
|
|
251
|
-
*
|
|
252
|
-
* recording (4xx exempt). For the paths that cannot fail over (Koios-only,
|
|
253
|
-
* Ogmios-only, batch-capable loops) — these previously bypassed timeout AND
|
|
254
|
-
* breaker entirely, so a hanging socket blocked the caller indefinitely.
|
|
229
|
+
* Single-backend call with the executeWithPriority resilience contract (breaker gate, timeout,
|
|
230
|
+
* success/failure recording, 4xx exempt) for paths that cannot fail over.
|
|
255
231
|
*/
|
|
256
232
|
async callWithResilience(backend, fn) {
|
|
257
233
|
if (!this.circuitBreaker.shouldAttempt(backend.name)) {
|
|
@@ -275,15 +251,11 @@ class CardanoClient {
|
|
|
275
251
|
throw backendError;
|
|
276
252
|
}
|
|
277
253
|
}
|
|
278
|
-
/**
|
|
279
|
-
|
|
280
|
-
* @param fn function to execute on backend
|
|
281
|
-
* @param preferLive whether to prefer live backend over historical
|
|
282
|
-
* @returns {Promise<T>} result from backend
|
|
283
|
-
*/
|
|
284
|
-
async executeWithPriority(fn, preferLive, methodName) {
|
|
254
|
+
/** Execute on backends in preference order (live vs historical) with automatic fallback. */
|
|
255
|
+
async executeWithPriority(fn, preferLive, methodName, finalOnClientError = false) {
|
|
285
256
|
await this.ensureInitialized();
|
|
286
257
|
const errors = [];
|
|
258
|
+
const skipped = [];
|
|
287
259
|
// Determine backend order based on preference
|
|
288
260
|
const primaryBackends = preferLive
|
|
289
261
|
? (this.liveBackend ? [this.liveBackend] : [])
|
|
@@ -297,10 +269,12 @@ class CardanoClient {
|
|
|
297
269
|
// capability routing: declared non-support is a skip, not a failure
|
|
298
270
|
if (methodName && backend.unsupportedMethods?.has(methodName)) {
|
|
299
271
|
logger.debug(`${backend.name} does not support ${methodName}, skipping`);
|
|
272
|
+
skipped.push(`${backend.name}: does not support ${methodName}`);
|
|
300
273
|
continue;
|
|
301
274
|
}
|
|
302
275
|
if (!this.circuitBreaker.shouldAttempt(backend.name)) {
|
|
303
276
|
logger.debug(`Circuit open for ${backend.name}, skipping`);
|
|
277
|
+
skipped.push(`${backend.name}: circuit open`);
|
|
304
278
|
continue;
|
|
305
279
|
}
|
|
306
280
|
// fast-path sync check — only await the init retry in the rare failure case
|
|
@@ -317,148 +291,106 @@ class CardanoClient {
|
|
|
317
291
|
catch (err) {
|
|
318
292
|
const backendError = (0, errors_1.normalizeBackendError)(err, backend.name);
|
|
319
293
|
errors.push(backendError);
|
|
320
|
-
// 4xx are definitive verdicts from a
|
|
321
|
-
//
|
|
322
|
-
// health and may open the circuit. Counting client errors opened the breaker
|
|
323
|
-
// on user mistakes and took working backends out of rotation.
|
|
294
|
+
// 4xx are definitive verdicts from a healthy backend and must not open the circuit;
|
|
295
|
+
// only 5xx/timeouts/transport errors indicate backend health.
|
|
324
296
|
const isClientError = backendError.statusCode >= 400 && backendError.statusCode < 500;
|
|
325
297
|
if (!isClientError) {
|
|
326
298
|
this.circuitBreaker.recordFailure(backend.name);
|
|
327
299
|
}
|
|
328
300
|
const logLevel = backendError.statusCode === 404 ? 'debug' : 'warn';
|
|
329
301
|
logger[logLevel](`Backend failed${backendError.statusCode === 404 ? ': resource not found' : ''}: ${backend.name} - ${backendError.message}`);
|
|
302
|
+
if (finalOnClientError && isClientError)
|
|
303
|
+
throw backendError;
|
|
330
304
|
}
|
|
331
305
|
}
|
|
332
|
-
throw new errors_1.AllBackendsFailedError(errors);
|
|
306
|
+
throw new errors_1.AllBackendsFailedError(errors, undefined, skipped);
|
|
333
307
|
}
|
|
334
|
-
/**
|
|
335
|
-
* Route method call to appropriate backend based on method routing configuration
|
|
336
|
-
* @param methodName name of the method being called
|
|
337
|
-
* @param fn function to execute on backend
|
|
338
|
-
* @returns {Promise<T>} result from backend
|
|
339
|
-
*/
|
|
308
|
+
/** Route a call by METHOD_ROUTING (default: historical first). */
|
|
340
309
|
route(methodName, fn) {
|
|
341
310
|
const config = METHOD_ROUTING[methodName] ?? { preferLive: false };
|
|
342
|
-
return this.executeWithPriority(fn, config.preferLive, methodName);
|
|
311
|
+
return this.executeWithPriority(fn, config.preferLive, methodName, config.finalOnClientError);
|
|
343
312
|
}
|
|
344
|
-
/**
|
|
345
|
-
* Get transaction by hash with fallback between backends
|
|
346
|
-
* @param txHash transaction hash (hex)
|
|
347
|
-
* @returns {Promise<Transaction>} transaction data
|
|
348
|
-
*/
|
|
313
|
+
/** Transaction by hash (coalesced across concurrent callers). */
|
|
349
314
|
getTransaction(txHash) {
|
|
350
315
|
return this.txCoalescer.get(txHash, () => this.route('getTransaction', b => b.getTransaction(txHash)));
|
|
351
316
|
}
|
|
352
|
-
/**
|
|
353
|
-
* Get address by bech32 address with fallback between backends
|
|
354
|
-
* @param address bech32 address
|
|
355
|
-
* @returns {Promise<Address>} address data
|
|
356
|
-
*/
|
|
317
|
+
/** Address data (coalesced across concurrent callers). */
|
|
357
318
|
getAddress(address) {
|
|
358
319
|
return this.addrCoalescer.get(address, () => this.route('getAddress', b => b.getAddress(address)));
|
|
359
320
|
}
|
|
360
|
-
/**
|
|
361
|
-
* Get address UTxOs with fallback between backends
|
|
362
|
-
* @param address bech32 address
|
|
363
|
-
* @returns {Promise<UTxO[]>} list of UTxOs
|
|
364
|
-
*/
|
|
365
321
|
getAddressUtxos(address) {
|
|
366
322
|
return this.route('getAddressUtxos', b => b.getAddressUtxos(address));
|
|
367
323
|
}
|
|
368
324
|
/**
|
|
369
|
-
*
|
|
370
|
-
*
|
|
371
|
-
* Throws ProviderUnavailableError if Koios is not configured.
|
|
372
|
-
* @param credHash 28-byte payment credential as 56-char lowercase hex
|
|
373
|
-
* @returns {Promise<UTxO[]>} list of UTxOs across all bech32 forms sharing the credential
|
|
325
|
+
* UTxOs by 28-byte payment credential (56-char hex), across all bech32 forms sharing it.
|
|
326
|
+
* Needs a credential-keyed backend (Koios, or another ODATANO); Blockfrost has none.
|
|
374
327
|
*/
|
|
375
328
|
getCredentialUtxos(credHash) {
|
|
376
329
|
const candidates = [this.liveBackend, ...this.historicalBackends];
|
|
377
|
-
const
|
|
378
|
-
|
|
379
|
-
|
|
330
|
+
const capable = candidates.filter((b) => typeof b?.getCredentialUtxos === 'function');
|
|
331
|
+
const backend = capable.find(b => b.name === 'koios') ?? capable[0];
|
|
332
|
+
if (!backend) {
|
|
333
|
+
throw new errors_1.ProviderUnavailableError("getCredentialUtxos requires a Koios or odatano backend. Configure one in cds.requires.odatano-core.backends.", 'koios');
|
|
380
334
|
}
|
|
381
|
-
return this.credCoalescer.get(credHash, () => this.callWithResilience(
|
|
335
|
+
return this.credCoalescer.get(credHash, () => this.callWithResilience(backend, () => backend.getCredentialUtxos(credHash)));
|
|
382
336
|
}
|
|
383
|
-
/**
|
|
384
|
-
* Get address transactions with fallback between backends
|
|
385
|
-
* @param address bech32 address
|
|
386
|
-
* @returns {Promise<Transaction[]>} list of transactions for this address
|
|
387
|
-
*/
|
|
388
337
|
getAddressTransactions(address, limit) {
|
|
389
338
|
return this.route('getAddressTransactions', b => b.getAddressTransactions(address, limit));
|
|
390
339
|
}
|
|
391
|
-
/**
|
|
392
|
-
* Get network information with fallback between backends
|
|
393
|
-
* @returns {Promise<Network>} network information
|
|
394
|
-
*/
|
|
395
340
|
getNetworkInformation() {
|
|
396
341
|
return this.route('getNetworkInformation', b => b.getNetworkInformation());
|
|
397
342
|
}
|
|
398
|
-
/**
|
|
399
|
-
* Get transaction metadata with fallback between backends
|
|
400
|
-
* @param tx_hash transaction hash (hex)
|
|
401
|
-
* @returns {Promise<MetadataLabelTx[]>} transaction metadata
|
|
402
|
-
*/
|
|
403
343
|
getTransactionMetadata(tx_hash) {
|
|
404
344
|
return this.route('getTransactionMetadata', b => b.getTransactionMetadata(tx_hash));
|
|
405
345
|
}
|
|
406
|
-
/**
|
|
407
|
-
* Get block data with fallback between backends
|
|
408
|
-
* @param block_hash block hash (hex)
|
|
409
|
-
* @returns {Promise<BlockData>} block data
|
|
410
|
-
*/
|
|
411
346
|
getBlock(block_hash) {
|
|
412
347
|
return this.route('getBlock', b => b.getBlock(block_hash));
|
|
413
348
|
}
|
|
414
|
-
/**
|
|
415
|
-
* Get epoch data with fallback between backends
|
|
416
|
-
* @param epochNumber epoch number
|
|
417
|
-
* @returns {Promise<EpochData>} epoch data
|
|
418
|
-
*/
|
|
419
349
|
getEpoch(epochNumber) {
|
|
420
350
|
return this.route('getEpoch', b => b.getEpoch(epochNumber));
|
|
421
351
|
}
|
|
422
|
-
/**
|
|
423
|
-
* Get Pool data with fallback between backends
|
|
424
|
-
* @param poolId pool id
|
|
425
|
-
* @returns {Promise<PoolData>} pool data
|
|
426
|
-
*/
|
|
427
352
|
getPool(poolId) {
|
|
428
353
|
return this.route('getPool', b => b.getPool(poolId));
|
|
429
354
|
}
|
|
430
|
-
/**
|
|
431
|
-
* Get drep data with fallback between backends
|
|
432
|
-
* @param drepId drep id
|
|
433
|
-
* @returns {Promise<DrepData>} drep data
|
|
434
|
-
*/
|
|
435
355
|
getDrep(drepId) {
|
|
436
356
|
return this.route('getDrep', b => b.getDrep(drepId));
|
|
437
357
|
}
|
|
438
|
-
/**
|
|
439
|
-
* Get account data with fallback between backends
|
|
440
|
-
* @param stakeAddress stake address
|
|
441
|
-
* @returns {Promise<AccountData>} account data
|
|
442
|
-
*/
|
|
443
358
|
getAccount(stakeAddress) {
|
|
444
359
|
return this.route('getAccount', b => b.getAccount(stakeAddress));
|
|
445
360
|
}
|
|
446
361
|
/**
|
|
447
|
-
*
|
|
448
|
-
*
|
|
449
|
-
|
|
450
|
-
|
|
362
|
+
* Whether the reward account is registered in the ledger. Node first; the first backend that
|
|
363
|
+
* answers decides, its "not found" means not registered. Only a backend failure falls through.
|
|
364
|
+
*/
|
|
365
|
+
/**
|
|
366
|
+
* Whether the stake credential is registered. The node's reward-account summary proves it, but
|
|
367
|
+
* the summary lists only credentials delegated to a pool or a DRep, so its absence proves nothing and the
|
|
368
|
+
* providers answer from the certificate history; one that knows the account decides, one that
|
|
369
|
+
* knows no such account means not registered. `null`: nobody could tell (no summary on the
|
|
370
|
+
* node, no provider configured or reachable).
|
|
451
371
|
*/
|
|
372
|
+
async isRewardAccountRegistered(stakeAddress) {
|
|
373
|
+
try {
|
|
374
|
+
return await this.executeWithPriority(async (b) => (await b.getAccount(stakeAddress)).active, true, 'getAccount');
|
|
375
|
+
}
|
|
376
|
+
catch (err) {
|
|
377
|
+
if (err instanceof errors_1.AllBackendsFailedError) {
|
|
378
|
+
const providers = err.errors.filter(e => e.backendName !== this.liveBackend?.name);
|
|
379
|
+
if (providers.some(e => e.statusCode === 404))
|
|
380
|
+
return false;
|
|
381
|
+
if (err.errors.length > 0 && err.errors.every(e => e.statusCode === 404))
|
|
382
|
+
return null;
|
|
383
|
+
}
|
|
384
|
+
throw err;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
/** Asset info (supply, mint history, CIP-25/CIP-26 metadata); Blockfrost and Koios only. */
|
|
452
388
|
getAssetInfo(unit) {
|
|
453
389
|
return this.route('getAssetInfo', b => b.getAssetInfo(unit));
|
|
454
390
|
}
|
|
455
391
|
/**
|
|
456
|
-
*
|
|
457
|
-
*
|
|
458
|
-
* Ogmios doesn't implement — fallback skips it via the optional method check.
|
|
459
|
-
* @param unit policyId + assetNameHex (concatenated hex)
|
|
460
|
-
* @param limit max number of events (default 100)
|
|
461
|
-
* @returns {Promise<AssetHistoryEntry[]>} list of mint/burn events (most recent first)
|
|
392
|
+
* Latest mint/burn events for an asset, most recent first. Prefers Koios (block timestamps);
|
|
393
|
+
* Blockfrost is the fallback with timestamps left null.
|
|
462
394
|
*/
|
|
463
395
|
getAssetHistory(unit, limit = 100) {
|
|
464
396
|
const candidates = [...this.historicalBackends, this.liveBackend];
|
|
@@ -475,15 +407,10 @@ class CardanoClient {
|
|
|
475
407
|
// Batch Methods (N+1 Optimization)
|
|
476
408
|
//-----------------------------------------------------------------------------
|
|
477
409
|
/**
|
|
478
|
-
*
|
|
479
|
-
*
|
|
480
|
-
* falls back to getAddressTransactions() and extracts hashes.
|
|
481
|
-
* @param address bech32 address
|
|
482
|
-
* @param limit maximum number of hashes
|
|
483
|
-
* @returns {Promise<string[]>} list of transaction hashes
|
|
410
|
+
* Transaction hashes for an address; falls back to a full getAddressTransactions when no
|
|
411
|
+
* backend supports the lightweight listing.
|
|
484
412
|
*/
|
|
485
413
|
async getAddressTransactionHashes(address, limit) {
|
|
486
|
-
// Try backends with getAddressTransactionHashes support
|
|
487
414
|
const allBackends = this.getOrderedBackends();
|
|
488
415
|
for (const backend of allBackends) {
|
|
489
416
|
if (backend.getAddressTransactionHashes) {
|
|
@@ -499,15 +426,8 @@ class CardanoClient {
|
|
|
499
426
|
const txs = await this.getAddressTransactions(address, limit);
|
|
500
427
|
return txs.map(tx => tx.hash);
|
|
501
428
|
}
|
|
502
|
-
/**
|
|
503
|
-
* Batch fetch multiple transactions by hash.
|
|
504
|
-
* Uses getTransactionsBatch() if backend supports it, otherwise
|
|
505
|
-
* falls back to individual getTransaction() calls (still coalesced).
|
|
506
|
-
* @param txHashes array of transaction hashes
|
|
507
|
-
* @returns {Promise<Map<string, Transaction>>} map of hash -> Transaction
|
|
508
|
-
*/
|
|
429
|
+
/** Batch fetch transactions by hash; falls back to individual coalesced getTransaction calls. */
|
|
509
430
|
async getTransactionsBatch(txHashes) {
|
|
510
|
-
// Try backends with batch support
|
|
511
431
|
const allBackends = this.getOrderedBackends();
|
|
512
432
|
for (const backend of allBackends) {
|
|
513
433
|
if (backend.getTransactionsBatch) {
|
|
@@ -523,11 +443,7 @@ class CardanoClient {
|
|
|
523
443
|
const entries = await Promise.all(txHashes.map(h => this.getTransaction(h).then(tx => [h, tx])));
|
|
524
444
|
return new Map(entries);
|
|
525
445
|
}
|
|
526
|
-
/**
|
|
527
|
-
* Backends in historical-first order. Used by the batch methods — both
|
|
528
|
-
* always prefer historical data, so the former preferLive parameter was a
|
|
529
|
-
* dead branch.
|
|
530
|
-
*/
|
|
446
|
+
/** Backends in historical-first order, for the batch methods. */
|
|
531
447
|
getOrderedBackends() {
|
|
532
448
|
const backends = [...this.historicalBackends];
|
|
533
449
|
if (this.liveBackend)
|
|
@@ -535,11 +451,8 @@ class CardanoClient {
|
|
|
535
451
|
return backends;
|
|
536
452
|
}
|
|
537
453
|
/**
|
|
538
|
-
*
|
|
539
|
-
*
|
|
540
|
-
* Backends whose init() failed are skipped — the crawler uses these instances
|
|
541
|
-
* DIRECTLY (outside executeWithPriority's lazy-retry/breaker), so handing out an
|
|
542
|
-
* uninitialized backend would wedge it.
|
|
454
|
+
* A backend that supports streamed chain-sync (Ogmios), or null. Backends whose init failed
|
|
455
|
+
* are skipped: the crawler uses these instances directly, outside the lazy-retry/breaker path.
|
|
543
456
|
*/
|
|
544
457
|
getChainSyncBackend() {
|
|
545
458
|
const candidates = [this.liveBackend, ...this.historicalBackends];
|
|
@@ -550,13 +463,80 @@ class CardanoClient {
|
|
|
550
463
|
return null;
|
|
551
464
|
}
|
|
552
465
|
/**
|
|
553
|
-
*
|
|
554
|
-
*
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
466
|
+
* getChainSyncBackend(), retrying a failed startup init. Polled by the crawler while it runs
|
|
467
|
+
* on pagination because no chain-sync source was usable when it started.
|
|
468
|
+
*/
|
|
469
|
+
async recoverChainSyncBackend() {
|
|
470
|
+
const candidates = [this.liveBackend, ...this.historicalBackends];
|
|
471
|
+
for (const b of candidates) {
|
|
472
|
+
if (!b || !(0, cardano_backend_1.isChainSyncBackend)(b))
|
|
473
|
+
continue;
|
|
474
|
+
if (await this.ensureBackendInitialized(b))
|
|
475
|
+
return b;
|
|
476
|
+
}
|
|
477
|
+
return null;
|
|
478
|
+
}
|
|
479
|
+
/** The first usable backend that can dump the UTxO set at a point (Ogmios) — crawler.utxoSet import. */
|
|
480
|
+
getLedgerStateBackend() {
|
|
481
|
+
const candidates = [this.liveBackend, ...this.historicalBackends];
|
|
482
|
+
for (const b of candidates) {
|
|
483
|
+
if (b && !this.uninitializedBackends.has(b) && (0, cardano_backend_1.isLedgerStateBackend)(b))
|
|
484
|
+
return b;
|
|
485
|
+
}
|
|
486
|
+
return null;
|
|
487
|
+
}
|
|
488
|
+
/**
|
|
489
|
+
* Outputs among `refs` that are unspent at the tip, read from the node's ledger (Ogmios, or a remote
|
|
490
|
+
* ODATANO that asks its node); null when no backend can (the caller then resolves via the producing
|
|
491
|
+
* transaction, which carries no reference-script bytes).
|
|
492
|
+
*/
|
|
493
|
+
async getUnspentOutputs(refs) {
|
|
494
|
+
const backend = this.getLedgerStateBackend() ?? this.outputLookupBackend();
|
|
495
|
+
return backend ? backend.getUnspentOutputs(refs) : null;
|
|
496
|
+
}
|
|
497
|
+
/** A usable backend answering output-reference lookups without a whole-ledger dump (the odatano backend). */
|
|
498
|
+
outputLookupBackend() {
|
|
499
|
+
const candidates = [this.liveBackend, ...this.historicalBackends];
|
|
500
|
+
for (const b of candidates) {
|
|
501
|
+
const lookup = b;
|
|
502
|
+
if (lookup && !this.uninitializedBackends.has(lookup) && typeof lookup.getUnspentOutputs === 'function') {
|
|
503
|
+
return lookup;
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
return null;
|
|
507
|
+
}
|
|
508
|
+
/** True when an initialized backend implements `method` and does not declare it unsupported. */
|
|
509
|
+
hasBackendFor(method) {
|
|
510
|
+
const candidates = [this.liveBackend, ...this.historicalBackends];
|
|
511
|
+
return candidates.some((b) => !!b
|
|
512
|
+
&& !this.uninitializedBackends.has(b)
|
|
513
|
+
&& typeof b[method] === 'function'
|
|
514
|
+
&& !b.unsupportedMethods?.has(method));
|
|
515
|
+
}
|
|
516
|
+
/** A backend that reads pool/DRep/pot state at a block (Ogmios) — preferred source of epoch snapshots. */
|
|
517
|
+
getEpochStateBackend() {
|
|
518
|
+
const candidates = [this.liveBackend, ...this.historicalBackends];
|
|
519
|
+
for (const b of candidates) {
|
|
520
|
+
if (b && !this.uninitializedBackends.has(b) && (0, cardano_backend_1.isEpochStateBackend)(b))
|
|
521
|
+
return b;
|
|
522
|
+
}
|
|
523
|
+
return null;
|
|
524
|
+
}
|
|
525
|
+
/**
|
|
526
|
+
* A backend that can enumerate the full pool/DRep set (Koios), or null; without one the epoch
|
|
527
|
+
* snapshots stay off instead of degrading into thousands of single requests.
|
|
528
|
+
*/
|
|
529
|
+
getEnumeratingBackend() {
|
|
530
|
+
const candidates = [...this.historicalBackends, this.liveBackend];
|
|
531
|
+
for (const b of candidates) {
|
|
532
|
+
if (b && !this.uninitializedBackends.has(b) && (0, cardano_backend_1.isEnumeratingBackend)(b))
|
|
533
|
+
return b;
|
|
534
|
+
}
|
|
535
|
+
return null;
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* A backend that can be walked forward by pagination (Blockfrost/Koios), or null. Koios is
|
|
539
|
+
* preferred: it batches 100 txs per call, Blockfrost needs ~3 calls per tx.
|
|
560
540
|
*/
|
|
561
541
|
getPaginatingBackend() {
|
|
562
542
|
const usable = this.historicalBackends.filter((b) => !this.uninitializedBackends.has(b) && (0, cardano_backend_1.isPaginatingBackend)(b));
|
|
@@ -570,21 +550,25 @@ class CardanoClient {
|
|
|
570
550
|
}
|
|
571
551
|
return null;
|
|
572
552
|
}
|
|
573
|
-
/**
|
|
574
|
-
* Get protocol parameters with caching (5 minute TTL)
|
|
575
|
-
* Protocol parameters rarely change (once per epoch at most), so caching improves performance
|
|
576
|
-
* @returns {Promise<LedgerProtocolParameters>} protocol parameters
|
|
577
|
-
*/
|
|
553
|
+
/** Protocol parameters with a 5-minute in-memory cache and coalesced refresh. */
|
|
578
554
|
async getProtocolParameters() {
|
|
579
555
|
const now = Date.now();
|
|
580
556
|
if (this.protocolParamsCache && (now - this.protocolParamsCache.fetchedAt) < CardanoClient.PROTOCOL_PARAMS_TTL_MS) {
|
|
581
557
|
logger.debug('Returning cached protocol parameters');
|
|
582
558
|
return this.protocolParamsCache.data;
|
|
583
559
|
}
|
|
584
|
-
// Promise coalescing: deduplicate concurrent requests during cache miss
|
|
585
560
|
if (!this.protocolParamsFetchPromise) {
|
|
586
561
|
logger.debug('Fetching fresh protocol parameters');
|
|
587
|
-
this.protocolParamsFetchPromise = this.route('getProtocolParameters', b =>
|
|
562
|
+
this.protocolParamsFetchPromise = this.route('getProtocolParameters', async (b) => {
|
|
563
|
+
const params = await b.getProtocolParameters();
|
|
564
|
+
if (b === this.liveBackend) {
|
|
565
|
+
this.lastLiveProtocolParams = params;
|
|
566
|
+
return params;
|
|
567
|
+
}
|
|
568
|
+
// A fallback answering for the epoch the node last reported: keep the node's values
|
|
569
|
+
const live = this.lastLiveProtocolParams;
|
|
570
|
+
return live && Number(live.epoch) === Number(params.epoch) ? live : params;
|
|
571
|
+
})
|
|
588
572
|
.then(params => {
|
|
589
573
|
this.protocolParamsCache = { data: params, fetchedAt: Date.now() };
|
|
590
574
|
this.protocolParamsFetchPromise = null;
|
|
@@ -597,12 +581,9 @@ class CardanoClient {
|
|
|
597
581
|
}
|
|
598
582
|
return this.protocolParamsFetchPromise;
|
|
599
583
|
}
|
|
600
|
-
/**
|
|
601
|
-
* Shutdown all backends (for cleanup in tests)
|
|
602
|
-
*/
|
|
584
|
+
/** Shut down all backends. */
|
|
603
585
|
async shutdown() {
|
|
604
586
|
logger.info('Shutting down CardanoClient backends...');
|
|
605
|
-
// Shutdown live backend if it has a shutdown method
|
|
606
587
|
if (this.liveBackend && 'shutdown' in this.liveBackend && typeof this.liveBackend.shutdown === 'function') {
|
|
607
588
|
try {
|
|
608
589
|
await this.liveBackend.shutdown();
|
|
@@ -612,7 +593,6 @@ class CardanoClient {
|
|
|
612
593
|
logger.error(`Error shutting down live backend ${this.liveBackend.name}: ${err}`);
|
|
613
594
|
}
|
|
614
595
|
}
|
|
615
|
-
// Shutdown historical backends
|
|
616
596
|
for (const backend of this.historicalBackends) {
|
|
617
597
|
if ('shutdown' in backend && typeof backend.shutdown === 'function') {
|
|
618
598
|
try {
|
|
@@ -628,70 +608,60 @@ class CardanoClient {
|
|
|
628
608
|
this.initPromise = null;
|
|
629
609
|
logger.info('CardanoClient shutdown complete');
|
|
630
610
|
}
|
|
631
|
-
/**
|
|
632
|
-
* Get latest block data with fallback between backends
|
|
633
|
-
* @returns {Promise<BlockData>} latest block data
|
|
634
|
-
*/
|
|
635
611
|
getLatestBlock() {
|
|
636
612
|
return this.route('getLatestBlock', b => b.getLatestBlock());
|
|
637
613
|
}
|
|
638
|
-
/**
|
|
639
|
-
* Get latest epoch data with fallback between backends
|
|
640
|
-
* @returns {Promise<EpochData>} latest epoch data
|
|
641
|
-
*/
|
|
642
614
|
getLatestEpoch() {
|
|
643
615
|
return this.route('getLatestEpoch', b => b.getLatestEpoch());
|
|
644
616
|
}
|
|
645
|
-
/**
|
|
646
|
-
* Get the current chain slot with fallback between backends.
|
|
647
|
-
* @returns {Promise<number>} current chain slot
|
|
648
|
-
*/
|
|
649
617
|
getCurrentSlot() {
|
|
650
618
|
return this.route('getCurrentSlot', b => b.getCurrentSlot());
|
|
651
619
|
}
|
|
652
|
-
/**
|
|
653
|
-
* Check whether a UTxO is still unspent with fallback between backends.
|
|
654
|
-
* @param txHash 64-char lowercase hex transaction hash
|
|
655
|
-
* @param outputIndex non-negative integer output index
|
|
656
|
-
* @returns {Promise<boolean>} true iff the UTxO exists and is unspent
|
|
657
|
-
*/
|
|
620
|
+
/** True iff the UTxO exists and is unspent. */
|
|
658
621
|
isUtxoUnspent(txHash, outputIndex) {
|
|
659
622
|
return this.route('isUtxoUnspent', b => b.isUtxoUnspent(txHash, outputIndex));
|
|
660
623
|
}
|
|
661
624
|
/**
|
|
662
|
-
* Submit transaction
|
|
663
|
-
*
|
|
664
|
-
* @returns {Promise<string>} transaction hash
|
|
665
|
-
*
|
|
666
|
-
* NOTE on failover semantics: when the preferred backend ACCEPTS the tx but
|
|
667
|
-
* its response is lost (timeout), the failover resubmits on the next backend,
|
|
668
|
-
* which may answer 409 "already submitted". Callers should treat a
|
|
669
|
-
* TransactionAlreadySubmittedError after a submit attempt as success — the
|
|
670
|
-
* transaction IS in the mempool.
|
|
625
|
+
* Submit a signed transaction (CBOR hex); returns its hash. On failover after a lost response
|
|
626
|
+
* the next backend may answer 409 "already submitted" — callers treat that as success.
|
|
671
627
|
*/
|
|
672
|
-
submitTransaction(signedTxCbor) {
|
|
673
|
-
|
|
628
|
+
async submitTransaction(signedTxCbor) {
|
|
629
|
+
try {
|
|
630
|
+
const hash = await this.route('submitTransaction', b => b.submitTransaction(signedTxCbor));
|
|
631
|
+
this.pendingSpends.record(signedTxCbor);
|
|
632
|
+
return hash;
|
|
633
|
+
}
|
|
634
|
+
catch (err) {
|
|
635
|
+
// already in the mempool or on chain: its inputs are gone all the same
|
|
636
|
+
if (err instanceof errors_1.TransactionAlreadySubmittedError)
|
|
637
|
+
this.pendingSpends.record(signedTxCbor);
|
|
638
|
+
throw err;
|
|
639
|
+
}
|
|
674
640
|
}
|
|
675
|
-
/**
|
|
676
|
-
* Check if Ogmios backend is available for transaction evaluation
|
|
677
|
-
* @returns {boolean} true if Ogmios is the live backend
|
|
678
|
-
*/
|
|
641
|
+
/** True when Ogmios is the live backend. */
|
|
679
642
|
hasOgmiosBackend() {
|
|
680
643
|
return this.liveBackend?.name === 'ogmios';
|
|
681
644
|
}
|
|
682
|
-
/**
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
645
|
+
/** The backend that evaluates scripts: the live one (Ogmios), else an evaluating historical one (odatano). */
|
|
646
|
+
evaluatingBackend() {
|
|
647
|
+
const candidates = [this.liveBackend, ...this.historicalBackends];
|
|
648
|
+
for (const b of candidates)
|
|
649
|
+
if (b && (0, cardano_backend_1.isEvaluatingBackend)(b))
|
|
650
|
+
return b;
|
|
651
|
+
return null;
|
|
652
|
+
}
|
|
653
|
+
/** True when some backend can evaluate script execution units. */
|
|
654
|
+
hasEvaluatingBackend() {
|
|
655
|
+
return this.evaluatingBackend() != null;
|
|
656
|
+
}
|
|
657
|
+
/** Evaluate script execution units of an unsigned transaction. */
|
|
687
658
|
async evaluateTransaction(unsignedTxCbor) {
|
|
688
659
|
await this.ensureInitialized();
|
|
689
|
-
|
|
690
|
-
if (!
|
|
691
|
-
throw new errors_1.ProviderUnavailableError('Transaction evaluation requires an evaluating backend (
|
|
660
|
+
const backend = this.evaluatingBackend();
|
|
661
|
+
if (!backend) {
|
|
662
|
+
throw new errors_1.ProviderUnavailableError('Transaction evaluation requires an evaluating backend (Ogmios or odatano)');
|
|
692
663
|
}
|
|
693
|
-
// timeout + breaker
|
|
694
|
-
const backend = this.liveBackend;
|
|
664
|
+
// timeout + breaker, so a hanging socket cannot block Plutus builds
|
|
695
665
|
return this.callWithResilience(backend, () => backend.evaluateTransaction(unsignedTxCbor));
|
|
696
666
|
}
|
|
697
667
|
}
|