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

Aperta
#1,431 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
53/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
haskell
Ambito
backend, databases

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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).
Lingua principale
Haskell
Stelle
41
Fork
33
Merge medio
3g 3h
PR unite (30g)
15

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di IntersectMBO/cardano-api

Tutte le issue di IntersectMBO/cardano-api

Issue simili

Altre issue su Haskell

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.