[BUG] - `fromLedgerUTxO` drops native reference scripts' original bytes, so `query ref-script-size` and `transaction build` undercount them

Open
#1,431 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
53/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
haskell
Domain
backend, databases

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-size reports fewer bytes than the ledger counts;
  • transaction build sets its fee from the same short count, and the ledger refuses the transaction;
  • a transaction sized from query ref-script-size can 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
  1. 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-size reports 9,124 for each output.

  2. 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.body
    

    It 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.

  3. Sign it with transaction sign and submit it with transaction 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
  1. 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.

  2. cardano-cli dijkstra query ref-script-size --tx-in <output> --testnet-magic 42 --output-json reports 15,748 for each output, and {"refInputScriptSize": 204724} for all 13 together.

  3. 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:

  1. A UTxO query's result goes through fromLedgerUTxO (call site), which converts each native script to a SimpleScript (Conway, Dijkstra). SimpleScript holds no bytes.

  2. toLedgerUTxO rebuilds the script through toShelleyScript, which calls toAllegraTimelock in Conway or toDijkstraNativeScript in Dijkstra. The ledger constructors they call memoize a fresh canonical encoding.

  3. Two functions then sum originalBytesSize over the rebuilt scripts: getReferenceInputsSizeForTxIds, and the ledger's calcMinFeeTx, which makeTransactionBodyAutoBalance reaches through calculateMinTxFee.

  4. In cardano-cli, runQueryRefScriptSizeCmd sums over toLedgerUTxO of queryUtxo's result. transaction build takes its UTxO from queryStateForBalancedTx and passes toLedgerUTxO of it to makeTransactionBodyAutoBalance.

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 07d904eb2 and, 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).
  • 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 build result is from the 37975bb0a build.
  • 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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from IntersectMBO/cardano-api

All issues in IntersectMBO/cardano-api

Similar issues

More Haskell issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.