[BUG] - `fromLedgerUTxO` drops native reference scripts' original bytes, so `query ref-script-size` and `transaction build` undercount them
Maintainers usually reply within 2 days
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 53/100
Research direction
Start with fromLedgerUTxO and toLedgerUTxO in Cardano/Api/Query/Internal/Type/QueryInMode.hs, then follow the native script conversions in Cardano/Api/Plutus/Internal/Script.hs. Compare how original bytes are preserved for Plutus scripts with the native script path; the expected result is that native reference scripts retain their on-chain bytes through the round trip. Check that query ref-script-size and transaction fee sizing count those bytes as the ledger does.
Written by the indexing model from the issue text.
Description
Internal/External: External
Area: Other: the size and fee of native reference scripts.
Summary
fromLedgerUTxO turns a native reference script into a SimpleScript, which keeps no bytes, and toLedgerUTxO rebuilds it in canonical CBOR (Diagnosis, below). When the script was written on chain in a non-canonical encoding, cardano-cli's counts then fall short of the ledger's:
cardano-cli query ref-script-sizereports fewer bytes than the ledger counts;transaction buildsets its fee from the same short count, and the ledger refuses the transaction;- a transaction sized from
query ref-script-sizecan exceed the reference-script size limit.
Anyone can create such an output. The ledger refuses the affected transactions, so no funds are at risk.
Steps to reproduce
The refusals below are from local devnets at protocol version 12.0 (Dijkstra). We created the script outputs with a tool that writes the script bytes as given.
transaction build
-
Create 13 outputs, each with a different native reference script that is 15,702 bytes as written on chain, with every CBOR head (integers, array lengths, and byte-string lengths) in its 8-byte form. Each script's canonical encoding is 9,124 bytes, and
query ref-script-sizereports 9,124 for each output. -
Build a transaction that references all 13 outputs:
cardano-cli dijkstra transaction build --testnet-magic 42 --socket-path <socket> --tx-in <input> --read-only-tx-in-reference <output-1> ... --read-only-tx-in-reference <output-13> --change-address <address> --out-file tx.bodyIt sets a fee of 2,754,831 lovelace, 4,444 above the minimum fee at the CLI's count of 13 × 9,124 = 118,612 bytes.
-
Sign it with
transaction signand submit it withtransaction submit. The ledger refuses it:
ShelleyTxValidationError ShelleyBasedEraDijkstra (DijkstraApplyTxError (LedgerFailure (DijkstraUtxowFailure (UtxoFailure (FeeTooSmallUTxO Mismatch (RelGTEQ) {supplied: Coin 2754831, expected: Coin 6484239}))) :| []))
The expected 6,484,239 is the minimum fee with the scripts counted as written, 13 × 15,702 = 204,126 bytes. The same transaction with that fee, and its change lowered to match, is accepted.
The reference-script size limit
-
Create 13 outputs, each with a different native reference script that is 15,871 bytes as written on chain. The scripts write some of their CBOR heads in their 8-byte form: the first writes 16 of its heads that way, and its canonical encoding is 15,748 bytes.
-
cardano-cli dijkstra query ref-script-size --tx-in <output> --testnet-magic 42 --output-jsonreports 15,748 for each output, and{"refInputScriptSize": 204724}for all 13 together. -
Submit a transaction that references all 13 outputs. The ledger counts 13 × 15,871 = 206,323 bytes, over the 204,800 limit, although the CLI's count of 204,724 is under it:
ShelleyTxValidationError ShelleyBasedEraDijkstra (DijkstraApplyTxError (LedgerFailure (DijkstraTxRefScriptsSizeTooBig Mismatch (RelLTEQ) {supplied: 206323, expected: 204800}) :| []))
The same transaction with 12 reference inputs (190,452 bytes by the ledger's count, 188,976 by the CLI's) is accepted.
A minimal script for a test
82 1b 00 00 00 00 00 00 00 00 58 1c <28-byte key hash> is a RequireSignature script with its leading 0, the constructor index, written in the 8-byte form: 40 bytes as written. Its canonical encoding, 82 00 58 1c <28-byte key hash>, is 32 bytes.
Expected behavior
A round trip through fromLedgerUTxO and toLedgerUTxO keeps each native reference script's original bytes, as it already keeps a Plutus script's. query ref-script-size and the fee from transaction build then count a reference script as the ledger does: as the creating transaction wrote it.
#1238 kept original bytes the same way in inline datums' JSON round trip, and IntersectMBO/cardano-ledger#6296 in Dijkstra's era translation of native scripts.
Diagnosis
From reading the source at master, in the order the data flows:
-
A UTxO query's result goes through
fromLedgerUTxO(call site), which converts each native script to aSimpleScript(Conway, Dijkstra).SimpleScriptholds no bytes. -
toLedgerUTxOrebuilds the script throughtoShelleyScript, which callstoAllegraTimelockin Conway ortoDijkstraNativeScriptin Dijkstra. The ledger constructors they call memoize a fresh canonical encoding. -
Two functions then sum
originalBytesSizeover the rebuilt scripts:getReferenceInputsSizeForTxIds, and the ledger'scalcMinFeeTx, whichmakeTransactionBodyAutoBalancereaches throughcalculateMinTxFee. -
In cardano-cli,
runQueryRefScriptSizeCmdsums overtoLedgerUTxOofqueryUtxo's result.transaction buildtakes its UTxO fromqueryStateForBalancedTxand passestoLedgerUTxOof it tomakeTransactionBodyAutoBalance.
The rebuilt script also hashes differently. On chain, the first script of the size-limit run hashes to 0988d691c3b026f1960b7c691024e05a073d1f1b5f9a1ecf534b86a9; its canonical encoding hashes to 6e614b818d262427d1006ca86576f8cc99b2573cdfc19dd5c2bcc0e1.
We observed these refusals only in Dijkstra, and haven't tried Conway, where cardano-api takes the same path through toAllegraTimelock. We found this while pricing reference-script fees on a devnet from query ref-script-size.
System info
- OS: Linux, x86_64.
- Local builds of these releases, against cardano-ledger
07d904eb2and, separately,37975bb0a:- cardano-cli 11.7.0.0 (
24c05829e8); - cardano-api 11.12.0.0 (
c727cd63bd); - cardano-node 11.2.1 (
90bc1cd938); - ouroboros-consensus 5.4.0.0 (
808e400545).
- cardano-cli 11.7.0.0 (
- Their only patches let them build against those ledger commits (version bounds, and adaptations to changed ledger functions), and touch nothing on the code path above. The ledger is unpatched.
- Both builds give the size-limit result. The
transaction buildresult is from the37975bb0abuild. - The code path above is the same on cardano-api master (
7a1705780f) and cardano-cli master (dabfa5aed6).
- Dominant language
- Haskell
- Stars
- 41
- Forks
- 33
- Avg merge
- 3d 3h
- Merged PRs (30d)
- 15
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from IntersectMBO/cardano-api
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
IntersectMBO/cardano-api#1434 ·
Maintainers usually reply within 2 days
-
`Prelude.maximum: empty list` crash in fee estimation for empty `RequireAnyOf` (`{"type": "any", "scripts": []}`) scriptsPossibly taken @Jimbo4350 claimed this 8 days ago. Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
IntersectMBO/cardano-api#1424 ·
Maintainers usually reply within 2 days
-
type: enhancement
Difficulty 4/5 3-5 days Newbie friendliness 45/100
IntersectMBO/cardano-api#1423 ·
Maintainers usually reply within 2 days
-
Add a dedicated transaction viewing type to the experimental APIPossibly taken @palas claimed this 21 days ago. Opentype: enhancement
IntersectMBO/cardano-api#1416 · 1 assignee ·
Maintainers usually reply within 2 days
-
Dijkstra era type: enhancement
Difficulty 5/5 Over a week Newbie friendliness 35/100
IntersectMBO/cardano-api#1386 ·
Maintainers usually reply within 2 days
All issues in IntersectMBO/cardano-api
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
haskell/bytestring#730 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
seal-harness/seal-harness#399 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
fmidue/codeworld-tasks#124 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
reflex-frp/reflex#563 ·
-
The codecov job never gets a runner on macos-26 and is cancelledPossibly taken @yegor256 claimed this 1 day ago. Openbug
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
objectionary/phino#2524 · 1 comment ·
Maintainers usually reply within 1 day