@odatano/core 2.0.0-rc.41 → 2.0.0-rc.42
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/CardanoODataService/index.d.ts +9 -0
- package/@cds-models/CardanoSignService/index.d.ts +2 -0
- package/@cds-models/CardanoTransactionService/index.d.ts +2 -0
- package/@cds-models/index.d.ts +2 -0
- package/@cds-models/odatano/cardano/index.d.ts +9 -0
- package/db/schema.cds +4 -0
- package/db/types.cds +8 -0
- package/package.json +1 -1
- package/srv/blockchain/backends/blockfrost-backend.js +1 -0
- package/srv/blockchain/backends/koios-backend.js +19 -0
- package/srv/blockchain/backends/ogmios-backend.js +15 -12
- package/srv/blockchain/cardano-indexer.js +22 -8
- package/srv/blockchain/crawler/transaction-backfill.js +6 -0
- package/srv/blockchain/crawler/utxo-set-import.d.ts +20 -1
- package/srv/blockchain/crawler/utxo-set-import.js +71 -3
- package/srv/blockchain/ledger-state.d.ts +5 -1
- package/srv/blockchain/ledger-state.js +2 -0
- package/srv/blockchain/stored-chain.js +4 -0
- package/srv/blockchain/transaction-building/buildooor-fee-patch.d.ts +17 -0
- package/srv/blockchain/transaction-building/buildooor-fee-patch.js +74 -0
- package/srv/blockchain/transaction-building/buildooor-tx.d.ts +10 -1
- package/srv/blockchain/transaction-building/buildooor-tx.js +55 -19
- package/srv/cardano-indexer-service.cds +1 -1
- package/srv/cardano-indexer-service.js +23 -1
- package/srv/utils/mappers.js +7 -0
- package/srv/utils/reference-script.d.ts +37 -0
- package/srv/utils/reference-script.js +95 -0
- package/srv/utils/types.d.ts +10 -0
|
@@ -374,6 +374,8 @@ export declare function _TransactionInputAspect<TBase extends new (...args: any[
|
|
|
374
374
|
utxoData_dataHash?: _.Blake2b256 | null
|
|
375
375
|
utxoData_inlineDatum?: string | null
|
|
376
376
|
utxoData_referenceScriptHash?: _.Blake2b256 | null
|
|
377
|
+
utxoData_referenceScriptSize?: number | null
|
|
378
|
+
utxoData_referenceScriptType?: string | null
|
|
377
379
|
spentTxHash?: _.Blake2b256 | null
|
|
378
380
|
spentOutputIndex?: number | null
|
|
379
381
|
isCollateral?: boolean | null
|
|
@@ -404,6 +406,8 @@ export declare function _TransactionOutputAspect<TBase extends new (...args: any
|
|
|
404
406
|
utxo_dataHash?: _.Blake2b256 | null
|
|
405
407
|
utxo_inlineDatum?: string | null
|
|
406
408
|
utxo_referenceScriptHash?: _.Blake2b256 | null
|
|
409
|
+
utxo_referenceScriptSize?: number | null
|
|
410
|
+
utxo_referenceScriptType?: string | null
|
|
407
411
|
assets?: __.Composition.of.many<TransactionOutputAssets>
|
|
408
412
|
hasAddresses?: boolean | null
|
|
409
413
|
hasAssets?: boolean | null
|
|
@@ -551,6 +555,8 @@ export declare function _LedgerUTxOAspect<TBase extends new (...args: any[]) =>
|
|
|
551
555
|
utxo_dataHash?: _.Blake2b256 | null
|
|
552
556
|
utxo_inlineDatum?: string | null
|
|
553
557
|
utxo_referenceScriptHash?: _.Blake2b256 | null
|
|
558
|
+
utxo_referenceScriptSize?: number | null
|
|
559
|
+
utxo_referenceScriptType?: string | null
|
|
554
560
|
hasAssets?: boolean | null
|
|
555
561
|
assets?: __.Composition.of.many<LedgerUTxOAssets>
|
|
556
562
|
} & InstanceType<TBase>
|
|
@@ -758,6 +764,8 @@ export declare function _AddressUTxOAspect<TBase extends new (...args: any[]) =>
|
|
|
758
764
|
utxodata_dataHash?: _.Blake2b256 | null
|
|
759
765
|
utxodata_inlineDatum?: string | null
|
|
760
766
|
utxodata_referenceScriptHash?: _.Blake2b256 | null
|
|
767
|
+
utxodata_referenceScriptSize?: number | null
|
|
768
|
+
utxodata_referenceScriptType?: string | null
|
|
761
769
|
lovelace?: _.Lovelace | null
|
|
762
770
|
assets?: __.Composition.of.many<UTxOAssets>
|
|
763
771
|
hasAssets?: boolean | null
|
|
@@ -878,6 +886,7 @@ export declare function _LedgerProtocolParameterAspect<TBase extends new (...arg
|
|
|
878
886
|
collateralPercent?: number | null
|
|
879
887
|
maxCollateralInputs?: number | null
|
|
880
888
|
coinsPerUtxoSize?: string | null
|
|
889
|
+
minFeeRefScriptCostPerByte?: (number | string) | null
|
|
881
890
|
coinsPerUtxoWord?: string | null
|
|
882
891
|
fetchedAt?: __.CdsTimestamp | null
|
|
883
892
|
source?: string | null
|
|
@@ -361,6 +361,8 @@ export declare function _AddressUTxOAspect<TBase extends new (...args: any[]) =>
|
|
|
361
361
|
utxodata_dataHash?: _.Blake2b256 | null
|
|
362
362
|
utxodata_inlineDatum?: string | null
|
|
363
363
|
utxodata_referenceScriptHash?: _.Blake2b256 | null
|
|
364
|
+
utxodata_referenceScriptSize?: number | null
|
|
365
|
+
utxodata_referenceScriptType?: string | null
|
|
364
366
|
lovelace?: _.Lovelace | null
|
|
365
367
|
assets?: __.Composition.of.many<UTxOAssets>
|
|
366
368
|
hasAssets?: boolean | null
|
|
@@ -368,6 +368,8 @@ export declare function _AddressUTxOAspect<TBase extends new (...args: any[]) =>
|
|
|
368
368
|
utxodata_dataHash?: _.Blake2b256 | null
|
|
369
369
|
utxodata_inlineDatum?: string | null
|
|
370
370
|
utxodata_referenceScriptHash?: _.Blake2b256 | null
|
|
371
|
+
utxodata_referenceScriptSize?: number | null
|
|
372
|
+
utxodata_referenceScriptType?: string | null
|
|
371
373
|
lovelace?: _.Lovelace | null
|
|
372
374
|
assets?: __.Composition.of.many<UTxOAssets>
|
|
373
375
|
hasAssets?: boolean | null
|
package/@cds-models/index.d.ts
CHANGED
|
@@ -219,6 +219,8 @@ export declare function _UTxODataSliceAspect<TBase extends new (...args: any[])
|
|
|
219
219
|
dataHash?: Blake2b256 | null
|
|
220
220
|
inlineDatum?: string | null
|
|
221
221
|
referenceScriptHash?: Blake2b256 | null
|
|
222
|
+
referenceScriptSize?: number | null
|
|
223
|
+
referenceScriptType?: string | null
|
|
222
224
|
} & InstanceType<TBase>
|
|
223
225
|
readonly kind: 'type';
|
|
224
226
|
readonly keys: __.KeysOf<UTxODataSlice>;
|
|
@@ -345,6 +345,8 @@ export declare function _AddressUTxOAspect<TBase extends new (...args: any[]) =>
|
|
|
345
345
|
utxodata_dataHash?: _.Blake2b256 | null
|
|
346
346
|
utxodata_inlineDatum?: string | null
|
|
347
347
|
utxodata_referenceScriptHash?: _.Blake2b256 | null
|
|
348
|
+
utxodata_referenceScriptSize?: number | null
|
|
349
|
+
utxodata_referenceScriptType?: string | null
|
|
348
350
|
lovelace?: _.Lovelace | null
|
|
349
351
|
assets?: __.Composition.of.many<UTxOAssets>
|
|
350
352
|
hasAssets?: boolean | null
|
|
@@ -463,6 +465,8 @@ export declare function _TransactionInputAspect<TBase extends new (...args: any[
|
|
|
463
465
|
utxoData_dataHash?: _.Blake2b256 | null
|
|
464
466
|
utxoData_inlineDatum?: string | null
|
|
465
467
|
utxoData_referenceScriptHash?: _.Blake2b256 | null
|
|
468
|
+
utxoData_referenceScriptSize?: number | null
|
|
469
|
+
utxoData_referenceScriptType?: string | null
|
|
466
470
|
spentTxHash?: _.Blake2b256 | null
|
|
467
471
|
spentOutputIndex?: number | null
|
|
468
472
|
isCollateral?: boolean | null
|
|
@@ -522,6 +526,8 @@ export declare function _LedgerUTxOAspect<TBase extends new (...args: any[]) =>
|
|
|
522
526
|
utxo_dataHash?: _.Blake2b256 | null
|
|
523
527
|
utxo_inlineDatum?: string | null
|
|
524
528
|
utxo_referenceScriptHash?: _.Blake2b256 | null
|
|
529
|
+
utxo_referenceScriptSize?: number | null
|
|
530
|
+
utxo_referenceScriptType?: string | null
|
|
525
531
|
hasAssets?: boolean | null
|
|
526
532
|
assets?: __.Composition.of.many<LedgerUTxOAssets>
|
|
527
533
|
} & InstanceType<TBase>
|
|
@@ -709,6 +715,8 @@ export declare function _TransactionOutputAspect<TBase extends new (...args: any
|
|
|
709
715
|
utxo_dataHash?: _.Blake2b256 | null
|
|
710
716
|
utxo_inlineDatum?: string | null
|
|
711
717
|
utxo_referenceScriptHash?: _.Blake2b256 | null
|
|
718
|
+
utxo_referenceScriptSize?: number | null
|
|
719
|
+
utxo_referenceScriptType?: string | null
|
|
712
720
|
assets?: __.Composition.of.many<TransactionOutputAssets>
|
|
713
721
|
hasAddresses?: boolean | null
|
|
714
722
|
hasAssets?: boolean | null
|
|
@@ -803,6 +811,7 @@ export declare function _LedgerProtocolParameterAspect<TBase extends new (...arg
|
|
|
803
811
|
collateralPercent?: number | null
|
|
804
812
|
maxCollateralInputs?: number | null
|
|
805
813
|
coinsPerUtxoSize?: string | null
|
|
814
|
+
minFeeRefScriptCostPerByte?: (number | string) | null
|
|
806
815
|
coinsPerUtxoWord?: string | null
|
|
807
816
|
fetchedAt?: __.CdsTimestamp | null
|
|
808
817
|
source?: string | null
|
package/db/schema.cds
CHANGED
|
@@ -1367,6 +1367,10 @@ entity LedgerProtocolParameters {
|
|
|
1367
1367
|
@description: 'The coins per UTxO size parameter for calculating min UTxO value'
|
|
1368
1368
|
coinsPerUtxoSize : String(32);
|
|
1369
1369
|
|
|
1370
|
+
@title : 'Minimum Fee per Reference Script Byte'
|
|
1371
|
+
@description: 'Lovelace per byte of reference script on the inputs and reference inputs of a transaction (Conway); the ledger raises the price by 1.2 for every 25600 bytes'
|
|
1372
|
+
minFeeRefScriptCostPerByte : Decimal(12, 6);
|
|
1373
|
+
|
|
1370
1374
|
@title : 'Coins Per UTxO Word'
|
|
1371
1375
|
@description: 'The coins per UTxO word parameter for calculating min UTxO value (alonzo legacy)'
|
|
1372
1376
|
coinsPerUtxoWord : String(32);
|
package/db/types.cds
CHANGED
|
@@ -112,6 +112,14 @@ type UTxODataSlice {
|
|
|
112
112
|
@title : 'Reference Script Hash'
|
|
113
113
|
@description: 'The reference script hash associated with the UTxO'
|
|
114
114
|
referenceScriptHash : Blake2b256;
|
|
115
|
+
|
|
116
|
+
@title : 'Reference Script Size'
|
|
117
|
+
@description: 'Size of the reference script in bytes as the ledger charges it (Conway reference-script fee); null when the source reports the hash only'
|
|
118
|
+
referenceScriptSize : Integer;
|
|
119
|
+
|
|
120
|
+
@title : 'Reference Script Type'
|
|
121
|
+
@description: 'native | plutusV1 | plutusV2 | plutusV3; null when the source reports the hash only'
|
|
122
|
+
referenceScriptType : String(10);
|
|
115
123
|
}
|
|
116
124
|
|
|
117
125
|
// -----------------------------------------------------
|
package/package.json
CHANGED
|
@@ -369,6 +369,7 @@ class BlockfrostBackend {
|
|
|
369
369
|
collateralPercent: protocolParams.collateral_percent,
|
|
370
370
|
maxCollateralInputs: protocolParams.max_collateral_inputs,
|
|
371
371
|
coinsPerUtxoSize: protocolParams.coins_per_utxo_size,
|
|
372
|
+
minFeeRefScriptCostPerByte: protocolParams.min_fee_ref_script_cost_per_byte ?? null,
|
|
372
373
|
maxBlockHeaderSize: protocolParams.max_block_header_size,
|
|
373
374
|
maxTxSize: protocolParams.max_tx_size,
|
|
374
375
|
keyDeposit: protocolParams.key_deposit,
|
|
@@ -13,6 +13,7 @@ const mappers_1 = require("../../utils/mappers");
|
|
|
13
13
|
const const_1 = require("../../utils/const");
|
|
14
14
|
const tx_build_helper_1 = require("../../utils/tx-build-helper");
|
|
15
15
|
const logger = cds_1.default.log('KoiosBackend');
|
|
16
|
+
const reference_script_1 = require("../../utils/reference-script");
|
|
16
17
|
/** Bounded retries for instance-specific PostgREST server errors (42703, 57014, …). */
|
|
17
18
|
const PG_SERVER_ERROR_MAX_RETRIES = 2;
|
|
18
19
|
const PG_SERVER_ERROR_RETRY_DELAY_MS = 300;
|
|
@@ -42,6 +43,14 @@ function koiosRefScriptHash(ref) {
|
|
|
42
43
|
const obj = ref;
|
|
43
44
|
return obj.hash || null;
|
|
44
45
|
}
|
|
46
|
+
/** Size and type of a Koios `reference_script` object; both null for the bare-hash form. */
|
|
47
|
+
function koiosRefScriptMeta(ref) {
|
|
48
|
+
if (!ref || typeof ref !== 'object')
|
|
49
|
+
return { size: null, type: null };
|
|
50
|
+
const obj = ref;
|
|
51
|
+
const size = Number(obj.size);
|
|
52
|
+
return { size: Number.isInteger(size) && size > 0 ? size : null, type: (0, reference_script_1.normalizeScriptType)(obj.type) };
|
|
53
|
+
}
|
|
45
54
|
/** Sort Koios /address_txs rows newest-first — Koios gives no ordering guarantee. */
|
|
46
55
|
function sortAddressTxsDesc(rows) {
|
|
47
56
|
return [...rows].sort((a, b) => Number(b.block_height ?? b.block_time ?? 0) - Number(a.block_height ?? a.block_time ?? 0));
|
|
@@ -307,6 +316,8 @@ class KoiosBackend {
|
|
|
307
316
|
amount: amount,
|
|
308
317
|
datumHash: utxo.datum_hash || null,
|
|
309
318
|
scriptRef: koiosRefScriptBytes(utxo.reference_script),
|
|
319
|
+
scriptRefSize: koiosRefScriptMeta(utxo.reference_script).size,
|
|
320
|
+
scriptRefType: koiosRefScriptMeta(utxo.reference_script).type,
|
|
310
321
|
inlineDatum: (0, tx_build_helper_1.inlineDatumToHex)(utxo.inline_datum),
|
|
311
322
|
};
|
|
312
323
|
});
|
|
@@ -341,6 +352,8 @@ class KoiosBackend {
|
|
|
341
352
|
amount: amount,
|
|
342
353
|
datumHash: utxo.datum_hash || null,
|
|
343
354
|
scriptRef: koiosRefScriptBytes(utxo.reference_script),
|
|
355
|
+
scriptRefSize: koiosRefScriptMeta(utxo.reference_script).size,
|
|
356
|
+
scriptRefType: koiosRefScriptMeta(utxo.reference_script).type,
|
|
344
357
|
inlineDatum: (0, tx_build_helper_1.inlineDatumToHex)(utxo.inline_datum),
|
|
345
358
|
};
|
|
346
359
|
});
|
|
@@ -727,6 +740,8 @@ class KoiosBackend {
|
|
|
727
740
|
collateralPercent: data.collateralPercentage,
|
|
728
741
|
maxCollateralInputs: data.maxCollateralInputs,
|
|
729
742
|
coinsPerUtxoSize: data.utxoCostPerByte.toString(),
|
|
743
|
+
// cardano-cli spelling, as /cli_protocol_params publishes it
|
|
744
|
+
minFeeRefScriptCostPerByte: data.minFeeRefScriptCostPerByte ?? null,
|
|
730
745
|
// --- Housekeeping ---
|
|
731
746
|
fetchedAt: new Date().toISOString(),
|
|
732
747
|
source: this.name
|
|
@@ -1131,6 +1146,8 @@ function mapKoiosInput(input, flags = {}) {
|
|
|
1131
1146
|
dataHash: input.datum_hash || null,
|
|
1132
1147
|
inlineDatum: (0, tx_build_helper_1.inlineDatumToHex)(input.inline_datum),
|
|
1133
1148
|
referenceScriptHash: koiosRefScriptHash(input.reference_script),
|
|
1149
|
+
referenceScriptSize: koiosRefScriptMeta(input.reference_script).size,
|
|
1150
|
+
referenceScriptType: koiosRefScriptMeta(input.reference_script).type,
|
|
1134
1151
|
...flags,
|
|
1135
1152
|
};
|
|
1136
1153
|
}
|
|
@@ -1144,6 +1161,8 @@ function mapKoiosOutput(txHash, output, isCollateral) {
|
|
|
1144
1161
|
inlineDatum: (0, tx_build_helper_1.inlineDatumToHex)(output.inline_datum),
|
|
1145
1162
|
isCollateral,
|
|
1146
1163
|
referenceScriptHash: koiosRefScriptHash(output.reference_script),
|
|
1164
|
+
referenceScriptSize: koiosRefScriptMeta(output.reference_script).size,
|
|
1165
|
+
referenceScriptType: koiosRefScriptMeta(output.reference_script).type,
|
|
1147
1166
|
};
|
|
1148
1167
|
}
|
|
1149
1168
|
/** Koios certificate `type` → shared `CertificateKind`; anything else passes through raw. */
|
|
@@ -16,6 +16,7 @@ const node_http_1 = __importDefault(require("node:http"));
|
|
|
16
16
|
const client_1 = require("@cardano-ogmios/client");
|
|
17
17
|
const bech32_1 = require("bech32");
|
|
18
18
|
const crypto_1 = require("@harmoniclabs/crypto");
|
|
19
|
+
const reference_script_1 = require("../../utils/reference-script");
|
|
19
20
|
const backend_request_handler_1 = require("../../utils/backend-request-handler");
|
|
20
21
|
const errors_1 = require("../../utils/errors");
|
|
21
22
|
const ogmios_frame_guard_1 = require("./ogmios-frame-guard");
|
|
@@ -37,20 +38,18 @@ function parseOgmiosRatio(ratio) {
|
|
|
37
38
|
const n = Number(ratio);
|
|
38
39
|
return Number.isFinite(n) ? n : 0;
|
|
39
40
|
}
|
|
40
|
-
const SCRIPT_LANGUAGE_TAG = { native: 0, 'plutus:v1': 1, 'plutus:v2': 2, 'plutus:v3': 3 };
|
|
41
41
|
/** Hash of an Ogmios `Script`: blake2b-224 over the language tag byte followed by the script CBOR. */
|
|
42
42
|
function ogmiosScriptHash(script) {
|
|
43
|
-
|
|
44
|
-
const tag = s?.language === undefined ? undefined : SCRIPT_LANGUAGE_TAG[s.language];
|
|
45
|
-
if (tag === undefined || typeof s?.cbor !== 'string' || s.cbor.length === 0)
|
|
46
|
-
return null;
|
|
47
|
-
const bytes = Uint8Array.from(Buffer.concat([Buffer.from([tag]), Buffer.from(s.cbor, 'hex')]));
|
|
48
|
-
return Buffer.from((0, crypto_1.blake2b_224)(bytes)).toString('hex');
|
|
43
|
+
return (0, reference_script_1.ogmiosReferenceScript)(script)?.hash ?? null;
|
|
49
44
|
}
|
|
50
45
|
/** Full CBOR of a Plutus reference script as Ogmios delivers it; null for native or absent scripts. */
|
|
51
46
|
function ogmiosPlutusScriptCbor(script) {
|
|
52
|
-
|
|
53
|
-
|
|
47
|
+
return (0, reference_script_1.ogmiosReferenceScript)(script)?.cbor ?? null;
|
|
48
|
+
}
|
|
49
|
+
/** Reference-script columns of an output or UTxO from an Ogmios `script`. */
|
|
50
|
+
function ogmiosReferenceScriptFields(script) {
|
|
51
|
+
const ref = (0, reference_script_1.ogmiosReferenceScript)(script);
|
|
52
|
+
return { referenceScriptHash: ref?.hash ?? null, referenceScriptSize: ref?.size ?? null, referenceScriptType: ref?.type ?? null };
|
|
54
53
|
}
|
|
55
54
|
/** Bech32 pool id of a block issuer: blake2b-224 of its cold verification key. */
|
|
56
55
|
function issuerKeyToPoolId(verificationKeyHex) {
|
|
@@ -557,6 +556,7 @@ class OgmiosBackend {
|
|
|
557
556
|
}
|
|
558
557
|
/** Ogmios `Utxo` entry → normalized UTxO line (shared by the address query and the set import). */
|
|
559
558
|
mapOgmiosUtxo(u, fallbackAddress = '') {
|
|
559
|
+
const ref = (0, reference_script_1.ogmiosReferenceScript)(u.script);
|
|
560
560
|
return {
|
|
561
561
|
txHash: u.transaction?.id || '',
|
|
562
562
|
outputIndex: u.index || 0,
|
|
@@ -566,8 +566,10 @@ class OgmiosBackend {
|
|
|
566
566
|
datumHash: u.datumHash,
|
|
567
567
|
// Ogmios delivers the inline datum as CBOR hex in `datum`
|
|
568
568
|
inlineDatum: typeof u.datum === 'string' ? u.datum : null,
|
|
569
|
-
scriptRef:
|
|
570
|
-
scriptRefCbor:
|
|
569
|
+
scriptRef: ref?.hash,
|
|
570
|
+
scriptRefCbor: ref?.cbor ?? null,
|
|
571
|
+
scriptRefSize: ref?.size ?? null,
|
|
572
|
+
scriptRefType: ref?.type ?? null,
|
|
571
573
|
};
|
|
572
574
|
}
|
|
573
575
|
/**
|
|
@@ -794,6 +796,7 @@ class OgmiosBackend {
|
|
|
794
796
|
collateralPercent: params.collateralPercentage || 0,
|
|
795
797
|
maxCollateralInputs: params.maxCollateralInputs || 0,
|
|
796
798
|
coinsPerUtxoSize: params.minUtxoDepositCoefficient?.toString() || '0',
|
|
799
|
+
minFeeRefScriptCostPerByte: params.minFeeReferenceScripts?.base ?? null,
|
|
797
800
|
maxBlockHeaderSize: params.maxBlockHeaderSize?.bytes || 0,
|
|
798
801
|
maxTxSize: params.maxTransactionSize?.bytes || 0,
|
|
799
802
|
keyDeposit: params.stakeCredentialDeposit?.ada?.lovelace?.toString() || '0',
|
|
@@ -1173,7 +1176,7 @@ class OgmiosBackend {
|
|
|
1173
1176
|
dataHash: output.datumHash ?? null,
|
|
1174
1177
|
inlineDatum: output.datum ?? null,
|
|
1175
1178
|
isCollateral,
|
|
1176
|
-
|
|
1179
|
+
...ogmiosReferenceScriptFields(output.script),
|
|
1177
1180
|
});
|
|
1178
1181
|
const outputs = spendsCollaterals
|
|
1179
1182
|
? (tx.collateralReturn
|
|
@@ -21,11 +21,23 @@ const epoch_slots_1 = require("../utils/epoch-slots");
|
|
|
21
21
|
const const_1 = require("../utils/const");
|
|
22
22
|
const { UPSERT, INSERT, UPDATE, SELECT, DELETE } = cds_1.default.ql;
|
|
23
23
|
const logger = cds_1.default.log('CardanoIndexer');
|
|
24
|
-
/** Datum hash, inline datum and reference
|
|
25
|
-
function copyUtxoData(input,
|
|
26
|
-
input.dataHash = input.dataHash ??
|
|
27
|
-
input.inlineDatum = input.inlineDatum ??
|
|
28
|
-
input.referenceScriptHash = input.referenceScriptHash ??
|
|
24
|
+
/** Datum hash, inline datum and reference script of the spent output onto its input. */
|
|
25
|
+
function copyUtxoData(input, out) {
|
|
26
|
+
input.dataHash = input.dataHash ?? out.utxo_dataHash ?? null;
|
|
27
|
+
input.inlineDatum = input.inlineDatum ?? out.utxo_inlineDatum ?? null;
|
|
28
|
+
input.referenceScriptHash = input.referenceScriptHash ?? out.utxo_referenceScriptHash ?? null;
|
|
29
|
+
input.referenceScriptSize = input.referenceScriptSize ?? out.utxo_referenceScriptSize ?? null;
|
|
30
|
+
input.referenceScriptType = input.referenceScriptType ?? out.utxo_referenceScriptType ?? null;
|
|
31
|
+
}
|
|
32
|
+
/** A provider output in the stored-column shape, for copyUtxoData. */
|
|
33
|
+
function utxoDataOf(o) {
|
|
34
|
+
return {
|
|
35
|
+
utxo_dataHash: o.dataHash,
|
|
36
|
+
utxo_inlineDatum: o.inlineDatum,
|
|
37
|
+
utxo_referenceScriptHash: o.referenceScriptHash,
|
|
38
|
+
utxo_referenceScriptSize: o.referenceScriptSize,
|
|
39
|
+
utxo_referenceScriptType: o.referenceScriptType,
|
|
40
|
+
};
|
|
29
41
|
}
|
|
30
42
|
function lovelaceSum(value) {
|
|
31
43
|
if (value === null || value === undefined || value === '')
|
|
@@ -752,6 +764,8 @@ class CardanoIndexer {
|
|
|
752
764
|
datumHash: u.utxo_dataHash ?? undefined,
|
|
753
765
|
inlineDatum: u.utxo_inlineDatum ?? null,
|
|
754
766
|
scriptRef: u.utxo_referenceScriptHash ?? undefined,
|
|
767
|
+
scriptRefSize: u.utxo_referenceScriptSize == null ? null : Number(u.utxo_referenceScriptSize),
|
|
768
|
+
scriptRefType: u.utxo_referenceScriptType ?? null,
|
|
755
769
|
});
|
|
756
770
|
}
|
|
757
771
|
}
|
|
@@ -1138,7 +1152,7 @@ class CardanoIndexer {
|
|
|
1138
1152
|
if (local) {
|
|
1139
1153
|
input.address = local.address;
|
|
1140
1154
|
input.amount = local.amount ?? [];
|
|
1141
|
-
copyUtxoData(input, local
|
|
1155
|
+
copyUtxoData(input, utxoDataOf(local));
|
|
1142
1156
|
}
|
|
1143
1157
|
else {
|
|
1144
1158
|
unresolved.push({ input, key });
|
|
@@ -1157,7 +1171,7 @@ class CardanoIndexer {
|
|
|
1157
1171
|
continue;
|
|
1158
1172
|
unresolved[i].input.address = row.address;
|
|
1159
1173
|
unresolved[i].input.amount = (0, ledger_state_1.ledgerRowAmount)(row, ledger.assets.get(unresolved[i].key));
|
|
1160
|
-
copyUtxoData(unresolved[i].input, row
|
|
1174
|
+
copyUtxoData(unresolved[i].input, row);
|
|
1161
1175
|
unresolved.splice(i, 1);
|
|
1162
1176
|
}
|
|
1163
1177
|
if (!unresolved.length)
|
|
@@ -1193,7 +1207,7 @@ class CardanoIndexer {
|
|
|
1193
1207
|
if (out?.address_address != null)
|
|
1194
1208
|
input.address = out.address_address;
|
|
1195
1209
|
if (out)
|
|
1196
|
-
copyUtxoData(input, out
|
|
1210
|
+
copyUtxoData(input, out);
|
|
1197
1211
|
const amt = amtByKey.get(key);
|
|
1198
1212
|
if (amt)
|
|
1199
1213
|
input.amount = amt;
|
|
@@ -83,6 +83,10 @@ function missingFields(row, fresh) {
|
|
|
83
83
|
set.utxoData_inlineDatum = fresh.inlineDatum;
|
|
84
84
|
if (row.utxoData_referenceScriptHash == null && fresh.referenceScriptHash)
|
|
85
85
|
set.utxoData_referenceScriptHash = fresh.referenceScriptHash;
|
|
86
|
+
if (row.utxoData_referenceScriptSize == null && fresh.referenceScriptSize != null)
|
|
87
|
+
set.utxoData_referenceScriptSize = fresh.referenceScriptSize;
|
|
88
|
+
if (row.utxoData_referenceScriptType == null && fresh.referenceScriptType)
|
|
89
|
+
set.utxoData_referenceScriptType = fresh.referenceScriptType;
|
|
86
90
|
if (!row.address_address && fresh.address) {
|
|
87
91
|
set.address_address = fresh.address;
|
|
88
92
|
set.hasAddresses = true;
|
|
@@ -100,6 +104,8 @@ const FILL_COLUMNS = [
|
|
|
100
104
|
['utxoData_dataHash', 'text'],
|
|
101
105
|
['utxoData_inlineDatum', 'text'],
|
|
102
106
|
['utxoData_referenceScriptHash', 'text'],
|
|
107
|
+
['utxoData_referenceScriptSize', 'integer'],
|
|
108
|
+
['utxoData_referenceScriptType', 'text'],
|
|
103
109
|
['address_address', 'text'],
|
|
104
110
|
['hasAddresses', 'boolean'],
|
|
105
111
|
['hasAssets', 'boolean'],
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { CardanoClient } from '../cardano-client';
|
|
2
2
|
import type { CardanoIndexer } from '../cardano-indexer';
|
|
3
|
-
import type { Amount } from '../../utils/types';
|
|
3
|
+
import type { Amount, ReferenceScriptType } from '../../utils/types';
|
|
4
4
|
import { type LedgerAnchor } from '../ledger-state';
|
|
5
5
|
/**
|
|
6
6
|
* One-off import of the UTxO set at the anchor `crawler.utxoSet` builds on: from Ogmios
|
|
@@ -22,6 +22,8 @@ export interface UtxoSetEntry {
|
|
|
22
22
|
dataHash?: string | null;
|
|
23
23
|
inlineDatum?: string | null;
|
|
24
24
|
referenceScriptHash?: string | null;
|
|
25
|
+
referenceScriptSize?: number | null;
|
|
26
|
+
referenceScriptType?: ReferenceScriptType | null;
|
|
25
27
|
}
|
|
26
28
|
export interface UtxoSetImportOptions {
|
|
27
29
|
source: 'ogmios' | 'file';
|
|
@@ -75,4 +77,21 @@ export declare function importUtxoSet(opts: UtxoSetImportOptions): Promise<UtxoS
|
|
|
75
77
|
export declare function rebuildUtxoSetAggregates(opts: {
|
|
76
78
|
indexer: CardanoIndexer;
|
|
77
79
|
}): Promise<UtxoSetImportResult>;
|
|
80
|
+
export interface UtxoSetScriptRefreshResult {
|
|
81
|
+
/** Rows whose reference-script columns were written. */
|
|
82
|
+
updated: number;
|
|
83
|
+
/** Rows looked at: dump entries carrying a script, or stored rows with a hash but no size. */
|
|
84
|
+
candidates: number;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Fill the reference-script columns (hash, size, type) of `LedgerUTxOs` rows an earlier import
|
|
88
|
+
* left empty: from the same cardano-cli dump, or, without a file, from the node for the unspent
|
|
89
|
+
* rows that hold a hash but no size. Takes the cursor lease like an import, so the crawler must
|
|
90
|
+
* be paused; the set stays `active` throughout.
|
|
91
|
+
*/
|
|
92
|
+
export declare function refreshUtxoSetReferenceScripts(opts: {
|
|
93
|
+
filePath?: string;
|
|
94
|
+
client: CardanoClient;
|
|
95
|
+
batchSize?: number;
|
|
96
|
+
}): Promise<UtxoSetScriptRefreshResult>;
|
|
78
97
|
//# sourceMappingURL=utxo-set-import.d.ts.map
|
|
@@ -10,15 +10,17 @@ exports.readUtxoSetFile = readUtxoSetFile;
|
|
|
10
10
|
exports.aggregateStatements = aggregateStatements;
|
|
11
11
|
exports.importUtxoSet = importUtxoSet;
|
|
12
12
|
exports.rebuildUtxoSetAggregates = rebuildUtxoSetAggregates;
|
|
13
|
+
exports.refreshUtxoSetReferenceScripts = refreshUtxoSetReferenceScripts;
|
|
13
14
|
const cds_1 = __importDefault(require("@sap/cds"));
|
|
14
15
|
const node_fs_1 = require("node:fs");
|
|
15
16
|
const promises_1 = require("node:fs/promises");
|
|
16
17
|
const node_readline_1 = require("node:readline");
|
|
18
|
+
const reference_script_1 = require("../../utils/reference-script");
|
|
17
19
|
const ledger_state_1 = require("../ledger-state");
|
|
18
20
|
const sync_state_1 = require("./sync-state");
|
|
19
21
|
const cardano_1 = require("#cds-models/odatano/cardano");
|
|
20
22
|
const collections_1 = require("../../utils/collections");
|
|
21
|
-
const { UPSERT, DELETE, SELECT } = cds_1.default.ql;
|
|
23
|
+
const { UPSERT, DELETE, SELECT, UPDATE } = cds_1.default.ql;
|
|
22
24
|
const logger = cds_1.default.log('LedgerState');
|
|
23
25
|
/**
|
|
24
26
|
* One-off import of the UTxO set at the anchor `crawler.utxoSet` builds on: from Ogmios
|
|
@@ -68,6 +70,7 @@ function parseCliUtxoEntry(key, value) {
|
|
|
68
70
|
}
|
|
69
71
|
if (!amount.some(a => a.unit === 'lovelace'))
|
|
70
72
|
amount.unshift({ unit: 'lovelace', quantity: '0' });
|
|
73
|
+
const ref = (0, reference_script_1.cliReferenceScript)(value.referenceScript);
|
|
71
74
|
return {
|
|
72
75
|
txHash: m[1].toLowerCase(),
|
|
73
76
|
outputIndex: Number(m[2]),
|
|
@@ -75,8 +78,9 @@ function parseCliUtxoEntry(key, value) {
|
|
|
75
78
|
amount,
|
|
76
79
|
dataHash: value.datumhash ?? value.datumHash ?? null,
|
|
77
80
|
inlineDatum: typeof value.inlineDatumRaw === 'string' ? value.inlineDatumRaw : null,
|
|
78
|
-
|
|
79
|
-
|
|
81
|
+
referenceScriptHash: ref?.hash ?? null,
|
|
82
|
+
referenceScriptSize: ref?.size ?? null,
|
|
83
|
+
referenceScriptType: ref?.type ?? null,
|
|
80
84
|
};
|
|
81
85
|
}
|
|
82
86
|
/** Stream a cardano-cli dump. See the module doc for the accepted layouts. */
|
|
@@ -124,6 +128,8 @@ async function* readFromOgmios(client, anchor) {
|
|
|
124
128
|
dataHash: u.datumHash ?? null,
|
|
125
129
|
inlineDatum: u.inlineDatum ?? null,
|
|
126
130
|
referenceScriptHash: u.scriptRef ?? null,
|
|
131
|
+
referenceScriptSize: u.scriptRefSize ?? null,
|
|
132
|
+
referenceScriptType: u.scriptRefType ?? null,
|
|
127
133
|
};
|
|
128
134
|
}
|
|
129
135
|
}
|
|
@@ -360,4 +366,66 @@ async function rebuildUtxoSetAggregates(opts) {
|
|
|
360
366
|
return { utxos, anchor };
|
|
361
367
|
});
|
|
362
368
|
}
|
|
369
|
+
/**
|
|
370
|
+
* Fill the reference-script columns (hash, size, type) of `LedgerUTxOs` rows an earlier import
|
|
371
|
+
* left empty: from the same cardano-cli dump, or, without a file, from the node for the unspent
|
|
372
|
+
* rows that hold a hash but no size. Takes the cursor lease like an import, so the crawler must
|
|
373
|
+
* be paused; the set stays `active` throughout.
|
|
374
|
+
*/
|
|
375
|
+
async function refreshUtxoSetReferenceScripts(opts) {
|
|
376
|
+
const cursor = await cds_1.default.tx((tx) => (0, sync_state_1.readCursor)(tx));
|
|
377
|
+
if (!cursor)
|
|
378
|
+
throw new UtxoSetImportError('No crawler cursor yet.');
|
|
379
|
+
if ((0, sync_state_1.isCrawlerLeaseActive)(cursor))
|
|
380
|
+
throw new UtxoSetImportError('Crawler is running — pause it first (pauseCrawler).');
|
|
381
|
+
if (cursor.utxoSet.anchorSlot == null)
|
|
382
|
+
throw new UtxoSetImportError('No imported UTxO set — run importUtxoSet first.');
|
|
383
|
+
const batchSize = opts.batchSize ?? 2000;
|
|
384
|
+
return withImportLease(async (lease) => {
|
|
385
|
+
let candidates = 0;
|
|
386
|
+
let updated = 0;
|
|
387
|
+
const write = async (fills) => {
|
|
388
|
+
if (!fills.length)
|
|
389
|
+
return;
|
|
390
|
+
await lease.writeTx(async (tx) => {
|
|
391
|
+
for (const f of fills) {
|
|
392
|
+
const n = await tx.run(UPDATE.entity(cardano_1.LedgerUTxOs)
|
|
393
|
+
.set({ utxo_referenceScriptHash: f.hash, utxo_referenceScriptSize: f.size, utxo_referenceScriptType: f.type })
|
|
394
|
+
.where({ txHash: f.txHash, outputIndex: f.outputIndex }));
|
|
395
|
+
updated += Number(n) || 0;
|
|
396
|
+
}
|
|
397
|
+
});
|
|
398
|
+
};
|
|
399
|
+
if (opts.filePath) {
|
|
400
|
+
let fills = [];
|
|
401
|
+
for await (const e of readUtxoSetFile(opts.filePath)) {
|
|
402
|
+
if (!e.referenceScriptHash || e.referenceScriptSize == null || !e.referenceScriptType)
|
|
403
|
+
continue;
|
|
404
|
+
candidates++;
|
|
405
|
+
fills.push({ txHash: e.txHash, outputIndex: e.outputIndex, hash: e.referenceScriptHash, size: e.referenceScriptSize, type: e.referenceScriptType });
|
|
406
|
+
if (fills.length >= batchSize) {
|
|
407
|
+
await write(fills);
|
|
408
|
+
fills = [];
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
await write(fills);
|
|
412
|
+
}
|
|
413
|
+
else {
|
|
414
|
+
const backend = opts.client.getLedgerStateBackend();
|
|
415
|
+
if (!backend)
|
|
416
|
+
throw new UtxoSetImportError('No Ogmios backend available — pass the cardano-cli dump as filePath instead.');
|
|
417
|
+
const rows = await cds_1.default.tx((tx) => tx.run(SELECT.from(cardano_1.LedgerUTxOs).columns('txHash', 'outputIndex')
|
|
418
|
+
.where({ spentTxHash: null, utxo_referenceScriptHash: { '!=': null }, utxo_referenceScriptSize: null })));
|
|
419
|
+
candidates = rows?.length ?? 0;
|
|
420
|
+
for (const refs of (0, collections_1.chunk)(rows ?? [], collections_1.IN_CHUNK)) {
|
|
421
|
+
const live = await backend.getUnspentOutputs(refs.map(r => ({ txHash: r.txHash, outputIndex: Number(r.outputIndex) })));
|
|
422
|
+
await write(live.flatMap(u => (u.scriptRef && u.scriptRefSize != null && u.scriptRefType
|
|
423
|
+
? [{ txHash: u.txHash, outputIndex: u.outputIndex, hash: u.scriptRef, size: u.scriptRefSize, type: u.scriptRefType }]
|
|
424
|
+
: [])));
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
logger.info(`UTxO set reference scripts refreshed: ${updated} of ${candidates} rows (${opts.filePath ? 'dump' : 'node'})`);
|
|
428
|
+
return { updated, candidates };
|
|
429
|
+
});
|
|
430
|
+
}
|
|
363
431
|
//# sourceMappingURL=utxo-set-import.js.map
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Transaction as CapTransaction } from '@sap/cds';
|
|
2
|
-
import type { Amount, BlockData, Transaction as ProviderTransaction } from '../utils/types';
|
|
2
|
+
import type { Amount, BlockData, Transaction as ProviderTransaction, ReferenceScriptType } from '../utils/types';
|
|
3
3
|
/**
|
|
4
4
|
* Crawler-fed ledger state (`crawler.utxoSet`): UTxO set + per-address/per-stake-key running
|
|
5
5
|
* sums in non-temporal tables. Only blocks AFTER the imported anchor are applied; a reorg past
|
|
@@ -32,6 +32,8 @@ export interface LedgerUtxoRow {
|
|
|
32
32
|
utxo_dataHash: string | null;
|
|
33
33
|
utxo_inlineDatum: string | null;
|
|
34
34
|
utxo_referenceScriptHash: string | null;
|
|
35
|
+
utxo_referenceScriptSize: number | null;
|
|
36
|
+
utxo_referenceScriptType: ReferenceScriptType | null;
|
|
35
37
|
hasAssets: boolean;
|
|
36
38
|
}
|
|
37
39
|
export interface LedgerUtxoAssetRow {
|
|
@@ -52,6 +54,8 @@ export interface LedgerOutputLike {
|
|
|
52
54
|
dataHash?: string | null;
|
|
53
55
|
inlineDatum?: string | null;
|
|
54
56
|
referenceScriptHash?: string | null;
|
|
57
|
+
referenceScriptSize?: number | null;
|
|
58
|
+
referenceScriptType?: ReferenceScriptType | null;
|
|
55
59
|
}
|
|
56
60
|
/** Build the `LedgerUTxOs` row (+ asset rows) for one output. */
|
|
57
61
|
export declare function buildLedgerUtxoRows(o: LedgerOutputLike, createdSlot: number | null): {
|
|
@@ -61,6 +61,8 @@ function buildLedgerUtxoRows(o, createdSlot) {
|
|
|
61
61
|
utxo_dataHash: o.dataHash || null,
|
|
62
62
|
utxo_inlineDatum: o.inlineDatum || null,
|
|
63
63
|
utxo_referenceScriptHash: o.referenceScriptHash || null,
|
|
64
|
+
utxo_referenceScriptSize: o.referenceScriptSize ?? null,
|
|
65
|
+
utxo_referenceScriptType: o.referenceScriptType ?? null,
|
|
64
66
|
hasAssets: assets.length > 0,
|
|
65
67
|
},
|
|
66
68
|
assets,
|
|
@@ -167,6 +167,8 @@ async function assembleTransactions(db, rows) {
|
|
|
167
167
|
dataHash: i.utxoData_dataHash ?? null,
|
|
168
168
|
inlineDatum: i.utxoData_inlineDatum ?? null,
|
|
169
169
|
referenceScriptHash: i.utxoData_referenceScriptHash ?? null,
|
|
170
|
+
referenceScriptSize: numOrNull(i.utxoData_referenceScriptSize),
|
|
171
|
+
referenceScriptType: i.utxoData_referenceScriptType ?? null,
|
|
170
172
|
isCollateral: Boolean(i.isCollateral),
|
|
171
173
|
isReference: Boolean(i.isReference),
|
|
172
174
|
}));
|
|
@@ -183,6 +185,8 @@ async function assembleTransactions(db, rows) {
|
|
|
183
185
|
inlineDatum: o.utxo_inlineDatum ?? null,
|
|
184
186
|
isCollateral: spendsCollaterals,
|
|
185
187
|
referenceScriptHash: o.utxo_referenceScriptHash ?? null,
|
|
188
|
+
referenceScriptSize: numOrNull(o.utxo_referenceScriptSize),
|
|
189
|
+
referenceScriptType: o.utxo_referenceScriptType ?? null,
|
|
186
190
|
}));
|
|
187
191
|
const certs = certsByHash.get(hash);
|
|
188
192
|
const withdrawn = withdrawalsByHash.get(hash);
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Tx } from '@harmoniclabs/cardano-ledger-ts';
|
|
2
|
+
/** Conway reference-script fee: the price per byte rises by 1.2 for every tier of this many bytes. */
|
|
3
|
+
export declare const REF_SCRIPT_TIER_BYTES = 25600n;
|
|
4
|
+
export interface Rational {
|
|
5
|
+
num: bigint;
|
|
6
|
+
den: bigint;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Reference-script fee as the ledger computes it: `totalBytes` priced tier by tier, each tier
|
|
10
|
+
* 1.2 times the previous, exact rationals, floored once at the end.
|
|
11
|
+
*/
|
|
12
|
+
export declare function tierRefScriptFee(totalBytes: bigint, pricePerByte: Rational): bigint;
|
|
13
|
+
/** Reference-script bytes of the inputs and reference inputs, not deduplicated, as the ledger counts them. */
|
|
14
|
+
export declare function refScriptBytesOf(tx: Tx): bigint;
|
|
15
|
+
/** Replace `TxBuilder.prototype.calcMinFee` once per process. */
|
|
16
|
+
export declare function installReferenceScriptFeePatch(): void;
|
|
17
|
+
//# sourceMappingURL=buildooor-fee-patch.d.ts.map
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.REF_SCRIPT_TIER_BYTES = void 0;
|
|
4
|
+
exports.tierRefScriptFee = tierRefScriptFee;
|
|
5
|
+
exports.refScriptBytesOf = refScriptBytesOf;
|
|
6
|
+
exports.installReferenceScriptFeePatch = installReferenceScriptFeePatch;
|
|
7
|
+
const buildooor_1 = require("@harmoniclabs/buildooor");
|
|
8
|
+
const utils_1 = require("@harmoniclabs/buildooor/dist/TxBuilder/utils");
|
|
9
|
+
const cardano_ledger_ts_1 = require("@harmoniclabs/cardano-ledger-ts");
|
|
10
|
+
const reference_script_1 = require("../../utils/reference-script");
|
|
11
|
+
/** Conway reference-script fee: the price per byte rises by 1.2 for every tier of this many bytes. */
|
|
12
|
+
exports.REF_SCRIPT_TIER_BYTES = 25600n;
|
|
13
|
+
/**
|
|
14
|
+
* Reference-script fee as the ledger computes it: `totalBytes` priced tier by tier, each tier
|
|
15
|
+
* 1.2 times the previous, exact rationals, floored once at the end.
|
|
16
|
+
*/
|
|
17
|
+
function tierRefScriptFee(totalBytes, pricePerByte) {
|
|
18
|
+
let remaining = totalBytes;
|
|
19
|
+
let priceNum = pricePerByte.num;
|
|
20
|
+
let priceDen = pricePerByte.den;
|
|
21
|
+
let accNum = 0n;
|
|
22
|
+
let accDen = 1n;
|
|
23
|
+
const add = (bytes) => {
|
|
24
|
+
accNum = accNum * priceDen + bytes * priceNum * accDen;
|
|
25
|
+
accDen *= priceDen;
|
|
26
|
+
};
|
|
27
|
+
while (remaining >= exports.REF_SCRIPT_TIER_BYTES) {
|
|
28
|
+
add(exports.REF_SCRIPT_TIER_BYTES);
|
|
29
|
+
priceNum *= 6n;
|
|
30
|
+
priceDen *= 5n;
|
|
31
|
+
remaining -= exports.REF_SCRIPT_TIER_BYTES;
|
|
32
|
+
}
|
|
33
|
+
add(remaining);
|
|
34
|
+
return accNum / accDen;
|
|
35
|
+
}
|
|
36
|
+
/** Reference-script bytes of the inputs and reference inputs, not deduplicated, as the ledger counts them. */
|
|
37
|
+
function refScriptBytesOf(tx) {
|
|
38
|
+
const utxos = [...tx.body.inputs, ...(tx.body.refInputs ?? [])];
|
|
39
|
+
return utxos.reduce((sum, u) => sum + BigInt(u.resolved.refScript ? (0, reference_script_1.referenceScriptSize)(u.resolved.refScript) : 0), 0n);
|
|
40
|
+
}
|
|
41
|
+
function refScriptPrice(pp) {
|
|
42
|
+
const r = (0, cardano_ledger_ts_1.cborFromRational)(pp.minfeeRefScriptCostPerByte ?? cardano_ledger_ts_1.defaultProtocolParameters.minfeeRefScriptCostPerByte);
|
|
43
|
+
return { num: BigInt(r.num), den: BigInt(r.den) };
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* `TxBuilder.calcMinFee` with the ledger's reference-script rule. Buildooor 0.2.9 prices only the
|
|
47
|
+
* reference inputs, linearly; the ledger also counts scripts on spent inputs and prices in tiers.
|
|
48
|
+
* The rest (linear fee, estimated key witnesses) is Buildooor's own arithmetic.
|
|
49
|
+
*/
|
|
50
|
+
function calcMinFeeWithRefScripts(tx, minimum) {
|
|
51
|
+
const self = this;
|
|
52
|
+
const pp = self.protocolParamters;
|
|
53
|
+
const perByte = BigInt(pp.txFeePerByte ?? cardano_ledger_ts_1.defaultProtocolParameters.txFeePerByte);
|
|
54
|
+
const nVkeyWits = BigInt((0, utils_1.estimateMaxSignersNeeded)(tx));
|
|
55
|
+
const minFee = self.calcLinearFee(tx)
|
|
56
|
+
+ tierRefScriptFee(refScriptBytesOf(tx), refScriptPrice(pp))
|
|
57
|
+
// one key witness is 102 CBOR bytes plus 2 for encoding slack, plus the array header
|
|
58
|
+
+ 104n * nVkeyWits * perByte
|
|
59
|
+
+ BigInt(nVkeyWits < 24n ? 1 : nVkeyWits < 256n ? 2 : 4) * perByte;
|
|
60
|
+
if (minimum === undefined || minimum === null)
|
|
61
|
+
return minFee;
|
|
62
|
+
const min = BigInt(minimum);
|
|
63
|
+
return minFee < min ? min : minFee;
|
|
64
|
+
}
|
|
65
|
+
const PATCHED = Symbol.for('odatano.buildooor.refScriptFee');
|
|
66
|
+
/** Replace `TxBuilder.prototype.calcMinFee` once per process. */
|
|
67
|
+
function installReferenceScriptFeePatch() {
|
|
68
|
+
const proto = buildooor_1.TxBuilder.prototype;
|
|
69
|
+
if (proto[PATCHED])
|
|
70
|
+
return;
|
|
71
|
+
proto.calcMinFee = calcMinFeeWithRefScripts;
|
|
72
|
+
proto[PATCHED] = true;
|
|
73
|
+
}
|
|
74
|
+
//# sourceMappingURL=buildooor-fee-patch.js.map
|
|
@@ -34,12 +34,15 @@ export declare class BuildooorTxBuilder implements CardanoTxBuilder {
|
|
|
34
34
|
*/
|
|
35
35
|
private _buildSimpleTransfer;
|
|
36
36
|
buildUnsignedMintTransaction(req: TxBuildMintRequest, ctx: TxBuildContext): Promise<TxBuildResult>;
|
|
37
|
+
private _buildMintTransaction;
|
|
37
38
|
buildUnsignedPlutusSpendTransaction(req: TxBuildPlutusSpendRequest, ctx: TxBuildContext): Promise<TxBuildResult>;
|
|
39
|
+
private _buildPlutusSpendTransaction;
|
|
38
40
|
/**
|
|
39
41
|
* Several script inputs in one transaction, each with its own redeemer (inline validator or a
|
|
40
42
|
* reference-script UTxO), the outputs exactly in the given order and change after them.
|
|
41
43
|
*/
|
|
42
44
|
buildUnsignedPlutusTransaction(req: TxBuildPlutusRequest, ctx: TxBuildContext): Promise<TxBuildResult>;
|
|
45
|
+
private _buildPlutusTransaction;
|
|
43
46
|
/**
|
|
44
47
|
* Outputs of a BuildPlutusTransaction in the given order: inline datum or datum hash, ref script, min-ADA each.
|
|
45
48
|
* Returns the inline datums given as CBOR, keyed by output index.
|
|
@@ -109,6 +112,11 @@ export declare class BuildooorTxBuilder implements CardanoTxBuilder {
|
|
|
109
112
|
* so raw chain arrays (passed through unclamped) are substituted where available.
|
|
110
113
|
*/
|
|
111
114
|
private _languageViewCostModels;
|
|
115
|
+
/**
|
|
116
|
+
* Run a build without the sender's reference-script UTxOs first; when that leaves the funds short
|
|
117
|
+
* and such UTxOs exist, build again with them in the pool (their script bytes then count in the fee).
|
|
118
|
+
*/
|
|
119
|
+
private _withRefScriptFallback;
|
|
112
120
|
/**
|
|
113
121
|
* Split UTxOs into forced inputs (must be consumed) and remaining candidates; forceInputs
|
|
114
122
|
* refs absent from utxos are ignored (existence is validated upstream).
|
|
@@ -179,7 +187,8 @@ export declare class BuildooorTxBuilder implements CardanoTxBuilder {
|
|
|
179
187
|
private _parseReferenceScript;
|
|
180
188
|
/**
|
|
181
189
|
* Map an input UTxO's reference script to a Script: `scriptRefCbor` (Ogmios), else a full-CBOR
|
|
182
|
-
* `scriptRef` (Koios)
|
|
190
|
+
* `scriptRef` (Koios). With the hash and size only (Blockfrost, the tables) a placeholder of
|
|
191
|
+
* that size stands in, so the fee counts its bytes; without the size nothing can be counted.
|
|
183
192
|
*/
|
|
184
193
|
private _buildInputRefScript;
|
|
185
194
|
/**
|
|
@@ -18,6 +18,8 @@ const TxMetadatum_1 = require("@harmoniclabs/cardano-ledger-ts/dist/tx/metadata/
|
|
|
18
18
|
const plutus_data_1 = require("@harmoniclabs/plutus-data");
|
|
19
19
|
const caller_datums_1 = require("./caller-datums");
|
|
20
20
|
const const_1 = require("../../utils/const");
|
|
21
|
+
const buildooor_fee_patch_1 = require("./buildooor-fee-patch");
|
|
22
|
+
const reference_script_1 = require("../../utils/reference-script");
|
|
21
23
|
const logger = cds_1.default.log('BuildooorTxBuilder');
|
|
22
24
|
/**
|
|
23
25
|
* Build options for script-bearing transactions. Buildooor stamps each redeemer with its
|
|
@@ -115,11 +117,13 @@ function chunkUtf8(str, maxBytes) {
|
|
|
115
117
|
function ledgerScript(script) {
|
|
116
118
|
return new cardano_ledger_ts_1.Script(script.type, script.bytes);
|
|
117
119
|
}
|
|
120
|
+
const hasRefScript = (u) => Boolean(u.scriptRef || u.scriptRefCbor);
|
|
118
121
|
// Hash32 inherits Hash.clone(), which returns a plain Hash; TxOut rejects that as datum when
|
|
119
122
|
// Buildooor clones outputs, so a datum-hash output could never be built. Hash28 has its own clone.
|
|
120
123
|
if (!Object.prototype.hasOwnProperty.call(cardano_ledger_ts_1.Hash32.prototype, 'clone')) {
|
|
121
124
|
cardano_ledger_ts_1.Hash32.prototype.clone = function () { return new cardano_ledger_ts_1.Hash32(this.toString()); };
|
|
122
125
|
}
|
|
126
|
+
(0, buildooor_fee_patch_1.installReferenceScriptFeePatch)();
|
|
123
127
|
/** CardanoTxBuilder implementation on top of Buildooor. */
|
|
124
128
|
class BuildooorTxBuilder {
|
|
125
129
|
name = 'BuildooorTxBuilder';
|
|
@@ -167,17 +171,17 @@ class BuildooorTxBuilder {
|
|
|
167
171
|
}
|
|
168
172
|
/** Build an unsigned transfer transaction (ADA-only or with native assets). */
|
|
169
173
|
buildUnsignedTransfer(req, ctx) {
|
|
170
|
-
return this._buildSimpleTransfer(req, ctx, 'transfer');
|
|
174
|
+
return this._withRefScriptFallback(ctx.utxos, (include) => this._buildSimpleTransfer(req, ctx, 'transfer', include));
|
|
171
175
|
}
|
|
172
176
|
/** Build an unsigned transfer transaction with attached metadata. */
|
|
173
177
|
buildUnsignedTransactionWithMetadata(req, ctx) {
|
|
174
|
-
return this._buildSimpleTransfer(req, ctx, 'metadata transfer');
|
|
178
|
+
return this._withRefScriptFallback(ctx.utxos, (include) => this._buildSimpleTransfer(req, ctx, 'metadata transfer', include));
|
|
175
179
|
}
|
|
176
180
|
/**
|
|
177
181
|
* Shared non-script transfer build; assets, outputDatum and metadataJson are each
|
|
178
182
|
* handled only when present in the request.
|
|
179
183
|
*/
|
|
180
|
-
async _buildSimpleTransfer(req, ctx, label) {
|
|
184
|
+
async _buildSimpleTransfer(req, ctx, label, includeRefScriptUtxos) {
|
|
181
185
|
try {
|
|
182
186
|
this._ensureCurrentProtocolParameters(ctx);
|
|
183
187
|
if (!ctx.utxos || ctx.utxos.length === 0) {
|
|
@@ -208,7 +212,7 @@ class BuildooorTxBuilder {
|
|
|
208
212
|
const outputValue = recipient.value;
|
|
209
213
|
const outputs = [recipient];
|
|
210
214
|
// Partition: forced UTxOs become fixed inputs; rest is the coin-selection pool
|
|
211
|
-
const { forced, rest } = this._partitionForcedInputs(ctx.utxos, req.forceInputs, this._referencedKeys(ctx, req.referenceInputs), req.protectInputs);
|
|
215
|
+
const { forced, rest } = this._partitionForcedInputs(ctx.utxos, req.forceInputs, this._referencedKeys(ctx, req.referenceInputs), req.protectInputs, includeRefScriptUtxos);
|
|
212
216
|
const forcedInputs = forced.map(u => ({ utxo: this._mapMultiAssetUtxoToLedgerUtxo(u) }));
|
|
213
217
|
const candidateInputs = rest.map(u => ({ utxo: this._mapMultiAssetUtxoToLedgerUtxo(u) }));
|
|
214
218
|
// Coin selection on candidates only; forced inputs are prepended unconditionally
|
|
@@ -229,7 +233,10 @@ class BuildooorTxBuilder {
|
|
|
229
233
|
(0, tx_build_helper_1.mapBuilderError)(err, undefined, undefined, 1);
|
|
230
234
|
}
|
|
231
235
|
}
|
|
232
|
-
|
|
236
|
+
buildUnsignedMintTransaction(req, ctx) {
|
|
237
|
+
return this._withRefScriptFallback(ctx.utxos, (include) => this._buildMintTransaction(req, ctx, include));
|
|
238
|
+
}
|
|
239
|
+
async _buildMintTransaction(req, ctx, includeRefScriptUtxos) {
|
|
233
240
|
// Set once the collateral partition is known: it explains insufficient-funds rejections
|
|
234
241
|
// that the builder's own message cannot attribute.
|
|
235
242
|
let coinSelectionContext;
|
|
@@ -251,7 +258,7 @@ class BuildooorTxBuilder {
|
|
|
251
258
|
}
|
|
252
259
|
// Partition forced vs candidate UTxOs. Forced inputs are already committed;
|
|
253
260
|
// coin selection operates on the remainder only.
|
|
254
|
-
const { forced, rest } = this._partitionForcedInputs(ctx.utxos, req.forceInputs, this._referencedKeys(ctx, req.referenceInputs), req.protectInputs);
|
|
261
|
+
const { forced, rest } = this._partitionForcedInputs(ctx.utxos, req.forceInputs, this._referencedKeys(ctx, req.referenceInputs), req.protectInputs, includeRefScriptUtxos);
|
|
255
262
|
const forcedInputs = forced.map(u => ({ utxo: this._mapMultiAssetUtxoToLedgerUtxo(u) }));
|
|
256
263
|
// Funding must cover the lovelace plus, for extra outputs, their lovelace and any asset
|
|
257
264
|
// demand the mint itself does not cover.
|
|
@@ -322,8 +329,11 @@ class BuildooorTxBuilder {
|
|
|
322
329
|
(0, tx_build_helper_1.mapBuilderError)(err, undefined, coinSelectionContext, requestedOutputs);
|
|
323
330
|
}
|
|
324
331
|
}
|
|
325
|
-
|
|
326
|
-
|
|
332
|
+
buildUnsignedPlutusSpendTransaction(req, ctx) {
|
|
333
|
+
return this._withRefScriptFallback(ctx.utxos, (include) => this._buildPlutusSpendTransaction(req, ctx, include));
|
|
334
|
+
}
|
|
335
|
+
async _buildPlutusSpendTransaction(req, ctx, includeRefScriptUtxos) {
|
|
336
|
+
// See _buildMintTransaction — same collateral-partition context for rejections.
|
|
327
337
|
let coinSelectionContext;
|
|
328
338
|
let requestedOutputs;
|
|
329
339
|
try {
|
|
@@ -346,7 +356,7 @@ class BuildooorTxBuilder {
|
|
|
346
356
|
const changeAddress = cardano_ledger_ts_1.Address.fromString(req.changeAddress ?? req.senderAddress);
|
|
347
357
|
// Partition forced inputs (excluding any ref that matches the script UTxO — silently ignored)
|
|
348
358
|
const forceInputsFiltered = (req.forceInputs ?? []).filter(r => !(r.txHash === scriptUtxoRef.txHash && r.outputIndex === scriptUtxoRef.outputIndex));
|
|
349
|
-
const { forced, rest } = this._partitionForcedInputs(senderUtxos, forceInputsFiltered, this._referencedKeys(ctx, req.referenceInputs), req.protectInputs);
|
|
359
|
+
const { forced, rest } = this._partitionForcedInputs(senderUtxos, forceInputsFiltered, this._referencedKeys(ctx, req.referenceInputs), req.protectInputs, includeRefScriptUtxos);
|
|
350
360
|
const forcedInputs = forced.map(u => ({ utxo: this._mapMultiAssetUtxoToLedgerUtxo(u) }));
|
|
351
361
|
// Funding covers fee + min change (the script UTxO covers the output) plus extra outputs;
|
|
352
362
|
// assets the script UTxO already provides net out, so over-requesting is harmless.
|
|
@@ -455,7 +465,10 @@ class BuildooorTxBuilder {
|
|
|
455
465
|
* Several script inputs in one transaction, each with its own redeemer (inline validator or a
|
|
456
466
|
* reference-script UTxO), the outputs exactly in the given order and change after them.
|
|
457
467
|
*/
|
|
458
|
-
|
|
468
|
+
buildUnsignedPlutusTransaction(req, ctx) {
|
|
469
|
+
return this._withRefScriptFallback(ctx.utxos, (include) => this._buildPlutusTransaction(req, ctx, include));
|
|
470
|
+
}
|
|
471
|
+
async _buildPlutusTransaction(req, ctx, includeRefScriptUtxos) {
|
|
459
472
|
let coinSelectionContext;
|
|
460
473
|
let requestedOutputs;
|
|
461
474
|
try {
|
|
@@ -555,7 +568,7 @@ class BuildooorTxBuilder {
|
|
|
555
568
|
const scriptKeyClash = (req.protectInputs ?? []).map(keyOf).find(k => scriptKeys.has(k));
|
|
556
569
|
if (scriptKeyClash)
|
|
557
570
|
throw new errors_1.TransactionValidationError(`protectInputs ${scriptKeyClash} is also a script input`);
|
|
558
|
-
const { forced, rest } = this._partitionForcedInputs(senderUtxos, (req.forceInputs ?? []).filter(r => !scriptKeys.has(keyOf(r))), this._referencedKeys(ctx, req.referenceInputs, req.scriptInputs.flatMap(s => (s.referenceScript ? [s.referenceScript] : []))), req.protectInputs);
|
|
571
|
+
const { forced, rest } = this._partitionForcedInputs(senderUtxos, (req.forceInputs ?? []).filter(r => !scriptKeys.has(keyOf(r))), this._referencedKeys(ctx, req.referenceInputs, req.scriptInputs.flatMap(s => (s.referenceScript ? [s.referenceScript] : []))), req.protectInputs, includeRefScriptUtxos);
|
|
559
572
|
this._checkForeignForcedInputs(forced, req);
|
|
560
573
|
const forcedInputs = forced.map(u => ({ utxo: this._mapMultiAssetUtxoToLedgerUtxo(u) }));
|
|
561
574
|
// Funding covers only what the outputs need beyond the script inputs, forced inputs, mints and
|
|
@@ -1008,15 +1021,30 @@ class BuildooorTxBuilder {
|
|
|
1008
1021
|
return models;
|
|
1009
1022
|
return { ...models, ...this.rawCostModelArrays };
|
|
1010
1023
|
}
|
|
1024
|
+
/**
|
|
1025
|
+
* Run a build without the sender's reference-script UTxOs first; when that leaves the funds short
|
|
1026
|
+
* and such UTxOs exist, build again with them in the pool (their script bytes then count in the fee).
|
|
1027
|
+
*/
|
|
1028
|
+
async _withRefScriptFallback(utxos, build) {
|
|
1029
|
+
try {
|
|
1030
|
+
return await build(false);
|
|
1031
|
+
}
|
|
1032
|
+
catch (err) {
|
|
1033
|
+
if (!(err instanceof errors_1.InsufficientFundsError) || !(utxos ?? []).some(hasRefScript))
|
|
1034
|
+
throw err;
|
|
1035
|
+
logger.info('Funds short without the reference-script UTxOs — building again with them in the coin-selection pool');
|
|
1036
|
+
return build(true);
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1011
1039
|
/**
|
|
1012
1040
|
* Split UTxOs into forced inputs (must be consumed) and remaining candidates; forceInputs
|
|
1013
1041
|
* refs absent from utxos are ignored (existence is validated upstream).
|
|
1014
1042
|
*/
|
|
1015
|
-
_partitionForcedInputs(utxos, forceInputs, referenced = new Set(), protectInputs) {
|
|
1016
|
-
const
|
|
1043
|
+
_partitionForcedInputs(utxos, forceInputs, referenced = new Set(), protectInputs, includeRefScriptUtxos = false) {
|
|
1044
|
+
const excluded = (u) => !includeRefScriptUtxos && hasRefScript(u);
|
|
1017
1045
|
const key = (r) => `${r.txHash.toLowerCase()}#${r.outputIndex}`;
|
|
1018
1046
|
const protectedKeys = new Set((protectInputs ?? []).map(key));
|
|
1019
|
-
if ((!forceInputs || forceInputs.length === 0) && referenced.size === 0 && protectedKeys.size === 0 && !utxos.some(
|
|
1047
|
+
if ((!forceInputs || forceInputs.length === 0) && referenced.size === 0 && protectedKeys.size === 0 && !utxos.some(excluded)) {
|
|
1020
1048
|
return { forced: [], rest: utxos };
|
|
1021
1049
|
}
|
|
1022
1050
|
const forcedKeys = new Set((forceInputs ?? []).map(key));
|
|
@@ -1032,9 +1060,9 @@ class BuildooorTxBuilder {
|
|
|
1032
1060
|
for (const u of utxos) {
|
|
1033
1061
|
if (forcedKeys.has(key(u)))
|
|
1034
1062
|
forced.push(u);
|
|
1035
|
-
// reference inputs
|
|
1036
|
-
//
|
|
1037
|
-
else if (!referenced.has(key(u)) && !protectedKeys.has(key(u)) && !
|
|
1063
|
+
// reference inputs and protected UTxOs stay out of coin selection; UTxOs carrying a reference
|
|
1064
|
+
// script too, until the other funds prove short (then their bytes are priced into the fee)
|
|
1065
|
+
else if (!referenced.has(key(u)) && !protectedKeys.has(key(u)) && !excluded(u))
|
|
1038
1066
|
rest.push(u);
|
|
1039
1067
|
}
|
|
1040
1068
|
return { forced, rest };
|
|
@@ -1339,13 +1367,18 @@ class BuildooorTxBuilder {
|
|
|
1339
1367
|
}
|
|
1340
1368
|
/**
|
|
1341
1369
|
* Map an input UTxO's reference script to a Script: `scriptRefCbor` (Ogmios), else a full-CBOR
|
|
1342
|
-
* `scriptRef` (Koios)
|
|
1370
|
+
* `scriptRef` (Koios). With the hash and size only (Blockfrost, the tables) a placeholder of
|
|
1371
|
+
* that size stands in, so the fee counts its bytes; without the size nothing can be counted.
|
|
1343
1372
|
*/
|
|
1344
1373
|
_buildInputRefScript(utxo) {
|
|
1345
1374
|
const cbor = utxo.scriptRefCbor ?? (utxo.scriptRef && utxo.scriptRef.length > 56 ? utxo.scriptRef : undefined);
|
|
1346
1375
|
if (!cbor) {
|
|
1376
|
+
if (utxo.scriptRef && utxo.scriptRefSize) {
|
|
1377
|
+
logger.debug(`UTxO ${utxo.txHash}#${utxo.outputIndex} carries scriptRef hash and size only — fee counts ${utxo.scriptRefSize} bytes, local Plutus eval will not see the script`);
|
|
1378
|
+
return (0, reference_script_1.sizePlaceholderScript)(utxo.scriptRefSize);
|
|
1379
|
+
}
|
|
1347
1380
|
if (utxo.scriptRef)
|
|
1348
|
-
logger.
|
|
1381
|
+
logger.warn(`UTxO ${utxo.txHash}#${utxo.outputIndex} carries scriptRef hash only — its reference-script fee cannot be counted, the node may reject the transaction as underpaid`);
|
|
1349
1382
|
return undefined;
|
|
1350
1383
|
}
|
|
1351
1384
|
try {
|
|
@@ -1464,6 +1497,9 @@ class BuildooorTxBuilder {
|
|
|
1464
1497
|
stakePoolTargetNum: toUInt(pp.nOpt) ?? d.stakePoolTargetNum,
|
|
1465
1498
|
collateralPercentage: toUInt(pp.collateralPercent) ?? d.collateralPercentage,
|
|
1466
1499
|
maxCollateralInputs: toUInt(pp.maxCollateralInputs) ?? d.maxCollateralInputs,
|
|
1500
|
+
minfeeRefScriptCostPerByte: pp.minFeeRefScriptCostPerByte != null && Number(pp.minFeeRefScriptCostPerByte) > 0
|
|
1501
|
+
? Number(pp.minFeeRefScriptCostPerByte)
|
|
1502
|
+
: d.minfeeRefScriptCostPerByte,
|
|
1467
1503
|
};
|
|
1468
1504
|
const maxTxExMem = toUInt(pp.maxTxExMem);
|
|
1469
1505
|
const maxTxExSteps = toUInt(pp.maxTxExSteps);
|
|
@@ -185,7 +185,7 @@ service CardanoIndexerService @(impl: './cardano-indexer-service') {
|
|
|
185
185
|
action resumeCrawler() returns Boolean;
|
|
186
186
|
|
|
187
187
|
@title : 'Import UTxO Set'
|
|
188
|
-
@description: 'One-off import of the UTxO set that anchors crawler.utxoSet. Pause the crawler first. source=ogmios acquires the set at the crawler cursor (crawl must be at the tip); source=file loads a cardano-cli query utxo --whole-utxo dump (.json, or .ndjson from jq -c to_entries[]) taken at anchorSlot/anchorHash. source=aggregates rebuilds only the sums (LedgerAddresses, LedgerAddressAssets, LedgerAccounts) from the rows an earlier import left, e.g. after its aggregate phase failed; a cursor past the anchor is set back to it. Runs in the background; progress via getStatus().utxoSet.'
|
|
188
|
+
@description: 'One-off import of the UTxO set that anchors crawler.utxoSet. Pause the crawler first. source=ogmios acquires the set at the crawler cursor (crawl must be at the tip); source=file loads a cardano-cli query utxo --whole-utxo dump (.json, or .ndjson from jq -c to_entries[]) taken at anchorSlot/anchorHash. source=aggregates rebuilds only the sums (LedgerAddresses, LedgerAddressAssets, LedgerAccounts) from the rows an earlier import left, e.g. after its aggregate phase failed; a cursor past the anchor is set back to it. source=scripts fills the reference-script columns (utxo_referenceScriptHash, utxo_referenceScriptSize, utxo_referenceScriptType) of rows an earlier import left empty: from the same dump (filePath), or without filePath from Ogmios for the unspent rows that hold a hash but no size; the set stays active. Runs in the background; progress via getStatus().utxoSet and the log.'
|
|
189
189
|
@requires : 'Admin'
|
|
190
190
|
action importUtxoSet(source: String, filePath: String, anchorSlot: Integer64, anchorHash: String) returns UtxoSetImportResult;
|
|
191
191
|
|
|
@@ -259,9 +259,31 @@ module.exports = (srv) => {
|
|
|
259
259
|
};
|
|
260
260
|
});
|
|
261
261
|
}
|
|
262
|
+
if (data.source === 'scripts') {
|
|
263
|
+
if ((0, crawler_1.isCrawlerRunning)())
|
|
264
|
+
return (0, errors_1.rejectInvalid)(req, 'importUtxoSet', 'Crawler is running in this process — pauseCrawler first');
|
|
265
|
+
if (utxoSetImportInFlight)
|
|
266
|
+
return (0, errors_1.rejectInvalid)(req, 'importUtxoSet', 'A UTxO set import is already running');
|
|
267
|
+
return (0, backend_request_handler_1.handleRequest)(req, async (db) => {
|
|
268
|
+
const cursor = await (0, sync_state_1.readCursor)(db);
|
|
269
|
+
const { anchorSlot, anchorHash } = cursor?.utxoSet ?? {};
|
|
270
|
+
if (anchorSlot == null || !anchorHash)
|
|
271
|
+
throw new errors_1.TransactionValidationError('No imported UTxO set to fill — run importUtxoSet first.');
|
|
272
|
+
utxoSetImportInFlight = (0, utxo_set_import_1.refreshUtxoSetReferenceScripts)({ filePath: data.filePath, client: (0, server_1.getCardanoClient)() })
|
|
273
|
+
.then((r) => logger.info(`UTxO set reference scripts filled: ${r.updated} of ${r.candidates} rows`))
|
|
274
|
+
.catch((err) => logger.error(`UTxO set reference-script fill failed: ${err instanceof Error ? err.message : String(err)}`))
|
|
275
|
+
.finally(() => { utxoSetImportInFlight = null; });
|
|
276
|
+
return {
|
|
277
|
+
accepted: true,
|
|
278
|
+
anchorSlot: String(anchorSlot),
|
|
279
|
+
anchorHash,
|
|
280
|
+
message: `Reference-script columns of the UTxO set are being filled (${data.filePath ? 'from the dump' : 'from the node'}); watch the log, then resumeCrawler.`,
|
|
281
|
+
};
|
|
282
|
+
});
|
|
283
|
+
}
|
|
262
284
|
const source = data.source === 'file' || data.source === 'ogmios' ? data.source : null;
|
|
263
285
|
if (!source)
|
|
264
|
-
return (0, errors_1.rejectInvalid)(req, 'importUtxoSet', 'source must be "ogmios", "file" or "
|
|
286
|
+
return (0, errors_1.rejectInvalid)(req, 'importUtxoSet', 'source must be "ogmios", "file", "aggregates" or "scripts"', 'source');
|
|
265
287
|
if (source === 'file' && !data.filePath)
|
|
266
288
|
return (0, errors_1.rejectMissing)(req, 'importUtxoSet', 'filePath');
|
|
267
289
|
const anchorGiven = data.anchorSlot != null || !!data.anchorHash;
|
package/srv/utils/mappers.js
CHANGED
|
@@ -101,6 +101,8 @@ function mapTransactionInputs(txSeq, txInputs) {
|
|
|
101
101
|
utxoData_dataHash: input.dataHash || null,
|
|
102
102
|
utxoData_inlineDatum: input.inlineDatum || null,
|
|
103
103
|
utxoData_referenceScriptHash: input.referenceScriptHash || null,
|
|
104
|
+
utxoData_referenceScriptSize: input.referenceScriptSize ?? null,
|
|
105
|
+
utxoData_referenceScriptType: input.referenceScriptType ?? null,
|
|
104
106
|
// outpoint of the consumed UTxO
|
|
105
107
|
spentTxHash: input.txHash || null,
|
|
106
108
|
spentOutputIndex: Number.isInteger(input.outputIndex) ? input.outputIndex : null,
|
|
@@ -212,6 +214,8 @@ function mapTransactionOutputs(txSeq, txOutputs) {
|
|
|
212
214
|
utxo_dataHash: output.dataHash || null,
|
|
213
215
|
utxo_inlineDatum: output.inlineDatum || null,
|
|
214
216
|
utxo_referenceScriptHash: output.referenceScriptHash || null,
|
|
217
|
+
utxo_referenceScriptSize: output.referenceScriptSize ?? null,
|
|
218
|
+
utxo_referenceScriptType: output.referenceScriptType ?? null,
|
|
215
219
|
hasAddresses: hasAddresses,
|
|
216
220
|
hasAssets: hasAssets,
|
|
217
221
|
};
|
|
@@ -345,6 +349,8 @@ function mapAddressUtxos(addr, validFrom, validTo, addressUtxosData) {
|
|
|
345
349
|
// Hash column: Blockfrost/Ogmios give a 56-hex hash, Koios the full script CBOR, so only
|
|
346
350
|
// hash-length values are persisted
|
|
347
351
|
utxodata_referenceScriptHash: utxo.scriptRef && utxo.scriptRef.length <= 64 ? utxo.scriptRef : null,
|
|
352
|
+
utxodata_referenceScriptSize: utxo.scriptRefSize ?? null,
|
|
353
|
+
utxodata_referenceScriptType: utxo.scriptRefType ?? null,
|
|
348
354
|
lovelace: lovelace,
|
|
349
355
|
validFrom: validFrom,
|
|
350
356
|
validTo: validTo,
|
|
@@ -773,6 +779,7 @@ function mapProtocolParameters(providerParams) {
|
|
|
773
779
|
collateralPercent: providerParams.collateralPercent,
|
|
774
780
|
maxCollateralInputs: providerParams.maxCollateralInputs,
|
|
775
781
|
coinsPerUtxoSize: providerParams.coinsPerUtxoSize,
|
|
782
|
+
minFeeRefScriptCostPerByte: providerParams.minFeeRefScriptCostPerByte,
|
|
776
783
|
fetchedAt: providerParams.fetchedAt,
|
|
777
784
|
source: providerParams.source,
|
|
778
785
|
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Script } from '@harmoniclabs/cardano-ledger-ts';
|
|
2
|
+
/** Script language of a reference script as the entities expose it. */
|
|
3
|
+
export type ReferenceScriptType = 'native' | 'plutusV1' | 'plutusV2' | 'plutusV3';
|
|
4
|
+
/** Hash, ledger byte size and type of a reference script; `cbor` holds the Plutus script bytes when known. */
|
|
5
|
+
export interface ReferenceScriptInfo {
|
|
6
|
+
hash: string;
|
|
7
|
+
size: number;
|
|
8
|
+
type: ReferenceScriptType;
|
|
9
|
+
cbor: string | null;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Provider spelling of a script type to ours: Ogmios `plutus:v3` / `native`, Koios and cardano-cli
|
|
13
|
+
* `plutusV3` / `PlutusScriptV3` / `timelock` / `multisig` / `SimpleScript`. Null when unknown.
|
|
14
|
+
*/
|
|
15
|
+
export declare function normalizeScriptType(raw: unknown): ReferenceScriptType | null;
|
|
16
|
+
/** Ledger script hash: blake2b-224 over the language tag byte followed by the script bytes. */
|
|
17
|
+
export declare function referenceScriptHash(type: ReferenceScriptType, bytes: Uint8Array): string;
|
|
18
|
+
/**
|
|
19
|
+
* Bytes the ledger charges for a reference script: for Plutus the script bytestring as the
|
|
20
|
+
* compiler emits it, for a native script its CBOR.
|
|
21
|
+
*/
|
|
22
|
+
export declare function referenceScriptSize(script: Script): number;
|
|
23
|
+
/**
|
|
24
|
+
* A Plutus script object of the given ledger size and no content: lets the fee count the bytes of
|
|
25
|
+
* a reference script whose bytes are not at hand (hash and size only). Never a witness, never
|
|
26
|
+
* serialized; its hash matches no real script.
|
|
27
|
+
*/
|
|
28
|
+
export declare function sizePlaceholderScript(size: number): Script;
|
|
29
|
+
/** Hash, size and type of an Ogmios `script` object (`{ language, cbor }`); null without one. */
|
|
30
|
+
export declare function ogmiosReferenceScript(script: unknown): ReferenceScriptInfo | null;
|
|
31
|
+
/**
|
|
32
|
+
* Hash, size and type of a cardano-cli dump's `referenceScript`. The Plutus TextEnvelope
|
|
33
|
+
* `cborHex` wraps the script bytestring once more than the ledger does; the inner bytes are
|
|
34
|
+
* what the ledger hashes and charges for. A native script is given as its JSON.
|
|
35
|
+
*/
|
|
36
|
+
export declare function cliReferenceScript(ref: unknown): ReferenceScriptInfo | null;
|
|
37
|
+
//# sourceMappingURL=reference-script.d.ts.map
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.normalizeScriptType = normalizeScriptType;
|
|
4
|
+
exports.referenceScriptHash = referenceScriptHash;
|
|
5
|
+
exports.referenceScriptSize = referenceScriptSize;
|
|
6
|
+
exports.sizePlaceholderScript = sizePlaceholderScript;
|
|
7
|
+
exports.ogmiosReferenceScript = ogmiosReferenceScript;
|
|
8
|
+
exports.cliReferenceScript = cliReferenceScript;
|
|
9
|
+
const crypto_1 = require("@harmoniclabs/crypto");
|
|
10
|
+
const cbor_1 = require("@harmoniclabs/cbor");
|
|
11
|
+
const cardano_ledger_ts_1 = require("@harmoniclabs/cardano-ledger-ts");
|
|
12
|
+
const LANGUAGE_TAG = { native: 0, plutusV1: 1, plutusV2: 2, plutusV3: 3 };
|
|
13
|
+
/**
|
|
14
|
+
* Provider spelling of a script type to ours: Ogmios `plutus:v3` / `native`, Koios and cardano-cli
|
|
15
|
+
* `plutusV3` / `PlutusScriptV3` / `timelock` / `multisig` / `SimpleScript`. Null when unknown.
|
|
16
|
+
*/
|
|
17
|
+
function normalizeScriptType(raw) {
|
|
18
|
+
if (typeof raw !== 'string')
|
|
19
|
+
return null;
|
|
20
|
+
const m = /^(?:plutus:?(?:script)?v?)([1-3])$/i.exec(raw.replace(/\s+/g, ''));
|
|
21
|
+
if (m)
|
|
22
|
+
return `plutusV${m[1]}`;
|
|
23
|
+
return /^(native|timelock|multisig|simplescript)/i.test(raw) ? 'native' : null;
|
|
24
|
+
}
|
|
25
|
+
/** Ledger script hash: blake2b-224 over the language tag byte followed by the script bytes. */
|
|
26
|
+
function referenceScriptHash(type, bytes) {
|
|
27
|
+
const tagged = Uint8Array.from(Buffer.concat([Buffer.from([LANGUAGE_TAG[type]]), Buffer.from(bytes)]));
|
|
28
|
+
return Buffer.from((0, crypto_1.blake2b_224)(tagged)).toString('hex');
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Bytes the ledger charges for a reference script: for Plutus the script bytestring as the
|
|
32
|
+
* compiler emits it, for a native script its CBOR.
|
|
33
|
+
*/
|
|
34
|
+
function referenceScriptSize(script) {
|
|
35
|
+
if (script.type === 'NativeScript')
|
|
36
|
+
return script.bytes.length;
|
|
37
|
+
const n = script.bytes.length;
|
|
38
|
+
// definite-length CBOR bytestring header
|
|
39
|
+
const header = n < 24 ? 1 : n < 0x100 ? 2 : n < 0x10000 ? 3 : 5;
|
|
40
|
+
return n + header;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* A Plutus script object of the given ledger size and no content: lets the fee count the bytes of
|
|
44
|
+
* a reference script whose bytes are not at hand (hash and size only). Never a witness, never
|
|
45
|
+
* serialized; its hash matches no real script.
|
|
46
|
+
*/
|
|
47
|
+
function sizePlaceholderScript(size) {
|
|
48
|
+
for (let n = Math.max(0, size - 5); n <= size; n++) {
|
|
49
|
+
const header = n < 24 ? 1 : n < 0x100 ? 2 : n < 0x10000 ? 3 : 5;
|
|
50
|
+
if (n + header === size)
|
|
51
|
+
return cardano_ledger_ts_1.Script.plutusV3(new Uint8Array(n));
|
|
52
|
+
}
|
|
53
|
+
return cardano_ledger_ts_1.Script.plutusV3(new Uint8Array(Math.max(0, size - 1)));
|
|
54
|
+
}
|
|
55
|
+
/** Hash, size and type of an Ogmios `script` object (`{ language, cbor }`); null without one. */
|
|
56
|
+
function ogmiosReferenceScript(script) {
|
|
57
|
+
const s = script;
|
|
58
|
+
const type = normalizeScriptType(s?.language);
|
|
59
|
+
if (!type || typeof s?.cbor !== 'string' || s.cbor.length === 0)
|
|
60
|
+
return null;
|
|
61
|
+
const bytes = Buffer.from(s.cbor, 'hex');
|
|
62
|
+
return { hash: referenceScriptHash(type, bytes), size: bytes.length, type, cbor: type === 'native' ? null : s.cbor };
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Hash, size and type of a cardano-cli dump's `referenceScript`. The Plutus TextEnvelope
|
|
66
|
+
* `cborHex` wraps the script bytestring once more than the ledger does; the inner bytes are
|
|
67
|
+
* what the ledger hashes and charges for. A native script is given as its JSON.
|
|
68
|
+
*/
|
|
69
|
+
function cliReferenceScript(ref) {
|
|
70
|
+
const r = ref;
|
|
71
|
+
const script = r?.script;
|
|
72
|
+
if (!script || typeof script !== 'object')
|
|
73
|
+
return null;
|
|
74
|
+
const envelope = script;
|
|
75
|
+
const type = normalizeScriptType(envelope.type) ?? normalizeScriptType(r?.scriptLanguage);
|
|
76
|
+
try {
|
|
77
|
+
if (type && type !== 'native' && typeof envelope.cborHex === 'string') {
|
|
78
|
+
const outer = cbor_1.Cbor.parse(Buffer.from(envelope.cborHex, 'hex'));
|
|
79
|
+
if (!(outer instanceof cbor_1.CborBytes))
|
|
80
|
+
return null;
|
|
81
|
+
const inner = Buffer.from(outer.bytes);
|
|
82
|
+
return { hash: referenceScriptHash(type, inner), size: inner.length, type, cbor: inner.toString('hex') };
|
|
83
|
+
}
|
|
84
|
+
if (type === 'native' || r?.scriptLanguage?.startsWith('SimpleScript')) {
|
|
85
|
+
// the cli prints a native script as its JSON (sig / all / any / atLeast / after / before)
|
|
86
|
+
const native = cardano_ledger_ts_1.Script.native(script);
|
|
87
|
+
return { hash: native.hash.toString(), size: native.bytes.length, type: 'native', cbor: null };
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
//# sourceMappingURL=reference-script.js.map
|
package/srv/utils/types.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { LedgerProtocolParameter } from "#cds-models/odatano/cardano";
|
|
2
|
+
import type { ReferenceScriptType } from "./reference-script";
|
|
3
|
+
export type { ReferenceScriptType };
|
|
2
4
|
/** Hex-encoded string (case depends on source) */
|
|
3
5
|
export type Hex = string;
|
|
4
6
|
/** Lovelace amount; a string preserves precision above Number.MAX_SAFE_INTEGER (max supply is 4.5e16). */
|
|
@@ -21,6 +23,8 @@ export interface TxInputLine {
|
|
|
21
23
|
dataHash?: Hex | null;
|
|
22
24
|
inlineDatum?: string | null;
|
|
23
25
|
referenceScriptHash?: Hex | null;
|
|
26
|
+
referenceScriptSize?: number | null;
|
|
27
|
+
referenceScriptType?: ReferenceScriptType | null;
|
|
24
28
|
isCollateral?: boolean;
|
|
25
29
|
isReference?: boolean;
|
|
26
30
|
}
|
|
@@ -34,6 +38,8 @@ export interface TxOutputLine {
|
|
|
34
38
|
inlineDatum: string | null;
|
|
35
39
|
isCollateral: boolean;
|
|
36
40
|
referenceScriptHash?: Hex | null;
|
|
41
|
+
referenceScriptSize?: number | null;
|
|
42
|
+
referenceScriptType?: ReferenceScriptType | null;
|
|
37
43
|
}
|
|
38
44
|
/**
|
|
39
45
|
* Normalized certificate kinds; unknown types pass through as the raw source string. A Conway
|
|
@@ -138,6 +144,9 @@ export interface UTxO {
|
|
|
138
144
|
scriptRef?: Hex | null;
|
|
139
145
|
/** Full CBOR of the reference script (Plutus) when the source delivers it; builder use only. */
|
|
140
146
|
scriptRefCbor?: Hex | null;
|
|
147
|
+
/** Bytes the ledger charges for the reference script; null when the source reports the hash only. */
|
|
148
|
+
scriptRefSize?: number | null;
|
|
149
|
+
scriptRefType?: ReferenceScriptType | null;
|
|
141
150
|
inlineDatum?: string | null;
|
|
142
151
|
}
|
|
143
152
|
/** Basic block information */
|
|
@@ -307,6 +316,7 @@ export type LedgerProtocolParameters = {
|
|
|
307
316
|
collateralPercent: number | null;
|
|
308
317
|
maxCollateralInputs: number | null;
|
|
309
318
|
coinsPerUtxoSize: string | null;
|
|
319
|
+
minFeeRefScriptCostPerByte: number | null;
|
|
310
320
|
fetchedAt: string;
|
|
311
321
|
source: string;
|
|
312
322
|
};
|