Skip to content

Count each reference script UTxO once toward the fee - #505

Open
theeldermillenial wants to merge 1 commit into
Python-Cardano:mainfrom
theeldermillenial:fix/reference-script-fee-once-per-utxo
Open

theeldermillenial wants to merge 1 commit into
Python-Cardano:mainfrom
theeldermillenial:fix/reference-script-fee-once-per-utxo

Conversation

@theeldermillenial

Copy link
Copy Markdown
Contributor

Summary

TransactionBuilder counted a reference script once per redeemer that used it, not once per reference input. A transaction that spends a script UTxO and also withdraws through the same script, both resolved from one reference UTxO, got that script's size added to _ref_script_size() twice. reference_inputs is a set, so the UTxO appears in the transaction once, and the ledger charges each input's and reference input's script once. The builder overpaid the fee by the full tiered reference-script fee for every extra use, and max_tx_fee (which sizes the collateral) was inflated the same way.

_add_reference_script(utxo) now records each UTxO's script once, keyed by its input, at every site that appends one: add_input, add_script_input, add_minting_script, add_withdrawal_script and add_certificate_script. The same script held by two different UTxOs still counts twice, matching the ledger.

Example

A mainnet transaction with a 9,851-byte Plutus V3 reference script shared by a spend and a withdrawal:

before after
reference-script bytes counted 19,702 9,851
fee (with fee_buffer=100) 603,681 455,916
ledger minimum fee 455,816 455,816

Replaying the transaction through TransactionBuilder before the fix reproduces its on-chain fee, collateral, change and script data hash exactly. After the fix, the fee is the ledger minimum plus the buffer.

Tests

  • test_reference_script_shared_by_spend_and_withdrawal_counted_once: one reference UTxO used by a spend and a withdrawal counts once (failed before the fix: 38 vs 19)
  • test_same_script_in_two_reference_utxos_counted_per_utxo: the same script in two reference UTxOs counts twice
  • pytest test: 643 passed, 5 xfailed; flake8, mypy and black clean

🤖 Generated with Claude Code

https://claude.ai/code/session_01VAhmCntFmrPj5dnjaBBFeG

A reference script UTxO that several redeemers resolve their script from
(for example a pool spend and a withdraw-zero validated by the same script)
was appended to _reference_scripts once per use, so _ref_script_size()
charged its size once per use. reference_inputs is a set, so the UTxO appears
in the transaction once, and the ledger charges the scripts of a
transaction's inputs and reference inputs once per UTxO. The inflated size
overpaid the fee by the full tiered reference-script fee of every extra use,
and also inflated max_tx_fee, which sizes the collateral.

_add_reference_script(utxo) now records each UTxO's script once, keyed by its
input, at all five sites (add_input, add_script_input, add_minting_script,
add_withdrawal_script, add_certificate_script). The same script held by two
different UTxOs still counts twice, as the ledger charges it.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VAhmCntFmrPj5dnjaBBFeG
@codecov

codecov Bot commented Sep 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.80%. Comparing base (877be3a) to head (7904ea5).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #505      +/-   ##
==========================================
+ Coverage   90.79%   90.80%   +0.01%     
==========================================
  Files          37       37              
  Lines        5422     5429       +7     
  Branches      825      826       +1     
==========================================
+ Hits         4923     4930       +7     
  Misses        309      309              
  Partials      190      190              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant