@odatano/core 1.9.5 → 1.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/@cds-models/CardanoSignService/index.d.ts +14 -4
- package/@cds-models/CardanoSignService/index.d.ts.map +1 -1
- package/@cds-models/CardanoSignService/index.js.map +1 -1
- package/@cds-models/CardanoSignService/index.ts +7 -6
- package/@cds-models/odatano/cardano/index.d.ts +6 -0
- package/@cds-models/odatano/cardano/index.d.ts.map +1 -1
- package/@cds-models/odatano/cardano/index.js.map +1 -1
- package/@cds-models/odatano/cardano/index.ts +1 -0
- package/db/schema.cds +4 -0
- package/package.json +163 -163
- package/src/index.js +3 -0
- package/srv/blockchain/signing/signature-verifier.js +4 -5
- package/srv/blockchain/signing/submission-finalizer.d.ts +63 -0
- package/srv/blockchain/signing/submission-finalizer.js +153 -0
- package/srv/blockchain/transaction-building/buildooor-tx.d.ts +7 -10
- package/srv/blockchain/transaction-building/buildooor-tx.js +12 -15
- package/srv/cardano-sign-service.cds +8 -2
- package/srv/cardano-sign-service.js +100 -92
- package/srv/server.d.ts +8 -0
- package/srv/server.js +56 -0
- package/srv/utils/error-codes.d.ts +9 -0
- package/srv/utils/error-codes.js +9 -0
- package/srv/utils/tx-build-helper.d.ts +1 -2
- package/srv/utils/tx-build-helper.js +3 -5
- package/srv/utils/tx-utils.d.ts +32 -0
- package/srv/utils/tx-utils.js +108 -0
- package/srv/blockchain/transaction-building/keep-relevant.d.ts +0 -29
- package/srv/blockchain/transaction-building/keep-relevant.js +0 -116
|
@@ -0,0 +1,108 @@
|
|
|
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.DETACHED_TX_TIMEOUT_MS = void 0;
|
|
7
|
+
exports.runWithoutAmbientTx = runWithoutAmbientTx;
|
|
8
|
+
exports.detachedTx = detachedTx;
|
|
9
|
+
const cds_1 = __importDefault(require("@sap/cds"));
|
|
10
|
+
const errors_1 = require("./errors");
|
|
11
|
+
const error_codes_1 = require("./error-codes");
|
|
12
|
+
/**
|
|
13
|
+
* Transaction utilities for the sign-service.
|
|
14
|
+
*
|
|
15
|
+
* Context: @cap-js/sqlite runs on a SINGLE pooled connection. A request
|
|
16
|
+
* transaction begins lazily on its first CQL statement and holds that
|
|
17
|
+
* connection until the request commits. Any code that then needs a NEW ROOT
|
|
18
|
+
* transaction (detached committed bookkeeping) waits on the pool — forever,
|
|
19
|
+
* if the waiter is awaited by the very request holding the connection
|
|
20
|
+
* (docs/KNOWN_ISSUES.md issue 11).
|
|
21
|
+
*/
|
|
22
|
+
const logger = cds_1.default.log('tx-utils');
|
|
23
|
+
/** Default acquire budget for detached bookkeeping transactions. */
|
|
24
|
+
exports.DETACHED_TX_TIMEOUT_MS = 10_000;
|
|
25
|
+
/** Effective timeout: env override (ODATANO_DETACHED_TX_TIMEOUT_MS) > default. Read per call so tests can tune it. */
|
|
26
|
+
function effectiveTimeoutMs() {
|
|
27
|
+
const fromEnv = Number(process.env.ODATANO_DETACHED_TX_TIMEOUT_MS);
|
|
28
|
+
return Number.isFinite(fromEnv) && fromEnv > 0 ? fromEnv : exports.DETACHED_TX_TIMEOUT_MS;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Run `fn` with `cds.context` cleared, so any `db.run(...)` inside it gets a
|
|
32
|
+
* fresh short-lived transaction instead of joining a long-lived ambient one.
|
|
33
|
+
* Under @cap-js/sqlite (pool.max=1) this is what keeps the single pooled
|
|
34
|
+
* connection free during long awaits (e.g. the deferred network submit).
|
|
35
|
+
*/
|
|
36
|
+
function runWithoutAmbientTx(fn) {
|
|
37
|
+
return cds_1.default._with(undefined, fn);
|
|
38
|
+
}
|
|
39
|
+
/** Thrown inside an orphaned `cds.tx` callback after its caller already timed out. */
|
|
40
|
+
class DetachedTxAbortedError extends Error {
|
|
41
|
+
constructor(label) {
|
|
42
|
+
super(`Detached transaction '${label}' aborted: acquire succeeded only after the caller timed out`);
|
|
43
|
+
this.name = 'DetachedTxAbortedError';
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* `cds.tx(fn)` with a deadlock guard: a NEW ROOT transaction that fails fast
|
|
48
|
+
* instead of hanging forever when no pooled connection becomes available.
|
|
49
|
+
*
|
|
50
|
+
* Why: the sign-service must commit its submit bookkeeping (claim, finalize,
|
|
51
|
+
* failure marker) independently of the caller's request transaction. When an
|
|
52
|
+
* in-process consumer awaits the action while its own request transaction
|
|
53
|
+
* holds the single pooled sqlite connection, the acquire can never succeed —
|
|
54
|
+
* without this guard that is a silent, zero-CPU, infinite hang.
|
|
55
|
+
*
|
|
56
|
+
* Guarantees:
|
|
57
|
+
* - On timeout, throws `BackendError` 503 `ODATANO_NESTED_TX_TIMEOUT` with a
|
|
58
|
+
* diagnosis pointing at KNOWN_ISSUES #11.
|
|
59
|
+
* - A late connection grant (after the timeout already fired) performs NO
|
|
60
|
+
* work: the callback forces the acquire via an explicit `begin()` and
|
|
61
|
+
* checks the abort flag right after it, so it rolls back immediately and
|
|
62
|
+
* the caller never observes an error for work that then silently happened
|
|
63
|
+
* anyway. (A flag check at callback ENTRY would run long before the lazy
|
|
64
|
+
* acquire and miss the timeout.)
|
|
65
|
+
*/
|
|
66
|
+
async function detachedTx(label, fn, timeoutMs) {
|
|
67
|
+
const effectiveTimeout = timeoutMs ?? effectiveTimeoutMs();
|
|
68
|
+
let timedOut = false;
|
|
69
|
+
let timer;
|
|
70
|
+
const work = cds_1.default.tx(async (db) => {
|
|
71
|
+
// cds.tx() starts this callback immediately; the pooled connection is only
|
|
72
|
+
// acquired lazily by the first statement. Force the acquire NOW, then check
|
|
73
|
+
// the abort flag — a late grant (after the caller already received the
|
|
74
|
+
// timeout error) must roll back before doing any work.
|
|
75
|
+
await db.begin();
|
|
76
|
+
if (timedOut)
|
|
77
|
+
throw new DetachedTxAbortedError(label);
|
|
78
|
+
return fn(db);
|
|
79
|
+
});
|
|
80
|
+
const guard = new Promise((_, reject) => {
|
|
81
|
+
timer = setTimeout(() => {
|
|
82
|
+
timedOut = true;
|
|
83
|
+
reject(new errors_1.BackendError(`${label}: could not begin a detached DB transaction within ${effectiveTimeout}ms. ` +
|
|
84
|
+
`Most likely the calling request still holds the pooled DB connection open ` +
|
|
85
|
+
`(in-process nested call — do not await this action while your request ` +
|
|
86
|
+
`transaction is open; detach your reads into a committed cds.tx first, ` +
|
|
87
|
+
`see docs/KNOWN_ISSUES.md issue 11). Alternatively the connection pool is exhausted.`, 503, error_codes_1.ERROR_CODES.NESTED_TX_TIMEOUT));
|
|
88
|
+
}, effectiveTimeout);
|
|
89
|
+
});
|
|
90
|
+
try {
|
|
91
|
+
return await Promise.race([work, guard]);
|
|
92
|
+
}
|
|
93
|
+
finally {
|
|
94
|
+
if (timer)
|
|
95
|
+
clearTimeout(timer);
|
|
96
|
+
if (timedOut) {
|
|
97
|
+
// The orphaned acquire may still be granted later; its callback aborts
|
|
98
|
+
// itself via the flag. Observe the rejection so it never surfaces as an
|
|
99
|
+
// unhandled promise rejection.
|
|
100
|
+
work.catch((e) => {
|
|
101
|
+
if (!(e instanceof DetachedTxAbortedError)) {
|
|
102
|
+
logger.warn(`Orphaned detached tx '${label}' failed after timeout: ${e instanceof Error ? e.message : String(e)}`);
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
//# sourceMappingURL=tx-utils.js.map
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { type CanBeUInteger, type ITxBuildInput } from "@harmoniclabs/buildooor";
|
|
2
|
-
import { Value, type ValueUnits } from "@harmoniclabs/cardano-ledger-ts";
|
|
3
|
-
/**
|
|
4
|
-
* Coin selection ("keep relevant" strategy) — vendored fix for
|
|
5
|
-
* @harmoniclabs/buildooor 0.2.6 (HarmonicLabs/buildooor PR #12).
|
|
6
|
-
*
|
|
7
|
-
* The shipped `TxBuilder.keepRelevant` has several defects: the asset-relevance
|
|
8
|
-
* filter matches lovelace too (whenever lovelace is requested, EVERY input counts
|
|
9
|
-
* as a multi-asset match and the whole UTxO set is selected), the dedup between
|
|
10
|
-
* the asset and lovelace selection phases compares only the tx id — not id#index,
|
|
11
|
-
* so sibling outputs of one transaction collapse and inputs get selected twice —
|
|
12
|
-
* the lovelace sort goes through parseInt/number (breaks above 2^53), and
|
|
13
|
-
* `enoughValueHasBeenSelected` has an operator-precedence bug that makes asset
|
|
14
|
-
* sums meaningless.
|
|
15
|
-
*
|
|
16
|
-
* Vendored here (instead of patch-package) so the fix actually reaches consumers
|
|
17
|
-
* of the published package: `patches/` never ships in the tarball, and running
|
|
18
|
-
* patch-package from a dependency's postinstall is unreliable under npm hoisting.
|
|
19
|
-
* Drop this module and call `txBuilder.keepRelevant` again once a buildooor
|
|
20
|
-
* release contains the upstream PR.
|
|
21
|
-
*
|
|
22
|
-
* Semantics: select the inputs holding any requested non-lovelace asset, then add
|
|
23
|
-
* smallest-first lovelace-relevant inputs until the requested lovelace (plus a
|
|
24
|
-
* safety minimum for fee/change) is covered. Selection may still undershoot when
|
|
25
|
-
* the pool itself is insufficient — Buildooor's build() surfaces that as its own
|
|
26
|
-
* insufficient-funds error, exactly like the upstream implementation.
|
|
27
|
-
*/
|
|
28
|
-
export declare function keepRelevant(requestedOutputSet: Value | ValueUnits | Record<string, CanBeUInteger>, initialUTxOSet: ITxBuildInput[], minimumLovelaceRequired?: CanBeUInteger): ITxBuildInput[];
|
|
29
|
-
//# sourceMappingURL=keep-relevant.d.ts.map
|
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.keepRelevant = keepRelevant;
|
|
4
|
-
const buildooor_1 = require("@harmoniclabs/buildooor");
|
|
5
|
-
const cardano_ledger_ts_1 = require("@harmoniclabs/cardano-ledger-ts");
|
|
6
|
-
/**
|
|
7
|
-
* Coin selection ("keep relevant" strategy) — vendored fix for
|
|
8
|
-
* @harmoniclabs/buildooor 0.2.6 (HarmonicLabs/buildooor PR #12).
|
|
9
|
-
*
|
|
10
|
-
* The shipped `TxBuilder.keepRelevant` has several defects: the asset-relevance
|
|
11
|
-
* filter matches lovelace too (whenever lovelace is requested, EVERY input counts
|
|
12
|
-
* as a multi-asset match and the whole UTxO set is selected), the dedup between
|
|
13
|
-
* the asset and lovelace selection phases compares only the tx id — not id#index,
|
|
14
|
-
* so sibling outputs of one transaction collapse and inputs get selected twice —
|
|
15
|
-
* the lovelace sort goes through parseInt/number (breaks above 2^53), and
|
|
16
|
-
* `enoughValueHasBeenSelected` has an operator-precedence bug that makes asset
|
|
17
|
-
* sums meaningless.
|
|
18
|
-
*
|
|
19
|
-
* Vendored here (instead of patch-package) so the fix actually reaches consumers
|
|
20
|
-
* of the published package: `patches/` never ships in the tarball, and running
|
|
21
|
-
* patch-package from a dependency's postinstall is unreliable under npm hoisting.
|
|
22
|
-
* Drop this module and call `txBuilder.keepRelevant` again once a buildooor
|
|
23
|
-
* release contains the upstream PR.
|
|
24
|
-
*
|
|
25
|
-
* Semantics: select the inputs holding any requested non-lovelace asset, then add
|
|
26
|
-
* smallest-first lovelace-relevant inputs until the requested lovelace (plus a
|
|
27
|
-
* safety minimum for fee/change) is covered. Selection may still undershoot when
|
|
28
|
-
* the pool itself is insufficient — Buildooor's build() surfaces that as its own
|
|
29
|
-
* insufficient-funds error, exactly like the upstream implementation.
|
|
30
|
-
*/
|
|
31
|
-
function keepRelevant(requestedOutputSet, initialUTxOSet, minimumLovelaceRequired = 5_000_000) {
|
|
32
|
-
const requested = normalizeRequestedOutputSet(requestedOutputSet);
|
|
33
|
-
const requestedLovelace = (requested["lovelace"] ?? 0n) + (0, buildooor_1.forceBigUInt)(minimumLovelaceRequired);
|
|
34
|
-
const requestedAssetUnits = Object.keys(requested).filter(unit => unit !== "lovelace");
|
|
35
|
-
const multiAssetIns = initialUTxOSet.filter(input => new cardano_ledger_ts_1.UTxO(input.utxo).resolved.value.toUnits()
|
|
36
|
-
.filter(asset => asset.unit !== "lovelace")
|
|
37
|
-
.some(asset => requestedAssetUnits.includes(asset.unit)));
|
|
38
|
-
const totLovelaces = getTotLovelaces(multiAssetIns);
|
|
39
|
-
const lovelaceIns = totLovelaces < requestedLovelace
|
|
40
|
-
? remainingLovelace(requestedLovelace - totLovelaces,
|
|
41
|
-
// filter out inputs already picked by the multi-asset selection
|
|
42
|
-
initialUTxOSet.filter(initialUtxo => {
|
|
43
|
-
const refStr = utxoRefStr(initialUtxo);
|
|
44
|
-
return !multiAssetIns.some(selected => utxoRefStr(selected) === refStr);
|
|
45
|
-
}))
|
|
46
|
-
: [];
|
|
47
|
-
return lovelaceIns.concat(multiAssetIns).map(buildooor_1.normalizeITxBuildInput);
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Tolerates `Value` instances from a duplicated `cardano-ledger-ts` in
|
|
51
|
-
* node_modules, where `instanceof` fails across the two copies.
|
|
52
|
-
*/
|
|
53
|
-
function isValueLike(x) {
|
|
54
|
-
return x instanceof cardano_ledger_ts_1.Value ||
|
|
55
|
-
(!Array.isArray(x) && typeof x?.toUnits === "function");
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* `keepRelevant` historically accepted two shapes besides `Value`: the
|
|
59
|
-
* `ValueUnits` array returned by `Value.toUnits()` and a Mesh-style
|
|
60
|
-
* `{ [unit]: quantity }` record. Normalize all of them to a record with
|
|
61
|
-
* `bigint` quantities so the algorithm only deals with one shape.
|
|
62
|
-
*/
|
|
63
|
-
function normalizeRequestedOutputSet(requested) {
|
|
64
|
-
const units = isValueLike(requested)
|
|
65
|
-
? requested.toUnits()
|
|
66
|
-
: requested;
|
|
67
|
-
const result = {};
|
|
68
|
-
if (Array.isArray(units)) {
|
|
69
|
-
for (const { unit, quantity } of units) {
|
|
70
|
-
result[unit] = (result[unit] ?? 0n) + BigInt(quantity);
|
|
71
|
-
}
|
|
72
|
-
return result;
|
|
73
|
-
}
|
|
74
|
-
for (const unit of Object.keys(units)) {
|
|
75
|
-
result[unit] = BigInt(units[unit]);
|
|
76
|
-
}
|
|
77
|
-
return result;
|
|
78
|
-
}
|
|
79
|
-
/** Full utxo ref (id#index) — the id alone collapses sibling outputs of one tx. */
|
|
80
|
-
function utxoRefStr(input) {
|
|
81
|
-
const ref = input.utxo.utxoRef;
|
|
82
|
-
return `${ref.id.toString()}#${ref.index.toString()}`;
|
|
83
|
-
}
|
|
84
|
-
function getTotLovelaces(inputs) {
|
|
85
|
-
return inputs.reduce((sum, input) => sum + new cardano_ledger_ts_1.UTxO(input.utxo).resolved.value.lovelaces, 0n);
|
|
86
|
-
}
|
|
87
|
-
function remainingLovelace(quantity, initialUTxOSet) {
|
|
88
|
-
// smallest-first, compared as bigint (a number sort breaks above 2^53 lovelace)
|
|
89
|
-
const sortedUTxOs = initialUTxOSet.slice().sort((a, b) => {
|
|
90
|
-
const aLovelaces = new cardano_ledger_ts_1.UTxO(a.utxo).resolved.value.lovelaces;
|
|
91
|
-
const bLovelaces = new cardano_ledger_ts_1.UTxO(b.utxo).resolved.value.lovelaces;
|
|
92
|
-
return aLovelaces < bLovelaces ? -1 : aLovelaces > bLovelaces ? 1 : 0;
|
|
93
|
-
});
|
|
94
|
-
return selectValue(sortedUTxOs, { lovelace: quantity });
|
|
95
|
-
}
|
|
96
|
-
function enoughValueHasBeenSelected(selection, assets) {
|
|
97
|
-
return Object.keys(assets).every(unit => selection.reduce((selectedQuantity, input) => {
|
|
98
|
-
const utxoQuantity = new cardano_ledger_ts_1.UTxO(input.utxo).resolved.value.toUnits()
|
|
99
|
-
.reduce((quantity, a) => quantity + (unit === a.unit ? BigInt(a.quantity) : 0n), 0n);
|
|
100
|
-
return selectedQuantity + utxoQuantity;
|
|
101
|
-
}, 0n) >= assets[unit]);
|
|
102
|
-
}
|
|
103
|
-
function selectValue(inputUTxO, outputSet, selection = []) {
|
|
104
|
-
if (inputUTxO.length === 0 || enoughValueHasBeenSelected(selection, outputSet)) {
|
|
105
|
-
return selection;
|
|
106
|
-
}
|
|
107
|
-
if (canValueBeSelected(inputUTxO[0], outputSet)) {
|
|
108
|
-
return selectValue(inputUTxO.slice(1), outputSet, selection.concat(inputUTxO[0]));
|
|
109
|
-
}
|
|
110
|
-
return selectValue(inputUTxO.slice(1), outputSet, selection);
|
|
111
|
-
}
|
|
112
|
-
function canValueBeSelected(input, assets) {
|
|
113
|
-
return Object.keys(assets).some(unit => new cardano_ledger_ts_1.UTxO(input.utxo).resolved.value.toUnits()
|
|
114
|
-
.some(asset => asset.unit === unit));
|
|
115
|
-
}
|
|
116
|
-
//# sourceMappingURL=keep-relevant.js.map
|